Implementing Exponential Fog for Depth Perception
In this lesson, you will learn how to create a mathematically driven fog effect that increases in density as objects move further from the camera, providing essential depth cues for massive 3D environments.
The Physics of Exponential Decay
While linear fog fades objects at a constant rate, Exponential Fog uses the natural laws of atmospheric light scattering. The density increases rapidly as the distance increases, following the formula . This approach mimics how thick air obscures distant mountain ranges or sprawling cityscapes much more realistically than a simple gradient, giving your players an intuitive sense of scale.
Implementing in the Shader Graph
To implement this, you need to tap into the Scene Depth and the Camera Position nodes. You are calculating the distance between the pixel and the camera in World Space.
- Calculate Distance: Subtract the
Camera Positionfrom theAbsolute World Position. Use theDistancenode on this result to get the raw distance value. - Apply Exponential Logic: Pass this distance through a
Multiplynode (to control fog density) and then into aPowernode (or use anExpnode). - Lerp the Color: Use the resulting value as the T input of a
Lerpnode. The A input is your object’s base color, and the B input is your desired fog color.
// Conceptual Shader Logic
float dist = distance(WorldSpaceViewDir(i.pos), _WorldSpaceCameraPos);
float fogFactor = 1.0 - exp(-pow(dist * density, 2));
float3 finalColor = lerp(objectColor, fogColor, fogFactor);
By connecting this to your Fragment output, you ensure that distant geometry smoothly transitions into your fog color, preventing "pop-in" and grounding your distant assets within the atmosphere of your scene. This technique is vital for wide-open levels where you want to hide the horizon line while emphasizing the silhouette of foreground objects.
Key Takeaway: Exponential fog uses a mathematical decay curve to mimic atmospheric density, providing a superior sense of depth and scale compared to linear distance fading.