UV_2 coordinates has no affect.

I am attempting to write a shader that has an alpha mask texture overlay. The _mask texture’s UV coordinates can be rotated, but I need the _Main texture’s UV coordinates to be independent of the mask.

In the example below, I am trying to rotate the stars based on _Angle, but the yellow lines should not move or rotate.

This is the shader I have written so far (also, please understand that I am just starting to learn shader programming!!).

My issue is that I tried storing rotated UV coordinates in v2f.uv_1 variable,

// Rotation around pivot
float2 uv = v.texcoord0.xy - pivot;
o.uv_1 = mul(rot, uv) + pivot;

I then try retrieving them in my fragment program:

fixed4 texInjury = tex2D(_MaskTexture, i.uv_1);
col.a *= texInjury.r;

But the alpha mask does not rotate.
I have tested the roation math on the primary UV coordinates and the yellow lines do rotate, so I know that is working.

Could someone please review my shader program and help me spot what I am doing wrong?

Thanks!

Shader "Custom/RotateOverlay"
{
    Properties
    {
         _Angle ("Angle", Range(-5.0,  5.0)) = 0.0

        _MainTexture("Main Texture", 2D) = "black" {}
        _MaskTexture("Mask Texture", 2D) = "black" {}
    }

    SubShader {
        Tags { "Queue"="Transparent" "RenderType"="Transparent" }
 
        Pass {
            Cull Back
            ZWrite Off
            Blend srcAlpha OneMinusSrcAlpha
         
            CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag
            #pragma fragmentoption ARB_precision_hint_fastest
 
            #include "UnityCG.cginc"

            float _Angle;

            sampler2D _MaskTexture;
            float4 _MaskTexture_ST;

            sampler2D _MainTexture;
            float4 _MainTexture_ST;
         
            // Struct Input || VertOut
            struct appdata {
                float2 uv_0 : TEXCOORD0;
                half4 vertex : POSITION;
                half2 texcoord0 : TEXCOORD0;
                fixed4 color : COLOR;

                float2 uv_1 : TEXCOORD1;
            };
         
            //VertIn
            struct v2f {
                float2 uv_0 : TEXCOORD0;
                half4 pos : POSITION;
                half2 texcoord0 : TEXCOORD0;
                fixed4 color : COLOR;

                float2 uv_1 : TEXCOORD1;
            };

            v2f vert (appdata v)
            {
                v2f o;
                o.pos = UnityObjectToClipPos (v.vertex);
                o.color = v.color;

                o.texcoord0 = TRANSFORM_TEX(v.texcoord0, _MainTexture);
                 o.uv_0 = TRANSFORM_TEX(v.uv_0, _MainTexture);


                // Pivot
                float2 pivot = float2(0.5, 0.5);
                // Rotation Matrix
                float cosAngle = cos(_Angle);
                float sinAngle = sin(_Angle);
                float2x2 rot = float2x2(cosAngle, -sinAngle, sinAngle, cosAngle);
 
                // Rotation around pivot
                float2 uv = v.texcoord0.xy - pivot;
                o.uv_1 = mul(rot, uv) + pivot;
                
                 return o;
            }

 
            fixed4 frag (v2f i) : COLOR
            {
                fixed4 col;

                fixed4 tex = tex2D(_MainTexture, i.texcoord0);

                col.rgb = i.color.rgb * tex.rgb;
                col.a = i.color.a * tex.a;

                fixed4 texInjury = tex2D(_MaskTexture, i.uv_1);
                col.a *= texInjury.r;
                             
                return col;             
            }
            ENDCG        
        }
    }
    FallBack "Diffuse"
}

You have used TEXCOORD0 twice?

  • float2 uv_0 : TEXCOORD0;
  • half2 texcoord0 : TEXCOORD0;
1 Like

Thanks for pointing that out!! By removing that redundancy everything started working!! I think I copied and pasted two totally different tutorials together and somehow missed that entirely!

Here’s the fixed shader code:

Shader "Custom/RotateOverlay"
{
    Properties
    {
         _Angle ("Angle", Range(-5.0,  5.0)) = 0.0

        _MainTexture("Main Texture", 2D) = "black" {}
        _MaskTexture("Mask Texture", 2D) = "black" {}
    }

    SubShader {
        Tags { "Queue"="Transparent" "RenderType"="Transparent" }
 
        Pass {
            Cull Back
            ZWrite Off
            Blend srcAlpha OneMinusSrcAlpha
   
            CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag
            #pragma fragmentoption ARB_precision_hint_fastest
 
            #include "UnityCG.cginc"

            float _Angle;

            sampler2D _MaskTexture;
            float4 _MaskTexture_ST;

            sampler2D _MainTexture;
            float4 _MainTexture_ST;
   
            // Struct Input || VertOut
            struct appdata {
                half4 vertex : POSITION;
                fixed4 color : COLOR;
                half2 uv_0 : TEXCOORD0;
                half2 uv_1 : TEXCOORD1;
            };
   
            //VertIn
            struct v2f {
                half4 pos : POSITION;     
                fixed4 color : COLOR;
                half2 uv_0 : TEXCOORD0;
                half2 uv_1 : TEXCOORD1;
            };

            v2f vert (appdata v)
            {
                v2f o;
                o.pos = UnityObjectToClipPos (v.vertex);
                o.color = v.color;

                o.uv_0 = TRANSFORM_TEX(v.uv_0, _MainTexture);

                 // Pivot
                float2 pivot = float2(0.5, 0.5);
                // Rotation Matrix
                float cosAngle = cos(_Angle);
                float sinAngle = sin(_Angle);
                float2x2 rot = float2x2(cosAngle, -sinAngle, sinAngle, cosAngle);
 
                 // Rotation around pivot
                float2 uv = v.uv_0.xy - pivot;
                o.uv_1 = mul(rot, uv) + pivot;
          
                return o;
            }

 
            fixed4 frag (v2f i) : COLOR
            {
                fixed4 col;
                fixed4 tex = tex2D(_MainTexture, i.uv_0);

                col.rgb = i.color.rgb * tex.rgb;
                col.a = i.color.a * tex.a;

                fixed4 texInjury = tex2D(_MaskTexture, i.uv_1);
                col.a *= texInjury.r;
                       
                return col;       
            }
            ENDCG  
        }
    }
    FallBack "Diffuse"
}