Skip to main content

Getting Started

An XR application is an ordinary PlayCanvas application that can also run in an immersive session. Until the user enters XR, it renders to the canvas on the page like any other. When they press a button to enter VR, the browser hands the display over to the headset. The engine then renders the scene once for each eye, from a camera that follows the user's head. This page sets up XR on each surface and builds a first VR scene: a few shapes on a floor, models of the user's controllers, and a button that enters VR.

Requirements​

Browsers offer WebXR, the web API behind immersive sessions, only when:

  • The page is in a secure context. Serve it over HTTPS. A page on http://localhost also counts as secure, which is enough for a browser on your own computer, but not for a headset that loads the page over your network. See Testing on a Device.
  • The user starts the session. A session can only start in response to a user action, such as a click on a button. A page can't enter XR as it loads, or from a timer.
  • An iframe allows it. A page in an iframe needs allow="xr-spatial-tracking" on its <iframe> element. Without it, the browser hides XR from the page, and the engine reports every session type as unavailable.

The graphics device must also be able to present to the headset. WebGL 2 always can. WebGPU can only in a browser that supports WebXR with WebGPU, which few do yet. See Choosing a Graphics Device.

Setting Up​

XR is run by the application's XR manager, an XrManager available as app.xr. It checks which session types the device supports, starts and ends sessions, and drives the camera and input while a session runs.

Pass XrManager in the application's options. An application created without it has no app.xr:

const canvas = document.getElementById('application');
const device = await createGraphicsDevice(canvas, {
deviceTypes: [DEVICETYPE_WEBGL2]
});

// Render at the full resolution of the display, and of the headset in XR
device.maxPixelRatio = window.devicePixelRatio;

const options = new AppOptions();
options.graphicsDevice = device;
options.xr = XrManager;
options.componentSystems = [
CameraComponentSystem,
LightComponentSystem,
RenderComponentSystem,
ScriptComponentSystem
];
options.resourceHandlers = [
ContainerHandler,
TextureHandler
];

const app = new AppBase(canvas);
app.init(options);
app.setCanvasFillMode(FILLMODE_FILL_WINDOW);
app.setCanvasResolution(RESOLUTION_AUTO);
app.start();

window.addEventListener('resize', () => app.resizeCanvas());

createGraphicsDevice makes the device compatible with XR whenever the browser supports WebXR. The ContainerHandler loads the glTF models of controllers and hands. See Using the Engine Standalone for the rest of the setup.

Choosing a Graphics Device​

An XR session presents the frames it renders through a binding between WebXR and the graphics API. Every browser with WebXR has the binding for WebGL. Only some have it for WebGPU, such as the Meta Quest Browser and Safari on Apple Vision Pro, while Chrome has it only behind a flag. See Platforms. On a WebGPU device in a browser without it, the engine reports VR and AR as unavailable, so the safe choice for an XR application is WebGL 2.

To use WebGPU where it works with XR, and WebGL 2 elsewhere, ask XrManager.isDeviceSupported before you create the device. It is a static method, so you can call it before there is an application:

const webgpu = await XrManager.isDeviceSupported(DEVICETYPE_WEBGPU, XRTYPE_VR);
const device = await createGraphicsDevice(canvas, {
deviceTypes: webgpu ? [DEVICETYPE_WEBGPU, DEVICETYPE_WEBGL2] : [DEVICETYPE_WEBGL2]
});

It only resolves to true while a headset is available, so a desktop browser with no headset gets WebGL 2. In stereo on WebGPU, the engine doesn't yet support grab passes or per-camera post-processing, and GPU depth sensing in AR needs WebGL.

Your First VR Scene​

The scene in the video above has a light, a floor and three shapes. Its camera is the child of an entity called the rig, which stands for the user's place in the world. Two of the engine's XR scripts go on the rig:

  • XrSession starts a VR session when the application fires a vr:start event, and ends it on xr:end or when the user presses Esc.
  • XrControllers draws a model of each of the user's controllers or hands.

