Example #1
0
        void Update()
        {
            int NewHands = Leap.GetHandCount(), OldHands = Hands.Length;

            if (NewHands != OldHands)
            {
                for (int i = NewHands; i < OldHands; ++i)
                {
                    Destroy(Hands[i].Hand.gameObject);
                    Destroy(Hands[i].JointMat);
                }
                Array.Resize(ref Hands, NewHands);
                for (int i = OldHands; i < NewHands; ++i)
                {
                    Hands[i].Hand = new GameObject().transform;
                    Hands[i].Hand.SetParent(gameObject.transform, false);
                    Hands[i].Hand.localScale = new Vector3(.001f, .001f, .001f);
                }
            }
            for (int i = 0; i < NewHands; ++i)
            {
                Hand     h        = Leap.RawFrame.Hands[i];
                Material JointMat = Hands[i].JointMat ? Hands[i].JointMat : Hands[i].JointMat = new Material(Shader.Find("Standard"))
                {
                    color = Color.HSVToRGB(Random.value, 1, 1)
                };
                Transform    Palm = AddSphere(Hands[i].Hand, 0, h.PalmPosition, h.PalmWidth * .2f, JointMat);
                GrabbingPalm Grabber;
                if (Grabber = Palm.gameObject.GetComponent <GrabbingPalm>())
                {
                    Grabber.HandID = i;
                }
                else
                {
                    Palm.gameObject.AddComponent <GrabbingPalm>().HandID = i;
                }
                int     ObjectID    = 1;
                Vector3 PalmForward = Vector3.zero;
                foreach (Finger f in h.Fingers)
                {
                    foreach (Bone b in f.bones)
                    {
                        AddSphere(Hands[i].Hand, ObjectID++, b.Basis.translation, b.Width, JointMat);
                        if (b.Type != Bone.BoneType.TYPE_DISTAL)
                        {
                            AddCylinder(Hands[i].Hand, ObjectID++, b.Basis.translation, b.Center, b.Width * .5f);
                        }
                        if (b.Type == Bone.BoneType.TYPE_METACARPAL)
                        {
                            PalmForward += new Vector3(b.Direction.x, b.Direction.y, b.Direction.z);
                        }
                    }
                }
                Palm.rotation = Quaternion.LookRotation(PalmForward, new Vector3(h.PalmNormal.x, h.PalmNormal.y, h.PalmNormal.z));
            }
        }