unity+leapMotion⼿势识别⼊门教程
CheckFingerOpenToHand()⽅法是我根据原作者的⽅法改的,因为⽤他的⽅法感觉这个⽅法不通⽤,本⼈很懒,为了以后的幸福⽣活
就⼩改了⼀下,如果你想判断某个⼿指是否打开(0~5个⼿指),你可以定义这样⼀个数组,如:
我想判断⾷指跟中指打开:就可以下⾯这样写,再将这个数组传进⽅法⾥,他会给你返回⼀个bool 值,哈哈
开⼼,⽜逼,⾃⼰觉得这个⽅法写的很实⽤,想判断⼏个⼿指打开都⾏
Finger.FingerType[] arr = { Finger.FingerType.TYPE_INDEX, Finger.FingerType.TYPE_MIDDLE };
using System.Collections;
using System.Collections.Generic;
using Leap;
using Leap.Unity;
using UnityEngine;
unity 教程public class JudgeSwipe : MonoBehaviour {
public HandModelBase leftHandModel;
[Tooltip("Velocity (m/s) move toward ")]
protected float deltaVelocity = 0.7f;
//这⾥传进来你要打开的⼿指紧握⼿指 {} 传⼀个⼿指{Finger.FingerType.TYPE_RING}...以此类推,当传进5个值得时候代表⼿张开,当传进0个值的时候代表握⼿ Finger.FingerType[] arr = { Finger.FingerType.TYPE_INDEX, Finger.FingerType.TYPE_MIDDLE };
// Update is called once per frame
void Update() {
if (!leftHandModel.IsTracked) return;
Hand leftHand = leftHandModel.GetLeapHand();
//if (IsMoveLeft(leftHand))
//{
// print("左⼿向左滑动");
//}
//if (IsMoveRight(leftHand))
//{
// print("左⼿向右滑动");
//}
//if (IsMoveUp(leftHand))
//{
// print("左⼿向上滑动");
/
/}
//if (IsMoveDown(leftHand))
//{
// print("左⼿向下滑动");
//}
//if (IsCloseHand(leftHand))
//{
// print("握拳");
//}
//if (IsOpenFullHand(leftHand))
//{
/
/ print("张⼿");
//}
//if (CheckFingerCloseToHand(leftHand))
//{
// print("四指指向掌⼼");
//}
if (CheckFingerOpenToHand(leftHand,arr))
{
print("ok");
print("ok");
}
}
/// <summary>
/// 这个⽅法⽤来扩展哪⼏个⼿指打开,这⾥传进来你要判断是否打开的⼿指紧握⼿指 {} 传⼀个⼿指{Finger.FingerType.TYPE_RING}... /// 以此类推,当传进5个值得时候代表⼿张开,当传进0个值的时候代表握⼿
/// </summary>
/// <param name="hand"></param>
/// <param name="arr"></param>
/// <returns></returns>
bool CheckFingerOpenToHand(Hand hand, Finger.FingerType[] fingerTypesArr,float deltaCloseFinger = 0.05f)
{
List<Finger> listOfFingers = hand.Fingers;
float count = 0;
// 遍历5个⼿指
for (int f = 0; f < listOfFingers.Count; f++)
{
Finger finger = listOfFingers[f];
// 判读每个⼿指的指尖位置和掌⼼位置的长度是不是⼩于某个值,以判断⼿指是否贴着掌⼼
if ((finger.TipPosition - hand.PalmPosition).Magnitude < deltaCloseFinger)
{
// 如果传进来的数组长度是0,有⼀个⼿指那么 count + 1,continue 跳出,不执⾏下⾯数组长度不是0 的逻辑
if (fingerTypesArr.Length == 0)
{
count++;
continue;
}
// 传进来的数组长度不是 0,
for (int i = 0; i < fingerTypesArr.Length; i++)
{
// 假如本例⼦传进来的是⾷指和中指,逻辑⾛到这⾥,如果你的⾷指是紧握的,下⾯会判断这个⼿指是不是⾷指,返回 false if (finger.Type == fingerTypesArr[i])
{
return false;
}
else
{
count++;
}
}
}
}
if (fingerTypesArr.Length == 0)
{
return count == 5;
}
/
/ 这⾥除以length 是因为上⾯数组在每次 for 循环 count ++ 会执⾏ length 次
return (count/ fingerTypesArr.Length == 5 - fingerTypesArr.Length);
}
/// <summary>
/// 判断是否抓取
/// </summary>
/// <param name="hand"></param>
/// <returns></returns>
bool isGrabHand(Hand hand)
{
return hand.GrabStrength > 0.8f;
}
/// <summary>
/// 判断是不是握拳
/// </summary>
/// <param name="hand"></param>
/// <returns></returns>
/// <returns></returns>
bool IsCloseHand(Hand hand)
{
List<Finger> listOfFingers = hand.Fingers;
int count = 0;
for (int f = 0; f < listOfFingers.Count; f++)
{
Finger finger = listOfFingers[f];
if ((finger.TipPosition - hand.PalmPosition).Magnitude < 0.05f) {
count++;
}
}
return (count == 4);
}
/// <summary>
/// 判断⼿指是否全张开
/
// </summary>
/// <param name="hand"></param>
/// <returns></returns>
bool IsOpenFullHand(Hand hand)
{
return hand.GrabStrength == 0;
}
/// <summary>
/// ⼿滑向左边
/// </summary>
/// <param name="hand"></param>
/
// <returns></returns>
protected bool IsMoveLeft(Hand hand) // ⼿划向左边
{
//x轴移动的速度 deltaVelocity = 0.7f
return hand.PalmVelocity.x < -deltaVelocity;
}
/// <summary>
/// ⼿滑向右边
/// </summary>
/// <param name="hand"></param>
/// <returns></returns>
protected bool IsMoveRight(Hand hand)
{
return hand.PalmVelocity.x > deltaVelocity;
}
/// <summary>
/// ⼿滑向上边
/// </summary>
/// <param name="hand"></param>
/// <returns></returns>
protected bool IsMoveUp(Hand hand)
{
return hand.PalmVelocity.y > deltaVelocity;
}
/// <summary>
/// ⼿滑向下边
/// </summary>
/// <param name="hand"></param>
/// <returns></returns>
protected bool IsMoveDown(Hand hand)
{
return hand.PalmVelocity.y < -deltaVelocity;
}
}
/// <summary>
/// 判断四指是否靠向掌⼼
/// </summary>
/// <param name="hand"></param>
/// <returns></returns>
bool CheckFingerCloseToHand(Hand hand)
{
List<Finger> listOfFingers = hand.Fingers;
int count = 0;
for (int f = 0; f < listOfFingers.Count; f++)
{
Finger finger = listOfFingers[f];
if ((finger.TipPosition - hand.PalmPosition).Magnitude < 0.05f) {
if (finger.Type == Finger.FingerType.TYPE_THUMB)
{
return false;
}
else
{
count++;
}
}
}
return (count == 4);
}
}
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论