---
title: "xr"
description: "The tracked devices of an XR session and the hand controllers: where each part of the person is, what the controllers' sticks, triggers and buttons read…"
section: "API Reference"
slug: "api-xr"
canonical: "https://origozero.ai/docs/api-xr"
updated: "2026-11-24T03:50:57.404488910+00:00"
tags: ["api", "reference"]
---

# xr

The tracked devices of an XR session and the hand controllers: where each part of the person is, what the controllers' sticks, triggers and buttons read, what they point at, vibration, and the comfort settings of the person wearing the headset.

```lua
require("@builtin::modules.api.engine.xr") -- xr (also available as global 'xr')
```

The `xr` namespace: 122 functions.

## globals/xr/aim {#globals-xr-aim}

```lua
xr.aim() -> CameraAim
```

Where the game camera faces in a headset: `"head"` by default, or
`"controller"`.

**Returns** `CameraAim` — The aim source.

```lua
print(xr.aim())
```

## globals/xr/body {#globals-xr-body}

```lua
xr.body() -> Body?
```

The local player's body and how the engine found it, in order: the
avatar the spawn system bound to the player (`avatar`), the entity the
game's camera follows (`cameraFollow`), the entity the player's character
controller moves (`controller`), whichever the game uses: the game's camera
follows it or a character controller the local player drives moves it. The
eyes sit in this body while the perspective is `"embodied"`. Nil when the
game has no body to find (a game that plays from its own fixed camera, a
strategy or top-down camera over many units), where the game's camera is
the view. Found while a session draws frames in play mode.

**Returns** `Body?` — `{ entity, reason }`, or nil at the floor.

```lua
local body = xr.body(); if body then print(body.entity, body.reason) end
```

## globals/xr/controller {#globals-xr-controller}

```lua
xr.controller(hand: Hand) -> Controller?
```

What a hand controller's inputs read as of the frame last drawn. A hand
tracked without a controller is active, and its `trigger` is its pinch (1
while held). The sticks run -1 to 1 with `y` positive pushed away from the person;
`trigger` and `squeeze` run 0 to 1. `primary` is A on the right controller
and X on the left, `secondary` is B and Y, and `menu` is the left
controller's menu button. Where the controller is comes from
`xr.pose(hand)`.

**Parameters**

- `hand` `Hand` — `"left_hand"` or `"right_hand"`.

**Returns** `Controller?` — The controller's inputs, or nil while no session holds input focus.

```lua
local c = xr.controller("right_hand"); if c and c.trigger > 0.8 then fire() end
```

## globals/xr/dominantHand {#globals-xr-dominanthand}

```lua
xr.dominantHand() -> Hand
```

The role of the hand whose controller points: `"right_hand"` by default.

**Returns** `Hand` — The dominant hand.

```lua
print(xr.dominantHand())
```

## globals/xr/enter {#globals-xr-enter}

```lua
xr.enter()
```

Start a session now, on the active runtime, starting the runtime
when it is not running: what the VR button does. On an engine whose
policy is `"off"` it does nothing. The person's to do: a call from world
or agent code raises.

```lua
xr.enter()
```

## globals/xr/entry {#globals-xr-entry}

```lua
xr.entry() -> Entry
```

How this engine comes to draw to a headset: its `policy` (`"off"`,
`"auto"` or `"always"`), whether an OpenXR runtime is `installed`, its
`runtime` name and `kind`, whether it is `running`, and whether the person
left VR (`declined`).

**Returns** `Entry` — The entry.

```lua
local e = xr.entry(); if e.installed and e.running ~= "running" then print("the VR button starts", e.runtime) end
```

## globals/xr/exit {#globals-xr-exit}

```lua
xr.exit()
```

Leave VR: the session ends on the next frame. On an engine with a
flat view to return to (`xr.returnsToFlat()`) the window or the page is
the view again, and the engine does not enter VR by itself again until
`xr.enter()` or the runtime starts anew; on a standalone headset app the
runtime ends the app, as the headset's own Quit does. The person's to do:
a call from world or agent code raises.

```lua
xr.exit()
```

## globals/xr/fade {#globals-xr-fade}

```lua
xr.fade(opacity: number, seconds: number?, color: { number }?)
```

Run the veil over the person's whole view toward `opacity` (0 open, 1
closed) over `seconds`, in `color` (`{ r, g, b }`, 0..1). The world panels
stay readable over it, so a world can be taken down behind a closed view
while a panel says what comes next.

**Parameters**

- `opacity` `number` — 0 (open) to 1 (closed).
- `seconds` `number` _(optional)_ — How long the fade runs; 0 when omitted.
- `color` `{ number }` _(optional)_ — `{ r, g, b }`; the colour the veil has when omitted.

```lua
xr.fade(1, 0.35)
```

## globals/xr/fadeState {#globals-xr-fadestate}

```lua
xr.fadeState() -> Fade
```

The veil now: its `opacity`, the `target` it runs to, whether it has
arrived (`done`), its `color` and how many fades were asked for.

**Returns** `Fade` — The fade.

```lua
if xr.fadeState().done then print("arrived") end
```

## globals/xr/focused {#globals-xr-focused}

```lua
xr.focused() -> boolean
```

Whether an XR session holds input focus, which is when the hand
controllers drive the game's input. False with no session, while the
system menu holds input, and while the headset is not worn.

**Returns** `boolean` — True while controller input flows.

```lua
if xr.focused() then showHandHints() end
```

## globals/xr/gestures {#globals-xr-gestures}

```lua
xr.gestures() -> Gestures
```

What the gestures of the bare hands read. The left palm facing the
person with a pinch holds the menu button (`xr.controller("left_hand").menu`),
a right-hand pinch-flick to the left or the right is one snap turn, and the
left hand pointing with a pinch held aims a teleport that lands when the pinch
opens. A pinch with the laser on an object grips it in the editor. Hand
tracking drives the gestures; a hand holding a controller makes none.

**Returns** `Gestures` — The gestures.

```lua
local g = xr.gestures(); if g.teleport.aiming then print(g.teleport.seconds) end
```

## globals/xr/hand {#globals-xr-hand}

```lua
xr.hand(hand: Hand) -> HandPose?
```

The pose of a driven hand: its palm in world space and the direction each
finger bone points along in the palm's frame, with its pinch and where its
aim comes from. A controller's hand is modelled around its grip.

**Parameters**

- `hand` `Hand` — `"left_hand"` or `"right_hand"`.

**Returns** `HandPose?` — The hand's pose, or nil while nothing drives the hand.

```lua
local pose = xr.hand("left_hand"); if pose and pose.pinching then grab() end
```

## globals/xr/handSource {#globals-xr-handsource}

```lua
xr.handSource(hand: Hand) -> HandSource
```

What drives a hand now: `"controller"` while a controller is held in it,
`"hand_tracking"` while the runtime tracks the bare hand, `"none"` otherwise
and while no session holds input focus.

**Parameters**

- `hand` `Hand` — `"left_hand"` or `"right_hand"`.

**Returns** `HandSource` — The source.

```lua
if xr.handSource("right_hand") == "hand_tracking" then showPinchHint() end
```

## globals/xr/handTracking {#globals-xr-handtracking}

```lua
xr.handTracking() -> HandTracking
```

What the runtime offers for tracking bare hands. Where it offers none,
`reason` says why and the controllers are the only hands.

**Returns** `HandTracking` — The hand tracking status.

```lua
if not xr.handTracking().available then print(xr.handTracking().reason) end
```

## globals/xr/joints {#globals-xr-joints}

```lua
xr.joints(hand: Hand) -> HandJoints?
```

The 26 joints of a driven hand in world space, by name. They are the
runtime's wherever it locates the hand (`modelled` false) and the model's
around the controller's grip otherwise (`modelled` true).

**Parameters**

- `hand` `Hand` — `"left_hand"` or `"right_hand"`.

**Returns** `HandJoints?` — The joints, or nil while nothing drives the hand.

```lua
local tip = xr.joints("right_hand").joints.index_tip.position
```

## globals/xr/panels/autoHud {#globals-xr-panels-autohud}

```lua
xr.panels.autoHud(enabled: boolean?) -> boolean
```

Whether the engine shows the world's HUD on the panel `"hud"`. Pass a
boolean to turn that off or on.

**Parameters**

- `enabled` `boolean` _(optional)_ — Choose whether the HUD panel is made.

**Returns** `boolean` — Whether it is on.

```lua
xr.panels.autoHud(false)
```

## globals/xr/panels/create {#globals-xr-panels-create}

