Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Join us on Thursday, June 8, for a Q&A with Unity's Content Pipeline group here on the forum, and on the Unity Discord, and discuss topics around Content Build, Import Workflows, Asset Database, and Addressables!
    Dismiss Notice

Resolved How to correctly linearize AROcclusion environment depth texture?

Discussion in 'AR' started by Thokr, Mar 17, 2023.

  1. Thokr


    May 27, 2016
    I'm trying to implement this shader but instead of Z buffer I want to use AROcclusionManager environment depth texture.

    I replaced this code:

    Code (CSharp):
    1. //get depth from depth texture
    2. float depth = SAMPLE_DEPTH_TEXTURE(_CameraDepthTexture, screenPos);
    3. depth = Linear01Depth (depth) * _ProjectionParams.z;
    with this:

    Code (CSharp):
    1. // Get depth UV from the display matrix.
    2. float2 depthUv = mul(float3(uv, 1.0f), _DepthDisplayMatrix).xy;
    3. // Sample the environment depth (in meters).
    4. float depth = tex2D(_CurrentDepthTexture, depthUv).r;
    5. // Normalizing the depth
    6. depth = (depth - _MinDistance) / (_MaxDistance - _MinDistance);
    7. // Reversing the value so that brighter=closer darker=farther
    8. depth = 1.0 - depth;
    where _CurrentDepthTexture is AROcclusionManager.environmentDepthTexture, _DepthDisplayMatrix is ARCameraFrameEventArgs.displayTexture, _MinDistance and _MaxDistance are values from 0 to 32 that I can adjust with sliders at runtime.

    But it doesn't work very well, the decal appears very distorted and it doesn't stay in place.

    Is it possible at all to achieve the effect I'm looking for with the environment depth texture? If it is, what's the correct way to do so?

    Basically, I'm looking for an equivalent of Linear01Depth (I tried using it directly, but it didn't work).
  2. Thokr


    May 27, 2016
    Okay... never mind. Turns out that the values are already correctly linearized correctly out of the box. You don't need to do any conversion with something like Linear01Depth like you do with Z-buffer. I don't know why I didn't just try using it directly from the beginning.
    KyryloKuzyk and andyb-unity like this.