SceneCoreEntity

Functions summary

Unit
@Composable
@SubspaceComposable
<T : Entity> SceneCoreEntity(
    factory: () -> T,
    modifier: SubspaceModifier,
    update: (T) -> Unit,
    sizeAdapter: SceneCoreEntitySizeAdapter<T>?,
    content: @Composable @SubspaceComposable () -> Unit
)

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

Functions

SceneCoreEntity

@Composable
@SubspaceComposable
fun <T : Entity> SceneCoreEntity(
    factory: () -> T,
    modifier: SubspaceModifier = SubspaceModifier,
    update: (T) -> Unit = {},
    sizeAdapter: SceneCoreEntitySizeAdapter<T>? = null,
    content: @Composable @SubspaceComposable () -> Unit = {}
): Unit

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

Usage of this API requires the SceneCore dependency to be added. See https://developer.android.com/jetpack/androidx/releases/xr-scenecore

import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.platform.LocalDensity
import androidx.compose.ui.unit.dp
import androidx.xr.compose.platform.LocalSession
import androidx.xr.compose.subspace.SceneCoreEntity
import androidx.xr.runtime.math.FloatSize2d
import androidx.xr.scenecore.PanelEntity
import androidx.xr.scenecore.scene

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)
    },
)
Parameters
factory: () -> T

the factory method for creating the SceneCore Entity.

modifier: SubspaceModifier = SubspaceModifier

the SubspaceModifier that will be applied to this node.

update: (T) -> Unit = {}

a callback to be invoked on recomposition to apply any state changes to the Entity. This will track snapshot state reads and call update when they change.

sizeAdapter: SceneCoreEntitySizeAdapter<T>? = null

an adapter that allows compose to integrate its layout size changes with the rendered entity size. This adapter implementation will likely be different for every entity and some SceneCore entities may not require sizing at all (this may be null).

content: @Composable @SubspaceComposable () -> Unit = {}

the children of this Entity.

See also
SceneCoreEntitySizeAdapter

for more information on how compose sizes SceneCore entities.