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.
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
- Three live
drawableHTML 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>. - One WebGL 2 context through
ElementGL. On everypaintevent,stage.upload(el)runsgl.texElementSubImage2D()for each dirty child and regenerates mipmaps so text stays legible across the room and through portals. - 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. - Projective 3D hit testing scaled by
geometryUnit(). Each flat wall panel maps element CSS pixels throughM_ndcToCss * VP * M_elemToWorld, scales bygeometryUnit()for HiDPI screens, callsupdateElementGeometry()and togglesdataset.hitTick. - 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
| Result | Check | Detail |
|---|
Findings
- Credit and prior art. The portal-camera render-to-texture technique and test chamber aesthetic come from Valve's Portal (2007). Projective
updateElementGeometry()hit testing and split-view portal concepts build on/three-form/and/portal-split/on this site. - Screen-space FBO projection matches live HTML textures with zero distortion. Rendering the virtual camera with the main projection matrix into a 512×512 FBO and sampling at
ndc.xy * 0.5 + 0.5lets you read and watch live edits to#chamber-consolethrough the Blue Portal as you orbit, with no extra WebGL contexts. - A 180-degree Y rotation keeps text readable through the portal. Using
R_180 = diag(-1, 1, -1, 1)has determinant +1, so the portal view rotates into the exit wall's frame without mirroring letters backwards. - Fragment exit-plane clipping avoids oblique-frustum depth issues. Discarding fragments where
dot(worldPos - B.pos, B.normal) < -0.005cleanly hides the wall behind the exit portal on any wall, floor or ceiling, and drawing opaque vortices inside the FBO pass avoids WebGL 2 texture feedback loops. - Caching
geometryUnit()per canvas resize prevents layout thrashing. CallinggeometryUnit()once when the canvas size changes and reusing that factor for both#chamber-consoleand#chamber-signkeeps HiDPI hit boxes lined up without callinggetBoundingClientRect()every frame. - Browser verification status. Source checks (
npm run check:gfxand strict writing checks) pass locally; live execution in headless Chromium 157 and Chrome on Android is not verified yet (seefindings/portal-room.md).