夜風のMixedReality

xRと出会って変わった人生と出会った技術を書き残すためのGeekなHoloRangerの居場所

HoloLens 2のHandTrackingのJoint(手の関節)検知して利用する。

本日はHoloLens 2のHandTrakcing調査枠です。

HoloLens 2ではHandTrackingによってユーザーの手を検知することができます。

f:id:Holomoto-Sumire:20200805214630p:plain

片手25点+手全体の軸などが検知できます。

今回はこのJointの情報を取得します。

〇左右の手の検知

次のスクリプトでは手、正確には手のジョイントを検知するスクリプトです。

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Microsoft.MixedReality.Toolkit.Utilities;
using Microsoft.MixedReality.Toolkit.Input;
/// <summary>
/// This class detects the HoloLens 2's HandTracking joints.
/// </summary>
public class HandDetector : MonoBehaviour
{

    [SerializeField, HeaderAttribute("DetectTargetHand")]
    HandMode HandDetectMode;

    Handedness handednesstype;

    enum HandMode
    {
        RightHand,
        LeftHand,
        BothHand,
    }

    // Start is called before the first frame update
    void Start()
    {
        //DetectRighitHandWrist
        if ((int)HandDetectMode == 0)
        {
            handednesstype = Handedness.Right;
        }
        //DetectLeftHandWrist
        if ((int)HandDetectMode ==1)
        {
            handednesstype = Handedness.Left;
        }
        //DetectBothHandWrist
        if ((int)HandDetectMode == 2)
        {
            handednesstype = Handedness.Both;
        }
        Debug.Log(handednesstype);
    }

    // Update is called once per frame
    void Update()
    {
        //DetectRighitHandWrist
        if (HandJointUtils.TryGetJointPose(TrackedHandJoint.Wrist, handednesstype, out MixedRealityPose pose))
        {
                       Debug.Log("Detect");
               Debug.Log(pose);
        }
    }
}

enum形で右、左、両方のどれかをinspectorウィンドウで指定しています。

if (HandJointUtils.TryGetJointPose(TrackedHandJoint.Wrist, handednesstype, out MixedRealityPose pose))
        {
        }

[HandJointUtils.TryGetJointPose]メソッドは指定された[Handness]を持つオブジェクトから要求されたジョイントのポーズを取得します。

[TrackedHandJoint joint],[Handedness handedness],[out MixedRealityPose pose]の3つの引数を持ちそれぞれ次のようになります。

・TrackedHandJoint

追跡される手のジョイントを指します。

microsoft.github.io

・Handedness

Handedness は、enum形で右手、左手、その他、など現在使用しているコントローラーを(HoloLensの場合手)指定します。

microsoft.github.io

・MixedRealityPose

MixedRealityPoseは引数にTransform(vector3,Quaternion)を持ちます。

スクリプトの使い方

①MRTKを導入したシーンにからオブジェクトを作成して[HandDetector]コンポーネントをアタッチします。

f:id:Holomoto-Sumire:20201017143406j:plain

②[HandDetectMode]に検知したい手を指定します。

f:id:Holomoto-Sumire:20201017143450j:plain

③この状態でエディタを実行します。手が検知されると手の[Wrist]オブジェクトのTransform情報がデバッグログに返されます。

f:id:Holomoto-Sumire:20201017143810j:plain

以上でHoloLens 2でHandTrackingのJointが検知できました。