viewZToPerspectiveDepth
Performs the standard, non-linear (hyperbolic) transformation to convert a view-space Z coordinate into the [0, 1] depth value required by the GPU for a perspective camera.
Core Advantages
Distributes depth precision in a way that mimics human perception by allocating most of it to nearby objects, which is highly effective at preventing Z-fighting in the foreground for most real-time 3D applications.
Common Uses
As the default depth calculation method in the vast majority of 3D games (FPS, third-person, RPGs, etc.)
Rendering realistic 3D product visualizations and online configurators
Calculating depth in architectural or interior design walkthroughs to simulate realistic perspective
How to adjust
As a pure function, it's tuned by changing the camera's `near` and `far` properties. The key is to keep the `far/near` ratio as small as possible. A large ratio will cause severe Z-fighting in the distance, while a smaller ratio distributes precision more effectively, mitigating the issue.
Code Examples
1
2// Get the z-component of the view-space position.
3const viewZ = positionView.z;
4
5// Calculate the perspective-correct depth value.
6const perspectiveDepth = viewZToPerspectiveDepth( viewZ, cameraNear, cameraFar );
7
8// Assign the result to the material's depth property.
9material.depth = perspectiveDepth;
10