title: Metal + ARKit — Get z distance to current fragment in fragment shader
body: <p>I have a metal shader (written as a <code>SCNProgram</code> for an ARKit app) that takes a depth map of the current scene captured from <a href="https://developer.apple.com/documentation/arkit/arframe/3674209-smoothedscenedepth" rel="nofollow noreferrer"><code>smoothedSceneDepth</code></a>. I would like to use the captured depth information to discard parts of any of my virtual objects that are behind a real world object. However I am having trouble getting the expected fragment depth in my shader.</p>
  <p>Here's my basic fragment shader:</p>
  <pre class="lang-cpp prettyprint-override"><code>struct ColorInOut {
      float4 position [[ position ]];
      float2 depthTexCoords;
  };
  
  fragment float4 fragmentShader(
      const ColorInOut in [[ stage_in ]],
      depth2d&lt;float, access::sample&gt; sceneDepthTexture [[ texture(1) ]]
  ) {
      constexpr sampler textureSampler(mag_filter::linear, min_filter::linear);
      
      // Closest = 0.0m, farthest = 5.0m (lidar max)
      // Seems to be in meters?
      const float lidarDepth = sceneDepthTexture.sample(textureSampler, in.depthTexCoords);
   
      float fragDepth = // ??? somehow get z distance to the current fragment in meters ???
  
      // Compare the distances
      const float zMargin = 0.1
      if (lidarDepth &lt; fragDepth - zMargin) {
           discard_fragement()
      }
  
      ...
  }
  </code></pre>
  <p>My understanding was that <code>position.z</code> in a fragment shader should be in the range of closest=0 to farthest=1. However when I tried converting this back to real world distances using the current camera planes, the result seem off:</p>
  <pre class="lang-cpp prettyprint-override"><code>const float zNear = 0.001;
  const float zFar = 1000;
  
  float fragDepth = in.position.z * (zFar - zNear);
  </code></pre>
  <p>When I debugged the shader using <code>return float4(fragDepth, 0, 0, 1);</code>, the red is brightest when I am closest to the object and then falls off as I back away. Even if I use <code>fragDepth = 1 - fragDepth</code>, the depth seems to differ from <code>lidarDepth</code>.</p>
  <p>Here's using <code>1 - fragDepth</code>:</p>
  <p><a href="https://i.stack.imgur.com/wKGsi.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/wKGsi.jpg" alt="Using 1 - fragDepth" /></a></p>
  <p>(I also tried using the mapping from <a href="https://stackoverflow.com/questions/6652253/getting-the-true-z-value-from-the-depth-buffer">this answer</a> but wasn't able to get it working)</p>
  <p>So my questions are:</p>
  <ul>
  <li><p>What coordinate system is <code>in.position.z</code> in?</p>
  </li>
  <li><p>How can I transform <code>in.position.z</code> into a depth value I can compare against the captured depth information I already have? (or vice versa)</p>
  </li>
  </ul>