前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >AR开发-(四)LeapMotion-Unity主要类

AR开发-(四)LeapMotion-Unity主要类

作者头像
孙寅
发布2020-06-02 16:52:51
8670
发布2020-06-02 16:52:51
举报
文章被收录于专栏:宜达数字宜达数字
命名空间
One : Leap.Unity

using Leap.Unity 命名空间下的类,一般在Unity中要引用它才能与物体进行交互,获取跟踪数据。 ** LeapServiceProvider** 访问跟踪(手势轨迹)数据,听说还转换右手>左手坐标系 ** LeapHandController** 负责管理手的控制器,并处理与游戏对象的交互。

Two : Leap

using Leap Leap空间中的类定义了LeapMotion所追踪的具体内容。 Frame 某个时间点的数据集合,包含(hand)手对象. 可以通过

代码语言:javascript
复制
pro = FindObjectOfType<LeapProvider> () as LeapProvider;
Frame frame = pro.CurrentFrame;

或者

代码语言:javascript
复制
public LeapServiceProvider spro;
spro = FindObjectOfType<LeapServiceProvider> ();
Frame f = spro.CurrentFrame;
        foreach (Hand hand in f.Hands) {
            // 如果是左手
            if (hand.IsRight) {
                // 如果物体的距离与左手的距离小于1F
                if (Vector3.Distance (this.transform.position, hand.PalmPosition.ToVector3 ()) < 1f) {
                    // 将手的位置与旋转赋值给当前物体的位置或旋转
                    transform.position = hand.PalmPosition.ToVector3 () + hand.PalmNormal.ToVector3 () * (transform.localScale.y * .5f + .02f);
                    transform.rotation = hand.Basis.CalculateRotation ();
                }
            }

Arm 胳膊而已,用处不大,不过可以在特定位置进行相关的开发。例如将角色动画的胳膊给它赋值。做一个游戏中的提线布偶

代码语言:javascript
复制
        Frame frame = pro.CurrentFrame;

        foreach (Hand h in frame.Hands) {
            if (h.Arm != null) {
                transform.rotation = h.Arm.Rotation.ToQuaternion();

            }

Hand 一个Hand手对象表示了一个手,一个手包含5根手指

代码语言:javascript
复制
                // 手的方向
                Debug.Log ("1:"+h.Direction.ToVector3 ());
                // 手掌的位置
                Debug.Log ("2:" + h.PalmPosition.ToVector3 ());
                // 拿到的是Leap的transform
                Debug.Log ("3:" +h.Basis.rotation);

Finger Finger手指对象表示了追踪的手指。手指包含四块骨头。

代码语言:javascript
复制
        // 获取手指个数
        Debug.Log (h.Fingers.Count);
        foreach (Finger i in h.Fingers) {
                    Debug.Log (i.Type);
                    if (i.Type == Finger.FingerType.TYPE_INDEX) {
                        Debug.Log ("食指");
                    }
                    if (i.Type == Finger.FingerType.TYPE_THUMB) {
                        Debug.Log ("拇指");
                    }
                    if (i.Type == Finger.FingerType.TYPE_MIDDLE) {
                        Debug.Log ("中指");
                    }
                    if (i.Type == Finger.FingerType.TYPE_PINKY) {
                        Debug.Log ("小拇指");
                    }
                    if (i.Type == Finger.FingerType.TYPE_RING) {
                        Debug.Log ("无名指");
                    }
                    if (i.Type == Finger.FingerType.TYPE_UNKNOWN) {
                        Debug.Log ("六指");
                    }
                }

Bone Bone骨头对象表示手指的一段,并包含位置、大小和方位数据。

代码语言:javascript
复制
if (i.Type == Finger.FingerType.TYPE_MIDDLE) {
                        Debug.Log ("中指");
                        Debug.Log("中指有多少截" + i.bones.Length);
}

               Debug.Log (h.Fingers.Count);
                foreach (Finger i in h.Fingers) {
                    Debug.Log (i.Type);
                    if (i.Type == Finger.FingerType.TYPE_INDEX) {
                        //Debug.Log ("食指");
                        Debug.Log ("食指有多少截" + i.bones.Length);
                        //Debug.Log ("位置" + i.bones [0].Type);
                        foreach (Bone ss in i.bones) {
                            Debug.Log (ss);
                            if (ss.Type == Bone.BoneType.TYPE_DISTAL) {
                                Debug.Log ("DISTAL");
                                //Debug.Log (ss.Direction.ToVector3 ());
                                // 通过测试这是用来查看当前手指骨骼的角度(偏航角)
                                Debug.Log (ss.Direction.Yaw);
                                Debug.Log (" X:" +ss.Direction.x);

                            }
                        }
                    }

最后说一下,我们在通过上面的代码可以知道,leap中的坐标都需要转一下:

代码语言:javascript
复制
Vector3 v = leapVector.ToVector3();
Quaternion q = leapMatrix.Rotation();
Vector3 t = leapMatrix.origin.ToVector3();
Vector3 scale = new Vector3(leapMatrix.xBasis.Magnitude,
                                 leapMatrix.yBasis.Magnitude,
                                 leapMatrix.zBasis.Magnitude);
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 命名空间
    • One : Leap.Unity
      • Two : Leap
      领券
      问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档