Migrate SceneView 2.x and 3.x to 4.x¶
The main change is ownership: Compose declares and removes nodes. Replace
Scene(childNodes = ...) with SceneView { ModelNode(...) } and replace
ARScene with ARSceneView or PlacementScene.
Update the dependency and toolchain¶
Choose the 3D dependency, or the AR dependency which includes 3D:
implementation("io.github.sceneview:sceneview:4.76.0")
// For AR, use this instead:
// implementation("io.github.sceneview:arsceneview:4.76.0")
Use minSdk 24+, compileSdk 37, AGP 9.4.0+, Kotlin 2.3+ and JVM target 21. Follow the setup guide, including the AR manifest when needed.
Replace the node list with the content block¶
The old shape was val nodes = rememberNodes { ... } followed by
Scene(childNodes = nodes). Those compatibility APIs still exist, deprecated,
in 4.76.0. The replacement is a complete composable:
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import io.github.sceneview.SceneView
import io.github.sceneview.rememberModelInstance
@Composable
fun ModelViewer4x(modelPath: String = "models/model.glb") {
SceneView(modifier = Modifier.fillMaxSize()) {
rememberModelInstance(modelLoader, modelPath)?.let { instance ->
ModelNode(modelInstance = instance, scaleToUnits = 1f, autoAnimate = true)
}
}
}
Place the GLB under app/src/main/assets/. modelLoader is supplied by the
scene scope. ModelNode here is a scope member, so remove the imperative
import io.github.sceneview.node.ModelNode. Orbit controls and neutral lighting
are defaults. scaleToUnits = 1f fits the longest dimension to one metre; omit
it when preserving authored size matters.
rememberModelInstance replaces synchronous createModelInstance in Compose;
always handle its nullable result. For a visible error state, use
rememberModelLoadState as in the placement recipe.
Filament resource creation must stay on the main thread.
Replace AR child mutation with nested composables¶
Code that appended an imperative AnchorNode to childNodes and called
addChildNode can use PlacementScene. It supplies a reticle and creates the
ARCore anchor when the user taps:
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import io.github.sceneview.ar.PlacementScene
import io.github.sceneview.rememberModelInstance
@Composable
fun TapToPlace4x() {
PlacementScene(
modifier = Modifier.fillMaxSize(),
coaching = true,
groundShadows = true,
onPlaced = { anchor ->
AnchorNode(anchor = anchor) {
rememberModelInstance(modelLoader, "models/model.glb")?.let { instance ->
ModelNode(modelInstance = instance, scaleToUnits = null, isEditable = true)
}
}
},
)
}
AnchorNode is an AR scope member; it takes a real ARCore Anchor and owns
its lifetime. Load a separate model instance for each placed node. For custom
hit tests, keep anchors in Compose state and declare an AnchorNode per item
with key(anchor), as in the measurement recipe.
Removing an item removes its node and detaches the anchor.
| Existing code | 4.76.0 replacement |
|---|---|
Scene(childNodes = nodes) |
SceneView { ... } |
ARScene(childNodes = nodes) |
ARSceneView { ... } or PlacementScene |
rememberNodes() and nodes += node |
State describing content, with nodes declared in the scene block |
modelLoader.createModelInstance(...) in composition |
rememberModelInstance(modelLoader, path)?.let { ... } |
Imperative ModelNode / AnchorNode imports |
Scope members inside the scene block |
anchorNode.addChildNode(modelNode) |
AnchorNode(anchor = anchor) { ModelNode(...) } |
nodes -= node; node.destroy() |
Remove the corresponding state item |
ARSceneView(view = rememberView(engine)) |
Omit view, or pass rememberARView(engine) from io.github.sceneview |
The last row matters visually: AR's default view preserves camera colours; the 3D view's tone mapping and effects can oversaturate the camera feed.
See the full upgrade guide for additional mappings. Sources: Compat2x.kt, ARCompat2x.kt and PlacementScene.kt. Verified by source inspection at 4.76.0; these snippets were not compiled or run on a device.