```lua
xr.panels.create(description: PanelDescription)
```

Make a panel. `screens` lists the screens it shows, or `space` names a
space whose shown screens it shows; `width` and `height` are metres;
`curve` is the radius in metres of the cylinder it is bent around (0 is
flat); `pose` is where it stands, in the `anchor` space (`"world"`;
`"tracking"` for a pose fixed to the room that follows the rig when the
game moves or turns the player; or `"view"` for a pose in the frame at the
head turned by the head's yaw and pitch and not its roll, so the panel
stands that far in front of where the person looks, easing towards that
place with a time constant of 0.12 seconds and standing there at once on a
recenter, when the session begins and when a script states its `pose` or
`anchor`); `handle` is `{ left, top, right, bottom }`
in the panel's uv (0,0 top left): a press there reaches the panel's UI like
any press, and holds the panel once the laser has turned more than 6 degrees
from where the press began with the trigger held; the panel then follows the
laser until the trigger is released, turning to face the person, and the
stick pulls and pushes it. A press on the handle that is released before
the laser turned that far is a click, counted in the state's `taps`. `resize` makes a flat panel resizable: the
trigger pulled on a corner's zone drags that corner with the laser while the
opposite corner stays, within the stated minimum and maximum size, and the
panel keeps its points per metre, so a larger panel lays out more content.
`passThrough` lets the laser through a panel except over something its UI
can act on. `keepsFocus` leaves the keys where they are when the panel is
pressed: a text field holding the caret on another panel keeps it while
this panel's UI is used, the way an on-screen keyboard is; a press on any
other panel, or away from every panel, takes the caret from that field.
`order` stacks panels that overlap: a panel of a higher order is drawn
over one of a lower order and takes the laser wherever it covers it,
whatever their distances, and of panels of one order the nearer takes the
laser. A `modal` panel stands alone: while it is visible, every panel of a
lower order is neither drawn nor pointed at. Raises when a field is
invalid or the id is taken.

**Parameters**

- `description` `PanelDescription` — The panel's `id` and fields.

```lua
xr.panels.create({ id = "stats", screens = { "stats_hud" }, width = 1.2, height = 0.7, pose = xr.panels.placement({ distance = 1.2 }), anchor = "tracking" })
```

## globals/xr/panels/drag {#globals-xr-panels-drag}

```lua
xr.panels.drag() -> PanelDrag?
```

The UI drag in flight on the panels: a widget with a `dragPayload` that
the trigger pulled. It reads the `panel` whose UI holds the drag, the
`payload` the source declared, the `label` its ghost reads, whether the laser
points at a panel now (`overPanel`) and the pointing `hand`. The reading
holds for as long as the trigger is held, wherever the laser points, and is
nil after the release, so a script places what the payload names where the
laser points at the world. A drag in the window reads through `ui.dragState()`.

**Returns** `PanelDrag?` — The drag, or nil when no panel's UI holds one.

```lua
local d = xr.panels.drag(); if d and not d.overPanel then print("carrying", d.payload) end
```

## globals/xr/panels/grab {#globals-xr-panels-grab}

```lua
xr.panels.grab(id: string, options: PanelGrabOptions?)
```

Hold a panel with the pointing hand, which must be holding its trigger:
the panel follows the laser `distance` metres along it (default 0.8), its
centre `offset` from the held point in the panel's own axes, turning to face
the person, until the trigger is released; the stick pulls and pushes it. A
press the hand began on another panel ends there without a click. This is
how a panel made from a press on a widget (an entry in a list) is dragged out
and placed where the trigger is released. Raises when no panel has the id,
the trigger is not held, or a panel is already held.

**Parameters**

- `id` `string` — The panel's id.
- `options` `PanelGrabOptions` _(optional)_ — `{ distance?, offset? }`.

```lua
xr.panels.grab("console", { distance = 0.8, offset = { x = 0, y = -0.2, z = 0 } })
```

## globals/xr/panels/hover {#globals-xr-panels-hover}

```lua
xr.panels.hover() -> PanelHover?
```

The panel the laser points at: its `id`, the `uv` on it (0,0 is the top
left), the `distance` along the laser, whether it is on the panel's grab
handle (`onHandle`) or a resize corner (`onCorner`), and the `point` in the world.

**Returns** `PanelHover?` — The hover, or nil when the laser points at no panel.

```lua
local h = xr.panels.hover(); if h then print(h.id, h.uv.u) end
```

## globals/xr/panels/list {#globals-xr-panels-list}

```lua
xr.panels.list() -> { PanelDescription }
```

Every panel's description, in the order the panels were made.

**Returns** `{ PanelDescription }` — The descriptions, each with the `texture` size of its picture in pixels.

```lua
for _, panel in xr.panels.list() do print(panel.id, panel.width, panel.height) end
```

## globals/xr/panels/placement {#globals-xr-panels-placement}

```lua
xr.panels.placement(options: PanelPlacementOptions?) -> Pose?
```

A pose for a `"tracking"` anchored panel in front of the person:
`distance` metres ahead (default 1.5), `drop` metres below eye height
(default 0.15), turned `yaw` degrees to the left (default 0), facing the
person. `from` names the head it is measured from: `"recenter"` (the
default), the head as it stood when the view was last recentered, or
`"head"`, the head as it stands now. With `atPointer = true` the pose is
`distance` metres along the laser instead, facing the person.

**Parameters**

- `options` `PanelPlacementOptions` _(optional)_ — `{ distance?, drop?, yaw?, from?, atPointer? }`.

**Returns** `Pose?` — The pose, or nil until the first frame has been drawn, and with `atPointer` while nothing points.

```lua
local pose = xr.panels.placement({ distance = 1.2, yaw = 25 })
```

## globals/xr/panels/remove {#globals-xr-panels-remove}

```lua
xr.panels.remove(id: string) -> boolean
```

Remove a panel.

**Parameters**

- `id` `string` — The panel's id.

**Returns** `boolean` — Whether a panel was removed.

```lua
xr.panels.remove("stats")
```

## globals/xr/panels/state {#globals-xr-panels-state}

```lua
xr.panels.state(id: string) -> PanelState?
```

What the laser and the hand are doing with a panel: whether it is
`world` (the pose the panel is drawn at in the world, nil before the first
frame has been routed and, for a panel anchored to the view, until a frame
that located the head has placed it), `hovered` (with the `uv` and `distance` of the laser on it), whether the
laser is `onHandle`, whether it is `grabbed`, how many grabs have ended
(`grabs`), how many presses on the handle ended as a click (`taps`), the turn
in degrees that makes a press on the handle a grab (`grabAngle`), how many
degrees the laser has turned since the press held on the panel began
(`pressTurn`, nil while none is held), whether the laser is on a resize corner (`onCorner`) or a resize
is running (`resizing`, with `resizes` counting those that ended), the panel
the laser pointed at when the last grab ended
(`droppedOn`), `dropTarget`, true while another panel is held and the
laser points at this one, and `covered`, true while a visible modal panel
of a higher order leaves this one undrawn.

**Parameters**

- `id` `string` — The panel's id.

**Returns** `PanelState?` — The state, or nil when no panel has the id.

```lua
local s = xr.panels.state("dock"); if s and s.hovered then print(s.uv.u, s.uv.v) end
```

## globals/xr/panels/update {#globals-xr-panels-update}

```lua
xr.panels.update(id: string, fields: PanelFields)
```

Change fields of a panel. A field that is invalid raises and leaves
the panel as it was.

**Parameters**

- `id` `string` — The panel's id.
- `fields` `PanelFields` — The fields to change; those of `xr.panels.create`.

```lua
xr.panels.update("stats", { visible = false })
```

## globals/xr/perspective {#globals-xr-perspective}

```lua
xr.perspective() -> Perspective
```

What the person sees from in a headset: `"embodied"` seats the eyes in
the local player's body (at its head height, over its root, the real head
free on top, the body's head hidden from the eyes alone and its arms
following the hands), and `"game"` shows the game's own camera with the
head free. A world whose camera is first person is embodied; a world with
no body to find shows the game's camera; any other world is embodied
unless the person chose otherwise. The right thumbstick's click switches.

**Returns** `Perspective` — The perspective in force.

```lua
if xr.perspective() == "embodied" then print("in the body") end
```

## globals/xr/perspectiveStatus {#globals-xr-perspectivestatus}

```lua
xr.perspectiveStatus() -> PerspectiveStatus
```

Everything about the perspective: the one in force, the kept choice,
whether the person can choose, why it is what it is, the body found, where
the eyes stand, and how much of the body is hidden from the eyes or
reaching for the hands.

