1:
using Leap;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Leap.Unity;
public class ControlAnimation : MonoBehaviour
{
private Frame frame;
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; //旋转初始位置值
[Tooltip ("Velocity (m/s) move toward ")]//速度(m/s)走向
const float smallestVelocity = 0.1f;
[Tooltip("Velocity (m/s) move toward ")]
protected float deltaVelocity = 0.000001f;
const float deltaCloseFinger = 0.06f;
[Tooltip("Delta degree to check 2 vectors same direction")]//三角度检查2个向量的方向相同
protected float handForwardDegree = 30;
private float deltaAngleThumb = 90;
void Start()
{
provider = FindObjectOfType<LeapProvider>() as LeapProvider;
}
void Update()
{
//Scale();
//Rotation();
//Position();
frame = provider.CurrentFrame;
if (frame.Hands.Count > 0)
{
foreach (var item in frame.Hands)
{
//Debug.Log(item.Fingers.Count + "@" + item.IsLeft+"#"+item.IsRight);
if (item.IsLeft)
{
//isThumbDirection(item, Vector3.up);
Debug.Log(isThumbDirection(item, item.GetPinchPosition()));
}
}
}
}
/// <summary>
/// 缩小
/// </summary>
public void Scale()
{
Frame frame = provider.CurrentFrame;
foreach (Hand hand in frame.Hands)
{
if (isOpenFullHand(hand))
{
Debug.Log("大");
Vector3 value = transform.localScale;
value += new Vector3(value.x * 0.01f, value.y * 0.01f, value.z * 0.01f);
// Debug.Log(value);
transform.localScale = value;
}
if (isCloseHand(hand))
{
Debug.Log("小");
Vector3 value = transform.localScale;
value -= new Vector3(value.x * 0.01f, value.y * 0.01f, value.z * 0.01f);
// Debug.Log(value);
transform.localScale = value;
}
}
}
/// <summary>
/// 旋转
/// </summary>
public void Rotation()
{
Frame frame = provider.CurrentFrame;
foreach (Hand hand in frame.Hands)
{
if (hand.IsLeft || hand.IsRight)
{
Vector3 value = transform.localEulerAngles;
value = new Vector3(hand.PalmPosition.y * rotate_sensitive + rotate_initial_value, hand.PalmPosition.x * rotate_sensitive + rotate_initial_value, 0);
transform.localEulerAngles = value;
}
else
{
hand.PalmPosition.y = transform.localEulerAngles.x;
hand.PalmPosition.x = transform.localEulerAngles.y;
}
}
}
public void Position()
{
Frame frame = provider.CurrentFrame;
foreach (Hand hand in frame.Hands)
{
if (hand.IsLeft || hand.IsRight)
{
if (isMoveLeft(hand))
{
transform.localPosition = new Vector3(hand.PalmPosition.y * displacement_sensitive, 0, 0) + transform.localPosition;
}
if (isMoveRight(hand))
{
transform.localPosition = new Vector3(-hand.PalmPosition.y * displacement_sensitive, 0, 0) + transform.localPosition;
}
}
}
}
protected bool isMoveRight(Hand hand)// 手划向右边
{
return hand.PalmVelocity.x > deltaVelocity && !isStationary(hand);
}
protected bool isMoveLeft(Hand hand) // 手划向左边
{
//x轴移动的速度 deltaVelocity = 0.7f isStationary (hand) 判断hand是否禁止
return hand.PalmVelocity.x < -deltaVelocity && !isStationary(hand);
}
protected bool isMoveUp(Hand hand) //手向上
{
return hand.PalmVelocity.y > deltaVelocity && !isStationary(hand);
}
protected bool isMoveDown(Hand hand) //手向下
{
return hand.PalmVelocity.y < -deltaVelocity && !isStationary(hand);
}
protected bool isGrabHand(Hand hand) //是否抓取
{
return hand.GrabStrength > 0.8f; //抓取力
}
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[f];
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;
}
/// <summary>
/// 向量转化成 角度
/// </summary>
/// <param name="a"></param>
/// <param name="b"></param>
/// <returns></returns>
protected float angle2LeapVectors(Leap.Vector a, Leap.Vector b)
{
return Vector3.Angle(UnityVectorExtension.ToVector3(a), UnityVectorExtension.ToVector3(b));
}
/// <summary>
/// 判断两个向量是否 相同 方向
/// </summary>
/// <param name="a"></param>
/// <param name="b"></param>
/// <returns></returns>
protected bool isSameDirection(Vector a, Vector b)
{
return angle2LeapVectors(a, b) < handForwardDegree;
//Debug.Log (angle2LeapVectors (a, b) + " " + b);
}
/// <summary>
/// 判断两个向量是否 相反 方向
/// </summary>
/// <param name="a"></param>
/// <param name="b"></param>
/// <returns></returns>
protected bool isOppositeDirection(Vector a, Vector b)
{
return angle2LeapVectors(a, b) > (180 - handForwardDegree);
}
/// <summary>
/// 判断手的掌心方向于一个 向量 是否方向相同
/// </summary>
/// <param name="hand"></param>
/// <param name="dir"></param>
/// <returns></returns>
protected bool isPalmNormalSameDirectionWith(Hand hand, Vector3 dir)
{
return isSameDirection(hand.PalmNormal, UnityVectorExtension.ToVector(dir));
}
/// <summary>
/// 向手掌的方向 移动
/// </summary>
/// <param name="hand"></param>
/// <returns></returns>
protected bool isHandMoveForward(Hand hand)
{
return isSameDirection(hand.PalmNormal, hand.PalmVelocity) && !isStationary(hand);
}
/// <summary>
/// 手掌是否垂直(掌心水平)
/// </summary>
/// <param name="hand"></param>
/// <returns></returns>
protected bool checkPalmNormalInXZPlane(Hand hand) // hand.PalmNormal 垂直于掌心的向量
{
float anglePalmNormal = angle2LeapVectors(hand.PalmNormal, UnityVectorExtension.ToVector(Vector3.up));
return (anglePalmNormal > 70 && anglePalmNormal < 110);
}
/// <summary>
/// 判断大拇指是否竖直或向下
/// </summary>
/// <param name="hand"></param>
/// <param name="dir"></param>
/// <returns></returns>
protected bool isThumbDirection(Hand hand, Vector3 dir)
{
List<Finger> listOfFingers = hand.Fingers;
for (int f = 0; f < listOfFingers.Count; f++)
{
Finger finger = listOfFingers[f];
if (finger.Type == Finger.FingerType.TYPE_THUMB)
{
float angleThumbFinger = angle2LeapVectors(finger.Direction,
UnityVectorExtension.ToVector(dir));
float angleThumbFinger2 = angle2LeapVectors(
finger.TipPosition - hand.PalmPosition, UnityVectorExtension.ToVector(dir));
//Debug.Log (angleThumbFinger + "@" + angleThumbFinger2);
if (angleThumbFinger < deltaAngleThumb
|| angleThumbFinger2 < deltaAngleThumb)
return true;
else
return false;
}
}
return false;
}
/// <summary>
/// 判断四指是否靠拢掌心
/// </summary>
/// <param name="hand"></param>
/// <returns></returns>
protected 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 < deltaCloseFinger)
{
if (finger.Type == Finger.FingerType.TYPE_THUMB)
{
return false;
}
else
{
count++;
}
}
}
//Debug.Log (count);
return (count == 4);
}
}
2:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Leap;
using Leap.Unity;
public class LeapGestures : MonoBehaviour
{
public static bool Gesture_left = false;
public static bool Gesture_right = false;
public static bool Gesture_up = false;
public static bool Gesture_down = false;
public static bool Gesture_zoom = false;
public static float movePOs = 0.0f;
private LeapProvider mProvider;
private Frame mFrame;
private Hand mHand;
private Vector leftPosition;
private Vector rightPosition;
public static float zoom = 1.0f;
[Tooltip("Velocity (m/s) of Palm ")]
public float smallestVelocity = 1.45f;//手掌移动的最小速度
[Tooltip("Velocity (m/s) of Single Direction ")]
[Range(0, 1)]
public float deltaVelocity = 1.0f;//单方向上手掌移动的速度
// Use this for initialization
void Start()
{
mProvider = FindObjectOfType<LeapProvider>() as LeapProvider;
}
// Update is called once per frame
void Update()
{
mFrame = mProvider.CurrentFrame;
//获取当前帧
//获得手的个数
//print ("hand num are " + mFrame.Hands.Count);
if (mFrame.Hands.Count > 0)
{
if (mFrame.Hands.Count == 2)
zoom = CalcuateDistance(mFrame);
if (mFrame.Hands.Count == 1)
LRUDGestures(mFrame, ref movePOs);
}
}
float CalcuateDistance(Frame mFrame)
{
Gesture_zoom = true;
Gesture_left = false;
Gesture_right = false;
float distance = 0f;
//print ("Two hands");
foreach (var itemHands in mFrame.Hands)
{
if (itemHands.IsLeft)
{
leftPosition = itemHands.PalmPosition;
//print ("leftPosition" + leftPosition);
}
if (itemHands.IsRight)
{
rightPosition = itemHands.PalmPosition;
//print ("rightPosition" + rightPosition);
}
}
if (leftPosition != Vector.Zero && rightPosition != Vector.Zero)
{
Vector3 leftPos = new Vector3(leftPosition.x, leftPosition.y, leftPosition.z);
Vector3 rightPos = new Vector3(rightPosition.x, rightPosition.y, rightPosition.z);
distance = 10 * Vector3.Distance(leftPos, rightPos);
print("distance" + distance);
}
if (distance != 0)
return distance;
else
return distance = 1;
}
void LRUDGestures(Frame mFrame, ref float movePOs)
{
Gesture_zoom = false;
foreach (var item in mFrame.Hands)
{
int numFinger = item.Fingers.Count;
//print ("item is " + numFinger);
//print("hand are " + isOpenFullHand (item));
// print ("isOpenFullHands is " + isOpenFullHands(item));
if (item.GrabStrength == 1)
{
//print ("num is 0, gestures is woquan");
}
else if (item.GrabStrength == 0)
{
//print ("num is 5, open your hand");
//print("PalmVelocity" + item.PalmVelocity);
//print("PalmPosition" + item.PalmPosition);
movePOs = item.PalmPosition.x;
if (isMoveLeft(item))
{
Gesture_left = true;
Gesture_right = false;
print("move left");
}
else if (isMoveRight(item))
{
Gesture_left = false;
Gesture_right = true;
print("move Right");
}
else if (isMoveUp(item))
{
Gesture_left = false;
Gesture_right = false;
print("move Up");
}
else if (isMoveDown(item))
{
Gesture_left = false;
Gesture_right = false;
print("move Down");
}
else if (isMoveForward(item))
{
Gesture_left = false;
Gesture_right = false;
print("move Forward");
}
else if (isMoveBack(item))
{
Gesture_left = false;
Gesture_right = false;
print("move back");
}
}
}
}
private bool isStone(Hand hand)
{
//print ("hand.GrabAngle" + hand.GrabAngle);
return hand.GrabAngle > 2.0f;
}
//是否抓取
public bool isGrabHand(Hand hand)
{
return hand.GrabStrength > 0.8f; //抓取力
}
//hand move four direction
public bool isMoveRight(Hand hand)
{
return hand.PalmVelocity.x > deltaVelocity && !isStationary(hand);
}
// 手划向右边
public bool isMoveLeft(Hand hand)
{
//print (hand.PalmVelocity.x );
return hand.PalmVelocity.x < -deltaVelocity && !isStationary(hand);
}
//手向上
public bool isMoveUp(Hand hand)
{
//print ("hand.PalmVelocity.y" + hand.PalmVelocity.y);
return hand.PalmVelocity.y > deltaVelocity && !isStationary(hand);
}
//手向下
public bool isMoveDown(Hand hand)
{
return hand.PalmVelocity.y < -deltaVelocity && !isStationary(hand);
}
//手向前
public bool isMoveForward(Hand hand)
{
//print (hand.PalmVelocity.z);
return hand.PalmVelocity.z > deltaVelocity && !isStationary(hand);
}
//手向后
public bool isMoveBack(Hand hand)
{
return hand.PalmVelocity.z < -deltaVelocity && !isStationary(hand);
}
//固定不动的
public bool isStationary(Hand hand)
{
return hand.PalmVelocity.Magnitude < smallestVelocity; //Vector3.Magnitude返回向量的长度
}
}