Artifacts in directional lights shadows as the game progresses

Hi,

I have been working on a game of mine using Unity 2017 and have been constantly facing a weird graphical glitch. As you can see in the images, the quality of shadows go from good to unacceptably worse as the game progress. It is a simple endless game in which the light (directional) stays where it is and the camera moves in the forward direction. All the plausible reasons for such behaviour of shadows have been tested and not many make sense since the shadows are absolutely fine in the beginning of the game.

My current project settings are these -
1 Directional light, forward rendering path , 2X AA, shadows - soft shadows, medium resolution, 4 shadow cascades, stable fit, shadow distance - 60, near plane offset - 5 , Camera near clip - 1, far clip - 175.

Im sure that I am not doing anything crazy with lighting, this looks more like a glitch to me. Anyone ever experienced anything like this? I am tired of debugging it and now seek some help from you guys.

Thanks in advance.


Hi!

I assume that when one progresses in your game, the world coordinate of the avatar is further and further away from the origin (0,0,0). If that’s the case, you’re suffering from floating-point rounding errors. Google for “floating point precision large numbers”.

I dont really understand what I can do to make unity not suffer from those errors since creating shadow maps is not in my hands. Am I missing something here ?

Keep the camera looking at 0,0,0 and move the world instead. Or move everything back to zero every so often.

Ok that might be a solution , but this should not mean that we can not approach an endless runner with shadows like the way I did ( the obvious way). There is gonna be a huge set of changes that I will have to do to reverse my approach and I am sure that there should be a more viable explanation to this issue.

And one more thing, I dont understand why this floating point precision error doesnt affect the editor and only occurs on mobile?

Mobile can use lower precision than desktops (e.g. 16-bit floats instead of 32, which correspond to “half” and “float” in the shader). On the desktop it will be 32 bit regardless of what the shader says. (See https://docs.unity3d.com/Manual/SL-DataTypesAndPrecision.html, “Precision, Hardware Support and Performance”)
The same thing will occur in the editor, but you’ll need much larger numbers to see this effect.

Yes this makes complete sense. This explains why it is happening. But I was wondering that in my game it hardly goes beyond a Z value of 300-400 before it starts behaving like this. Is this because of the same float precision error on mobile ? And if yes , is there any solution to this other than moving the entire world back ?

The effect you see happens around the values you have when medium precision is used.
Are you using custom shaders or built-in ones?
And what device are you running it on?

We are using the built in standard shaders on almost everything in the game. We are experiencing this issue on android devices. I have tried it on xiaomi phones and pixel.

OK, this sounds suspicious enough to check this.
Please submit a bug report :slight_smile: