Example #1
0
        public ColladaAnimationData GetColladaAnimation(NUIHumanoidAnimation animation)
        {
            NUISkeleton outputStructure = GetTargetStructure();

            ColladaAnimationData colladaAnimation = new ColladaAnimationData(rigData);
            List <float>         elapsedTimes     = new List <float>();

            Vector3 startingPosition    = Vector3.zero;
            Vector3 rigStartingPosition = rigData.GetJoint("SpineBase").Translation;


            int frameRate = 0;

            switch (selectedFrameRateIndex)
            {
            case 0:
                frameRate = 30;
                break;

            case 1:
                frameRate = 60;
                break;
            }

            NUIHumanoidAnimation animationConstrained = animation.ConstrainFramerate(frameRate);

            for (int k = 0; k < animationConstrained.Keyframes.Count; k++)
            {
                NUIAnimationKeyframe keyframe = animationConstrained.Keyframes[k];

                elapsedTimes.Add(keyframe.ElapsedTime);

                ColladaRigData   currentRig  = new ColladaRigData();
                ColladaJointData parentJoint = rigData.GetJoint("SpineBase");
                currentRig.Add(parentJoint.Id, parentJoint);

                foreach (KeyValuePair <string, ColladaJointData> jointData in rigData.JointData)
                {
                    ColladaJointData colladaJointData = rigData.GetJoint(jointData.Key);

                    if (colladaJointData.Id == "SpineBase")
                    {
                        Vector3 hipPosition = keyframe.Skeleton.Joints[NUIJointType.SpineBase].Position * 100;
                        hipPosition.x *= -1;
                        if (startingPosition == Vector3.zero)
                        {
                            startingPosition = hipPosition;
                        }
                        colladaJointData.Translation = (hipPosition - startingPosition) + rigStartingPosition;
                    }
                    colladaAnimation.jointTranslateX[jointData.Key] += string.Format(cultureUS, "{0} ", colladaJointData.Translation.x);
                    colladaAnimation.jointTranslateY[jointData.Key] += string.Format(cultureUS, "{0} ", colladaJointData.Translation.y);
                    colladaAnimation.jointTranslateZ[jointData.Key] += string.Format(cultureUS, "{0} ", colladaJointData.Translation.z);
                }

                foreach (KeyValuePair <NUIJointType, NUIJoint> kvp in keyframe.Skeleton.Joints)
                {
                    // For parent joints
                    Quaternion rotation = kvp.Value.Rotation;

                    string           id = NUIJointToColladaMapping(kvp.Key);
                    ColladaJointData colladaJointData = rigData.GetJoint(id);

                    if (!outputStructure.Structure.IsJointAnExtremity(kvp.Key))
                    {
                        Vector3 revert    = QuaternionHelper.ToEulerAnglesXYZ(rotation);
                        Vector3 corrected = new Vector3(revert.x, -revert.y, -revert.z);

                        colladaAnimation.jointRotateX[id] += string.Format(cultureUS, "{0} ", corrected.x);
                        colladaAnimation.jointRotateY[id] += string.Format(cultureUS, "{0} ", corrected.y);
                        colladaAnimation.jointRotateZ[id] += string.Format(cultureUS, "{0} ", corrected.z);

                        Matrix4x4 transformation = Matrix4x4.TRS(colladaJointData.Translation, QuaternionHelper.FromEulerAnglesXYZ(corrected), Vector3.one);
                        colladaAnimation.jointValues[id] += string.Format(cultureUS, "{0} ", transformation.ToString());
                    }
                    else
                    {
                        // Extremeties
                        colladaAnimation.jointRotateX[id] += string.Format(cultureUS, "{0} ", colladaJointData.RotationVector.x);
                        colladaAnimation.jointRotateY[id] += string.Format(cultureUS, "{0} ", colladaJointData.RotationVector.y);
                        colladaAnimation.jointRotateZ[id] += string.Format(cultureUS, "{0} ", colladaJointData.RotationVector.z);

                        Matrix4x4 transformation = Matrix4x4.TRS(colladaJointData.Translation, colladaJointData.Rotation, Vector3.one);
                        colladaAnimation.jointValues[id] += string.Format(cultureUS, "{0} ", transformation.ToString());
                    }
                }
            }

            colladaAnimation.frameTimelapse = elapsedTimes;
            return(colladaAnimation);
        }