Exponential Fog - Advanced Rendering - Shader Learning

Advanced Rendering

Exponential Fog

Task

Modify the exponential fog calculation so that the fog effect starts only at a distance of 8.0 units from the camera. Before reaching this distance, the fog factor will be 0, meaning no fog effect is applied. The fog become fully opaque at a distance of 13.0 units.

Theory

Unlike linear fog, which increases linearly with distance, exponential fog increases exponentially, providing a more natural and realistic appearance. In the real world, fog density often increases exponentially with distance.


How Exponential Fog Works


Exponential fog calculates the fog factor based on density parameter and the distance between the camera and the object. The fog factor determines how much of the object's color is blended with the fog color:

fogFactor = 1.0 − exp(−density * fragDistance)

The fog factor is then clamped to the range [0.0, 1.0] to ensure it stays within valid bounds.


Far Far Distance


Sometimes, it is more convenient to specify the far fog distance rather than the density value. This is because the far fog distance directly relates to the visual effect you want to achieve in your scene. By specifying the distance at which the fog becomes nearly opaque, you can easily control how the fog interacts with objects at various distances, making it simpler to achieve the desired atmospheric effect without having to manually adjust the density value.


To determine the fog density, we need to establish the distance at which the fog becomes nearly opaque. Typically, this is the distance where the fog intensity reaches 99% of its maximum value, i.e., fogFactor = 0.99.


By substituting this value into the equation, we get:

0.99 = 1.0 − exp(−density * fogFar)

solving this equation for density:

exp(−density * fogFar) = 0.01

taking the natural logarithm of both sides:

−density * fogFar= ln(0.01)

solving for density:

density = -ln(0.01) / fogFar

The natural logarithm of 0.01 (ln(0.01)) is approximately -4.605. Therefore, the formula for fog density becomes:

density ≈ 4.605 / fogFar