Skip to content

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.

  • 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?”. XRRaycaster asks 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.

When the player enters VR or AR:

  1. 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.
  2. The player is placed. The spawn point places the rig, in VR and AR alike. That placement becomes the home. The XR Origin’s floorOffset and worldScale apply here.
  3. 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.
  4. 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.xr.controller(…), XRButton XR controllers and hands
Know where the player looks, and read the passthrough camera, the light or the body Input.xr.pose, Input.xr.camera, Input.xr.light(), Input.xr.body XR input
Recenter or reset the player XROriginComponent XR Origin
Place content on a real surface XRRaycaster, XRAnchorComponent XR anchors
React to the real floor, walls and tables XRPlanesComponent, XRMeshesComponent and their events XR world
Adapt the layout to a headset Device.isXRPresenting() XR input

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.

  1. Make a flat ring mesh named Reticle. It will follow the real surface under the player’s gaze.

  2. Make a Chair entity (any model or primitive) with an XR Anchor component, and switch the entity off. It is the original that the script copies.

  3. 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);
    }
    }
  4. 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 from startup because 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, or null.
  • hit.point is the place and hit.orientation a 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.
  • spawn copies the hidden Chair; anchorAt(hit) ties the copy to that real spot so it stays put.
If you want… Use… Read
A “recenter” button for seated or standing play XROriginComponent.recenter() 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.controller XR controllers and hands
Teleporting and walking in VR Teleport and Character Controller Movement

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:

EntityTypeWhat it is
XR Meshesent-xr-meshesThe scanned shape of the real room an AR or mixed-reality device detects.
XR Planesent-xr-planesThe real floors, walls and tables an AR or mixed-reality device detects.
XR Worldent-xr-worldThe real room an AR or mixed-reality device understands: where it sits in the scene, its light and its depth.
GlobalWhat it is
InputEvery input device: keyboard, mouse, touch, XR, accelerometer, gyroscope, microphone, device camera, location.
XRButton
XRRaycasterA 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:
DevicePlatform facts for scripts that adapt to the device they run on.
HapticsVibration on whatever the player has: XR controllers in an XR session, a gamepad's rumble when one is connected, the phone otherwise.
XROriginComponentHow 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.
XRAnchorComponentPins 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.
XRPlanesComponentThe planes the device detects (floors, walls, tables, …), each a flat mesh where it is in the room. On XR Planes; see XRGeometryComponent.
XRMeshesComponentThe 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.
XRPlaneColliderComponentA 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.
XRMeshColliderComponentThe 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.
XRLightEstimateComponentLights 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.
XRDepthOcclusionComponentReal 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.
XRWorldEntityThe 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.
XRPlanesEntityThe planes the device detects (floors, walls, tables, …), from its XRPlanesComponent. Add an XR Plane Collider and physics lands on them. See XRSurfacesEntity.
XRMeshesEntityThe 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.
ComponentTypeWhat it does
XR Anchorcomp-xr-anchorPins the entity to a real-world spot.
XR Light Estimatecomp-xr-light-estimateLights the scene like the real room.
XR Mesh Collidercomp-xr-mesh-colliderPhysics collides with the scanned room meshes.
XR Meshescomp-xr-meshesShows the scanned shape of the real room.
XR Occlusioncomp-xr-occlusionReal objects hide virtual ones behind them.
XR Origincomp-xr-originHow the player rig sits on the spawn point; recenter support.
XR Plane Collidercomp-xr-plane-colliderPhysics collides with detected real planes.
XR Planescomp-xr-planesShows detected real floors, walls and tables.

The XR Plane Collider and XR Mesh Collider are listed with the physics components.

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.