depth2viewZ: Depth Buffer to View Space Conversion
Camera Projection Depth Transformation Functions
The depth2viewZ
family of functions converts normalized depth buffer values to linear view space Z coordinates. These functions handle the mathematical transformation required to reconstruct world positions from depth textures in camera projection systems.
Mathematical Foundation
For perspective projection, the transformation follows:
For orthographic projection:
Where:
- = normalized depth buffer value [0,1]
- = near clipping plane distance
- = far clipping plane distance
- = linear view space Z coordinate
Function Variants
Function | Purpose | Parameters |
---|---|---|
depth2viewZ | Perspective projection | depth , near , far |
depth2viewZOrthographic | Orthographic projection | depth , near , far |
depth2viewZCombined | Unified function | depth , near , far , orthographic |
Live Editor
const fragment = () => { const depth = sin(uv.x.mul(8)).mul(0.5).add(0.5) const near = float(1) const far = float(20) const ortho = step(0.5, uv.y) const viewZ = depth2viewZCombined(depth, near, far, ortho) const color = viewZ.div(far).add(0.5) return vec4(vec3(color), 1) }