**Returns** `PerspectiveStatus` — The status.

```lua
print(xr.perspectiveStatus().why)
```

## globals/xr/pointerRay {#globals-xr-pointerray}

```lua
xr.pointerRay() -> PointerRay?
```

The ray the person points along, in world space: the aim pose of the
dominant hand, or of the other hand while the dominant one cannot be
located.
The engine's pointer hit (`getPointerHitTable()`, what a mouse hover resolves on desktop) is cast
along this ray while the session holds input focus, and the game's mouse
(`input.snapshot().mouse_position`) is the point it rests on, seen from the
game's camera; `screenToRay` at that position returns this ray.

**Returns** `PointerRay?` — `{ role, origin, direction }`, or nil while no session holds input focus.

```lua
local ray = xr.pointerRay(); if ray then print(ray.role, ray.origin.x) end
```

## globals/xr/pose {#globals-xr-pose}

```lua
xr.pose(role: Role, kind: PoseKind?) -> Pose?
```

The pose of the device holding `role`, in world space, as of the frame
last drawn: `position` in metres and `rotation` a quaternion. The pose
passes through the same rig as the eyes, so it moves and turns with the
game's camera. For a hand it is where the hand holds the controller, and
`kind` `"aim"` is where the controller points, along its -Z axis.

**Parameters**

- `role` `Role` — `"head"`, `"left_hand"` or `"right_hand"`.
- `kind` `PoseKind` _(optional)_ — `"pose"` (the default) or `"aim"`.

**Returns** `Pose?` — The pose, or nil while the role's device is untracked or has no such pose.

```lua
local head = xr.pose("head"); if head then print(head.position.y) end
local aim = xr.pose("right_hand", "aim")
```

## globals/xr/pulse {#globals-xr-pulse}

```lua
xr.pulse(hand: Hand, amplitude: number, seconds: number, frequency: number?) -> boolean
```

Vibrate a hand controller. The pulse plays on the next frame. A
request that is not a valid pulse raises; one that cannot play because
no session holds input focus (or too many pulses are waiting) returns
false and plays nothing.

**Parameters**

- `hand` `Hand` — `"left_hand"` or `"right_hand"`.
- `amplitude` `number` — Strength, 0 to 1.
- `seconds` `number` — Length in seconds, above 0 and up to 10.
- `frequency` `number` _(optional)_ — Hertz; 0 or nil lets the runtime choose.

**Returns** `boolean` — Whether the pulse was accepted.

```lua
xr.pulse("right_hand", 0.6, 0.1) -- a short tap on the right hand
```

## globals/xr/recenter {#globals-xr-recenter}

```lua
xr.recenter()
```

Centre the headset's world on where the person stands and faces now,
on the next frame drawn; `xr.recenters()` rises by one.

```lua
xr.recenter()
```

## globals/xr/returnsToFlat {#globals-xr-returnstoflat}

```lua
xr.returnsToFlat() -> boolean
```

Whether this engine has a flat view to return to when its session
ends: a desktop window or a browser page. A standalone headset app (the
Android engine in a headset) has none, so its menus offer Quit where a
desktop offers Exit VR.

**Returns** `boolean` — True with a window or a page behind the headset.

```lua
if xr.returnsToFlat() then print("Exit VR goes back to the window") end
```

## globals/xr/roles {#globals-xr-roles}

```lua
xr.roles() -> { Role }
```

The roles whose tracked device the runtime locates now, in the order
`"head"`, `"left_hand"`, `"right_hand"`. The head is listed while a
session draws frames; a hand is listed while the session holds input
focus and its controller is located.

**Returns** `{ Role }` — Array of role names; empty with no session.

```lua
for _, role in xr.roles() do print(role) end
```

## globals/xr/setAim {#globals-xr-setaim}

```lua
xr.setAim(aim: CameraAim) -> CameraAim
```

Choose where the game camera faces in a headset. `"head"` faces where
the head does; `"controller"` faces along the dominant hand's aim
direction with the head's up, so a game that aims, picks up or prompts
along its camera's forward does it where the controller points. A setting
of this client (`xr.aim` in the client settings): it holds across scene
loads and applies on the next frame.

**Parameters**

- `aim` `CameraAim` — `"head"` or `"controller"`.

**Returns** `CameraAim` — The canonical spelling of the aim now in force.

```lua
xr.setAim("controller")
```

## globals/xr/setDominantHand {#globals-xr-setdominanthand}

```lua
xr.setDominantHand(hand: Hand) -> Hand
```

Choose the hand whose controller points. A setting of this client: it
holds across scene loads.

**Parameters**

- `hand` `Hand` — `"left_hand"` or `"right_hand"`.

**Returns** `Hand` — The canonical spelling of the hand now in force.

```lua
xr.setDominantHand("left_hand")
```

## globals/xr/setPerspective {#globals-xr-setperspective}

```lua
xr.setPerspective(perspective: Perspective) -> Perspective
```

Choose what the person sees from: the person's client setting
`xr.perspective`, the same in every world. A first-person world stays
embodied and a world with no body stays on the game's camera; the choice
is kept and applies where it can. The choice is the person's, made in the
client settings or by the right thumbstick's click: a call from world code
raises, and a world reads the choice with `xr.perspective()`.

**Parameters**

- `perspective` `Perspective` — `"embodied"` or `"game"`.

**Returns** `Perspective` — The perspective in force after the call.

```lua
xr.setPerspective("game")
```

## globals/xr/setPointerReach {#globals-xr-setpointerreach}

```lua
xr.setPointerReach(distance: number?)
```

Say how far along the pointer ray the laser is drawn while a script
carries something to a point the engine's own cast does not know of (an
asset held under the laser): the beam ends `distance` metres out. The laser
over a panel still ends on the panel. Call it each frame it applies; nil
hands the beam back to the engine's cast.

**Parameters**

- `distance` `number` _(optional)_ — Metres along the ray, above 0, or nil.

```lua
xr.setPointerReach(where.distance)
```

## globals/xr/setSnapTurnDegrees {#globals-xr-setsnapturndegrees}

```lua
xr.setSnapTurnDegrees(degrees: number) -> number
```

Choose the angle one snap-turn step turns the view by, held to
10..180 degrees. The client settings apply the person's snap angle through
it.

**Parameters**

- `degrees` `number` — Degrees per step.

**Returns** `number` — The angle now in force.

```lua
xr.setSnapTurnDegrees(30)
```

## globals/xr/setTurnMode {#globals-xr-setturnmode}

```lua
xr.setTurnMode(mode: TurnMode) -> TurnMode
```

Choose how the right stick turns the view in a headset. A comfort
setting of this client: it holds across scene loads, and a settings
screen offering it is this call.

**Parameters**

- `mode` `TurnMode` — `"snap"` or `"smooth"`.

**Returns** `TurnMode` — The canonical spelling of the mode now in force.

```lua
xr.setTurnMode("smooth")
```

## globals/xr/setVignette {#globals-xr-setvignette}

```lua
xr.setVignette(strength: number) -> number
```

Choose how dark the comfort vignette grows while the game moves the
person. A comfort setting of this client.

**Parameters**

- `strength` `number` — 0 (none) to 1.

**Returns** `number` — The strength now in force.

```lua
xr.setVignette(0.5)
```

## globals/xr/snapTurnDegrees {#globals-xr-snapturndegrees}

```lua
xr.snapTurnDegrees() -> number
```

The angle one snap-turn step turns the view by, in degrees: the
person's snap angle (client setting `xr.snapAngle`), 45 by default.

**Returns** `number` — Degrees per step.

```lua
print(xr.snapTurnDegrees())
```

## globals/xr/tracked {#globals-xr-tracked}

```lua
xr.tracked(role: Role) -> boolean
```

Whether the runtime locates the device holding `role` now.

**Parameters**

- `role` `Role` — `"head"`, `"left_hand"` or `"right_hand"`.

**Returns** `boolean` — True while the role's device is located.

```lua
if xr.tracked("left_hand") then showLeftHandHint() end
```

## globals/xr/turnMode {#globals-xr-turnmode}

```lua
xr.turnMode() -> TurnMode
```

How the right stick turns the view in a headset. `"snap"` (the
default) turns by one step of `xr.snapTurnDegrees()` per flick of the
stick; `"smooth"` turns continuously in proportion to the stick's
deflection. Either way the game turns its own camera through its look
action and the headset follows the camera's yaw; the stick never tilts
the view, because the head owns pitch.

