Skip to content

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

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:

  • labels keeps only the meshes with these labels, such as couch, table or global mesh. An empty list keeps them all. Changing it rebuilds the meshes from what is detected now.
  • receiveShadow lets 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.

  1. Add an XR Meshes entity to the scene, anywhere. See Scenes and hierarchy.
  2. Set labels to the kinds of object you need, or leave it empty for everything.
  3. To make the room solid for physics, add an XR Mesh Collider: it uses the exact triangles of the scan.
  4. To see the scan or catch shadows on it, add a material component and turn on receiveShadow as needed.
  5. To react to new meshes from a script, add a Signal Emitter with a signal name.

Collect the labels of the scanned objects and announce them once the room has been scanned.

RoomInventory.jx
#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::detected when it appears and XRMeshes::lost when it goes, with its label when 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/inventory logs the list.

Throw balls from the right controller that collide with the real room.

ThrowBalls.jx
#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() and direction() give where the right controller is and where it points; both are null while it is not tracked.
  • spawn copies the ball template at the controller, and applyImpulse throws 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.
  • delay removes each ball after 20 seconds so they do not pile up.

Switch shadows between the flat floor planes and the detailed room meshes from a settings menu.

ShadowTarget.jx
#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:

  • receiveShadow is set on each entity separately, so only one of them receives at a time and no shadow is drawn twice.
  • Changing receiveShadow updates the surfaces in place; nothing is rebuilt and no event is sent.
  • count reports 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 lost event is sent when the entity is switched off or removed: its meshes go silently with it.

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-transform Required One Position, rotation and scale of the entity, with optional editor-only per-axis locks.
XR Meshes comp-xr-meshes 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

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

  • XRMeshes::detected
  • XRMeshes::updated
  • XRMeshes::lost

Like every entity, it also reports: 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 Meshes.

XRMeshesEntity.jx
#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);
}