Search Unity

Question Sprite Shader Cutoff bug

Discussion in 'Shaders' started by Ikaro88, Oct 19, 2021.

  1. Ikaro88

    Ikaro88

    Joined:
    Jun 6, 2016
    Posts:
    300
    Hi I have to create a shader to have a fill effect on a sprite renderer.
    I have this:


    Code (CSharp):
    1. Shader "Sprites/DefaultCutoff"
    2. {
    3.     Properties
    4.     {
    5.         [PerRendererData] _MainTex("Sprite Texture", 2D) = "white" {}
    6.     _Color("Tint", Color) = (0,0,0,0)
    7.         _TransitionTex("Transition Texture", 2D) = "white" {}
    8.     _Cutoff("Cutoff", Range(0, 1)) = 0
    9.         [MaterialToggle] PixelSnap("Pixel snap", Float) = 0
    10.     }
    11.  
    12.         SubShader
    13.     {
    14.         Tags
    15.     {
    16.         "Queue" = "Transparent"
    17.         "IgnoreProjector" = "True"
    18.         "RenderType" = "Transparent"
    19.         "PreviewType" = "Plane"
    20.         "CanUseSpriteAtlas" = "True"
    21.     }
    22.  
    23.         Cull Off
    24.         Lighting Off
    25.         ZWrite Off
    26.         //Blend SrcAlpha OneMinusSrcAlpha
    27.         Blend SrcAlpha OneMinusSrcAlpha
    28.  
    29.         Pass
    30.     {
    31.         CGPROGRAM
    32. #pragma vertex vert
    33. #pragma fragment frag
    34. #pragma multi_compile _ PIXELSNAP_ON
    35. #include "UnityCG.cginc"
    36.  
    37.         struct appdata_t
    38.     {
    39.         float4 vertex   : POSITION;
    40.         float4 color    : COLOR;
    41.         float2 texcoord : TEXCOORD0;
    42.     };
    43.  
    44.     struct v2f
    45.     {
    46.         float4 vertex   : SV_POSITION;
    47.         fixed4 color : COLOR;
    48.         float2 texcoord  : TEXCOORD0;
    49.     };
    50.  
    51.     fixed4 _Color;
    52.     float _Cutoff;
    53.     sampler2D _TransitionTex;
    54.  
    55.     v2f vert(appdata_t IN)
    56.     {
    57.         v2f OUT;
    58.         OUT.vertex = UnityObjectToClipPos(IN.vertex);
    59.         OUT.texcoord = IN.texcoord;
    60.         OUT.color = IN.color * _Color;
    61. #ifdef PIXELSNAP_ON
    62.         OUT.vertex = UnityPixelSnap(OUT.vertex);
    63. #endif
    64.  
    65.         return OUT;
    66.     }
    67.  
    68.     sampler2D _MainTex;
    69.     sampler2D _AlphaTex;
    70.     float _AlphaSplitEnabled;
    71.  
    72.     fixed4 SampleSpriteTexture(float2 uv)
    73.     {
    74.         fixed4 color = tex2D(_MainTex, uv);
    75.  
    76. #if UNITY_TEXTURE_ALPHASPLIT_ALLOWED
    77.         if (_AlphaSplitEnabled)
    78.             color.a = tex2D(_AlphaTex, uv).r;
    79. #endif //UNITY_TEXTURE_ALPHASPLIT_ALLOWED
    80.  
    81.         return color;
    82.     }
    83.  
    84.     fixed4 frag(v2f IN) : SV_Target
    85.     {
    86.         fixed4 c = SampleSpriteTexture(IN.texcoord) * IN.color;
    87.     c.rgb *= c.a;
    88.  
    89.     fixed4 transit = tex2D(_TransitionTex, IN.texcoord);
    90.     if (transit.b > _Cutoff)
    91.         c.a = 0;
    92.  
    93.     return c;
    94.     }
    95.         ENDCG
    96.     }
    97.     }
    98. }
    I get a very annoing line on the top of the sprite when the cutoff is more than 0.5....
    upload_2021-10-19_13-42-58.png

    How to solve this?