viewZToLogarithmicDepth
Converts a linear view-space Z coordinate to a logarithmic depth value, resolving Z-fighting artifacts in scenes with vast depth ranges by distributing precision more evenly.
Core Advantages
Enables stable rendering of scenes with enormous scale differences (like space sims or open worlds) by providing constant relative depth precision from very near to extremely far objects, effectively eliminating rendering errors and flickering for distant objects.
Common Uses
Rendering large-scale space or astronomical visualization scenes
Powering depth calculations for open-world games or flight simulators with long view distances
Ensuring visual fidelity at any zoom level in architectural or city-scale visualization applications
How to adjust
This is a pure function, adjusted by changing its inputs (primarily the camera's `near` and `far` properties). Increasing `far` extends the depth range and improves precision for distant objects; decreasing `near` concentrates precision on objects very close to the camera.
Code Examples
1
2// Get the z-component from the view-space position.
3const viewZ = positionView.z;
4
5// Calculate the logarithmic depth value.
6const logDepth = viewZToLogarithmicDepth( viewZ, cameraNear, cameraFar );
7
8// Assign the result to the material's depth property.
9material.depth = logDepth;
10