# Camera Access

In AR on a phone, the user sees the world through the device's camera, with your scene drawn over the camera's image. Camera access gives your application that image as a texture, updated every frame. Use it for effects that need the real world's colors: a material that reflects or refracts the room, a mirror, or a still that the user captures.

[Live example: AR Camera Color](https://playcanvas.com/examples/#/xr/ar-camera-color) ([source](https://github.com/playcanvas/engine/blob/main/examples/src/examples/xr/ar-camera-color/example.mjs))

## Requesting Camera Access

Ask for camera access when you start an AR session:

```javascript
camera.camera.startXr(XRTYPE_AR, XRSPACE_LOCALFLOOR, {
    cameraColor: true
});

app.xr.on('start', () => {
    if (!app.xr.views.availableColor) {
        console.log('This session has no camera access');
    }
});
```

`app.xr.views.supportedColor` is `true` when the browser implements camera access for the application's graphics device, and `availableColor` is `true` when a session that has it starts. Like every optional feature, the session starts without it if the browser or the user doesn't allow it.

Few devices allow camera access: Chrome on Android phones does, the Meta Quest doesn't, and Google doesn't list it for Android XR headsets. See [Platforms](https://developer.playcanvas.com/user-manual/xr/platforms.md#ar-features). The Meta Quest Browser offers the headset's passthrough cameras as ordinary video inputs, through [`getUserMedia()`](https://developer.mozilla.org/docs/Web/API/MediaDevices/getUserMedia), but without the pose of each image, which you would need to line it up with the scene.

## The Camera Texture

The image comes through the session's views. `app.xr.views` lists them: one view on a phone, with the eye `XREYE_NONE`, and one for each eye on a headset. Views are added shortly after the session starts, and each one's `textureColor` is a [`Texture`](https://api.playcanvas.com/engine/classes/Texture.html) that the engine updates with the camera image every frame, or `null` without camera access. This shows the image on a quad in the scene:

```javascript
const material = new StandardMaterial();
material.useLighting = false;
material.update();

const screen = new Entity('screen');
screen.addComponent('render', { type: 'plane', material });
screen.setLocalPosition(0, 1.2, -1);
screen.setLocalEulerAngles(90, 0, 0);
screen.setLocalScale(0.4, 1, 0.3);
rig.addChild(screen);

app.xr.views.on('add', (view) => {
    if (view.eye === XREYE_NONE && view.textureColor) {
        material.emissiveMap = view.textureColor;
        material.update();
    }
});

app.xr.on('end', () => {
    material.emissiveMap = null;
    material.update();
});
```

The texture is the view's own, and the engine destroys it when the view is removed, which happens when the session ends, so stop using it then. It has the camera's resolution, which can differ from the screen's.

To keep an image, such as a photo the user takes, copy the texture's pixels, for example by rendering it to a [render target](https://developer.playcanvas.com/user-manual/graphics/advanced-rendering/render-targets.md) and reading that back. Reading pixels from the GPU stalls the frame, so do it once, not every frame.

## Privacy

The camera image shows the user's surroundings, and possibly other people. Ask for camera access only when your application needs it, tell the user what it's for, and don't send the image anywhere without their consent.

## See Also

- [Depth Sensing](https://developer.playcanvas.com/user-manual/xr/ar/depth-sensing.md) - The distance to the real world at each pixel
- [Light Estimation](https://developer.playcanvas.com/user-manual/xr/ar/light-estimation.md) - The real world's light, without the camera image
- [XrViews](https://api.playcanvas.com/engine/classes/XrViews.html) and [XrView](https://api.playcanvas.com/engine/classes/XrView.html) - API reference
