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)
OpenXR™和OpenXR徽标是The Khronos Group Inc.拥有的商标,并在中国、欧盟、日本和英国注册为商标。