Search Unity

Question 2D Stencil Shader, Sorting Issue

Discussion in 'Shaders' started by KaidaStudios, Dec 26, 2022.

  1. KaidaStudios

    KaidaStudios

    Joined:
    Aug 4, 2017
    Posts:
    34
    Hello! Im trying to make a stencil shader for my 2D game. Which I managed to get it working, however its detecting the sorting order in the wrong direction I need it to. Ive tried dabling with the code but it just turns everything invisible.

    Here it is working. The numbers displayed are the sorting order in the sprite renderer component. This is not how my game tilemap is setup to work though, so this works fine from less to greater.




    The issue is, my tilemap sorting order goes from greater to lesser. As -7 is higher than -28. I cannot get the shader code to flip this around.

    Here is my shader code:

    Code (CSharp):
    1. Shader "Custom/BlendLiquid"
    2. {
    3.     Properties
    4.     {
    5.         [PerRendererData] _MainTex("Sprite Texture", 2D) = "white" {}
    6.         _Color("Tint", Color) = (1, 1, 1, 1)
    7.         [MaterialToggle] PixelSnap("Pixel snap", Float) = 0
    8.              _Cutoff("Alpha cutoff", Range(0,1)) = 0.5
    9.     }
    10.  
    11.         SubShader
    12.         {
    13.             Tags
    14.             {
    15.                 "Queue" = "Transparent"
    16.                 "IgnoreProjector" = "True"
    17.                 "RenderType" = "TransparentCutout"
    18.                 "PreviewType" = "Plane"
    19.                 "CanUseSpriteAtlas" = "True"
    20.             }
    21.  
    22.             Cull Off
    23.             Lighting Off
    24.             ZWrite Off
    25.  
    26.             Fog
    27.             {
    28.                 Mode Off
    29.             }
    30.  
    31.             Blend One OneMinusSrcAlpha
    32.  
    33.             Pass
    34.             {
    35.                 Stencil
    36.                 {
    37.                     Ref 1
    38.                     Comp Greater
    39.                     Pass IncrSat
    40.                 }
    41.  
    42.                 CGPROGRAM
    43.                 #pragma vertex vert
    44.                 #pragma fragment frag
    45.                 #pragma multi_compile DUMMY PIXELSNAP_ON
    46.                 #include "UnityCG.cginc"
    47.                 struct appdata_t
    48.                 {
    49.                     float4 vertex : POSITION;
    50.                     float4 color : COLOR;
    51.                     float2 texcoord : TEXCOORD0;
    52.                 };
    53.  
    54.                 struct v2f
    55.                 {
    56.                     float4 vertex : SV_POSITION;
    57.                     fixed4 color : COLOR;
    58.                     half2 texcoord : TEXCOORD0;
    59.                 };
    60.  
    61.                 fixed4 _Color;
    62.                 fixed _Cutoff;
    63.                 v2f vert(appdata_t IN)
    64.                 {
    65.                     v2f OUT;
    66.                     OUT.vertex = UnityObjectToClipPos(IN.vertex);
    67.                     OUT.texcoord = IN.texcoord;
    68.                     OUT.color = IN.color * _Color;
    69.  
    70.     #ifdef PIXELSNAP_ON
    71.                     OUT.vertex = UnityPixelSnap(OUT.vertex);
    72.     #endif
    73.  
    74.                     return OUT;
    75.                 }
    76.  
    77.                 sampler2D _MainTex;
    78.  
    79.                 fixed4 frag(v2f IN) : SV_Target
    80.                 {
    81.                     fixed4 c = tex2D(_MainTex, IN.texcoord) * IN.color;
    82.                     c.rgb *= c.a;
    83.                     clip(c.a - _Cutoff);
    84.                     return c;
    85.                 }
    86.  
    87.                 ENDCG
    88.             }
    89.         }
    90. }
    Thank you any help is appreciated!