AR/VR/MR

互动开发-(四)LeapMotion-Unity主要类

2017-05-15  本文已影响0人  元宇宙协会

命名空间

One : Leap.Unity

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

Two : Leap

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

pro = FindObjectOfType<LeapProvider> () as LeapProvider;
Frame frame = pro.CurrentFrame;

或者

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

        Frame frame = pro.CurrentFrame;

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

            }

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

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

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

        // 获取手指个数
        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骨头对象表示手指的一段,并包含位置、大小和方位数据。

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中的坐标都需要转一下:

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);
上一篇下一篇

猜你喜欢

热点阅读