【问题标题】:Clip the region in Ellipse shape using ShaderLab in Unity在 Unity 中使用 ShaderLab 将区域裁剪为椭圆形状
【发布时间】:2020-03-11 19:18:15
【问题描述】:

我正在尝试使用 Unity 中的着色器绘制椭圆体

按照步骤
1. 我在一些互联网教程的帮助下编写了一个自定义着色器
2.在材质上添加了shader
3. 将该材质应用到对象上

Shader "Custom/HoleMaker" {
    Properties{
            _Position("Position", Vector) = (0,0,0,0)
            _Radius("Radius", Range(0,1)) = 0.01
            _Color("Color", Color) = (1,1,1,1)
            _CutawayColor("Cutaway Color", Color) = (1,1,1,1)
    }
            SubShader{
                    Tags { "RenderType" = "Opaque"}
                    LOD 200
                    Cull Front

                    CGPROGRAMtypes
                    #pragma surface surf MonoColor fullforwardshadows vertex:vert

                    // Use shader model 3.0 target, to get nicer looking lighting
                    #pragma target 3.0

                    fixed4 _CutawayColor;

                    float4 _Position;
                    float _Radius;

                    struct Input
                    {
                            float2 uv_MainTex;
                            float3 worldPos;
                    };

                    void vert(inout appdata_full v, out Input o)
                    {
                            UNITY_INITIALIZE_OUTPUT(Input, o);
                            v.normal *= -1;
                    }

                    half4 LightingMonoColor(SurfaceOutput s, half3 lightDir, half atten) {
                            return _CutawayColor;
                    }

                    void surf(Input IN, inout SurfaceOutput o)
                    {

                            //spherical clipping
                            float dist = distance(IN.worldPos.xyz, _Position.xyz);
                            if (dist < _Radius)
                                    clip(-1);


                            o.Albedo = _CutawayColor.rgb;
                    }
                    ENDCG
            }
                    FallBack "Diffuse"
   }

此代码用于制作圆形,但当我尝试使用函数 clip() 制作椭圆时,它不起作用。根据文档,clip(float4 x) 也可用于剪辑表面。变量 _Radius 在世界空间中形成了一个完美的球体,但是当我尝试使用 _Radius.x 和 _Radius.y 时,代码不起作用。请参考随附的图片。

【问题讨论】:

    标签: unity3d glsl shader shaderlab


    【解决方案1】:

    _Radius.x_Radius.y 中断着色器,因为 _Radiusfloat。它没有xy 成员。 _Radius.y 怎么可能有值?

    相反,请考虑添加一个Scale 属性,然后将世界位置与_Position 之间的差异缩放该数量,然后再将差异的大小与_radius 进行比较:

    Shader "Custom/HoleMaker" {
        Properties{
                _Position("Position", Vector) = (0,0,0,0)
                _Scale("Scale", Vector) = (1,1,1,0)
                _Radius("Radius", Range(0,1)) = 0.01
                _Color("Color", Color) = (1,1,1,1)
                _CutawayColor("Cutaway Color", Color) = (1,1,1,1)
        }
                SubShader{
                        Tags { "RenderType" = "Opaque"}
                        LOD 200
                        Cull Front
    
                        CGPROGRAMtypes
                        #pragma surface surf MonoColor fullforwardshadows vertex:vert
    
                        // Use shader model 3.0 target, to get nicer looking lighting
                        #pragma target 3.0
    
                        fixed4 _CutawayColor;
    
                        float4 _Position;
                        float4 _Scale;
                        float _Radius;
    
                        struct Input
                        {
                                float2 uv_MainTex;
                                float3 worldPos;
                        };
    
                        void vert(inout appdata_full v, out Input o)
                        {
                                UNITY_INITIALIZE_OUTPUT(Input, o);
                                v.normal *= -1;
                        }
    
                        half4 LightingMonoColor(SurfaceOutput s, half3 lightDir, half atten) {
                                return _CutawayColor;
                        }
    
                        void surf(Input IN, inout SurfaceOutput o)
                        {
    
                                //spherical clipping
                                float dist = length(_Scale.xyz * IN.worldPos.xyz - _Position.xyz);
                                if (dist < _Radius)
                                        clip(-1);
    
    
                                o.Albedo = _CutawayColor.rgb;
                        }
                        ENDCG
                }
                        FallBack "Diffuse"
       }
    

    【讨论】:

      猜你喜欢
      • 2017-07-05
      • 1970-01-01
      • 2014-09-07
      • 2014-05-01
      • 2011-09-25
      • 2012-04-21
      • 1970-01-01
      • 1970-01-01
      • 2014-09-10
      相关资源
      最近更新 更多