Infinity mirror room
I wanted to know if one WebGL 2 fragment shader could raytrace a 6-wall mirrored cube around a glowing HTML plaque, shift deeper bounces into soda-lime glass green, and keep the primary plaque live via projective updateElementGeometry(). Drag inside the room to orbit, type directly onto the floating plaque, switch LED palettes or drag the bounce depth between 2 and 14.
Show the code ElementGL texture upload, 6-wall mirror raytracer and projective updateElementGeometry()
- Floating plaque markup inside one drawable canvas. The plaque sits inside
<canvas content="drawable" layoutsubtree>with transparent rail gaps and open corner eyelets so mirror rays pass through the holes. - One WebGL 2 context through
ElementGL. On everypaintevent,stage.upload(plaque)callsgl.texElementSubImage2D()and rebuilds mipmaps so distant bounces stay legible. - 6-wall mirrored cube raytracer in GLSL. Loops up to 14 reflections inside
[-1, +1]^3, samples the double-sided plaque with analytic ray differentials viatextureGrad(), passes remaining light through transparent cutouts, and applies soda-lime glass absorption per bounce. - Double-sided projective
updateElementGeometry(). Maps the floating plaque quad throughP * V * M_world, flips its horizontal basis when orbited behindz = 0, scales bygeometryUnit()and togglesdataset.hitTick.
How it works
Everything draws on a single WebGL 2 context made with new ElementGL(canvas, ...) from /lib/element-gl.js. Inside <canvas content="drawable" layoutsubtree> sits one drawable plaque with an editable headline <input>, four LED palette buttons, a polka-dot LED border and a pulse button. Whenever you type or press a button, Chromium fires a paint event, stage.upload(plaque) updates the texture via gl.texElementSubImage2D() and gl.generateMipmap() refreshes the mip chain.
For each screen pixel, the fragment shader marches a ray inside a mirrored cube spanning [-1, +1]^3 for up to uMaxBounces reflections. Before hitting a mirror wall, the ray tests the central plaque at z = 0 and ten floating micro-lantern spheres in front-to-back order. Rather than calling implicit-derivative texture() inside a conditional loop, the shader carries two reflected pixel differential vectors rayDx and rayDy across each wall bounce and calls textureGrad() on the plaque texture. When a ray hits the hollow corner eyelets or the transparent air gap around the neon rails, (1.0 - tex.a) continues through the cutout to the mirrors behind it.
Each wall bounce reflects the ray and differential vectors across the hit axis, adds a faint mitered bevel highlight near wall edges, and multiplies throughput by uMirrorTint (vec3(0.86, 0.94, 0.90) at 100% tint). After 12 bounces, red drops faster than green, turning far-off plaques and micro-lanterns deep bottle-green like real soda-lime mirror glass. To keep the primary depth-0 plaque interactive from any angle, buildPlaqueCanvasMatrix() projects the 3D quad into canvas CSS pixels, swaps the local X axis when the camera orbits behind z = 0 so the back side stays upright and front-facing, scales by geometryUnit() from /lib/surface.js, calls updateElementGeometry() and toggles dataset.hitTick when the matrix moves. If WebGL 2 texElementSubImage2D() is missing, a 2D fallback draws nested green-tinted frames with 5-argument drawElementImage(); if HTML-in-canvas is missing or the context stays lost, the plaque moves into plain HTML below the hidden canvas.
Live checks
These checks run on isolated fixtures inside testStage() and wait for a tap so loading the page never opens extra WebGL contexts or moves focus.
| Status | Check | Detail |
|---|
Findings
- Measured in
HeadlessChrome/160.0.0.0on 2026-11-27 (412 px wide, DPR 2.625, withCanvasDrawElementenabled): all five live checks pass with 0 console errors, 0.992 MP canvas buffer (11.86 MB peak across all canvases), 8x anisotropic mipmaps, real pointer clicks hitting the Cyan palette,+1 Rippleand<input>controls on the front face, and drag-orbiting behindz = 0keeping theRedbutton clickable. Chrome 159 on Android andnpm run check:jankare not verified yet. - Carrying reflected screen-space ray differentials (
rayDx,rayDy) and sampling the plaque withtextureGrad()removes the 1-pixel mipmap halo along the plaque's outer edge inside the conditional bounce loop. - Removing outer CSS
box-shadowspread on the plaque rails and eyelets keeps the air gap and hollow corner eyelets atalpha == 0intexElementSubImage2D(), so virtual rooms show cleanly through the cutouts. - Flipping the plaque's UV X coordinate on back-face ray hits (
rd.z > 0) makes odd mirror bounces read backwards like a real two-sided lightbox in a mirror while keeping even bounces upright, and lets you read and click the back of the plaque directly when orbiting behindz = 0. Auto-driftstarts off and respectsprefers-reduced-motion: reduce, and ripple animations stop after 1.6 s so the GPU stays idle between interactions.