SpatialEnvironment


public final class SpatialEnvironment


The SpatialEnvironment is used to manage the XR background and passthrough. There is a single instance of this class managed by each Session and it is accessible through Session.scene.

The SpatialEnvironment is a composite of a stand-alone skybox, and of a glTF-specified geometry. A single skybox and a single glTF can be set at the same time. Applications are encouraged to supply glTFs for ground and horizon visibility.

The XR background can be set to display one of three configurations:

  1. A combination of a skybox and glTF geometry.

  2. A Passthrough surface, where the XR background is a live feed from the device's outward facing cameras. At full opacity, this surface completely occludes the skybox and geometry.

  3. A mixed configuration where the passthrough surface is not at full opacity nor is it at zero opacity. The passthrough surface becomes semi-transparent and alpha blends with the skybox and geometry behind it.

Note that methods in this class do not necessarily take effect immediately. Rather, they set a preference that will be applied when the device enters a state where the XR background can be changed.

Summary

Nested types

Represents the preferred spatial environment for the application.

Constants

static final float

Passed into preferredPassthroughOpacity to clear the application's passthrough opacity preference and to let the system manage passthrough opacity.

Public methods

final void

Notifies an application when the user visible passthrough state changes, such as when the application enters or exits passthrough or when the passthrough opacity changes.

final void

Notifies an application when the user visible passthrough state changes, such as when the application enters or exits passthrough or when the passthrough opacity changes.

final void

Notifies an application whether or not the preferred spatial environment for the application is active.

final void

Notifies an application whether or not the preferred spatial environment for the application is active.

final float

Gets the current passthrough opacity value visible to the user.

final float

The application's preferred passthrough opacity.

final SpatialEnvironment.SpatialEnvironmentPreference

The preferred spatial environment for the application.

final boolean

Checks if the application's preferred spatial environment set through preferredSpatialEnvironment is active.

final void

Remove a listener previously added by addOnPassthroughOpacityChangedListener.

final void

Remove a listener previously added by addOnSpatialEnvironmentChangedListener.

final void
setPreferredPassthroughOpacity(float preferredPassthroughOpacity)

The application's preferred passthrough opacity.

final void

The preferred spatial environment for the application.

Constants

NO_PASSTHROUGH_OPACITY_PREFERENCE

public static final float NO_PASSTHROUGH_OPACITY_PREFERENCE

Passed into preferredPassthroughOpacity to clear the application's passthrough opacity preference and to let the system manage passthrough opacity.

Public methods

addOnPassthroughOpacityChangedListener

Added in 1.0.0-alpha05
public final void addOnPassthroughOpacityChangedListener(
    @NonNull Consumer<@NonNull Float> listener
)

Notifies an application when the user visible passthrough state changes, such as when the application enters or exits passthrough or when the passthrough opacity changes.

This listener will be called on the Application's main thread.

Parameters
@NonNull Consumer<@NonNull Float> listener

The Consumer to be added to listen for passthrough opacity changes.

addOnPassthroughOpacityChangedListener

Added in 1.0.0-alpha05
public final void addOnPassthroughOpacityChangedListener(
    @NonNull Executor executor,
    @NonNull Consumer<@NonNull Float> listener
)

Notifies an application when the user visible passthrough state changes, such as when the application enters or exits passthrough or when the passthrough opacity changes.

This listener will be invoked on the given Executor.

Parameters
@NonNull Executor executor

The Executor to invoke the listener on.

@NonNull Consumer<@NonNull Float> listener

The Consumer to be added to listen for passthrough opacity changes.

addOnSpatialEnvironmentChangedListener

Added in 1.0.0-alpha05
public final void addOnSpatialEnvironmentChangedListener(
    @NonNull Consumer<@NonNull Boolean> listener
)

Notifies an application whether or not the preferred spatial environment for the application is active.

The environment will try to transition to the application environment when a non-null preference is set through preferredSpatialEnvironment and the application has the SpatialCapabilities.SPATIAL_CAPABILITY_APP_ENVIRONMENT capability. The environment preferences will otherwise not be active.

The listener consumes a boolean value that is true if the environment preference is active when the listener is notified.

This listener will be invoked on the Application's main thread.

Parameters
@NonNull Consumer<@NonNull Boolean> listener

The Consumer to be added to listen for spatial environment changes.

addOnSpatialEnvironmentChangedListener

Added in 1.0.0-alpha05
public final void addOnSpatialEnvironmentChangedListener(
    @NonNull Executor executor,
    @NonNull Consumer<@NonNull Boolean> listener
)

Notifies an application whether or not the preferred spatial environment for the application is active.

The environment will try to transition to the application environment when a non-null preference is set through preferredSpatialEnvironment and the application has the SpatialCapabilities.SPATIAL_CAPABILITY_APP_ENVIRONMENT capability. The environment preferences will otherwise not be active.

The listener consumes a boolean value that is true if the environment preference is active when the listener is notified.

This listener will be invoked on the given Executor.

Parameters
@NonNull Executor executor

The Executor to invoke the listener on.

@NonNull Consumer<@NonNull Boolean> listener

The Consumer to be added to listen for spatial environment changes.

getCurrentPassthroughOpacity

Added in 1.0.0-alpha05
public final float getCurrentPassthroughOpacity()

Gets the current passthrough opacity value visible to the user.

Unlike the application's opacity preference returned by preferredPassthroughOpacity, this value can be overwritten by the system, and is not directly under the application's control.

Returns
float

The current passthrough opacity value between 0.0f and 1.0f. A value of 0.0f means no passthrough is shown, and a value of 1.0f means the passthrough completely obscures the spatial environment geometry and skybox.

getPreferredPassthroughOpacity

