Visualizations
013 / 500

013 · 3D

Path Tracer

Photorealistic light, computed by bouncing millions of random rays.

This is how film renderers make light look real. For each pixel, a ray bounces randomly around a lit room of matte, mirror, glass and glossy spheres, and at every matte bounce a shadow ray also checks the ceiling light directly, which lets the image converge in seconds. Samples add up in a floating point buffer, so soft shadows, color bleeding from the red and green walls, and the caustic under the glass ball all emerge on their own. The image starts noisy and sharpens as samples accumulate.

Try it. Drag to move the camera, which restarts the render. Click a sphere to cycle its material.

  • Monte Carlo path tracing
  • Next event estimation
  • Progressive accumulation

View the source · one module, plus a small shared runtime for sizing, the animation loop and input

Build your own

Paste this into Claude Code, Codex or any coding agent to get a simple version running, then take it wherever you like.

Build a progressive path tracer of a Cornell box with JavaScript and the HTML canvas element. Put everything in a single index.html file with no libraries or build step, so I can open it directly in a browser.

Start simple:
- Render into a small offscreen canvas (around 240 by 160) with ImageData and draw it scaled up to fill the window.
- Build the scene from planes and spheres: a room with a red left wall, a green right wall, white floor, ceiling and back wall, a square light in the ceiling, and two or three spheres on the floor.
- For each pixel, shoot a ray from a pinhole camera. When it hits a matte surface, bounce it in a random cosine-weighted direction and multiply the path's color by the surface color. When it hits the light, add the light's brightness. Stop after about five bounces.
- Keep a Float32Array with the running sum for every pixel. Each frame add one more sample per pixel (or as many as fit in about 12 ms) and display the average, so the noisy image visibly converges.

Once that works, make it beautiful:
- Add next event estimation: at every matte bounce, also pick a random point on the light, send a shadow ray to it, and add its contribution if nothing blocks it. Then ignore the light when a matte bounce hits it directly. The image will clean up dramatically.
- Give one sphere a mirror material and one sphere glass: refract with Snell's law and choose reflection or refraction using the Fresnel equations, so a caustic appears under it.
- Tone map with the ACES filmic curve and apply gamma before writing pixels, and show the samples per pixel in a corner.

Explain the key ideas in short code comments. When you're done, tell me how to open it and suggest three directions I could take it next, such as depth of field, rough metals and glossy plastic, or a simple denoiser that blurs only across similar surfaces.
PreviousVoxel DioramaA floating voxel island with baked AO, sun shadows and a waterfall. Click to build. NextTotalityA total solar eclipse from a lakeshore, driven by the real shadow geometry.

Related visualizations

  • Light & ShadowPhysics Red, green and blue lights cast crisp, colored shadows through spinning shapes.
  • Optical BenchPhysics A blueprint ray tracer: prisms, lenses, mirrors and a fiber with real glass dispersion.
  • Interior Mapping3D A night skyline of thousands of furnished, lit rooms, and not one of them has geometry.
  • MandelbulbFractals A 3D fractal, ray marched pixel by pixel on the CPU.
  • MiragePhysics Bent light over hot asphalt and cold sea: road puddles, floating cars, Fata Morgana.
  • Dynamical BilliardsPhysics A fan of 1,000 balls in reshapable tables, linked live to the Birkhoff phase map.
  • Near Light SpeedPhysics Drive a city street near light speed and see what relativity really does to the view.
  • SOFAR ChannelPhysics Sound rays bend into the ocean's deep channel, lighting up caustics and shadow zones.
  • Checker ShadowIllusions Squares A and B are the exact same gray. Move the light and the shadow keeps lying.

Use ← and → to move between demos. While the canvas has focus, keys go to the demo instead.

← More from Emergent Mind Labs