rememberGltfAsset

fun rememberGltfAsset(engine: Engine = LocalFilamentEngine.current, key: Any = Unit, onError: (Throwable) -> Unit? = null, load: suspend () -> ByteArray): GltfAsset?

Asynchronously loads a glTF/glb asset and keeps it alive as long as the calling composable is in the composition.

Returns null while loading and on failure — it never throws inside composition, so a bad model can't crash the app. Pass onError to react to failures (show a placeholder, log, retry). Both failure modes are reported: the load lambda throwing (missing file, network error) and the bytes failing to parse as glb/glTF.

Can be called either inside rememberFilamentScene { } (engine is picked up from LocalFilamentEngine) or outside it by hoisting the engine via rememberFilamentEngine:

// Inside rememberFilamentScene — engine implicit
GltfInstance(asset = rememberGltfAsset { Res.readBytes("Duck.glb") }, ...)

// Outside a scene — engine hoisted, asset can outlive any single scene
val engine = rememberFilamentEngine()
val duck = rememberGltfAsset(engine) { Res.readBytes("Duck.glb") }
val scene = rememberFilamentScene(engine = engine) { GltfInstance(asset = duck, ...) }

// Distinguishing loading from failure: null + a captured error
var error by remember { mutableStateOf<Throwable?>(null) }
val model = rememberGltfAsset(onError = { error = it }) { loadFromNetwork() }
when {
error != null -> ErrorPlaceholder() // failed
model == null -> LoadingSpinner() // still loading
else -> GltfInstance(asset = model)
}

Each GltfInstance created from the returned asset lives until the asset is destroyed (when this call leaves composition) — see GltfAsset.

Parameters

engine

The Filament engine that owns the asset's GPU resources. Defaults to the engine in the current composition scope.

key

Reloads the asset when this value changes. Defaults to Unit for static assets.

onError

Invoked once if load throws or the bytes don't parse. The asset stays null.

load

Suspend function that produces the raw glb/glTF bytes.