Add Mask To Shader

Can someone help me add a mask to the gradient in this shader? I’m new to shaders and have no idea what I’m doing.

Properties
    {
        _Tex ("Texture", 2D) = "white" {}
        _Gradient ("Gradient texture",
        2D) = "white" {}
        _Speed ("Speed", Float) = 1
        _Glow ("Glow", Range (0, 1)) = 0
    }
    SubShader
    {
        Tags { "RenderType"="Opaque" }
        LOD 100

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

            struct appdata
            {
                float4 vertex : POSITION;
                fixed4 color : COLOR;
                float2 uv : TEXCOORD0;
                UNITY_VERTEX_INPUT_INSTANCE_ID
            };

            struct v2f
            {
                float2 uv : TEXCOORD0;
                float4 vertex : SV_POSITION;
                half4 color : COLOR;
            };

            float _Speed;
            float _Glow;

            sampler2D _Tex;
            float4 _Tex_ST;
            sampler2D _Gradient;
           
            v2f vert (appdata v)
            {
                v2f o;
                o.vertex = UnityObjectToClipPos(v.vertex);
                o.uv = v.uv;
                o.color = v.color;
                return o;
            }
           
            fixed4 frag (v2f i) : SV_Target
            {
                // sample the texture
                fixed4 col = tex2D(_Gradient,float2(_Time.y * _Speed,0.0)) * tex2D(_Tex, TRANSFORM_TEX(i.uv, _Tex));

                return col * float4(1.0,1.0,1.0,_Glow) * i.color;
            }
            ENDCG
        }
    }
}

Hi and welcome,

What do you want to mask? Please explain. But I assume you might want to somehow overlay the gradient using a mask?

Currently, the gradient overlays the entire surface. I want to mask it to only overlay on certain parts of the surface using a map texture.

Generally it’s easiest to use lerp to interpolate between two textures values depending on a mask texture.
Something like this (you need to do the other changes needed like add those texture samplers.) I would also start from the default Unlit shader, I don’t know where you got that from but it looks bit weird.

fixed4 col = tex2D(_MainTex, i.uv);
fixed4 gradient = tex2D(_Gradient, i.uv);
fixed mask = tex2D(_Mask, i.uv).r;
fixed4 result = lerp(col, gradient, mask);

Of course you could write back to col to avoid having unnecessary resources allocated in the shader like my result there. But I just did that for the sake of clarity.

So - that mask texture’s red channel is used as a mask (it could also be alpha channel from your main texture or the gradient texture, for example.) First the color texture is sampled using the input UVs, then gradient texture is sampled using same UVs and after that mask is sampled too. Then I just generate that “result” using intrinsic lerp function to mix those two colors I sampled based on that mask. And finally that result is what is returned as the product of the fragment program.

1 Like