Exemple #1
0
        public JNT1(Assimp.Scene scene, VTX1 vertexData)
        {
            BoneNameIndices = new Dictionary <string, int>();
            FlatSkeleton    = new List <Rigging.Bone>();
            Assimp.Node root = null;

            for (int i = 0; i < scene.RootNode.ChildCount; i++)
            {
                if (scene.RootNode.Children[i].Name.ToLowerInvariant() == "skeleton_root")
                {
                    root = scene.RootNode.Children[i].Children[0];
                    break;
                }
            }

            if (root == null)
            {
                SkeletonRoot = new Rigging.Bone("root");
                SkeletonRoot.Bounds.GetBoundsValues(vertexData.Attributes.Positions);

                FlatSkeleton.Add(SkeletonRoot);
                BoneNameIndices.Add("root", 0);
            }

            else
            {
                SkeletonRoot = AssimpNodesToBonesRecursive(root, null, FlatSkeleton);

                foreach (Rigging.Bone bone in FlatSkeleton)
                {
                    BoneNameIndices.Add(bone.Name, FlatSkeleton.IndexOf(bone));
                }
            }
        }
Exemple #2
0
        public JNT1(Assimp.Scene scene)
        {
            BoneNameIndices = new Dictionary <string, int>();
            FlatSkeleton    = new List <Rigging.Bone>();
            Assimp.Node root = null;

            for (int i = 0; i < scene.RootNode.ChildCount; i++)
            {
                if (scene.RootNode.Children[i].Name.ToLowerInvariant() == "skeleton_root")
                {
                    root = scene.RootNode.Children[i].Children[0];
                    break;
                }
            }

            if (root == null)
            {
                throw new Exception("Skeleton root was not found. Please make sure the root is under a node called \"skeleton_root.\"");
            }

            SkeletonRoot = AssimpNodesToBonesRecursive(root, null, FlatSkeleton);

            foreach (Rigging.Bone bone in FlatSkeleton)
            {
                BoneNameIndices.Add(bone.Name, FlatSkeleton.IndexOf(bone));
            }
        }
Exemple #3
0
        private void GetNodesRecursive(Rigging.Bone bone, List <Rigging.Bone> skeleton, SceneNode parent, List <Mesh> meshes, List <Material> materials)
        {
            SceneNode node = new SceneNode(NodeType.Joint, skeleton.IndexOf(bone), parent);

            FlatNodes.Add(node);

            int downNodeCount = 0;

            for (int mat_index = 0; mat_index < materials.Count; mat_index++)
            {
                foreach (Mesh mesh in meshes)
                {
                    if (mesh.MaterialIndex != mat_index)
                    {
                        continue;
                    }
                    if (mesh.BoneCount != 1 || mesh.Bones[0].Name != bone.Name)
                    {
                        continue;
                    }

                    SceneNode downNode1 = new SceneNode(NodeType.OpenChild, 0, Root);
                    SceneNode matNode   = new SceneNode(NodeType.Material, mesh.MaterialIndex, Root);
                    SceneNode downNode2 = new SceneNode(NodeType.OpenChild, 0, Root);
                    SceneNode shapeNode = new SceneNode(NodeType.Shape, meshes.IndexOf(mesh), Root);

                    FlatNodes.Add(downNode1);
                    FlatNodes.Add(matNode);
                    FlatNodes.Add(downNode2);
                    FlatNodes.Add(shapeNode);

                    downNodeCount += 2;
                }
            }

            if (bone.Children.Count > 0)
            {
                SceneNode downNode = new SceneNode(NodeType.OpenChild, 0, parent);
                FlatNodes.Add(downNode);

                foreach (Rigging.Bone child in bone.Children)
                {
                    GetNodesRecursive(child, skeleton, node, meshes, materials);
                }

                SceneNode upNode = new SceneNode(NodeType.CloseChild, 0, parent);
                FlatNodes.Add(upNode);
            }

            for (int i = 0; i < downNodeCount; i++)
            {
                FlatNodes.Add(new SceneNode(NodeType.CloseChild, 0, Root));
            }
        }
Exemple #4
0
        private Rigging.Bone AssimpNodesToBonesRecursive(Assimp.Node node, Rigging.Bone parent, List <Rigging.Bone> boneList)
        {
            Rigging.Bone newBone = new Rigging.Bone(node, parent);
            boneList.Add(newBone);

            for (int i = 0; i < node.ChildCount; i++)
            {
                newBone.Children.Add(AssimpNodesToBonesRecursive(node.Children[i], newBone, boneList));
            }

            return(newBone);
        }
