Search Unity

Bug URP bug with shadow cascades

Discussion in 'Universal Render Pipeline' started by joaopat98, Dec 31, 2021.

  1. joaopat98

    joaopat98

    Joined:
    Feb 18, 2017
    Posts:
    5
    Posted on Unity Answers a few months ago but got no answers, posting here to see if anyone can help.

    I'm making a project in Unity 2020.3.21f1 with URP, and depending on the camera's position and rotation some of the shadow cascades show up completely dark:



    Does anyone know what might be the problem? I tried fiddling with the shadow cascade settings but it only resulted in variations of the same problem.
     
  2. UnityLighting

    UnityLighting

    Joined:
    Mar 31, 2015
    Posts:
    3,874
    Use red arrows in your screenshot to show us your problem area...
     
  3. joaopat98

    joaopat98

    Joined:
    Feb 18, 2017
    Posts:
    5
    The problem is the part in shadow in the screenshot which should also be lit, outside the lit circle in the bottom
     

    Attached Files:

  4. UnityLighting

    UnityLighting

    Joined:
    Mar 31, 2015
    Posts:
    3,874
    I think your problem is the shadow distance
     
  5. joaopat98

    joaopat98

    Joined:
    Feb 18, 2017
    Posts:
    5
    Are you sure? The picture I sent isn't what it usually looks like, most of the time the scene is completely lit and looks fine, it's only at some particular camera angles that it looks like the screenshot.
     
  6. joaopat98

    joaopat98

    Joined:
    Feb 18, 2017
    Posts:
    5
    Bump, does anyone have any idea on how to fix this? It's a really odd bug and I cant figure out what is causing the cascades to get messed up.
     
  7. mgruv

    mgruv

    Joined:
    May 14, 2015
    Posts:
    2
    Send a pic of what it normally looks like.
     
  8. joaopat98

    joaopat98

    Joined:
    Feb 18, 2017
    Posts:
    5
    Picture on top is what it usually looks like, below it is the bug in effect, only change is that the camera angle is slightly different.



    Thx for the help!
     
  9. KSzczech

    KSzczech

    Joined:
    Sep 24, 2020
    Posts:
    30
    It is most likely a bug.
    Something similar happens even in the most basic scenes in both URP and 3D projects.

    It seems to me that there is some problem calculating occlusion of objects, shadow offsets or camera clip planes when Unity renders objects into shadow cascades. Big objects are often not rendered correctly into smallest shadow cascades.

    You can just create and empty URP or 3D project, put 2 cubes in the scene and you will see similar issue with default lighting and shadow settings:

    UnitySadowCascadesIssue.png

    I'm attaching both 3D and URP proejcts that show this issue.

    Unity version 2019.4.36f1 LTS

    Or you can just create empty scene, put 2 boxes in it and see for yourself.
    My cubes are scaled 100x100x1

    In general, I've had terrible experiences with shadowmaps in Unity. Especially in combination with using Graphics.DrawMeshInstancedProcedural in URP. The only shaders I managed to get to work with this are HLSL shaders. No luck with CGSHADERS, no support for surface shaders and not possible in shader graphs.
     

    Attached Files:

    Last edited: Apr 22, 2022
    rostykul likes this.
  10. funkyCoty

    funkyCoty

    Joined:
    May 22, 2018
    Posts:
    727
    You should submit your zip in a bug report, or it'll never get fixed.
     
  11. KSzczech

    KSzczech

    Joined:
    Sep 24, 2020
    Posts:
    30
    Submitted.

    There's definitely something wrong with clipping of objects in individual cascades
     
    funkyCoty likes this.
  12. Sameer1472

    Sameer1472

    Joined:
    Apr 1, 2020
    Posts:
    5
    Thank god I'm not alone, I thought it was only me. Exact same problem. I thought it was because I switched to unity 2021 but rolling back (that was not fun) to 2020 LTS didn't fix the problem either. I've never seen this bug happen before 2 days ago.

     
    Last edited: May 9, 2022
  13. PutridEx

    PutridEx

    Joined:
    Feb 3, 2021
    Posts:
    1,136
    This happened to me when I used URP a while ago as well.
     
  14. KSzczech

    KSzczech

    Joined:
    Sep 24, 2020
    Posts:
    30
    Well, I just shown my game to my 6-year old niece and she immediately noticed there's something wrong with shadows dissapearing as I move closer.

    I wish Unity team had the perception of my 6-year old niece ;)

    This shadow cascades bug is so obvious and so in plain sight that I wonder why it has to be submitted as a bug report in the first place


    Fortunately for me, the characteristics of my project make it a perfect case to abandon shadow cascades in favor of parallel-split shadow maps, so I'm currently abandoning Unity's lighting and shadows in favor of my own implementation
     
  15. Sameer1472

    Sameer1472

    Joined:
    Apr 1, 2020
    Posts:
    5
    I've noticed a few things: The size of an object has a part in this bug. To test this, take a 1x1 cube and take another 100x100 cube (x,z axis) now put the 1x1 cube on top of the big cube so that its bottom face is going through the top face of the bigger cube or atleast touching it.

    Now, start scaling the small cube in one of the axis so that the shadow becomes bigger horizontally rather than vertically. As you keep scaling you will notice that the shadow starts showing gaps especially when viewed from a similar angle as shown in KSzczech's earlier reply above.

    Ultimately, I think this has something to do with the shadowmap's max size.

    So a way to get around it is to break apart big assets like walls into smaller walls when possible and increase the max shadow distance.
     
    Last edited: May 11, 2022
  16. KSzczech

    KSzczech

    Joined:
    Sep 24, 2020
    Posts:
    30
    Yes, that's true - it depends on relation between object size and shadow cascade size.

    Small objects usually fall into one shadow cascade while bigger objects can span across multiple shadow cascades and this is where clipping problem occurs.
     
  17. palomav

    palomav

    Joined:
    Sep 21, 2020
    Posts:
    1
    I'm using Unity 2020.3.12f1 and running into this problem too, but it seems to affect even small objects (chairs, pillows, etc.), not sure why.
     
  18. KSzczech

    KSzczech

    Joined:
    Sep 24, 2020
    Posts:
    30
    With small objects that may have something to do with shadow bias settings.

    Also, the way Unity renders shadow casters surprized me. Normally, due to limited resolution of shadowmaps, you get arrtifacts. You need to compensate for it with shadow bias.
    My approach is to kind of "grow" the shadow receiver along its face normals and sample shadow slightly outside of obect.
    Unity does the opposite - it does not "grow" shadow receiver, but "shrinks" shadow caster. In my case it caused some elements of my car, like exhaust pipes or rollcage, to cast much thinner shadows than expected.

    I suggest you experiment a little with shadow settings to see how much can you decrease shadow bias settings without getting artifacts.
    Unless you're sure this is due to shadow cascade clipping, not shadow bias settings.

    But you will never get proper size out of small shadow casters in Unity.

    I completely disabled Unitys realitime shadows in my game and replaced them with my own realtime shadows - works well for me. I will probably put them in the Asset Store in some near future.
     
    funkyCoty and andreiagmu like this.
  19. rostykul

    rostykul

    Joined:
    Oct 9, 2018
    Posts:
    22
    Unity - Manual: Shadow troubleshooting (unity3d.com)
    I'm experiencing the same issue in my URP project. Here in Unity's manual it shows a similar-looking issue in built-in render pipeline. It says you can adjust the Shadow Near Plane Offset property to avoid this problem. However, I can't find any property like this in URP :(
     
    Last edited: May 18, 2022
    andreiagmu likes this.
  20. andreiagmu

    andreiagmu

    Joined:
    Feb 20, 2014
    Posts:
    175
    Hi! Just wanted to say I'm interested in your realtime shadows implementation, if you release it on the Asset Store :)
    I'm also having troubles with Unity's realtime shadows and shadow cascades on URP, and I'm looking for better alternatives.
     
  21. andreiagmu

    andreiagmu

    Joined:
    Feb 20, 2014
    Posts:
    175
    You can find that property on each of your lights, in their Shadows settings, when the Light Mode is set to Realtime or Mixed. You need to adjust this Shadow Near Plane setting per-light.

    capture_20220531_015221_004.png
     
    Furgl and Malmarthur like this.
  22. rostykul

    rostykul

    Joined:
    Oct 9, 2018
    Posts:
    22
    I do not know how I didn't notice that before. This solved the shadow cascade issue for me! tysm :)
     
    Last edited: May 31, 2022
    andreiagmu likes this.
  23. KSzczech

    KSzczech

    Joined:
    Sep 24, 2020
    Posts:
    30
    Just tested it on the attached URP project above - increasing near plane to around 2.5 was enough offset to make the problem go away in that sample scene.
    Default value was 0.2.

    I wonder why it's implementd like this. Usually with ortho shadows (directional lights) you should have enough z-precision relative to overall resolution of shadowmap to be able to just cover enough space. If an object is supposed to be renered to given shadow cascade, near and far clipping planes should automatically include its bounding box/sphere. It may be excessive a bit, but loosing even half of precision is in my opinion better than having a big hole in your shadow :)
    It also means we have to read documentation to understand how certain things work in Unity just to get something as basic as shadows to even work properly

    PSSM is a pretty special case for shadows but it can be very useful in some scenarios.
    I've sent you an email, since I don't want to hijack this thread for PSSM shadowmap discussion
     
    andreiagmu likes this.