Z-buffer depth - is it possible to know it or set explicitely?

We have some z-fighting problems in our game, which possibly can be solved by using higher precision z-buffer depth. Usual tricks with tweaking near / far clip planes of the camera will not work, because we have wide object distance range - very close and very far. So, is it possible to set z-buffer depth to 24 or 32 bits explicitely?

Unless you’re on a mobile device, or already manually creating a render texture with a 16 bit depth, Unity uses a 32 bit depth buffer. Technically it requests a 24 bit depth buffer, but most GPUs don’t actually support that anymore and provide a 32 bit depth instead.

On Android devices that support it, there may be a setting in the Player settings to force 16 or 24 bit depth.

On desktop and consoles, Unity also uses a reversed Z which removes a lot of additional z precision issues. However on OpenGL using a reversed Z doesn’t actually help due to the clip space Z range being -1.0 to +1.0 instead of 0.0 to 1.0 like all other graphics APIs, so they don’t use reversed Z there. That means mobile devices running OpenGLES can’t take advantage of this, but they can if they’re running Vulkan or Metal.

4 Likes

Thank you.

I saw mention of this setting in older Unity versions, but I can’t find it in Unity 2020.3 (URP, Android)

The documentation notes that PlayerSettings.Android.use24BitDepthBuffer was deprecated in favor of PlayerSettings.Android.disableDepthAndStencilBuffers, which I find quite unintuitive as they seem to do totally different things.

Note that you can improve most Z-Fighting issues by reducing the range between Near and Far planes from Unity’s default 0.3 & 1000 meters, but the thread starter ruled this out already for his project.

Precision is highest at the near plane. So if you push away the Near plane (from 0.3 to maybe 0.5) you are offering all nearby objects more precision, but risk them getting culled.

If you pull the Far plane closer, you are bringing all the bad imprecision closer to you, so be careful.
Significant imprecisions and Z-fighting happens around far plane, so you want it as far as possible.
But without stretching entire frustum too much