leapmotion 定义识别手势改变物体颜色

1,加预制体。


2. 脚本

LeapGestures.cs

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 = 10.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");

				}
			}
		}
	}
	//判断有多少个手指是伸开的,以及伸开的手指是哪个
	public int extendFingerNum(Hand hand)
	{
		int extendedFingers = 0;
		//一个手指的一次遍历
		for (int f = 0; f < hand.Fingers.Count; f++)
		{
			Finger digit = hand.Fingers[f];
			if (digit.IsExtended) {
				print ("Fingers is " + digit);
				extendedFingers++;
			}
		}
		return extendedFingers;
	}




	//是否抓取
	public bool isGrabHand (Hand hand)  		
	{
		return hand.GrabStrength > 0.8f;    	//抓取力 
	}


	//hand move four direction
	public bool isMoveRight (Hand hand)   	
	{
		//print (hand.PalmVelocity.x);
		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)   	
	{
		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返回向量的长度
	}


}




CubeChange.cs

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class CubeChange : MonoBehaviour {

	private bool isSure = true;

	// Use this for initialization
	void Start () {
		
		
	}
	
	// Update is called once per frame
	void Update () {

		if (LeapGestures.Gesture_left) {
			ColorGreen ();
			MoveCube (LeapGestures.movePOs);
		} else if (LeapGestures.Gesture_right) {
			ColorRed ();
		} else if (LeapGestures.Gesture_up) {
			ColorWihte ();
		} else if (LeapGestures.Gesture_down) {
			ColorBlue ();
		} else if (LeapGestures.Gesture_zoom) {
			if (true) {
				ZoomCube (LeapGestures.zoom);
			}
		}
		
	}


	void MoveCube (float move_x)
	{
		float pos_x = transform.localPosition.x;
		float pos_y = transform.localPosition.y;
		float pos_z = transform.localPosition.z;

	}

	void ZoomCube (float scale)
	{
		scale *= Mathf.PI;
		this.transform.localScale = new Vector3(scale, scale, scale);

	}

	void ColorGreen()
	{
		this.GetComponent<MeshRenderer> ().material.color = Color.green;
		//this.transform.localScale = 
	}
	void ColorRed()
	{
		this.GetComponent<MeshRenderer> ().material.color = Color.red;

	}
	void ColorWihte ()
	{
		this.GetComponent<MeshRenderer> ().material.color = Color.white;

	}
	void ColorBlue()
	{
		this.GetComponent<MeshRenderer> ().material.color = Color.blue;

	}
}

hand.grabAngle()返回的是除却大拇指四根手指的平均弯曲程度,所以紧握拳头的时候数值为3.14左右,手全部张开是的时候基本也为0。一般使用0为一个手势,>2为一个手势,其余为一个手势。

所有⼿指的弯曲程度。通过观察四个⼿指的⽅向和⼿的⽅向之间的夹⻆来计算这个⻆。当计算⻆度时,不考虑拇指。这个⻆度是⼀个开着的⼿的0弧度,当这个姿势是⼀个紧的拳头时,它就会到达pi弧度。

猜你喜欢

转载自blog.csdn.net/moonlightpeng/article/details/80185959
今日推荐