适用于 Jetpack XR 的 ARCore 可以提供有关用户检测到的手的信息,并提供手部及其关联关节的姿势信息。这些手部数据可用于将实体和模型附加到用户的手部,例如工具菜单:
创建适用于 Jetpack XR 的 ARCore 会话
通过 ARCore for Jetpack XR 会话访问手部信息。请参阅了解会话的生命周期,以获取 Session
。
检索手部数据
手部数据分别针对左手和右手提供。使用每只手的 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 = scenecoreSession.perceptionSpace.transformPoseTo( palmPose, scenecoreSession.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 = scenecoreSession.perceptionSpace.transformPoseTo( tipPose, scenecoreSession.activitySpace, ) val position = transformedPose.translation + transformedPose.forward * 0.03f val rotation = Quaternion.fromLookTowards(transformedPose.up, Vector3.Up) indexFingerEntity.setPose(Pose(position, rotation))