Search Unity

Directional Light shadows flickering issue

Discussion in 'General Graphics' started by ritendev, May 3, 2019.

  1. ritendev

    ritendev

    Joined:
    Jan 18, 2018
    Posts:
    12


    As you can see on video if I move my game objects 2500f (or even 200f) from world center point then shadows start to dance like crazy and if I go back to (0, 0, 0) everything is fine (don't look at camera shake, it was some recording issue, only shadows flickering matters).

    I've literally read every topic at google about it, I've found many solutions but none of them works. I've tested every option in editor:
    graphic settings (even reseting and trying from 0), quality, bias, shadow distance, light rotation, camera far/near.

    Only distance from center matters for me and I just can't create 150x150 terrain.

    I'm sure there is some solution, because for example there is no such problem in "Escape from Tarkov". What am I doing wrong, what else should I check?

    I'm using 2018.2.14, but I also tried with 2019.1.1 with same result. My graphic card is MSI GTX 1080.
     
    Last edited: May 3, 2019
    Joe-Censored likes this.
  2. ritendev

    ritendev

    Joined:
    Jan 18, 2018
    Posts:
    12
    spikezart likes this.
  3. RoughSpaghetti3211

    RoughSpaghetti3211

    Joined:
    Aug 11, 2015
    Posts:
    1,708
  4. ritendev

    ritendev

    Joined:
    Jan 18, 2018
    Posts:
    12
    In LWRP there were problems with trees and probably other shaders. I was really frustrated so decided to move my project to Unreal, always wanted to remind C++ :p
     
  5. AggressiveMastery

    AggressiveMastery

    Joined:
    Nov 19, 2018
    Posts:
    206
    LOL Amazing. Good choice @ritendev , good luck
     
  6. DavidSWu

    DavidSWu

    Joined:
    Jun 20, 2016
    Posts:
    183
    I get the flickering in LWRP and the default pipeline. Raising the camera near z helps, lowering far z helps. The odd thing is that if you look at the Frame Debugger, most shadow casters stop drawing which would indicate that it is a culling problem.
    At least the game speeds up when the casters are not rendering ;) maybe it is an optimization.
     
    devorenge and Noogy like this.
  7. DavidSWu

    DavidSWu

    Joined:
    Jun 20, 2016
    Posts:
    183
    In LWRP it looks like you can find angles where GetShadowCasterBounds returns false even if there are clearly shadow casters and receivers
     
  8. KarlKarl2000

    KarlKarl2000

    Joined:
    Jan 25, 2016
    Posts:
    606
    I'm getting this shadow glitching too.. It's strange something like this is happening in Unity. Wish a Unity dev could get in on this thread..
     
    twitchfactor likes this.
  9. Bzuco

    Bzuco

    Joined:
    Sep 6, 2015
    Posts:
    56
    Same issue here, but after using temporal antialiasing those glitches are almost(huge diference) gone. Tested on small cube at 5700f away from center with 0.05f camera near and 10000f far clipping. :)
     
    Pourya-MDP, Azarecx and KarlKarl2000 like this.
  10. Komikom

    Komikom

    Joined:
    May 20, 2015
    Posts:
    38
    What worked for me as a workaround is disabling Dynamic Rescale in HDPipilineAsset
    With that disabled, I no longer get flickering shadows.
     
  11. FedericoCara

    FedericoCara

    Joined:
    Jan 5, 2016
    Posts:
    6
    We get the same issue. Since it's an endless racing game, the player can reach 10000 z points, everything starts to flicker much earlier. Using 2019.4
     
  12. KarlKarl2000

    KarlKarl2000

    Joined:
    Jan 25, 2016
    Posts:
    606
    Hi @Tim-C @UnityMaru
    Are you guys the best devs to ask about the flickering shadows?
     
  13. Zunno

    Zunno

    Joined:
    Apr 25, 2020
    Posts:
    2
    So, please, what is the solution to fix shadows flickering issue?
     
  14. darksamnikk

    darksamnikk

    Joined:
    Jul 2, 2020
    Posts:
    1
    guys solution is on temporal anti alising...lower jitter and all other settings...and shadows will stop flickering..just add post processing to scene in order to find temporal anti alising
     
    Revelation_Jeff likes this.
  15. J_Lewis_

    J_Lewis_

    Joined:
    Sep 22, 2020
    Posts:
    3
    Hello! As a quick fix for this, I found that disabling shadows on the directional light, fixed this flickering problem. Unity should definitely look into this though, as this really screws up devs. Hope this quick advice helps some. (Temporary Fix).
     
    clabbe likes this.
  16. GreenPandaStudios

    GreenPandaStudios

    Joined:
    Feb 4, 2020
    Posts:
    9
    I'm not completely sure about this, but it could arise due to floating-point error at very large numbers. For whatever reason, Unity transforms are configured using single precision floating points instead of double. This means you can get something akin to a rounding error when you move too far from the origin. I don't want to explain the binary architecture of floats here, but if you're interested, this video does a great job of touching on these issues:
     
  17. GreenPandaStudios

    GreenPandaStudios

    Joined:
    Feb 4, 2020
    Posts:
    9
    You should not be moving the player in an endless runner. You should move the world around the player. It's quite odd at first, but a computer doesn't conceptualize numbers like us, and it can only store a certain amount of precision. Hopefully this helps: if I say 0 and 0.1, you would recognize those as different numbers. If I say 11 million and 11,000,000.1 you would likely just round that to 11 million. Large numbers eat up the precision, so you should always keep the camera near the origin.
     
    arkano22 and chadfranklin47 like this.
  18. crace96

    crace96

    Joined:
    Apr 25, 2017
    Posts:
    3
    Setting my Far plane to 1500 on a 4km/4km terrain took away my flickering issue. This should not even be an issue to begin with but thats what i did to work around it for now.
     
  19. GebhardtXR

    GebhardtXR

    Joined:
    Jan 10, 2020
    Posts:
    7
    Decreasing the shadow distance helped me with this issue
     
  20. Amo-deus

    Amo-deus

    Joined:
    Jul 27, 2015
    Posts:
    25
    This Works^^
     
  21. harplot28

    harplot28

    Joined:
    Jul 2, 2020
    Posts:
    3
    thanks it fixed my problem
     
    Pourya-MDP likes this.
  22. nathanjams

    nathanjams

    Joined:
    Jul 27, 2016
    Posts:
    304
    For myself, in Unity 2020.3 URP, dropping the Shadow's Max Distance from 500 to 100 in the Universal Render Pipeline Asset fixed the issue.
     
    GebhardtXR likes this.
  23. PixelPad

    PixelPad

    Joined:
    Dec 2, 2014
    Posts:
    3
    Thanks, that solved our problem too!
     
  24. Arithebee

    Arithebee

    Joined:
    Apr 4, 2017
    Posts:
    4
    When will Unity stop all this stuff? I have so many issues with this engine that i'm so tired
     
  25. pistoleta

    pistoleta

    Joined:
    Sep 14, 2017
    Posts:
    539
    I have a windmill on a 3d environment. Shadow casting and receiving...
    The shadows of the trees around are stuttering when the windmill wings rotate... If I stop the wings or turn off 'receive shadow' on them the stuttering stops.
    Is this the same topic we are talking about ?

    If you notice in the video: when the windmills are being rendered the shadows stutter a lot. Once I move away the shadows go back to normal.


    PS: when camera rotates shadows stutter too, even if its not seen in the video.
     
    Last edited: Sep 16, 2021
  26. atomicjoe

    atomicjoe

    Joined:
    Apr 10, 2013
    Posts:
    1,869
    Have you tried setting the shadows to STABLE FIT instead of CLOSE FIT?
     
  27. pistoleta

    pistoleta

    Joined:
    Sep 14, 2017
    Posts:
    539
    Thanks a lot for answering. I tried doing that but what I get is this:
    https://i.ibb.co/gjGnyJ0/Captura-de-pantalla-2021-09-16-a-las-16-07-56.png

    But here says...
    "Stable Fit projection basically does each shadow cascade as a circle, centered around some point. So that when camera rotates, the shadow size/resolution can stay exactly the same, with no "shadow edges swimming" artifacts. And since it's a circle, the shadow face-out range is circular too."
    thats why the edges won't have shadows....

    Still investigating...
     
  28. atomicjoe

    atomicjoe

    Joined:
    Apr 10, 2013
    Posts:
    1,869
    Increase the maximum shadow distance and it will fix this.
     
  29. pistoleta

    pistoleta

    Joined:
    Sep 14, 2017
    Posts:
    539
    Yeah I just tried but it doesn't work, removed the cascades and set shadow distance to 1000 but still...
     
  30. pistoleta

    pistoleta

    Joined:
    Sep 14, 2017
    Posts:
    539
    You mean this one right?
     
  31. atomicjoe

    atomicjoe

    Joined:
    Apr 10, 2013
    Posts:
    1,869
    yes. Set it to 9000.
     
  32. pistoleta

    pistoleta

    Joined:
    Sep 14, 2017
    Posts:
    539
    It didnt work, 10000, 100000 whatever... I tried setting the clipping plane further and then it worked.
    Sadly the shadows lose a lot of quality by using STABLE FIT so we are considering to move to URP....
     
  33. pistoleta

    pistoleta

    Joined:
    Sep 14, 2017
    Posts:
    539
    Does anybody know if this problem is fixed in URP ?
     
  34. robert-nally

    robert-nally

    Joined:
    Mar 5, 2014
    Posts:
    76
    Try setting the Normal Bias higher or lower on your light. For me the flickering stopped when I put the normal bias at max which was 3.
     
  35. robert-nally

    robert-nally

    Joined:
    Mar 5, 2014
    Posts:
    76
    Could also try this from the issue tracker. I think this worked for me before.

    "I had this issue on Unity 2019.2 with large terrains and large shadow distance, but changing the clipping plane of the Camera from the default of 0.1 to 0.5 fixed it for me."
     
    FORMFUL and wraex like this.
  36. steryo

    steryo

    Joined:
    Feb 7, 2017
    Posts:
    8
    To fix flickering shadows on moving platforms, reduce shadow distance to prevent overlapping shadows which was the issue for me. Hope this helps someone
     
  37. pauldelacrz

    pauldelacrz

    Joined:
    Jan 1, 2015
    Posts:
    144
    Guys, another way to resolve this issue is just to increase the Normal Bias from 0 to 3. This one simple change was able to fix the issue for me.
     
  38. mono08022000

    mono08022000

    Joined:
    Sep 20, 2015
    Posts:
    1
    I fixed the issue by changing the camera's Near ClippingPlanes from 0.01 to 0.1.
     
  39. Hossein5017

    Hossein5017

    Joined:
    Oct 9, 2017
    Posts:
    4
    enabled softshadows in shadows in universal renderer pipeline asset fixed for me

    Capture.PNG
     
  40. pihik

    pihik

    Joined:
    Mar 15, 2022
    Posts:
    13
    You can simply add Post Processing from Package Manager, put Anti-aliasing to Temporal Anti-aliasing (TAA)
     
  41. bluescrn

    bluescrn

    Joined:
    Feb 25, 2013
    Posts:
    642
    This thread is about all sorts of shadow issues now... but I do think there's a problem with shadow caster culling when you get a certain distance from the origin. I'm in URP, 2021.3.2f1, and seem to be able to quite easily create cases where *spotlight* shadow caster culling fails.

    All you have to do is move away from the origin. Build a scene at 5000, 5000, 5000 with a shadow-casting spotlight, then rotate the spotlight and watch for the shadows to disappear at certain angles.

    Move the entire scene to 2500,2500,2500, and I could still rereate the problem, but only at more specific angles. Couldn't reproduce it at 2000,2000,2000 or closer to the origin.

    Left: Shadows working, Right: Just rotated the spotlight a little, shadow vanishes. If the whole scene is moved closer to the origin, it works.

    upload_2023-1-13_15-57-21.png

    (This test was done with a little test project and unmodified URP, but I ran into this while trying to hack in camera-relative rendering to a modified version of URP, as I'm dealing with a moving vehicle in a scene that needs to at least handling +/-5km from the origin without becoming a wobbly mess. But this vehicle interior contains a spotlight, which starts flickering due to this issue)
     
  42. Nick-Carver

    Nick-Carver

    Joined:
    Feb 20, 2016
    Posts:
    13
    This fixed the issue for me also.
     
  43. rgrlee71

    rgrlee71

    Joined:
    Sep 11, 2012
    Posts:
    37
    If you have a single player game and you need to move the player a long distance, I have a script that I wrote years ago, that was adapted from something I read. The script will reset the player back to 0,0,0 and all the game objects and particles in the scene will be reset in relation. It works wonders and is imperceptible when it happens. I use it in every one of my games. I'm using it now in a space game where I can fly as far as I want and my shadows never glitch. Here is the code if anyone wants to try it.

    using UnityEngine;

    public class PlayerReset : MonoBehaviour
    {
    public float distance_limit = 2000.0f;
    Object[] objects;
    Vector3 origin = new Vector3(0, 0, 0);
    Vector3 totalOffset;
    Vector3 playerPOS;

    ParticleSystem.Particle[] parts = null;

    [System.Obsolete]
    void LateUpdate()
    {
    playerPOS = gameObject.transform.position;
    float x = playerPOS.x;
    float y = playerPOS.y;
    float z = playerPOS.z;
    Vector3 offset = new Vector3(x, y, z);

    //if the player is beyond the distance limit,from origin, reset all scene objects.
    if (playerPOS.magnitude > distance_limit)
    {

    //offset all game objects.
    objects = FindObjectsOfType(typeof(Transform));
    foreach (Object o in objects)
    {
    Transform t = (Transform)o;
    if (t.parent == null && t.gameObject.layer != 5 && t.gameObject.layer != 4) //ignore UI and Water layers
    {
    t.position -= playerPOS;
    }
    }

    // Move all the particles
    objects = FindObjectsOfType(typeof(ParticleSystem));
    foreach (UnityEngine.Object o in objects)
    {
    ParticleSystem sys = (ParticleSystem)o;

    if (sys.simulationSpace != ParticleSystemSimulationSpace.World)
    continue;

    int particlesNeeded = sys.maxParticles;

    if (particlesNeeded <= 0)
    continue;

    bool wasPaused = sys.isPaused;
    bool wasPlaying = sys.isPlaying;

    if (!wasPaused)
    sys.Pause();

    // ensure a sufficiently large array in which to store the particles
    if (parts == null || parts.Length < particlesNeeded)
    {
    parts = new ParticleSystem.Particle[particlesNeeded];
    }

    // now get the particles
    int num = sys.GetParticles(parts);

    for (int i = 0; i < num; i++)
    {
    parts.position -= playerPOS;
    }

    sys.SetParticles(parts, num);

    if (wasPlaying)
    sys.Play();
    }

    }
    }
    }
     
  44. TobyKaos

    TobyKaos

    Joined:
    Mar 4, 2015
    Posts:
    214
    Hi @rgrlee71 In term of optimisation, is it possible to active Occlusion ? Static Batching ....
     
  45. rgrlee71

    rgrlee71

    Joined:
    Sep 11, 2012
    Posts:
    37
    Sorry for the late reply. I've never tried occlusion with this but I see no reason why it wouldn't work. Static batching definitely works. I use it with thousands of geometry asteroids in my current project and it doesn't cause any hitching or stutters when it happens. I don't even notice when it updates. The only problem I've come across is in using trail renderers. When they are moved, the trail continues to draw and I've never worked on fixing it.
     
  46. clabbe

    clabbe

    Joined:
    Aug 9, 2013
    Posts:
    26
    Lol