SpatialGltfModelState

class SpatialGltfModelState : AutoCloseable


A state object that can be hoisted to observe and control a SpatialGltfModel.

A SpatialGltfModelState can be used to query loading and animation status, and to start or stop animations on the associated SpatialGltfModel.

To create and remember a SpatialGltfModelState, use rememberSpatialGltfModelState. If a SpatialGltfModelState instance is manually created using the constructor then the caller needs to call close to free its resources when it is no longer needed.

Summary

Public constructors

Public functions

open Unit

Public properties

List<SpatialGltfModelAnimation>

The animations defined in the glTF model.

List<GltfModelNode>

The subnodes defined in the glTF model.

SpatialGltfModelStatus

The current SpatialGltfModelStatus of the glTF model.

Public constructors

SpatialGltfModelState

Added in 1.0.0-alpha13
SpatialGltfModelState(source: SpatialGltfModelSource)
Parameters
source: SpatialGltfModelSource

The SpatialGltfModelSource that defines where to load the 3D model from.

Public functions

close

Added in 1.0.0-alpha13
open fun close(): Unit

Public properties

animations

Added in 1.0.0-alpha13
val animationsList<SpatialGltfModelAnimation>

The animations defined in the glTF model.

Each animation can be controlled individually and multiple animations may be playing at one time as long as those animations affect different parts of the model.

import androidx.compose.runtime.DisposableEffect
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.ui.unit.sp
import androidx.xr.compose.subspace.SpatialGltfModel
import androidx.xr.compose.subspace.SpatialGltfModelSource
import androidx.xr.compose.subspace.rememberSpatialGltfModelState

val modelState =
    rememberSpatialGltfModelState(
        source = SpatialGltfModelSource.fromPath(Paths.get("models", "Biped.gltf"))
    )
val animation = modelState.animations.find { it.name == "Walk" }

animation?.animationState?.let { state ->
    LaunchedEffect(state) {
        Log.i("SpatialGltfModelAnimationSample", "Animation State: $state")
    }
}

DisposableEffect(animation) {
    animation?.loop()
    onDispose { animation?.stop() }
}

SpatialGltfModel(state = modelState)

nodes

Added in 1.0.0-alpha13
val nodesList<GltfModelNode>

The subnodes defined in the glTF model.

These nodes allow access and manipulation of the pose and scale of the node which could be useful for manipulating the glTF model or positioning content relative to a particular node in the glTF model.

import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.shape.CornerSize
import androidx.compose.material3.Text
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.xr.compose.subspace.SpatialGltfModel
import androidx.xr.compose.subspace.SpatialGltfModelSource
import androidx.xr.compose.subspace.SpatialPanel
import androidx.xr.compose.subspace.layout.SpatialRoundedCornerShape
import androidx.xr.compose.subspace.layout.SubspaceModifier
import androidx.xr.compose.subspace.layout.height
import androidx.xr.compose.subspace.layout.offset
import androidx.xr.compose.subspace.layout.width
import androidx.xr.compose.subspace.rememberSpatialGltfModelState
import androidx.xr.compose.unit.Meter

val modelState =
    rememberSpatialGltfModelState(
        source = SpatialGltfModelSource.fromPath(Paths.get("models", "Dragon_Evolved.gltf"))
    )
SpatialGltfModel(state = modelState) {
    val headNode = modelState.nodes.find { it.name == "head" }
    if (headNode != null) {
        val offsetX = Meter(headNode.modelPose.translation.x).toDp()
        val offsetY = Meter(headNode.modelPose.translation.y).toDp()
        val offsetZ = Meter(headNode.modelPose.translation.z).toDp()
        SpatialPanel(
            shape = SpatialRoundedCornerShape(CornerSize(25)),
            modifier =
                SubspaceModifier.width(700.dp).height(700.dp).offset(offsetX, offsetY, offsetZ),
        ) {
            Box(
                Modifier.fillMaxSize().background(Color.Blue.copy(alpha = 0.8f)),
                contentAlignment = Alignment.Center,
            ) {
                Text(text = "Head", color = Color.White, fontSize = 30.sp)
            }
        }
    }
}

status

Added in 1.0.0-alpha13
val statusSpatialGltfModelStatus

The current SpatialGltfModelStatus of the glTF model.

It will initially be SpatialGltfModelStatus.Loading until the model is loaded and ready to be displayed at which point it should be SpatialGltfModelStatus.Loaded. However, if the model fails to load for any reason, the status will be a SpatialGltfModelStatus.Failed with the exception that was thrown.