viewZToOrthographicDepth
Linearly converts a view-space Z coordinate into the standardized [0, 1] depth range required by the GPU's depth buffer, specifically for an orthographic camera.
Core Advantages
Provides a linear depth distribution, ensuring uniform precision across the entire view frustum. This is critical for applications that require accurate measurements and distortion-free rendering, like CAD or 2D games.
Common Uses
Rendering 2D games or UI elements to ensure correct depth sorting and layering
Displaying models in CAD, engineering, or architectural drawings with distortion-free orthographic views
Generating linear depth maps for specific post-processing effects like linear fog or NPR outlines
How to adjust
As a pure function, its behavior is tuned by changing the `near` and `far` properties of the orthographic camera. A smaller distance between `near` and `far` concentrates the depth precision, while a larger distance spreads it out, potentially increasing the risk of Z-fighting for distant objects.
Code Examples
1
2// Get the z-component of the view-space position.
3const viewZ = positionView.z;
4
5// Linearly maps the viewZ range of [-near, -far] to the depth range of [0, 1].
6const orthoDepth = viewZToOrthographicDepth( viewZ, cameraNear, cameraFar );
7
8// Assign the result to the material's depth property.
9material.depth = orthoDepth;
10