Search Unity

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

Discussion in 'General Graphics' started by ytrewq, Feb 28, 2021.

  1. ytrewq

    ytrewq

    Joined:
    Aug 5, 2012
    Posts:
    42
    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?
     
  2. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,348
    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.
    https://developer.nvidia.com/content/depth-precision-visualized
     
  3. ytrewq

    ytrewq

    Joined:
    Aug 5, 2012
    Posts:
    42
    Thank you.
     
  4. chrismarch

    chrismarch

    Joined:
    Jul 24, 2013
    Posts:
    472
    I saw mention of this setting in older Unity versions, but I can't find it in Unity 2020.3 (URP, Android)
     
  5. Marcurion

    Marcurion

    Joined:
    Jul 29, 2015
    Posts:
    13
    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.
     
  6. IgorAherne

    IgorAherne

    Joined:
    May 15, 2013
    Posts:
    393
    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