LoginSignup
10
13

More than 5 years have passed since last update.

UnityでLeapMotionから手のデータを取得する

Posted at

LeapMotionで手の情報を取得

UnityでLeapMotionを使用して手の情報を取得する方法について説明します。
以下の記事を参考にしました。

[Unity] Leapmotion Orionで手のモデルの位置を簡単に取得する

Unityのバージョン

以下の通りです。

  • Unity 2018.1.0f2 Personal(64bit)

準備

UnityでLeapMotionを使用するためのパッケージをLeapMotionの公式サイトからダウンロードしてください。
ダウンロードしたパッケージファイルをUnityにインポートしてください

インポートするパッケージファイルとして、自分は以下のファイルを使用しました。
Leap_Motion_Core_Assets_ 4.4.0.unitypackage

UnityでLeapMotionを使う

Unityを起動してパッケージファイルをインポートしたら、アセットからLeapHandControllerをヒエラルキーにドラッグしてください。
2019-01-26.png

Leap Service ProviderHand Model Managerがインスペクターにあると思います。
バージョンが新しくなって使えない可能性もあるので、それはご了承ください。

手の情報を取得

以下のスクリプトで取得できます。

using UnityEngine;
using System.Collections;
using Leap;
using Leap.Unity;
using System.Collections.Generic;
using UnityEngine.UI;

public class VisibleBallHand : MonoBehaviour
{

    [SerializeField]
    GameObject m_ProviderObject;

    LeapServiceProvider m_Provider;

    public Text scoreText; // 取得した手のトラッキングデータをUIに表示

    public GameObject Apple;

    void Start()
    {
        m_Provider = m_ProviderObject.GetComponent<LeapServiceProvider>();
    }

    void Update()
    {
        Frame frame = m_Provider.CurrentFrame;

        // 右手と左手を取得する
        Hand rightHand = null;
        Hand leftHand = null;
        foreach (Hand hand in frame.Hands)
        {
            if (hand.IsRight)
            {
                rightHand = hand;
            }
            if (hand.IsLeft)
            {
                leftHand = hand;
            }
        }

        if (rightHand == null && leftHand == null)
        {
            return;
        }

        Vector3 right_normal;
        Vector3 right_direction;
        Vector3 right_position;
        Vector3 left_normal;
        Vector3 left_direction;
        Vector3 left_position;
        float distance;

        if (rightHand != null && leftHand != null)
        {
            right_normal = rightHand.PalmNormal.ToVector3();
            right_direction = rightHand.Direction.ToVector3();
            right_position = rightHand.PalmPosition.ToVector3();
            left_normal = leftHand.PalmNormal.ToVector3();
            left_direction = leftHand.Direction.ToVector3();
            left_position = leftHand.PalmPosition.ToVector3();
            distance = Vector3.Distance(right_position, left_position);
            scoreText.text = "右手の法線ベクトル: " + right_normal + "\n" +
                             "右手の方向ベクトル: " + right_direction + "\n" +
                             "右手の位置ベクトル: " + right_position + "\n" +
                             "左手の法線ベクトル: " + left_normal + "\n" +
                             "左手の方向ベクトル: " + left_direction + "\n" +
                             "左手の位置ベクトル: " + left_position + "\n" +
                             "内積: " + Vector3.Dot(right_normal, left_normal) + "\n" +
                             "中点: " + Vector3.Lerp(right_position, left_position, 0.5f) + "\n" +
                             "2点間の距離: " + distance;
        }

        if (rightHand != null && leftHand == null)
        {
            right_normal = rightHand.PalmNormal.ToVector3();
            right_direction = rightHand.Direction.ToVector3();
            right_position = rightHand.PalmPosition.ToVector3();
            scoreText.text = "右手の法線ベクトル: " + right_normal + "\n" +
                             "右手の方向ベクトル: " + right_direction + "\n" +
                             "右手の位置ベクトル: " + right_position;
        }
        if (rightHand == null && leftHand != null)
        {
            left_normal = leftHand.PalmNormal.ToVector3();
            left_direction = leftHand.Direction.ToVector3();
            left_position = leftHand.PalmPosition.ToVector3();
            scoreText.text = "左手の法線ベクトル: " + left_normal + "\n" +
                             "左手の方向ベクトル: " + left_direction + "\n" +
                             "左手の位置ベクトル: " + left_position;
        }
    }
}

これによりLeapMotionで手の情報を取得して操作することができます。
取得した情報を使えばこんなこともできます。

10
13
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
13