使用适用于 Jetpack XR 的 ARCore 处理手部操作

适用于 Jetpack XR 的 ARCore 可以提供有关用户检测到的手的信息,并提供手部及其关联关节的姿势信息。这些手部数据可用于将实体和模型附加到用户的手部,例如工具菜单:

获取会话

通过 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)