linearizeDepth: Depth Buffer Linearization
Perspective Depth Buffer to Linear Space Conversion
The linearizeDepth
function converts non-linear depth buffer values to linear depth coordinates. This transformation corrects the perspective projection's non-uniform depth distribution, providing linear interpolation between near and far clipping planes.
Mathematical Foundation
The linearization formula inverts perspective projection depth encoding:
Where:
- (depth mapped from [0,1] to [-1,1])
- = near clipping plane distance
- = far clipping plane distance
- = linear depth value
Live Editor
const fragment = () => { const radialDepth = length(uv.sub(0.5)).mul(2) const depth = clamp(radialDepth, 0, 1) const near = float(1) const far = float(50) const linear = linearizeDepth(depth, near, far) const color = linear.div(far.sub(near)) return vec4(color, color.mul(0.5), float(1).sub(color), 1) }