Motion SDK

WebXR

Put a scene in a VR or AR headset with the opt-in @motion-sdk/core/xr entry, stereo rendering from the headset's poses, and controllers and hands as scene nodes.

@motion-sdk/core/xr plays a scene in a WebXR headset. It is opt-in: a separate entry with its own wasm bundles, so pages that never import it download nothing extra and the regular player, authoring and WebGL2 bundles stay the size they were.

Quick Start

Write the scene with defineXRScene from @motion-sdk/scene/xr. It adds the controller and hand nodes and a standing preview camera. Units are meters and the floor is at y = 0.

import { mesh } from "@motion-sdk/scene";
import { defineXRScene } from "@motion-sdk/scene/xr";

export const scene = defineXRScene({
  lighting: { rig: "studio_soft" },
  materials: { glow: { color: "#5eead4", emissive: "#5eead4", emissive_intensity: 0.5 } },
  nodes: {
    orb: { transform: { position: [0, 1.3, -1.5] }, mesh: mesh({ sphere: { radius: 0.2 } }, { material: "glow" }) },
  },
});

Then load it and add a button. Browsers only start a session from a click, so VRButton calls player.enter() for you.

import { createXRPlayer, VRButton } from "@motion-sdk/core/xr";

const player = await createXRPlayer(canvas, scene);
document.body.append(VRButton(player)); // or ARButton(player)

Before the user enters, the canvas shows a normal preview from the scene camera. patch, event, key, load, setTransforms and machineStates work as they do on Player.

The full example is examples/ts/xr-gallery.ts. The playground serves it at /xr.html, and /xr.html?emulate runs it on an emulated headset on a desktop.

Controllers and Hands

The XR player places these nodes every frame with runtime overrides. It shows them while their input is tracked and hides them otherwise. They are plain nodes, so restyle them with materials like anything else.

NodeWhat it follows
xr_left_ray, xr_right_rayThe target ray. Its child xr_<hand>_ray_beam is a thin beam along -Z
xr_left_grip, xr_right_gripWhere the controller is held
xr_<hand>_hand_<joint>One sphere per hand joint (25 per hand), scaled to the joint radius, under the xr_<hand>_hand group

xrRig({ controllers, hands, rayLength, rayColor, gripColor, handColor }) builds the same nodes if you assemble the scene yourself, and xrNodeId gives their ids.

Trigger and pinch presses reach the scene's state machines as events: xr_select, xr_select_left and xr_select_right, plus the same three for xr_squeeze.

transitions: [{ from: "idle", to: "lit", trigger: { event: { name: "xr_select" } } }]

AR

ARButton starts immersive-ar. Pass { passthrough: true } to defineXRScene to give the scene a transparent background, so the room shows through.

How It Renders

Each XR frame renders every eye with the headset's own view and projection matrices, asymmetric frusta included, through Engine.set_view. That call is only in the XR bundles. The finished eye is then copied into the session's XRWebGLLayer viewport.

EngineBundleWhen
WebGPUwebgpu-xr: the lean player plus per-eye viewsDefault when WebGPU is available and the scene needs no authoring features
WebGL2webgl-xr: the full WebGL2 build plus per-eye viewsNo WebGPU, or the scene uses prefabs, models, 2D motion graphics and the like

The XR layer is always a WebGL2 XRWebGLLayer, because that is what headsets ship today. The engine draws into its own canvas, on WebGPU or WebGL2, and each eye is copied into the layer with one textured triangle.

Not Yet Supported

  • WebGPU XR binding (XRGPUBinding). In late 2026 it ships in Safari on Apple Vision Pro and in Meta Quest Browser, and Chrome still keeps it behind flags. wgpu cannot yet render into a texture the browser hands it, so for now WebGPU scenes take the copy path above. Every one of those browsers also supports XRWebGLLayer, so they all work today. Zero-copy rendering straight into the layer, for either backend, is the main follow-up.
  • Pointing the controller ray at interactive nodes (hover and click from a ray), hit testing, anchors, depth sensing and layers beyond the base projection layer.
  • Moving onto a new GPU device mid-session. On device loss the regular Player recovers onto WebGL2, but XRPlayer does not yet.

Testing Without a Headset

The playground's Playwright test (apps/playground/e2e/xr.spec.ts) loads /xr.html?emulate. That page installs an emulated headset (IWER, a dev dependency of the playground only) with two controllers and hand tracking. The test enters a session through the VR button, then reads each stereo frame with player.snapshot() (a PNG of the layer framebuffer after the next tick). It does not screenshot the XR canvas in the page: IWER moves that canvas into a fullscreen overlay and resizes it, which makes Playwright's element screenshot wait for layout stability until the test times out. It then checks:

  • Both eyes render, and they differ by parallax.
  • Moving the headset changes the view.
  • A controller held in front of the eyes appears in the frame.
  • The trigger fires xr_select.
  • Switching to hands shows the joints.
  • Leaving the session returns to the preview.

It runs on both the WebGPU and WebGL2 engines.