Skip to main content

AR

In augmented reality (AR), your scene appears in the user's surroundings. A headset such as the Meta Quest 3 shows the room through its passthrough cameras, and a phone shows it through its camera, with your scene drawn over it. AR sessions do everything that VR sessions do, with the same input, and add features that describe the real world: where its surfaces are, how far away it is, and how it's lit.

A kitchen seen in AR through a headset: an orange box and a blue cone on the kitchen island, a green sphere on the floor, and a placement ring on the dining table

AR Basic

How AR Differs from VR​

An AR session starts like a VR session, with the type XRTYPE_AR. See Sessions. What changes is what the user sees around your content:

  • The real world is the background. The camera must clear to a fully transparent color, and nothing may cover the view: no skybox, and no floor or room model. The XrSession script makes the camera transparent and hides the skybox for every AR session on its camera, and puts them back when the session ends. Disable any other background content during AR sessions yourself.
  • Content appears at its real size. One unit is one meter, and the user sees your content beside real things. Make content the size it would be in the room, and put it within reach of where the user starts.
  • The user moves by walking. Moving the rig moves your scene relative to the room, so AR applications rarely do. Locomotion is for VR.
  • The room has its own light. Your scene's lights don't light the real world, and the real world's light doesn't light your scene. Light estimation tells you how the room is lit, where the device supports it.

A headset that doesn't have cameras, but shows the world through a transparent display, such as the Magic Leap 2, can only add light to the view. On it, black is transparent and dark colors are faint, so use bright colors. app.xr.session.environmentBlendMode is 'additive' on such a display, and 'alpha-blend' on passthrough headsets and phones.

AR on a phone and AR on a headset also differ in ways that shape an application:

PhoneHeadset
ViewsOne, the screenTwo, one for each eye
InputTaps on the screen, each a transient input sourceControllers and hands
InterfaceHTML with DOM Overlay, or UI in the sceneUI in the scene
The user's handsOne holds the phoneBoth are free

On a phone, keep interactions to taps, and place content where the user points the phone. On a headset, the user can walk around, reach out and touch, so put content within reach. The same session code serves both, as long as it handles the input sources each one has.

Your First AR Scene​

Start from Your First VR Scene, in the Engine, the Editor, React or Web Components, and make these changes:

  1. Enter AR. Have the button fire ar:start instead of vr:start, check availability with XRTYPE_AR, and label it Enter AR. XrSession starts an AR session when ar:start fires.
  2. Remove the floor. The real floor is there.
  3. Bring the shapes close. Make them 0.3 m across, about the size of a vase, and put them a meter from where the user starts. XrSession starts the user on the ground below the camera, facing the way it faces, so with the camera at (0, 1.6, 2), the user starts at (0, 0, 2), and shapes at z = 1 are a meter ahead of them.

On a phone, the user looks at the shapes through the screen. On a headset, they can walk around them.

In React, <Camera> sets the camera's clear color from its clearColor prop each time it renders, over the transparent color that XrSession sets. Keep the component that renders it from rendering during a session, as the example in Getting Started does by keeping the button's state in its own component, or give clearColor a transparent value, '#00000000', during AR sessions.

XrSession starts sessions without options. To ask for features such as plane detection or anchors, start the session yourself, with the options you need. XrSession still makes the camera transparent and moves the rig:

button.addEventListener('click', () => {
camera.camera.startXr(XRTYPE_AR, XRSPACE_LOCALFLOOR, {
planeDetection: true,
anchors: true
});
});

Placing Content in the Room​

Most AR applications put content on real surfaces. The usual way is to hit test from the center of the view, show the user where content will go, place it when they select, and anchor it so that it stays put.

Everything an AR session finds in the real world, from hit test results to planes, meshes, anchors and tracked images, has its pose in the session's tracking space, relative to the rig. Make the entities that follow them children of the rig, and set their local position and rotation. They then line up with the real world wherever the rig is, and XrSession moves the rig when a session starts.

Features​

These features describe the real world:

FeatureWhat it does
Hit TestingFinds where a ray meets a real surface, to place content on it
AnchorsKeeps content fixed to a place in the room, and between sessions
Plane DetectionThe floor, walls, tables and other flat surfaces, with their outlines and labels
Mesh DetectionThe shape of the room and its furniture, for physics and occlusion
Depth SensingThe distance to the real world at each pixel
Light EstimationThe direction, color and intensity of the room's light
Camera AccessThe camera's image, as a texture
Image TrackingThe position of printed images that you provide
DOM OverlayHTML over the AR view on a phone

Every AR session asks for hit testing and light estimation. Ask for the others in the options of startXr(), and check that the session has them once it starts, as each page shows. Devices differ widely in which they have: see Platforms.

Testing Without a Device​

Meta's Immersive Web Emulator puts an emulated Meta Quest 3 in a desktop browser, in a choice of furnished rooms, and its AR sessions can hit test, anchor, and detect planes, meshes and depth in the room you choose. It's how the images and videos in this section were made. See Testing and Debugging.

AR on iPhone and iPad​

Safari and every other browser on iPhone and iPad have no WebXR AR, so these pages don't apply there. Third-party web AR frameworks bring AR to them. See Third-Party Frameworks.

See Also​