Search Unity

Question Separating the Directional Shadow Frustrum from Light Direction.

Discussion in 'General Graphics' started by dmchaderer, Oct 1, 2021.

  1. dmchaderer

    dmchaderer

    Joined:
    Nov 13, 2019
    Posts:
    23
    Hi Comunity,

    I have a question about Directional Shadows:

    I have a day-night-cycle set up using the Physically Based Sky and its all looking quite good so far. This means that when the sun sets the Directional light is at a 90-degree angle creating a really dramatic mood.

    This also means that this light casts shadows at a 90-degree angle which is correct but causes two issues: First, the shadows move really fast at sunset making the scene quite unsettled. And second the shadow frustrum then needs to render about the entire scene which has a considerable performance overhead.

    Now I’m trying to do what can be observed in a lot of games: Clamp the shadow frustrums angle at a level so that the shadows won’t get longer than I want them to be.
    Of course I could just clamp the angle of the directional light. But one would soon notice that it’s incorrect from its specular highlights and the scenes volumetrics.

    I’ve been searching for an entry point for this around the C#-side of HDRP but I couldn’t find one.

    So please: If anyone else has tried this or one of you Unity guys could point me in the right direction that would be much appreciated.

    Cheers
    Dominik


    Note:
    Currently on HDRP 10.5 with Unity 2020.3.14f1
     
  2. dmchaderer

    dmchaderer

    Joined:
    Nov 13, 2019
    Posts:
    23
    bump

    Here's a sketch of what I'm trying to achieve: The directional Light is at a low angle and the shadow frustrum would be the black rectangle at a much steeper angle so that the cubes shadows won't get too long.

    Any thoughts?


    upload_2021-10-26_12-15-52.png
     
  3. joshuacwilde

    joshuacwilde

    Joined:
    Feb 4, 2018
    Posts:
    731
    If you are using the standard shader you should be able to check if you are currently lighting using a directional light (_WorldSpaceLightPos0.w == 0) and then instead of lighting with that direction, use your own direction that you set from a uniform Shader.SetGlobalVector().

    You can do all this from a custom lighting model : https://docs.unity3d.com/Manual/SL-SurfaceShaderLighting.html

    But instead of writing your own lighting model, you just call Unity's built in lighting model, only overriding the light direction with your own.
     
  4. tawdry

    tawdry

    Joined:
    Sep 3, 2014
    Posts:
    1,357
    If you are using deferred lightning you could add a second directional light. Set your main one you are using for the day night cycle to shadow off and enable shadows on the second directional light then set its angle to what you wish at whatever stage of day.Never tried hdrp so not sure if it has a deferred mode option?