Walk a scene in VR
Let a visitor walk a scene with a headset such as a Meta Quest 3, in their own room, on its floors and stairs, pointing where to go and jumping to the places you chose.
The three.js renderer can show a scene in a VR headset through WebXR. It is off unless you ask
for it: pass xr to ThreeView (or xr: true to createThreeRenderer), and nothing about a
page that does not ask changes.
"use client";
import { useEffect, useRef, useState } from "react";
import { KloradProvider, SceneObject } from "@klorad/react";
import { vrSupported, type CameraView } from "@klorad/engine-three";
import { ThreeView, type ThreeViewHandle } from "@klorad/engine-three/react";
// Each stop is an ordinary camera view: in VR the visitor stands on the ground under its camera and faces its heading.
const stops: CameraView[] = [
{ target: { east: 0, north: 0, up: 1.6 }, heading: 0, tilt: 5, distance: 6 },
{ target: { east: 0, north: 0, up: 1.2 }, heading: 135, tilt: 15, distance: 2.5 },
];
export default function Exhibit() {
const view = useRef<ThreeViewHandle>(null);
const [canVR, setCanVR] = useState(false);
const [stop, setStop] = useState(0);
useEffect(() => void vrSupported().then(setCanVR), []);
const next = () => {
const i = (stop + 1) % stops.length;
setStop(i);
view.current?.moveVR(stops[i]!);
};
return (
<KloradProvider options={{ coordinateSystem: { origin: { lat: 40.632, lon: 22.951, height: 12 } } }}>
<SceneObject id="piece" name="Scanned piece" position={{ east: 0, north: 0 }} geometry={{ kind: "model", uri: "/models/piece.stl" }} />
{canVR ? <button onClick={() => void view.current?.enterVR(stops[stop])}>Enter VR</button> : null}
<div style={{ height: 480 }}>
<ThreeView ref={view} xr xrPlaces={stops} onXRPlace={setStop} onXRSelect={next} />
</div>
</KloradProvider>
);
}What the visitor needs
vrSupported() resolves to true in a browser that can start an immersive VR session (the
Meta Quest browser can) and false elsewhere, including a desktop without a headset. Show the
button only when it is true. A browser asks the visitor before a session starts, and
enterVR only works from a click or a tap.
WebXR runs only on a secure page: https, or localhost while you develop. To try a page from
your machine on a headset, open it over https or forward the port to the headset.
When the visitor leaves VR, the page's camera comes back to where it was.
Without a headset, the same scene runs on the screen: the stops are camera views you set with
setView, so a desktop preview of every stop needs nothing more.
Where the visitor stands
A VR stop is an ordinary CameraView, the same value view() returns and setView takes.
enterVR(view) starts an immersive-vr session and places the visitor on the floor under that
view's camera (a mezzanine or a stage when one is there at the height the view looks at, else
the scene's ground), facing its heading. The floor of their room is the scene's ground, so
they stand at their own height and the scene is life size around them. The tilt of the view does
not tilt the visitor: they look up and down with their head. Called without a view, it starts
from the view on the screen.
The session stands on the visitor's floor (local-floor), and they walk metre for metre in the
scene as far as the boundary they set up on their headset lets them. On a Meta Quest, a room
scale boundary (rather than a stationary one, in the headset's settings) is what lets them walk.
moveVR(view) moves the visitor to another stop while the session runs, by moving their room
under them; nothing in the scene moves. Outside a session it does nothing, so you can call it
next to setView in the same handler.
Pointing, jumping and turning
Each controller is drawn as a plain handle with a pointing ray, and an arc thrown along it shows
where a jump lands. A trigger takes the visitor there, up to 30 m away, when it is somewhere a
person may stand: a floor, a stair, a stage or a mezzanine with room above it. Where it is not,
on glass, a wall or under a low ceiling, the arc and its ring turn orange and the trigger does
nothing. Pass xrPlaces, a list of views, and each is a glowing ball over the ground:
pointed at, it takes the visitor to that place facing its heading, and onXRPlace says which.
Pushing a thumbstick sideways turns the visitor by 30 degrees about where they stand; pushing it
forward walks them along where they look, at an easy pace, stopped by what stops a person.
Floors, stairs and what stops a visitor
Where a visitor may stand is read from what is drawn (ADR-0066). A surface that faces up, no
steeper than 40 degrees, is stood on; any other stops them. A building's model from IFC says more
by each element's class: walls, windows, doors, plates and railings always stop a visitor, and
spaces and openings are passed through. An object's navigation says it outright: "walkable",
"blocks" or "ignore". The room follows the floor under the visitor's head, up a step of
35 cm and down a drop, so walking up a stair in their room climbs it in the scene.
On the screen, walk (a prop of ThreeView, an option of createThreeRenderer) does the same
for a double click: on something a person may stand on, it stands the camera there at eye height,
looking the way it looked; on a wall or glass it does nothing.
onXRSelect fires for a select that pointed at neither a place nor the ground. The renderer
does not decide what it means: the example walks the stops in order.
Speaking, and words in front of the visitor
A grip held is the visitor's button to speak: onXRTalk(true) when it is pressed and
onXRTalk(false) when it is let go, so a page can record a question while the grip is held.
xrCaption shows words on a panel in front of the visitor, a little below the eyes, that follows
where they look with a lag; null hides it.
<ThreeView xr onXRTalk={(holding) => (holding ? startRecording() : stopRecording())} xrCaption={answer} />The panel is drawn on a canvas in the scene, so it needs no font file from the network.
Changes
Before 21 October 2026 the visitor stood with their eyes where the view's camera was, so a view
taken from above put them in the air; EYE_HEIGHT is kept, deprecated. Testing on a headset
needs a real device: the examples check what a desktop browser can, and say so.
Something wrong or unclear? Tell us.