./jerome{m}jack in

~/Labs/Optics Lab.md · lab · 4 min · updated

Optics Lab

2026 · Rust · WebAssembly · WebGL2 · How this site works

Ray tracing usually draws surfaces and hides the rays. The Optics Lab flips that around: it draws the light itself. Put a beam, a glass prism, a spherical lens, a curved mirror or a gravitational lens on a dark bench, and a small Monte Carlo spectral tracer fires hundreds of thousands of wavelengths through the scene, accumulating them into a floating-point buffer until rainbows and caustics bloom out of the dark. Drag anything to move it, drag the handle or scroll to turn it, and press H to hide the outlines. Your whole bench fits in a link.

optics · livePick a preset or add elements from the right panel. Drag to move, drag the circle handle (or scroll) to rotate, H hides outlines, D duplicates, Del removes. Copy link shares the whole optical bench.open ↗

Spectral rays, not RGB

An RGB ray tracer can't split white light into a rainbow, because refraction depends on wavelength: blue light slows down more in glass than red light does. Here every ray starts by drawing a single wavelength \lambda between 390 nm and 690 nm from a low-discrepancy sequence, and carries its colour from a smooth analytic fit to the CIE 1931 colour-matching functions, balanced so a uniform mix across the band sums back to neutral white.

When that ray meets glass, its refractive index comes from Cauchy's equation:

n(\lambda) = A + \frac{B}{\lambda^2}

The lab anchors n at the sodium D-line (589.3\text{ nm}) and lets the Cauchy coefficient B bend shorter wavelengths harder than longer ones. In prism, that spreads one white beam into a continuous spectrum; in lens, it creates real chromatic aberration — blue rays cross the optical axis ahead of red ones; in raindrop, sunlight reflecting once off the back of a water sphere piles up along Descartes' 42° rainbow angle, red outside and violet inside.

Fresnel and total internal reflection

At every glass boundary, the tracer solves the exact unpolarised Fresnel equations (R = \tfrac{1}{2}(R_s + R_p)) and decides stochastically whether the ray reflects or refracts. Because a progressive frame fires thousands of rays, even a 4% reflection off the front face of a prism or lens shows up cleanly without an exponential ray tree.

When a ray tries to leave dense glass at an angle steeper than the critical angle \theta_c = \arcsin(1 / n), \sin^2\theta_t \ge 1 and the Fresnel reflectance jumps to 100%: total internal reflection. You see it in the fiber preset, where a tilted beam zigzags down a glass slab, and in prism if you rotate the flint triangle far enough that the far face turns into a mirror.

Caustics and curved mirrors

A caustic is the bright envelope where a family of reflected or refracted rays crowds together. In caustic, parallel rays striking a circular concave mirror don't meet at a single point — spherical aberration spreads their crossings along a glowing two-cusped curve called a nephroid, the exact bright heart you see at the bottom of a mug of coffee in sunlight.

Continuous media: gravitational lenses

Prisms and lenses bend light abruptly at a boundary. A gravity well bends it continuously through a smooth gradient-index (GRIN) field:

n(r) = 1 + k\left(1 - \frac{r^2}{R^2}\right)^2

At the outer edge r = R, both n - 1 and \nabla n vanish smoothly, so entering rays never kink. Inside, the engine integrates the Hamiltonian eikonal ray equations:

\frac{d\mathbf{r}}{dt} = \mathbf{v}, \qquad \frac{d\mathbf{v}}{dt} = n(\mathbf{r})\,\nabla n(\mathbf{r})

with fourth-order Runge–Kutta (RK4), emitting short glowing segments until the ray escapes or falls into the central dark event-horizon core.

How it's built

The tracer is a zero-dependency Rust crate compiled to a 39 KB WebAssembly module with no bindings, like the other labs'. Each animation frame traces 3,200 quasi-random rays per emitter (1,400 while you drag, so movement stays at 60 fps) and writes flat [x0, y0, x1, y1, r, g, b] segments straight into linear memory.

WebGL2 draws those segments as instanced screen-aligned ribbons with a Gaussian cross-section, added into an RGBA16F floating-point accumulation buffer — the same glow-instead-of-clip idea behind the Byte Lab's 3D cloud. Each frame divides by the total rays traced so exposure stays constant while the image refines over about a second, then runs a soft two-radius bloom and ACES filmic tone mapping so intense caustic cusps burn cleanly toward incandescent white instead of clipping to flat colour.

Back to Labs · see light on surfaces in Physically Based Rendering · look at the path-traced stills in JM-01 Cyberdeck · see how this site works.