ARCore for Jetpack XR 可以提供有关检测到的用户手部的信息,并提供手部及其关联关节的姿态信息。此手部数据可用于将实体和模型附加到用户的手部,例如工具菜单:
访问会话
通过 Jetpack XR 运行时 Session 访问手部信息,该运行时可由您的应用创建。
配置会话
默认情况下,XR 会话中未启用手部追踪功能。如需接收手部数据,请配置会话并设置 HandTrackingMode.BOTH 模式:
val newConfig = Config.Builder(session.config) .setHandTracking(HandTrackingMode.BOTH) .build() when (val result = session.configure(newConfig)) { is SessionConfigureSuccess -> TODO(/* Success! */) else -> TODO(/* The session could not be configured. See SessionConfigureResult for possible causes. */) }
检索手部数据
手部数据分别针对左手和右手提供。使用每只手的 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 == HandSide.LEFT) Hand.right(session) else Hand.left(session) detectGesture(secondaryHand.state)
OpenXR™ 和 OpenXR 徽标是 The Khronos Group Inc. 拥有的商标,并在中国、欧盟、日本和英国注册为商标。