XR
Updated
The same MIRA scene that runs in a browser tab can run in a headset or on a phone held up to the room. XR (extended reality) is the name for both: VR, where the headset replaces the world with your scene, and AR or mixed reality, where your scene is drawn over the real room. This section explains what changes for a scene and its scripts in such an immersive session: how the player is placed and recentered, how to pin content to real places, how the real floor, walls and tables enter the scene, and what a script can read from the headset.
Key concepts
Section titled “Key concepts”- Immersive session. The time between the player entering VR or AR and leaving it. Outside a session, everything on this section’s pages holds nothing and costs almost nothing, so one scene serves desktop, mobile and headsets.
- Player rig. In a session the player stands in a real room, and the headset tracks them in it. The rig is the virtual stand-in for that room: it carries the player through the scene. Moving the rig moves the player’s whole room; the player walking around the room moves them within it.
- Home. Where the spawn point placed the rig when the session started. The XR Origin can bring the player back to it.
- XR Origin. The component that controls how the rig sits on the home and lets the player recenter, turning the scene to face them wherever they stand.
- XR Anchor. A component that pins an entity to a real spot the device keeps tracking, so a virtual vase stays on the real table even as tracking drifts.
- XR World, XR Planes and XR Meshes. Entities that bring the real room into the scene: its detected planes (floors, walls, tables), its scanned meshes, its light and its depth, so virtual objects land on the real floor, bounce off a real table, are lit like the room and hide behind real things. In VR, the XR World also lets the player recenter and play seated. The real surfaces are placed where they are in the room, so the transform of an XR Planes or XR Meshes entity plays no part. Without a material, the surfaces are hidden but still collide when the entity has a collider; with a material, they are drawn with it.
- Hit test. A question asked of the device: “where does this ray meet a real surface?”.
XRRaycasterasks it; it is how AR content is placed on the floor. - Session features. Capabilities such as depth sensing or light estimation are granted only when a session starts. The runtime asks for those your scene uses, so add the XR World’s components before the player enters AR.
- Play only. None of this does anything in the editor.
How it works
Section titled “How it works”When the player enters VR or AR:
- Features are requested. The runtime looks at the active scene once and asks the device for the optional features its components and scripts use, such as anchors, depth sensing or light estimation. A device without a feature still enters; the related components simply have nothing to show.
- The player is placed. The spawn point places the rig, in VR and AR alike. That placement becomes the home. The XR Origin’s
floorOffsetandworldScaleapply here. - Every frame, the headset drives the camera. Camera controllers, camera paths and camera effects pause, because the headset owns the view. Controllers, hands and the viewer’s pose are read through
Input.xr. Detected surfaces are placed where they are in the room, anchors move their entities to the tracked places, and the light estimate updates the scene’s lights. - When the session ends, anchors are deleted and their entities keep their last pose; detected surfaces are removed.
Scripts take part through a few globals and components:
| You want to | Use | Read |
|---|---|---|
| Read controllers, buttons, sticks, hands and gestures | Input., XRButton |
XR controllers and hands |
| Know where the player looks, and read the passthrough camera, the light or the body | Input.xr.pose, Input., Input., Input.xr.body |
XR input |
| Recenter or reset the player | XROrigin |
XR Origin |
| Place content on a real surface | XRRaycaster, XRAnchor |
XR anchors |
| React to the real floor, walls and tables | XRPlanes, XRMeshes and their events |
XR world |
| Adapt the layout to a headset | Device. |
XR input |
Your first AR placement
Section titled “Your first AR placement”This walkthrough shows a ring on the real floor where the player looks and places a chair there with the trigger, or a tap on a phone.
-
Make a flat ring mesh named
Reticle. It will follow the real surface under the player’s gaze. -
Make a
Chairentity (any model or primitive) with an XR Anchor component, and switch the entity off. It is the original that the script copies. -
Add a Behavior anywhere in the scene with this script:
PlaceChair.jx #pragma lifecycle(startup, update)let reticle;let hand;function startup() {reticle = findEntity('Reticle');hand = Input.xr.controller(0);}function update() {const hit = XRRaycaster.fromViewer().hit();reticle.visible = Boolean(hit);if (!hit) return;reticle.position.copy(hit.point);reticle.quaternion.copy(hit.orientation);if (hand.isReleased(XRButton.Trigger)) {const chair = spawn('Chair', { position: hit.point, enabled: true });chair.getComponent(XRAnchorComponent).anchorAt(hit);}} -
Enter AR on a phone or a mixed-reality headset. Look at the floor: the ring lies on it. Pull the trigger, or tap the screen, to place a chair.
What each part does:
Input.xr.controller(0)is the first XR input: a controller, a tracked hand, or a finger on an AR phone’s screen. It is kept fromstartupbecause it is always the same live object.XRRaycaster.fromViewer()makes a ray straight ahead of the headset, or of the phone’s camera.hit()returns where it meets a real surface, ornull.hit.pointis the place andhit.orientationa rotation standing on the surface, so the ring lies flat on the floor or flat against a wall.isReleased(XRButton.Trigger)is true for one frame after the trigger is let go; on a phone, after the tap.spawncopies the hidden Chair;anchorAt(hit)ties the copy to that real spot so it stays put.
Your first AR placement: a ring follows the real surface under the viewer’s gaze, and the trigger or a tap places an anchored copy of a hidden chair there.
This one needs AR: open the page on an AR phone or a mixed-reality headset. On a desktop the ray meets no real surface, so the ring stays hidden and no chair is placed.
Choosing
Section titled “Choosing”| If you want… | Use… | Read |
|---|---|---|
| A “recenter” button for seated or standing play | XROrigin |
XR Origin |
| Seated play or a giant’s-eye view | XR Origin floorOffset, worldScale |
XR Origin |
| Content that stays on a real table | XR Anchor | XR anchors |
| Content that is still there next session | XR Anchor with persistent |
XR anchors |
| A recenter button, seated play, a different world scale, room lighting or real objects hiding virtual ones, in one entity | XR World and its components | XR world |
| The real floors, walls and tables as flat surfaces, for placement, physics and shadows | XR Planes | XR world |
| The detailed scanned shape of the room and its furniture | XR Meshes | XR world |
| Balls that bounce on the real floor | XR Planes with an XR Plane Collider | XR world |
| Virtual shadows on the real floor | XR Planes with receiveShadow and a matte shadow material |
XR world |
| Virtual objects lit like the room | XR Light Estimate | XR world |
| Real objects hiding virtual ones | XR Depth Occlusion | XR world |
| A HUD that follows the player’s gaze | Input.xr.pose |
XR input |
| Buttons, triggers, sticks, hand gestures | Input. |
XR controllers and hands |
| Teleporting and walking in VR | Teleport and Character Controller | Movement |
Entity types
Section titled “Entity types”The XR entity types work only in play, during an immersive session. Each has a page of its components, props, events and a first script:
| Entity | Type | What it is |
|---|---|---|
| XR Meshes | ent-xr-meshes | The scanned shape of the real room an AR or mixed-reality device detects. |
| XR Planes | ent-xr-planes | The real floors, walls and tables an AR or mixed-reality device detects. |
| XR World | ent-xr-world | The real room an AR or mixed-reality device understands: where it sits in the scene, its light and its depth. |
In scripts
Section titled “In scripts”| Global | What it is |
|---|---|
Input | Every input device: keyboard, mouse, touch, XR, accelerometer, gyroscope, microphone, device camera, location. |
XRButton | |
XRRaycaster | A ray against the real world, asked of the device (a WebXR hit test): the floor, walls, tables and anything else it understands, in an immersive session. The device only casts from what it tracks, so the rays start there: |
Device | Platform facts for scripts that adapt to the device they run on. |
Haptics | Vibration on whatever the player has: XR controllers in an XR session, a gamepad's rumble when one is connected, the phone otherwise. |
XROriginComponent | How the player rig (the XR origin) sits on the spawn placement. The spawn point always places the player, in VR and AR alike; this builds on where it put them, its home: recenter puts the user's head there from wherever they stand in the room, reset goes back to it, and so does a device recenter (sending reset). floorOffset and worldScale apply at every placement. |
XRAnchorComponent | Pins its entity to a spot in the real world: an anchor the device keeps tracking as its understanding of the room improves, so the entity stays put on the real table while the virtual world around it drifts. With auto it anchors where the entity is when a session starts; anchorAt anchors on a hit-test hit, which ties it to that surface. |
XRPlanesComponent | The planes the device detects (floors, walls, tables, …), each a flat mesh where it is in the room. On XR Planes; see XRGeometryComponent. |
XRMeshesComponent | The room meshes the device scans (the shape of furniture and walls), each a mesh where it is in the room. On XR Meshes; see XRGeometryComponent. |
XRPlaneColliderComponent | A thin box under each plane the device detects (XRPlanesComponent): the plane's bounds, 5 cm thick behind its surface, its top flush with it, so physics lands on the real floor, tables and walls. Rebuilt when the planes change or the rig moves; without planes it builds no collider. Each box is placed by its plane, so offset and rotation play no part. On XR Planes. |
XRMeshColliderComponent | The exact triangles of the room meshes the device scans (XRMeshesComponent), and only those: physics lands on the real furniture. Rebuilt when the meshes change or the rig moves; without room meshes it builds no collider. On XR Meshes. |
XRLightEstimateComponent | Lights the scene like the real room: with it on the XR World, entering XR asks the device to estimate the room's light, and each frame it puts the estimate on two lights it owns, an ambient light probe (spherical harmonics) and a directional light for the main light. Updated in place, nothing rebuilt. Until there is an estimate, and outside a session, its lights are hidden and the scene's own lights are untouched. |
XRDepthOcclusionComponent | Real things hide virtual ones: with it on the XR World, entering XR asks the device for its depth sensing, and three draws the sensed depth before the scene, so a real table occludes a virtual ball behind it. Real surfaces past maxDistance hide nothing, and depthBias pushes them back. With softness, the built-in mesh materials fade a virtual thing out over that depth behind a real surface instead of cutting it (their output scaled, as passthrough is composited); the hard pass is then pushed back by half of it, so other shaders (points, custom ones) are still cut. |
XRWorldEntity | The real room an AR or mixed-reality device understands, as a whole: with XROriginComponent where the room sits in the scene, XRLightEstimateComponent its light and XRDepthOcclusionComponent its depth. Starts empty. Its surfaces are entities of their own, XR Planes and XR Meshes (XRSurfacesEntity). Holds nothing outside a session. |
XRPlanesEntity | The planes the device detects (floors, walls, tables, …), from its XRPlanesComponent. Add an XR Plane Collider and physics lands on them. See XRSurfacesEntity. |
XRMeshesEntity | The room meshes the device scans (the shape of furniture and walls), from its XRMeshesComponent. Add an XR Mesh Collider and physics lands on them. See XRSurfacesEntity. |
Components
Section titled “Components”| Component | Type | What it does |
|---|---|---|
| XR Anchor | comp-xr-anchor | Pins the entity to a real-world spot. |
| XR Light Estimate | comp-xr-light-estimate | Lights the scene like the real room. |
| XR Mesh Collider | comp-xr-mesh-collider | Physics collides with the scanned room meshes. |
| XR Meshes | comp-xr-meshes | Shows the scanned shape of the real room. |
| XR Occlusion | comp-xr-occlusion | Real objects hide virtual ones behind them. |
| XR Origin | comp-xr-origin | How the player rig sits on the spawn point; recenter support. |
| XR Plane Collider | comp-xr-plane-collider | Physics collides with detected real planes. |
| XR Planes | comp-xr-planes | Shows detected real floors, walls and tables. |
The XR Plane Collider and XR Mesh Collider are listed with the physics components.
In this section
Section titled “In this section”| Topic | Description |
|---|---|
| XR Origin | The player rig, the home placement, recentering and resetting, seated play and world scale. |
| XR anchors | Pin entities to real places, place AR content with hit tests, keep anchors across sessions. |
| XR world | The real room in the scene: planes, meshes, their colliders and shadows, light estimation and depth occlusion. |
| XR input | The viewer’s pose, the passthrough camera, the light estimate, body tracking and session features. |