Ejemplo n.º 1
0
        private void BuildNode(Node parent, ref Matrix4x4 rootTransform)
        {
            Matrix4x4 trafo   = parent.Transform;
            Matrix4x4 world   = trafo * rootTransform;
            Matrix4   worldTK = AssimpHelper.TKMatrix(world);

            foreach (int index in parent.MeshIndices)
            {
                objects.Add(CreateGenericObject(scene.Meshes[index], index, worldTK));
            }

            if (scene.HasMeshes && scene.Meshes.Any(x => x.HasBones))
            {
                foreach (Node child in parent.Children)
                {
                    BuildNode(child, ref rootTransform);
                }
            }
            else
            {
                foreach (Node child in parent.Children)
                {
                    BuildNode(child, ref world);
                }
            }
        }
Ejemplo n.º 2
0
        private void BuildSkeletonNodes(Node node, List <string> boneNames, STSkeleton skeleton, ref Matrix4x4 rootTransform)
        {
            Matrix4x4 trafo   = node.Transform;
            Matrix4x4 world   = trafo * rootTransform;
            Matrix4   worldTK = AssimpHelper.TKMatrix(world);



            bool IsBone = boneNames.Contains(node.Name) && !boneNames.Contains(node.Parent.Name) ||
                          node.Name == "Skl_Root" || node.Name == "nw4f_root";

            Console.WriteLine("node list " + node.Name + " " + IsBone);

            short SmoothIndex = 0;
            short RigidIndex  = -1;

            //Loop through all the bones. If the parent is not in the bone list, then it's Parent is the root
            if (IsBone)
            {
                var idenity = Matrix4x4.Identity;
                var Root    = node.Parent;
                CreateByNode(node, skeleton, SmoothIndex, RigidIndex, true, ref idenity);
            }
            else
            {
                foreach (Node child in node.Children)
                {
                    BuildSkeletonNodes(child, boneNames, skeleton, ref world);
                }
            }
        }
Ejemplo n.º 3
0
        private void BuildNode(Node parent, ref Matrix4x4 rootTransform)
        {
            Matrix4x4 world   = rootTransform;
            Matrix4   worldTK = Matrix4.Identity;

            if (UseTransformMatrix)
            {
                Matrix4x4 trafo = parent.Transform;
                world   = trafo * rootTransform;
                worldTK = AssimpHelper.TKMatrix(world);
            }

            if (parent.MeshCount > 0)
            {
                STConsole.WriteLine($"Use Transform Matrix {UseTransformMatrix}");
                STConsole.WriteLine($"Transform node {parent.Name}");
                STConsole.WriteLine($"Translation {worldTK.ExtractTranslation()}");
                STConsole.WriteLine($"Rotation {worldTK.ExtractRotation()}");
                STConsole.WriteLine($"Scale {worldTK.ExtractScale()}");
            }

            foreach (int index in parent.MeshIndices)
            {
                objects.Add(CreateGenericObject(scene.Meshes[index], index, worldTK));
            }

            foreach (Node child in parent.Children)
            {
                BuildNode(child, ref world);
            }
        }
Ejemplo n.º 4
0
        private void SaveBones(Node parentBone, STBone bone, STSkeleton skeleton)
        {
            Node boneNode = new Node(bone.Text);

            parentBone.Children.Add(boneNode);

            boneNode.Transform = AssimpHelper.GetBoneMatrix(bone);

            foreach (STBone child in bone.GetChildren())
            {
                SaveBones(boneNode, child, skeleton);
            }
        }
Ejemplo n.º 5
0
        private void BuildSkeletonNodes(Node node, List <string> boneNames, STSkeleton skeleton, ref Matrix4x4 rootTransform)
        {
            Matrix4x4 trafo   = node.Transform;
            Matrix4x4 world   = trafo * rootTransform;
            Matrix4   worldTK = AssimpHelper.TKMatrix(world);

            string Name = node.Name;
            string ParentArmatureName = node.Parent != null ? node.Parent.Name : "";

            if (DaeHelper.IDMapToName.ContainsKey(node.Name))
            {
                Name = DaeHelper.IDMapToName[node.Name];
            }

            if (ParentArmatureName != string.Empty && DaeHelper.IDMapToName.ContainsKey(ParentArmatureName))
            {
                ParentArmatureName = DaeHelper.IDMapToName[ParentArmatureName];
            }

            bool IsBone = boneNames.Contains(Name) && !boneNames.Contains(ParentArmatureName) ||
                          Name.Contains("Skl_Root") || Name.Contains("nw4f_root") ||
                          Name.Contains("skl_root") || Name.Contains("_root") ||
                          Name.Contains("skeleton_root");

            short SmoothIndex = 0;
            short RigidIndex  = -1;

            //Loop through all the bones. If the parent is not in the bone list, then it's Parent is the root
            if (IsBone)
            {
                var idenity = Matrix4x4.Identity;

                var Root = node;
                if (node.Parent != null)
                {
                    Root = node.Parent;
                }

                CreateByNode(node, skeleton, ParentArmatureName, SmoothIndex, RigidIndex, true, ref idenity);
            }
            else
            {
                if (node.HasChildren)
                {
                    foreach (Node child in node.Children)
                    {
                        BuildSkeletonNodes(child, boneNames, skeleton, ref world);
                    }
                }
            }
        }
