Skip to content

Commit 0217ca1

Browse files
authored
Port environment snippets from DAC (#498)
* Port environment snippets from DAC * Apply Spotless --------- Co-authored-by: devbridie <[email protected]>
1 parent 0248140 commit 0217ca1

File tree

1 file changed

+77
-0
lines changed

1 file changed

+77
-0
lines changed
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
/*
2+
* Copyright 2025 The Android Open Source Project
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.example.xr.scenecore
18+
19+
import androidx.xr.scenecore.ExrImage
20+
import androidx.xr.scenecore.GltfModel
21+
import androidx.xr.scenecore.Session
22+
import androidx.xr.scenecore.SpatialEnvironment
23+
import kotlinx.coroutines.guava.await
24+
25+
private class Environments(val session: Session) {
26+
suspend fun loadEnvironmentGeometry() {
27+
// [START androidxr_scenecore_environment_loadEnvironmentGeometry]
28+
val environmentGeometryFuture = GltfModel.create(session, "DayGeometry.glb")
29+
val environmentGeometry = environmentGeometryFuture.await()
30+
// [END androidxr_scenecore_environment_loadEnvironmentGeometry]
31+
}
32+
33+
fun loadEnvironmentSkybox() {
34+
// [START androidxr_scenecore_environment_loadEnvironmentSkybox]
35+
val skybox = ExrImage.create(session, "BlueSkybox.exr")
36+
// [END androidxr_scenecore_environment_loadEnvironmentSkybox]
37+
}
38+
39+
fun setEnvironmentPreference(environmentGeometry: GltfModel, skybox: ExrImage) {
40+
// [START androidxr_scenecore_environment_setEnvironmentPreference]
41+
val spatialEnvironmentPreference =
42+
SpatialEnvironment.SpatialEnvironmentPreference(skybox, environmentGeometry)
43+
val preferenceResult =
44+
session.spatialEnvironment.setSpatialEnvironmentPreference(spatialEnvironmentPreference)
45+
if (preferenceResult == SpatialEnvironment.SetSpatialEnvironmentPreferenceChangeApplied()) {
46+
// The environment was successfully updated and is now visible, and any listeners
47+
// specified using addOnSpatialEnvironmentChangedListener will be notified.
48+
} else if (preferenceResult == SpatialEnvironment.SetSpatialEnvironmentPreferenceChangePending()) {
49+
// The environment is in the process of being updated. Once visible, any listeners
50+
// specified using addOnSpatialEnvironmentChangedListener will be notified.
51+
}
52+
// [END androidxr_scenecore_environment_setEnvironmentPreference]
53+
}
54+
55+
fun setPassthroughOpacityPreference() {
56+
// [START androidxr_scenecore_environment_setPassthroughOpacityPreference]
57+
val preferenceResult = session.spatialEnvironment.setPassthroughOpacityPreference(1.0f)
58+
if (preferenceResult == SpatialEnvironment.SetPassthroughOpacityPreferenceChangeApplied()) {
59+
// The passthrough opacity request succeeded and should be visible now, and any listeners
60+
// specified using addOnPassthroughOpacityChangedListener will be notified
61+
} else if (preferenceResult == SpatialEnvironment.SetPassthroughOpacityPreferenceChangePending()) {
62+
// The passthrough opacity preference was successfully set, but not
63+
// immediately visible. The passthrough opacity change will be applied
64+
// when the activity has the
65+
// SpatialCapabilities.SPATIAL_CAPABILITY_PASSTHROUGH_CONTROL capability.
66+
// Then, any listeners specified using addOnPassthroughOpacityChangedListener
67+
// will be notified
68+
}
69+
// [END androidxr_scenecore_environment_setPassthroughOpacityPreference]
70+
}
71+
72+
fun getCurrentPassthroughOpacity() {
73+
// [START androidxr_scenecore_environment_getCurrentPassthroughOpacity]
74+
val currentPassthroughOpacity = session.spatialEnvironment.getCurrentPassthroughOpacity()
75+
// [END androidxr_scenecore_environment_getCurrentPassthroughOpacity]
76+
}
77+
}

0 commit comments

Comments
 (0)