Native Android AR product placement¶
Keep the camera, placement and Compose controls in your activity with
AutoPlacementScene. This example uses no external Scene Viewer intent. It
automatically places the model on a detected horizontal surface, then supports
drag, twist and pinch. Coaching, contact shadows, permission recovery and ARCore
availability UI belong to the embedded scene.
Add implementation("io.github.sceneview:arsceneview:4.76.0") and follow the
Android setup for the toolchain and AR manifest. Put a
self-contained GLB in app/src/main/assets/models/, or supply an HTTPS URL.
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.padding
import androidx.compose.material3.Button
import androidx.compose.material3.CircularProgressIndicator
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.key
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.unit.dp
import io.github.sceneview.ModelLoadState
import io.github.sceneview.ar.AutoPlacementModel
import io.github.sceneview.ar.AutoPlacementScene
import io.github.sceneview.ar.PlacementSize
import io.github.sceneview.ar.PlacementSurface
import io.github.sceneview.ar.rememberAutoPlacementState
import io.github.sceneview.rememberEngine
import io.github.sceneview.rememberModelLoadState
import io.github.sceneview.rememberModelLoader
@Composable
fun ProductPlacementScreen(modelLocation: String, onConfirm: () -> Unit) {
val engine = rememberEngine()
val modelLoader = rememberModelLoader(engine)
val loadState = rememberModelLoadState(modelLoader, modelLocation)
val instance = (loadState as? ModelLoadState.Ready)?.instance
// A different model starts a new placement rather than reusing the previous pose.
key(modelLocation) {
val state = rememberAutoPlacementState()
Box(Modifier.fillMaxSize()) {
AutoPlacementScene(
assetReady = instance != null,
modifier = Modifier.fillMaxSize(),
state = state,
surface = PlacementSurface.SURFACE,
engine = engine,
modelLoader = modelLoader,
groundShadows = true,
coaching = true,
) { placement ->
instance?.let {
AutoPlacementModel(
placement = placement,
state = state,
modelInstance = it,
size = PlacementSize.RealWorld,
)
}
}
when (loadState) {
is ModelLoadState.Loading ->
CircularProgressIndicator(Modifier.align(Alignment.Center))
is ModelLoadState.Failed ->
Text("Could not load the model", Modifier.align(Alignment.Center))
is ModelLoadState.Ready -> if (state.hasPlacement) {
Button(
onClick = onConfirm,
modifier = Modifier.align(Alignment.BottomCenter).padding(24.dp),
) {
Text(state.placedSize?.let {
"Confirm · %.0f × %.0f cm".format(it.x * 100f, it.z * 100f)
} ?: "Confirm placement")
}
}
}
}
}
}
PlacementSize.RealWorld preserves glTF's authored metres at 100%. Verify the
asset dimensions before using it for a size decision. Pinching still resizes the
model; to lock real size, set state.allowsScaling = false in a Compose
SideEffect. state.placedSize reports the current dimensions after scaling.
Use PlacementSurface.WALL for wall-mounted content. Placement support and
tracking quality depend on the device and its environment.
rememberModelLoadState distinguishes downloading from failure and owns the model
lifetime. Inspect ModelLoadState.Failed.error in your app's error handling.
Do not create Filament resources on a background dispatcher. For explicit
tap-to-place instead, see the migration recipe.
Source: AutoPlacementScene.kt, PlacementSize.kt and ModelLoadState.kt. Verified by source inspection at 4.76.0; this snippet was not compiled or run on a device.