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. 拥有的商标,已在中国、欧盟、日本和英国注册为商标。