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

基本的な手のジェスチャーを検出する

手のジョイントのポーズを使用して、基本的な手のジェスチャーを検出します。手のジョイントの規則を参照して、特定のポーズとして登録するためにジョイントがどの範囲のポーズにある必要があるかを判断します。

たとえば、親指と人差し指でピンチ操作を検出するには、2 本の指先の関節間の距離を使用します。

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)