3D portal test chamber with live HTML consoles and FBO sightlines

I wanted to find out if a single WebGL 2 canvas could render a 3D test chamber with live HTML wall controls and a Companion Cube, shoot Blue and Orange portals onto any wall, draw a secondary portal camera into an off-screen framebuffer so you see the live HTML console through the portal, and launch the cube out of Orange. The portal-camera render-to-texture design and test chamber aesthetic come from Valve's Portal (2007), while the projective updateElementGeometry() hit testing and split-view ideas build on /three-form/ and /portal-split/ on this site.

Test Chamber Console Live DOM
Blue: back / Orange: left READY
04
Chamber
AP-04
Cube teleports 0
FBO sightline on
Cube 04 T-0
READY / CLICK ME
starting...

Drag empty chamber space to orbit. Click buttons, change lighting or type a chamber code on the right-wall console, click the Companion Cube to launch it, or switch to Shoot Blue / Orange portal and click any wall, floor or ceiling.

Show the code ElementGL 3D chamber, dual FBO portal cameras, projective hit testing and cube launch
  1. Three live drawable HTML children inside one canvas. The wall console, the backlit chamber lightbox and the Companion Cube face are ordinary DOM nodes inside one <canvas content="drawable" layoutsubtree>.
  2. One WebGL 2 context through ElementGL. On every paint event, stage.upload(el) runs gl.texElementSubImage2D() for each dirty child and regenerates mipmaps so text stays legible across the room and through portals.
  3. Secondary portal camera and 512×512 off-screen FBO pass. For each portal, I build the virtual camera M_B * R_180 * inv(M_A) * M_cam, clip geometry behind the exit plane, render the chamber (with the live HTML textures) into an FBO, and map that FBO onto the portal ellipse in screen space.
  4. Projective 3D hit testing scaled by geometryUnit(). Each flat wall panel maps element CSS pixels through M_ndcToCss * VP * M_elemToWorld, scales by geometryUnit() for HiDPI screens, calls updateElementGeometry() and toggles dataset.hitTick.
  5. Companion Cube arc through Blue out of Orange. The cube flies along a ballistic arc into Blue Portal, mutates the live DOM counters on both the cube face and wall sign at transit (raising paint), and launches out of Orange Portal onto the chamber pad.

How it works

Everything in the chamber draws on one WebGL 2 context created with new ElementGL(canvas, ...) from /lib/element-gl.js. Inside <canvas content="drawable" layoutsubtree> sit three live HTML children: the right-wall control console (#chamber-console), the back-wall lightbox sign (#chamber-sign) and the Companion Cube face (#cube-face). Whenever you edit the chamber code, pick a lighting theme or launch the cube, Chromium fires a paint event and I re-upload the changed elements with gl.texElementSubImage2D() plus mipmaps.

Before drawing the main view, I render two 512×512 off-screen colour-and-depth framebuffers on the same stage.gl context: one for what you see looking into Blue (out of Orange) and one for looking into Orange (out of Blue). Each portal defines an orthonormal frame M on its wall. The virtual camera for Portal A looking out of Portal B is M_B * R_180 * inv(M_A) * M_cam, where R_180 = diag(-1, 1, -1, 1) turns the inward ray around without flipping left and right (its determinant is +1, so text on the console stays readable through the portal). A fragment clip plane discards the exit wall behind Portal B, and in the main pass the elliptical portal surface samples the FBO at screen-space coordinates (ndc.xy * 0.5 + 0.5). Because VP_virt * T(X) = VP_main * X, parallax lines up at every orbit angle.

To keep the right-wall console and back-wall sign clickable in 3D, I project each quad's element CSS coordinates through the main view-projection matrix into canvas CSS pixels, scale by geometryUnit() from /lib/surface.js for HiDPI alignment, pass the 4×4 projective DOMMatrix to canvas.updateElementGeometry() and toggle dataset.hitTick whenever the matrix moves. While you're in Shoot Blue or Shoot Orange mode, those hit boxes move off-canvas so a wall shot won't press a console button by mistake. If WebGL 2 texElementSubImage2D() isn't available, the page falls back to a 2D room view drawn with 5-argument drawElementImage() calls; if HTML-in-canvas is missing or the context is lost, all three elements move into plain HTML below the hidden canvas.

Live checks

ResultCheckDetail

Findings