Building Vitrail: Stained-Glass Jenga in WebGPU & MuJoCo 4.0
From multi-block Beer-Lambert refraction and catching a Rapier solver regression to migrating onto Google DeepMind's MuJoCo 4.0 WASM, Brewster stress-optic polariscopy, Timoshenko modal acoustics, 2-ply counterfactual AI, and chaos-tested Online Rooms.
The premise sounded straightforward: build regulation-scale Jenga (7.5 × 2.5 × 1.5 cm, 71.1 g per block across 18 courses) out of leaded cathedral glass in raw WebGPU. What made it hard was that every graphics or rigid-body shortcut broke the physical illusion at once — single-surface refraction made blocks behind the front face vanish, contact compliance made the 54-prism stack sag and chatter, and translucent web overlays clashed with the studio backdrop.
The first milestone coupled multi-block raytraced Beer–Lambert transmission (n = 1.522), 2048×2048 Snell caustics, Sutherland–Hodgman 3D Voronoi fracture, and a pointed-arch stained-glass lancet HUD — catching an early solver regression when upgrading Rapier from 0.14.0 to 0.21.0 (TGS Soft inflating 18-course sag to 0.388 mm). Pinning Rapier 0.14.0 shipped v1, but sequential Gauss–Seidel velocity impulses still lacked elliptic torsional friction and multi-course force-chain coupling.
The second arc replaced Rapier3D entirely with Google DeepMind's @mujoco/[email protected] (4 WASM pthreads in a dedicated Web Worker, 600 Hz sparse Cholesky Newton Hessian, 0.098 mm calibrated 18-course sag, 54/54 native island sleep in 8.6 µs/step). Exposing exact efc_force contact wrenches unlocked dark-field Brewster stress-optic polariscopy (), first-principles Cowper–Timoshenko thick-bar modal acoustics (), zero-mutation mj_copyState 2-ply Cross-Entropy Glazier AI, 520-seed passivity fuzzing, and dual-transport Online Rooms (WebRTC RTCDataChannel with <= 4.0 s failover to a Cloudflare Durable Object SQLite relay), backed by 58/58 Vitest invariants and a full Science & Mathematics reference.
Latest Update
Two-ply counterfactual Glazier AI, 520-seed passivity fuzzing, and chaos-tested Online Rooms
Thu, Nov 12, 2026
Timeline
I started Vitrail by refusing to fake stained-glass Jenga with hollow meshes or particle-sprite shatters. Every prism is modeled at 1:1 regulation Jenga dimensions (7.5 × 2.5 × 1.5 cm, 71.1 g of soda-lime glass at 2.50 g/cm³ across 18 courses = 27.0 cm), raytraced as beveled dielectric solids (n = 1.522) wrapped in copper-foil solder cames. My first draft sprinkled floating dust motes into the room and burst broken blocks into generic fragments, which immediately read like a video game instead of heavy glass on wood. I ripped out the dust motes and wrote a 3D Sutherland-Hodgman half-space Voronoi clipper (MAX_ACTIVE_SHARDS = 512, <= 132 vertices per convex hull) seeded around the impact point. Across 200 deterministic fracture seeds, total shard volume stays within < 0.05% of the intact prism while separating leaded outer faces from raw conchoidal cleavage planes inside the shader.
Single-surface refraction fell apart the moment two colored prisms crossed behind each other — the back block vanished instead of tinting the light. I upgraded the WebGPU rgba16float pipeline to march primary rays (n = 1.522) through up to two additional interior stained-glass prisms behind the hit face, accumulating Beer-Lambert spectral extinction alongside a 2048×2048 vertex-refracted Snell waterglass caustic map and a 1024×1024 optical-depth PCSS shadow atlas. On the physics side, pulling a block initially made the whole 54-block stack chatter, and tumbled blocks kept jittering on the table. Switching to Froude-invariant (Fr = 1) scaling at 120 Hz dropped 18-row stack sag under 0.15 mm with 54/54 bodies sleeping cleanly, while ~0.09 mm micro-clearance variance lets you tap-test blocks (< 5 px pointer travel): free glass rings at 2.3 kHz and pinned load-bearing columns tick dryly at 3.8 kHz. After a four-year-old friend playtested the build and dictated feedback, I also wired full multi-touch orbit, pinch-zoom, and drag extraction so the whole tower plays natively on an iPad.
Once the tower optics locked in, standard translucent HUD cards looked pasted on top of the studio. Because the backdrop was a muted warm plaster, floating dark-glass rectangles barely registered as glass at all — they just looked like generic web chrome. I threw out the sidebar panels and rebuilt the navigation as a single pointed-arch gothic lancet window leaded with custom SVG solder cames, milk-opal title cartouches, and clickable clerestory sky panes (Morning, Vespers, Noon) that shift the studio sun angle directly. Each glass pane (QuarryButton) tracks pointer coordinates for specular sweep and physically recesses on pointerdown before rebounding on an underdamped spring. On the right margin, tower telemetry moved onto a frameless wheel-etched crystal plate with acid-etched corner registration marks and a live center-of-mass plumb compass.
Upgrading @dimforge/rapier3d-compat from 0.14.0 to 0.21.0 during a toolchain modernization pass silently wrecked the tower's structural physics. My T1–T6 Vitest physical tolerance suite caught the regression immediately: Rapier 0.15+ replaced velocity PGS (erp) with the TGS Soft regularized contact solver, which pushed 18-row stack sag from < 0.15 mm to 0.388 mm (T1 failure) and spiked transient contact impulses enough that 1.2 g micro-splinters triggered chain-reaction false shatters (4 intact survivors in an asymmetric collapse vs the >= 15 gate). I reverted and pinned Rapier at ^0.14.0 while upgrading the rest of the repo to Node 24, Vite 8 Rolldown, and TypeScript 7, and built a hardware Metal optical bench (scripts/optical-bench.mjs) that reads back linear Float32 HDR buffers. In the same pass, I split the post-processor into dual swapchain pipelines: rgba16float with toneMapping: { mode: 'extended' } for specular glints above SDR white on Apple Silicon XDR displays, paired with automatic SDR fallback via matchMedia('(dynamic-range: high)').
Before putting Vitrail on a live URL, I did a full audit across the table geometry, procedural Web Audio synthesis, Sandbox UX, and responsive HUD scaling. The flat oak plane became a turned quarter-sawn dark walnut stave pedestal table with a beveled soffit edge so grazing Vespers caustics wrap cleanly over the rim instead of clipping into nothing. In the audio engine, Voronoi fractures now pan across the stereo field relative to camera-space impact coordinates, pairing modal glass chime partials with continuous glass-on-glass slide friction and a persistent mute toggle (<= 14 concurrent voices with zero leaked transient nodes post-decay). I also added a one-click Buckle & Shatter Tower trigger inside Sandbox mode so visitors can detonate 300+ raytraced Voronoi shards without pulling blocks first, then shipped the build to vitrail.andrewvoirol.com.
Pinning Rapier at 0.14.0 kept the 18-course tower under 0.15 mm sag, but sequential Gauss–Seidel velocity impulses still lacked torsional contact friction and second-order multi-course force-chain coupling. I replaced Rapier3D completely with Google DeepMind's @mujoco/[email protected] running inside a dedicated ES Module Web Worker (4 WASM pthreads via COOP/COEP SharedArrayBuffer, solver="Newton", sparse Cholesky Hessian at 600 Hz, implicit <connect> spring-tether extraction, and a pre-allocated 512-slot sleep="init" convex Voronoi shard pool). Calibrated 18-course sag dropped to 0.098 mm (0.199 mm at 24 courses) with 54/54 constraint-island sleep in 8.6 µs/step and 0.0 ms main-thread rAF stall. Because MuJoCo's Newton solver computes exact upward contact clamping forces (efc_force) across each prism's -X, 0, and +X bearing zones plus Euler–Bernoulli three-point flexural bending, I wired those 4-component wrenches directly into a dark-field Brewster stress-optic Polariscope shader pass () so players can inspect live isochromatic stress fringes shifting onto a single fulcrum column during extraction.
With MuJoCo 4.0 handling rigid-body dynamics, I ran an adversarial physics, optics, and acoustics audit (C1–C8 and T8–T9) to replace remaining empirical graphics shortcuts with closed-form models. On the optical bench, dark walnut absorbed more than half of projected caustic radiance, so the pedestal became a quarter-sawn ring-porous white oak bullnose table (43 cm radius) while Schlick's approximation gave way to exact unpolarized Maxwell–Fresnel reflectance , Cauchy dispersion (), and cross-pass 2048×2048 Snell caustics cast through all 512 convex 3D Voronoi shards. In the audio engine, fixed 2.3 kHz / 3.8 kHz chime frequencies were replaced by Cowper–Timoshenko thick-bar dispersion () with Reuss copper-foil/solder came compliance, Hertzian velocity-bandwidth cutoffs (), Leissa orthotropic oak plate modes, and shard chattering. The audit also caught an explicit Euler integration trap on 0.2 g Voronoi shards (): setting dof_damping caused sign inversions, while height-based micro-jitter gates froze mid-air bounce apexes — solved by routing shard-table restitution through a dedicated oak_table_shard contact pair (solref = [0.0060, 0.48]) and gating sleep strictly on verified upward qfrc_constraint.
Because MuJoCo's mj_copyState clones full solver state into a scratch buffer with zero live world mutation, AI opponents no longer have to rely on static heuristics. I built Glazier AI (Pass & Play -> AI) using 2-ply counterfactual physics rollouts inside the Web Worker: first optimizing extraction pull azimuth via Cross-Entropy Method (optimalPullAzimuthRad), then scoring 2nd-ply adversarial top-course placements (twoPlyMasterScore01 = 0.763 on free center prisms vs 0.277 on pinned bearing columns) to maximize center-of-mass eccentricity for the next turn. Alongside the AI, I added a 520-seed × 80-action stateful trajectory fuzzer verifying passivity with delta-debugging (ddmin) shrinking, plus C8 post-collapse sleep hardening (8/8 collapse seeds reaching 100% native tree sleep in 1.12–1.82 s with zero post-settle buzz). Finally, I shipped 5-character Online Rooms (?room=K7M4P) combining public WebRTC RTCDataChannel (< 4 KB binary 0x564d snapshots + round-robin 512-shard sync) with automatic <= 4.0 s ICE failover to a Cloudflare Durable Object SQLite WebSocket relay — verified across a 100-match adversarial network chaos simulator (8% drop + 65% burst loss, 120 ± 45 ms jitter, < 2.0 mm Guest convergence) and 58/58 Vitest invariants.