Unity color gradient shader

In Unity, you can use Shader to achieve color gradient effects.

To achieve this effect, you need to create a new Shader file in Unity. You can use Surface Shader to simplify this process, because Surface Shader automatically generates color buffers and basic lighting for you.

In the Shader, you need to define some variables to store the start color and end color of the color gradient. You can then use a function called "lerp" to interpolate the two colors. The Lerp function interpolates between two colors based on a weight value.

Here is the code for a sample Shader:

Shader"Custom/ColorGradient" {
    Properties {
        _Color1 ("Start Color", Color) = (1,1,1,1)
        _Color2 ("End Color", Color) = (0,0,0,1)
        _Weight ("Weight", Range(0,1)) = 0.5
    }

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

        CGPROGRAM
        #pragma surface surf BlinnPhong
        #pragma target 3.0

        struct Input {
            float2 uv_MainTex;
        };

        sampler2D _MainTex;
        fixed4 _Color1;
        fixed4 _Color2;
        fixed _Weight;

        void surf (Input IN, inout SurfaceOutput o) {
            fixed4 c = lerp(_Color1, _Color2, _Weight);
            o.Albedo = c.rgb;
            o.Alpha = c.a;
        }
        ENDCG
    }
}

You can use the _Color1 and _Color2 variables in this Shader to set the starting color and ending color of the color gradient. The _Weight variable can be used to control the progress of the color gradient.

you can

Guess you like

Origin blog.csdn.net/weixin_35754676/article/details/128866585