ARCore for Jetpack XR 可提供使用者偵測到的手部資訊,並提供手部和相關關節的姿勢資訊。這類手部資料可用於將實體和模型附加至使用者的手部,例如工具選單:
取得工作階段
透過 Android XR Session
存取手部資訊。請參閱「瞭解工作階段的生命週期」一文,瞭解如何取得 Session
。
設定工作階段
根據預設,系統不會在 XR 工作階段中啟用手勢追蹤功能。如要接收手部資料,請設定時段:
val newConfig = session.config.copy( handTracking = Config.HandTrackingMode.Enabled ) when (val result = session.configure(newConfig)) { is SessionConfigureConfigurationNotSupported -> TODO(/* Some combinations of configurations are not valid. Handle this failure case. */) is SessionConfigurePermissionsNotGranted -> TODO(/* The required permissions in result.permissions have not been granted. */) is SessionConfigureSuccess -> TODO(/* Success! */) }
擷取手部資料
手部資料可分別針對左手和右手提供。使用每隻手的 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) }
手部具有下列屬性:
isActive
:是否追蹤手部。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.setHidden(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.setHidden(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.getHandedness(activity.contentResolver) val secondaryHand = if (handedness == Hand.Handedness.LEFT) Hand.right(session) else Hand.left(session) val handState = secondaryHand?.state ?: return detectGesture(handState)