RhinoFreak 发表于 2022-4-28 13:34

LeapMotion在unity中保姆级使用教程


一、插件

1、下载资源包,包括:Core为核心引擎,Interaction Engine为实现虚拟物体交互的插件,hands提供手势渲染等。
Ultraleap Plugin for Unity — Ultraleap for Developers (leapmotion.com)

https://developer.leapmotion.com/unity


2、unity中导入
3、安装Magic Leap XR Plugin


4、准备完成

二、场景创建

1、在自己工程的场景需要添加以下,一个个说


位置如下


2、对于LeapHanController模块,其中的Hand Model Manager这样添,该拖的物体拖进来


这个脚本能选择使用的是AR还是电脑


3、 就这么简单,基本环境就搭建好了。

三、使用方法

1、创建一个脚本,挂载在场景里
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

//引用
using Leap;
using Leap.Unity;

public class HandControl : MonoBehaviour
{
    LeapProvider provider;
    public HandModelBase leftHandModel;//左手
    public HandModelBase rightHandModel;//右手
    private const float rotate_sensitive = 1500f;//旋转灵敏度
    private const float displacement_sensitive = 0.015f; //位移灵敏度
    private const float rotate_initial_value = 0f;//旋转初始位置值
    float shake = 0;

    /// <summary>
    /// 判断条件尽量勿动
    /// </summary>
    const float smallestVelocity = 0.1f;
    const float deltaVelocity = 0.000001f;
    const float deltaCloseFinger = 0.06f;

    void Start()
    {
      provider = FindObjectOfType<LeapProvider>() as LeapProvider;
    }
    void Update()
    {
      jjj();
    }
    public void jjj()
    {
      Frame frame = provider.CurrentFrame;
      foreach (Hand hand in frame.Hands)
      {
            if (hand.IsLeft && !hand.IsRight)
            {
                if (isOpenFullHand(hand))
                {
                  //Debug.Log("检测到手啦");
                  if (isMoveRight(hand))
                  {
                        shake += Time.deltaTime;//计手移动的时间
                        if (shake > 0.8f)
                        {
                            shake = 0;//时间大于*f后,时间置零,避免一直触发
                            Debug.Log("左掌向右");
                        }
                  }
                }
                if (isCloseHand(hand))
                {
                  if (isMoveRight(hand))
                  {
                        Debug.Log("左拳向右");
                  }
                }

            }
      }
    }
    /// <summary>
    /// 定义手势的基础类型
    /// </summary>
    /// <param name="hand"></param>
    /// <returns></returns>
    protected bool isMoveRight(Hand hand)// 手划向右
    {
      return hand.PalmVelocity.x > deltaVelocity && !isStationary(hand);//x,y,z控制三维轴,±控制轴方向
    }


    protected bool isMoveLeft(Hand hand)   // 手划向左
    {
      //x轴移动的速度   deltaVelocity = 0.7f    isStationary (hand)判断hand是否禁止
      return hand.PalmVelocity.x < -deltaVelocity && !isStationary(hand);
    }
    protected bool isMoveup(Hand hand)   // 手划向上
    {
      //x轴移动的速度   deltaVelocity = 0.7f    isStationary (hand)判断hand是否禁止
      return hand.PalmVelocity.y < deltaVelocity && !isStationary(hand);
    }
    protected bool isMovedown(Hand hand)   // 手划向下
    {
      //x轴移动的速度   deltaVelocity = 0.7f    isStationary (hand)判断hand是否禁止
      return hand.PalmVelocity.y < -deltaVelocity && !isStationary(hand);
    }

    protected bool isStationary(Hand hand)// 固定不动的
    {
      return hand.PalmVelocity.Magnitude < smallestVelocity;
    }

    protected bool isCloseHand(Hand hand)   //是否握拳
    {
      List<Finger> listOfFingers = hand.Fingers;
      int count = 0;
      for (int f = 0; f < listOfFingers.Count; f++)
      { //循环遍历所有的手~~
            Finger finger = listOfFingers;
            if ((finger.TipPosition - hand.PalmPosition).Magnitude < deltaCloseFinger)    // Magnitude向量的长度 。是(x*x+y*y+z*z)的平方根。    //float deltaCloseFinger = 0.05f;
            {
                count++;
                //if (finger.Type == Finger.FingerType.TYPE_THUMB)
                //Debug.Log ((finger.TipPosition - hand.PalmPosition).Magnitude);
            }
      }
      return (count == 5);
    }
    protected bool isOpenFullHand(Hand hand)         //手掌全展开~
    {
      //Debug.Log (hand.GrabStrength + " " + hand.PalmVelocity + " " + hand.PalmVelocity.Magnitude);
      return hand.GrabStrength == 0;
    }
}其它手势基础,可以参考这个:
LeapMotion初步学习_alnh9788的博客-CSDN博客

https://blog.csdn.net/alnh9788/article/details/101463386
LEAPMotion VR 各种手势的判断~_miccall的博客-CSDN博客_leapmotion 手势识别

https://blog.csdn.net/qq_31411825/article/details/54773801Leapmotion 左右上下前后挥动手势设计,动态手势_moonlightpeng的博客-CSDN博客

https://blog.csdn.net/moonlightpeng/article/details/80191468?utm_medium=distribute.pc_relevant.none-task-blog-2~default~baidujs_baidulandingword~default-1.no_search_link&spm=1001.2101.3001.4242.2 判断每个手指的动作,参考这个:
unity + leapMotion 手势识别入门教程__Afra 的博客-CSDN博客_leapmotion手势识别

https://blog.csdn.net/qq_39097425/article/details/84027730?spm=1001.2101.3001.6650.1&utm_medium=distribute.pc_relevant.none-task-blog-2~default~CTRLIST~default-1.essearch_pc_relevant&depth_1-utm_source=distribute.pc_relevant.none-task-blog-2~default~CTRLIST~default-1.essearch_pc_relevant
2、就可以用了,Debug的一些语句就能触发了
3、 控制捏动作的脚本


这个圈圈判断是否捏住


4、抓取物体
在需要交互的物体上加上Rigidbody和Interaction Behaviour两个组件
页: [1]
查看完整版本: LeapMotion在unity中保姆级使用教程