예제 #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);
        }
예제 #2
0
 public override void Start()
 {
     angle = QuaternionHelper.ToEulerAngles(transform.localRotation);
 }
예제 #3
0
        /// <summary>
        /// Get the structure for what is expected in this output method.
        /// </summary>
        /// <returns></returns>
        public override NUISkeleton GetTargetStructure()
        {
            // Define the skeletal structure
            SkeletonStructure structure = new SkeletonStructure();

            structure.SetRootJoint(NUIJointType.SpineBase);
            structure.AddBone(ColladaToNUIJointMapping("SpineMid"), ColladaToNUIJointMapping("SpineBase"));
            structure.AddBone(ColladaToNUIJointMapping("SpineShoulder"), ColladaToNUIJointMapping("SpineMid"));
            structure.AddBone(ColladaToNUIJointMapping("Neck"), ColladaToNUIJointMapping("SpineShoulder"));
            structure.AddBone(ColladaToNUIJointMapping("Head"), ColladaToNUIJointMapping("Neck"));

            structure.AddBone(ColladaToNUIJointMapping("ShoulderLeft"), ColladaToNUIJointMapping("SpineShoulder"));
            structure.AddBone(ColladaToNUIJointMapping("ElbowLeft"), ColladaToNUIJointMapping("ShoulderLeft"));
            structure.AddBone(ColladaToNUIJointMapping("WristLeft"), ColladaToNUIJointMapping("ElbowLeft"));
            structure.AddBone(ColladaToNUIJointMapping("HandLeft"), ColladaToNUIJointMapping("WristLeft"));
            structure.AddBone(ColladaToNUIJointMapping("HandTipLeft"), ColladaToNUIJointMapping("HandLeft"));
            structure.AddBone(ColladaToNUIJointMapping("ThumbLeft"), ColladaToNUIJointMapping("HandLeft"));

            structure.AddBone(ColladaToNUIJointMapping("ShoulderRight"), ColladaToNUIJointMapping("SpineShoulder"));
            structure.AddBone(ColladaToNUIJointMapping("ElbowRight"), ColladaToNUIJointMapping("ShoulderRight"));
            structure.AddBone(ColladaToNUIJointMapping("WristRight"), ColladaToNUIJointMapping("ElbowRight"));
            structure.AddBone(ColladaToNUIJointMapping("HandRight"), ColladaToNUIJointMapping("WristRight"));
            structure.AddBone(ColladaToNUIJointMapping("HandTipRight"), ColladaToNUIJointMapping("HandRight"));
            structure.AddBone(ColladaToNUIJointMapping("ThumbRight"), ColladaToNUIJointMapping("HandRight"));

            structure.AddBone(ColladaToNUIJointMapping("HipLeft"), ColladaToNUIJointMapping("SpineBase"));
            structure.AddBone(ColladaToNUIJointMapping("KneeLeft"), ColladaToNUIJointMapping("HipLeft"));
            structure.AddBone(ColladaToNUIJointMapping("AnkleLeft"), ColladaToNUIJointMapping("KneeLeft"));
            structure.AddBone(ColladaToNUIJointMapping("FootLeft"), ColladaToNUIJointMapping("AnkleLeft"));

            structure.AddBone(ColladaToNUIJointMapping("HipRight"), ColladaToNUIJointMapping("SpineBase"));
            structure.AddBone(ColladaToNUIJointMapping("KneeRight"), ColladaToNUIJointMapping("HipRight"));
            structure.AddBone(ColladaToNUIJointMapping("AnkleRight"), ColladaToNUIJointMapping("KneeRight"));
            structure.AddBone(ColladaToNUIJointMapping("FootRight"), ColladaToNUIJointMapping("AnkleRight"));

            // Define the skeleton in Unity terms.
            NUISkeleton skeleton = new NUISkeleton(structure);

            // Get the hip right and the chest right.
            Vector3 hipRightTranslation      = rigData.GetJoint("HipRight").LHSWorldTransformationMatrix.GetColumn(3);
            Vector3 hipLeftTranslation       = rigData.GetJoint("HipLeft").LHSWorldTransformationMatrix.GetColumn(3);
            Vector3 shoulderRightTranslation = rigData.GetJoint("ShoulderRight").LHSWorldTransformationMatrix.GetColumn(3);
            Vector3 shoulderLeftTranslation  = rigData.GetJoint("ShoulderLeft").LHSWorldTransformationMatrix.GetColumn(3);

            Vector3 hipRight   = hipRightTranslation - hipLeftTranslation;
            Vector3 chestRight = shoulderRightTranslation - shoulderLeftTranslation;

            skeleton.SpineBaseRight = rigData.GetJoint("SpineBase").LHSWorldTransformationMatrix.inverse.MultiplyVector(hipRight);
            skeleton.ChestRight     = rigData.GetJoint("SpineMid").LHSWorldTransformationMatrix.inverse.MultiplyVector(chestRight);

            foreach (KeyValuePair <string, ColladaJointData> jointData in rigData.JointData)
            {
                NUIJointType jointType = ColladaToNUIJointMapping(jointData.Key);
                NUIJoint     joint     = new NUIJoint(jointType);

                // Convert the Collada Joint rotation from RHS to LHS (Unity)
                ColladaJointData currentJoint = jointData.Value;
                joint.Position                  = currentJoint.Translation;
                joint.Rotation                  = QuaternionHelper.RHStoLHS(currentJoint.RotationVector);
                joint.TransformationMatrix      = currentJoint.LHSTransformationMatrix;
                joint.WorldTransformationMatrix = currentJoint.LHSWorldTransformationMatrix;

                Vector3 directionToChild = Vector3.zero;

                if (!structure.IsJointAnExtremity(jointType)) // directionToChild is not needed for extremeties.
                {
                    NUIJointType childType = structure.GetChildJoint(jointType);
                    if (childType != NUIJointType.Unspecified)
                    {
                        Vector3 child  = rigData.GetJoint(NUIJointToColladaMapping(childType)).LHSWorldTransformationMatrix.GetColumn(3);
                        Vector3 parent = currentJoint.LHSWorldTransformationMatrix.GetColumn(3);
                        directionToChild = child - parent;
                    }
                }

                if (jointType == NUIJointType.SpineBase) // The Hip is a special case.
                {
                    Vector3 rightHipWorldPosition = rigData.GetJoint("HipRight").LHSWorldTransformationMatrix.GetColumn(3);
                    Vector3 leftHipWorldPosition  = rigData.GetJoint("HipLeft").LHSWorldTransformationMatrix.GetColumn(3);
                    Vector3 hipWorldPosition      = rigData.GetJoint("SpineBase").LHSWorldTransformationMatrix.GetColumn(3);

                    directionToChild = ((rightHipWorldPosition + leftHipWorldPosition) / 2F) - hipWorldPosition;

                    //float angle = Vector3.Angle(Vector3.up, directionToChild);
                    //directionToChild = Quaternion.AngleAxis(40 - angle, hipRight) * directionToChild;
                }

                joint.directionToChild = currentJoint.LHSWorldTransformationMatrix.inverse.MultiplyVector(directionToChild);

                skeleton.Joints.Add(jointType, joint);
            }
            skeleton.ChestRight -= Vector3.Project(skeleton.ChestRight, skeleton.Joints[NUIJointType.SpineMid].directionToChild);

            return(skeleton);
        }
 public void NormalizeAngleLessThanZero()
 {
     Assert.That(QuaternionHelper.NormaliseAngle(-10), Is.EqualTo(350));
 }
 private static void ApplyToChild(Transform visible, Transform network, ref Vector3 positionVelocity, ref Quaternion rotationVelocity)
 {
     visible.localPosition = Vector3.SmoothDamp(visible.localPosition, network.localPosition, ref positionVelocity, SmoothTime);
     visible.localRotation = QuaternionHelper.SmoothDamp(visible.localRotation, network.localRotation, ref rotationVelocity, SmoothTime);
 }
예제 #6
0
    public void LookAt(Vector3 pos, Vector3 up)
    {
        Vector3 dir = (pos - position).Normalized() * 100;

        rotation = QuaternionHelper.LookRotation(dir, up);
    }
예제 #7
0
    void UpdateAngVelocity()
    {
        Quaternion rotOffset = QuaternionHelper.Euler(angVelocity * AngSpeed * Time.fixedDeltaTime);

        transform.rotation *= rotOffset;
    }