Skip to content

XR world

Updated

In an AR session, or in mixed reality on a headset, the device understands the real room around the player: its floor, walls, tables and furniture, its light and its depth. Three kinds of entity bring that knowledge into your scene, so virtual things land on the real floor, cast shadows on it, are lit like the room, and hide behind real objects. This page covers the XR World, XR Planes and XR Meshes entities, the components they carry, their events and how scripts read them.

  • Read the XR overview for the ideas of session and session features.
  • For colliders and rigid bodies, see Physics.
  • Everything on this page works in play, in an AR or mixed-reality session. In the editor and outside a session, these entities hold nothing.
Entity Type Holds
XR World ent-xr-world The room as a whole: where it sits in the scene, its light and its depth.
XR Planes ent-xr-planes The detected planes: flat surfaces such as floors, walls and tables.
XR Meshes ent-xr-meshes The scanned room meshes: the detailed shape of furniture and walls.

Planes are simple and widely available; room meshes are more detailed and come from devices that scan the room.

The XR World starts empty, with only its Transform, and can carry:

Component Type What it adds
XR Origin comp-xr-origin Where the room sits in the scene; see XR Origin.
XR Light Estimate comp-xr-light-estimate The scene is lit like the real room.
XR Depth Occlusion comp-xr-occlusion Real things hide virtual ones behind them.
Signal Emitter comp-signal-emitter The XR Origin’s events reach scripts.

Outside a session, and in a scene that does not use these components, the XR World costs nothing: nothing is asked of the device until a component needs it.

The real surfaces are entities of their own, anywhere in the scene, under the XR World or not. Their own transform plays no part: each surface is where it is in the room. A scene can have several of each, each keeping some kinds of surface with its own look: for example a floor that catches shadows, and walls drawn as a hologram.

Entity Starts with Can also carry
XR Planes XR Planes (comp-xr-planes) XR Plane Collider, a material, a Signal Emitter
XR Meshes XR Meshes (comp-xr-meshes) XR Mesh Collider, a material, a Signal Emitter

Each detected surface becomes a plain child mesh of its entity, placed where it is in the room every frame. The surfaces follow the player rig. Geometry is rebuilt only when the device says a surface changed shape.

  • With no material on the entity, the surfaces are hidden: never drawn, but they still collide.
  • With a material component, every surface of the entity is drawn with that one material. Use a translucent look to see what the device detects while you test, or a Matte Shadow material to catch virtual shadows on the real floor. See Graphics.
  • GraphicsRaycaster hits the surfaces only while they are drawn. PhysicsRaycaster hits them through their colliders.
Prop Type Default Description
labels string list [] Keep only surfaces with these labels. Empty keeps all.
receiveShadow boolean false The surfaces receive the shadows of virtual things.

The labels the device can give are floor, ceiling, wall, door, window, table, desk, couch, bed, shelf, screen, lamp, plant, wall art, global mesh and other. Changing labels rebuilds the surfaces from what is detected now. Changing receiveShadow updates them in place.

With receiveShadow, the real surfaces receive the shadows of virtual things. They never cast any: real things already cast real shadows. Planes and room meshes each have their own setting, so a shadow does not land twice where a floor plane and the room mesh overlap. The entity’s material may override it: a matte shadow catcher always receives, a hologram never does. Any light that casts shadows reaches them, including the XR Light Estimate with castShadow.

Member Description
count How many surfaces it holds now.
meshes An iterator over its surfaces, each a THREE.Mesh placed in its entity’s space.
const planes = findEntity('XR Planes').getComponent(XRPlanesComponent);
log(planes.count, 'planes detected');

With a Signal Emitter on the XR Planes or XR Meshes entity:

eventType Constant data When
XRPlanes::detected, XRMeshes::detected EventType.XRPlanes.Detected, EventType.XRMeshes.Detected { label?, mesh } A surface appeared.
XRPlanes::updated, XRMeshes::updated EventType.XRPlanes.Updated, EventType.XRMeshes.Updated { label?, mesh } The device changed its shape.
XRPlanes::lost, XRMeshes::lost EventType.XRPlanes.Lost, EventType.XRMeshes.Lost { label?, mesh } It went.

label is the device’s label for the surface, absent when it gives none. mesh is the surface in the scene.

  • Nothing is sent when a surface only moves.
  • lost is also sent for each surface when the session ends or labels changes.
  • No lost is sent when the entity is removed or switched off, or its scene is left: the surfaces go silently with it.

Two collider components make the real surfaces solid, so virtual balls bounce on the real floor and roll off the real table. Each goes on the fixed body of its entity, takes only that entity’s surfaces, is rebuilt with them, and builds nothing while there are none.

Component Type On Shape
XR Plane Collider comp-xr-plane-collider XR Planes A thin box under each plane: the plane’s bounds, 5 cm thick behind the surface, its top flush with it. Solid and cheap. A plane that is not a rectangle collides as its bounding box.
XR Mesh Collider comp-xr-mesh-collider XR Meshes The exact triangles of the room meshes, like a Mesh Collider.

Both take the shared collider props friction, restitution, sensor and collisionLayer. Each box or mesh is placed by its surface, so offset and rotation play no part. See Rigid bodies and colliders.

XR Light Estimate (comp-xr-light-estimate) lights the scene like the real room. It owns two lights: an ambient light for the light all around, and a directional light for the main light, such as a window or a ceiling lamp. Each frame it puts the device’s estimate on them.

Prop Type Default Description
castShadow boolean false The estimated main light casts shadows.

Until there is an estimate, and outside a session, its lights are hidden and the scene’s own lights are untouched. Android phones estimate light; most headsets do not. Scripts can read the same estimate directly with Input.xr.light(); see XR input.

