Skip to content

Commit 7eb7eb5

Browse files
committed
Add snippets for HDR and Material improvements
1 parent bac9645 commit 7eb7eb5

File tree

1 file changed

+80
-0
lines changed

1 file changed

+80
-0
lines changed

xr/src/main/java/com/example/xr/scenecore/SpatialVideo.kt

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,19 @@ package com.example.xr.scenecore
1919
import android.content.ContentResolver
2020
import android.net.Uri
2121
import androidx.activity.ComponentActivity
22+
import androidx.lifecycle.lifecycleScope
2223
import androidx.media3.common.C
2324
import androidx.media3.common.MediaItem
2425
import androidx.media3.exoplayer.ExoPlayer
2526
import androidx.xr.runtime.Session
2627
import androidx.xr.runtime.math.Pose
2728
import androidx.xr.runtime.math.Vector3
2829
import androidx.xr.scenecore.SurfaceEntity
30+
import androidx.xr.scenecore.Texture
31+
import androidx.xr.scenecore.TextureSampler
2932
import androidx.xr.scenecore.scene
33+
import java.nio.file.Paths
34+
import kotlinx.coroutines.launch
3035

3136
private fun ComponentActivity.surfaceEntityCreate(xrSession: Session) {
3237
// [START androidxr_scenecore_surfaceEntityCreate]
@@ -145,3 +150,78 @@ private fun ComponentActivity.surfaceEntityCreateDRM(xrSession: Session) {
145150

146151
// [END androidxr_scenecore_surfaceEntityCreateDRM]
147152
}
153+
154+
private fun ComponentActivity.surfaceEntityHDR(xrSession: Session) {
155+
// [START androidxr_scenecore_surfaceEntityHDR]
156+
// Define the color properties for your HDR video. These values should be specific
157+
// to your content.
158+
val hdrMetadata = SurfaceEntity.ContentColorMetadata(
159+
colorSpace = SurfaceEntity.ContentColorMetadata.ColorSpace.BT2020,
160+
colorTransfer = SurfaceEntity.ContentColorMetadata.ColorTransfer.ST2084, // PQ
161+
colorRange = SurfaceEntity.ContentColorMetadata.ColorRange.LIMITED,
162+
maxCLL = 1000 // Example: 1000 nits
163+
)
164+
165+
// Create a SurfaceEntity, passing the HDR metadata at creation time.
166+
val hdrSurfaceEntity = SurfaceEntity.create(
167+
session = xrSession,
168+
stereoMode = SurfaceEntity.StereoMode.MONO,
169+
pose = Pose(Vector3(0.0f, 0.0f, -1.5f)),
170+
canvasShape = SurfaceEntity.CanvasShape.Quad(1.0f, 1.0f),
171+
contentColorMetadata = hdrMetadata
172+
)
173+
174+
// Initialize ExoPlayer and set the surface.
175+
val exoPlayer = ExoPlayer.Builder(this).build()
176+
exoPlayer.setVideoSurface(hdrSurfaceEntity.getSurface())
177+
178+
// Define the URI for your HDR content.
179+
val videoUri = "https://your-content-provider.com/hdr_video.mp4"
180+
val mediaItem = MediaItem.fromUri(videoUri)
181+
182+
// Set the media item and start playback.
183+
exoPlayer.setMediaItem(mediaItem)
184+
exoPlayer.prepare()
185+
exoPlayer.play()
186+
// [END androidxr_scenecore_surfaceEntityHDR]
187+
}
188+
189+
private fun surfaceEntityEdgeFeathering(xrSession: Session) {
190+
// [START androidxr_scenecore_surfaceEntityEdgeFeathering]
191+
// Create a SurfaceEntity.
192+
val surfaceEntity = SurfaceEntity.create(
193+
session = xrSession,
194+
pose = Pose(Vector3(0.0f, 0.0f, -1.5f))
195+
)
196+
197+
// Feather the edges of the surface.
198+
surfaceEntity.edgeFeather =
199+
SurfaceEntity.EdgeFeatheringParams.SmoothFeather(0.1f, 0.1f)
200+
// [END androidxr_scenecore_surfaceEntityEdgeFeathering]
201+
}
202+
203+
private fun surfaceEntityAlphaMasking(xrSession: Session, activity: ComponentActivity) {
204+
// [START androidxr_scenecore_surfaceEntityAlphaMasking]
205+
// Create a SurfaceEntity.
206+
val surfaceEntity = SurfaceEntity.create(
207+
session = xrSession,
208+
pose = Pose(Vector3(0.0f, 0.0f, -1.5f))
209+
)
210+
211+
// Load the texture in a coroutine scope.
212+
activity.lifecycleScope.launch {
213+
val alphaMaskTexture =
214+
Texture.create(
215+
xrSession,
216+
Paths.get("textures", "alpha_mask.png"),
217+
TextureSampler.create()
218+
)
219+
220+
// Apply the alpha mask.
221+
surfaceEntity.primaryAlphaMaskTexture = alphaMaskTexture
222+
223+
// To remove the mask, set the property to null.
224+
surfaceEntity.primaryAlphaMaskTexture = null
225+
}
226+
// [END androidxr_scenecore_surfaceEntityAlphaMasking]
227+
}

0 commit comments

Comments
 (0)