Skip to content

Port environment snippets from DAC #498

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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
77 changes: 77 additions & 0 deletions xr/src/main/java/com/example/xr/scenecore/Environments.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
/*
* Copyright 2025 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.scenecore

import androidx.xr.scenecore.ExrImage
import androidx.xr.scenecore.GltfModel
import androidx.xr.scenecore.Session
import androidx.xr.scenecore.SpatialEnvironment
import kotlinx.coroutines.guava.await

private class Environments(val session: Session) {
suspend fun loadEnvironmentGeometry() {
// [START androidxr_scenecore_environment_loadEnvironmentGeometry]
val environmentGeometryFuture = GltfModel.create(session, "DayGeometry.glb")
val environmentGeometry = environmentGeometryFuture.await()
// [END androidxr_scenecore_environment_loadEnvironmentGeometry]
}

fun loadEnvironmentSkybox() {
// [START androidxr_scenecore_environment_loadEnvironmentSkybox]
val skybox = ExrImage.create(session, "BlueSkybox.exr")
// [END androidxr_scenecore_environment_loadEnvironmentSkybox]
}

fun setEnvironmentPreference(environmentGeometry: GltfModel, skybox: ExrImage) {
// [START androidxr_scenecore_environment_setEnvironmentPreference]
val spatialEnvironmentPreference =
SpatialEnvironment.SpatialEnvironmentPreference(skybox, environmentGeometry)
val preferenceResult =
session.spatialEnvironment.setSpatialEnvironmentPreference(spatialEnvironmentPreference)
if (preferenceResult == SpatialEnvironment.SetSpatialEnvironmentPreferenceChangeApplied()) {
// The environment was successfully updated and is now visible, and any listeners
// specified using addOnSpatialEnvironmentChangedListener will be notified.
} else if (preferenceResult == SpatialEnvironment.SetSpatialEnvironmentPreferenceChangePending()) {
// The environment is in the process of being updated. Once visible, any listeners
// specified using addOnSpatialEnvironmentChangedListener will be notified.
}
// [END androidxr_scenecore_environment_setEnvironmentPreference]
}

fun setPassthroughOpacityPreference() {
// [START androidxr_scenecore_environment_setPassthroughOpacityPreference]
val preferenceResult = session.spatialEnvironment.setPassthroughOpacityPreference(1.0f)
if (preferenceResult == SpatialEnvironment.SetPassthroughOpacityPreferenceChangeApplied()) {
// The passthrough opacity request succeeded and should be visible now, and any listeners
// specified using addOnPassthroughOpacityChangedListener will be notified
} else if (preferenceResult == SpatialEnvironment.SetPassthroughOpacityPreferenceChangePending()) {
// The passthrough opacity preference was successfully set, but not
// immediately visible. The passthrough opacity change will be applied
// when the activity has the
// SpatialCapabilities.SPATIAL_CAPABILITY_PASSTHROUGH_CONTROL capability.
// Then, any listeners specified using addOnPassthroughOpacityChangedListener
// will be notified
}
// [END androidxr_scenecore_environment_setPassthroughOpacityPreference]
}

fun getCurrentPassthroughOpacity() {
// [START androidxr_scenecore_environment_getCurrentPassthroughOpacity]
val currentPassthroughOpacity = session.spatialEnvironment.getCurrentPassthroughOpacity()
// [END androidxr_scenecore_environment_getCurrentPassthroughOpacity]
}
}