Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Unlit cutout receive shadow

Discussion in 'General Graphics' started by dzft3w, Jul 30, 2020.

  1. dzft3w

    dzft3w

    Joined:
    Apr 4, 2020
    Posts:
    13
    Hello! I have a shader that makes unlit cutout surface receive shadow. It works as below:
    I want to make its cutout let through light/shadow so shadow do not cast on transparent part of the surface.
    Can someone please help with this?
    unlit_shadow_through.JPG

    Code (CSharp):
    1.  
    2. Shader "Unlit With Shadows" {
    3.     Properties{
    4.         _Color("Main Color", Color) = (1,1,1,1)
    5.         _MainTex("Base (RGB)", 2D) = "white" {}
    6.     _Depthmap("Base (RGB) Trans (A)", 2D) = "white" {}
    7.     _Cutoff("Alpha cutoff", Range(0,1)) = 0.5
    8.     }
    9.         SubShader{
    10.             Tags {"Queue" = "Geometry" "RenderType" = "TransparentCutout"}
    11.             LOD 100
    12.             ZWrite On
    13.  
    14.  
    15.             Pass {
    16.                 Tags {"LightMode" = "ForwardBase"}
    17.                 CGPROGRAM
    18.                     #pragma vertex vert
    19.                     #pragma fragment frag
    20.                     #pragma multi_compile_fwdbase
    21.                     #pragma fragmentoption ARB_fog_exp2
    22.                     #pragma fragmentoption ARB_precision_hint_fastest
    23.  
    24.                     #include "UnityCG.cginc"
    25.                     #include "AutoLight.cginc"
    26.  
    27.                     struct v2f
    28.                     {
    29.                         float4    pos            : SV_POSITION;
    30.                         float2    uv            : TEXCOORD0;
    31.                         LIGHTING_COORDS(1,2)
    32.                     };
    33.  
    34.                     float4 _MainTex_ST;
    35.  
    36.                     v2f vert(appdata_tan v)
    37.                     {
    38.                         v2f o;
    39.  
    40.                         o.pos = UnityObjectToClipPos(v.vertex);
    41.                         o.uv = TRANSFORM_TEX(v.texcoord, _MainTex).xy;
    42.                         TRANSFER_VERTEX_TO_FRAGMENT(o);
    43.                         UNITY_TRANSFER_FOG(o, o.pos);
    44.  
    45.                         return o;
    46.                     }
    47.  
    48.                     sampler2D _MainTex;
    49.                     sampler2D _Depthmap;
    50.                     fixed _Cutoff;
    51.  
    52.                     fixed4 frag(v2f i) : COLOR
    53.                     {
    54.                         //fixed atten = LIGHT_ATTENUATION(i);    // Light attenuation + shadows.
    55.                         fixed atten = SHADOW_ATTENUATION(i); // Shadows ONLY.
    56.  
    57.                         fixed4 col = tex2D(_MainTex, i.uv);
    58.                         col.a = tex2D(_Depthmap, i.uv);                      
    59.                         clip(col.a - _Cutoff); //clip alpha value,basic distance filter.
    60.                         UNITY_APPLY_FOG(i.fogCoord, col);
    61.  
    62.                         return tex2D(_MainTex, i.uv) * atten;
    63.                     }
    64.                 ENDCG
    65.             }
    66.  
    67.             Pass {
    68.                 Tags {"LightMode" = "ForwardAdd"}
    69.                 Blend One One
    70.                 CGPROGRAM
    71.                     #pragma vertex vert
    72.                     #pragma fragment frag
    73.                     #pragma multi_compile_fwdadd_fullshadows
    74.                     #pragma fragmentoption ARB_fog_exp2
    75.                     #pragma fragmentoption ARB_precision_hint_fastest
    76.  
    77.                     #include "UnityCG.cginc"
    78.                     #include "AutoLight.cginc"
    79.  
    80.                     struct v2f
    81.                     {
    82.                         float4    pos            : SV_POSITION;
    83.                         float2    uv            : TEXCOORD0;
    84.                         LIGHTING_COORDS(1,2)
    85.                     };
    86.  
    87.                     float4 _MainTex_ST;
    88.  
    89.                     v2f vert(appdata_tan v)
    90.                     {
    91.                         v2f o;
    92.  
    93.                         o.pos = UnityObjectToClipPos(v.vertex);
    94.                         o.uv = TRANSFORM_TEX(v.texcoord, _MainTex).xy;
    95.                         TRANSFER_VERTEX_TO_FRAGMENT(o);
    96.                         return o;
    97.                     }
    98.  
    99.                     sampler2D _MainTex;
    100.  
    101.                     fixed4 frag(v2f i) : COLOR
    102.                     {
    103.                         //fixed atten = LIGHT_ATTENUATION(i);    // Light attenuation + shadows.
    104.                         fixed atten = SHADOW_ATTENUATION(i); // Shadows ONLY.
    105.                         return tex2D(_MainTex, i.uv) * atten;
    106.                     }
    107.                 ENDCG
    108.             }
    109.     }
    110.         FallBack "VertexLit"
    111. }
     
  2. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,256
    Your fallback shader is for an opaque material, which means its shadow caster (and shadow receiver, which reuses the same shader as the shadow caster) is opaque. You need to use the cutout version.
    Code (csharp):
    1. Fallback "Legacy Shaders/Transparent/Cutout/VertexLit"
     
  3. dzft3w

    dzft3w

    Joined:
    Apr 4, 2020
    Posts:
    13
    I have changed the code, and no difference made.:confused:
     
  4. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,256
    I missed you were doing this:
    Code (csharp):
    1.                         col.a = tex2D(_Depthmap, i.uv);
    The built in shadow caster passes assume you're using the alpha from the
    _MainTex
    . Since your shader isn't doing that, you'll need to manually write a custom shadow caster pass that uses the same logic as the base pass to clip, be that getting the alpha from the
    _Depthmap
    's red channel or whatever you end up doing for your final shader.
     
  5. dzft3w

    dzft3w

    Joined:
    Apr 4, 2020
    Posts:
    13
    How can I transfer this alpha from _Depthmap to _MainTex, so the shadow caster passes can use it,
    or how to write this custom shadow pass?
     
  6. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,256