Forge linear fog

From Nebulavenus/forge-gpu · View interactive version

Forge linear fog is an AI agent skill from the GitHub repository Nebulavenus/forge-gpu. Add depth-based distance fog to an SDL GPU project. Supports linear, exponential, and exponential-squared modes. Fog is applied in the fragment shader after lighting, blending the lit color toward a fog color based on camera distance.

How to use this skill

Skill file

Linear Fog Skill

Adds distance fog to any SDL3 GPU scene. Derived from Lesson 20.

When to use

Fog uniform block (append to existing fragment cbuffer)

/* Append these fields to your existing fragment uniform buffer.
 * Total addition: 32 bytes (2 x float4 worth). */
float4 fog_color;     /* Must match the clear color (rgb)    */
float  fog_start;     /* Linear mode: distance where fog begins */
float  fog_end;       /* Linear mode: fully fogged distance     */
float  fog_density;   /* Exp/Exp2 modes: density coefficient    */
uint   fog_mode;      /* 0 = linear, 1 = exp, 2 = exp-squared  */

Matching C struct fields:

float fog_color[4];
float fog_start;
float fog_end;
float fog_density;
Uint32 fog_mode;

HLSL fog calculation (paste after lighting)

/* Compute Euclidean distance from fragment to camera. */
float dist = length(eye_pos.xyz - input.world_pos);

float fog_factor = 1.0;
if (fog_mode == 0)
{
    /* Linear */
    fog_factor = saturate((fog_end - dist) / (fog_end - fog_start));
}
else if (fog_mode == 1)
{
    /* Exponential */
    fog_factor = saturate(exp(-fog_density * dist));
}
else
{
    /* Exponential squared */
    float f = fog_density * dist;
    fog_factor = saturate(exp(-(f * f)));
}

/* Blend: fog_factor=1 → lit color, fog_factor=0 → fog color */
float3 final_color = lerp(fog_color.rgb, lit_color, fog_factor);

Typical parameter values

| Parameter | Value | Notes |

|-----------|-------|-------|

| fog_color | match your clear color | Must equal the framebuffer clear color |

| fog_start | 2.0 | Linear mode: fully visible before this distance |

| fog_end | 18.0 | Linear mode: fully fogged beyond this distance |

| fog_density (exp) | 0.12 | Exponential mode |

| fog_density (exp2) | 0.08 | Exp-squared mode |

Key rules

  1. Fog color = clear color. Always set the framebuffer clear color to the

same RGB as the fog color. Otherwise there will be a visible seam at the

horizon.

  1. Apply fog to ALL shaders. Every fragment shader in the scene (objects,

grid, terrain, particles) must apply the same fog with the same parameters.

Inconsistent fog breaks the atmospheric illusion.

  1. Use Euclidean distance, not Z depth. Compute length(eye - frag_pos)

in world space. Using only the Z component causes fog to shift when the

camera rotates.

  1. Fog goes after lighting. Compute the full Blinn-Phong (or PBR) result

first, then blend it toward the fog color. Applying fog to individual

lighting terms produces incorrect results.

  1. Clamp the fog factor. Use saturate() to keep the factor in [0, 1].

Without clamping, objects beyond the fog end distance can produce negative

values that invert the fog.

Common mistakes

between fogged geometry and the background.

fog while objects fade correctly (or vice versa).

because fragments at the same world position get different fog values.

fogged individually, producing darker or brighter results than expected.

modes at runtime requires updating the uniform each frame.

Reference

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.