Three.js Reflection Probes (Box-Projected Cube Maps)
Local reflection probes in Three.js, with a CubeCamera in each of two differently lit rooms and parallax-corrected box projection for a moving chrome sphere and glossy floors, blending probes across the doorway.
Key topics: three.js reflection probe, cubecamera, box projection, parallax corrected cubemap, local reflections
Game Over
What this code does
- Probes: a `CubeCamera` per room renders the room into a `WebGLCubeRenderTarget`, with reflective objects hidden.
- Infinite Cube Maps: standard env maps assume the environment is infinitely far, so nearby walls reflect in the wrong place.
- Box Projection: the reflection ray is intersected with the room's bounding box, and the cube is sampled from the probe toward that hit.
- Probe Blending: shading mixes the warm and cool probes by position across the doorway.
- Real-Time vs Baked: probes can re-render every frame (12 extra renders) to catch the moving lamp, or be baked once.
- Controls: reflection mode, real-time probes, blend width, sphere motion and re-bake.
Related Demos
Continue Learning
JavaScript Implementation
// Box-projected cube map lookup (parallax-corrected reflections) — fragment shader excerpt
const boxProjection = `
vec3 R = reflect(-viewDir, normal);
vec3 t1 = (boxMax - worldPos) / R;
vec3 t2 = (boxMin - worldPos) / R;
vec3 tf = max(t1, t2);
float t = min(min(tf.x, tf.y), tf.z); // where the reflection ray exits the room
vec3 dir = worldPos + R * t - probePosition; // direction as seen from the probe
vec3 env = textureCube(probeCube, dir).rgb;`
const probe = new THREE.CubeCamera(0.1, 50, new THREE.WebGLCubeRenderTarget(256))
probe.position.set(-5, 2, 0)
probe.update(renderer, scene) // capture (hide reflective objects first)