XR Meshes
Updated
The scanned shape of the real room an AR or mixed-reality device detects.
- Type
ent-xr-meshes- Script class
XRMeshesEntity- Family
- XR
Overview
Section titled “Overview”XR Meshes brings the scanned shape of the real room into the scene: the detailed geometry of furniture, walls and objects that some mixed-reality devices build by scanning the room. Each scanned room mesh becomes a child mesh of the entity, placed where it is in the room every frame. Where XR Planes gives flat outlines, XR Meshes follows the real shapes, so a virtual ball can roll off the curved arm of a real sofa.
Room meshes appear only in play, during a mixed-reality session, on a device that scans the room. In the editor, on a desktop or in VR, the entity holds nothing. Its own transform plays no part: each mesh is where it is in the room.
How the meshes look depends on the entity’s material:
- With no material, they are hidden: never drawn, but they still collide when the entity has an XR Mesh Collider.
- With a material component, every mesh is drawn with that one material: a wireframe or translucent look to see the scan while you test, or a Matte Shadow material that shows only virtual shadows on the real room.
The props that matter:
labelskeeps only the meshes with these labels, such ascouch,tableorglobal mesh. An empty list keeps them all. Changing it rebuilds the meshes from what is detected now.receiveShadowlets the meshes receive the shadows of virtual objects. It is separate from the XR Planes setting, so a shadow does not land twice where a floor plane and the room mesh overlap.
Choose XR Meshes for exact collisions and shadows on furniture; choose XR Planes where flat surfaces are enough or the device does not scan rooms. The XR World handles the room’s light, depth and recentering.
Set it up
Section titled “Set it up”- Add an XR Meshes entity to the scene, anywhere. See Scenes and hierarchy.
- Set
labelsto the kinds of object you need, or leave it empty for everything. - To make the room solid for physics, add an XR Mesh Collider: it uses the exact triangles of the scan.
- To see the scan or catch shadows on it, add a material component and turn on
receiveShadowas needed. - To react to new meshes from a script, add a Signal Emitter with a signal name.
Examples
Section titled “Examples”List what the device recognised
Section titled “List what the device recognised”Collect the labels of the scanned objects and announce them once the room has been scanned.
#pragma lifecycle(startup)
const found = new Map(); // label -> how many
function startup() { // The XR Meshes entity has a Signal Emitter with the signal 'scan'. on('scan', (event) => { const label = event.data.label || 'other'; if (event.eventType === EventType.XRMeshes.Detected) found.set(label, (found.get(label) || 0) + 1); if (event.eventType === EventType.XRMeshes.Lost) found.set(label, Math.max(0, (found.get(label) || 0) - 1)); });
on('ui/inventory', () => { const lines = []; found.forEach((count, label) => { if (count > 0) lines.push(`${label}: ${count}`); }); log(lines.length ? lines.join(', ') : 'Nothing scanned yet'); });}How it works:
- Each room mesh sends
XRMeshes::detectedwhen it appears andXRMeshes::lostwhen it goes, with itslabelwhen the device gives one. - The script keeps a count per label in a
Map, so it always knows how many couches, tables or shelves are in the room. - A UI button that emits
ui/inventorylogs the list.
Throw balls that bounce off the furniture
Section titled “Throw balls that bounce off the furniture”Throw balls from the right controller that collide with the real room.
#pragma lifecycle(startup, update)
const SPEED = 4;let right;
function startup() { // The XR Meshes entity has an XR Mesh Collider. // 'Ball' is a disabled sphere with a dynamic Rigid Body and a Sphere Collider. 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);}How it works:
position()anddirection()give where the right controller is and where it points; both arenullwhile it is not tracked.spawncopies the ball template at the controller, andapplyImpulsethrows it along the pointing direction.- The XR Mesh Collider gives the room’s real triangles a solid shape, so the ball bounces off the sofa and rolls under the table.
delayremoves each ball after 20 seconds so they do not pile up.
Choose where shadows land
Section titled “Choose where shadows land”Switch shadows between the flat floor planes and the detailed room meshes from a settings menu.
#pragma lifecycle(startup)
function startup() { const planes = findEntity('XR Planes')?.getComponent(XRPlanesComponent); const meshes = findEntity('XR Meshes')?.getComponent(XRMeshesComponent);
on('settings/shadows', (data) => { const detailed = Boolean(data && data.detailed); if (meshes) meshes.setProps({ receiveShadow: detailed }); if (planes) planes.setProps({ receiveShadow: !detailed }); log('Shadows on', detailed ? 'room meshes' : 'planes', '-', meshes ? meshes.count : 0, 'meshes scanned'); });}How it works:
receiveShadowis set on each entity separately, so only one of them receives at a time and no shadow is drawn twice.- Changing
receiveShadowupdates the surfaces in place; nothing is rebuilt and no event is sent. countreports how many room meshes the entity holds now.
- Room meshes can hold many triangles. An XR Mesh Collider is exact but costs more than the thin boxes of an XR Plane Collider; use planes when flat surfaces are enough.
- Not every device scans rooms. Design so the experience still works with XR Planes alone, or with no detected surfaces at all.
- No
lostevent is sent when the entity is switched off or removed: its meshes go silently with it.
Components
Section titled “Components”What a new XR Meshes 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- |
Required | One | Position, rotation and scale of the entity, with optional editor-only per-axis locks. |
| XR Meshes | comp- |
Required | One | Shows the scanned shape of the real room. |
You can also add 13 optional components, such as Basic Material, Glass Material, Lambert Material, Matte Shadow Material, Normal Material, Phong Material.
The settings of a new entity, as the runtime creates it. Change them in the Properties panel of XR Creator, or from a script with getComponent(…).setProps({ … }).
| Prop | Default | Range |
|---|---|---|
labels |
[] |
|
receiveShadow |
false |
Events
Section titled “Events”With a Signal Emitter on the entity, these events reach scripts as { entityId, eventType, data }. Compare eventType with the EventType constants.
XRMeshes::detectedXRMeshes::updatedXRMeshes::lost
Like every entity, it also reports: Transform::change (and position-change, rotation-change, scale-change), Enable::change, Layer::change and Lock::change.
Live example
Section titled “Live example”Run it in the browser, read its scripts, and download it as an XR Creator project.
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.
From a script
Section titled “From a script”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 Meshes.
#pragma lifecycle(startup)
function startup() { // Find the entity by the name it has in the Hierarchy. const entity = findEntity('My XR Meshes'); if (!(entity instanceof XRMeshesEntity)) return;
// Its XR Meshes component holds the settings listed under Props. const component = entity.getComponent(XRMeshesComponent); log(component.props);}