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