Search Unity

Issue with Shadow Casting and Custom Shader

Discussion in 'Shaders' started by adjurkov, May 15, 2022.

  1. adjurkov

    adjurkov

    Joined:
    Feb 10, 2022
    Posts:
    2
    Hello,

    I've been following this example from the Unity documentation on custom shaders:
    https://docs.unity3d.com/Manual/SL-VertexFragmentShaderExamples.html

    And I can't seem to get shadow casting to work for URP. I've applied the following shader to both the plane and the cube shown in the photo. It seems receiving shadows works, there is just a problem with casting shadows.

    Any ideas on what could be missing here?
    Thank you!




    Code (CSharp):
    1. Shader "Custom/Radial_Bend"
    2. {
    3.     Properties
    4.     {
    5.         _MainTex("Sprite Texture", 2D) = "white" {}
    6.     }
    7.  
    8.     SubShader
    9.     {
    10.         Pass
    11.         {
    12.             Tags
    13.             {
    14.                  "LightMode" = "UniversalForward"
    15.                  "RenderType" = "Opaque"
    16.                  "RenderPipeline" = "UniversalPipeline"
    17.                  "UniversalMaterialType" = "Lit"
    18.                  "IgnoreProjector" = "True"
    19.             }
    20.  
    21.             HLSLPROGRAM
    22.             #pragma vertex vert
    23.             #pragma fragment frag
    24.  
    25.             #pragma multi_compile __ BEND_ON
    26.             #pragma multi_compile __ BEND_OFF
    27.              
    28.             #include "UnityCG.cginc"
    29.             #include "Lighting.cginc"
    30.  
    31.             #pragma multi_compile_fwdbase nolightmap nodirlightmap nodynlightmap novertexlight
    32.             #include "AutoLight.cginc"
    33.  
    34.             float4 _MainTex_ST;
    35.             sampler2D _MainTex;
    36.  
    37.             struct v2f
    38.             {
    39.                 float2 uv : TEXCOORD0;
    40.                 SHADOW_COORDS(1)
    41.                 fixed3 diff : COLOR0;
    42.                 fixed3 ambient : COLOR1;
    43.                 float4 pos : SV_POSITION;
    44.             };
    45.  
    46.             uniform float _HORIZON = 0.0f;
    47.             uniform float _ATTENUATE = 0.0f;
    48.             uniform float _SPREAD = 0.0f;
    49.  
    50.             float4 BendEffect(float4 v)
    51.             {
    52.                 // Put the given vertex in world space
    53.                 float4 t = mul(unity_ObjectToWorld, v);
    54.  
    55.                 // Calculate z-depth
    56.                 float dist = max(0, abs(_HORIZON - t.z) - _SPREAD);
    57.  
    58.                 // Apply parabola formula (-ve to bend down)
    59.                 t.y -= dist * dist * _ATTENUATE;
    60.  
    61.                 // Put vertex back in object space
    62.                 t.xyz = mul(unity_WorldToObject, t).xyz * 1.0;
    63.                 return t;
    64.             }
    65.  
    66.             v2f vert(appdata_base v)
    67.             {
    68.                 v2f o;
    69.                 o.pos = UnityObjectToClipPos(BendEffect(v.vertex));
    70.                 o.uv = v.texcoord;
    71.  
    72.                 // For lighting
    73.                 half3 worldNormal = UnityObjectToWorldNormal(v.normal);
    74.                 half nl = max(0, dot(worldNormal, _WorldSpaceLightPos0.xyz));
    75.                 o.diff = nl * _LightColor0.rgb;
    76.  
    77.                 o.ambient = ShadeSH9(half4(worldNormal, 1));
    78.  
    79.                 // compute shadows data
    80.                 TRANSFER_SHADOW(o)
    81.  
    82.                 return o;
    83.             }
    84.  
    85.             fixed4 frag(v2f i) : SV_Target
    86.             {
    87.                 fixed4 col = tex2D(_MainTex, i.uv);
    88.  
    89.                 // compute shadow attenuation (1.0 = fully lit, 0.0 = fully shadowed)
    90.                 fixed shadow = SHADOW_ATTENUATION(i);
    91.  
    92.                 // darken light's illumination with shadow, keep ambient intact
    93.                 fixed3 lighting = i.diff * shadow + i.ambient;
    94.                 col.rgb *= lighting;
    95.                 return col;
    96.             }
    97.             ENDHLSL  
    98.         }
    99.         // shadow casting support
    100.         // UsePass "Legacy Shaders/VertexLit/SHADOWCASTER"
    101.          UsePass "Universal Render Pipeline/Lit/ShadowCaster"
    102.     }
    103. }
     
  2. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,343
    The samples are only intended for the built in renderer, and are not expected to work for the URP. Some of the unlit ones will, but the built in renderer and the URP shaders are structured completely differently.

    The "correct" solution (from Unity's point of view) is to use Shader Graph to construct your shaders for URP. There's zero official documentation on writing vertex fragment shaders for the URP, and no guarantee hand written shaders will work between versions of the URP as they can (and often do) change significantly between revisions. This is also the reason why you won't find any tutorials on how to do it written by the community, because a few people have attempted to do so and they were obsolete shortly after, or sometimes before they were finished. Because, again, the URP was changing so rapidly between revisions.

    Unfortunately the way this kind of world bend shader works can't be reproduced within Shader Graph as you can't modify the vertex position without also modifying how lighting affects it in Shader Graph. The easiest option for you right now might be to create a basic shader using Shader Graph that does what you need, minus the world bend, and then copy the generated shader code into a new .shader file and hand modify it to do what you need.
     
    adjurkov likes this.
  3. adjurkov

    adjurkov

    Joined:
    Feb 10, 2022
    Posts:
    2
    Ahh I see, that makes more sense now. Thanks so much for taking the time to reply :)
     
  4. Adriano_Alb

    Adriano_Alb

    Joined:
    Jan 19, 2019
    Posts:
    1