Exemple #5
0
        private void GetNodesRecursive(Rigging.Bone bone, List <Rigging.Bone> skeleton, SceneNode parent)
        {
            SceneNode node = new SceneNode(NodeType.Joint, skeleton.IndexOf(bone), parent);

            FlatNodes.Add(node);

            foreach (Rigging.Bone child in bone.Children)
            {
                SceneNode downNode = new SceneNode(NodeType.OpenChild, 0, parent);
                FlatNodes.Add(downNode);

                GetNodesRecursive(child, skeleton, node);

                SceneNode upNode = new SceneNode(NodeType.CloseChild, 0, parent);
                FlatNodes.Add(upNode);
            }
        }
Exemple #6
0
        public Bone(Assimp.Node node, Rigging.Bone parent)
        {
            Children = new List <Bone>();

            Name   = node.Name;
            Parent = parent;

            TransformationMatrix = new Matrix4(
                node.Transform.A1, node.Transform.A2, node.Transform.A3, node.Transform.A4,
                node.Transform.B1, node.Transform.B2, node.Transform.B3, node.Transform.B4,
                node.Transform.C1, node.Transform.C2, node.Transform.C3, node.Transform.C4,
                node.Transform.D1, node.Transform.D2, node.Transform.D3, node.Transform.D4);

            m_Scale       = TransformationMatrix.ExtractScale();
            m_Rotation    = TransformationMatrix.ExtractRotation();
            m_Translation = TransformationMatrix.Column3.Xyz;

            Bounds = new BoundingVolume();
        }
