Fog Scattering Playground (r183-inspired)

Atmospheric spotlight demo using exponential fog and additive beam scattering to visualize how light volumetrics react to fog density and penumbra.

Key topics: three.js fog scattering, volumetric spotlight, fogexp2, atmospheric lighting, r183

Overview

In clear air a spotlight beam is invisible; you only see where it lands. Add fog, smoke or dust and the beam itself becomes visible, because particles in the air scatter some of the light towards your eye. This demo combines Three.js exponential fog with an additive beam mesh to approximate that effect cheaply, and lets you see how fog density and the spotlight's shape interact.

How it works

THREE.FogExp2 blends every fragment towards a fog colour based on its distance from the camera: factor = 1 − exp(−(density × distance)²). Unlike linear fog, it has no hard start and end distances and looks more natural for atmospheric haze. It fades distant geometry, which gives depth, but it does not create light shafts on its own.

The shaft comes from a cone mesh aligned with a SpotLight. The cone uses additive blending and a procedural gradient texture drawn on a canvas, brightest near the light and fading along its length, which hides the banding a flat colour would show. As the spotlight's target sweeps around the scene, the cone follows it, and the real spotlight lights the floor and objects underneath. Changing the spotlight's penumbra changes both the softness of the pool on the floor and the beam's edge falloff, so the two stay visually consistent.

Exploring the demo

Increase fog density and notice that two things happen together: distant objects fade into the haze and the beam reads more strongly. Then soften the penumbra and watch both the pool and the shaft edges blur. Pause the sweep and orbit around the beam to judge it from different angles.

Practical advice

Tie beam opacity to fog density in your own scenes; a bright beam in clear air looks wrong. For a version with real shadows cast through the beam, see the volumetric spotlight lab; for the simplest form of the trick, see god rays; and for true volumetric scattering, explore the raymarched clouds.

What this code does

- Exponential Fog Base: `THREE.FogExp2` controls atmospheric thickness and distance fade.
- Spotlight Through Media: a `SpotLight` drives a cone-shaped additive beam to approximate forward scattering.
- Beam Texture Gradient: a procedural canvas gradient reduces harsh banding and keeps the shaft readable.
- Penumbra Interaction: changing spotlight penumbra reshapes soft beam falloff against fog.
- Dynamic Sweep: animated target motion demonstrates directional light scattering changes over time.
- Practical Use: useful as a lightweight volumetric look when full volumetric raymarching is unnecessary.

JavaScript Implementation

ES6+
import * as THREE from 'three'
import { OrbitControls } from 'three/examples/jsm/controls/OrbitControls.js'

const scene = new THREE.Scene()
scene.background = new THREE.Color(0x0f1628)
scene.fog = new THREE.FogExp2(0x0f1628, 0.08)

const camera = new THREE.PerspectiveCamera(65, width / height, 0.1, 120)
camera.position.set(0, 2.2, 7)

const renderer = new THREE.WebGLRenderer({ antialias: true })
renderer.setSize(width, height)
document.querySelector('#app').appendChild(renderer.domElement)

const controls = new OrbitControls(camera, renderer.domElement)
controls.target.set(0, 1, 0)
controls.update()

const spot = new THREE.SpotLight(0xbfd8ff, 18, 40, Math.PI / 6, 0.45, 1.2)
spot.position.set(0, 8, 2)
const target = new THREE.Object3D()
target.position.set(0, 0.5, -3)
scene.add(target)
spot.target = target
scene.add(spot)

const beam = new THREE.Mesh(
  new THREE.ConeGeometry(1.8, 11, 48, 1, true),
  new THREE.MeshBasicMaterial({
    color: 0xbad4ff,
    transparent: true,
    opacity: 0.45,
    depthWrite: false,
    blending: THREE.AdditiveBlending,
    side: THREE.DoubleSide
  })
)
beam.position.copy(spot.position)
beam.lookAt(target.position)
scene.add(beam)

function animate () {
  requestAnimationFrame(animate)
  controls.update()
  renderer.render(scene, camera)
}
animate()

Related demos

Trending this week

Most popular