Skip to content

XR World

Updated

The real room an AR or mixed-reality device understands: where it sits in the scene, its light and its depth.

Type
ent-xr-world
Script class
XRWorldEntity
Family
XR

The XR World stands for the real room around the player during an AR or mixed-reality session, and for the player’s room in VR: where it sits in the scene, how it is lit and how deep it is. It starts empty, with only its Transform, and you give it what your scene needs by adding components:

  • XR Origin decides how the player’s room sits on the spawn point, and lets the player recenter: bring the scene back in front of them, wherever they have wandered. Its props floorOffset (raise the room for seated play) and worldScale (play as a giant or a tiny figure) apply at every spawn placement.
  • XR Light Estimate lights the scene like the real room, with an ambient light and a main directional light updated from the device’s estimate. Its castShadow prop lets that main light cast shadows.
  • XR Depth Occlusion lets real things hide virtual ones, so a real table hides a virtual ball behind it. Its maxDistance, depthBias and softness props tune how far, where and how softly.
  • Signal Emitter passes the XR Origin’s events, XROrigin::recenter and XROrigin::reset, on to scripts.

Everything here works in play, during an immersive session. In the editor, on a desktop or on a phone outside a session, the XR World holds nothing and costs nothing: nothing is asked of the device until a component needs it. Add one XR World per scene.

The real surfaces themselves are separate entities: use XR Planes for detected floors, walls and tables, and XR Meshes for the scanned shape of the room.

  1. Add an XR World entity to the scene. See Scenes and hierarchy.
  2. Add an XR Origin to it if players should be able to recenter, play seated or change scale.
  3. For AR and mixed reality, add XR Light Estimate and XR Depth Occlusion if the scene should match the room’s light and hide behind real objects.
  4. Add a Signal Emitter with a signal name to receive the XR Origin’s events in scripts.
  5. Make sure the scene has a Spawn Point: it sets the home the XR Origin recenters to.

Bring the scene back in front of the player when they press the left grip.

GripRecenter.jx
#pragma lifecycle(startup, update)
let xrOrigin;
let left;
function startup() {
xrOrigin = findEntity('XR World')?.getComponent(XROriginComponent);
left = Input.xr.controller('left');
}
function update() {
if (!xrOrigin || !left.isReleased(XRButton.Squeeze)) return;
if (!xrOrigin.recenter()) log('Cannot recenter: no session or head not tracked');
}

How it works:

  • getComponent(XROriginComponent) reaches the XR Origin on the XR World; Input.xr.controller('left') is the left controller, the same live object every frame.
  • isReleased(XRButton.Squeeze) is true for one frame after the grip is let go, so the action runs once per press.
  • recenter() puts the player’s head on the home, facing its way, and returns false outside a session or while tracking is lost.

Give the player feedback when the scene recenters, whether a script or the headset did it.

RecenterFeedback.jx
#pragma lifecycle(startup)
function startup() {
// The XR World has an XR Origin and a Signal Emitter with the signal 'xr-world'.
on('xr-world', (event) => {
if (event.eventType === EventType.XROrigin.Recenter) {
Haptics.vibrate(60, { intensity: 0.5 });
}
if (event.eventType === EventType.XROrigin.Reset) {
emit('hud/toast', { text: 'View reset' });
}
});
}

How it works:

  • XROrigin::recenter is sent after recenter() succeeds; Haptics.vibrate buzzes the controllers for 60 milliseconds.
  • XROrigin::reset is sent when the device recenters its own tracking, for example after a long press of the headset’s menu button, and the player is already back on the home.

Let a settings menu switch between a fast look and a realistic one that matches the room.

RealismSettings.jx
#pragma lifecycle(startup)
function startup() {
const world = findEntity('XR World');
const light = world?.getComponent(XRLightEstimateComponent);
const occlusion = world?.getComponent(XRDepthOcclusionComponent);
on('settings/quality', (data) => {
const high = Boolean(data && data.high);
if (light) light.setProps({ castShadow: high });
if (occlusion) occlusion.setProps({ softness: high ? 0.05 : 0, depthBias: 0.02 });
log(high ? 'Realistic mode' : 'Fast mode');
});
}

How it works:

  • Both components live on the XR World; the checks keep the script working when the scene has only one of them.
  • castShadow makes the estimated main light cast shadows on surfaces that receive them.
  • softness: 0.05 fades virtual objects out over 5 cm behind real surfaces instead of cutting them sharply; depthBias: 0.02 pushes the real surfaces back 2 cm so a virtual cup on a real table is not cut at the contact line.

Raise the room for players who sit down, from a menu choice.

SeatedMode.jx
#pragma lifecycle(startup)
const SEATED_RAISE = 0.5; // metres between sitting and standing eye height
function startup() {
const xrOrigin = findEntity('XR World')?.getComponent(XROriginComponent);
if (!xrOrigin) return;
on('settings/seated', (data) => {
const seated = Boolean(data && data.seated);
xrOrigin.setProps({ floorOffset: seated ? SEATED_RAISE : 0 });
});
}

How it works:

  • floorOffset raises the room at the spawn placement, so a seated player’s eyes are at standing height in the scene.
  • Changing it during a session applies it at once and puts the player back on the home; outside a session, it is kept for the next one.
  • Light estimation and depth occlusion depend on the device. Many phones estimate light but give no depth the occlusion can use, while most headsets do not estimate light and only some give depth. The components simply have no effect where a feature is missing.
  • Do not change worldScale or floorOffset every frame: each change re-places the player.

What a new XR World is made of. Required components are always present and cannot be removed; the others can. Per entity says whether an entity can hold one of that component or several. Layer, Lock and Enable are on every placed entity, and Transform holds its position, rotation and scale.

Component Type Role Per entity What it does
Layer comp-layer Required One Puts the entity on a render/raycast layer so cameras and rays can include or skip it.
Lock comp-lock Required One Editor-only flag that stops the entity from being selected or moved.
Enable comp-enable Required One Turns the entity and its children on or off: hides them and stops their behavior.
Transform comp-transform Required One Position, rotation and scale of the entity, with optional editor-only per-axis locks.

You can also add 4 optional components, such as Signal Emitter, XR Light Estimate, XR Occlusion, XR Origin.

With a Signal Emitter on the entity, these events reach scripts as { entityId, eventType, data }. Compare eventType with the EventType constants.

It reports the events every entity has: Transform::change (and position-change, rotation-change, scale-change), Enable::change, Layer::change and Lock::change.

Run it in the browser, read its scripts, and download it as an XR Creator project.

Find the entity by its name, check its class, and work with its components. Name the entity in the Hierarchy first; this example expects My XR World.

XRWorldEntity.jx
#pragma lifecycle(startup)
function startup() {
// Find the entity by the name it has in the Hierarchy.
const entity = findEntity('My XR World');
if (!(entity instanceof XRWorldEntity)) return;
log(entity.name, entity.position);
}