Ejemplo n.º 6
0
        private Matrix4 FromAssimpMatrix(Matrix4x4 mat)
        {
            Vector3D scaling;
            Vector3D tranlation;

            Assimp.Quaternion rot;
            mat.Decompose(out scaling, out rot, out tranlation);

            Console.WriteLine($"rotQ " + rot);

            Matrix4 positionMat = Matrix4.CreateTranslation(AssimpHelper.FromVector(tranlation));
            Matrix4 rotQ        = Matrix4.CreateFromQuaternion(AssimpHelper.TKQuaternion(rot));
            Matrix4 scaleMat    = Matrix4.CreateScale(AssimpHelper.FromVector(scaling));
            Matrix4 matrixFinal = scaleMat * rotQ * positionMat;

            return(matrixFinal);
        }
Ejemplo n.º 7
0
        private void SaveSkeleton(STSkeleton skeleton, Node parentNode)
        {
            Node root = new Node("skeleton_root");

            parentNode.Children.Add(root);

            if (skeleton.bones.Count > 0)
            {
                Node boneNode = new Node(skeleton.bones[0].Text);
                boneNode.Transform = AssimpHelper.GetBoneMatrix(skeleton.bones[0]);
                root.Children.Add(boneNode);

                foreach (STBone child in skeleton.bones[0].GetChildren())
                {
                    SaveBones(boneNode, child, skeleton);
                }
            }
        }
Ejemplo n.º 8
0
        public List <Vertex> GetVertices(Mesh msh, Matrix4 transform, STGenericObject STobj)
        {
            List <Vertex> vertices = new List <Vertex>();

            for (int v = 0; v < msh.VertexCount; v++)
            {
                Vertex vert = new Vertex();

                if (msh.HasVertices)
                {
                    vert.pos = Vector3.TransformPosition(AssimpHelper.FromVector(msh.Vertices[v]), transform);
                }
                if (msh.HasNormals)
                {
                    vert.nrm = Vector3.TransformNormal(AssimpHelper.FromVector(msh.Normals[v]), transform);
                }
                if (msh.HasTextureCoords(0))
                {
                    vert.uv0 = new Vector2(msh.TextureCoordinateChannels[0][v].X, msh.TextureCoordinateChannels[0][v].Y);
                }
                if (msh.HasTextureCoords(1))
                {
                    vert.uv1 = new Vector2(msh.TextureCoordinateChannels[1][v].X, msh.TextureCoordinateChannels[1][v].Y);
                }
                if (msh.HasTextureCoords(2))
                {
                    vert.uv2 = new Vector2(msh.TextureCoordinateChannels[2][v].X, msh.TextureCoordinateChannels[2][v].Y);
                }
                if (msh.HasTangentBasis)
                {
                    vert.tan = new Vector4(msh.Tangents[v].X, msh.Tangents[v].Y, msh.Tangents[v].Z, 1);
                }
                if (msh.HasVertexColors(0))
                {
                    vert.col = new Vector4(msh.VertexColorChannels[0][v].R, msh.VertexColorChannels[0][v].G, msh.VertexColorChannels[0][v].B, msh.VertexColorChannels[0][v].A);
                }
                if (msh.HasTangentBasis)
                {
                    vert.bitan = new Vector4(msh.BiTangents[v].X, msh.BiTangents[v].Y, msh.BiTangents[v].Z, 1);
                }
                vertices.Add(vert);
            }
            if (msh.HasBones)
            {
                for (int i = 0; i < msh.BoneCount; i++)
                {
                    Bone bn = msh.Bones[i];
                    if (bn.HasVertexWeights)
                    {
                        foreach (VertexWeight w in bn.VertexWeights)
                        {
                            //  vertices[w.VertexID].pos = Vector3.TransformPosition(vertices[w.VertexID].pos, AssimpHelper.TKMatrix(bn.OffsetMatrix));
                            vertices[w.VertexID].boneWeights.Add(w.Weight);
                            vertices[w.VertexID].boneNames.Add(bn.Name);
                            BoneNames.Add(bn.Name);
                        }
                    }
                }
            }


            return(vertices);
        }
