perspectiveDepthToViewZ
Converts a non-linear [0, 1] depth value from a perspective camera into a linear, physically meaningful view-space Z coordinate.
Core Advantages
Encapsulates the complex inverse depth calculation into a single function, providing the essential linear depth data required for advanced post-processing (like SSAO, DoF), making their implementation more intuitive and physically correct.
Common Uses
Custom fog or underwater effects
Reconstructing world position from a depth map (for deferred rendering, SSAO)
Implementing accurate Depth of Field (DoF) effects
How to adjust
This is a precise mathematical tool; its `near` and `far` parameters must strictly match the properties of the camera that generated the depth map. Any mismatch leads to incorrect results. For example, providing a smaller `far` value than the original camera's will "flatten" the reconstructed depth, causing distant fog to appear earlier and denser than expected.
Code Examples
1// Sample a non-linear depth value from a depth texture
2const nonLinearDepth = depthTexture.uv( uv() ).r;
3
4// Convert it to a linear view-space Z coordinate
5const viewZ = perspectiveDepthToViewZ( nonLinearDepth, cameraNear, cameraFar );