Added in 1.0.0-alpha05
public final float getPreferredPassthroughOpacity()

The application's preferred passthrough opacity.

Upon construction, the default value is NO_PASSTHROUGH_OPACITY_PREFERENCE, which means "no application preference". The application's preferred passthrough opacity can be set between 0.0f and 1.0f.

Setting the application preference does not guarantee that the value will be immediately applied and visible to the user. The actual passthrough opacity value is controlled by the system in response to a combination of this preference and user actions outside the application. Generally, this preference is honored when the application has the SpatialCapabilities.SPATIAL_CAPABILITY_PASSTHROUGH_CONTROL capability.

The value should be between 0.0f (passthrough disabled) and 1.0f (passthrough fully obscures the spatial environment). Values within 0.01f of 0.0 or 1.0 are snapped to those values. Values outside 0.0f, 1.0f are clamped. Other values result in semi-transparent passthrough that is alpha blended with the spatial environment. Setting this property to NO_PASSTHROUGH_OPACITY_PREFERENCE clears the application's preference, allowing the system to manage passthrough opacity.

The actual value visible to the user can be observed by calling currentPassthroughOpacity or by registering a listener with addOnPassthroughOpacityChangedListener.

getPreferredSpatialEnvironment

Added in 1.0.0-alpha05
public final SpatialEnvironment.SpatialEnvironmentPreference getPreferredSpatialEnvironment()

The preferred spatial environment for the application.

If no preference has ever been set by the application, this will be null.

Setting this property only sets the preference and does not cause an immediate change unless isPreferredSpatialEnvironmentActive is already true. Once the device enters a state where the XR background can be changed and the SpatialCapabilities.SPATIAL_CAPABILITY_APP_ENVIRONMENT capability is available, the preferred spatial environment for the application will be automatically displayed.

Setting the preference to null will disable the preferred spatial environment for the application, meaning the default system environment will be displayed instead.

If the given SpatialEnvironmentPreference is not null, but all of its properties are null, then the spatial environment will consist of a black skybox and no geometry.

See isPreferredSpatialEnvironmentActive or the addOnSpatialEnvironmentChangedListener listeners to know when this preference becomes active.

isPreferredSpatialEnvironmentActive

Added in 1.0.0-alpha05
public final boolean isPreferredSpatialEnvironmentActive()

Checks if the application's preferred spatial environment set through preferredSpatialEnvironment is active.

Spatial environment preference set through preferredSpatialEnvironment are shown when this is true, but passthrough or other objects in the scene could partially or totally occlude them. When this is false, the default system environment will be active instead.

Returns
boolean

True if the environment set by preferredSpatialEnvironment is active.

removeOnPassthroughOpacityChangedListener

Added in 1.0.0-alpha05
public final void removeOnPassthroughOpacityChangedListener(
    @NonNull Consumer<@NonNull Float> listener
)

Remove a listener previously added by addOnPassthroughOpacityChangedListener.

Parameters
@NonNull Consumer<@NonNull Float> listener

The previously-added Consumer listener to be removed.

removeOnSpatialEnvironmentChangedListener

Added in 1.0.0-alpha05
public final void removeOnSpatialEnvironmentChangedListener(
    @NonNull Consumer<@NonNull Boolean> listener
)

Remove a listener previously added by addOnSpatialEnvironmentChangedListener.

Parameters
@NonNull Consumer<@NonNull Boolean> listener

The previously-added Consumer listener to be removed.

setPreferredPassthroughOpacity

Added in 1.0.0-alpha05
public final void setPreferredPassthroughOpacity(float preferredPassthroughOpacity)

The application's preferred passthrough opacity.

Upon construction, the default value is NO_PASSTHROUGH_OPACITY_PREFERENCE, which means "no application preference". The application's preferred passthrough opacity can be set between 0.0f and 1.0f.

Setting the application preference does not guarantee that the value will be immediately applied and visible to the user. The actual passthrough opacity value is controlled by the system in response to a combination of this preference and user actions outside the application. Generally, this preference is honored when the application has the SpatialCapabilities.SPATIAL_CAPABILITY_PASSTHROUGH_CONTROL capability.

The value should be between 0.0f (passthrough disabled) and 1.0f (passthrough fully obscures the spatial environment). Values within 0.01f of 0.0 or 1.0 are snapped to those values. Values outside 0.0f, 1.0f are clamped. Other values result in semi-transparent passthrough that is alpha blended with the spatial environment. Setting this property to NO_PASSTHROUGH_OPACITY_PREFERENCE clears the application's preference, allowing the system to manage passthrough opacity.

The actual value visible to the user can be observed by calling currentPassthroughOpacity or by registering a listener with addOnPassthroughOpacityChangedListener.

setPreferredSpatialEnvironment

Added in 1.0.0-alpha05
public final void setPreferredSpatialEnvironment(
    SpatialEnvironment.SpatialEnvironmentPreference preferredSpatialEnvironment
)

The preferred spatial environment for the application.

If no preference has ever been set by the application, this will be null.

Setting this property only sets the preference and does not cause an immediate change unless isPreferredSpatialEnvironmentActive is already true. Once the device enters a state where the XR background can be changed and the SpatialCapabilities.SPATIAL_CAPABILITY_APP_ENVIRONMENT capability is available, the preferred spatial environment for the application will be automatically displayed.

Setting the preference to null will disable the preferred spatial environment for the application, meaning the default system environment will be displayed instead.

If the given SpatialEnvironmentPreference is not null, but all of its properties are null, then the spatial environment will consist of a black skybox and no geometry.

See isPreferredSpatialEnvironmentActive or the addOnSpatialEnvironmentChangedListener listeners to know when this preference becomes active.