SpatialEnvironment


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

const Float

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

Public functions

Unit

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.

Unit
addOnPassthroughOpacityChangedListener(
    executor: Executor,
    listener: Consumer<Float>
)

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.

Unit

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

Unit
addOnSpatialEnvironmentChangedListener(
    executor: Executor,
    listener: Consumer<Boolean>
)

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

Unit

Remove a listener previously added by addOnPassthroughOpacityChangedListener.

Unit

Remove a listener previously added by addOnSpatialEnvironmentChangedListener.

Public properties

Float

Gets the current passthrough opacity value visible to the user.

Boolean

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

Float

The application's preferred passthrough opacity.

SpatialEnvironment.SpatialEnvironmentPreference?

The preferred spatial environment for the application.

Constants

NO_PASSTHROUGH_OPACITY_PREFERENCE

const val NO_PASSTHROUGH_OPACITY_PREFERENCEFloat

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

Public functions

addOnPassthroughOpacityChangedListener

Added in 1.0.0-alpha05
fun addOnPassthroughOpacityChangedListener(listener: Consumer<Float>): Unit

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
listener: Consumer<Float>

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

addOnPassthroughOpacityChangedListener

Added in 1.0.0-alpha05
fun addOnPassthroughOpacityChangedListener(
    executor: Executor,
    listener: Consumer<Float>
): Unit

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
executor: Executor

The Executor to invoke the listener on.

listener: Consumer<Float>

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

addOnSpatialEnvironmentChangedListener

Added in 1.0.0-alpha05
fun addOnSpatialEnvironmentChangedListener(listener: Consumer<Boolean>): Unit

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
listener: Consumer<Boolean>

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

addOnSpatialEnvironmentChangedListener

Added in 1.0.0-alpha05
fun addOnSpatialEnvironmentChangedListener(
    executor: Executor,
    listener: Consumer<Boolean>
): Unit

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
executor: Executor

The Executor to invoke the listener on.

listener: Consumer<Boolean>

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

removeOnPassthroughOpacityChangedListener

Added in 1.0.0-alpha05
fun removeOnPassthroughOpacityChangedListener(listener: Consumer<Float>): Unit

Remove a listener previously added by addOnPassthroughOpacityChangedListener.

Parameters
listener: Consumer<Float>

The previously-added Consumer listener to be removed.

removeOnSpatialEnvironmentChangedListener

Added in 1.0.0-alpha05
fun removeOnSpatialEnvironmentChangedListener(listener: Consumer<Boolean>): Unit

Remove a listener previously added by addOnSpatialEnvironmentChangedListener.

Parameters
listener: Consumer<Boolean>

The previously-added Consumer listener to be removed.

Public properties

currentPassthroughOpacity

Added in 1.0.0-alpha05
val currentPassthroughOpacityFloat

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.

isPreferredSpatialEnvironmentActive

Added in 1.0.0-alpha05
val isPreferredSpatialEnvironmentActiveBoolean

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.

preferredPassthroughOpacity

Added in 1.0.0-alpha05
var preferredPassthroughOpacityFloat

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.

preferredSpatialEnvironment

Added in 1.0.0-alpha05
var preferredSpatialEnvironmentSpatialEnvironment.SpatialEnvironmentPreference?

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.