创建着色器为什么编辑器中编译的代码显示错误标识符“_FillAmount”?

问题描述 投票:0回答:1

我检查了很多次,变量已经声明了,但仍然找不到它。

填充量应设置圆圈将被填充的程度,例如,值为 0 时,圆圈将像环形,值为 100 时,圆圈将被完全填充。

“UnityLibrary/2D/Patterns/Circles”中的着色器错误:第 63 行未声明标识符“_FillAmount”(在 d3d11 上)

错误出现在第63行:

// draws circle pattern
Shader "UnityLibrary/2D/Patterns/Circles"
{
    Properties
    {
        _Color ("Color", Color) = (1,1,1,1)
        _CircleSize("Size", Range(0,1)) = 0.5
        _Circles("Amount", Range(1,64)) = 8
        _FillAmount("Fill", Range(1, 100)) = 3
    }

    SubShader
    {
        Tags { "RenderType"="Opaque" }
        LOD 200

        CGPROGRAM
        #pragma surface surf Standard fullforwardshadows vertex:vert
        #pragma target 3.0

        struct Input
        {
            float2 texcoord : TEXCOORD0;
        };

        sampler2D _MainTex;
        float _Circles;
        float _CircleSize;

        half _Glossiness;
        half _Metallic;
        fixed4 _Color;

        UNITY_INSTANCING_BUFFER_START(Props)
        UNITY_INSTANCING_BUFFER_END(Props)

        // https://thebookofshaders.com/09/
        float2 tile(float2 _st, float _zoom)
        {
            _st *= _zoom;
            return frac(_st);
        }

        // https://thebookofshaders.com/07/
        float Circle(float2 _st, float _radius)
        {
            float2 dist = _st - float2(0.5, 0.5);
            return 1. - smoothstep(_radius - (_radius * 0.01), _radius + (_radius * 0.01), dot(dist, dist) * 4.0);
        }

        void vert(inout appdata_full v, out Input o) 
        {
            UNITY_INITIALIZE_OUTPUT(Input, o);
            o.texcoord = v.texcoord;
        }

        void surf (Input IN, inout SurfaceOutputStandard o)
{
    float2 st = IN.texcoord.xy;
    float radius = _CircleSize;

    // Calculate the threshold for filling based on _FillAmount
    float fillThreshold = radius * (_FillAmount / 100.0);

    // Calculate the circle value using the modified threshold
    float c = Circle(tile(st, round(_Circles)), radius);

    // Apply the fill threshold to control the filling
    if (c >= fillThreshold) {
        o.Albedo = _Color;
    } else {
        o.Albedo = float3(0, 0, 0); // Set the color to black for the unfilled part
    }

    o.Metallic = _Metallic;
    o.Smoothness = _Glossiness;
    o.Alpha = 1.0; // Always fully opaque
}
        ENDCG
    }
    FallBack "Diffuse"
}
unity-game-engine shader
1个回答
0
投票

您需要将

float _FillAmount
添加到您的
subshader

// draws circle pattern
Shader "UnityLibrary/2D/Patterns/Circles"
{
    Properties
    {
        _Color ("Color", Color) = (1,1,1,1)
        _CircleSize("Size", Range(0,1)) = 0.5
        _Circles("Amount", Range(1,64)) = 8
        _FillAmount("Fill", Range(1, 100)) = 3
    }

    SubShader
    {
        Tags { "RenderType"="Opaque" }
        LOD 200

        CGPROGRAM
        #pragma surface surf Standard fullforwardshadows vertex:vert
        #pragma target 3.0

        struct Input
        {
            float2 texcoord : TEXCOORD0;
        };

        sampler2D _MainTex;
        float _Circles;
        float _CircleSize;
        float _FillAmount;  // Add this line to declare _FillAmount

        half _Glossiness;
        half _Metallic;
        fixed4 _Color;

        UNITY_INSTANCING_BUFFER_START(Props)
        UNITY_INSTANCING_BUFFER_END(Props)

        // https://thebookofshaders.com/09/
        float2 tile(float2 _st, float _zoom)
        {
            _st *= _zoom;
            return frac(_st);
        }

        // https://thebookofshaders.com/07/
        float Circle(float2 _st, float _radius)
        {
            float2 dist = _st - float2(0.5, 0.5);
            return 1. - smoothstep(_radius - (_radius * 0.01), _radius + (_radius * 0.01), dot(dist, dist) * 4.0);
        }

        void vert(inout appdata_full v, out Input o) 
        {
            UNITY_INITIALIZE_OUTPUT(Input, o);
            o.texcoord = v.texcoord;
        }

        void surf (Input IN, inout SurfaceOutputStandard o)
{
    float2 st = IN.texcoord.xy;
    float radius = _CircleSize;

    // Calculate the threshold for filling based on _FillAmount
    float fillThreshold = radius * (_FillAmount / 100.0);

    // Calculate the circle value using the modified threshold
    float c = Circle(tile(st, round(_Circles)), radius);

    // Apply the fill threshold to control the filling
    if (c >= fillThreshold) {
        o.Albedo = _Color;
    } else {
        o.Albedo = float3(0, 0, 0); // Set the color to black for the unfilled part
    }

    o.Metallic = _Metallic;
    o.Smoothness = _Glossiness;
    o.Alpha = 1.0; // Always fully opaque
}
        ENDCG
    }
    FallBack "Diffuse"
}
© www.soinside.com 2019 - 2024. All rights reserved.