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.
Before you start
Section titled “Before you start”- 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.
Three entities
Section titled “Three entities”| 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 entity
Section titled “The XR World entity”The XR World starts empty, with only its Transform, and can carry:
| Component | Type | What it adds |
|---|---|---|
| XR Origin | comp- |
Where the room sits in the scene; see XR Origin. |
| XR Light Estimate | comp- |
The scene is lit like the real room. |
| XR Depth Occlusion | comp- |
Real things hide virtual ones behind them. |
| Signal Emitter | comp- |
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 XR Planes and XR Meshes entities
Section titled “The XR Planes and XR Meshes entities”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 Plane Collider, a material, a Signal Emitter |
| XR Meshes | XR Meshes (comp-) |
XR Mesh Collider, a material, a Signal Emitter |
Surfaces: XR Planes and XR Meshes
Section titled “Surfaces: XR Planes and XR Meshes”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.
How the surfaces look
Section titled “How the surfaces look”- 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.
GraphicsRaycasterhits the surfaces only while they are drawn.PhysicsRaycasterhits 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.
Shadows
Section titled “Shadows”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.
Scripting
Section titled “Scripting”| 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');Events
Section titled “Events”With a Signal Emitter on the XR Planes or XR Meshes entity:
| eventType | Constant | data | When |
|---|---|---|---|
XRPlanes::detected, XRMeshes::detected |
Event, Event |
{ label?, mesh } |
A surface appeared. |
XRPlanes::updated, XRMeshes::updated |
Event, Event |
{ label?, mesh } |
The device changed its shape. |
XRPlanes::lost, XRMeshes::lost |
Event, Event |
{ 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.
lostis also sent for each surface when the session ends orlabelschanges.- No
lostis sent when the entity is removed or switched off, or its scene is left: the surfaces go silently with it.
Colliders for the real room
Section titled “Colliders for the real room”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 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 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 Planes with an XR Plane Collider and XR Meshes with an XR Mesh Collider make the detected room solid, drawn as translucent blue as in the runtime AR Placement demo. Balls drop every two seconds and bounce on the real floor and furniture.
This one needs AR: enter AR on a device that detects planes or room meshes and the balls land on real surfaces. On a desktop nothing is detected, so a virtual floor catches them.
XR Light Estimate
Section titled “XR Light Estimate”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
Section titled “XR Depth Occlusion”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.
An XR World with XR Light Estimate and XR Depth Occlusion: in AR the scene is lit like the real room and real things hide virtual ones. Buttons switch between a fast mode and high realism, with shadows from the real main light and soft occlusion.
This one needs AR: enter AR on an Android phone or a Quest 3 and press High realism. On a desktop there is no estimate or depth, so the stage lights stay in charge and the buttons only change the settings.
Examples
Section titled “Examples”1. Count the tables in the room
Section titled “1. Count the tables in the room”#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 }); });}Counts the real tables an AR device detects from the XR Planes events, and emits the count on hud/tables for a HUD to show.
This one needs AR: in a headset or AR phone that labels planes, each table found raises the count. On a desktop no planes are detected.
- Every detected plane sends
detectedonce andlostonce, so the count stays right, also when the session ends. event.data.labelis the device’s label; planes without a label, or with another one, are ignored.updatedevents, sent when a table’s outline grows, do not change the count, so they are skipped.
Snippets
Section titled “Snippets”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.
#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
floorsurfaces, so every event here is about the floor. event.data.meshis the floor surface in the scene;getWorldPositiongives where it is in the world. The board appears on it.setPositionalso moves any physics body on the board;setEnabled(true)shows it.placedmakes it happen once. For a board that should stay exactly there, add an XR Anchor and callanchorHere(); 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.
#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()anddirection()give where the right controller is and where it points; both arenullwhile it is not tracked.spawncopies the ball at the controller;applyImpulsethrows 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.
delayremoves each ball after 20 seconds so they do not pile up.
3. Choose which surfaces count
Section titled “3. Choose which surfaces count”A settings menu switches between “floor only” and “every surface” for the colliders.
#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'); });}labelsfilters 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
lostevent is sent for each surface that goes.
4. Match the room’s light and depth
Section titled “4. Match the room’s light and depth”A quality setting turns on shadows from the real main light and soft occlusion.
#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.
castShadowmakes the estimated main light cast shadows, which a shadow-receiving floor plane then shows.softness: 0.05fades virtual objects over 5 cm behind real surfaces;depthBiaspushes the real surfaces back 2 cm so objects resting on a table are not cut at the contact line.
Common mistakes
Section titled “Common mistakes”- 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
receiveShadowand use a light that casts shadows, or a matte shadow material. - Waiting for a
lostevent 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.