Course contentsShow
← Unity Game Development
Lesson 635 of 8289. Rendering and VisualsFree lesson

Implementing Exponential Fog for Depth Perception

Master the use of distance-based fog to obscure distant geometry and create a sense of scale within your 3D environments.

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 Density=1−e−(distance⋅density)2Density = 1 - e^{-(distance \cdot density)^2}. 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.

  1. Calculate Distance: Subtract the Camera Position from the Absolute World Position. Use the Distance node on this result to get the raw distance value.
  2. Apply Exponential Logic: Pass this distance through a Multiply node (to control fog density) and then into a Power node (or use an Exp node).
  3. Lerp the Color: Use the resulting value as the T input of a Lerp node. 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.