MRが楽しい

MRやVRについて学習したことを書き残す

hololensで指の位置を検知する

本日は Unity(hololens) の技術調査枠です。
下記の記事を参考に、hololens での手の位置検出について試してみます。
qiita.com

まずは新規プロジェクトを作成します。
f:id:bluebirdofoz:20170829001554j:plain

今回のプロジェクトは hololens 上での確認が必須になるので、HoloToolKit をインポートして
hololens 向けプロジェクトに変更しておきます。
f:id:bluebirdofoz:20170829001606j:plain

動かすオブジェクトのサンプルとして Sphere オブジェクトを配置します。
f:id:bluebirdofoz:20170829001621j:plain

参考記事の HandPosition.cs を元に Sphere オブジェクトを掴んで運ぶスクリプトを作成します。
・HandPosition.cs(改二)

using UnityEngine;
using System.Collections;
using UnityEngine.VR.WSA.Input;

public class HandPosition : MonoBehaviour
{
    public GameObject HandObject;
    private bool HandFlg;

    void Start()
    {
        HandFlg = false;
        InteractionManager.SourceUpdated += InteractionManager_SourceUpdated;
        InteractionManager.SourceLost += InteractionManager_SourceLost;
        InteractionManager.SourcePressed += InteractionManager_SourcePressed;
        InteractionManager.SourceReleased += InteractionManager_SourceReleased;
    }

    void InteractionManager_SourceUpdated(InteractionSourceState state)
    {
        Vector3 handPosition;
        if (HandFlg == true)
        {
            if (state.source.kind == InteractionSourceKind.Hand && state.properties.location.TryGetPosition(out handPosition))
            {
                HandObject.transform.position = handPosition;
            }
        }
    }

    void InteractionManager_SourceLost(InteractionSourceState state)
    {
        HandFlg = false;
    }

    void InteractionManager_SourcePressed(InteractionSourceState state)
    {
        HandFlg = true;
    }

    void InteractionManager_SourceReleased(InteractionSourceState state)
    {
        HandFlg = false;
    }
}

ゲーム内のオブジェクトに作成したスクリプトを適用します。
その後、Sphere オブジェクトをドラック対象のオブジェクトとして設定します。
f:id:bluebirdofoz:20170829001641j:plain

アプリをビルドして hololens 上で動作してみます。
指刺す動作でドラッグ開始、離す動作でドラッグが終了します。
f:id:bluebirdofoz:20170829001652g:plain

視線ではなく手の動きを検知しているため、例えば、オブジェクトを掴んで移動して、好きなところに置くといった動作が自然にできます。
色々と活用できそうですね。