ナレコム藤本です、HoloLens Advent Calendar 2017 3日目の記事です!
MixedRealityCompanionKitのPosterCalibrationSampleをやってみました。
PosterCalibrationSampleでは、ポスターのような自然マーカーを用いて、ホログラムの位置合わせを行うことができます。
HoloLensのWorld Anchor機能を用いると、空間認識の誤差により固定したホログラムが徐々にズレてきてしまう場合があります。
アプリの起動時にマーカーを用いて、ホログラムの位置を補正してあげることで改善することができます。
開発環境
開発環境は、以下です。
■ Windows 10 Home
■ Visual Studio 2017 (15.4.4)
☑C++ ユニバーサル Windows プラットフォーム ツール
■ Unity 2017.2.0f3
■ Microsoft HoloLens
実行手順
1. こちらのreleasesをクリック、2017/12/01時点で最新のSource code (zip) をダウンロードします。
2. PosterCalibrationSampleフォルダのPlugin.slnをVisual Studio 2017で開きます。
3. ビルド設定をRelease/Win32にし、Ctrl+Shift+F5でビルドのみ行います。
※最初プラットフォームツールセットのバージョンでエラーが出ます。プロジェクトの再ターゲットが必要ですので、プロジェクト名を右クリック->プロジェクトの再ターゲットでプラットフォームツールセットをv141にアップグレードしてください。
4. PosterCalibrationSample/Plugin/Release/PluginフォルダにできたファイルをPosterCalibrationSample/Unity/Assets/Scripts/PosterAlign/Binariesにコピペします。
↓
5. Unityを起動し、PosterCalibrationSample/Unityフォルダを開いてください。Unity 5.5.0b11で作成されていたようです。Continueをクリックしましょう。
6. ProjectビューにあるSingleZoneをクリックしシーンを開いてください。File->Build Settingsからビルド設定を行います。以下のようになります。Player Settingsは特に変更ありません。
7. Switch Platformをクリック、Buildをクリックしてビルドします。ビルド先のフォルダ名はAppとしましょう。
8. エラーが出るので、Assets/Scripts/SupportingScripts/Input/GestureStateManager.csを以下のように訂正してください。訂正後いくつかWarningが出ますが、ビルドできると思います。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 |
// Copyright (c) Microsoft Corporation. All rights reserved. // Licensed under the MIT License. See LICENSE in the project root for license information. using System; using UnityEngine; namespace PosterAlignment.InputUtilities { public class GestureStateManager : StateManager { public enum Mode { Navigation, Manipulation } public ushort ClickCount { get; private set; } public override bool IsPressed(UnityEngine.EventSystems.PointerEventData.InputButton button) { return this.ClickCount > 0; } public override bool IsReleased(UnityEngine.EventSystems.PointerEventData.InputButton button) { return this.isReleased; } private bool isReleased; public bool IsCancelled { get; private set; } public bool IsCapturingGestures { get { return (this.activeRecognizer != null) ? this.activeRecognizer.IsCapturingGestures() : false; } } public Mode GestureMode { get { return this.recognizerType; } set { this.ResetGestureRecognizer(value); } } public Mode recognizerType = Mode.Manipulation; private UnityEngine.XR.WSA.Input.GestureRecognizer activeRecognizer = null; private UnityEngine.XR.WSA.Input.GestureRecognizer navigationRecognizer = null; private Vector3 navigationPosition = Vector3.zero; private UnityEngine.XR.WSA.Input.GestureRecognizer manipulationRecognizer = null; private Vector3 manipulationPosition = Vector3.zero; private Vector3 manipulationPrevious = Vector3.zero; public override Vector2 ScreenPosition { get { return this.screenPosition; } } private Vector2 screenPosition; public override Vector3 WorldScreenPosition { get { return this.worldScreenPosition; } } private Vector3 worldScreenPosition; public override Vector2 ScreenDelta { get { return this.screenDelta; } } private Vector2 screenDelta; public override Vector3 WorldDelta { get { return this.worldDelta; } } private Vector3 worldDelta; private void Awake() { // Instantiate the Navigation recognizer this.navigationRecognizer = new UnityEngine.XR.WSA.Input.GestureRecognizer(); this.navigationRecognizer.SetRecognizableGestures( UnityEngine.XR.WSA.Input.GestureSettings.Tap | UnityEngine.XR.WSA.Input.GestureSettings.DoubleTap | UnityEngine.XR.WSA.Input.GestureSettings.NavigationX | UnityEngine.XR.WSA.Input.GestureSettings.NavigationY | UnityEngine.XR.WSA.Input.GestureSettings.NavigationZ); this.navigationRecognizer.TappedEvent += this.Recognizer_TappedEvent; this.navigationRecognizer.NavigationStartedEvent += this.NavigationRecognizer_NavigationStartedEvent; this.navigationRecognizer.NavigationUpdatedEvent += this.NavigationRecognizer_NavigationUpdatedEvent; this.navigationRecognizer.NavigationCompletedEvent += this.NavigationRecognizer_NavigationCompletedEvent; this.navigationRecognizer.NavigationCanceledEvent += this.NavigationRecognizer_NavigationCanceledEvent; // Instantiate the Manipulation Recognizer. this.manipulationRecognizer = new UnityEngine.XR.WSA.Input.GestureRecognizer(); this.manipulationRecognizer.SetRecognizableGestures( UnityEngine.XR.WSA.Input.GestureSettings.Tap | UnityEngine.XR.WSA.Input.GestureSettings.DoubleTap | UnityEngine.XR.WSA.Input.GestureSettings.ManipulationTranslate); this.manipulationRecognizer.TappedEvent += this.Recognizer_TappedEvent; this.manipulationRecognizer.ManipulationStartedEvent += this.ManipulationRecognizer_ManipulationStartedEvent; this.manipulationRecognizer.ManipulationUpdatedEvent += this.ManipulationRecognizer_ManipulationUpdatedEvent; this.manipulationRecognizer.ManipulationCompletedEvent += this.ManipulationRecognizer_ManipulationCompletedEvent; this.manipulationRecognizer.ManipulationCanceledEvent += this.ManipulationRecognizer_ManipulationCanceledEvent; // interaction manager state handling UnityEngine.XR.WSA.Input.InteractionManager.InteractionSourcePressed += InteractionManager_SourcePressed; UnityEngine.XR.WSA.Input.InteractionManager.InteractionSourceReleased += InteractionManager_SourceReleased; } private void OnDestroy() { if (this.navigationRecognizer != null) { this.navigationRecognizer.TappedEvent -= this.Recognizer_TappedEvent; this.navigationRecognizer.NavigationStartedEvent -= this.NavigationRecognizer_NavigationStartedEvent; this.navigationRecognizer.NavigationUpdatedEvent -= this.NavigationRecognizer_NavigationUpdatedEvent; this.navigationRecognizer.NavigationCompletedEvent -= this.NavigationRecognizer_NavigationCompletedEvent; this.navigationRecognizer.NavigationCanceledEvent -= this.NavigationRecognizer_NavigationCanceledEvent; } if (this.manipulationRecognizer != null) { this.manipulationRecognizer.TappedEvent -= this.Recognizer_TappedEvent; this.manipulationRecognizer.ManipulationStartedEvent -= this.ManipulationRecognizer_ManipulationStartedEvent; this.manipulationRecognizer.ManipulationUpdatedEvent -= this.ManipulationRecognizer_ManipulationUpdatedEvent; this.manipulationRecognizer.ManipulationCompletedEvent -= this.ManipulationRecognizer_ManipulationCompletedEvent; this.manipulationRecognizer.ManipulationCanceledEvent -= this.ManipulationRecognizer_ManipulationCanceledEvent; } // interaction manager state handling UnityEngine.XR.WSA.Input.InteractionManager.InteractionSourcePressed -= InteractionManager_SourcePressed; UnityEngine.XR.WSA.Input.InteractionManager.InteractionSourceReleased -= InteractionManager_SourceReleased; } private void OnEnable() { this.ResetGestureRecognizer(this.GestureMode); } private void OnDisable() { this.Transition(null); } private void ResetGestureRecognizer(Mode type) { switch (type) { case Mode.Navigation: this.Transition(this.navigationRecognizer); break; case Mode.Manipulation: this.Transition(this.manipulationRecognizer); break; } this.recognizerType = type; } private void Transition(UnityEngine.XR.WSA.Input.GestureRecognizer newRecognizer) { if (newRecognizer != null && this.activeRecognizer == newRecognizer && this.IsCapturingGestures) { return; } if (this.activeRecognizer != null) { if (this.activeRecognizer == newRecognizer) { return; } this.activeRecognizer.CancelGestures(); this.activeRecognizer.StopCapturingGestures(); } if (newRecognizer != null) { newRecognizer.StartCapturingGestures(); } this.activeRecognizer = newRecognizer; } // only used when called via Unity UI // use ResetGestureRecognizer public void SetGestureMode(int type) { this.GestureMode = (Mode)Enum.ToObject(typeof(Mode), type); } private void Recognizer_TappedEvent(UnityEngine.XR.WSA.Input.InteractionSourceKind source, int tapCount, Ray headRay) { this.ClickCount = (ushort)tapCount; this.isReleased = true; } private void NavigationRecognizer_NavigationStartedEvent(UnityEngine.XR.WSA.Input.InteractionSourceKind source, Vector3 relativePosition, Ray ray) { this.ClickCount = 1; this.isReleased = false; this.navigationPosition = relativePosition; } private void NavigationRecognizer_NavigationUpdatedEvent(UnityEngine.XR.WSA.Input.InteractionSourceKind source, Vector3 relativePosition, Ray ray) { this.ClickCount = 0; this.isReleased = false; this.navigationPosition = relativePosition; } private void NavigationRecognizer_NavigationCompletedEvent(UnityEngine.XR.WSA.Input.InteractionSourceKind source, Vector3 relativePosition, Ray ray) { this.ClickCount = 0; this.isReleased = true; this.IsCancelled = false; this.navigationPosition = relativePosition; } private void NavigationRecognizer_NavigationCanceledEvent(UnityEngine.XR.WSA.Input.InteractionSourceKind source, Vector3 relativePosition, Ray ray) { this.ClickCount = 0; this.isReleased = true; this.IsCancelled = true; this.navigationPosition = Vector3.zero; } private void ManipulationRecognizer_ManipulationStartedEvent(UnityEngine.XR.WSA.Input.InteractionSourceKind source, Vector3 position, Ray ray) { this.ClickCount = 1; this.isReleased = false; this.manipulationPosition = position; this.manipulationPrevious = position; } private void ManipulationRecognizer_ManipulationUpdatedEvent(UnityEngine.XR.WSA.Input.InteractionSourceKind source, Vector3 position, Ray ray) { this.ClickCount = 0; this.isReleased = false; this.manipulationPrevious = this.manipulationPosition; this.manipulationPosition = position; } private void ManipulationRecognizer_ManipulationCompletedEvent(UnityEngine.XR.WSA.Input.InteractionSourceKind source, Vector3 position, Ray ray) { this.ClickCount = 0; this.isReleased = true; this.IsCancelled = false; this.manipulationPrevious = this.manipulationPosition; this.manipulationPosition = position; } private void ManipulationRecognizer_ManipulationCanceledEvent(UnityEngine.XR.WSA.Input.InteractionSourceKind source, Vector3 position, Ray ray) { this.ClickCount = 0; this.isReleased = true; this.IsCancelled = true; this.manipulationPrevious = Vector3.zero; this.manipulationPosition = Vector3.zero; } private void InteractionManager_SourcePressed(UnityEngine.XR.WSA.Input.InteractionSourcePressedEventArgs state) { this.ClickCount = 1; } private void InteractionManager_SourceReleased(UnityEngine.XR.WSA.Input.InteractionSourceReleasedEventArgs state) { this.isReleased = true; } // IModuleManager public override bool IsSupported() { return (this.activeRecognizer != null); } public override bool ShouldActivate() { bool shouldActivate = false; if(this.IsCapturingGestures) { shouldActivate |= this.IsPressed(UnityEngine.EventSystems.PointerEventData.InputButton.Left); // did we click shouldActivate |= !this.IsReleased(UnityEngine.EventSystems.PointerEventData.InputButton.Left); // did not release shouldActivate |= this.IsCancelled; // did we cancel } return shouldActivate; } public override bool ShouldSubmit() { // TODO: support a voice command for submit return false; } public override bool ShouldCancel() { return this.IsCancelled; } public override bool ShouldMove(out Vector2 movement) { // TODO: support a voice command for next previous / up/down actions movement = Vector3.zero; return false; } public override void ResetButtonState(UnityEngine.EventSystems.PointerEventData.InputButton button) { if (this.isReleased == true && this.ClickCount > 0) { this.ClickCount = 0; } this.IsCancelled = false; this.isReleased = false; } public override void ActivateModule() { } public override void DeactivateModule() { } public override void UpdateModule() { this.screenPosition = StateManager.ScreenCenter; this.worldScreenPosition = StateManager.WorldScreenCenter; Vector3 delta = Vector3.zero; if (this.IsCapturingGestures) { if (this.GestureMode == Mode.Navigation) { delta = this.navigationPosition; } else { delta = this.manipulationPosition - this.manipulationPrevious; } } this.worldDelta = delta; this.screenDelta = Camera.main.WorldToScreenPoint(this.worldDelta); } } } |
9. Appフォルダ内のPosterCalibrationSample.slnをVisual Studio 2017で開き、ビルド設定をRelease/x86、リモートコンピューターに設定してHoloLensのIPアドレスを指定し、Ctrl+F5で実行します。
10. PosterCalibrationSample/Unity/Assets/PostersにあるMinecraft.pngとEscher.png(MultiZoneシーンで用いる)をA4で印刷しておいてください。
11. MultiZoneも同様に実行できます。
実行結果
MultiZoneのみ説明します。
MultiZone
ボイスコマンドの説明文と、マインクラフトのポスターホログラムと絵画(Escher)のホログラムが見えると思います。
“align poster 1″と発声すると、右にtargetとなるマインクラフトのポスターが表示され、左にHoloLensが見ている画像と特徴点が出ています。
その状態で、マインクラフトのポスターを見ると、緑色の枠で囲まれ認識されます。
すると、マインクラフトのポスターのホログラムが現実世界のポスターの位置に移動します。
Scale Up / Down、Move X/Y/Z Plus/Minusと発声して、位置を調整してください。
調整ができたら”lock alignment”と発声して、ホログラムを固定します。
poster 2も同様にできます。調整した結果が次のようになります。
Fordのデモでは、自動車のナンバープレートをマーカーとして、自動車(粘土モデル)の上にデザイナーの設計図(ホログラム)を被せて表示しています。表示した際のズレは、XBOXゲームコントローラーを用いて補正しているそうです。誤差を10mm程度まで抑えることができるそうです。
以上です。