GLSL shader example

Distance Fog in a Ray-Marched Scene

Hit distance drives exponential transmittance, mixing distant surfaces toward fog color rather than applying one flat screen overlay.

Live example

Edit the code and the preview updates as you type.

What to observe

Distance Fog in a Ray-Marched Scene.

Mechanism

Hit distance drives exponential transmittance, mixing distant surfaces toward fog color rather than applying one flat screen overlay.

The controlling code

p.z=mod(p.z+1.,2.)-1.;return min(length(p-vec3(.4,0,0))-.3,p.y+.55);

Experiment

With time paused, set the density expression .1+u_parameter*.7 to .8. Distant repeated spheres and the floor blend more strongly toward vec3(.08,.13,.23) than nearby surfaces do.

Debugging and cost

The ray loop stops at 80 steps or distance 12; fog is added only after a surface hit. Replace 1.-exp(-t*(.1+u_parameter*.7)) with 0.5 to see an equal-strength overlay that loses the near-to-far depth cue.

Built-in functions used

Learn it step by step