Ejemplo n.º 9
0
        private void CreateByNode(Node node, STSkeleton skeleton, short SmoothIndex, short RigidIndex, bool IsRoot, ref Assimp.Matrix4x4 rootTransform)
        {
            Matrix4x4 trafo        = node.Transform;
            Matrix4x4 world        = trafo * rootTransform;
            var       transformMat = AssimpHelper.TKMatrix(world);

            int matchedBoneIndex = skeleton.bones.FindIndex(item => item.Name == node.Name);

            if (matchedBoneIndex < 0)
            {
                tempBoneNodes.Add(node);

                STBone bone = new STBone();
                bone.skeletonParent = skeleton;
                skeleton.bones.Add(bone);

                bone.Text = node.Name;
                bone.SmoothMatrixIndex = (short)skeleton.bones.IndexOf(bone);
                bone.RigidMatrixIndex  = -1; //Todo calculate these

                STConsole.WriteLine($"-".Repeat(30));
                STConsole.WriteLine($"Processing Bone {bone.Text}");
                STConsole.WriteLine($"SmoothMatrixIndex {bone.SmoothMatrixIndex}");
                STConsole.WriteLine($"RigidMatrixIndex {bone.RigidMatrixIndex}");
                STConsole.WriteLine($"Transform Matrix {transformMat}");
                STConsole.WriteLine($"-".Repeat(30));

                if (IsRoot)
                {
                    bone.parentIndex = -1;
                    transformMat     = AssimpHelper.TKMatrix(world * Matrix4x4.FromRotationX(MathHelper.DegreesToRadians(BoneRotation)));
                }
                else
                {
                    if (tempBoneNodes.Contains(node.Parent))
                    {
                        bone.parentIndex = tempBoneNodes.IndexOf(node.Parent);
                    }
                }


                var scale    = transformMat.ExtractScale();
                var rotation = transformMat.ExtractRotation();
                var position = transformMat.ExtractTranslation();

                var rotEular = AssimpHelper.ToEular(rotation);

                bone.position = new float[] { position.X, position.Y, position.Z };
                bone.scale    = new float[] { scale.X, scale.Y, scale.Z };
                bone.rotation = new float[] { rotEular.X, rotEular.Y, rotEular.Z, 0 };
            }
            else
            {
                STConsole.WriteLine($"Duplicate node name found for bone {node.Name}!", Color.Red);
            }

            foreach (Node child in node.Children)
            {
                CreateByNode(child, skeleton, SmoothIndex, RigidIndex, false, ref rootTransform);
            }
        }
Ejemplo n.º 10
0
        public List <Vertex> GetVertices(Mesh msh, Matrix4 transform, STGenericObject STobj)
        {
            Matrix4 NormalsTransform = Matrix4.CreateFromQuaternion(transform.ExtractRotation());

            List <Vertex> vertices = new List <Vertex>();

            for (int v = 0; v < msh.VertexCount; v++)
            {
                Vertex vert = new Vertex();

                if (msh.HasVertices)
                {
                    vert.pos = Vector3.TransformPosition(AssimpHelper.FromVector(msh.Vertices[v]), transform);
                }
                if (msh.HasNormals)
                {
                    vert.nrm = Vector3.TransformNormal(AssimpHelper.FromVector(msh.Normals[v]), NormalsTransform);
                }
                if (msh.HasTextureCoords(0))
                {
                    vert.uv0 = new Vector2(msh.TextureCoordinateChannels[0][v].X, msh.TextureCoordinateChannels[0][v].Y);
                }
                if (msh.HasTextureCoords(1))
                {
                    vert.uv1 = new Vector2(msh.TextureCoordinateChannels[1][v].X, msh.TextureCoordinateChannels[1][v].Y);
                }
                if (msh.HasTextureCoords(2))
                {
                    vert.uv2 = new Vector2(msh.TextureCoordinateChannels[2][v].X, msh.TextureCoordinateChannels[2][v].Y);
                }
                if (msh.HasTangentBasis)
                {
                    vert.tan = new Vector4(msh.Tangents[v].X, msh.Tangents[v].Y, msh.Tangents[v].Z, 1);
                }
                if (msh.HasVertexColors(0))
                {
                    vert.col = new Vector4(msh.VertexColorChannels[0][v].R, msh.VertexColorChannels[0][v].G, msh.VertexColorChannels[0][v].B, msh.VertexColorChannels[0][v].A);
                    Console.WriteLine("VTX Col " + vert.col);
                }
                if (msh.HasTangentBasis)
                {
                    vert.bitan = new Vector4(msh.BiTangents[v].X, msh.BiTangents[v].Y, msh.BiTangents[v].Z, 1);
                }
                vertices.Add(vert);
            }
            if (msh.HasBones)
            {
                STConsole.WriteLine(msh.Name + " HasBones " + msh.HasBones);
                STConsole.WriteLine(msh.Name + " BoneCount " + msh.BoneCount);

                for (int i = 0; i < msh.BoneCount; i++)
                {
                    Bone bn = msh.Bones[i];
                    if (bn.HasVertexWeights)
                    {
                        foreach (VertexWeight w in bn.VertexWeights)
                        {
                            vertices[w.VertexID].boneWeights.Add(w.Weight);
                            vertices[w.VertexID].boneNames.Add(bn.Name);
                            BoneNames.Add(bn.Name);
                        }
                    }
                }
            }


            return(vertices);
        }