Example #1
0
        public static void ConvertDFFtoOBJ(string fileName, RenderWareModelFile renderWareModelFile, bool flipUVs)
        {
            lastMaterial = "";
            int totalVertexIndices = 1;

            string          materialLibrary     = Path.ChangeExtension(fileName, "MTL");
            int             untexturedMaterials = 0;
            List <Triangle> triangleList        = new List <Triangle>();

            StreamWriter writer = new StreamWriter((Path.ChangeExtension(fileName, "obj")), false);

            writer.WriteLine("# Exported by Industrial Park");
            writer.WriteLine("mtllib " + Path.GetFileName(materialLibrary));
            writer.WriteLine();

            foreach (RWSection rw in renderWareModelFile.GetAsRWSectionArray())
            {
                if (rw is Clump_0010 w)
                {
                    foreach (Geometry_000F rw2 in w.geometryList.geometryList)
                    {
                        ExportGeometryToOBJ(writer, rw2, ref triangleList, ref totalVertexIndices, ref untexturedMaterials, flipUVs);
                    }
                }
            }

            writer.Close();

            StreamWriter MTLWriter = new StreamWriter(materialLibrary, false);

            MTLWriter.WriteLine("# Exported by Industrial Park");
            MTLWriter.WriteLine();

            foreach (RWSection rw in renderWareModelFile.GetAsRWSectionArray())
            {
                if (rw is Clump_0010 w)
                {
                    foreach (Geometry_000F rw2 in w.geometryList.geometryList)
                    {
                        WriteMaterialLib(rw2, MTLWriter, ref untexturedMaterials);
                    }
                }
            }

            MTLWriter.Close();
        }
