Search Unity

CanvasRenderer + shader clipped Material

Discussion in 'UGUI & TextMesh Pro' started by etoreo, Oct 19, 2014.

  1. etoreo

    etoreo

    Joined:
    Apr 11, 2012
    Posts:
    104
    I have spent all day on this and can't figure out the math to make this work.

    I have a custom Material that includes the shader seen below. The shader makes it so that the mesh will not draw (because the alpha is set to 0) outside of the range specified.

    When I use the shader on game object without a CanvasRenderer and just a normal Mesh + Material, I get the behavior I expect (although it seems to like local coordinates, not world). When I use a CanvasRenderer and set the Material on it the clipping works, it just clips in places I can't understand and seems to be tied to the world coordinates.

    Is there something I am not accounting for? Is my shader messed up?

    Basic Game Objects with Mesh (works):
    Here is an example of the basic Mesh (the two lines are the meshes, each with the custom Material applied and setup for that plot). As you can see, the lines are clipped to the plot background (they extend out in both directions, but you can't see it because the shader is working "correctly"):


    CanvasRender (broken):
    Here is an example of the same shader applied to the plot lines using a CanvasRenderer:


    Shader:
    Code (CSharp):
    1. Shader "Test/Simple Color With Alpha Clip"
    2. {
    3.    Properties
    4.    {
    5.       _TintColor ("Tint Color", Color) = (1,1,1,1)
    6.       _Clipping ("Clipping", Vector) = (-1, -1, 1, 1)
    7.    }
    8.  
    9.    SubShader
    10.    {
    11.       Lighting Off
    12.       ZWrite Off
    13.       Cull Back
    14.       Blend SrcAlpha OneMinusSrcAlpha
    15.       Tags {"Queue" = "Transparent"}
    16.       Color[_TintColor]
    17.       Pass
    18.       {
    19.          CGPROGRAM
    20.          #pragma vertex vert
    21.          #pragma fragment frag
    22.          #include "UnityCG.cginc"
    23.        
    24.          struct appdata_t
    25.          {
    26.             float4 vertex : POSITION;
    27.             half4 color : COLOR;
    28.          };
    29.        
    30.          struct v2f
    31.          {
    32.             float4 pos : SV_POSITION;
    33.             float2 worldPos : TEXCOORD1;
    34.             half4 color : COLOR;
    35.          };
    36.        
    37.          fixed4 _TintColor;
    38.          float4 _Clipping;
    39.        
    40.          v2f vert (appdata_t v)
    41.          {
    42.             v2f o;
    43.             o.pos = mul (UNITY_MATRIX_MVP, v.vertex);
    44.             o.color = _TintColor;
    45.             o.worldPos = v.vertex;
    46.             return o;
    47.          }
    48.          half4 frag (v2f IN) : COLOR
    49.          {
    50.             half4 col = half4(IN.color);
    51.          
    52.             if(IN.worldPos.x < _Clipping.x) col.a = 0.0;
    53.             else if(IN.worldPos.x > _Clipping.y) col.a = 0.0;
    54.             else if(IN.worldPos.y < _Clipping.z) col.a = 0.0;
    55.             else if(IN.worldPos.y > _Clipping.w) col.a = 0.0;
    56.          
    57.             return col;
    58.          }
    59.          ENDCG
    60.       }
    61.    
    62.    }
    63.    FallBack "Unlit/Transparent"
    64. }
    Any thoughts, ideas, comments are welcome!