1

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

按照步骤
1. 我在一些互联网教程的帮助下编写了一个自定义着色器
2. 在材质上添加了着色器
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 时,代码不起作用。请参阅随附的图片。

在此处输入图像描述

4

1 回答 1

2

_Radius.x_Radius.y打破着色器,因为_Radius它是一个float. 它没有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"
   }
于 2020-03-11T19:50:10.350 回答