**Returns** `TurnMode` — The turn mode.

```lua
print(xr.turnMode())
```

## globals/xr/vignette {#globals-xr-vignette}

```lua
xr.vignette() -> number
```

How dark the comfort vignette grows at the edge of the view while the
game moves or turns the person, 0 (none) to 1. How much of it shows
follows how fast the person is moved or turned; a snap turn or a cut
darkens nothing.

**Returns** `number` — The strength.

```lua
print(xr.vignette())
```

## modules/xr/aim {#modules-xr-aim}

```lua
aim(): CameraAim
```

Where the game camera faces in a headset: `"head"` by default, or
`"controller"`.

```lua
print(xr.aim())
```

## modules/xr/body {#modules-xr-body}

```lua
body(): Body?
```

The local player's body and how the engine found it, in order: the
avatar the spawn system bound to the player (`avatar`), the entity the
game's camera follows (`cameraFollow`), the entity the player's character
controller moves (`controller`), whichever the game uses: the game's camera
follows it or a character controller the local player drives moves it. The
eyes sit in this body while the perspective is `"embodied"`. Nil when the
game has no body to find (a game that plays from its own fixed camera, a
strategy or top-down camera over many units), where the game's camera is
the view. Found while a session draws frames in play mode.

```lua
local body = xr.body(); if body then print(body.entity, body.reason) end
```

## modules/xr/controller {#modules-xr-controller}

```lua
controller(hand: Hand): Controller?
```

What a hand controller's inputs read as of the frame last drawn. A hand
tracked without a controller is active, and its `trigger` is its pinch (1
while held). The sticks run -1 to 1 with `y` positive pushed away from the person;
`trigger` and `squeeze` run 0 to 1. `primary` is A on the right controller
and X on the left, `secondary` is B and Y, and `menu` is the left
controller's menu button. Where the controller is comes from
`xr.pose(hand)`.

**Parameters**

- `hand` `Hand` — `"left_hand"` or `"right_hand"`.

```lua
local c = xr.controller("right_hand"); if c and c.trigger > 0.8 then fire() end
```

## modules/xr/dominantHand {#modules-xr-dominanthand}

```lua
dominantHand(): Hand
```

The role of the hand whose controller points: `"right_hand"` by default.

```lua
print(xr.dominantHand())
```

## modules/xr/enter {#modules-xr-enter}

```lua
enter()
```

Start a session now, on the active runtime, starting the runtime
when it is not running: what the VR button does. On an engine whose
policy is `"off"` it does nothing. The person's to do: a call from world
or agent code raises.

```lua
xr.enter()
```

## modules/xr/entry {#modules-xr-entry}

```lua
entry(): Entry
```

How this engine comes to draw to a headset: its `policy` (`"off"`,
`"auto"` or `"always"`), whether an OpenXR runtime is `installed`, its
`runtime` name and `kind`, whether it is `running`, and whether the person
left VR (`declined`).

```lua
local e = xr.entry(); if e.installed and e.running ~= "running" then print("the VR button starts", e.runtime) end
```

## modules/xr/exit {#modules-xr-exit}

```lua
exit()
```

Leave VR: the session ends on the next frame. On an engine with a
flat view to return to (`xr.returnsToFlat()`) the window or the page is
the view again, and the engine does not enter VR by itself again until
`xr.enter()` or the runtime starts anew; on a standalone headset app the
runtime ends the app, as the headset's own Quit does. The person's to do:
a call from world or agent code raises.

```lua
xr.exit()
```

## modules/xr/fade {#modules-xr-fade}

```lua
fade(opacity: number, seconds: number?, color: { number }?)
```

Run the veil over the person's whole view toward `opacity` (0 open, 1
closed) over `seconds`, in `color` (`{ r, g, b }`, 0..1). The world panels
stay readable over it, so a world can be taken down behind a closed view
while a panel says what comes next.

**Parameters**

- `opacity` `number` — 0 (open) to 1 (closed).
- `seconds` `number?` _(optional)_ — How long the fade runs; 0 when omitted.
- `color` `{ number }?` _(optional)_ — `{ r, g, b }`; the colour the veil has when omitted.

```lua
xr.fade(1, 0.35)
```

## modules/xr/fadeState {#modules-xr-fadestate}

```lua
fadeState(): Fade
```

The veil now: its `opacity`, the `target` it runs to, whether it has
arrived (`done`), its `color` and how many fades were asked for.

```lua
if xr.fadeState().done then print("arrived") end
```

## modules/xr/focused {#modules-xr-focused}

```lua
focused(): boolean
```

Whether an XR session holds input focus, which is when the hand
controllers drive the game's input. False with no session, while the
system menu holds input, and while the headset is not worn.

```lua
if xr.focused() then showHandHints() end
```

## modules/xr/gestures {#modules-xr-gestures}

```lua
gestures(): Gestures
```

What the gestures of the bare hands read. The left palm facing the
person with a pinch holds the menu button (`xr.controller("left_hand").menu`),
a right-hand pinch-flick to the left or the right is one snap turn, and the
left hand pointing with a pinch held aims a teleport that lands when the pinch
opens. A pinch with the laser on an object grips it in the editor. Hand
tracking drives the gestures; a hand holding a controller makes none.

```lua
local g = xr.gestures(); if g.teleport.aiming then print(g.teleport.seconds) end
```

## modules/xr/hand {#modules-xr-hand}

```lua
hand(hand: Hand): HandPose?
```

The pose of a driven hand: its palm in world space and the direction each
finger bone points along in the palm's frame, with its pinch and where its
aim comes from. A controller's hand is modelled around its grip.

**Parameters**

- `hand` `Hand` — `"left_hand"` or `"right_hand"`.

```lua
local pose = xr.hand("left_hand"); if pose and pose.pinching then grab() end
```

## modules/xr/handSource {#modules-xr-handsource}

```lua
handSource(hand: Hand): HandSource
```

What drives a hand now: `"controller"` while a controller is held in it,
`"hand_tracking"` while the runtime tracks the bare hand, `"none"` otherwise
and while no session holds input focus.

**Parameters**

- `hand` `Hand` — `"left_hand"` or `"right_hand"`.

```lua
if xr.handSource("right_hand") == "hand_tracking" then showPinchHint() end
```

## modules/xr/handTracking {#modules-xr-handtracking}

```lua
handTracking(): HandTracking
```

What the runtime offers for tracking bare hands. Where it offers none,
`reason` says why and the controllers are the only hands.

```lua
if not xr.handTracking().available then print(xr.handTracking().reason) end
```

## modules/xr/joints {#modules-xr-joints}

```lua
joints(hand: Hand): HandJoints?
```

The 26 joints of a driven hand in world space, by name. They are the
runtime's wherever it locates the hand (`modelled` false) and the model's
around the controller's grip otherwise (`modelled` true).

**Parameters**

- `hand` `Hand` — `"left_hand"` or `"right_hand"`.

```lua
local tip = xr.joints("right_hand").joints.index_tip.position
```

## modules/xr/panels.autoHud {#autohud}

```lua
panels.autoHud(enabled: boolean?): boolean
```

Whether the engine shows the world's HUD on the panel `"hud"`. Pass a
boolean to turn that off or on.

**Parameters**

- `enabled` `boolean?` _(optional)_ — Choose whether the HUD panel is made.

```lua
xr.panels.autoHud(false)
```

## modules/xr/panels.create {#create}

```lua
panels.create(description: PanelDescription)
```

Make a panel. `screens` lists the screens it shows, or `space` names a
space whose shown screens it shows; `width` and `height` are metres;
`curve` is the radius in metres of the cylinder it is bent around (0 is
flat); `pose` is where it stands, in the `anchor` space (`"world"`;
`"tracking"` for a pose fixed to the room that follows the rig when the
game moves or turns the player; or `"view"` for a pose in the frame at the
head turned by the head's yaw and pitch and not its roll, so the panel
stands that far in front of where the person looks, easing towards that
place with a time constant of 0.12 seconds and standing there at once on a
recenter, when the session begins and when a script states its `pose` or
`anchor`); `handle` is `{ left, top, right, bottom }`
in the panel's uv (0,0 top left): a press there reaches the panel's UI like
any press, and holds the panel once the laser has turned more than 6 degrees
from where the press began with the trigger held; the panel then follows the
laser until the trigger is released, turning to face the person, and the
stick pulls and pushes it. A press on the handle that is released before
the laser turned that far is a click, counted in the state's `taps`. `resize` makes a flat panel resizable: the
trigger pulled on a corner's zone drags that corner with the laser while the
opposite corner stays, within the stated minimum and maximum size, and the
panel keeps its points per metre, so a larger panel lays out more content.
`passThrough` lets the laser through a panel except over something its UI
can act on. `keepsFocus` leaves the keys where they are when the panel is
pressed: a text field holding the caret on another panel keeps it while
this panel's UI is used, the way an on-screen keyboard is; a press on any
other panel, or away from every panel, takes the caret from that field.
`order` stacks panels that overlap: a panel of a higher order is drawn
over one of a lower order and takes the laser wherever it covers it,
whatever their distances, and of panels of one order the nearer takes the
laser. A `modal` panel stands alone: while it is visible, every panel of a
lower order is neither drawn nor pointed at. Raises when a field is
invalid or the id is taken.

