Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice
  3. Dismiss Notice

Depth map generation and subsequent distance reconstruction

Discussion in 'High Definition Render Pipeline' started by FedericoVasile1, Jun 3, 2022.

  1. FedericoVasile1

    FedericoVasile1

    Joined:
    Oct 18, 2021
    Posts:
    3
    Hi,

    I'm trying to obtain a depth map of the entire scene in a HDRP project.
    There are some working packages that can achive this, e.g.
    https://github.com/needle-mirror/com.unity.recorder
    or
    https://github.com/needle-mirror/com.unity.simulation.capture

    But, once the depth image is saved (as a .jpg or .png image) on disk, I would like to do some post-processing on it outside of Unity (e.g. with python). What I want to do is: given the depth image, I want to read it and reconstruct back the distance for each pixel in the image. The distance I mean is the camera distance (along z axis), which is the one with which the depth image has been generated by Unity.

    To reconstruct the distance, I have to know which formulas Unity applies to the distance in order to construct the depth image. Knowing this, I can apply the inverse formulas to reconstruct the distance from the depth image.

    About this, taking for example the com.unity.simulation.capture package, there is this shader
    https://github.com/needle-mirror/co...time/Resources/USimBlitToDepthHDRP.shader#L25
    in which the FullScreenPass function is where the distance to depth image conversion happens.
    Code (Boo):
    1.             float4 FullScreenPass(Varyings varyings) : SV_Target
    2.             {
    3.                 UNITY_SETUP_STEREO_EYE_INDEX_POST_VERTEX(varyings);
    4.                 float depth = LoadCameraDepth(varyings.positionCS.xy);
    5.                 depth = Linear01Depth(depth, _ZBufferParams);
    6.                 return float4(depth, depth, depth, 1);
    7.             }
    Within this function, the Linear01Depth function simply takes the depth value and converts it from a non-linear to a linear [0, 1] range (considering near and far clipping planes).
    While the LoadCameraDepth function is where I suppose the conversion from the raw distance (i.e. distance along z axis of the camera) to the non linear [0, 1] range happens. However, I can't find how this conversion happens, which formulas are applied?

    Thank you