Spatial capabilities can change as users interact with your app or the system, or can even be changed by your app itself—for example, moving into Home Space or Full Space. To avoid issues, your app needs to check for spatial capabilities to determine which APIs are supported in the current environment.
Check for spatial capabilities using Jetpack Compose for XR
Jetpack Compose for XR creates a Composition Local for checking spatial capabilities. Use this to check whether spatial UI, spatial audio, environments, passthrough, or 3D content is enabled.
You can use LocalSpatialCapabilities.current
to check if
the following spatial capabilities are currently available:
isSpatialUiEnabled
: Indicates whether the application may create spatial UI elements (for example,SpatialPanel
).isContent3dEnabled
: Indicates whether the application may create 3D objects.isAppEnvironmentEnabled
: Indicates whether the application may set the environment.isPassthroughControlEnabled
: Indicates whether the application may control the passthrough state.isSpatialAudioEnabled
: Indicates whether the application may use spatial audio.
The following example shows how to check if spatial UI is enabled.
if (LocalSpatialCapabilities.current.isSpatialUiEnabled) {
Subspace {
SpatialPanel(
modifier = SubspaceModifier
.width(1488.dp)
.fillMaxHeight()
) {
AppContent()
}
}
} else {
AppContent()
}
Check for spatial capabilities using SceneCore
When using the SceneCore library, you'll have to create a
session. Once the session is created, call
getSpatialCapabilities
on the session to query which spatial
capabilities are currently available.
SPATIAL_CAPABILITY_3D_CONTENT
: The activity can create 3D contents.SPATIAL_CAPABILITY_APP_ENVIRONMENT
: The activity can set its own environment.SPATIAL_CAPABILITY_EMBED_ACTIVITY
: The activity can spatially embed another activity.SPATIAL_CAPABILITY_PASSTHROUGH_CONTROL
: The activity can enable or disable passthrough.SPATIAL_CAPABILITY_SPATIAL_AUDIO
: The activity can use spatial audio.SPATIAL_CAPABILITY_UI
: The activity can spatialize itself e.g. adding a spatial panel.
You can also choose to subscribe to a callback, addSpatialCapabilitiesChangedListener
that notifies you when spatial
capabilities have changed.
val xrSession = Session.create(this)
// Example 1: check if enabling passthrough mode is allowed
if (xrSession.getSpatialCapabilities().hasCapability(
SpatialCapabilities.SPATIAL_CAPABILITY_PASSTHROUGH_CONTROL)) {
xrSession.spatialEnvironment.setPassthroughOpacityPreference(0f)
}
// Example 2: Multiple capability flags can be checked simultaneously:
if (xrSession.getSpatialCapabilities().hasCapability(
SpatialCapabilities.SPATIAL_CAPABILITY_PASSTHROUGH_CONTROL and
SpatialCapabilities.SPATIAL_CAPABILITY_3D_CONTENT)) {
// ...
}
// Example 3: Create a spatialized panel if/when spatialization UI becomes available
xrSession.addSpatialCapabilitiesChangedListener((capabilities) -> {
if (capabilities.hasCapability(SpatialCapabilities.SPATIAL_CAPABILITY_UI)){
Subspace{
SpatialPanel(...)
}
}
});
See also
- Create a session
- Transition between HSM and FSM
- Add spatial environments to your app
- Add 3D models to your app