Ejemplo n.º 1
0
        public Model(EndianBinaryReader reader, Arguments args)
        {
            ModelStats = new BMDInfo();

            int j3d2Magic  = reader.ReadInt32();
            int modelMagic = reader.ReadInt32();

            if (j3d2Magic != 0x4A334432 && j3d2Magic != 0x3244334A)
            {
                throw new Exception("Model was not a BMD or BDL! (J3D2 magic not found)");
            }
            if ((modelMagic != 0x62646C34) && (modelMagic != 0x626D6433 && modelMagic != 0x346C6462))
            {
                throw new Exception("Model was not a BMD or BDL! (Model type was not bmd3 or bdl4)");
            }

            //Reversed magic found. File uses little endian byte order.
            if (j3d2Magic == 0x3244334A)
            {
                reader.CurrentEndian = Endian.Little;
                littleEndian         = true;
            }

            int modelSize    = reader.ReadInt32();
            int sectionCount = reader.ReadInt32();

            ModelStats.TotalSize = modelSize;

            // Skip the dummy section, SVR3
            reader.Skip(16);

            Scenegraph        = new INF1(reader, 32, ModelStats);
            VertexData        = new VTX1(reader, (int)reader.BaseStream.Position, ModelStats);
            SkinningEnvelopes = new EVP1(reader, (int)reader.BaseStream.Position, ModelStats);
            PartialWeightData = new DRW1(reader, (int)reader.BaseStream.Position, ModelStats);
            Joints            = new JNT1(reader, (int)reader.BaseStream.Position, ModelStats);
            SkinningEnvelopes.SetInverseBindMatrices(Joints.FlatSkeleton);
            Shapes = SHP1.Create(reader, (int)reader.BaseStream.Position, ModelStats);
            Shapes.SetVertexWeights(SkinningEnvelopes, PartialWeightData);
            Materials = new MAT3(reader, (int)reader.BaseStream.Position, ModelStats);
            SkipMDL3(reader);
            Textures = new TEX1(reader, (int)reader.BaseStream.Position, ModelStats);
            Materials.SetTextureNames(Textures);


            if (args.output_materials_path != "")
            {
                Materials.DumpMaterials(Path.GetDirectoryName(args.output_materials_path));
            }
            else
            {
                if (args.output_path != "")
                {
                    string outDir        = Path.GetDirectoryName(args.output_path);
                    string filenameNoExt = Path.GetFileNameWithoutExtension(args.input_path);
                    Materials.DumpMaterials(Path.Combine(outDir, filenameNoExt + "_materials.json"));
                }
                else
                {
                    string inDir         = Path.GetDirectoryName(args.input_path);
                    string filenameNoExt = Path.GetFileNameWithoutExtension(args.input_path);

                    Materials.DumpMaterials(Path.Combine(inDir, filenameNoExt + "_materials.json"));
                }
            }

            foreach (Geometry.Shape shape in Shapes.Shapes)
            {
                packetCount += shape.Packets.Count;
            }

            vertexCount = VertexData.Attributes.Positions.Count;
        }
