软件
Unity2018.3.5f1
Leap_Motion_Core_Assets_4.4.0.unitypackage
Tip
如果不想显示手模型,只往场景中拖入一个LeapHandController就够了。
效果图
LeapDebuger.cs
using System.Collections;
using System.Collections.Generic;
using Leap;
using Leap.Unity;
using UnityEngine;
using UnityEngine.UI;
public class LeapDebuger : MonoBehaviour
{
public LeapProvider LeapDataProvider;
public HandModelBase leftHandModel;
public HandModelBase rightHandModel;
public Text tipText;
public RectTransform cursorImage;
void Start()
{
if (LeapDataProvider == null)
{
LeapDataProvider = FindObjectOfType<LeapProvider>();
if (LeapDataProvider == null || !LeapDataProvider.isActiveAndEnabled)
{
Debug.LogError("似乎没有找到LeapMotion连接");
enabled = false;
return;
}
}
}
Frame mFrame;
string tip = "";
string handName = "";
Vector2 handPos;
void Update()
{
mFrame = LeapDataProvider.CurrentFrame;
tip = "\n手的数量: " + mFrame.Hands.Count;
handPos = Vector2.zero;
for (int i = 0; i < mFrame.Hands.Count; i++)
{
Hand hand = mFrame.Hands[i];
handName = hand.IsLeft ? "左手" : "右手";
Vector3 pos = hand.GetPalmPose().position;
handPos = new Vector2(pos.x, pos.y);
tip += "\n\n [" + handName + "] : " + hand.ToString() + " 掌宽: " + hand.PalmWidth.ToString("f3");
tip += " 位置:(" + pos.x.ToString("f3") + "," + pos.y.ToString("f3") + "," + pos.z.ToString("f3") + ")";
tip += "\n 四指抓握:" + IsHandGrab(hand);
tip += " 握拳:" + IsHandFist(hand);
tip += " 张开:" + IsHandOpenFull(hand);
tip += " 静止:" + IsHandMotionless(hand);
tip += "\n 两指捏合: " + IsHandPinch(hand);
tip += " PinchDistance: " + (int)hand.PinchDistance;
tip += " PinchStrength: " + hand.PinchStrength;
}
cursorImage.anchoredPosition = new Vector2(handPos.x.Map(-0.15f, 0.15f, 0, 1920), handPos.y.Map(0.1f, 0.3f, 0, 1080));
Ray ray = Camera.main.ScreenPointToRay(new Vector3(cursorImage.anchoredPosition.x, cursorImage.anchoredPosition.y, 0));
RaycastHit rayCastHit;
if (Physics.Raycast(ray, out rayCastHit, 1000))
{
if (rayCastHit.collider.gameObject != null)
{
tip += "\n\n物体触发:" + rayCastHit.collider.gameObject.name;
}
Debug.DrawLine(ray.origin, rayCastHit.point, Color.red, 0.1f);
}
tipText.text = tip;
}
public bool IsLeftHandTracked()
{
return leftHandModel.IsTracked;
}
public bool IsRightHandTracked()
{
return rightHandModel.IsTracked;
}
public bool IsHandGrab(Hand hand)
{
return hand.GrabStrength > 0.8f;
}
public bool IsHandOpenFull(Hand hand)
{
return hand.GrabStrength < 0.1f;
}
public bool IsHandPinch(Hand hand)
{
return hand.IsPinching();
}
public bool IsHandFist(Hand hand)
{
return hand.GetFistStrength() > 0.8f;
}
public bool IsHandMotionless(Hand hand)
{
return hand.PalmVelocity.Magnitude < 0.02f;
}
bool CheckFingerOpenToHand(Hand hand, Finger.FingerType[] fingerTypesArr, float deltaCloseFinger = 0.05f)
{
List<Finger> listOfFingers = hand.Fingers;
float count = 0;
for (int f = 0; f < listOfFingers.Count; f++)
{
Finger finger = listOfFingers[f];
if ((finger.TipPosition - hand.PalmPosition).Magnitude < deltaCloseFinger)
{
if (fingerTypesArr.Length == 0)
{
count++;
continue;
}
for (int i = 0; i < fingerTypesArr.Length; i++)
{
if (finger.Type == fingerTypesArr[i])
{
return false;
}
else
{
count++;
}
}
}
}
if (fingerTypesArr.Length == 0)
{
return count == 5;
}
return (count / fingerTypesArr.Length == 5 - fingerTypesArr.Length);
}
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);
}
}
加入新媒体交互微信群,分享更多Unity开发技能