Skip to main content

Light Estimation

Virtual objects look out of place when they're lit differently from the room around them: brightly lit in a dim room, or with shadows that fall the wrong way. Light estimation tells you how the real world is lit, from the device's view of it: the direction, color and intensity of the main light, such as the sun through a window, and the color of the light from every other direction. Use it to light your scene to match.

Starting Light Estimation​

Every AR session asks for light estimation, but it doesn't run until you start it. app.xr.lightEstimation.supported is true during an AR session that has it, so check it and start in the session's start handler:

app.xr.on('start', () => {
if (app.xr.lightEstimation.supported) {
app.xr.lightEstimation.start();
}
});

app.xr.lightEstimation.on('available', () => {
console.log('The first light estimate has arrived');
});

app.xr.lightEstimation.on('error', (err) => {
console.error(`Light estimation failed: ${err.message}`);
});

The first estimate takes a moment, and available is true, firing available, once it has arrived. app.xr.lightEstimation.end() stops it, and it stops when the session ends. Few devices estimate light: see Platforms.

The Main Light​

While light estimation is available, these properties describe the strongest light that the device can see, as if it were a directional light. They are null until it's available:

PropertyDescription
rotationA Quat: the rotation of a directional light that shines the way the real light does
colorA Color: the light's color
intensityA number: the light's intensity, for a light's intensity property. It's at least 1

Copy them to a directional light every frame. This light lights the scene's objects, and its shadows fall the way the room's do:

const light = new Entity('light');
light.addComponent('light', {
type: 'directional',
castShadows: true
});
app.root.addChild(light);

app.on('update', () => {
const estimate = app.xr.lightEstimation;
if (!app.xr.active || !estimate.available) return;

light.setRotation(estimate.rotation);
light.light.color = estimate.color;
light.light.intensity = estimate.intensity;
});

The rotation doesn't include the rig's rotation, so for a rig that is turned, make the light a child of the rig and set its local rotation instead.

Ambient Light​

Light also comes from everywhere else: the sky, the walls and the floor. sphericalHarmonics describes this ambient light, as it reaches a surface facing each direction, with nine spherical harmonic coefficients, each one red, green and blue. It's a Float32Array of 27 numbers, in the order WebXR gives them, or null until light estimation is available.

The engine doesn't light the scene with it for you. A StandardMaterial whose ambientSH property is set to such an array lights its surfaces with it, in place of the scene's ambient light and environment lighting. The array is updated in place, so set it once:

app.xr.lightEstimation.on('available', () => {
material.ambientSH = app.xr.lightEstimation.sphericalHarmonics;
material.update();
});

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

WebXR can also estimate the room's reflections, as a cube map. The engine doesn't support it yet.

See Also​