Search Unity

Resolved Clip, Addshadow and real time lighting

Discussion in 'Shaders' started by rc31, Jan 5, 2023.

  1. rc31

    rc31

    Joined:
    Mar 12, 2016
    Posts:
    11
    Hello,

    I am working on a project that uses clipping to create holes in geometry. I had some shadow artefacts (kind of faded geometry) using real time lighting but solved them by adding addshadow directive in the shader.

    The issue is that addshadow is removing artefacts but lighting is changing according to clipping.
    As an example, if there is a wall clipped, the wall shadow is not projected on the ground that is not clipped (It should).
    I want the lighting to stay the same even if the clipping region is changing. (It works pretty well with baked lights!)

    So, is it possible to clip shadows without using addshadow ? to clip the shadowmap ?

    Thanks in advance for your answers !
     
  2. adamgryu

    adamgryu

    Joined:
    Mar 1, 2014
    Posts:
    188
    Could you include some screenshots of the problem? It might make it easier for us to understand the issue.

    Also, it would help to know which pipeline you're using (URP, HDRP, Built-In) - it sounds like you're using Built-In with surface shaders? Any sample code would also help.
     
  3. rc31

    rc31

    Joined:
    Mar 12, 2016
    Posts:
    11
    Sure!
    Yes, I'm using surface shader with Built In pipeline.

    Here an example without addshadow :

    So, a lot of artefacts due to overlapped geometry. I tried dealing with layers but it did not fixed anything. Baked lights is the only solution for now.

    With addshadow :

    No artefacts anymore but lighting is affected by clipping. (that's an issue)

    For the shader code, it's a basic standard surface shader with clipping test (clip function), triplanar mapping, 1 color and 1 diffuse texture.
    Here the part with the "addshadow" :
    ...
    Tags { "RenderType"="Opaque" }
    LOD 200
    CGPROGRAM
    #pragma surface surf Standard fullforwardshadows addshadow
    ...
     
  4. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,352
    You want
    addshadow
    , but you'll want to try to detect in the shader if you're rendering a shadow map or the camera depth texture. The problem is in the built in rendering path the same "shadow pass" is used for both.

    Code (CSharp):
    1. Shader "Custom/NonClippedShadow"
    2. {
    3.     Properties
    4.     {
    5.         _Color ("Color", Color) = (1,1,1,1)
    6.         _MainTex ("Albedo (RGB)", 2D) = "white" {}
    7.     }
    8.     SubShader
    9.     {
    10.         Tags { "RenderType"="Opaque" }
    11.         LOD 200
    12.  
    13.         CGPROGRAM
    14.         #pragma surface surf Standard fullforwardshadows addshadow
    15.  
    16.         #pragma target 3.0
    17.  
    18.         sampler2D _MainTex;
    19.  
    20.         struct Input
    21.         {
    22.             float2 uv_MainTex;
    23.         };
    24.  
    25.         fixed4 _Color;
    26.  
    27.         void surf (Input IN, inout SurfaceOutputStandard o)
    28.         {
    29.             // Albedo comes from a texture tinted by color
    30.             fixed4 c = tex2D (_MainTex, IN.uv_MainTex) * _Color;
    31.             o.Albedo = c.rgb;
    32.  
    33.             #ifdef UNITY_PASS_SHADOWCASTER
    34.             #ifdef SHADOWS_DEPTH
    35.             if (unity_LightShadowBias.z != 0.0)
    36.                 return; // is shadow caster
    37.             #endif
    38.             #endif
    39.  
    40.             clip(IN.uv_MainTex.y * 2.0 - 1.0 + sin(_Time.y));
    41.         }
    42.         ENDCG
    43.     }
    44.     FallBack "Diffuse"
    45. }
     
  5. rc31

    rc31

    Joined:
    Mar 12, 2016
    Posts:
    11
    Hello bgolus

    It seems to work pretty well ! It is not perfect right now, but hope it will with some tweaking.
    Thanks a lot for your answer ! ( and all your other interesting posts on this forum :D )