プログマのプログラマ日記

技術メモや自社サービスに関する記事を書いていく予定です。

Unity + Oculus Integrationで物をつかむ(その3:手を表示)

Unity + Oculus Integrationを使ってコントローラやハンドトラッキングでオブジェクトをつかむまでの手順をStep by stepで説明します。
やっていることはOculus Integrationのサンプルシーン「HandGrabExamples」と同様ですが、これを0から組み立ててみてSceneを構成するコンポーネントの役割への理解を深めようという趣旨の記事です。

以下のステップで実装を進めていきます。

  • ルームスケールで動き回れるSceneを作成する手順
  • コントローラを表示できるようにする手順
  • 手を表示できるようにする手順(本記事)
  • オブジェクトをつかめるようにする手順
  • オブジェクトをつかむときのポーズ(つかむ場所・手の形)を設定する手順

2022/11/7: githubにプロジェクトを共有しました。
github.com

環境構築・プロジェクト作成

環境構築とプロジェクト作成の手順については、こちらの記事を参照してください。

rhikos-prgm.hatenablog.com

利用するコンポーネント

今回の記事で新たに使用するコンポーネントは以下の通りです。

ControllerHands

コントローラの位置に手を表示するために使います。InteractionRigOVR/InputOVR配下に追加します。

Controller Hands
 +- LeftControllerHand(Hand, HandActiveState)
 |     +- ControllerHandDataSource(FromOVRControllerHandDataSource)
 |     +- ControllerHandInteractors(InteractorGroup)
 +- RightControllerHand
 |     +- ControllerHandDataSource(FromOVRControllerHandDataSource)
 |     +- ControllerHandInteractors(InteractorGroup)
 +- LeftHandSynthetic(SyntheticHand)
 |     +- LeftHandVisual(HandVIsual, HandPhysicsCapsule)
 +- RightHandSynthetic(SyntheticHand)
       +- RightHandVisual(HandVIsual, HandPhysicsCapsule)

詳細は下記の「InputOVR」を参照。
Inputs | Oculus Developers

Hands

ハンドトラッキングで手を表示するために使います。InteractionRigOVR/InputOVR配下に追加します。

Hands
 +- LeftHand
 |     +- HandDataSource(FromOVRControllerHandDataSource, LastKnownGoodHand, HandFilter)
 |     +- HandFeatures(FingerFeatureStateProvider, TransformFeatureStateProvider, JointDeltaProvider)
 |     +- HandInteractorsLeft(InteractorGroup)
 +- RightHand
       +- HandDataSource(FromOVRControllerHandDataSource, LastKnownGoodHand, HandFilter)
       +- HandFeatures(FingerFeatureStateProvider, TransformFeatureStateProvider, JointDeltaProvider)
       +- HandInteractorsRight(InteractorGroup)

詳細は下記の「InputOVR」を参照。
Inputs | Oculus Developers

Sceneの実装手順

Sceneの作成

空のシーンを作成します。(Assets/Sandbox/HandGrab/Scenes/Scene2)
MainCameraを削除します。
Scene配下に「Oculus/Interaction/Runtime/Prefab/InteractionRigOVR」のprefabを配置します。

コントローラを非表示に設定

InteractionRigOVR/OVRCameraRig/TrackingSpace/LeftControllerAnchor/OVRControllerPrefabを非アクティブにします。(インスペクタの左上チェックを外す)
※Right側も同様の手順で設定します。

コントローラを使う場合の設定

※コントローラを使用しない(ハンドトラッキングだけを使う)場合は、この手順はスキップしても構いません。

ControllerHandsの配置・設定

InteractionRigOVR/InputOVR配下に「Oculus/Interaction/Runtime/Prefab/ControllerHands」を配置します。
ControllerHands/LeftControllerHand/ControllerHandDataSourceのコンポーネント「From OVR Controller Hand Data Source」の属性を以下のように設定します。

  • Camera Rig Ref ... InteractionRigOVR/InputOVR
  • Tracking To World Transformer ... InteractionRigOVR/InputOVR
  • HmdData ... InteractionRigOVR/InputOVR/Hmd

※Right側も同様の手順で設定します。

ハンドトラッキングを使う場合の設定

※ハンドトラッキングを使用しない(コントローラだけを使う)場合は、この手順はスキップしても構いません。

Handsの配置・設定

InteractionRigOVR/InputOVR配下に「Oculus/Interaction/Runtime/Prefab/Hands」を配置します。
Hands/LeftHand/HandDataSourceのコンポーネント「From OVR Controller Hand Data Source」の属性を以下のように設定します。

  • Camera Rig Ref ... InteractionRigOVR/InputOVR
  • Tracking To World Transformer ... InteractionRigOVR/InputOVR
  • HmdData ... InteractionRigOVR/InputOVR/Hmd

Hands/LeftHand/HandFeaturesのコンポーネント「Transform Feature State Provider」のTracking To World Transformer属性にInputOVRを設定します。

※Right側も同様の手順で設定します。

Left(Right)HandSyntheticの配置・設定

InteractionRigOVR/InputOVR/Hands配下に「Oculus/Interaction/Runtime/Prefab/Hands/LeftHandSynthetic」を配置します。
LeftHandSyntheticのコンポーネント「Synthetic Hand」のI Modify Data From Source Monoに「InteractionRigOVR/InputOVR/Hands/LeftHand」を設定します。
※Right側も同様の手順で設定します。

動作確認

実行するとコントローラの位置またはハンドトラッキングした手の位置に手のグラフィックが表示されるようになります。

参考サイト

公式:Oculusスタートガイド(Unity)
Get Started with Oculus in Unity | Oculus Developers

公式:入力データの概要
※サンプルシーンのInteractionRigOVR~prefab配下のコンポーネント群が何をやっているのか、ざっくり理解するための最適なドキュメントだと思います。
Inputs | Oculus Developers

公式:APIリファレンス
Reference