Search Unity

Question Lit cutout shader, but without normal/vertex-based lighting?

Discussion in 'Shaders' started by ergonomi, Jul 1, 2022.

  1. ergonomi

    ergonomi

    Joined:
    Feb 14, 2021
    Posts:
    1
    Hi! I'm trying to make a shader that works like a standard cutout shader, that is affected by the shadowmap but does not calculate the basic surface-normals based (NdotL) lighting that comes with most shaders.
    The reason for this is that my game's environment inverts the colors of everything that is in shadow as opposed to drawing them normally. With flat planes/quads, this lighting didn't quite look right stylistically, having them be inverted if they were just facing away from the light.

    I've gotten it working about halfway with the following code, which calculates the shadow correctly:
    Code (HLSL):
    1. Shader "Lit/Normal"
    2. {
    3.     Properties
    4.     {
    5.         _MainTex("Texture", 2D) = "white" {}
    6.     }
    7.     SubShader
    8.     {
    9.         Pass
    10.         {
    11.             Tags {"LightMode" = "ForwardBase"}
    12.             CGPROGRAM
    13.             #pragma vertex vert
    14.             #pragma fragment frag
    15.             #include "UnityCG.cginc"
    16.             #include "Lighting.cginc"
    17.  
    18.             #pragma multi_compile_fwdbase nolightmap nodirlightmap nodynlightmap novertexlight
    19.             #include "AutoLight.cginc"
    20.  
    21.             sampler2D _MainTex;
    22.             float4 _MainTex_ST;
    23.  
    24.             struct v2f
    25.             {
    26.                 float2 uv : TEXCOORD0;
    27.                 SHADOW_COORDS(1)
    28.                 float4 pos : SV_POSITION;
    29.             };
    30.             v2f vert(appdata_base v)
    31.             {
    32.                 v2f o;
    33.                 o.pos = UnityObjectToClipPos(v.vertex);
    34.                 o.uv = TRANSFORM_TEX(v.texcoord, _MainTex);
    35.  
    36.                 half3 worldNormal = UnityObjectToWorldNormal(v.normal);
    37.  
    38.                 TRANSFER_SHADOW(o)
    39.                 return o;
    40.             }
    41.  
    42.             float4 frag(v2f i) : SV_Target
    43.             {
    44.                 float4 col = tex2D(_MainTex, i.uv);
    45.  
    46.                 fixed shadow = SHADOW_ATTENUATION(i);
    47.                 if (shadow < 0.5)
    48.                 {
    49.                     col.rgb = 1 - col.rgb;
    50.                 }
    51.                 return col;
    52.             }
    53.             ENDCG
    54.         }
    55.         UsePass "Legacy Shaders/VertexLit/SHADOWCASTER"
    56.     }
    57. }
    Giving this result:

    Problem is, I've not been able to make this shader support transparency/cutout as most attemps have ended up looking like the following:

    Where the shadows on the plane overwrites the shadows on objects behind it. Is there any way to make this work with custom lighting and transparency?

    I'm relatively new to shaders so any help is greatly appreciated!