Ejemplo n.º 2
0
        public Model(Scene scene, Arguments args, List <SuperBMDLib.Materials.Material> mat_presets = null, string additionalTexPath = null)
        {
            ModelStats = new BMDInfo();
            if (args.ensure_one_material_per_mesh)
            {
                EnsureOneMaterialPerMesh(scene);
            }

            Console.WriteLine();
            if (args.sort_meshes)
            {
                SortMeshesByObjectNames(scene);
                Console.WriteLine();
            }


            // For FBX mesh names are empty, instead we need to check the nodes and rename
            // the meshes after the node names.
            foreach (Assimp.Node node in scene.RootNode.Children)
            {
                foreach (int meshindex in node.MeshIndices)
                {
                    Assimp.Mesh mesh = scene.Meshes[meshindex];
                    if (mesh.Name == String.Empty)
                    {
                        mesh.Name = node.Name;
                    }
                }
            }

            Console.WriteLine();
            Console.Write("Searching for the Skeleton Root");
            Assimp.Node root = null;
            for (int i = 0; i < scene.RootNode.ChildCount; i++)
            {
                if (scene.RootNode.Children[i].Name.ToLowerInvariant() == "skeleton_root")
                {
                    if (scene.RootNode.Children[i].ChildCount == 0)
                    {
                        throw new System.Exception("skeleton_root has no children! If you are making a rigged model, make sure skeleton_root contains the root of your skeleton.");
                    }
                    root = scene.RootNode.Children[i].Children[0];
                    break;
                }
                Console.Write(".");
            }

            Console.Write(root == null ? "✓ No Skeleton found" : "✓ Skeleton Found");
            Console.WriteLine();

            foreach (Mesh mesh in scene.Meshes)
            {
                if (mesh.HasBones && root == null)
                {
                    throw new System.Exception("Model uses bones but the skeleton root has not been found! Make sure your skeleton is inside a dummy object called 'skeleton_root'.");
                }
            }


            if (args.rotate_model)
            {
                Console.WriteLine();
                Console.Write("Rotating the model");
                int       i         = 0;
                Matrix4x4 rotate    = Matrix4x4.FromRotationX((float)(-(1 / 2.0) * Math.PI));
                Matrix4x4 rotateinv = rotate;
                rotateinv.Inverse();


                foreach (Mesh mesh in scene.Meshes)
                {
                    if (root != null)
                    {
                        foreach (Assimp.Bone bone in mesh.Bones)
                        {
                            bone.OffsetMatrix = rotateinv * bone.OffsetMatrix;
                            Console.Write("|");
                        }
                    }

                    for (i = 0; i < mesh.VertexCount; i++)
                    {
                        Vector3D vertex = mesh.Vertices[i];
                        vertex.Set(vertex.X, vertex.Z, -vertex.Y);
                        mesh.Vertices[i] = vertex;
                    }
                    for (i = 0; i < mesh.Normals.Count; i++)
                    {
                        Vector3D norm = mesh.Normals[i];
                        norm.Set(norm.X, norm.Z, -norm.Y);

                        mesh.Normals[i] = norm;
                    }
                    Console.Write(".");
                }
                Console.Write("✓");
                Console.WriteLine();
            }

            foreach (Mesh mesh in scene.Meshes)
            {
                if (mesh.HasNormals)
                {
                    for (int i = 0; i < mesh.Normals.Count; i++)
                    {
                        Vector3D normal = mesh.Normals[i];
                        normal.X        = (float)Math.Round(normal.X, 4);
                        normal.Y        = (float)Math.Round(normal.Y, 4);
                        normal.Z        = (float)Math.Round(normal.Z, 4);
                        mesh.Normals[i] = normal;
                    }
                }
            }

            Console.WriteLine();
            Console.WriteLine("Generating the Vertex Data ->");
            VertexData = new VTX1(scene, args.forceFloat);
            Console.WriteLine();
            Console.Write("Generating the Bone Data");
            Joints = new JNT1(scene, VertexData);
            Console.WriteLine();
            Console.WriteLine("Generating the Texture Data -> ");
            Textures = new TEX1(scene, args);
            Console.WriteLine();
            Console.Write("Generating the Envelope Data");
            SkinningEnvelopes = new EVP1();
            SkinningEnvelopes.SetInverseBindMatrices(scene, Joints.FlatSkeleton);

            Console.WriteLine();
            Console.Write("Generating the Weight Data");
            PartialWeightData = new DRW1(scene, Joints.BoneNameIndices);
            Console.WriteLine();

            Console.WriteLine();
            Console.WriteLine("Generating the Mesh Data ->");
            Shapes = SHP1.Create(scene, Joints.BoneNameIndices, VertexData.Attributes, SkinningEnvelopes, PartialWeightData,
                                 args.tristrip_mode, args.degenerateTriangles);

            Console.WriteLine();
            Console.WriteLine("Generating the Material Data ->");
            Materials = new MAT3(scene, Textures, Shapes, args, mat_presets);

            Console.WriteLine();
            Console.WriteLine("Loading the Textures ->");
            if (additionalTexPath == null)
            {
                Materials.LoadAdditionalTextures(Textures, Path.GetDirectoryName(args.input_path), args.readMipmaps);
            }
            else
            {
                Materials.LoadAdditionalTextures(Textures, additionalTexPath, args.readMipmaps);
            }

            Materials.MapTextureNamesToIndices(Textures);

            if (args.output_bdl)
            {
                Console.WriteLine();
                Console.WriteLine("Compiling the MDL3 ->");
                MatDisplayList = new MDL3(Materials.m_Materials, Textures.Textures);
            }
            if (args.littleEndian)
            {
                littleEndian = true;
            }

            Console.WriteLine();
            Console.Write("Generating the Joints");
            Scenegraph = new INF1(scene, Joints);

            foreach (Geometry.Shape shape in Shapes.Shapes)
            {
                packetCount += shape.Packets.Count;
            }

            vertexCount = VertexData.Attributes.Positions.Count;
        }