|
| 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.arcore |
| 18 | + |
| 19 | +import android.annotation.SuppressLint |
| 20 | +import android.os.Bundle |
| 21 | +import androidx.activity.ComponentActivity |
| 22 | +import androidx.lifecycle.lifecycleScope |
| 23 | +import androidx.xr.arcore.Hand |
| 24 | +import androidx.xr.arcore.HandJointType |
| 25 | +import androidx.xr.compose.platform.setSubspaceContent |
| 26 | +import androidx.xr.runtime.Session |
| 27 | +import androidx.xr.runtime.math.Pose |
| 28 | +import androidx.xr.runtime.math.Quaternion |
| 29 | +import androidx.xr.runtime.math.Vector3 |
| 30 | +import androidx.xr.scenecore.Entity |
| 31 | +import androidx.xr.scenecore.GltfModel |
| 32 | +import androidx.xr.scenecore.GltfModelEntity |
| 33 | +import kotlinx.coroutines.guava.await |
| 34 | +import kotlinx.coroutines.launch |
| 35 | + |
| 36 | +class SampleHandsActivity : ComponentActivity() { |
| 37 | + lateinit var session: Session |
| 38 | + lateinit var scenecoreSession: androidx.xr.scenecore.Session |
| 39 | + lateinit var sessionHelper: SessionLifecycleHelper |
| 40 | + |
| 41 | + var palmEntity: Entity? = null |
| 42 | + var indexFingerEntity: Entity? = null |
| 43 | + |
| 44 | + override fun onCreate(savedInstanceState: Bundle?) { |
| 45 | + super.onCreate(savedInstanceState) |
| 46 | + setSubspaceContent { } |
| 47 | + |
| 48 | + scenecoreSession = androidx.xr.scenecore.Session.create(this@SampleHandsActivity) |
| 49 | + lifecycleScope.launch { |
| 50 | + val model = GltfModel.create(scenecoreSession, "models/saturn_rings.glb").await() |
| 51 | + palmEntity = GltfModelEntity.create(scenecoreSession, model).apply { |
| 52 | + setScale(0.3f) |
| 53 | + setHidden(true) |
| 54 | + } |
| 55 | + indexFingerEntity = GltfModelEntity.create(scenecoreSession, model).apply { |
| 56 | + setScale(0.2f) |
| 57 | + setHidden(true) |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + sessionHelper = SessionLifecycleHelper( |
| 62 | + onCreateCallback = { session = it }, |
| 63 | + onResumeCallback = { |
| 64 | + collectHands(session) |
| 65 | + } |
| 66 | + ) |
| 67 | + lifecycle.addObserver(sessionHelper) |
| 68 | + } |
| 69 | +} |
| 70 | + |
| 71 | +fun SampleHandsActivity.collectHands(session: Session) { |
| 72 | + lifecycleScope.launch { |
| 73 | + // [START androidxr_arcore_hand_collect] |
| 74 | + Hand.left(session)?.state?.collect { handState -> // or Hand.right(session) |
| 75 | + // Hand state has been updated. |
| 76 | + // Use the state of hand joints to update an entity's position. |
| 77 | + renderPlanetAtHandPalm(handState) |
| 78 | + } |
| 79 | + // [END androidxr_arcore_hand_collect] |
| 80 | + } |
| 81 | + lifecycleScope.launch { |
| 82 | + Hand.right(session)?.state?.collect { rightHandState -> |
| 83 | + renderPlanetAtFingerTip(rightHandState) |
| 84 | + } |
| 85 | + } |
| 86 | +} |
| 87 | + |
| 88 | +@SuppressLint("RestrictedApi") // HandJointType is mistakenly @Restrict: b/397415504 |
| 89 | +fun SampleHandsActivity.renderPlanetAtHandPalm(leftHandState: Hand.State) { |
| 90 | + val palmEntity = palmEntity ?: return |
| 91 | + // [START androidxr_arcore_hand_entityAtHandPalm] |
| 92 | + val palmPose = leftHandState.handJoints[HandJointType.PALM] ?: return |
| 93 | + |
| 94 | + // the down direction points in the same direction as the palm |
| 95 | + val angle = Vector3.angleBetween(palmPose.rotation * Vector3.Down, Vector3.Up) |
| 96 | + palmEntity.setHidden(angle > Math.toRadians(40.0)) |
| 97 | + |
| 98 | + val transformedPose = |
| 99 | + scenecoreSession.perceptionSpace.transformPoseTo( |
| 100 | + palmPose, |
| 101 | + scenecoreSession.activitySpace, |
| 102 | + ) |
| 103 | + val newPosition = transformedPose.translation + transformedPose.down * 0.05f |
| 104 | + palmEntity.setPose(Pose(newPosition, transformedPose.rotation)) |
| 105 | + // [END androidxr_arcore_hand_entityAtHandPalm] |
| 106 | +} |
| 107 | + |
| 108 | +@SuppressLint("RestrictedApi") // HandJointType is mistakenly @Restrict: b/397415504 |
| 109 | +fun SampleHandsActivity.renderPlanetAtFingerTip(rightHandState: Hand.State) { |
| 110 | + val indexFingerEntity = indexFingerEntity ?: return |
| 111 | + |
| 112 | + // [START androidxr_arcore_hand_entityAtIndexFingerTip] |
| 113 | + val tipPose = rightHandState.handJoints[HandJointType.INDEX_TIP] ?: return |
| 114 | + |
| 115 | + // the forward direction points towards the finger tip. |
| 116 | + val angle = Vector3.angleBetween(tipPose.rotation * Vector3.Forward, Vector3.Up) |
| 117 | + indexFingerEntity.setHidden(angle > Math.toRadians(40.0)) |
| 118 | + |
| 119 | + val transformedPose = |
| 120 | + scenecoreSession.perceptionSpace.transformPoseTo( |
| 121 | + tipPose, |
| 122 | + scenecoreSession.activitySpace, |
| 123 | + ) |
| 124 | + val position = transformedPose.translation + transformedPose.forward * 0.03f |
| 125 | + val rotation = Quaternion.fromLookTowards(transformedPose.up, Vector3.Up) |
| 126 | + indexFingerEntity.setPose(Pose(position, rotation)) |
| 127 | + // [END androidxr_arcore_hand_entityAtIndexFingerTip] |
| 128 | +} |
0 commit comments