Three.js XR UI Panels (World-Locked, Lazy-Follow, Head-Locked)
Spatial UI for XR in Three.js, with canvas-drawn buttons, toggles and sliders on 3D panels hit-tested in UV space, and three placement strategies, world-locked, lazy-follow and head-locked, driven by a mouse pointer or an XR controller ray.
Key topics: xr ui, spatial ui, world locked ui, vr menu, canvas texture ui, three.js interface
Game Over
What this code does
- Panels: widgets are drawn into a 2D canvas used as a texture, and a raycast hit's uv maps back to canvas pixels to find the widget under the ray.
- States: hover, press and slider drag redraw the canvas, with texture.needsUpdate uploading the change.
- World-Locked: the panel lives in the scene and stays put in the room, best for content tied to a place.
- Lazy-Follow: a tag-along menu re-centres in front of you only when it drifts more than 35° out of view, then eases in and billboards around y.
- Head-Locked: a child of the camera that is always visible; keep it small and peripheral, because head-locked UI is tiring.
- Controls: menu behaviour and auto look-around; in VR, aim the controller ray and pull the trigger.
Related Demos
Continue Learning
JavaScript Implementation
// World-space UI: draw widgets on a canvas, put it on a plane, hit-test the ray's UV
const hit = raycaster.intersectObject(panelMesh)[0]
if (hit) {
const x = hit.uv.x * canvas.width, y = (1 - hit.uv.y) * canvas.height
const widget = widgets.find(w => x >= w.x && x <= w.x + w.w && y >= w.y && y <= w.y + w.h)
if (widget && pressed) widget.action()
redraw(); texture.needsUpdate = true
}
// Placement: world-locked (scene child) · head-locked (camera.add(panel)) ·
// lazy-follow: if angle(viewDir, panelDir) > 35°, lerp the panel to a point in front of the head