Example #2
0
        public static void ConvertBSPtoOBJ(string fileName, RenderWareModelFile bspFile, bool flipUVs)
        {
            int totalVertexIndices = 1;

            string materialLibrary          = Path.ChangeExtension(fileName, "MTL");
            string fileNameWithoutExtension = Path.GetFileNameWithoutExtension(fileName);

            StreamWriter OBJWriter = new StreamWriter((Path.ChangeExtension(fileName, "OBJ")), false);

            List <Triangle> triangleList = new List <Triangle>();

            foreach (RWSection rw in bspFile.GetAsRWSectionArray())
            {
                if (rw is World_000B w)
                {
                    OBJWriter.WriteLine("# Exported by Heroes Power Plant");
                    OBJWriter.WriteLine("mtllib " + Path.GetFileName(materialLibrary));
                    OBJWriter.WriteLine();
                    if (w.firstWorldChunk.sectionIdentifier == Section.AtomicSector)
                    {
                        GetAtomicTriangleList(OBJWriter, (AtomicSector_0009)w.firstWorldChunk, ref triangleList, ref totalVertexIndices, flipUVs);
                    }
                    else if (w.firstWorldChunk.sectionIdentifier == Section.PlaneSector)
                    {
                        GetPlaneTriangleList(OBJWriter, (PlaneSector_000A)w.firstWorldChunk, ref triangleList, ref totalVertexIndices, flipUVs);
                    }
                }
            }

            for (int i = 0; i < bspFile.MaterialList.Count; i++)
            {
                OBJWriter.WriteLine("g " + fileNameWithoutExtension + "_" + bspFile.MaterialList[i]);
                OBJWriter.WriteLine("usemtl " + bspFile.MaterialList[i] + "_m");

                foreach (Triangle j in triangleList)
                {
                    if (j.materialIndex == i)
                    {
                        OBJWriter.WriteLine("f "
                                            + j.vertex1.ToString() + "/" + j.vertex1.ToString() + "/" + j.vertex1.ToString() + " "
                                            + j.vertex2.ToString() + "/" + j.vertex2.ToString() + "/" + j.vertex2.ToString() + " "
                                            + j.vertex3.ToString() + "/" + j.vertex3.ToString() + "/" + j.vertex3.ToString());
                    }
                }

                OBJWriter.WriteLine();
            }

            OBJWriter.Close();
            WriteMaterialLib(bspFile.MaterialList.ToArray(), materialLibrary);
        }
        public static void ConvertBSPtoOBJ(string fileName, RenderWareModelFile bspFile)
        {
            int totalVertexIndices = 0;

            string materialLibrary          = Path.ChangeExtension(fileName, "MTL");
            string fileNameWithoutExtension = Path.GetFileNameWithoutExtension(fileName);

            StreamWriter OBJWriter = new StreamWriter((Path.ChangeExtension(fileName, "OBJ")), false);

            List <Triangle> triangleList = new List <Triangle>();

            foreach (RWSection rw in bspFile.GetAsRWSectionArray())
            {
                if (rw is World_000B w)
                {
                    OBJWriter.WriteLine("# Exported by Heroes Power Plant");
                    OBJWriter.WriteLine("mtllib " + Path.GetFileName(materialLibrary));
                    OBJWriter.WriteLine();
                    if (w.firstWorldChunk.sectionIdentifier == Section.AtomicSector)
                    {
                        GetAtomicTriangleList(OBJWriter, (AtomicSector_0009)w.firstWorldChunk, ref triangleList, ref totalVertexIndices, bspFile.isShadowCollision);
                    }
                    else if (w.firstWorldChunk.sectionIdentifier == Section.PlaneSector)
                    {
                        GetPlaneTriangleList(OBJWriter, (PlaneSector_000A)w.firstWorldChunk, ref triangleList, ref totalVertexIndices, bspFile.isShadowCollision);
                    }
                }
            }

            if (bspFile.isShadowCollision)
            {
                RenderWareFile.Color currentColFlag = new RenderWareFile.Color(3, 3, 3, 3);

                foreach (TriangleExt j in triangleList)
                {
                    if (j.collisionFlag != currentColFlag)
                    {
                        currentColFlag = j.collisionFlag;
                        OBJWriter.WriteLine();
                        OBJWriter.WriteLine("g " + fileNameWithoutExtension + "_" + currentColFlag.ToString());
                        OBJWriter.WriteLine("usemtl colmat_" + currentColFlag.ToString());
                    }

                    OBJWriter.WriteLine("f "
                                        + (j.vertex1 + 1).ToString() + " "
                                        + (j.vertex2 + 1).ToString() + " "
                                        + (j.vertex3 + 1).ToString());
                }

                OBJWriter.Close();
                WriteCollisionMaterialLib(triangleList, bspFile.MaterialList.ToArray(), materialLibrary);
            }
            else
            {
                for (int i = 0; i < bspFile.MaterialList.Count; i++)
                {
                    OBJWriter.WriteLine("g " + fileNameWithoutExtension + "_" + bspFile.MaterialList[i]);
                    OBJWriter.WriteLine("usemtl " + bspFile.MaterialList[i] + "_m");

                    foreach (Triangle j in triangleList)
                    {
                        if (j.MaterialIndex == i)
                        {
                            OBJWriter.WriteLine("f "
                                                + (j.vertex1 + 1).ToString() + "/" + (j.vertex1 + 1).ToString() + " "
                                                + (j.vertex2 + 1).ToString() + "/" + (j.vertex2 + 1).ToString() + " "
                                                + (j.vertex3 + 1).ToString() + "/" + (j.vertex3 + 1).ToString());
                        }
                    }

                    OBJWriter.WriteLine();
                }

                OBJWriter.Close();
                WriteMaterialLib(bspFile.MaterialList.ToArray(), materialLibrary);
            }
        }
        public static void ExportAssimp(string fileName, RenderWareModelFile bspFile, bool flipUVs, ExportFormatDescription format, string textureExtension)
        {
            Scene scene = new Scene();

            foreach (RWSection rw in bspFile.GetAsRWSectionArray())
            {
                if (rw is World_000B w)
                {
                    for (int i = 0; i < w.materialList.materialList.Length; i++)
                    {
                        var mat = w.materialList.materialList[i];

                        scene.Materials.Add(new Material()
                        {
                            ColorDiffuse = new Color4D(
                                mat.materialStruct.color.R / 255f,
                                mat.materialStruct.color.G / 255f,
                                mat.materialStruct.color.B / 255f,
                                mat.materialStruct.color.A / 255f),
                            TextureDiffuse = new TextureSlot()
                            {
                                FilePath    = mat.texture.diffuseTextureName.stringString + textureExtension,
                                TextureType = TextureType.Diffuse
                            },
                            Name = "mat_" + mat.texture.diffuseTextureName.stringString
                        });

                        scene.Meshes.Add(new Mesh(PrimitiveType.Triangle)
                        {
                            MaterialIndex = i, Name = "mesh_" + mat.texture.diffuseTextureName.stringString
                        });
                    }

                    if (w.firstWorldChunk.sectionIdentifier == Section.AtomicSector)
                    {
                        GetAtomicTriangleList(scene, (AtomicSector_0009)w.firstWorldChunk, flipUVs);
                    }
                    else if (w.firstWorldChunk.sectionIdentifier == Section.PlaneSector)
                    {
                        GetPlaneTriangleList(scene, (PlaneSector_000A)w.firstWorldChunk, flipUVs);
                    }
                }
            }

            scene.RootNode = new Node()
            {
                Name = "root"
            };

            Node latest = scene.RootNode;

            for (int i = 0; i < scene.MeshCount; i++)
            {
                latest.Children.Add(Newtonsoft.Json.JsonConvert.DeserializeObject <Node>(
                                        "{\"Name\":\"" + scene.Meshes[i].Name + "\", \"MeshIndices\": [" + i.ToString() + "]}"));

                //latest = latest.Children[0];
            }

            new AssimpContext().ExportFile(scene, fileName, format.FormatId);
        }
        public static void ExportAssimp(string fileName, RenderWareModelFile bspFile, bool flipUVs, ExportFormatDescription format, string textureExtension)
        {
            Scene scene = new Scene();

            foreach (RWSection rw in bspFile.GetAsRWSectionArray())
            {
                if (rw is World_000B w)
                {
                    for (int i = 0; i < w.materialList.materialList.Length; i++)
                    {
                        var    mat     = w.materialList.materialList[i];
                        string objName = Path.GetFileNameWithoutExtension(fileName) + "_" + (mat.materialStruct.isTextured != 0 ? mat.texture.diffuseTextureName.stringString : "default");

                        scene.Materials.Add(new Material()
                        {
                            ColorDiffuse = new Color4D(
                                mat.materialStruct.color.R / 255f,
                                mat.materialStruct.color.G / 255f,
                                mat.materialStruct.color.B / 255f,
                                mat.materialStruct.color.A / 255f),
                            TextureDiffuse = mat.materialStruct.isTextured != 0 ? new TextureSlot()
                            {
                                FilePath    = mat.texture.diffuseTextureName.stringString + textureExtension,
                                TextureType = TextureType.Diffuse
                            } : new TextureSlot(),
                            Name = "mat_" + objName
                        });

                        scene.Meshes.Add(new Mesh(PrimitiveType.Triangle)
                        {
                            MaterialIndex = i, Name = "mesh_" + objName
                        });
                    }

                    if (w.firstWorldChunk.sectionIdentifier == Section.AtomicSector)
                    {
                        GetAtomicTriangleList(scene, (AtomicSector_0009)w.firstWorldChunk);
                    }
                    else if (w.firstWorldChunk.sectionIdentifier == Section.PlaneSector)
                    {
                        GetPlaneTriangleList(scene, (PlaneSector_000A)w.firstWorldChunk);
                    }
                }
            }

            scene.RootNode = new Node()
            {
                Name = "root"
            };

            Node latest = scene.RootNode;

            for (int i = 0; i < scene.MeshCount; i++)
            {
                latest.Children.Add(Newtonsoft.Json.JsonConvert.DeserializeObject <Node>(
                                        "{\"Name\":\"" + scene.Meshes[i].Name + "\", \"MeshIndices\": [" + i.ToString() + "]}"));

                //latest = latest.Children[0];
            }

            new AssimpContext().ExportFile(scene, fileName, format.FormatId,

                                           //PostProcessSteps.GenerateNormals |
                                           PostProcessSteps.JoinIdenticalVertices |
                                           PostProcessSteps.RemoveRedundantMaterials |
                                           PostProcessSteps.ValidateDataStructure |

                                           PostProcessSteps.Debone |
                                           PostProcessSteps.FindInstances |
                                           PostProcessSteps.FindInvalidData |
                                           PostProcessSteps.OptimizeGraph |
                                           PostProcessSteps.OptimizeMeshes |
                                           PostProcessSteps.Triangulate |
                                           PostProcessSteps.PreTransformVertices |

                                           (flipUVs ? PostProcessSteps.FlipUVs : 0));
        }