viewportDepthTexture
Provides direct sampling of the current viewport's depth map in the shader, returning a float value (0.0 for near, 1.0 for far) representing scene geometry distance.
Core Advantages
Drastically simplifies obtaining scene depth data by completely abstracting complex backend setups, thereby unlocking the implementation of advanced rendering techniques like SSAO and Depth of Field (DoF).
Common Uses
Screen-Space Ambient Occlusion (SSAO)
Depth of Field (DoF)
Soft Particles
Reconstructing World Position
How to adjust
Controlled by adjusting its `uvNode` and `levelNode` constructor parameters. `uvNode` changes the sampling location (e.g., applying an offset for edge detection), while `levelNode` changes the sampling precision (e.g., using a high Mipmap level for a blurred depth value to optimize performance).
Code Examples
1// Sample the depth value (0.0 to 1.0) at the current pixel's location.
2const depth = viewportDepthTexture();
3
4// Output the depth as a grayscale color.
5// (Near objects will be black, far objects will be white).
6return vec3( depth );