Search Unity

Reset UV back to 0..1 space

Discussion in 'Shaders' started by Catinightmare, Oct 29, 2016.

  1. Catinightmare

    Catinightmare

    Joined:
    Sep 18, 2013
    Posts:
    11
    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? question.jpg

    Code (CSharp):
    1. Shader "ShaderResetUV"
    2. {
    3. Properties
    4. {
    5.     _MainTex ("_MainTex RGBA", 2D) = "white" {}
    6.     _MaskTex ("_MaskTex RGBA", 2D) = "white" {}
    7. }
    8.  
    9. Category
    10. {
    11.     Tags { "Queue"="Transparent" "IgnoreProjector"="True" "RenderType"="Transparent" }
    12.     Blend SrcAlpha OneMinusSrcAlpha
    13.     Cull Off Lighting Off ZWrite Off
    14.     Lighting Off
    15.    
    16.     SubShader
    17.     {
    18.         Pass
    19.         {
    20.             CGPROGRAM
    21.             #pragma vertex vert
    22.             #pragma fragment frag
    23.  
    24.             #include "UnityCG.cginc"
    25.  
    26.             sampler2D _MainTex;
    27.             fixed4 _MainTex_ST;
    28.             sampler2D _MaskTex;
    29.             fixed4 _MaskTex_ST;
    30.  
    31.             struct appdata_t
    32.             {
    33.                 fixed4 vertex : POSITION;
    34.                 fixed2 texcoord : TEXCOORD0;
    35.                 fixed4 color : COLOR;
    36.             };
    37.  
    38.             struct v2f
    39.             {
    40.                 fixed4 vertex : SV_POSITION;
    41.                 fixed2 texcoord : TEXCOORD0;
    42.                 fixed2 texcoord1 : TEXCOORD1;
    43.                 fixed4 color : COLOR;
    44.             };
    45.  
    46.             v2f vert (appdata_t v)
    47.             {
    48.                 v2f o;
    49.                 o.vertex = mul(UNITY_MATRIX_MVP, v.vertex);
    50.                 o.texcoord = TRANSFORM_TEX(v.texcoord, _MainTex);
    51.  
    52.                 //Reset UV
    53.                 o.texcoord1 = o.texcoord; // WHAT TO DO WITH THIS..
    54.                 //
    55.  
    56.                 o.color = v.color;
    57.                 return o;
    58.             }
    59.            
    60.             fixed4 frag (v2f i) : SV_Target
    61.             {
    62.                 fixed4 c = tex2D(_MainTex,i.texcoord);
    63.                 fixed mask = tex2D(_MaskTex,i.texcoord1).a;
    64.  
    65.                 c.rgb = c.rgb*i.color.rgb;
    66.                 c.a = c.a*mask*i.color.a;
    67.  
    68.                 return c;
    69.             }
    70.             ENDCG
    71.         }
    72.     }  
    73. }
    74. }
    75.  
     
    Last edited: Oct 29, 2016
  2. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,352
    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.
     
  3. Catinightmare

    Catinightmare

    Joined:
    Sep 18, 2013
    Posts:
    11
    Yes, I tried. the input texcoord1 value is just the same as texcoord0....
     
  4. hnakamura

    hnakamura

    Joined:
    Dec 30, 2016
    Posts:
    4
    I think this is great idea! Could you get succeed?
     
  5. brownboot67

    brownboot67

    Joined:
    Jan 5, 2013
    Posts:
    375
    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.