Exemple #7
0
        public EVP1(Scene scene, List <Rigging.Bone> flatSkeleton)
        {
            Weights = new List <Weight>();

            foreach (Mesh mesh in scene.Meshes)
            {
                SortedDictionary <int, Weight> weights = new SortedDictionary <int, Weight>();

                foreach (Assimp.Bone bone in mesh.Bones)
                {
                    Rigging.Bone bmdBone = flatSkeleton.Find(x => x.Name == bone.Name);

                    foreach (VertexWeight vertWeight in bone.VertexWeights)
                    {
                        if (vertWeight.Weight > 1.0f)
                        {
                            if (!weights.ContainsKey(vertWeight.VertexID))
                            {
                                weights.Add(vertWeight.VertexID, new Weight());
                            }

                            weights[vertWeight.VertexID].AddWeight(vertWeight.Weight, flatSkeleton.IndexOf(bmdBone));
                        }
                    }

                    Matrix4 invBind = new Matrix4(
                        bone.OffsetMatrix.A1, bone.OffsetMatrix.A2, bone.OffsetMatrix.A3, bone.OffsetMatrix.A4,
                        bone.OffsetMatrix.B1, bone.OffsetMatrix.B2, bone.OffsetMatrix.B3, bone.OffsetMatrix.B4,
                        bone.OffsetMatrix.C1, bone.OffsetMatrix.C2, bone.OffsetMatrix.C3, bone.OffsetMatrix.C4,
                        bone.OffsetMatrix.D1, bone.OffsetMatrix.D2, bone.OffsetMatrix.D3, bone.OffsetMatrix.D4);

                    bmdBone.SetInverseBindMatrix(invBind);
                }

                Weights.AddRange(weights.Values);
                foreach (Weight weight in Weights)
                {
                    weight.reorderBones();
                }
            }
        }
        public void FillScene(Scene scene, VertexData vertData, List <Rigging.Bone> flatSkeleton, List <Matrix4> inverseBindMatrices)
        {
            for (int i = 0; i < Shapes.Count; i++)
            {
                Mesh mesh = new Mesh($"mesh_{ i }", PrimitiveType.Triangle);
                mesh.MaterialIndex = i;

                int   vertexID = 0;
                Shape curShape = Shapes[i];
                foreach (Packet pack in curShape.Packets)
                {
                    foreach (Primitive prim in pack.Primitives)
                    {
                        List <Vertex> triVertices = J3DUtility.PrimitiveToTriangles(prim);

                        for (int triIndex = 0; triIndex < triVertices.Count; triIndex += 3)
                        {
                            Face newFace = new Face(new int[] { vertexID + 2, vertexID + 1, vertexID });
                            mesh.Faces.Add(newFace);

                            for (int triVertIndex = 0; triVertIndex < 3; triVertIndex++)
                            {
                                Vertex vert = triVertices[triIndex + triVertIndex];

                                for (int j = 0; j < vert.VertexWeight.WeightCount; j++)
                                {
                                    Rigging.Bone curWeightBone = flatSkeleton[vert.VertexWeight.BoneIndices[j]];

                                    int assBoneIndex = mesh.Bones.FindIndex(x => x.Name == curWeightBone.Name);

                                    if (assBoneIndex == -1)
                                    {
                                        Assimp.Bone newBone = new Assimp.Bone();
                                        newBone.Name         = curWeightBone.Name;
                                        newBone.OffsetMatrix = curWeightBone.InverseBindMatrix.ToMatrix4x4();
                                        mesh.Bones.Add(newBone);
                                        assBoneIndex = mesh.Bones.IndexOf(newBone);
                                    }

                                    mesh.Bones[assBoneIndex].VertexWeights.Add(new VertexWeight(vertexID, vert.VertexWeight.Weights[j]));
                                }

                                OpenTK.Vector3 posVec    = vertData.Positions[(int)vert.GetAttributeIndex(GXVertexAttribute.Position)];
                                OpenTK.Vector4 openTKVec = new Vector4(posVec.X, posVec.Y, posVec.Z, 1);

                                Vector3D vertVec = new Vector3D(openTKVec.X, openTKVec.Y, openTKVec.Z);

                                if (vert.VertexWeight.WeightCount == 1)
                                {
                                    if (inverseBindMatrices.Count > vert.VertexWeight.BoneIndices[0])
                                    {
                                        Matrix4 test = inverseBindMatrices[vert.VertexWeight.BoneIndices[0]].Inverted();
                                        test.Transpose();
                                        Vector4 trans = OpenTK.Vector4.Transform(openTKVec, test);
                                        vertVec = new Vector3D(trans.X, trans.Y, trans.Z);
                                    }
                                    else
                                    {
                                        Vector4 trans = OpenTK.Vector4.Transform(openTKVec, flatSkeleton[vert.VertexWeight.BoneIndices[0]].TransformationMatrix);
                                        vertVec = new Vector3D(trans.X, trans.Y, trans.Z);
                                    }
                                }

                                /*else
                                 * {
                                 *  Matrix4 finalMatrix = Matrix4.Zero;
                                 *
                                 *  for (int m = 0; m < vert.VertexWeight.WeightCount; m++)
                                 *  {
                                 *      Matrix4 sm1 = inverseBindMatrices[vert.VertexWeight.BoneIndices[m]];
                                 *      //sm1.Transpose();
                                 *      Matrix4 sm2 = flatSkeleton[vert.VertexWeight.BoneIndices[m]].TransformationMatrix;
                                 *      //sm2.Transpose();
                                 *
                                 *      finalMatrix += Matrix4.Mult(sm1, vert.VertexWeight.Weights[m]);
                                 *  }
                                 *
                                 *  Vector4 final = Vector4.Transform(openTKVec, finalMatrix);
                                 *
                                 *  vertVec = new Vector3D(final.X, final.Y, final.Z);
                                 * }*/

                                mesh.Vertices.Add(vertVec);

                                if (curShape.Descriptor.CheckAttribute(GXVertexAttribute.Normal))
                                {
                                    mesh.Normals.Add(vertData.Normals[(int)vert.NormalIndex].ToVector3D());
                                }

                                if (curShape.Descriptor.CheckAttribute(GXVertexAttribute.Color0))
                                {
                                    mesh.VertexColorChannels[0].Add(vertData.Color_0[(int)vert.Color0Index].ToColor4D());
                                }

                                if (curShape.Descriptor.CheckAttribute(GXVertexAttribute.Color1))
                                {
                                    mesh.VertexColorChannels[1].Add(vertData.Color_1[(int)vert.Color1Index].ToColor4D());
                                }

                                for (int texCoordNum = 0; texCoordNum < 8; texCoordNum++)
                                {
                                    if (curShape.Descriptor.CheckAttribute(GXVertexAttribute.Tex0 + texCoordNum))
                                    {
                                        Vector3D texCoord = new Vector3D();
                                        switch (texCoordNum)
                                        {
                                        case 0:
                                            texCoord = vertData.TexCoord_0[(int)vert.TexCoord0Index].ToVector2D();
                                            break;

                                        case 1:
                                            texCoord = vertData.TexCoord_1[(int)vert.TexCoord1Index].ToVector2D();
                                            break;

                                        case 2:
                                            texCoord = vertData.TexCoord_2[(int)vert.TexCoord2Index].ToVector2D();
                                            break;

                                        case 3:
                                            texCoord = vertData.TexCoord_3[(int)vert.TexCoord3Index].ToVector2D();
                                            break;

                                        case 4:
                                            texCoord = vertData.TexCoord_4[(int)vert.TexCoord4Index].ToVector2D();
                                            break;

                                        case 5:
                                            texCoord = vertData.TexCoord_5[(int)vert.TexCoord5Index].ToVector2D();
                                            break;

                                        case 6:
                                            texCoord = vertData.TexCoord_6[(int)vert.TexCoord6Index].ToVector2D();
                                            break;

                                        case 7:
                                            texCoord = vertData.TexCoord_7[(int)vert.TexCoord7Index].ToVector2D();
                                            break;
                                        }

                                        mesh.TextureCoordinateChannels[texCoordNum].Add(texCoord);
                                    }
                                }

                                vertexID++;
                            }
                        }
                    }
                }

                scene.Meshes.Add(mesh);
            }
        }
