UnityShader global pass value is invalid

1 Introduction

        In the development of Unity shader, it is necessary to use C# to pass values ​​to the shader code. When I used the global value to pass values ​​to the shader, I found that it did not work. Here are the problems that are easy to ignore.

2. Shader transfer parameters

        There are two ways for C# to pass parameters to Shader in unity:

        1. SetFloatXXX series for specific Material (this is an instance method)

        2. SetFloatXXX series for all Shaders (this is a static method)

What I use here is the second method.

3. Problem recovery

        1. First, for testing, I wrote a simple shader example that changes the transparency, using the value of transparency_AlphaScale as the passed parameter. code show as below:

Shader "PointCloud" {
    Properties{
        _AlphaScale("Alpha Scale", Range(0, 1)) = 1    //控制整体的透明度
    }
    SubShader{
        Tags {"Queue" = "Transparent" "IgnoreProjector" = "True" "RenderType" = "Transparent"}
        Pass{
            Tags { "LightMode" = "ForwardBase" }

            ZWrite Off    //关闭深度写入
            Blend SrcAlpha OneMinusSrcAlpha    //设置混合模式,为普通的方式
        LOD 200

        CGPROGRAM
        #pragma vertex vert
        #pragma fragment frag
        #include "UnityCG.cginc"

        fixed _AlphaScale;              //有默认的uniform

        struct VertexInput {
            float4 v : POSITION;
            float4 color: COLOR;
        };

        struct VertexOutput {
            float4 pos : SV_POSITION;
            float4 col : COLOR;
        };

        VertexOutput vert(VertexInput v) {

            VertexOutput o;
            o.pos = UnityObjectToClipPos(v.v);
            o.col = v.color;

            return o;
        }

        float4 frag(VertexOutput o) : COLOR{
            return o.col* _AlphaScale;
        }
        ENDCG
        }
    }

}

        2. Create a cube in Unity, create a new material, assign this Shader to this material, and then replace the material ball of the cube itself with this material.

        3. Create a Slider component in Unity, and use the Slider component to control the value of transparency. Create a new C# script and drag the script to the Slider component. code show as below:

private Slider slider;

    private void Awake()
    {
        slider = GetComponent<Slider>();
    }

    private void Start()
    {
        slider.onValueChanged.AddListener(SliderClick);
    }

    private void SliderClick(float value)
    {
        Shader.SetGlobalFloat("_AlphaScale", value);
    }

        4. After running, drag the Slider and find that the transparency of the cube has not changed.

4. Solve problems

        First of all, there is no problem with the slider passing values ​​to the shader. The problem is that _AlphaScale is defined in Properties, which needs to be modified on the material panel, which is equivalent to the conflict between the global modification of the parameters I passed and the modification of material properties, and the Properties structure The parameters in the shader are passed from the CPU to the GPU, and the parameters in the shader code cannot be modified. So the solution is to delete or comment out _AlphaScale in Properties.

    Properties{
        //_AlphaScale("Alpha Scale", Range(0, 1)) = 1    //控制整体的透明度
    }

In this way, the transparency can be changed correctly with the slider.

Guess you like

Origin blog.csdn.net/qq_26540577/article/details/126539574