**Parameters**

- `description` `PanelDescription` — The panel's `id` and fields.

```lua
xr.panels.create({ id = "stats", screens = { "stats_hud" }, width = 1.2, height = 0.7, pose = xr.panels.placement({ distance = 1.2 }), anchor = "tracking" })
```

## modules/xr/panels.drag {#drag}

```lua
panels.drag(): PanelDrag?
```

The UI drag in flight on the panels: a widget with a `dragPayload` that
the trigger pulled. It reads the `panel` whose UI holds the drag, the
`payload` the source declared, the `label` its ghost reads, whether the laser
points at a panel now (`overPanel`) and the pointing `hand`. The reading
holds for as long as the trigger is held, wherever the laser points, and is
nil after the release, so a script places what the payload names where the
laser points at the world. A drag in the window reads through `ui.dragState()`.

```lua
local d = xr.panels.drag(); if d and not d.overPanel then print("carrying", d.payload) end
```

## modules/xr/panels.grab {#grab}

```lua
panels.grab(id: string, options: PanelGrabOptions?)
```

Hold a panel with the pointing hand, which must be holding its trigger:
the panel follows the laser `distance` metres along it (default 0.8), its
centre `offset` from the held point in the panel's own axes, turning to face
the person, until the trigger is released; the stick pulls and pushes it. A
press the hand began on another panel ends there without a click. This is
how a panel made from a press on a widget (an entry in a list) is dragged out
and placed where the trigger is released. Raises when no panel has the id,
the trigger is not held, or a panel is already held.

**Parameters**

- `id` `string` — The panel's id.
- `options` `PanelGrabOptions?` _(optional)_ — `{ distance?, offset? }`.

```lua
xr.panels.grab("console", { distance = 0.8, offset = { x = 0, y = -0.2, z = 0 } })
```

## modules/xr/panels.hover {#hover}

```lua
panels.hover(): PanelHover?
```

The panel the laser points at: its `id`, the `uv` on it (0,0 is the top
left), the `distance` along the laser, whether it is on the panel's grab
handle (`onHandle`) or a resize corner (`onCorner`), and the `point` in the world.

```lua
local h = xr.panels.hover(); if h then print(h.id, h.uv.u) end
```

## modules/xr/panels.list {#list}

```lua
panels.list(): { PanelDescription }
```

Every panel's description, in the order the panels were made.

```lua
for _, panel in xr.panels.list() do print(panel.id, panel.width, panel.height) end
```

## modules/xr/panels.placement {#placement}

```lua
panels.placement(options: PanelPlacementOptions?): Pose?
```

A pose for a `"tracking"` anchored panel in front of the person:
`distance` metres ahead (default 1.5), `drop` metres below eye height
(default 0.15), turned `yaw` degrees to the left (default 0), facing the
person. `from` names the head it is measured from: `"recenter"` (the
default), the head as it stood when the view was last recentered, or
`"head"`, the head as it stands now. With `atPointer = true` the pose is
`distance` metres along the laser instead, facing the person.

**Parameters**

- `options` `PanelPlacementOptions?` _(optional)_ — `{ distance?, drop?, yaw?, from?, atPointer? }`.

```lua
local pose = xr.panels.placement({ distance = 1.2, yaw = 25 })
```

## modules/xr/panels.remove {#remove}

```lua
panels.remove(id: string): boolean
```

Remove a panel.

**Parameters**

- `id` `string` — The panel's id.

```lua
xr.panels.remove("stats")
```

## modules/xr/panels.state {#state}

```lua
panels.state(id: string): PanelState?
```

What the laser and the hand are doing with a panel: whether it is
`world` (the pose the panel is drawn at in the world, nil before the first
frame has been routed and, for a panel anchored to the view, until a frame
that located the head has placed it), `hovered` (with the `uv` and `distance` of the laser on it), whether the
laser is `onHandle`, whether it is `grabbed`, how many grabs have ended
(`grabs`), how many presses on the handle ended as a click (`taps`), the turn
in degrees that makes a press on the handle a grab (`grabAngle`), how many
degrees the laser has turned since the press held on the panel began
(`pressTurn`, nil while none is held), whether the laser is on a resize corner (`onCorner`) or a resize
is running (`resizing`, with `resizes` counting those that ended), the panel
the laser pointed at when the last grab ended
(`droppedOn`), `dropTarget`, true while another panel is held and the
laser points at this one, and `covered`, true while a visible modal panel
of a higher order leaves this one undrawn.

**Parameters**

- `id` `string` — The panel's id.

```lua
local s = xr.panels.state("dock"); if s and s.hovered then print(s.uv.u, s.uv.v) end
```

## modules/xr/panels.update {#update}

```lua
panels.update(id: string, fields: PanelFields)
```

Change fields of a panel. A field that is invalid raises and leaves
the panel as it was.

**Parameters**

- `id` `string` — The panel's id.
- `fields` `PanelFields` — The fields to change; those of `xr.panels.create`.

```lua
xr.panels.update("stats", { visible = false })
```

## modules/xr/perspective {#modules-xr-perspective}

```lua
perspective(): Perspective
```

What the person sees from in a headset: `"embodied"` seats the eyes in
the local player's body (at its head height, over its root, the real head
free on top, the body's head hidden from the eyes alone and its arms
following the hands), and `"game"` shows the game's own camera with the
head free. A world whose camera is first person is embodied; a world with
no body to find shows the game's camera; any other world is embodied
unless the person chose otherwise. The right thumbstick's click switches.

```lua
if xr.perspective() == "embodied" then print("in the body") end
```

## modules/xr/perspectiveStatus {#modules-xr-perspectivestatus}

```lua
perspectiveStatus(): PerspectiveStatus
```

Everything about the perspective: the one in force, the kept choice,
whether the person can choose, why it is what it is, the body found, where
the eyes stand, and how much of the body is hidden from the eyes or
reaching for the hands.

```lua
print(xr.perspectiveStatus().why)
```

## modules/xr/pointerRay {#modules-xr-pointerray}

```lua
pointerRay(): PointerRay?
```

The ray the person points along, in world space: the aim pose of the
dominant hand, or of the other hand while the dominant one cannot be
located.
The engine's pointer hit (`getPointerHitTable()`, what a mouse hover resolves on desktop) is cast
along this ray while the session holds input focus, and the game's mouse
(`input.snapshot().mouse_position`) is the point it rests on, seen from the
game's camera; `screenToRay` at that position returns this ray.

```lua
local ray = xr.pointerRay(); if ray then print(ray.role, ray.origin.x) end
```

## modules/xr/pose {#modules-xr-pose}

```lua
pose(role: Role, kind: PoseKind?): Pose?
```

The pose of the device holding `role`, in world space, as of the frame
last drawn: `position` in metres and `rotation` a quaternion. The pose
passes through the same rig as the eyes, so it moves and turns with the
game's camera. For a hand it is where the hand holds the controller, and
`kind` `"aim"` is where the controller points, along its -Z axis.

**Parameters**

- `role` `Role` — `"head"`, `"left_hand"` or `"right_hand"`.
- `kind` `PoseKind?` _(optional)_ — `"pose"` (the default) or `"aim"`.

```lua
local head = xr.pose("head"); if head then print(head.position.y) end
local aim = xr.pose("right_hand", "aim")
```

## modules/xr/pulse {#modules-xr-pulse}

```lua
pulse(hand: Hand, amplitude: number, seconds: number, frequency: number?): boolean
```

Vibrate a hand controller. The pulse plays on the next frame. A
request that is not a valid pulse raises; one that cannot play because
no session holds input focus (or too many pulses are waiting) returns
false and plays nothing.

**Parameters**