Exemple #9
0
        public void FillScene(Scene scene, VertexData vertData, List <Rigging.Bone> flatSkeleton, List <Matrix4> inverseBindMatrices)
        {
            for (int i = 0; i < Shapes.Count; i++)
            {
                int   vertexID = 0;
                Shape curShape = Shapes[i];

                Console.Write("Mesh " + i + ": ");
                string meshname = $"mesh_{ i }";

                switch (curShape.MatrixType)
                {
                case MatrixType.BillboardX:
                    meshname += "_BillX";
                    Console.Write("Billboarding Detected! ");
                    break;

                case MatrixType.BillboardXY:
                    meshname += "_BillXY";
                    Console.Write("Billboarding Detected! ");
                    break;

                default:
                    break;
                }

                Mesh mesh = new Mesh($"mesh_{ i }", PrimitiveType.Triangle);
                mesh.MaterialIndex = i;

                foreach (Packet pack in curShape.Packets)
                {
                    foreach (Primitive prim in pack.Primitives)
                    {
                        List <Vertex> triVertices = J3DUtility.PrimitiveToTriangles(prim);

                        for (int triIndex = 0; triIndex < triVertices.Count; triIndex += 3)
                        {
                            Face newFace = new Face(new int[] { vertexID + 2, vertexID + 1, vertexID });
                            mesh.Faces.Add(newFace);

                            for (int triVertIndex = 0; triVertIndex < 3; triVertIndex++)
                            {
                                Vertex vert = triVertices[triIndex + triVertIndex];

                                for (int j = 0; j < vert.VertexWeight.WeightCount; j++)
                                {
                                    Rigging.Bone curWeightBone = flatSkeleton[vert.VertexWeight.BoneIndices[j]];

                                    int assBoneIndex = mesh.Bones.FindIndex(x => x.Name == curWeightBone.Name);

                                    if (assBoneIndex == -1)
                                    {
                                        Assimp.Bone newBone = new Assimp.Bone();
                                        newBone.Name         = curWeightBone.Name;
                                        newBone.OffsetMatrix = curWeightBone.InverseBindMatrix.ToMatrix4x4();
                                        mesh.Bones.Add(newBone);
                                        assBoneIndex = mesh.Bones.IndexOf(newBone);
                                    }

                                    mesh.Bones[assBoneIndex].VertexWeights.Add(new VertexWeight(vertexID, vert.VertexWeight.Weights[j]));
                                }

                                OpenTK.Vector3 posVec    = vertData.Positions[(int)vert.GetAttributeIndex(GXVertexAttribute.Position)];
                                OpenTK.Vector4 openTKVec = new Vector4(posVec.X, posVec.Y, posVec.Z, 1);

                                Vector3D vertVec = new Vector3D(openTKVec.X, openTKVec.Y, openTKVec.Z);

                                if (vert.VertexWeight.WeightCount == 1)
                                {
                                    if (inverseBindMatrices.Count > vert.VertexWeight.BoneIndices[0])
                                    {
                                        Matrix4 test = inverseBindMatrices[vert.VertexWeight.BoneIndices[0]].Inverted();
                                        test.Transpose();
                                        Vector4 trans = OpenTK.Vector4.Transform(openTKVec, test);
                                        vertVec = new Vector3D(trans.X, trans.Y, trans.Z);
                                    }
                                    else
                                    {
                                        Vector4 trans = OpenTK.Vector4.Transform(openTKVec, flatSkeleton[vert.VertexWeight.BoneIndices[0]].TransformationMatrix);
                                        vertVec = new Vector3D(trans.X, trans.Y, trans.Z);
                                    }
                                }

                                mesh.Vertices.Add(vertVec);

                                if (curShape.Descriptor.CheckAttribute(GXVertexAttribute.Normal))
                                {
                                    OpenTK.Vector3 nrmVec    = vertData.Normals[(int)vert.NormalIndex];
                                    OpenTK.Vector4 openTKNrm = new Vector4(nrmVec.X, nrmVec.Y, nrmVec.Z, 1);
                                    Vector3D       vertNrm   = new Vector3D(nrmVec.X, nrmVec.Y, nrmVec.Z);

                                    if (vert.VertexWeight.WeightCount == 1)
                                    {
                                        if (inverseBindMatrices.Count > vert.VertexWeight.BoneIndices[0])
                                        {
                                            Matrix4 test = inverseBindMatrices[vert.VertexWeight.BoneIndices[0]].Inverted();
                                            vertNrm = Vector3.TransformNormalInverse(nrmVec, test).ToVector3D();
                                        }
                                        else
                                        {
                                            Vector4 trans = OpenTK.Vector4.Transform(openTKNrm, flatSkeleton[vert.VertexWeight.BoneIndices[0]].TransformationMatrix);
                                            vertNrm = new Vector3D(trans.X, trans.Y, trans.Z);
                                        }
                                    }

                                    mesh.Normals.Add(vertNrm);
                                }

                                if (curShape.Descriptor.CheckAttribute(GXVertexAttribute.Color0))
                                {
                                    mesh.VertexColorChannels[0].Add(vertData.Color_0[(int)vert.Color0Index].ToColor4D());
                                }

                                if (curShape.Descriptor.CheckAttribute(GXVertexAttribute.Color1))
                                {
                                    mesh.VertexColorChannels[1].Add(vertData.Color_1[(int)vert.Color1Index].ToColor4D());
                                }

                                for (int texCoordNum = 0; texCoordNum < 8; texCoordNum++)
                                {
                                    if (curShape.Descriptor.CheckAttribute(GXVertexAttribute.Tex0 + texCoordNum))
                                    {
                                        Vector3D texCoord = new Vector3D();
                                        switch (texCoordNum)
                                        {
                                        case 0:
                                            texCoord = vertData.TexCoord_0[(int)vert.TexCoord0Index].ToVector2D();
                                            break;

                                        case 1:
                                            texCoord = vertData.TexCoord_1[(int)vert.TexCoord1Index].ToVector2D();
                                            break;

                                        case 2:
                                            texCoord = vertData.TexCoord_2[(int)vert.TexCoord2Index].ToVector2D();
                                            break;

                                        case 3:
                                            texCoord = vertData.TexCoord_3[(int)vert.TexCoord3Index].ToVector2D();
                                            break;

                                        case 4:
                                            texCoord = vertData.TexCoord_4[(int)vert.TexCoord4Index].ToVector2D();
                                            break;

                                        case 5:
                                            texCoord = vertData.TexCoord_5[(int)vert.TexCoord5Index].ToVector2D();
                                            break;

                                        case 6:
                                            texCoord = vertData.TexCoord_6[(int)vert.TexCoord6Index].ToVector2D();
                                            break;

                                        case 7:
                                            texCoord = vertData.TexCoord_7[(int)vert.TexCoord7Index].ToVector2D();
                                            break;
                                        }

                                        mesh.TextureCoordinateChannels[texCoordNum].Add(texCoord);
                                    }
                                }

                                vertexID++;
                            }
                        }
                    }
                    Console.Write("...");
                }

                scene.Meshes.Add(mesh);
                Console.Write("✓");
                Console.WriteLine();
            }
        }