LeapMotion第2代 Unity示范代码(桌面开发)
一、官方地址:
官网:https://www.ultraleap.com/
驱动下载:https://leap2.ultraleap.com/downloads/leap-motion-controller-2/
docs地址:https://docs.ultraleap.com/xr-and-tabletop/tabletop/unity/getting-started/index.html
unity开发地址(Demo下载):https://docs.ultraleap.com/xr-and-tabletop/tabletop/unity/getting-started/index.html
unity开发代码:https://docs.ultraleap.com/xr-and-tabletop/xr/unity/plugin/features/scripting-fundamentals.html
二、安装驱动
地址:https://www.sogou.com/tx?ie=utf-8&query=<%2Fb>&hdq=sogou-addr-cc9657884708170e&sourceid=6_01_03
这样说明已经安装成功
三、安装TouchFree
Ultraleap的TouchFree软件使用手部跟踪数据来生成屏幕光标,用户可以无接触地控制该光标。这种非接触式手势控制允许与信息亭和数字显示器进行简单、直观和卫生的交互。
- 下载
- 安装完成后,进行配置
- 根据设备安放情况,选择
- 接着,把手指悬空点在绿色的圈圈上,并按下空格,这里需要2次这样操作
- 配置完成,看到手指已经有光标跟随了,手指往前戳下,代表点击
- 在小图标上,点击Start TouchFree,这样功能就永久启动起来了
touchfree模式,不用要开发,就可实验手势点击功能,由官方直接提供
四、Unity安装LeapMotion插件
https://docs.ultraleap.com/xr-and-tabletop/tabletop/unity/getting-started/index.html
- 安装包(能科学上网,速度更快)
- 打开Capsule Hands场景进行测试
- 测试出来手掌就成功了
五、Unity安装LeapMotion最基础开发
1. 识别哪只手进入
- 新建一个场景 LeapMotion
- 建立一个Service Provider Desktop
- 建立一个LeapMotion的cs脚本
using UnityEngine;
using Leap;public class LeapMotion : MonoBehaviour
{public LeapProvider leapProvider;private void OnEnable(){leapProvider.OnHandFound += OnHandFound;leapProvider.OnHandLost += OnHandLost;leapProvider.OnUpdateFrame += OnUpdateFrame;}private void OnDisable(){leapProvider.OnHandFound -= OnHandFound;leapProvider.OnHandLost -= OnHandLost;leapProvider.OnUpdateFrame -= OnUpdateFrame;}private void OnHandFound(Chirality hand){if (hand == Chirality.Left){Debug.Log("发现左手");}else if (hand == Chirality.Right) {Debug.Log("发现右手");}}private void OnHandLost(Chirality hand){if (hand == Chirality.Left){Debug.Log("左手消失");}else if (hand == Chirality.Right){Debug.Log("右手消失");}}void OnUpdateFrame(Frame frame){foreach (var hand in frame.Hands){if (hand.IsLeft){//获取左手Hand _leftHand = frame.GetHand(Chirality.Left);OnUpdateHand(_leftHand);}else if (hand.IsRight) {// 获取右手Hand _rightHand = frame.GetHand(Chirality.Right);OnUpdateHand(_rightHand);}}}void OnUpdateHand(Hand _hand){if (_hand.IsLeft){Debug.Log("左手");}else if (_hand.IsRight) {Debug.Log("右手");}}
}
- 建立一个LeapMotion对象,把脚本拖入场景,运行测试
该脚本识别出了哪只手进入或离开,并实时获取那只手,后面需要进一步获取手的更多信息
2. 获取手的位置与角度
- 五个指头的英文,Thumb大拇指,Index食指,Middle中指,Ring无名指,Pinky小指
Frames API:https://docs.ultraleap.com/api-reference/unity-api/class/class_leap_1_1_frame.html#class-Leap.Frame
Hand API:https://docs.ultraleap.com/api-reference/unity-api/class/class_leap_1_1_hand.html#class-Leap.Hand
Finger API:https://docs.ultraleap.com/api-reference/unity-api/class/class_leap_1_1_finger.html#class-Leap.Finger
- 获取代码
void OnUpdateFrame(Frame frame){foreach (var hand in frame.Hands){if (hand.IsLeft){//获取左手Hand _leftHand = frame.GetHand(Chirality.Left);OnUpdateHand(_leftHand);}else if (hand.IsRight) {// 获取右手Hand _rightHand = frame.GetHand(Chirality.Right);OnUpdateHand(_rightHand);}}}void OnUpdateHand(Hand _hand){Finger _index = _hand.GetFinger(Finger.FingerType.INDEX);if (_hand.IsLeft){Debug.Log("左手");}else if (_hand.IsRight) {Debug.Log("右手");}Debug.Log("手掌位置" + _hand.PalmPosition.ToString());Debug.Log("手掌角度" + _hand.PalmarAxis().ToString());Debug.Log("手指位置" + _index.TipPosition.ToString());Debug.Log("手指方向-向量" + _index.Direction.ToString());}
3. 获取手是否捏了下,拇指与食指触碰 和 获取是否握拳
LeapMotion对于捏和握拳有特别的检测器,分别是PinchDetector 捏检测器,GrabDetector 握拳检测器
- 建立一个检测器的对象,里面有左右手对象
- 在左右手里,分别加入检测器
- 代码:
using UnityEngine;
using Leap;public class LeapMotion : MonoBehaviour
{public LeapProvider leapProvider;public PinchDetector leftPinchDetector;public PinchDetector rightPinchDetector;public GrabDetector leftGrabDetector;public GrabDetector rightGrabDetector;private void OnEnable(){leapProvider.OnHandFound += OnHandFound;leapProvider.OnHandLost += OnHandLost;leapProvider.OnUpdateFrame += OnUpdateFrame;leftPinchDetector.onActionStart += LeftHandPinchStart;leftPinchDetector.onAction += LeftHandPinching;leftPinchDetector.onActionEnd += LeftHandPinchEnd;rightPinchDetector.onActionStart += RightHandPinchStart;rightPinchDetector.onAction += RightHandPinching;rightPinchDetector.onActionEnd += RightHandPinchEnd;leftGrabDetector.onActionStart += LeftHandGrapStart;leftGrabDetector.onActionEnd += LeftHandGrabEnd;rightGrabDetector.onActionStart += RightHandGrabStart;rightGrabDetector.onActionEnd += RightHandGrabEnd;}private void OnDisable(){leapProvider.OnHandFound -= OnHandFound;leapProvider.OnHandLost -= OnHandLost;leapProvider.OnUpdateFrame -= OnUpdateFrame;leftPinchDetector.onActionStart -= LeftHandPinchStart;leftPinchDetector.onAction -= LeftHandPinching;leftPinchDetector.onActionEnd -= LeftHandPinchEnd;rightPinchDetector.onActionStart -= RightHandPinchStart;rightPinchDetector.onAction -= RightHandPinching;rightPinchDetector.onActionEnd -= RightHandPinchEnd;leftGrabDetector.onActionStart -= LeftHandGrapStart;leftGrabDetector.onActionEnd -= LeftHandGrabEnd;rightGrabDetector.onActionStart -= RightHandGrabStart;rightGrabDetector.onActionEnd -= RightHandGrabEnd;}private void OnHandFound(Chirality hand){if (hand == Chirality.Left){Debug.Log("发现左手");}else if (hand == Chirality.Right){Debug.Log("发现右手");}}private void OnHandLost(Chirality hand){if (hand == Chirality.Left){Debug.Log("左手消失");}else if (hand == Chirality.Right){Debug.Log("右手消失");}}void OnUpdateFrame(Frame frame){foreach (var hand in frame.Hands){if (hand.IsLeft){//获取左手Hand _leftHand = frame.GetHand(Chirality.Left);OnUpdateHand(_leftHand);}else if (hand.IsRight) {// 获取右手Hand _rightHand = frame.GetHand(Chirality.Right);OnUpdateHand(_rightHand);}}}void OnUpdateHand(Hand _hand){Finger _index = _hand.GetFinger(Finger.FingerType.INDEX);if (_hand.IsLeft){Debug.Log("左手");}else if (_hand.IsRight){Debug.Log("右手");}Debug.Log("手掌位置" + _hand.PalmPosition.ToString());Debug.Log("手掌角度" + _hand.PalmarAxis().ToString());Debug.Log("手指位置" + _index.TipPosition.ToString());Debug.Log("手指方向-向量" + _index.Direction.ToString());}private void RightHandGrabEnd(Hand hand){Debug.Log("Right 抓拳");}private void RightHandGrabStart(Hand hand){Debug.Log("Right 抓拳放开");}private void LeftHandGrabEnd(Hand hand){Debug.Log("Left 抓拳");}private void LeftHandGrapStart(Hand hand){Debug.Log("Left 抓拳放开");}private void RightHandPinchEnd(Hand hand){Debug.Log("Right 捏放开");}private void RightHandPinching(Hand hand){Debug.Log("Right 捏住移动中");}private void RightHandPinchStart(Hand hand){Debug.Log("Righ 捏住");}private void LeftHandPinchEnd(Hand hand){Debug.Log("Left 捏放开");}private void LeftHandPinching(Hand hand){Debug.Log("Left 捏住移动中");}private void LeftHandPinchStart(Hand hand){Debug.Log("Left 捏住");}
}
六、结束语
2代的LeapMotion对长距离传输信号更加友好,好像也提供了大面积手势识别的融合方案,感谢一起学习
源代码:https://download.csdn.net/download/qq_17523181/90379338