- `hand` `Hand` — `"left_hand"` or `"right_hand"`.
- `amplitude` `number` — Strength, 0 to 1.
- `seconds` `number` — Length in seconds, above 0 and up to 10.
- `frequency` `number?` _(optional)_ — Hertz; 0 or nil lets the runtime choose.

```lua
xr.pulse("right_hand", 0.6, 0.1) -- a short tap on the right hand
```

## modules/xr/recenter {#modules-xr-recenter}

```lua
recenter()
```

Centre the headset's world on where the person stands and faces now,
on the next frame drawn; `xr.recenters()` rises by one.

```lua
xr.recenter()
```

## modules/xr/returnsToFlat {#modules-xr-returnstoflat}

```lua
returnsToFlat(): boolean
```

Whether this engine has a flat view to return to when its session
ends: a desktop window or a browser page. A standalone headset app (the
Android engine in a headset) has none, so its menus offer Quit where a
desktop offers Exit VR.

```lua
if xr.returnsToFlat() then print("Exit VR goes back to the window") end
```

## modules/xr/roles {#modules-xr-roles}

```lua
roles(): { Role }
```

The roles whose tracked device the runtime locates now, in the order
`"head"`, `"left_hand"`, `"right_hand"`. The head is listed while a
session draws frames; a hand is listed while the session holds input
focus and its controller is located.

```lua
for _, role in xr.roles() do print(role) end
```

## modules/xr/setAim {#modules-xr-setaim}

```lua
setAim(aim: CameraAim): CameraAim
```

Choose where the game camera faces in a headset. `"head"` faces where
the head does; `"controller"` faces along the dominant hand's aim
direction with the head's up, so a game that aims, picks up or prompts
along its camera's forward does it where the controller points. A setting
of this client (`xr.aim` in the client settings): it holds across scene
loads and applies on the next frame.

**Parameters**

- `aim` `CameraAim` — `"head"` or `"controller"`.

```lua
xr.setAim("controller")
```

## modules/xr/setDominantHand {#modules-xr-setdominanthand}

```lua
setDominantHand(hand: Hand): Hand
```

Choose the hand whose controller points. A setting of this client: it
holds across scene loads.

**Parameters**

- `hand` `Hand` — `"left_hand"` or `"right_hand"`.

```lua
xr.setDominantHand("left_hand")
```

## modules/xr/setPerspective {#modules-xr-setperspective}

```lua
setPerspective(perspective: Perspective): Perspective
```

Choose what the person sees from: the person's client setting
`xr.perspective`, the same in every world. A first-person world stays
embodied and a world with no body stays on the game's camera; the choice
is kept and applies where it can. The choice is the person's, made in the
client settings or by the right thumbstick's click: a call from world code
raises, and a world reads the choice with `xr.perspective()`.

**Parameters**

- `perspective` `Perspective` — `"embodied"` or `"game"`.

```lua
xr.setPerspective("game")
```

## modules/xr/setPointerReach {#modules-xr-setpointerreach}

```lua
setPointerReach(distance: number?)
```

Say how far along the pointer ray the laser is drawn while a script
carries something to a point the engine's own cast does not know of (an
asset held under the laser): the beam ends `distance` metres out. The laser
over a panel still ends on the panel. Call it each frame it applies; nil
hands the beam back to the engine's cast.

**Parameters**

- `distance` `number?` _(optional)_ — Metres along the ray, above 0, or nil.

```lua
xr.setPointerReach(where.distance)
```

## modules/xr/setSnapTurnDegrees {#modules-xr-setsnapturndegrees}

```lua
setSnapTurnDegrees(degrees: number): number
```

Choose the angle one snap-turn step turns the view by, held to
10..180 degrees. The client settings apply the person's snap angle through
it.

**Parameters**

- `degrees` `number` — Degrees per step.

```lua
xr.setSnapTurnDegrees(30)
```

## modules/xr/setTurnMode {#modules-xr-setturnmode}

```lua
setTurnMode(mode: TurnMode): TurnMode
```

Choose how the right stick turns the view in a headset. A comfort
setting of this client: it holds across scene loads, and a settings
screen offering it is this call.

**Parameters**

- `mode` `TurnMode` — `"snap"` or `"smooth"`.

```lua
xr.setTurnMode("smooth")
```

## modules/xr/setVignette {#modules-xr-setvignette}

```lua
setVignette(strength: number): number
```

Choose how dark the comfort vignette grows while the game moves the
person. A comfort setting of this client.

**Parameters**

- `strength` `number` — 0 (none) to 1.

```lua
xr.setVignette(0.5)
```

## modules/xr/snapTurnDegrees {#modules-xr-snapturndegrees}

```lua
snapTurnDegrees(): number
```

The angle one snap-turn step turns the view by, in degrees: the
person's snap angle (client setting `xr.snapAngle`), 45 by default.

```lua
print(xr.snapTurnDegrees())
```

## modules/xr/tracked {#modules-xr-tracked}

```lua
tracked(role: Role): boolean
```

Whether the runtime locates the device holding `role` now.

**Parameters**

- `role` `Role` — `"head"`, `"left_hand"` or `"right_hand"`.

```lua
if xr.tracked("left_hand") then showLeftHandHint() end
```

## modules/xr/turnMode {#modules-xr-turnmode}

```lua
turnMode(): TurnMode
```

How the right stick turns the view in a headset. `"snap"` (the
default) turns by one step of `xr.snapTurnDegrees()` per flick of the
stick; `"smooth"` turns continuously in proportion to the stick's
deflection. Either way the game turns its own camera through its look
action and the headset follows the camera's yaw; the stick never tilts
the view, because the head owns pitch.

```lua
print(xr.turnMode())
```

## modules/xr/vignette {#modules-xr-vignette}

```lua
vignette(): number
```

How dark the comfort vignette grows at the edge of the view while the
game moves or turns the person, 0 (none) to 1. How much of it shows
follows how fast the person is moved or turned; a snap turn or a cut
darkens nothing.

```lua
print(xr.vignette())
```

## typed/builtin//modules/api/engine/xr/xr/aim {#typed-builtin-modules-api-engine-xr-xr-aim}

```lua
xr.aim() -> CameraAim
```

Where the game camera faces in a headset: `"head"` by default, or
`"controller"`.

**Returns** `CameraAim` — The aim source.

```lua
print(xr.aim())
```

## typed/builtin//modules/api/engine/xr/xr/body {#typed-builtin-modules-api-engine-xr-xr-body}

```lua
xr.body() -> Body?
```

The local player's body and how the engine found it, in order: the
avatar the spawn system bound to the player (`avatar`), the entity the
game's camera follows (`cameraFollow`), the entity the player's character
controller moves (`controller`), whichever the game uses: the game's camera
follows it or a character controller the local player drives moves it. The
eyes sit in this body while the perspective is `"embodied"`. Nil when the
game has no body to find (a game that plays from its own fixed camera, a
strategy or top-down camera over many units), where the game's camera is
the view. Found while a session draws frames in play mode.

**Returns** `Body?` — `{ entity, reason }`, or nil at the floor.

```lua
local body = xr.body(); if body then print(body.entity, body.reason) end
```

## typed/builtin//modules/api/engine/xr/xr/controller {#typed-builtin-modules-api-engine-xr-xr-controller}

```lua
xr.controller(hand: Hand) -> Controller?
```

What a hand controller's inputs read as of the frame last drawn. A hand
tracked without a controller is active, and its `trigger` is its pinch (1
while held). The sticks run -1 to 1 with `y` positive pushed away from the person;
`trigger` and `squeeze` run 0 to 1. `primary` is A on the right controller
and X on the left, `secondary` is B and Y, and `menu` is the left
controller's menu button. Where the controller is comes from
`xr.pose(hand)`.

**Parameters**

- `hand` `Hand` — `"left_hand"` or `"right_hand"`.

**Returns** `Controller?` — The controller's inputs, or nil while no session holds input focus.

```lua
local c = xr.controller("right_hand"); if c and c.trigger > 0.8 then fire() end
```

## typed/builtin//modules/api/engine/xr/xr/dominantHand {#typed-builtin-modules-api-engine-xr-xr-dominanthand}

```lua
xr.dominantHand() -> Hand
```

The role of the hand whose controller points: `"right_hand"` by default.

**Returns** `Hand` — The dominant hand.

```lua
print(xr.dominantHand())
```

## typed/builtin//modules/api/engine/xr/xr/enter {#typed-builtin-modules-api-engine-xr-xr-enter}

```lua
xr.enter()
```

Start a session now, on the active runtime, starting the runtime
when it is not running: what the VR button does. On an engine whose
policy is `"off"` it does nothing. The person's to do: a call from world
or agent code raises.

