-
Notifications
You must be signed in to change notification settings - Fork 369
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
Changes from 13 commits
e5b17bf
6e6ee67
74b3d8c
6fbc96d
fc3e1e5
60757f9
cadf349
be6b0b2
d5ca3ff
d656c9c
46622aa
b4eb168
bc184c6
d995d05
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 |
|---|---|---|
| @@ -1,16 +1,17 @@ | ||
| [versions] | ||
| androidx-xr-arcore = "1.0.0-alpha13" | ||
| androidx-xr-arcore-play-services = "1.0.0-alpha13" | ||
| androidx-xr-compose = "1.0.0-alpha13" | ||
| androidx-xr-glimmer = "1.0.0-alpha12" | ||
| androidx-xr-projected = "1.0.0-alpha07" | ||
| androidx-xr-scenecore = "1.0.0-alpha14" | ||
| androidx-xr-arcore = "1.0.0-alpha14" | ||
| androidx-xr-arcore-play-services = "1.0.0-alpha14" | ||
| androidx-xr-compose = "1.0.0-alpha14" | ||
| androidx-xr-glimmer = "1.0.0-alpha13" | ||
| androidx-xr-projected = "1.0.0-alpha08" | ||
| androidx-xr-scenecore = "1.0.0-alpha15" | ||
|
|
||
| [libraries] | ||
| androidx-xr-arcore = { module = "androidx.xr.arcore:arcore", version.ref = "androidx-xr-arcore" } | ||
| androidx-xr-arcore-play-services = { module = "androidx.xr.arcore:arcore-play-services", version.ref = "androidx-xr-arcore-play-services" } | ||
| androidx-xr-compose = { module = "androidx.xr.compose:compose", version.ref = "androidx-xr-compose" } | ||
| androidx-xr-glimmer = { module = "androidx.xr.glimmer:glimmer", version.ref = "androidx-xr-glimmer" } | ||
| androidx-xr-glimmer-googlefonts = { module = "androidx.xr.glimmer:glimmer-google-fonts", version.ref = "androidx-xr-glimmer" } | ||
| androidx-xr-projected = { module = "androidx.xr.projected:projected", version.ref = "androidx-xr-projected" } | ||
| androidx-xr-projected-testing = { module = "androidx.xr.projected:projected-testing", version.ref = "androidx-xr-projected" } | ||
| androidx-xr-scenecore = { module = "androidx.xr.scenecore:scenecore", version.ref = "androidx-xr-scenecore" } |
| 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) | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,42 @@ | ||
| /* | ||
| * Copyright 2026 The Android Open Source Project | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * https://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| package com.example.xr.glimmer | ||
|
|
||
| import androidx.compose.material.icons.Icons | ||
| import androidx.compose.material.icons.automirrored.filled.Send | ||
| import androidx.compose.material.icons.filled.Favorite | ||
| import androidx.compose.runtime.Composable | ||
| import androidx.xr.glimmer.Button | ||
| import androidx.xr.glimmer.GlimmerTheme | ||
| import androidx.xr.glimmer.Icon | ||
| import androidx.xr.glimmer.Text | ||
|
|
||
| private val FavoriteIcon = Icons.Default.Favorite | ||
| private val SendIcon = Icons.AutoMirrored.Filled.Send | ||
|
|
||
| // [START xr_glimmer_button] | ||
| @Composable | ||
| fun ButtonSample() { | ||
| Button( | ||
| onClick = { /* Handle navigation or action */ }, | ||
| leadingIcon = { Icon(FavoriteIcon, contentDescription = null) }, | ||
| trailingIcon = { Icon(SendIcon, contentDescription = null) } | ||
| ) { | ||
| Text("Text Label", style = GlimmerTheme.typography.titleSmall) | ||
| } | ||
| } | ||
| // [END xr_glimmer_button] |
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.