-
Notifications
You must be signed in to change notification settings - Fork 63
Updating environment, additional dependencies, formatting, and optimizing imports #33
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 all commits
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,14 +16,17 @@ | |||||
|
|
||||||
| package com.example.helloandroidxr.environment | ||||||
|
|
||||||
| import android.net.Uri | ||||||
| import android.annotation.SuppressLint | ||||||
| import android.util.Log | ||||||
| import androidx.core.net.toUri | ||||||
| import androidx.xr.runtime.Session | ||||||
| import androidx.xr.scenecore.ExrImage | ||||||
| import androidx.xr.scenecore.GltfModel | ||||||
| import androidx.xr.scenecore.SpatialEnvironment | ||||||
| import androidx.xr.scenecore.scene | ||||||
| import kotlinx.coroutines.CoroutineScope | ||||||
| import kotlinx.coroutines.launch | ||||||
| import java.nio.file.Paths | ||||||
|
|
||||||
| class EnvironmentController(private val xrSession: Session, private val coroutineScope: CoroutineScope) { | ||||||
| private val assetCache: HashMap<String, Any> = HashMap() | ||||||
|
|
@@ -40,18 +43,22 @@ class EnvironmentController(private val xrSession: Session, private val coroutin | |||||
| /** | ||||||
| * Request the system load a custom Environment | ||||||
| */ | ||||||
| @SuppressLint("NewApi") // Paths.get is API 26+, but Android XR devices are 34+ | ||||||
| fun requestCustomEnvironment(environmentModelName: String) { | ||||||
| coroutineScope.launch { | ||||||
| try { | ||||||
| if (activeEnvironmentModelName == null || | ||||||
| activeEnvironmentModelName != environmentModelName | ||||||
| ) { | ||||||
|
|
||||||
| val lightingForSkybox = ExrImage.createFromZip( | ||||||
| xrSession, | ||||||
| Paths.get("environments/green_hills_ibl.zip") | ||||||
| ) | ||||||
| val environmentModel = assetCache[environmentModelName] as GltfModel | ||||||
|
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 forced cast to
Suggested change
|
||||||
|
|
||||||
| SpatialEnvironment.SpatialEnvironmentPreference( | ||||||
| skybox = null, | ||||||
| geometry = environmentModel | ||||||
| geometry = environmentModel, | ||||||
| skybox = lightingForSkybox, | ||||||
| ).let { | ||||||
| xrSession.scene.spatialEnvironment.preferredSpatialEnvironment = it | ||||||
| } | ||||||
|
|
@@ -74,7 +81,7 @@ class EnvironmentController(private val xrSession: Session, private val coroutin | |||||
| if (!assetCache.containsKey(modelName)) { | ||||||
| try { | ||||||
| val gltfModel = | ||||||
| GltfModel.create(xrSession, Uri.parse(modelName)) | ||||||
| GltfModel.create(xrSession, modelName.toUri()) | ||||||
| assetCache[modelName] = gltfModel | ||||||
|
|
||||||
| } catch (e: Exception) { | ||||||
|
|
||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -79,7 +79,7 @@ fun BugdroidModel( | |
|
|
||
| // Create and apply a custom PBR material to the model when the XR session or target node changes. | ||
| LaunchedEffect(xrSession, bugdroidNode) { | ||
| val material = KhronosPbrMaterial.create( | ||
| val material = pbrMaterial ?: KhronosPbrMaterial.create( | ||
|
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. The |
||
| session = xrSession, | ||
| alphaMode = AlphaMode.OPAQUE | ||
| ).also { | ||
|
|
||
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
ExrImageis recreated from the zip file every time the environment is changed. Unlike theGltfModel, this asset is not cached, which can lead to unnecessary I/O and processing overhead (unzipping and decoding) when switching between environments. Consider caching theExrImageto improve performance.