XR Depth Occlusion (comp-xr-occlusion, script class XRDepthOcclusionComponent) lets real things hide virtual ones. The device’s sensed depth is drawn before the scene, so a real table hides a virtual ball behind it.

Prop Type Default Description
maxDistance number 5 Real surfaces farther than this, in metres, hide nothing, because far depth is noisy. From 0.5 to 20.
depthBias number 0 Metres the real surfaces are pushed back, so a virtual cup on a real table is not cut at the contact line. From −0.5 to 0.5.
softness number 0 Metres over which a virtual object fades out behind a real surface, instead of being cut sharply. 0 is a hard edge. From 0 to 0.5.
  • With softness, points, lines and custom shaders are still cut, just not softly.
  • It works where the device gives depth on the GPU, such as Meta Quest 3. Most Android phones offer only CPU depth and show no occlusion.
CountTables.jx
#pragma lifecycle(startup)
let tables = 0;
function startup() {
on('room', (event) => {
if (event.data.label !== 'table') return;
if (event.eventType === EventType.XRPlanes.Detected) tables += 1;
if (event.eventType === EventType.XRPlanes.Lost) tables -= 1;
emit('hud/tables', { count: tables });
});
}
  • Every detected plane sends detected once and lost once, so the count stays right, also when the session ends.
  • event.data.label is the device’s label; planes without a label, or with another one, are ignored.
  • updated events, sent when a table’s outline grows, do not change the count, so they are skipped.

1. Put the game board on the first floor found

Section titled “1. Put the game board on the first floor found”

As soon as the device finds the floor, a hidden game board appears on it.

BoardOnFloor.jx
#pragma lifecycle(startup)
const spot = new THREE.Vector3();
let board;
let placed = false;
// The XR Planes entity has labels ['floor'] and a Signal Emitter with the signal 'floor'.
// 'Board' is a disabled entity in the scene.
function startup() {
board = findEntity('Board');
on('floor', (event) => {
if (placed || event.eventType !== EventType.XRPlanes.Detected) return;
event.data.mesh.getWorldPosition(spot);
board.setPosition(spot.x, spot.y, spot.z);
board.setEnabled(true);
placed = true;
});
}
  • The XR Planes entity keeps only floor surfaces, so every event here is about the floor.
  • event.data.mesh is the floor surface in the scene; getWorldPosition gives where it is in the world. The board appears on it.
  • setPosition also moves any physics body on the board; setEnabled(true) shows it.
  • placed makes it happen once. For a board that should stay exactly there, add an XR Anchor and call anchorHere(); see XR anchors.

2. Throw balls that bounce on the real floor

Section titled “2. Throw balls that bounce on the real floor”

The XR Planes entity carries an XR Plane Collider, so thrown balls land on real surfaces.

ThrowBalls.jx
#pragma lifecycle(startup, update)
const SPEED = 4;
let right;
// The XR Planes entity has an XR Plane Collider.
// 'Ball' is a disabled sphere with a dynamic Rigid Body and a Sphere Collider (restitution 0.6).
function startup() {
right = Input.xr.controller('right');
}
function update() {
if (!right.isReleased(XRButton.Trigger)) return;
const from = right.position();
const dir = right.direction();
if (!from || !dir) return;
const ball = spawn('Ball', { position: from, enabled: true });
ball.getComponent(PhysicsRigidBodyComponent).applyImpulse(dir.multiplyScalar(SPEED));
delay(() => destroy(ball), 20000);
}
  • position() and direction() give where the right controller is and where it points; both are null while it is not tracked.
  • spawn copies the ball at the controller; applyImpulse throws it along the pointing direction.
  • The XR Plane Collider gives every detected plane a thin solid box, so the ball bounces on the real floor and can land on a real table.
  • delay removes each ball after 20 seconds so they do not pile up.

A settings menu switches between “floor only” and “every surface” for the colliders.

SurfaceFilter.jx
#pragma lifecycle(startup)
function startup() {
const planes = findEntity('XR Planes').getComponent(XRPlanesComponent);
on('settings/surfaces', (data) => {
const floorOnly = Boolean(data && data.floorOnly);
planes.setProps({ labels: floorOnly ? ['floor'] : [] });
log(floorOnly ? 'Floor only' : 'All surfaces');
});
}
  • labels filters the planes the entity keeps. An empty list keeps them all.
  • Changing it rebuilds the surfaces from what is detected now, and their XR Plane Collider follows. A lost event is sent for each surface that goes.

A quality setting turns on shadows from the real main light and soft occlusion.

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 ? 'High realism' : 'Fast mode');
});
}
  • Both components live on the XR World. The checks keep the script working on a scene that has only one of them.
  • castShadow makes the estimated main light cast shadows, which a shadow-receiving floor plane then shows.
  • softness: 0.05 fades virtual objects over 5 cm behind real surfaces; depthBias pushes the real surfaces back 2 cm so objects resting on a table are not cut at the contact line.
  • Expecting surfaces on the desktop or in VR. Planes and meshes come from AR and mixed-reality sessions on devices that detect them.
  • Moving the XR Planes entity. Its transform plays no part: surfaces are where they are in the room.
  • Surfaces that do not show. Without a material component they are hidden on purpose. Add a material to see them.
  • No shadows on the floor. Turn on receiveShadow and use a light that casts shadows, or a matte shadow material.
  • Waiting for a lost event when the entity is switched off. Surfaces leave silently in that case.
  • Expecting occlusion on a phone. Depth occlusion needs depth on the GPU; most phones do not provide it.
  • Adding components after entering AR. Session features are fixed at entry. Add the XR World’s components before the player enters.