```lua
xr.enter()
```

## typed/builtin//modules/api/engine/xr/xr/entry {#typed-builtin-modules-api-engine-xr-xr-entry}

```lua
xr.entry() -> Entry
```

How this engine comes to draw to a headset: its `policy` (`"off"`,
`"auto"` or `"always"`), whether an OpenXR runtime is `installed`, its
`runtime` name and `kind`, whether it is `running`, and whether the person
left VR (`declined`).

**Returns** `Entry` — The entry.

```lua
local e = xr.entry(); if e.installed and e.running ~= "running" then print("the VR button starts", e.runtime) end
```

## typed/builtin//modules/api/engine/xr/xr/exit {#typed-builtin-modules-api-engine-xr-xr-exit}

```lua
xr.exit()
```

Leave VR: the session ends on the next frame. On an engine with a
flat view to return to (`xr.returnsToFlat()`) the window or the page is
the view again, and the engine does not enter VR by itself again until
`xr.enter()` or the runtime starts anew; on a standalone headset app the
runtime ends the app, as the headset's own Quit does. The person's to do:
a call from world or agent code raises.

```lua
xr.exit()
```

## typed/builtin//modules/api/engine/xr/xr/fade {#typed-builtin-modules-api-engine-xr-xr-fade}

```lua
xr.fade(opacity: number, seconds: number?, color: { number }?)
```

Run the veil over the person's whole view toward `opacity` (0 open, 1
closed) over `seconds`, in `color` (`{ r, g, b }`, 0..1). The world panels
stay readable over it, so a world can be taken down behind a closed view
while a panel says what comes next.

**Parameters**

- `opacity` `number` — 0 (open) to 1 (closed).
- `seconds` `number` _(optional)_ — How long the fade runs; 0 when omitted.
- `color` `{ number }` _(optional)_ — `{ r, g, b }`; the colour the veil has when omitted.

```lua
xr.fade(1, 0.35)
```

## typed/builtin//modules/api/engine/xr/xr/fadeState {#typed-builtin-modules-api-engine-xr-xr-fadestate}

```lua
xr.fadeState() -> Fade
```

The veil now: its `opacity`, the `target` it runs to, whether it has
arrived (`done`), its `color` and how many fades were asked for.

**Returns** `Fade` — The fade.

```lua
if xr.fadeState().done then print("arrived") end
```

## typed/builtin//modules/api/engine/xr/xr/focused {#typed-builtin-modules-api-engine-xr-xr-focused}

```lua
xr.focused() -> boolean
```

Whether an XR session holds input focus, which is when the hand
controllers drive the game's input. False with no session, while the
system menu holds input, and while the headset is not worn.

**Returns** `boolean` — True while controller input flows.

```lua
if xr.focused() then showHandHints() end
```

## typed/builtin//modules/api/engine/xr/xr/gestures {#typed-builtin-modules-api-engine-xr-xr-gestures}

```lua
xr.gestures() -> Gestures
```

What the gestures of the bare hands read. The left palm facing the
person with a pinch holds the menu button (`xr.controller("left_hand").menu`),
a right-hand pinch-flick to the left or the right is one snap turn, and the
left hand pointing with a pinch held aims a teleport that lands when the pinch
opens. A pinch with the laser on an object grips it in the editor. Hand
tracking drives the gestures; a hand holding a controller makes none.

**Returns** `Gestures` — The gestures.

```lua
local g = xr.gestures(); if g.teleport.aiming then print(g.teleport.seconds) end
```

## typed/builtin//modules/api/engine/xr/xr/hand {#typed-builtin-modules-api-engine-xr-xr-hand}

```lua
xr.hand(hand: Hand) -> HandPose?
```

The pose of a driven hand: its palm in world space and the direction each
finger bone points along in the palm's frame, with its pinch and where its
aim comes from. A controller's hand is modelled around its grip.

**Parameters**

- `hand` `Hand` — `"left_hand"` or `"right_hand"`.

**Returns** `HandPose?` — The hand's pose, or nil while nothing drives the hand.

```lua
local pose = xr.hand("left_hand"); if pose and pose.pinching then grab() end
```

## typed/builtin//modules/api/engine/xr/xr/handSource {#typed-builtin-modules-api-engine-xr-xr-handsource}

```lua
xr.handSource(hand: Hand) -> HandSource
```

What drives a hand now: `"controller"` while a controller is held in it,
`"hand_tracking"` while the runtime tracks the bare hand, `"none"` otherwise
and while no session holds input focus.

**Parameters**

- `hand` `Hand` — `"left_hand"` or `"right_hand"`.

**Returns** `HandSource` — The source.

```lua
if xr.handSource("right_hand") == "hand_tracking" then showPinchHint() end
```

## typed/builtin//modules/api/engine/xr/xr/handTracking {#typed-builtin-modules-api-engine-xr-xr-handtracking}

```lua
xr.handTracking() -> HandTracking
```

What the runtime offers for tracking bare hands. Where it offers none,
`reason` says why and the controllers are the only hands.

**Returns** `HandTracking` — The hand tracking status.

```lua
if not xr.handTracking().available then print(xr.handTracking().reason) end
```

## typed/builtin//modules/api/engine/xr/xr/joints {#typed-builtin-modules-api-engine-xr-xr-joints}

```lua
xr.joints(hand: Hand) -> HandJoints?
```

The 26 joints of a driven hand in world space, by name. They are the
runtime's wherever it locates the hand (`modelled` false) and the model's
around the controller's grip otherwise (`modelled` true).

**Parameters**

- `hand` `Hand` — `"left_hand"` or `"right_hand"`.

**Returns** `HandJoints?` — The joints, or nil while nothing drives the hand.

```lua
local tip = xr.joints("right_hand").joints.index_tip.position
```

## typed/builtin//modules/api/engine/xr/xr/perspective {#typed-builtin-modules-api-engine-xr-xr-perspective}

```lua
xr.perspective() -> Perspective
```

What the person sees from in a headset: `"embodied"` seats the eyes in
the local player's body (at its head height, over its root, the real head
free on top, the body's head hidden from the eyes alone and its arms
following the hands), and `"game"` shows the game's own camera with the
head free. A world whose camera is first person is embodied; a world with
no body to find shows the game's camera; any other world is embodied
unless the person chose otherwise. The right thumbstick's click switches.

**Returns** `Perspective` — The perspective in force.

```lua
if xr.perspective() == "embodied" then print("in the body") end
```

## typed/builtin//modules/api/engine/xr/xr/perspectiveStatus {#typed-builtin-modules-api-engine-xr-xr-perspectivestatus}

```lua
xr.perspectiveStatus() -> PerspectiveStatus
```

Everything about the perspective: the one in force, the kept choice,
whether the person can choose, why it is what it is, the body found, where
the eyes stand, and how much of the body is hidden from the eyes or
reaching for the hands.

**Returns** `PerspectiveStatus` — The status.

```lua
print(xr.perspectiveStatus().why)
```

## typed/builtin//modules/api/engine/xr/xr/pointerRay {#typed-builtin-modules-api-engine-xr-xr-pointerray}

```lua
xr.pointerRay() -> PointerRay?
```

The ray the person points along, in world space: the aim pose of the
dominant hand, or of the other hand while the dominant one cannot be
located.
The engine's pointer hit (`getPointerHitTable()`, what a mouse hover resolves on desktop) is cast
along this ray while the session holds input focus, and the game's mouse
(`input.snapshot().mouse_position`) is the point it rests on, seen from the
game's camera; `screenToRay` at that position returns this ray.

**Returns** `PointerRay?` — `{ role, origin, direction }`, or nil while no session holds input focus.

```lua
local ray = xr.pointerRay(); if ray then print(ray.role, ray.origin.x) end
```

## typed/builtin//modules/api/engine/xr/xr/pose {#typed-builtin-modules-api-engine-xr-xr-pose}

```lua
xr.pose(role: Role, kind: PoseKind?) -> Pose?
```

The pose of the device holding `role`, in world space, as of the frame
last drawn: `position` in metres and `rotation` a quaternion. The pose
passes through the same rig as the eyes, so it moves and turns with the
game's camera. For a hand it is where the hand holds the controller, and
`kind` `"aim"` is where the controller points, along its -Z axis.

**Parameters**

- `role` `Role` — `"head"`, `"left_hand"` or `"right_hand"`.
- `kind` `PoseKind` _(optional)_ — `"pose"` (the default) or `"aim"`.

