Start native apps faster with the Composables CLI ->
Composable Function

SceneCoreEntity

A composable that attaches to a SceneCore entity and allow compose to size, position, reparent, add children, and apply modifiers to the entity.

SceneCoreEntitySample

@Composable
@SubspaceComposable
public fun SceneCoreEntitySample() {
    val session = checkNotNull(LocalSession.current)
    val context = LocalContext.current
    val view = remember(context) { View(context) }
    val density = LocalDensity.current
    val virtualPixelDensity = session.scene.virtualPixelDensity
    // A state variable to dynamically control the size of the panel in DP
    var panelSizeDp by remember { mutableStateOf(400.dp) }
    SceneCoreEntity(
        factory = {
            // Convert the DP size to meters using the virtual pixel density, PanelEntity also has
            // APIs that use pixels directly, but a lot of other SceneCore APIs do not have this.
            val sizeInPx = with(density) { panelSizeDp.toPx() }
            val sizeInMeters = virtualPixelDensity.convertPixelsToMeters(sizeInPx)
            PanelEntity.create(
                session = session,
                view = view,
                dimensions = FloatSize2d(sizeInMeters, sizeInMeters),
                name = "SamplePanel",
            )
        },
        update = { entity ->
            // Update the entity size when state changes
            val sizeInPx = with(density) { panelSizeDp.toPx() }
            val sizeInMeters = virtualPixelDensity.convertPixelsToMeters(sizeInPx)
            entity.size = FloatSize2d(sizeInMeters, sizeInMeters)
        },
    )
}

Last updated: