HaloMap.RawData.BSPModel.ExtractModel C# (CSharp) Method

ExtractModel() public method

The extract model.
public ExtractModel ( string path ) : void
path string The path.
return void
        public void ExtractModel(string path)
        {
            if (path[path.Length - 1] != '\\')
            {
                path += "\\";
            }

            

            string texturepath = path + "Textures\\";
            Directory.CreateDirectory(texturepath);
            List<string> names = new List<string>();
            string mtllib = this.Name + ".mtl";
            FileStream FS = new FileStream(path + mtllib, FileMode.Create);
            StreamWriter SW = new StreamWriter(FS);

            Panel p = new Panel();
            Renderer r = new Renderer();
            r.CreateDevice(p);

            for (int x = 0; x < this.Shaders.Shader.Length; x++)
            {
                string[] namesplit = this.Shaders.Shader[x].shaderName.Split('\\');
                string temps = namesplit[namesplit.Length - 1];
                temps = temps.Replace(' ', '_');
                names.Add(temps);
                this.Shaders.Shader[x].MakeTextures(ref r.device);

                try
                {
                    TextureLoader.Save(
                        texturepath + temps + ".dds", ImageFileFormat.Dds, this.Shaders.Shader[x].MainTexture);
                }
                catch
                {
                }

                if (x > 0)
                {
                    // space
                    SW.WriteLine(string.Empty);
                }

                SW.WriteLine("newmtl " + temps);
                SW.WriteLine("Ka 1.000000 1.000000 1.000000");
                SW.WriteLine("Kd 1.000000 1.000000 1.000000");
                SW.WriteLine("Ks 1.000000 1.000000 1.000000");
                SW.WriteLine("Ns 0.000000");
                SW.WriteLine(@"map_Kd .\\Textures\\" + temps + ".dds");
            }

            SW.Close();
            FS.Close();

            

            #region ExportBSPMeshes

            for (int x = 0; x < this.BSPRawDataMetaChunks.Length; x++)
            {
                if (this.BSPRawDataMetaChunks[x].VerticeCount == 0)
                {
                    continue;
                }

                FS = new FileStream(path + this.Name + "[" + x + "].obj", FileMode.Create);
                SW = new StreamWriter(FS);
                SW.WriteLine("# ------------------------------------");
                SW.WriteLine("# Halo 2 BSP Mesh - Extracted with Entity");
                SW.WriteLine("# ------------------------------------");
                SW.WriteLine("mtllib " + mtllib);
                for (int y = 0; y < this.BSPRawDataMetaChunks[x].VerticeCount; y++)
                {
                    string temps = "v " + this.BSPRawDataMetaChunks[x].Vertices[y].X.ToString("R") + " " +
                                   this.BSPRawDataMetaChunks[x].Vertices[y].Y.ToString("R") + " " +
                                   this.BSPRawDataMetaChunks[x].Vertices[y].Z.ToString("R");
                    SW.WriteLine(temps);
                }

                SW.WriteLine("# " + this.BSPRawDataMetaChunks[x].Vertices.Count + " vertices");
                for (int y = 0; y < this.BSPRawDataMetaChunks[x].VerticeCount; y++)
                {
                    string temps = "vt " + this.BSPRawDataMetaChunks[x].UVs[y].X.ToString("R") + " " +
                                   this.BSPRawDataMetaChunks[x].UVs[y].Y.ToString("R");
                    SW.WriteLine(temps);
                }

                SW.WriteLine("# " + this.BSPRawDataMetaChunks[x].Vertices.Count + " texture vertices");
                for (int y = 0; y < this.BSPRawDataMetaChunks[x].VerticeCount; y++)
                {
                    string temps = "vn " + this.BSPRawDataMetaChunks[x].Normals[y].X.ToString("R") + " " +
                                   this.BSPRawDataMetaChunks[x].Normals[y].Y.ToString("R") + " " +
                                   this.BSPRawDataMetaChunks[x].Normals[y].Z.ToString("R");
                    SW.WriteLine(temps);
                }

                SW.WriteLine("# " + this.BSPRawDataMetaChunks[x].Vertices.Count + " normals");
                for (int y = 0; y < this.BSPRawDataMetaChunks[x].SubMeshInfo.Length; y++)
                {
                    SW.WriteLine("g 0." + y);
                    // SW.WriteLine("s 0." + y.ToString());
                    SW.WriteLine("usemtl  " + names[this.BSPRawDataMetaChunks[x].SubMeshInfo[y].ShaderNumber]);

                    int[] shite = new int[100000];
                    int s = 0;
                    // if (this.BSPRawDataMetaChunks[x].SubMeshInfo[y].IndiceCount == (this.BSPRawDataMetaChunks[x].SubMeshInfo[y].IndiceCount/3)*3)
                    if (this.BSPRawDataMetaChunks[x].FaceCount * 3 != this.BSPRawDataMetaChunks[x].IndiceCount)
                    {
                        int m = this.BSPRawDataMetaChunks[x].SubMeshInfo[y].IndiceStart;

                        bool dir = false;
                        short tempx;
                        short tempy;
                        short tempz;

                        do
                        {
                            // if (mode.EndOfIndices[x][j]>m+2){break;}
                            tempx = this.BSPRawDataMetaChunks[x].Indices[m];
                            tempy = this.BSPRawDataMetaChunks[x].Indices[m + 1];
                            tempz = this.BSPRawDataMetaChunks[x].Indices[m + 2];

                            if (tempx != tempy && tempx != tempz && tempy != tempz)
                            {
                                if (dir == false)
                                {
                                    shite[s] = tempx;
                                    shite[s + 1] = tempy;
                                    shite[s + 2] = tempz;
                                    s += 3;

                                    dir = true;
                                }
                                else
                                {
                                    shite[s] = tempx;
                                    shite[s + 1] = tempz;
                                    shite[s + 2] = tempy;
                                    s += 3;
                                    dir = false;
                                }

                                m += 1;
                            }
                            else
                            {
                                if (dir)
                                {
                                    dir = false;
                                }
                                else
                                {
                                    dir = true;
                                }

                                m += 1;
                            }
                        }
                        while (m <
                               this.BSPRawDataMetaChunks[x].SubMeshInfo[y].IndiceStart +
                               this.BSPRawDataMetaChunks[x].SubMeshInfo[y].IndiceCount - 2);
                    }
                    else
                    {
                        Array.Copy(
                            this.BSPRawDataMetaChunks[x].Indices, 
                            this.BSPRawDataMetaChunks[x].SubMeshInfo[y].IndiceStart, 
                            shite, 
                            0, 
                            this.BSPRawDataMetaChunks[x].SubMeshInfo[y].IndiceCount);
                        s = this.BSPRawDataMetaChunks[x].SubMeshInfo[y].IndiceCount;
                    }

                    for (int xx = 0; xx < s; xx += 3)
                    {
                        string temps = "f " + (shite[xx] + 1) + "/" + (shite[xx] + 1) + "/" + (shite[xx] + 1) + " " +
                                       (shite[xx + 1] + 1) + "/" + (shite[xx + 1] + 1) + "/" + (shite[xx + 1] + 1) + " " +
                                       (shite[xx + 2] + 1) + "/" + (shite[xx + 2] + 1) + "/" + (shite[xx + 2] + 1);
                        SW.WriteLine(temps);
                    }

                    SW.WriteLine("# " + (s / 3) + " elements");
                }

                SW.Close();
                FS.Close();
            }

            #endregion

            #region ExportBSPPermutationMeshes

            for (int x = 0; x < this.BSPPermutationRawDataMetaChunks.Length; x++)
            {
                if (this.BSPPermutationRawDataMetaChunks[x].SubMeshInfo == null)
                {
                    continue;
                }

                FS = new FileStream(path + this.Name + "-Permutation[" + x + "].obj", FileMode.Create);
                SW = new StreamWriter(FS);
                SW.WriteLine("# ------------------------------------");
                SW.WriteLine("# Halo 2 BSP Permutation Mesh - Extracted with Entity");
                SW.WriteLine("# ------------------------------------");
                SW.WriteLine("mtllib " + mtllib);
                for (int y = 0; y < this.BSPPermutationRawDataMetaChunks[x].VerticeCount; y++)
                {
                    string temps = "v " + this.BSPPermutationRawDataMetaChunks[x].Vertices[y].X.ToString("R") + " " +
                                   this.BSPPermutationRawDataMetaChunks[x].Vertices[y].Y.ToString("R") + " " +
                                   this.BSPPermutationRawDataMetaChunks[x].Vertices[y].Z.ToString("R");
                    SW.WriteLine(temps);
                }

                SW.WriteLine("# " + this.BSPPermutationRawDataMetaChunks[x].Vertices.Count + " vertices");
                for (int y = 0; y < this.BSPPermutationRawDataMetaChunks[x].VerticeCount; y++)
                {
                    string temps = "vt " + this.BSPPermutationRawDataMetaChunks[x].UVs[y].X.ToString("R") + " " +
                                   this.BSPPermutationRawDataMetaChunks[x].UVs[y].Y.ToString("R");
                    SW.WriteLine(temps);
                }

                SW.WriteLine("# " + this.BSPPermutationRawDataMetaChunks[x].Vertices.Count + " texture vertices");
                for (int y = 0; y < this.BSPPermutationRawDataMetaChunks[x].VerticeCount; y++)
                {
                    string temps = "vn " + this.BSPPermutationRawDataMetaChunks[x].Normals[y].X.ToString("R") + " " +
                                   this.BSPPermutationRawDataMetaChunks[x].Normals[y].Y.ToString("R") + " " +
                                   this.BSPPermutationRawDataMetaChunks[x].Normals[y].Z.ToString("R");
                    SW.WriteLine(temps);
                }

                SW.WriteLine("# " + this.BSPPermutationRawDataMetaChunks[x].Vertices.Count + " normals");
                for (int y = 0; y < this.BSPPermutationRawDataMetaChunks[x].SubMeshInfo.Length; y++)
                {
                    SW.WriteLine("g 0." + y);
                    // SW.WriteLine("s 0." + y.ToString());
                    SW.WriteLine(
                        "usemtl  " + names[this.BSPPermutationRawDataMetaChunks[x].SubMeshInfo[y].ShaderNumber]);

                    short[] shite = new short[100000];
                    int s = 0;
                    if (this.BSPPermutationRawDataMetaChunks[x].FaceCount * 3 !=
                        this.BSPPermutationRawDataMetaChunks[x].IndiceCount)
                    {
                        int m = this.BSPPermutationRawDataMetaChunks[x].SubMeshInfo[y].IndiceStart;

                        bool dir = false;
                        short tempx;
                        short tempy;
                        short tempz;

                        do
                        {
                            // if (mode.EndOfIndices[x][j]>m+2){break;}
                            tempx = this.BSPPermutationRawDataMetaChunks[x].Indices[m];
                            tempy = this.BSPPermutationRawDataMetaChunks[x].Indices[m + 1];
                            tempz = this.BSPPermutationRawDataMetaChunks[x].Indices[m + 2];

                            if (tempx != tempy && tempx != tempz && tempy != tempz)
                            {
                                if (dir == false)
                                {
                                    shite[s] = tempx;
                                    shite[s + 1] = tempy;
                                    shite[s + 2] = tempz;
                                    s += 3;

                                    dir = true;
                                }
                                else
                                {
                                    shite[s] = tempx;
                                    shite[s + 1] = tempz;
                                    shite[s + 2] = tempy;
                                    s += 3;
                                    dir = false;
                                }

                                m += 1;
                            }
                            else
                            {
                                if (dir)
                                {
                                    dir = false;
                                }
                                else
                                {
                                    dir = true;
                                }

                                m += 1;
                            }
                        }
                        while (m <
                               this.BSPPermutationRawDataMetaChunks[x].SubMeshInfo[y].IndiceStart +
                               this.BSPPermutationRawDataMetaChunks[x].SubMeshInfo[y].IndiceCount - 2);
                    }
                    else
                    {
                        Array.Copy(
                            this.BSPPermutationRawDataMetaChunks[x].Indices, 
                            this.BSPPermutationRawDataMetaChunks[x].SubMeshInfo[y].IndiceStart, 
                            shite, 
                            0, 
                            this.BSPPermutationRawDataMetaChunks[x].SubMeshInfo[y].IndiceCount);
                        s = this.BSPPermutationRawDataMetaChunks[x].SubMeshInfo[y].IndiceCount;
                    }

                    for (int xx = 0; xx < s; xx += 3)
                    {
                        string temps = "f " + (shite[xx] + 1) + "/" + (shite[xx] + 1) + "/" + (shite[xx] + 1) + " " +
                                       (shite[xx + 1] + 1) + "/" + (shite[xx + 1] + 1) + "/" + (shite[xx + 1] + 1) + " " +
                                       (shite[xx + 2] + 1) + "/" + (shite[xx + 2] + 1) + "/" + (shite[xx + 2] + 1);
                        SW.WriteLine(temps);
                    }

                    SW.WriteLine("# " + (s / 3) + " elements");
                }

                SW.Close();
                FS.Close();
            }

            #endregion

            if (this.UnknownChunks == null)
            {
                return;
            }

            #region ExportUnknown

            for (int x = 0; x < this.UnknownChunks.Length; x++)
            {
                if (this.UnknownChunks[x].IndiceCount == 0)
                {
                    continue;
                }

                FS = new FileStream(path + this.Name + "-Unknown[" + x + "].obj", FileMode.Create);
                SW = new StreamWriter(FS);
                SW.WriteLine("# ------------------------------------");
                SW.WriteLine("# Halo 2 BSP Unknown Mesh - Extracted with Entity");
                SW.WriteLine("# ------------------------------------");

                for (int y = 0; y < this.UnknownChunks[x].VerticeCount; y++)
                {
                    string temps = "v " + this.UnknownChunks[x].Vertices[y].X + " " +
                                   this.UnknownChunks[x].Vertices[y].Y + " " + this.UnknownChunks[x].Vertices[y].Z;
                    SW.WriteLine(temps);
                }

                SW.WriteLine("# " + this.UnknownChunks[x].Vertices.Count + " vertices");

                SW.WriteLine("g 0.0");

                short[] shite = new short[100000];
                int s = 0;

                Array.Copy(this.UnknownChunks[x].Indices, 0, shite, 0, UnknownChunks[x].IndiceCount);
                s = this.UnknownChunks[x].IndiceCount;

                for (int xx = 0; xx < s; xx += 3)
                {
                    string temps = "f " + (shite[xx] + 1) + "/" + (shite[xx] + 1) + "/" + (shite[xx] + 1) + " " +
                                   (shite[xx + 1] + 1) + "/" + (shite[xx + 1] + 1) + "/" + (shite[xx + 1] + 1) + " " +
                                   (shite[xx + 2] + 1) + "/" + (shite[xx + 2] + 1) + "/" + (shite[xx + 2] + 1);
                    SW.WriteLine(temps);
                }

                SW.WriteLine("# " + (s / 3) + " elements");

                SW.Close();
                FS.Close();
            }

            #endregion
        }

Usage Example

示例#1
0
        /// <summary>
        /// The export obj tool strip menu item_ click.
        /// </summary>
        /// <param name="sender">The sender.</param>
        /// <param name="e">The e.</param>
        /// <remarks></remarks>
        private void exportOBJToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (folderBrowserDialog.ShowDialog() == DialogResult.Cancel)
            {
                return;
            }

            BSPModel pm = new BSPModel(ref map.SelectedMeta);
            string[] temps = map.SelectedMeta.name.Split('\\');
            pm.Name = temps[temps.Length - 1];

            pm.ExtractModel(folderBrowserDialog.SelectedPath);

            MessageBox.Show("Done");
        }