A button on the page fires vr:start. It is shown only while VR is available and no session is running.

Add a button to the page:

<button id="enter-vr" hidden>Enter VR</button>

With the application set up as above:

import { XrControllers } from 'playcanvas/scripts/esm/xr/xr-controllers.mjs';
import { XrSession } from 'playcanvas/scripts/esm/xr/xr-session.mjs';

// A light, a floor and three shapes
const light = new Entity('light');
light.addComponent('light', { type: 'directional' });
light.setEulerAngles(50, 30, 0);
app.root.addChild(light);

const floor = new Entity('floor');
floor.addComponent('render', { type: 'plane' });
floor.setLocalScale(10, 1, 10);
app.root.addChild(floor);

const addShape = (type, color, x, z) => {
const material = new StandardMaterial();
material.diffuse = color;
material.update();

const shape = new Entity(type);
shape.addComponent('render', { type, material });
shape.setPosition(x, 0.5, z);
app.root.addChild(shape);
};
addShape('box', new Color(1, 0.55, 0.2), -1, -1);
addShape('sphere', new Color(0.3, 0.55, 1), 0, -2);
addShape('cylinder', new Color(0.3, 0.8, 0.45), 1, -1);

// The rig is the user's place in the world, and the camera is its child
const rig = new Entity('rig');
app.root.addChild(rig);

const camera = new Entity('camera');
camera.addComponent('camera', { clearColor: new Color(0.11, 0.12, 0.14) });
camera.setLocalPosition(0, 1.6, 2);
camera.setLocalEulerAngles(-20, 0, 0);
rig.addChild(camera);

rig.addComponent('script');
rig.script.create(XrSession);
rig.script.create(XrControllers);

// Show the button while VR is available and no session is running
const button = document.getElementById('enter-vr');
const update = (inSession) => {
button.hidden = inSession || !app.xr.isAvailable(XRTYPE_VR);
};
app.xr.on('available', () => update(app.xr.active));
app.xr.on('start', () => update(true));
app.xr.on('end', () => update(false));
update(false);

// XrSession starts a VR session when this event fires
button.addEventListener('click', () => app.fire('vr:start'));

The scripts are in the playcanvas package, under scripts/esm/xr/. Without a bundler, map playcanvas/scripts/ to that folder in your import map, as Using the Engine Standalone does for playcanvas.

How It Fits Together​

The camera rig of the scene above is:

rig XrSession and XrControllers scripts: the user's place in the world
└── camera camera component, driven by the headset during a session

A few rules follow from it, and the rest of this section builds on them:

  • The headset drives the camera, relative to the rig. Every frame of a session, the engine sets the camera entity's local position and rotation to the pose of the user's head, and derives its field of view and aspect ratio from the headset's displays. Anything you set on the camera's transform is overwritten. To move the user, by teleporting or walking, move the rig instead. See The Camera and Its Rig.
  • The rig's origin is on the floor of the room. The scene starts its session in the local-floor reference space, whose origin is on the floor near where the user stands, so the camera is at the height of the user's eyes above the rig. See Reference Spaces.
  • XrSession moves the rig when a session starts. It places the rig on the ground below the camera, facing the way the camera faces across the ground, so the user starts where the page's view was. The camera's tilt doesn't matter: the user's head decides where they look. When the session ends, it puts the rig and the camera back.
  • Input is reported in world space. The engine applies the rig's transform to the controllers and hands, so they move with the user. XrControllers downloads a model for each one from the WebXR Input Profiles repository on a CDN, and draws it at the controller's pose. See Input Sources.

Trying It​

You don't need a headset to try the scene. Install the Immersive Web Emulator browser extension in Chrome or Edge, open the page, and select Enter VR. The extension shows controls over the page that move the emulated headset and controllers.

To try it on a headset, open the page in the headset's browser over HTTPS. See Testing and Debugging for both, and for the Editor's launch page on a headset.

The engine's VR example is a gallery to walk around:

VR Basic

See Also​