Search Unity

Question Shader discarding some pixels based on color + casting shadows even for discarded ones

Discussion in 'Shaders' started by Misscelan, Sep 28, 2022.

  1. Misscelan

    Misscelan

    Joined:
    Mar 8, 2013
    Posts:
    176
    Hi,

    I have this shader that discards some pixels based on the alpha chanel of the color vertex.
    I would like however to still cast shadow for those discarded. I thought adding an extra shadowcaster pass for my surface shader would do the job, but shadows are still not casted.

    Here is the shader, any idea what I might be missing?

    Thanks!

    Code (CSharp):
    1. Shader "Custom/ColorVertexDirt" {
    2.     Properties {
    3.         _Color ("Color", Color) = (1,1,1,1)
    4.         _MainTex ("Albedo (RGB)", 2D) = "white" {}
    5.         _DirtTex ("Dirt Text (RGB)", 2D) = "white" {}
    6.         _Glossiness("Smoothness", Range(0,1)) = 0.5
    7.         _Metallic("Metallic", Range(0,1)) = 0.0
    8.         _Blend("Blend", Range(0,1)) = 0.5
    9.         _AlphaCut("_AlphaCut", Range(0,1)) = 0.3
    10.         _OccludedColor("Occluded Color", Color) = (1,1,1,1)
    11.     }
    12.         SubShader{
    13.  
    14.         Pass
    15.         {
    16.             Name "ShadowCaster"
    17.              Tags{ "Queue" = "Transparent" "LightMode" = "ShadowCaster"  }
    18.  
    19.             CGPROGRAM
    20.             #pragma vertex vert
    21.             #pragma fragment frag
    22.             #pragma multi_compile_shadowcaster
    23.             #include "UnityCG.cginc"
    24.             struct v2f {
    25.                 V2F_SHADOW_CASTER;
    26.             };
    27.  
    28.             v2f vert(appdata_full v)
    29.             {
    30.                 v2f o;
    31.                 TRANSFER_SHADOW_CASTER_NORMALOFFSET(o)
    32.                 return o;
    33.             }
    34.  
    35.             float4 frag(v2f i) : SV_Target
    36.             {
    37.                 SHADOW_CASTER_FRAGMENT(i)
    38.             }
    39.             ENDCG
    40.  
    41.         }
    42.  
    43.         Tags { "RenderType" = "Opaque" "Queue" = "Geometry+30"}
    44.         LOD 200
    45.         Cull Off
    46.  
    47.         CGPROGRAM
    48.         // Physically based Standard lighting model, and enable shadows on all light types
    49.         #pragma surface surf Standard  vertex:vert
    50.         // Use shader model 3.0 target, to get nicer looking lighting
    51.         #pragma target 4.0
    52.  
    53.         sampler2D _MainTex;
    54.         sampler2D _DirtTex;
    55.  
    56.         struct Input {
    57.             float2 uv_MainTex;
    58.             float4 color: COLOR;
    59.             float4 screenPos;
    60.             float3 worldPos;
    61.         };
    62.  
    63.         half _Glossiness;
    64.         half _Metallic;
    65.         fixed4 _Color;
    66.         float _Blend;
    67.         float _AlphaCut;
    68.  
    69.         void vert(inout appdata_full i, out Input o)
    70.         {
    71.             UNITY_INITIALIZE_OUTPUT(Input, o);
    72.             o.screenPos = ComputeScreenPos(i.vertex);
    73.         }
    74.  
    75.  
    76.         void surf (Input IN, inout SurfaceOutputStandard o) {
    77.             if (IN.color.a < 0.2) discard;
    78.  
    79.             fixed4 c = tex2D (_MainTex, IN.uv_MainTex) * _Color ;
    80.  
    81.             fixed normaladd = o.Normal.r + o.Normal.g + o.Normal.b;
    82.             fixed4 dirt = tex2D (_DirtTex, IN.uv_MainTex)*0.5;
    83.             fixed3 normalcolor = IN.color * 0.7 + IN.color * normaladd * 0.38 + o.Normal * 0.03 + _Color + unity_LightColor[0].rgb * 0.10;
    84.  
    85.             o.Albedo = normalcolor;
    86.  
    87.             if (dirt.a > _AlphaCut)
    88.             {
    89.  
    90.                 o.Albedo = (dirt * _Blend + normalcolor * (1-_Blend));
    91.             }
    92.  
    93.             o.Metallic = _Metallic;
    94.             o.Smoothness = _Glossiness;
    95.             o.Alpha = c.a;
    96.  
    97.         }
    98.         ENDCG
    99.  
    100.        
    101.     }
    102.     FallBack "Diffuse"
    103.     //Fallback "Transparent/VertexLit"
    104. }
    105.