WebXR panels made of ordinary HTML
A form, a guest book, a diagnostics read-out and a keyboard as panels in a 3D scene. Each one is plain HTML inside the
<canvas>, uploaded as a texture on every paint. On this page you use them with the mouse or a finger.
In a headset there is no mouse, so controller, hand and gaze rays are turned into pointer events on the same elements.
Show the code HTML panels in WebXR
- A drawable canvas and ordinary HTML for each panel. One of the four panels is shown.
- Give each panel a plane in the scene.
surfaces.add(el, mesh)puts the element's live texture on the mesh. On the page, the mouse and touch work natively. - In a session there is no mouse, so turn controller rays into pointer events.
surfaces.createRayPointer()gives each controller a pointer: aim it with a raycaster, thendown()andup()on the trigger. - Inside the library: what a ray pointer does. It finds the element under the ray and fires the events a real pointer would, plus focus by hand.
- Inside the library: DOM to texture.
- The whole library.
Drag empty space to look around. Everything on a panel takes input. Click a text field, then use the floating keyboard: its keys never take focus, so the caret stays where it was. Enter AR on an Android phone should put the panels in your room, with a tap as the trigger. That path has only been run in the emulator so far.
- Checks run two seconds after a session starts.
Live checks
| Result | Experiment | Detail |
|---|
Inside a session
These run two seconds after a session starts, and also show on the Diagnostics panel inside the headset.
| Result | Experiment | Detail |
|---|---|---|
| … | No session yet. Enter VR or AR, or use the emulator. | |
How it is put together
const surfaces = new HTMLSurfaces(renderer, camera, { scene });
surfaces.add(form, panelMesh); // texture + native hit testing on the page
// In a session the page has no pointer. Rays become pointer events on the same elements.
const pointer = surfaces.createRayPointer();
controller.addEventListener('selectstart', () => { pointer.move(raycaster); pointer.down(); });
controller.addEventListener('selectend', () => { pointer.move(raycaster); pointer.up(); });
Findings
- This page stays on WebGL. The other shader demos here use WebGPU when it works. An immersive session is presented through an
XRWebGLLayer, and the WebGPU route into WebXR needsXRGPUBinding, which is missing from the Chrome this was built on (157). So three'sWebGLRendererdraws both the page and the headset view. - The texture upload is the same in a headset. A panel is a mesh with an element texture. Nothing about the upload changes when the same renderer is presenting to a headset.
- The input half does not. Browser hit testing is tied to the page's 2D box, and an immersive session has no pointer over it. Rays have to be turned into events by hand, and synthetic events are untrusted: focus, caret placement, slider drags and
<select>all need a hand-written stand-in. elementFromPointturns a ray into an element. Lay the elements out flat withupdateElementGeometry(), turn the ray's UV hit into a point in that layout, and the browser still answers which element is there, including inside scrollers.- The keyboard is a set of buttons. Keys that cancel
mousedownnever take focus, sosetRangeText()plus aninputevent types into whatever field had the caret. - I don't know yet whether
paintkeeps firing inside a real headset. The Diagnostics panel counts paint events during the session and falls back to forced uploads if they stop. The emulator can't answer this. It needs a device.