rememberMaterial

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

Asynchronously loads a Filament Material 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. Pass onError to react when load throws (missing file, network error).

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
rememberMaterial { Res.readBytes("…") }

// Outside a scene — engine hoisted, can be shared across scenes
val engine = rememberFilamentEngine()
val mat = rememberMaterial(engine) { Res.readBytes("…") }
val scene = rememberFilamentScene(engine = engine) { ... }

Parameters

engine

The Filament engine to allocate the material on. Defaults to the engine in the current composition scope, which only exists inside rememberFilamentScene { }.

key

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

onError

Invoked once if load throws. The material stays null.

load

Suspend function that produces the raw .filamat bytes.