Ejemplo n.º 1
0
        /// <summary>
        /// Try to apply animation to model.
        /// </summary>
        public void Apply(SkinnedMesh skinnedMesh)
        {
            skinnedMesh.AnimationSpeed = animationSpeed;
            for (int i = 0; i < orientations.Count; i++)
            {
                SJoint joint = skinnedMesh.GetAllJoints()[i];
                for (int j = 0; j < positions[i].Count; j++)
                {
                    var poskey = skinnedMesh.AddPositionKey(joint);
                    poskey.Position = positions[i][j];
                    poskey.Frame    = positionsKeyframes[i][j];
                }

                for (int j = 0; j < orientations[i].Count; j++)
                {
                    var rotkey = skinnedMesh.AddRotationKey(joint);
                    rotkey.Rotation = orientations[i][j];
                    rotkey.Frame    = orientKeyframes[i][j];
                }

                for (int j = 0; j < scales[i].Count; j++)
                {
                    var scalekey = skinnedMesh.AddScaleKey(joint);
                    scalekey.Scale = scales[i][j];
                    scalekey.Frame = scalesKeyframes[i][j];
                }
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Try to get all root joints.
        /// </summary>
        public static List <SJoint> GetRootJoints(SkinnedMesh mesh)
        {
            List <SJoint> roots = new List <SJoint>();

            List <SJoint> allJoints = mesh.GetAllJoints();

            for (int i = 0; i < allJoints.Count; i++)
            {
                bool   isRoot      = true;
                SJoint testedJoint = allJoints[i];
                for (int j = 0; j < allJoints.Count; j++)
                {
                    SJoint testedJoint2 = allJoints[j];
                    for (int k = 0; k < testedJoint2.Children.Count; k++)
                    {
                        if (testedJoint.Equals(testedJoint2.Children[k]))
                        {
                            isRoot = false;
                        }
                    }
                }
                if (isRoot)
                {
                    roots.Add(testedJoint);
                }
            }

            return(roots);
        }
Ejemplo n.º 3
0
 /// <summary>
 /// Returns an array of serializable joints from matching position and rotation arrays.
 /// </summary>
 /// <param name="jointPositions"> Unity Vector3 array of the positions of the joints. </param>
 /// <param name="jointRotations"> Unity Quaternion array of the rotations of the joints. </param>
 /// <returns> An array of serilizable joints. </returns>
 public static SJoint[] SerializeJoints(Vector3[] jointPositions, Quaternion[] jointRotations)
 {
     SJoint[] joints = new SJoint[jointPositions.Length];
     for (int i = 0; i < jointPositions.Length; i++)
     {
         joints[i] = new SJoint(jointPositions[i], jointRotations[i]);
     }
     return(joints);
 }
Ejemplo n.º 4
0
 /// <summary>
 /// Returns an array of serializable joints from a GameObject array.
 /// </summary>
 /// <param name="jointObjects"> Unity GameObject array of joints. </param>
 /// <returns> An array of serilizable joints. </returns>
 public static SJoint[] SerializeJoints(GameObject[] jointObjects)
 {
     SJoint[] joints = new SJoint[jointObjects.Length];
     for (int i = 0; i < jointObjects.Length; i++)
     {
         joints[i] = new SJoint(jointObjects[i]);
     }
     return(joints);
 }
Ejemplo n.º 5
0
        /// <summary>
        /// Try to compute global matrix.
        /// </summary>
        public static void ComputeGlobal(SkinnedMesh mesh, SJoint joint)
        {
            SJoint parent = GetRealParent(mesh, joint);

            if (parent != null)
            {
                joint.GlobalMatrix = parent.GlobalMatrix * joint.LocalMatrix;
            }
            else
            {
                joint.GlobalMatrix = joint.LocalMatrix;
            }

            for (int i = 0; i < joint.Children.Count; ++i)
            {
                ComputeGlobal(mesh, joint.Children[i]);
            }
        }
Ejemplo n.º 6
0
        /// <summary>
        /// Try to get real parent.
        /// </summary>
        public static SJoint GetRealParent(SkinnedMesh mesh, SJoint joint)
        {
            List <SJoint> allJoints = mesh.GetAllJoints();

            for (int j = 0; j < allJoints.Count; j++)
            {
                SJoint testedJoint = allJoints[j];
                for (int k = 0; k < testedJoint.Children.Count; k++)
                {
                    if (joint == testedJoint.Children[k])
                    {
                        return(testedJoint);
                    }
                }
            }

            return(null);
        }
Ejemplo n.º 7
0
        /// <summary>
        /// Try to apply skeleton to model.
        /// </summary>
        public void Apply(SkinnedMesh skinnedMesh)
        {
            // Create the bones
            for (int i = 0; i < meshSkeleton.nbBones; i++)
            {
                string boneName = meshSkeleton.names[i];
                if (skinnedMesh.GetJointIndex(boneName) == -1)
                {
                    var joint = skinnedMesh.AddJoint();
                    joint.Name = boneName;
                }
            }

            // Set the hierarchy
            for (int i = 0; i < meshSkeleton.nbBones; i++)
            {
                short parent = meshSkeleton.parentIdx[i];
                if (parent != -1) // root
                {
                    var parentJoint = RenderSkeleton.GetJointByName(skinnedMesh, meshSkeleton.names[parent]);
                    if (parentJoint != null)
                    {
                        parentJoint.AddChildren(skinnedMesh.GetAllJoints()[i]);
                    }
                }
            }

            // Set the transformations
            for (int i = 0; i < meshSkeleton.nbBones; i++)
            {
                string boneName = meshSkeleton.names[i];

                var joint = RenderSkeleton.GetJointByName(skinnedMesh, boneName);
                if (joint == null)
                {
                    continue;
                }

                joint.LocalMatrix = meshSkeleton.matrix[i];

                joint.Animatedposition = meshSkeleton.positions[i];
                joint.Animatedrotation = meshSkeleton.rotations[i];
                joint.Animatedscale    = meshSkeleton.scales[i];
            }

            // Compute the global matrix
            List <SJoint> roots = RenderSkeleton.GetRootJoints(skinnedMesh);

            for (int i = 0; i < roots.Count; ++i)
            {
                RenderSkeleton.ComputeGlobal(skinnedMesh, roots[i]);
            }

            // The matrix of the bones
            for (int i = 0; i < CData.boneData.nbBones; i++)
            {
                var jointIdx = skinnedMesh.GetJointIndex(CData.boneData.jointNames[i]);
                if (jointIdx == -1)
                {
                    continue;                 //if the mesh bone is not in the animation rig (ie. dynamic)
                }
                SJoint joint = skinnedMesh.GetAllJoints()[jointIdx];

                Matrix matrix = CData.boneData.boneMatrices[i];

                Vector3Df position = matrix.Translation;
                Matrix    invRot   = matrix.GetInverse();
                //invRot.rotateVect(position);

                Vector3Df rotation = invRot.GetRotationDegrees(invRot.Scale);
                //rotation = core::vector3df(0, 0, 0);
                position = -position;
                Vector3Df scale = matrix.Scale;

                if (joint != null)
                {
                    // Build GlobalMatrix:
                    Matrix positionMatrix = new Matrix();
                    positionMatrix.Translation = position;
                    Matrix scaleMatrix = new Matrix();
                    scaleMatrix.Scale = scale;
                    Matrix rotationMatrix = new Matrix();
                    rotationMatrix.SetRotationRadians(rotation * CommonData.PI_OVER_180);

                    //joint.GlobalMatrix = scaleMatrix * rotationMatrix * positionMatrix;
                    //joint.LocalMatrix = joint.GlobalMatrix;

                    //joint.Animatedposition = joint.LocalMatrix.Translation;
                    //joint.Animatedrotation = new Quaternion(joint.LocalMatrix.GetRotationDegrees(joint.LocalMatrix.Scale)).MakeInverse();
                    //joint.Animatedscale = joint.LocalMatrix.Scale;

                    var bone = new W3_DataCache.BoneEntry();
                    bone.name         = joint.Name;
                    bone.offsetMatrix = matrix;
                    CData.w3_DataCache.bones.Add(bone);
                }
            }

            // Apply skin
            for (int i = 0; i < CData.w3_DataCache.vertices.Count; i++)
            {
                var entry = CData.w3_DataCache.vertices[i];

                // Check if it's a valid entry
                if (entry.boneId >= CData.w3_DataCache.bones.Count ||
                    entry.meshBufferId >= skinnedMesh.MeshBufferCount ||
                    entry.vertexId >= skinnedMesh.GetMeshBuffer(entry.meshBufferId).VertexCount)
                {
                    // Console.WriteLine("Fail to skin : the vertex entry is not valid.");
                    continue;
                }


                int jointID = skinnedMesh.GetJointIndex(CData.w3_DataCache.bones[(int)entry.boneId].name);
                if (jointID == -1)
                {
                    // Console.WriteLine("Fail to skin : joint not found." );
                    continue;
                }

                SJoint  joint  = skinnedMesh.GetAllJoints()[jointID];
                SWeight weight = skinnedMesh.AddWeight(joint);
                weight.BufferId = entry.meshBufferId;
                weight.VertexId = entry.vertexId;
                weight.Strength = entry.strength;
            }

            // Add weights

            /*for (int i = 0; i < w3_DataCache.vertices.Count; i++)
             * {
             *  uint boneId = w3_DataCache.vertices[i].boneId;
             *  ushort bufferId = w3_DataCache.vertices[i].meshBufferId;
             *  uint vertexId = w3_DataCache.vertices[i].vertexId;
             *  float fweight = w3_DataCache.vertices[i].strength;
             *
             *  if (boneId >= skinnedMesh.GetAllJoints().Count) // If bone doesnt exist
             *      continue;
             *
             *  SJoint joint = skinnedMesh.GetAllJoints()[(int)boneId];
             *
             *  SWeight w = skinnedMesh.AddWeight(joint);
             *  w.BufferId = bufferId;
             *  w.Strength = fweight;
             *  w.VertexId = vertexId;
             * }*/
        }