---
title: "VR (WebXR)"
description: "Play a MavonEngine game in a VR headset through WebXR — camera rig, controllers, thumbstick locomotion, labels and panels in VR, and mirroring to the browser window."
canonical_url: "https://mavonengine.com/core-concepts/vr"
---

> Index of every page on this site: [llms.txt](https://mavonengine.com/llms.txt)

A `Game` can be played in a VR headset through [WebXR](https://immersiveweb.dev): a Quest's or Pico's browser, or desktop Chrome with a PC headset (SteamVR, Quest Link). The [multiplayer template](https://mavonengine.com/getting-started/installation#multiplayer) turns it on wherever the browser can do it.

## Enabling VR

```typescript
import Game from '@mavonengine/core/Game'
import XR from '@mavonengine/core/XR'

const game = Game.instance()

if (await XR.isSupported())
  game.enableXR() // adds an "Enter VR" button
```

`enableXR(options)` returns the `XR` object, also at `game.xr`. Calling it again returns the same one.

<table>
<thead>
  <tr>
    <th>
      Option
    </th>
    
    <th>
      Default
    </th>
    
    <th>
      
    </th>
  </tr>
</thead>

<tbody>
  <tr>
    <td>
      <code>
        referenceSpace
      </code>
    </td>
    
    <td>
      <code>
        'local-floor'
      </code>
    </td>
    
    <td>
      Where the headset's origin sits: on the floor under the player, or <code>
        'local'
      </code>
      
       at their head height when the session started
    </td>
  </tr>
  
  <tr>
    <td>
      <code>
        button
      </code>
    </td>
    
    <td>
      <code>
        true
      </code>
    </td>
    
    <td>
      Show three.js's <strong>
        Enter VR
      </strong>
      
       button. Set it to <code>
        false
      </code>
      
       and call <code>
        xr.enter()
      </code>
      
       from your own UI instead
    </td>
  </tr>
  
  <tr>
    <td>
      <code>
        sessionInit
      </code>
    </td>
    
    <td>
      
    </td>
    
    <td>
      Extra session features, e.g. <code>
        { optionalFeatures: ['hand-tracking'] }
      </code>
    </td>
  </tr>
  
  <tr>
    <td>
      <code>
        input
      </code>
    </td>
    
    <td>
      <code>
        {}
      </code>
    </td>
    
    <td>
      How the <a href="#controllers">
        controllers
      </a>
      
       show, or <code>
        false
      </code>
      
       to leave controller input out
    </td>
  </tr>
  
  <tr>
    <td>
      <code>
        mirror
      </code>
    </td>
    
    <td>
      <code>
        false
      </code>
    </td>
    
    <td>
      Keep drawing the game in the browser window while a headset plays. Without it the window shows its last frame, blurred. See <a href="#mirroring-to-the-browser-window">
        Mirroring
      </a>
    </td>
  </tr>
</tbody>
</table>

<table>
<thead>
  <tr>
    <th>
      <code>
        XR
      </code>
    </th>
    
    <th>
      
    </th>
  </tr>
</thead>

<tbody>
  <tr>
    <td>
      <code>
        XR.isSupported()
      </code>
    </td>
    
    <td>
      Whether this browser and device can run immersive VR
    </td>
  </tr>
  
  <tr>
    <td>
      <code>
        enter()
      </code>
      
       / <code>
        exit()
      </code>
    </td>
    
    <td>
      Start or end a session. Browsers only start one from a user gesture, like a click
    </td>
  </tr>
  
  <tr>
    <td>
      <code>
        presenting
      </code>
    </td>
    
    <td>
      Whether a session is rendering to the headset
    </td>
  </tr>
  
  <tr>
    <td>
      <code>
        session
      </code>
    </td>
    
    <td>
      The running <code>
        XRSession
      </code>
      
      , if any
    </td>
  </tr>
  
  <tr>
    <td>
      <code>
        input
      </code>
      
       / <code>
        pointer
      </code>
    </td>
    
    <td>
      The controllers (also <code>
        game.input.xr
      </code>
      
      ) and what they point at
    </td>
  </tr>
  
  <tr>
    <td>
      <code>
        enableLocomotion(options)
      </code>
    </td>
    
    <td>
      <a href="#locomotion">
        Thumbstick movement
      </a>
    </td>
  </tr>
  
  <tr>
    <td>
      <code>
        addPanel(panel)
      </code>
      
       / <code>
        removePanel(panel)
      </code>
    </td>
    
    <td>
      <a href="#panels">
        Panels
      </a>
      
       shown in VR
    </td>
  </tr>
</tbody>
</table>

The game triggers `xrSessionStart` (with the `XRSession`) and `xrSessionEnd`.

WebXR only runs on `https` or `localhost`. To play the dev server on a Quest over USB, forward the port so the headset opens it as `localhost`:

```bash
adb reverse tcp:8050 tcp:8050
# then open http://localhost:8050 in the Quest's browser
```

Without a headset, the [Immersive Web Emulator](https://chromewebstore.google.com/detail/immersive-web-emulator/cgffilbpcibhmcfbgggfhfolhkfbhmik) extension for Chrome emulates one.

### Rendering in VR

While a session runs:

- the headset sets the frame rate, so `renderer.maxFps` is ignored;
- the scene is rendered straight to the headset's two eyes, skipping the [render pipeline](https://mavonengine.com/core-concepts/game-loop#renderer) and its effects, which render a single view. The renderer's `toneMapping` and `outputColorSpace` stand in for the pipeline's output;
- resizes are skipped, since the session owns the framebuffer, and caught up when it ends;
- a reversed depth buffer (`reversedDepthBuffer: true` in the render parameters) works. The headset is told the depth is reversed, so its reprojection (correcting the picture for head movement since the frame was rendered) doesn't make distant geometry jitter as the head moves;
- off-screen renders into a render target, such as an environment map or a reflection, use the camera they were given;
- the session only gets a WebXR Layers projection layer when the browser granted the `layers` feature, otherwise the WebGL layer. Chrome on Windows with SteamVR has the API without granting it, and showed no frames before.

Frames come through the renderer's animation loop, which a session drives, and the [`beforeRender`](https://mavonengine.com/core-concepts/game-loop#events) event runs on every one of them.

## The camera rig

In VR the headset moves `camera.instance` relative to its parent, so the camera now sits in a rig: `camera.rig`, a `Group` in the scene. Move the player's view by moving the rig, or with `place()`, which works the same on desktop and in VR:

```typescript
// A third person camera behind the player, every rendered frame
game.on('beforeRender', () => {
  game.camera.place(behindPlayer, player.position)
})
```

<table>
<thead>
  <tr>
    <th>
      <code>
        Camera
      </code>
    </th>
    
    <th>
      
    </th>
  </tr>
</thead>

<tbody>
  <tr>
    <td>
      <code>
        rig
      </code>
    </td>
    
    <td>
      The camera's parent. On desktop it stays at the origin unless moved, so setting <code>
        camera.instance.position
      </code>
      
       directly keeps working there
    </td>
  </tr>
  
  <tr>
    <td>
      <code>
        place(position, target, { turn, pinHead })
      </code>
    </td>
    
    <td>
      Put the view at <code>
        position
      </code>
      
       looking at <code>
        target
      </code>
      
      . On desktop it moves and turns the rig. In VR it moves the rig so the player's head lands at <code>
        position
      </code>
      
       and only turns it around the vertical axis: the headset keeps control of where the player looks. <code>
        turn: false
      </code>
      
       only moves it. In VR the player's own head movement (leaning, standing up) moves the view from <code>
        position
      </code>
      
      ; <code>
        pinHead: true
      </code>
      
       keeps the view at <code>
        position
      </code>
      
       and lets the head only turn it, for a first person view on a body
    </td>
  </tr>
  
  <tr>
    <td>
      <code>
        turn(angle)
      </code>
    </td>
    
    <td>
      Turn the view around the vertical axis through the player's head, e.g. a snap turn. Positive turns left
    </td>
  </tr>
  
  <tr>
    <td>
      <code>
        recenter()
      </code>
    </td>
    
    <td>
      Take the headset's current offset as the one <code>
        place()
      </code>
      
       lines up, e.g. after the player sat down. Done on the first frame of each session
    </td>
  </tr>
  
  <tr>
    <td>
      <code>
        worldPosition
      </code>
    </td>
    
    <td>
      Where the camera is in the world, refreshed every rendered frame
    </td>
  </tr>
</tbody>
</table>

> `camera.instance.position` is relative to the rig, so it is no longer the camera's world position once the rig moves. Use `camera.worldPosition` for distance checks and shader uniforms: it is the same `Vector3` throughout, so it can be handed to a uniform once. The grass and water prefabs, labels, rain and smoke use it.

When a session ends, the rig is reset to the origin, so desktop controls that move the camera itself (orbit, fly) start from where they were, and the camera gets its own field of view back.

Turning the view continuously makes many players queasy. Pass `turn: false` and turn in steps with `camera.turn()` instead, as the template does.

## Controllers

`game.input.xr` reads both controllers by hand, with the same API as [gamepads](https://mavonengine.com/input#gamepads). See [VR controllers](https://mavonengine.com/input#vr-controllers).

## Locomotion

`enableLocomotion()` walks the player with one thumbstick, relative to where they look, and turns with the other, by moving the camera rig every rendered frame:

```typescript
game.xr!.enableLocomotion({ speed: 2, turn: 'snap' })
```

<table>
<thead>
  <tr>
    <th>
      Option
    </th>
    
    <th>
      Default
    </th>
    
    <th>
      
    </th>
  </tr>
</thead>

<tbody>
  <tr>
    <td>
      <code>
        speed
      </code>
    </td>
    
    <td>
      <code>
        2
      </code>
    </td>
    
    <td>
      Walking speed at full stick, in meters per second
    </td>
  </tr>
  
  <tr>
    <td>
      <code>
        moveHand
      </code>
      
       / <code>
        turnHand
      </code>
    </td>
    
    <td>
      <code>
        'left'
      </code>
      
       / <code>
        'right'
      </code>
    </td>
    
    <td>
      The stick that moves and the one that turns
    </td>
  </tr>
  
  <tr>
    <td>
      <code>
        turn
      </code>
    </td>
    
    <td>
      <code>
        'snap'
      </code>
    </td>
    
    <td>
      <code>
        'snap'
      </code>
      
       turns in steps on a flick, <code>
        'smooth'
      </code>
      
       continuously, <code>
        false
      </code>
      
       leaves turning to the player's body
    </td>
  </tr>
  
  <tr>
    <td>
      <code>
        snapAngle
      </code>
    </td>
    
    <td>
      30°
    </td>
    
    <td>
      How far a snap turn turns, in radians
    </td>
  </tr>
  
  <tr>
    <td>
      <code>
        turnSpeed
      </code>
    </td>
    
    <td>
      90°
    </td>
    
    <td>
      How fast a smooth turn turns at full stick, in radians per second
    </td>
  </tr>
  
  <tr>
    <td>
      <code>
        fly
      </code>
    </td>
    
    <td>
      <code>
        false
      </code>
    </td>
    
    <td>
      Move where the head looks, up and down too, instead of staying level
    </td>
  </tr>
</tbody>
</table>

It moves freely and goes through walls, which suits free roaming. A game with physics or its own character moves that instead and places the view with `camera.place()`. `getMoveDirection(stick)` gives a stick's direction in the world, relative to where the player looks:

```typescript
import XRLocomotion from '@mavonengine/core/XRLocomotion'

// Not enabled: only used for the stick's direction
const locomotion = new XRLocomotion(game.camera, game.input.xr!)

const direction = locomotion.getMoveDirection(game.input.xr!.left.stick)
player.move(direction) // e.g. send it to the server
```

The template's `VRControls.ts` works this way: the left stick (or WASD) walks the character through the same `CL_MOVE` command as the keyboard, so the server needs nothing for VR, and the right stick (or Q / E) snap turns the chase view around it.

## Labels and panels in VR

A headset shows none of the page's HTML, so what a game shows in VR has to be in the scene.

### Labels

`ObjectLabel` (and `UILabel`) keep working: while a headset plays, the label is drawn in the scene as a `TextLabel` instead of its HTML element. By default it reads the element's text; override `xrText()` to choose what shows, lines split on `\n`, or return `undefined` to show nothing in VR. Override `xrLabelOptions()` to style it.

```typescript
class PlayerLabel extends ObjectLabel {
  protected xrText() {
    return this.chatBubble ? `${this.chatBubble}\n${this.name}` : this.name
  }
}
```

`TextLabel` can be used on its own: text on a sprite that always faces the viewer, unlit and out of tone mapping and fog, drawn over the scene unless `depthTest` is set.

```typescript
import TextLabel from '@mavonengine/core/ui/TextLabel'

const label = new TextLabel('Checkpoint', { angularSize: 2 })
label.position.set(0, 3, 0)
game.scene.add(label)
// With angularSize, keep its size in view every rendered frame
game.on('beforeRender', () => label.update(game.camera.instance))
```

<table>
<thead>
  <tr>
    <th>
      Option
    </th>
    
    <th>
      Default
    </th>
    
    <th>
      
    </th>
  </tr>
</thead>

<tbody>
  <tr>
    <td>
      <code>
        font
      </code>
    </td>
    
    <td>
      <code>
        '600 40px system-ui, sans-serif'
      </code>
    </td>
    
    <td>
      CSS font, at the size the text is drawn on the canvas
    </td>
  </tr>
  
  <tr>
    <td>
      <code>
        color
      </code>
      
       / <code>
        background
      </code>
    </td>
    
    <td>
      white / translucent black
    </td>
    
    <td>
      <code>
        background: ''
      </code>
      
       for none
    </td>
  </tr>
  
  <tr>
    <td>
      <code>
        padding
      </code>
      
       / <code>
        radius
      </code>
    </td>
    
    <td>
      a third of the font's height / half the padding
    </td>
    
    <td>
      In canvas pixels
    </td>
  </tr>
  
  <tr>
    <td>
      <code>
        height
      </code>
    </td>
    
    <td>
      <code>
        0.25
      </code>
    </td>
    
    <td>
      Its height in the world, in meters
    </td>
  </tr>
  
  <tr>
    <td>
      <code>
        angularSize
      </code>
    </td>
    
    <td>
      
    </td>
    
    <td>
      Keep it the same size in view at any distance, as an angle in degrees (2 reads like 14 px text on a monitor)
    </td>
  </tr>
  
  <tr>
    <td>
      <code>
        depthTest
      </code>
    </td>
    
    <td>
      <code>
        false
      </code>
    </td>
    
    <td>
      Hidden by the scene in front of it
    </td>
  </tr>
</tbody>
</table>

`setText()` and `setIcon()` redraw it only when they change.

### Panels

`XRPanel` is a flat panel with a canvas on it, for a menu, a scoreboard or a message. Draw on its `ctx`, call `redraw()`, and add it with `xr.addPanel()`. It shows while in VR and open, sits in the camera rig so it moves with the player, and the controllers point at it with a line and a dot, the trigger as the button:

```typescript
import XRPanel from '@mavonengine/core/ui/XRPanel'

const menu = game.xr!.addPanel(new XRPanel({ width: 1, height: 0.6 }))
menu.ctx.fillStyle = '#111'
menu.ctx.fillRect(0, 0, menu.canvas.width, menu.canvas.height)
menu.ctx.fillStyle = '#fff'
menu.ctx.font = '64px system-ui'
menu.ctx.fillText('Resume', 80, 300)
menu.redraw()

menu.on('click', ({ hand, x, y }) => {
  // x and y in canvas pixels from the top left
  if (y > 220 && y < 340)
    menu.close()
})

menu.open()
```

<table>
<thead>
  <tr>
    <th>
      Option
    </th>
    
    <th>
      Default
    </th>
    
    <th>
      
    </th>
  </tr>
</thead>

<tbody>
  <tr>
    <td>
      <code>
        width
      </code>
      
       / <code>
        height
      </code>
    </td>
    
    <td>
      <code>
        1
      </code>
      
       / <code>
        0.6
      </code>
    </td>
    
    <td>
      Its size in meters
    </td>
  </tr>
  
  <tr>
    <td>
      <code>
        pixelsPerMeter
      </code>
    </td>
    
    <td>
      <code>
        1000
      </code>
    </td>
    
    <td>
      How sharp the canvas is
    </td>
  </tr>
  
  <tr>
    <td>
      <code>
        follow
      </code>
    </td>
    
    <td>
      <code>
        'head'
      </code>
    </td>
    
    <td>
      <code>
        'head'
      </code>
      
       opens it in front of the player and glides it back once they look well away; <code>
        'none'
      </code>
      
       leaves <code>
        mesh
      </code>
      
       where the game puts it, in the rig's space
    </td>
  </tr>
  
  <tr>
    <td>
      <code>
        distance
      </code>
      
       / <code>
        drop
      </code>
    </td>
    
    <td>
      <code>
        1.5
      </code>
      
       / <code>
        0.15
      </code>
    </td>
    
    <td>
      How far in front of the head it floats, and how far below eye level
    </td>
  </tr>
  
  <tr>
    <td>
      <code>
        followAngle
      </code>
    </td>
    
    <td>
      <code>
        35
      </code>
    </td>
    
    <td>
      How far, in degrees, the view turns away before it follows
    </td>
  </tr>
</tbody>
</table>

Panels get `pointermove`, `pointerleave`, `pointerdown`, `pointerup` and `click`. `xr.pointer.pointingAt(hand)` says which panel a controller points at, so the game can leave the trigger to the menu instead of shooting.

## Mirroring to the browser window

Without a mirror, the browser window shows its last frame, blurred, with *Playing in VR* over it while a headset plays. The game's own UI stays on top. With `enableXR({ mirror: true })`, or `xr.mirror = true` at any time, the scene is drawn once more after every headset frame instead, onto the page from the player's head, with the window's shape. `mirror: { fov: 70 }` sets its field of view. It's for someone watching or a stream, and reuses the headset frame's shadow maps.

It costs the headset frames: three.js rebuilds every object's render pipeline whenever the camera changes between the headset's eyes and the mirror's. A Valve Index in Chrome on Windows ran at 45 Hz with it and 90 Hz without. So the template leaves it off:

```typescript
XR.isSupported().then((supported) => {
  if (supported)
    game.enableXR()
})
```

## Sitemap

See the full [sitemap](https://mavonengine.com/sitemap.md) for all pages.