**Returns** `Pose?` — The pose, or nil while the role's device is untracked or has no such pose.

```lua
local head = xr.pose("head"); if head then print(head.position.y) end
local aim = xr.pose("right_hand", "aim")
```

## typed/builtin//modules/api/engine/xr/xr/pulse {#typed-builtin-modules-api-engine-xr-xr-pulse}

```lua
xr.pulse(hand: Hand, amplitude: number, seconds: number, frequency: number?) -> boolean
```

Vibrate a hand controller. The pulse plays on the next frame. A
request that is not a valid pulse raises; one that cannot play because
no session holds input focus (or too many pulses are waiting) returns
false and plays nothing.

**Parameters**

- `hand` `Hand` — `"left_hand"` or `"right_hand"`.
- `amplitude` `number` — Strength, 0 to 1.
- `seconds` `number` — Length in seconds, above 0 and up to 10.
- `frequency` `number` _(optional)_ — Hertz; 0 or nil lets the runtime choose.

**Returns** `boolean` — Whether the pulse was accepted.

```lua
xr.pulse("right_hand", 0.6, 0.1) -- a short tap on the right hand
```

## typed/builtin//modules/api/engine/xr/xr/recenter {#typed-builtin-modules-api-engine-xr-xr-recenter}

```lua
xr.recenter()
```

Centre the headset's world on where the person stands and faces now,
on the next frame drawn; `xr.recenters()` rises by one.

```lua
xr.recenter()
```

## typed/builtin//modules/api/engine/xr/xr/returnsToFlat {#typed-builtin-modules-api-engine-xr-xr-returnstoflat}

```lua
xr.returnsToFlat() -> boolean
```

Whether this engine has a flat view to return to when its session
ends: a desktop window or a browser page. A standalone headset app (the
Android engine in a headset) has none, so its menus offer Quit where a
desktop offers Exit VR.

**Returns** `boolean` — True with a window or a page behind the headset.

```lua
if xr.returnsToFlat() then print("Exit VR goes back to the window") end
```

## typed/builtin//modules/api/engine/xr/xr/roles {#typed-builtin-modules-api-engine-xr-xr-roles}

```lua
xr.roles() -> { Role }
```

The roles whose tracked device the runtime locates now, in the order
`"head"`, `"left_hand"`, `"right_hand"`. The head is listed while a
session draws frames; a hand is listed while the session holds input
focus and its controller is located.

**Returns** `{ Role }` — Array of role names; empty with no session.

```lua
for _, role in xr.roles() do print(role) end
```

## typed/builtin//modules/api/engine/xr/xr/setAim {#typed-builtin-modules-api-engine-xr-xr-setaim}

```lua
xr.setAim(aim: CameraAim) -> CameraAim
```

Choose where the game camera faces in a headset. `"head"` faces where
the head does; `"controller"` faces along the dominant hand's aim
direction with the head's up, so a game that aims, picks up or prompts
along its camera's forward does it where the controller points. A setting
of this client (`xr.aim` in the client settings): it holds across scene
loads and applies on the next frame.

**Parameters**

- `aim` `CameraAim` — `"head"` or `"controller"`.

**Returns** `CameraAim` — The canonical spelling of the aim now in force.

```lua
xr.setAim("controller")
```

## typed/builtin//modules/api/engine/xr/xr/setDominantHand {#typed-builtin-modules-api-engine-xr-xr-setdominanthand}

```lua
xr.setDominantHand(hand: Hand) -> Hand
```

Choose the hand whose controller points. A setting of this client: it
holds across scene loads.

**Parameters**

- `hand` `Hand` — `"left_hand"` or `"right_hand"`.

**Returns** `Hand` — The canonical spelling of the hand now in force.

```lua
xr.setDominantHand("left_hand")
```

## typed/builtin//modules/api/engine/xr/xr/setPerspective {#typed-builtin-modules-api-engine-xr-xr-setperspective}

```lua
xr.setPerspective(perspective: Perspective) -> Perspective
```

Choose what the person sees from: the person's client setting
`xr.perspective`, the same in every world. A first-person world stays
embodied and a world with no body stays on the game's camera; the choice
is kept and applies where it can. The choice is the person's, made in the
client settings or by the right thumbstick's click: a call from world code
raises, and a world reads the choice with `xr.perspective()`.

**Parameters**

- `perspective` `Perspective` — `"embodied"` or `"game"`.

**Returns** `Perspective` — The perspective in force after the call.

```lua
xr.setPerspective("game")
```

## typed/builtin//modules/api/engine/xr/xr/setPointerReach {#typed-builtin-modules-api-engine-xr-xr-setpointerreach}

```lua
xr.setPointerReach(distance: number?)
```

Say how far along the pointer ray the laser is drawn while a script
carries something to a point the engine's own cast does not know of (an
asset held under the laser): the beam ends `distance` metres out. The laser
over a panel still ends on the panel. Call it each frame it applies; nil
hands the beam back to the engine's cast.

**Parameters**

- `distance` `number` _(optional)_ — Metres along the ray, above 0, or nil.

```lua
xr.setPointerReach(where.distance)
```

## typed/builtin//modules/api/engine/xr/xr/setSnapTurnDegrees {#typed-builtin-modules-api-engine-xr-xr-setsnapturndegrees}

```lua
xr.setSnapTurnDegrees(degrees: number) -> number
```

Choose the angle one snap-turn step turns the view by, held to
10..180 degrees. The client settings apply the person's snap angle through
it.

**Parameters**

- `degrees` `number` — Degrees per step.

**Returns** `number` — The angle now in force.

```lua
xr.setSnapTurnDegrees(30)
```

## typed/builtin//modules/api/engine/xr/xr/setTurnMode {#typed-builtin-modules-api-engine-xr-xr-setturnmode}

```lua
xr.setTurnMode(mode: TurnMode) -> TurnMode
```

Choose how the right stick turns the view in a headset. A comfort
setting of this client: it holds across scene loads, and a settings
screen offering it is this call.

**Parameters**

- `mode` `TurnMode` — `"snap"` or `"smooth"`.

**Returns** `TurnMode` — The canonical spelling of the mode now in force.

```lua
xr.setTurnMode("smooth")
```

## typed/builtin//modules/api/engine/xr/xr/setVignette {#typed-builtin-modules-api-engine-xr-xr-setvignette}

```lua
xr.setVignette(strength: number) -> number
```

Choose how dark the comfort vignette grows while the game moves the
person. A comfort setting of this client.

**Parameters**

- `strength` `number` — 0 (none) to 1.

**Returns** `number` — The strength now in force.

```lua
xr.setVignette(0.5)
```

## typed/builtin//modules/api/engine/xr/xr/snapTurnDegrees {#typed-builtin-modules-api-engine-xr-xr-snapturndegrees}

```lua
xr.snapTurnDegrees() -> number
```

The angle one snap-turn step turns the view by, in degrees: the
person's snap angle (client setting `xr.snapAngle`), 45 by default.

**Returns** `number` — Degrees per step.

```lua
print(xr.snapTurnDegrees())
```

## typed/builtin//modules/api/engine/xr/xr/tracked {#typed-builtin-modules-api-engine-xr-xr-tracked}

```lua
xr.tracked(role: Role) -> boolean
```

Whether the runtime locates the device holding `role` now.

**Parameters**

- `role` `Role` — `"head"`, `"left_hand"` or `"right_hand"`.

**Returns** `boolean` — True while the role's device is located.

```lua
if xr.tracked("left_hand") then showLeftHandHint() end
```

## typed/builtin//modules/api/engine/xr/xr/turnMode {#typed-builtin-modules-api-engine-xr-xr-turnmode}

```lua
xr.turnMode() -> TurnMode
```

How the right stick turns the view in a headset. `"snap"` (the
default) turns by one step of `xr.snapTurnDegrees()` per flick of the
stick; `"smooth"` turns continuously in proportion to the stick's
deflection. Either way the game turns its own camera through its look
action and the headset follows the camera's yaw; the stick never tilts
the view, because the head owns pitch.

**Returns** `TurnMode` — The turn mode.

```lua
print(xr.turnMode())
```

## typed/builtin//modules/api/engine/xr/xr/vignette {#typed-builtin-modules-api-engine-xr-xr-vignette}

```lua
xr.vignette() -> number
```

How dark the comfort vignette grows at the edge of the view while the
game moves or turns the person, 0 (none) to 1. How much of it shows
follows how fast the person is moved or turned; a snap turn or a cut
darkens nothing.

**Returns** `number` — The strength.

```lua
print(xr.vignette())
```
