예제 #1
0
        public override void UpdateHand()
        {
            // Register the hand tracker
            HandTracker tracker = GameObject.FindGameObjectWithTag("hand_tracker").GetComponent <HandTracker>();

            tracker.SetSphereMesh(_sphereMesh);
            if (_spherePositions == null || _spherePositions.Length != TOTAL_JOINT_COUNT)
            {
                _spherePositions = new Vector3[TOTAL_JOINT_COUNT];
            }

            if (_sphereMat == null)
            {
                _sphereMat           = new Material(_material);
                _sphereMat.hideFlags = HideFlags.DontSaveInEditor;
            }

            //Update all joint spheres in the fingers
            foreach (var finger in _hand.Fingers)
            {
                for (int j = 0; j < 4; j++)
                {
                    int key = getFingerJointIndex((int)finger.Type, j);

                    Vector3 position = finger.Bone((Bone.BoneType)j).NextJoint.ToVector3();
                    _spherePositions[key] = position;

                    //drawSphere(position)
                    tracker.RegisterSphere(System.String.Format("finger_{0}", key), position, _jointRadius, handedness, transform.lossyScale.x, drawSphere(position));
                }
            }

            //Now we just have a few more spheres for the hands
            //PalmPos, WristPos, and mockThumbJointPos, which is derived and not taken from the frame obj

            Vector3 palmPosition = _hand.PalmPosition.ToVector3();

            //drawSphere(palmPosition, _palmRadius)
            tracker.RegisterSphere("palm", palmPosition, _palmRadius, handedness, transform.lossyScale.x, drawSphere(palmPosition, _palmRadius));

            this.palm_position = palmPosition;

            float pitch = -_hand.Direction.Pitch;
            float yaw   = -_hand.Direction.Yaw;
            float roll  = _hand.PalmNormal.Roll;

            this.palm_rotation = new Vector3(pitch, yaw, roll) * 180.0f / Mathf.PI;

            Vector3 thumbBaseToPalm   = _spherePositions[THUMB_BASE_INDEX] - _hand.PalmPosition.ToVector3();
            Vector3 mockThumbJointPos = _hand.PalmPosition.ToVector3() + Vector3.Reflect(thumbBaseToPalm, _hand.Basis.xBasis.ToVector3());

            //drawSphere(mockThumbJointPos)
            tracker.RegisterSphere("thumb", mockThumbJointPos, _jointRadius, handedness, transform.lossyScale.x, drawSphere(mockThumbJointPos));

            //If we want to show the arm, do the calculations and display the meshes
            if (_showArm)
            {
                var arm = _hand.Arm;

                Vector3 right = arm.Basis.xBasis.ToVector3() * arm.Width * 0.7f * 0.5f;
                Vector3 wrist = arm.WristPosition.ToVector3();
                Vector3 elbow = arm.ElbowPosition.ToVector3();

                float armLength = Vector3.Distance(wrist, elbow);
                wrist -= arm.Direction.ToVector3() * armLength * 0.05f;

                Vector3 armFrontRight = wrist + right;
                Vector3 armFrontLeft  = wrist - right;
                Vector3 armBackRight  = elbow + right;
                Vector3 armBackLeft   = elbow - right;

                //drawSphere(armFrontRight)
                //drawSphere(armFrontLeft)
                //drawSphere(armBackLeft)
                //drawSphere(armBackRight)
                tracker.RegisterSphere("arm_front_right", armFrontRight, _jointRadius, handedness, transform.lossyScale.x, drawSphere(armFrontRight));
                tracker.RegisterSphere("arm_front_left", armFrontLeft, _jointRadius, handedness, transform.lossyScale.x, drawSphere(armFrontLeft));
                tracker.RegisterSphere("arm_back_left", armBackLeft, _jointRadius, handedness, transform.lossyScale.x, drawSphere(armBackLeft));
                tracker.RegisterSphere("arm_back_right", armBackRight, _jointRadius, handedness, transform.lossyScale.x, drawSphere(armBackRight));

                //drawCylinder(armFrontLeft, armFrontRight);
                //drawCylinder(armBackLeft, armBackRight);
                //drawCylinder(armFrontLeft, armBackLeft);
                //drawCylinder(armFrontRight, armBackRight);
                tracker.RegisterCylinder("bone_arm_front_lr", armFrontLeft, armFrontRight, handedness, gameObject.layer, transform.lossyScale.x, transform.lossyScale.y, drawCylinder(armFrontLeft, armFrontRight));
                tracker.RegisterCylinder("bone_arm_back_lr", armBackLeft, armBackRight, handedness, gameObject.layer, transform.lossyScale.x, transform.lossyScale.y, drawCylinder(armBackLeft, armBackRight));
                tracker.RegisterCylinder("bone_arm_fb_ll", armFrontLeft, armBackLeft, handedness, gameObject.layer, transform.lossyScale.x, transform.lossyScale.y, drawCylinder(armFrontLeft, armBackLeft));
                tracker.RegisterCylinder("bone_arm_fb_rr", armFrontRight, armBackRight, handedness, gameObject.layer, transform.lossyScale.x, transform.lossyScale.y, drawCylinder(armFrontRight, armBackRight));
            }

            //Draw cylinders between finger joints
            for (int i = 0; i < 5; i++)
            {
                for (int j = 0; j < 3; j++)
                {
                    int keyA = getFingerJointIndex(i, j);
                    int keyB = getFingerJointIndex(i, j + 1);

                    Vector3 posA = _spherePositions[keyA];
                    Vector3 posB = _spherePositions[keyB];

                    //drawCylinder(posA, posB);
                    tracker.RegisterCylinder(System.String.Format("finger_cyl_{0}_{1}", keyA, keyB), posA, posB, handedness, gameObject.layer, transform.lossyScale.x, transform.lossyScale.y, drawCylinder(posA, posB));
                }
            }

            //Draw cylinders between finger knuckles
            for (int i = 0; i < 4; i++)
            {
                int keyA = getFingerJointIndex(i, 0);
                int keyB = getFingerJointIndex(i + 1, 0);

                Vector3 posA = _spherePositions[keyA];
                Vector3 posB = _spherePositions[keyB];

                //drawCylinder(posA, posB);
                tracker.RegisterCylinder(System.String.Format("knuckle_{0}_{1}", keyA, keyB), posA, posB, handedness, gameObject.layer, transform.lossyScale.x, transform.lossyScale.y, drawCylinder(posA, posB));
            }

            //Draw the rest of the hand
            //drawCylinder(mockThumbJointPos, THUMB_BASE_INDEX);
            tracker.RegisterCylinder("thumb_base", mockThumbJointPos, _spherePositions[THUMB_BASE_INDEX], handedness, gameObject.layer, transform.lossyScale.x, transform.lossyScale.y, drawCylinder(mockThumbJointPos, THUMB_BASE_INDEX));
            //drawCylinder(mockThumbJointPos, PINKY_BASE_INDEX);
            tracker.RegisterCylinder("pinky_base", mockThumbJointPos, _spherePositions[PINKY_BASE_INDEX], handedness, gameObject.layer, transform.lossyScale.x, transform.lossyScale.y, drawCylinder(mockThumbJointPos, PINKY_BASE_INDEX));
        }