Jetpack XR 的 ARCore 可提供使用者偵測到的手部資訊,以及手部和相關關節的姿勢資訊。這項手部資料可用於將實體和模型附加至使用者的手部,例如工具選單:
取得工作階段
透過 Android XR Session
存取手部資訊。請參閱「瞭解工作階段的生命週期」,取得 Session
。
設定工作階段
XR 工作階段預設不會啟用手部追蹤功能。如要接收手部資料,請設定工作階段並設定 HandTrackingMode.BOTH
模式:
val newConfig = session.config.copy( handTracking = Config.HandTrackingMode.BOTH ) when (val result = session.configure(newConfig)) { is SessionConfigureConfigurationNotSupported -> TODO(/* Some combinations of configurations are not valid. Handle this failure case. */) is SessionConfigureSuccess -> TODO(/* Success! */) else -> TODO(/* A different unhandled exception was thrown. */) }
擷取手部資料
系統會分別提供左手和右手的資料。使用每隻手的 state
存取每個關節的姿勢位置:
Hand.left(session)?.state?.collect { handState -> // or Hand.right(session) // Hand state has been updated. // Use the state of hand joints to update an entity's position. renderPlanetAtHandPalm(handState) }
手部具有下列屬性:
trackingState
:是否正在追蹤手部。handJoints
:手部關節到姿勢的地圖。手部關節姿勢是由 OpenXR 標準指定。
在應用程式中使用手部資料
使用者手部關節的位置可用於將 3D 物件錨定至使用者手上,例如將模型附加至左手手掌:
val palmPose = leftHandState.handJoints[HandJointType.PALM] ?: return // the down direction points in the same direction as the palm val angle = Vector3.angleBetween(palmPose.rotation * Vector3.Down, Vector3.Up) palmEntity.setEnabled(angle > Math.toRadians(40.0)) val transformedPose = session.scene.perceptionSpace.transformPoseTo( palmPose, session.scene.activitySpace, ) val newPosition = transformedPose.translation + transformedPose.down * 0.05f palmEntity.setPose(Pose(newPosition, transformedPose.rotation))
如要將模型附加到右手食指指尖:
val tipPose = rightHandState.handJoints[HandJointType.INDEX_TIP] ?: return // the forward direction points towards the finger tip. val angle = Vector3.angleBetween(tipPose.rotation * Vector3.Forward, Vector3.Up) indexFingerEntity.setEnabled(angle > Math.toRadians(40.0)) val transformedPose = session.scene.perceptionSpace.transformPoseTo( tipPose, session.scene.activitySpace, ) val position = transformedPose.translation + transformedPose.forward * 0.03f val rotation = Quaternion.fromLookTowards(transformedPose.up, Vector3.Up) indexFingerEntity.setPose(Pose(position, rotation))
偵測基本手勢
系統會根據手部關節的姿勢偵測基本手勢,請參閱手部關節的慣例,判斷關節應處於哪個姿勢範圍,才能註冊為特定姿勢。
舉例來說,如要偵測拇指和食指的撥動動作,請使用兩個指尖關節之間的距離:
val thumbTip = handState.handJoints[HandJointType.THUMB_TIP] ?: return false val thumbTipPose = session.scene.perceptionSpace.transformPoseTo(thumbTip, session.scene.activitySpace) val indexTip = handState.handJoints[HandJointType.INDEX_TIP] ?: return false val indexTipPose = session.scene.perceptionSpace.transformPoseTo(indexTip, session.scene.activitySpace) return Vector3.distance(thumbTipPose.translation, indexTipPose.translation) < 0.05
「停止」手勢就是較複雜的手勢,在這個手勢中,每根手指都應伸直,也就是每根手指的每個關節大致都應指向相同方向:
val threshold = toRadians(angleInDegrees = 30f) fun pointingInSameDirection(joint1: HandJointType, joint2: HandJointType): Boolean { val forward1 = handState.handJoints[joint1]?.forward ?: return false val forward2 = handState.handJoints[joint2]?.forward ?: return false return Vector3.angleBetween(forward1, forward2) < threshold } return pointingInSameDirection(HandJointType.INDEX_PROXIMAL, HandJointType.INDEX_TIP) && pointingInSameDirection(HandJointType.MIDDLE_PROXIMAL, HandJointType.MIDDLE_TIP) && pointingInSameDirection(HandJointType.RING_PROXIMAL, HandJointType.RING_TIP)
開發手勢自訂偵測功能時,請注意下列事項:
- 使用者對任何手勢的解讀可能不同。舉例來說,有些人認為「停止」手勢應為手指張開,但有些人則覺得手指併攏更直覺。
- 某些手勢可能難以維持。使用直覺式手勢,避免使用者手部疲勞。
判斷使用者的次要手
Android 系統會將系統導覽功能放在使用者主要使用的手上,如使用者在系統偏好設定中指定。使用次要手部進行自訂手勢,避免與系統導覽手勢發生衝突:
val handedness = Hand.getPrimaryHandSide(activity.contentResolver) val secondaryHand = if (handedness == Hand.HandSide.LEFT) Hand.right(session) else Hand.left(session) val handState = secondaryHand?.state ?: return detectGesture(handState)
OpenXR™ 和 OpenXR 標誌是 The Khronos Group Inc. 的商標,已在中國、歐盟、日本和英國註冊為商標。