I'm trying to make a custom shader for UI sprites, which can mask every sprite with a single mask texture. texcoord0 in the shader is used by the atlas and the output coordinates are for the sprites. So I need texcoord1 to let the mask texture to be mapped with different coordinate, which is back to 0..1 uv space. Any advice on how to calculate the texcoord1 for the mask texture? Code (CSharp): Shader "ShaderResetUV" { Properties { _MainTex ("_MainTex RGBA", 2D) = "white" {} _MaskTex ("_MaskTex RGBA", 2D) = "white" {} } Category { Tags { "Queue"="Transparent" "IgnoreProjector"="True" "RenderType"="Transparent" } Blend SrcAlpha OneMinusSrcAlpha Cull Off Lighting Off ZWrite Off Lighting Off SubShader { Pass { CGPROGRAM #pragma vertex vert #pragma fragment frag #include "UnityCG.cginc" sampler2D _MainTex; fixed4 _MainTex_ST; sampler2D _MaskTex; fixed4 _MaskTex_ST; struct appdata_t { fixed4 vertex : POSITION; fixed2 texcoord : TEXCOORD0; fixed4 color : COLOR; }; struct v2f { fixed4 vertex : SV_POSITION; fixed2 texcoord : TEXCOORD0; fixed2 texcoord1 : TEXCOORD1; fixed4 color : COLOR; }; v2f vert (appdata_t v) { v2f o; o.vertex = mul(UNITY_MATRIX_MVP, v.vertex); o.texcoord = TRANSFORM_TEX(v.texcoord, _MainTex); //Reset UV o.texcoord1 = o.texcoord; // WHAT TO DO WITH THIS.. // o.color = v.color; return o; } fixed4 frag (v2f i) : SV_Target { fixed4 c = tex2D(_MainTex,i.texcoord); fixed mask = tex2D(_MaskTex,i.texcoord1).a; c.rgb = c.rgb*i.color.rgb; c.a = c.a*mask*i.color.a; return c; } ENDCG } } } }
Have you tried using TEXCOORD1 from the appdata instead of TEXCOORD0? I have no idea if it'll be any different, but worth a try.
So... there is a PositionAsUV1 Component in Unity. It will give you a BUNCH OF LIES, but might work if you have the right combo of Image mode, and anchor settings... If it doesn't you'll have to write your own Component to set the UV's on the verts of your UI renderer. This is non-trivial, as you'll have to account for the Image mode and anchoring stuff returning you wacky data, but it can be done.