Camera Height Based Fog - Advanced Rendering - Shader Learning

Advanced Rendering

Camera Height Based Fog

Task

Enhance a scene rendered using deferred shading by adding a camera based height fog effect. The fog density should increase with the height difference between the camera and objects. The fog should become fully opaque at a distance of 10 units from the camera. Use fogColor for blending.

Theory

Fog based on fragment and camera height is a technique used in computer graphics to simulate atmospheric effects where the fog density varies with the height of the camera and the fragment being rendered. This method creates a more realistic and immersive environment, especially in scenes with varying terrain elevations.


How It Works


The fog density is calculated using the heights of both the fragment and the camera:

fogFactor = 1.0 - exp(-density * distanceY * distanceY);

The greater the height difference between the camera and the object, the denser the fog. The fog factor is then clamped to the range [0.0, 1.0] to ensure it stays within valid bounds.