10
6

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

HoloLensでオブジェクトを壁に貼る

Posted at

はじめに

3月初めよりインターンで株式会社ゆめみ(https://www.yumemi.co.jp/ja) にてHoloLensの研究開発をしている近大生のよっしーです。
国内でこの1月から提供を開始したMicroSoft製MRデバイスのHoloLensについての記事を書いていきます。

「HoloLens +他社製の外部入力」や「動きを3Dモデルにして配信」など、いろいろな方たちが面白い試みを数多く試されていますが、HoloLens開発の基本的部分の記事があまりなかったので、それをメインに書いていこうと思います。

すること

オブジェクトを壁に接着させる

Holographic academy Holograms230で出てくるPlacementのところです。
物体を触ったときに接着したまま動かすコードは動画で出てきましたが、生成時に壁に貼り付けるところの説明はなかったのでしていきます。

前提環境

  • HoloLensの開発環境セットアップ済み
  • Unityでの開発
  • HoloToolkit-Unityの利用
  • HoloLens開発の基礎用語
  • PlaySpaceManager(Holographic Academy 230で出てくる)の利用で、メッシュを取り除いた平面へと変換が出来ている。

実装例

adhereObjectOnTheWall.cs
RaycastHit hitInfo;

bool hit = Physics.Raycast(GazeManager.Instance.GazeOrigin,GazeManager.Instance.GazeNormal, out hitInfo, 20f, SpatialMappingManager.Instance.LayerMask);
if (hit) {
    //ヒットした表面の取得
    GameObject surface = hitInfo.transform.gameObject;

    //表面からどれから浮かすかの値
    float hoverDistance = 0.15f;
    float offsetDistance = hoverDistance;

    //設定したhoverDistanceよりGazeと衝突判定の発生した平面との距離が少ない場合0fにする。
    if (hitInfo.distance <= hoverDistance) {
        offsetDistance = 0f;
    }
    //衝突判定のあったポイントを少し浮かせる
    Vector3 placementPosition = hitInfo.point + (offsetDistance * hitInfo.normal);

    // ぶつかった壁の方向を向かせる
    Quaternion rotation = Quaternion.LookRotation(surface.transform.forward, Vector3.up);
    Instantiate(SomePrefab,placementPosition,rotation);
}

縦にペラペラの場合

Holograms230のように、ポスターであったりして縦にペラペラなオブジェクトは上のコードで問題ないかと思います。絵にかくと下図のような感じ。
PosterOnTheWall.png

横に長い場合

これはこのままだと下図のようになります。
CatOverWall.png

対応策

Quaternionを足してあげましょう。上図のようなはみ出方の時は

AddQuaternion.cs
Quaternion rotation = Quaternion.LookRotation(surface.transform.forward, Vector3.up) * Quaternion.Euler(new Vector3(-90, 0, 0));

というふうに書き換えてみてください。(上を向かせる場合)

そうするとこうなるはずです。
CatOnTheWall.png

参考・関連

Holographic academy Holograms230
https://developer.microsoft.com/en-us/windows/holographic/holograms_230

HoloLensで壁、床、机、天井の分類をする
http://qiita.com/guitar_char/items/b06ef68777e10c6482c5

10
6
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
10
6

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?