rangeFogFactor
A node that calculates a distance-based linear fog factor. It generates a smooth 0-to-1 transition value based on the distance from the camera within a specified `near` and `far` range.
Core Advantages
Encapsulates the core fog logic of getting depth and using `smoothstep` for interpolation, greatly simplifying the creation of custom fog. It is performant and provides a natural transition, avoiding abrupt edges.
Common Uses
Implementing standard scene fog by mixing object color with a fog color.
Creating distance-based detail fade-out (LOD) by controlling the transparency of distant objects.
Simulating atmospheric perspective by blending distant objects with the sky color.
How to adjust
Control the fog's distribution by adjusting the `near` and `far` parameters. Increasing `near` pushes the fog back, enlarging the clear area. Decreasing `far` makes the fog transition sharper, creating a 'fog wall' effect. The difference between `far` and `near` determines the width of the transition zone from clear to dense fog.
Code Examples
1// Define the fog's start and end distances
2const fogNear = uniform(10);
3const fogFar = uniform(50);
4
5// Calculate the fog factor (0-1)
6const fogFactor = rangeFogFactor( fogNear, fogFar );
7
8// Mix the object color and fog color based on the factor
9const finalColor = mix( objectColor, fogColor, fogFactor );