使用 ARCore for Jetpack XR 與手部互動

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 =
    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))