Three.js GPU Path Tracer (Progressive Cornell Box)
Progressive GPU path tracing in a Three.js fragment shader, rendering an analytic Cornell box with diffuse, mirror and glass materials, next-event estimation on the area light, Russian roulette and running-average accumulation.
Key topics: three.js path tracing, gpu path tracer, global illumination, cornell box, monte carlo, progressive rendering
Game Over
What this code does
- Monte Carlo: each pixel traces random light paths; averaging many noisy samples converges to the rendering equation.
- Next-Event Estimation: at diffuse hits a shadow ray goes to a random point on the area light, which cuts noise sharply.
- Materials: cosine-weighted diffuse bounces, perfect mirrors and glass with Schlick Fresnel choosing reflection or refraction.
- Russian Roulette: after a few bounces paths end with probability tied to their throughput, which stays unbiased.
- Progressive Accumulation: ping-pong float targets hold a running mean, mix(prev, new, 1/(n+1)), which restarts when the camera moves.
- Controls: max bounces, samples per frame, light power, materials, exposure and restart.
Related Demos
Continue Learning
JavaScript Implementation
// Progressive accumulation: each frame renders one noisy estimate and averages it in
// fragment shader: gl_FragColor = vec4(mix(previousAverage, thisFrameSample, 1.0 / (frame + 1.0)), 1.0);
const targets = [0, 1].map(() => new THREE.WebGLRenderTarget(width, height, { type: THREE.FloatType }))
let frame = 0
function animate () {
requestAnimationFrame(animate)
material.uniforms.uAccum.value = targets[frame % 2].texture
material.uniforms.uFrame.value = frame
renderer.setRenderTarget(targets[(frame + 1) % 2])
renderer.render(traceScene, quadCamera) // path-trace pass (camera rays → bounces → light)
renderer.setRenderTarget(null)
renderer.render(displayScene, quadCamera) // tone-map the running average
frame++
}
animate()