-
Notifications
You must be signed in to change notification settings - Fork 363
Update XR library snippets for the May 19 release #918
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
4a20739
574d03c
198615c
ffd69e6
e2be477
1abd22f
dd82bc4
276a8cd
5b3e1b8
b14b45d
5d75aaa
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -16,7 +16,9 @@ | |
|
|
||
| package com.example.xr.compose | ||
|
|
||
| import android.util.Log | ||
| import androidx.compose.runtime.Composable | ||
| import androidx.compose.runtime.DisposableEffect | ||
| import androidx.compose.runtime.LaunchedEffect | ||
| import androidx.compose.runtime.getValue | ||
| import androidx.compose.runtime.mutableStateOf | ||
|
|
@@ -40,6 +42,7 @@ import kotlin.io.path.Path | |
| fun SpatialGltfModelExample(){ | ||
| val xrSession = checkNotNull(LocalSession.current) | ||
| val degrees = 1f | ||
| val removeMaterial = false | ||
|
|
||
| // [START androidxr_compose_SpatialGltfModelState] | ||
| val modelState = rememberSpatialGltfModelState( | ||
|
|
@@ -49,64 +52,101 @@ fun SpatialGltfModelExample(){ | |
| ) | ||
| // [END androidxr_compose_SpatialGltfModelState] | ||
|
|
||
| // [START androidxr_compose_SpatialGltfModelMaterial] | ||
| // [START androidxr_compose_SpatialGltfModelNode] | ||
| // Retrieve the list of nodes (individual components/meshes) defined within the glTF model. | ||
| val entityNodes = modelState.nodes | ||
|
|
||
| // Find a specific node by name to apply modifications, such as material overrides. | ||
| val node = entityNodes.find { it.name == "node_name" } | ||
| // [END androidxr_compose_SpatialGltfModelNode] | ||
|
|
||
| // [START androidxr_compose_SpatialGltfModelIntrospection] | ||
| LaunchedEffect(node, degrees) { | ||
| val rotation = Quaternion.fromEulerAngles(degrees, 0f, degrees) | ||
| node?.let { | ||
| it.localPose = Pose(it.localPose.translation, rotation) | ||
| } | ||
| } | ||
| // [END androidxr_compose_SpatialGltfModelIntrospection] | ||
|
|
||
| // [START androidxr_compose_SpatialGltfModelMaterial] | ||
| // Maintain a reference to the custom material to avoid re-creating it on every recomposition. | ||
| var pbrMaterial by remember { mutableStateOf<KhronosPbrMaterial?>(null) } | ||
|
|
||
| // Create and apply the custom material once the session is ready and the target node is available. | ||
| LaunchedEffect(node) { | ||
| val material = pbrMaterial ?: KhronosPbrMaterial.create( | ||
| val material = KhronosPbrMaterial.create( | ||
| session = xrSession, | ||
| alphaMode = AlphaMode.OPAQUE | ||
| ).also { | ||
| pbrMaterial = it | ||
| // Apply a base color factor (RGBA) to change the color of the model. | ||
| it.setBaseColorFactor( | ||
| Vector4( | ||
| x = 0.5f, | ||
| y = 0.0f, | ||
| z = 0.5f, | ||
| w = 1.0f | ||
| ) | ||
| ) | ||
| } | ||
| // [END androidxr_compose_SpatialGltfModelMaterial] | ||
|
|
||
| // [START androidxr_compose_SpatialGltfModelMaterialOverride] | ||
| node?.setMaterialOverride( | ||
| material = material | ||
| ) | ||
| // [END androidxr_compose_SpatialGltfModelMaterialOverride] | ||
|
|
||
| // [START androidxr_compose_SpatialGltfModelMaterialClear] | ||
| if (removeMaterial) { | ||
| node?.clearMaterialOverride() | ||
| } | ||
| // [END androidxr_compose_SpatialGltfModelMaterialClear] | ||
| } | ||
|
|
||
| // [START androidxr_compose_SpatialGltfModelTexture] | ||
| LaunchedEffect(node) { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This |
||
| val material = KhronosPbrMaterial.create( | ||
| session = xrSession, | ||
| alphaMode = AlphaMode.OPAQUE | ||
| ).also { | ||
| pbrMaterial = it | ||
|
|
||
| // Load a texture | ||
| val texture = Texture.create( | ||
| session = xrSession, | ||
| path = Path("textures/texture_name.png") | ||
| ) | ||
|
|
||
| // Configure occlusion to define how the material surface handles ambient lighting. | ||
| // Set the texture and configure occlusion to define how the material surface handles ambient lighting. | ||
| it.setOcclusionTexture( | ||
| texture = texture, | ||
| strength = 1.0f | ||
| ) | ||
|
|
||
| // Apply a base color factor (RGBA) to tint the model. | ||
| it.setBaseColorFactor( | ||
| Vector4( | ||
| x = 0.5f, | ||
| y = 0.0f, | ||
| z = 0.5f, | ||
| w = 1.0f | ||
| ) | ||
| ) | ||
| } | ||
|
|
||
| // Apply the custom PBR material to the specific node, overriding its original glTF material. | ||
| node?.setMaterialOverride( | ||
| material = material | ||
| ) | ||
| } | ||
| // [END androidxr_compose_SpatialGltfModelMaterial] | ||
| // [END androidxr_compose_SpatialGltfModelTexture] | ||
|
|
||
| // [START androidxr_compose_SpatialGltfModelIntrospection] | ||
| val arrows = modelState.nodes.find { | ||
| it.name == "Arrows" | ||
| // [START androidxr_compose_SpatialGltfModelAnimation] | ||
| val animation = modelState.animations.find { it.name == "Walk" } | ||
|
|
||
| animation?.animationState?.let { state -> | ||
| LaunchedEffect(state) { | ||
| Log.i("SpatialGltfModelAnimationSample", "Animation State: $state") | ||
| } | ||
| } | ||
|
|
||
| LaunchedEffect(arrows, degrees) { | ||
| val rotation = Quaternion.fromEulerAngles(degrees, 0f, degrees) | ||
| arrows?.localPose = | ||
| Pose(arrows.localPose.translation, rotation) | ||
| DisposableEffect(animation) { | ||
| animation?.loop() | ||
| onDispose { | ||
| animation?.stop() | ||
| } | ||
| } | ||
| // [END androidxr_compose_SpatialGltfModelIntrospection] | ||
| // [END androidxr_compose_SpatialGltfModelAnimation] | ||
|
|
||
| // [START androidxr_compose_SpatialGltfModelLoad] | ||
| SpatialGltfModel(state = modelState, modifier = SubspaceModifier) | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The material is being re-created every time this
LaunchedEffectruns, bypassing thepbrMaterialcache defined at line 78. This is less efficient than the previous implementation. Consider restoring the caching logic.