SpatialGltfModelState

@ExperimentalSpatialGltfModelApi
class SpatialGltfModelState : AutoCloseable


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

A SpatialGltfModelState can be used to query loading status 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<GltfModelNode>

The subnodes defined in the glTF model.

SpatialGltfModelStatus

The current SpatialGltfModelStatus of the glTF model.

Public constructors

SpatialGltfModelState

Added in 1.0.0-alpha17
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-alpha17
open fun close(): Unit

Public properties

nodes

Added in 1.0.0-alpha17
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.platform.LocalDensity
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.xr.compose.platform.LocalSession
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.scenecore.scene

val session = checkNotNull(LocalSession.current)
val pixelDensity = session.scene.virtualPixelDensity
val modelState =
    rememberSpatialGltfModelState(
        source = SpatialGltfModelSource.fromPath(Paths.get("models", "Dragon_Evolved.gltf"))
    )
SpatialGltfModel(state = modelState) {
    val headNode = modelState.nodes.find { it.name == "head" }
    val density = LocalDensity.current
    if (headNode != null) {
        val offsetX =
            with(density) {
                pixelDensity.convertMetersToPixels(headNode.modelPose.translation.x).toDp()
            }
        val offsetY =
            with(density) {
                pixelDensity.convertMetersToPixels(headNode.modelPose.translation.y).toDp()
            }
        val offsetZ =
            with(density) {
                pixelDensity.convertMetersToPixels(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-alpha17
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.