private static void WriteCollisionMaterialLib(List <Triangle> triangleList, string[] MaterialStream, string materialLibrary)
        {
            StreamWriter MTLWriter = new StreamWriter(materialLibrary, false);

            MTLWriter.WriteLine("# Exported by Heroes Power Plant");
            MTLWriter.WriteLine();

            Dictionary <string, string[]> mtlFileStrings = new Dictionary <string, string[]>();

            foreach (TriangleExt j in triangleList)
            {
                if (!mtlFileStrings.ContainsKey(j.collisionFlag.ToString()))
                {
                    RenderWareFile.Color color = RenderWareFile.Color.FromString(MaterialStream[j.MaterialIndex]);

                    mtlFileStrings.Add(j.collisionFlag.ToString(), new string[] {
                        "newmtl colmat_" + j.collisionFlag.ToString(),
                        $"Kd {color.R / 255f} {color.G / 255f} {color.B / 255f}"
                    });
                }
            }

            foreach (string[] ss in mtlFileStrings.Values)
            {
                MTLWriter.WriteLine(ss[0]);
                MTLWriter.WriteLine(ss[1]);
                MTLWriter.WriteLine("d 1.0");
                MTLWriter.WriteLine();
            }

            MTLWriter.Close();
        }
        private static void GetAtomicTriangleList(StreamWriter OBJWriter, AtomicSector_0009 AtomicSector, ref List <Triangle> triangleList, ref int totalVertexIndices, bool isCollision)
        {
            if (AtomicSector.atomicStruct.isNativeData)
            {
                GetNativeTriangleList(OBJWriter, AtomicSector.atomicExtension, ref triangleList, ref totalVertexIndices);
                return;
            }

            //Write vertex list to obj
            if (AtomicSector.atomicStruct.vertexArray != null)
            {
                foreach (Vertex3 i in AtomicSector.atomicStruct.vertexArray)
                {
                    OBJWriter.WriteLine("v " + i.X.ToString() + " " + i.Y.ToString() + " " + i.Z.ToString());
                }
            }

            OBJWriter.WriteLine();

            //Write uv list to obj
            if (AtomicSector.atomicStruct.uvArray != null)
            {
                if (Program.levelEditor.checkBoxFlipUVs.Checked)
                {
                    foreach (TextCoord i in AtomicSector.atomicStruct.uvArray)
                    {
                        OBJWriter.WriteLine("vt " + i.X.ToString() + " " + (-i.Y).ToString());
                    }
                }
                else
                {
                    foreach (TextCoord i in AtomicSector.atomicStruct.uvArray)
                    {
                        OBJWriter.WriteLine("vt " + i.X.ToString() + " " + i.Y.ToString());
                    }
                }
            }
            OBJWriter.WriteLine();

            // Write vcolors to obj
            if (AtomicSector.atomicStruct.colorArray != null)
            {
                foreach (RenderWareFile.Color i in AtomicSector.atomicStruct.colorArray)
                {
                    //if (i.R < 255)
                    //{
                    //    OBJWriter.WriteLine("vc " + ((byte)Math.Min(255, i.R * 2.5f)).ToString() + " " + ((byte)(i.G * 0.6f)).ToString() + " " + ((byte)(i.B * 0.3f)).ToString() + " " + i.A.ToString());
                    //}
                    //else
                    //{
                    OBJWriter.WriteLine("vc " + i.R.ToString() + " " + i.G.ToString() + " " + i.B.ToString() + " " + i.A.ToString());
                    //}
                }
            }

            OBJWriter.WriteLine();

            if (AtomicSector.atomicStruct.triangleArray != null)
            {
                if (isCollision)
                {
                    RenderWareFile.Color[] collisionFlagList = new RenderWareFile.Color[0];
                    foreach (RWSection r in AtomicSector.atomicExtension.extensionSectionList)
                    {
                        if (r is UserDataPLG_011F userdata)
                        {
                            collisionFlagList = userdata.collisionFlags;
                        }
                    }

                    for (int i = 0; i < AtomicSector.atomicStruct.triangleArray.Length; i++)
                    {
                        triangleList.Add(new TriangleExt
                        {
                            collisionFlag = collisionFlagList[i],
                            MaterialIndex = AtomicSector.atomicStruct.triangleArray[i].materialIndex,
                            vertex1       = AtomicSector.atomicStruct.triangleArray[i].vertex1 + totalVertexIndices,
                            vertex2       = AtomicSector.atomicStruct.triangleArray[i].vertex2 + totalVertexIndices,
                            vertex3       = AtomicSector.atomicStruct.triangleArray[i].vertex3 + totalVertexIndices,
                        });
                    }
                }
                else
                {
                    foreach (RenderWareFile.Triangle i in AtomicSector.atomicStruct.triangleArray)
                    {
                        triangleList.Add(new Triangle
                        {
                            MaterialIndex = i.materialIndex,
                            vertex1       = i.vertex1 + totalVertexIndices,
                            vertex2       = i.vertex2 + totalVertexIndices,
                            vertex3       = i.vertex3 + totalVertexIndices,
                        });
                    }
                }
            }

            if (AtomicSector.atomicStruct.vertexArray != null)
            {
                totalVertexIndices += AtomicSector.atomicStruct.vertexArray.Count();
            }
        }
        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 ModelConverterData ReadOBJFile(string InputFile, bool isShadowCollision)
        {
            ModelConverterData objData = new ModelConverterData()
            {
                MaterialList = new List <string>(),
                VertexList   = new List <Vertex>(),
                UVList       = new List <Vector2>(),
                ColorList    = new List <SharpDX.Color>(),
                TriangleList = new List <Triangle>(),
                MTLLib       = null
            };

            string[] OBJFile = File.ReadAllLines(InputFile);

            int CurrentMaterial = -1;

            bool hasUVCoords = true;

            RenderWareFile.Color TempColFlags = new RenderWareFile.Color(0, 0, 0, 0);

            foreach (string j in OBJFile)
            {
                if (j.Length > 2)
                {
                    if (j.StartsWith("v "))
                    {
                        string a = Regex.Replace(j, @"\s+", " ");

                        string[] SubStrings = a.Split(' ');
                        Vertex   TempVertex = new Vertex();
                        TempVertex.Position.X = Convert.ToSingle(SubStrings[1]);
                        TempVertex.Position.Y = Convert.ToSingle(SubStrings[2]);
                        TempVertex.Position.Z = Convert.ToSingle(SubStrings[3]);

                        TempVertex.Color = SharpDX.Color.White;

                        objData.VertexList.Add(TempVertex);
                    }
                    else if (j.Substring(0, 3) == "vt ")
                    {
                        string[] SubStrings = j.Split(' ');
                        Vector2  TempUV     = new Vector2
                        {
                            X = Convert.ToSingle(SubStrings[1]),
                            Y = Convert.ToSingle(SubStrings[2])
                        };
                        objData.UVList.Add(TempUV);
                    }
                    else if (j.Substring(0, 3) == "vc ") // Special code
                    {
                        string[]      SubStrings = j.Split(' ');
                        SharpDX.Color TempColor  = new SharpDX.Color
                        {
                            R = Convert.ToByte(SubStrings[1]),
                            G = Convert.ToByte(SubStrings[2]),
                            B = Convert.ToByte(SubStrings[3]),
                            A = Convert.ToByte(SubStrings[4])
                        };

                        objData.ColorList.Add(TempColor);
                    }
                    else if (j.StartsWith("f "))
                    {
                        string[] SubStrings = j.Split(' ');

                        Triangle TempTriangle = new Triangle
                        {
                            MaterialIndex = CurrentMaterial,
                            vertex1       = Convert.ToInt32(SubStrings[1].Split('/')[0]) - 1,
                            vertex2       = Convert.ToInt32(SubStrings[2].Split('/')[0]) - 1,
                            vertex3       = Convert.ToInt32(SubStrings[3].Split('/')[0]) - 1
                        };

                        if (hasUVCoords & !isShadowCollision)
                        {
                            try
                            {
                                TempTriangle.UVCoord1 = Convert.ToInt32(SubStrings[1].Split('/')[1]) - 1;
                                TempTriangle.UVCoord2 = Convert.ToInt32(SubStrings[2].Split('/')[1]) - 1;
                                TempTriangle.UVCoord3 = Convert.ToInt32(SubStrings[3].Split('/')[1]) - 1;
                            }
                            catch
                            {
                                MessageBox.Show("Error parsing texture coordinates. The model will be imported without them.");
                                hasUVCoords    = false;
                                objData.UVList = new List <Vector2>()
                                {
                                    new Vector2()
                                };
                            }
                        }

                        objData.TriangleList.Add(TempTriangle);
                    }
                    else if (j.StartsWith("usemtl "))
                    {
                        objData.MaterialList.Add(Regex.Replace(j.Substring(7), @"\s+", ""));
                        CurrentMaterial += 1;
                    }
                    else if (j.StartsWith("mtllib "))
                    {
                        objData.MTLLib = j.Substring(7).Split('\\').LastOrDefault();
                    }
                }
            }

            if (isShadowCollision)
            {
                return(objData);
            }

            // Special code
            if (objData.ColorList.Count == objData.VertexList.Count)
            {
                for (int i = 0; i < objData.VertexList.Count; i++)
                {
                    Vertex v = objData.VertexList[i];
                    v.Color = objData.ColorList[i];
                    objData.VertexList[i] = v;
                }
            }

            try
            {
                objData.MaterialList = ReplaceMaterialNames(InputFile, objData.MTLLib, objData.MaterialList);
            }
            catch
            {
                MessageBox.Show("Unable to load material lib. Will use material names as texture names.");
            }

            if (hasUVCoords)
            {
                return(FixUVCoords(objData));
            }
            else
            {
                return(objData);
            }
        }
Ejemplo n.º 5
0
        private void AddGeometry(SharpDevice device, Geometry_000F g, Matrix transformMatrix)
        {
            List <string> materialList = new List <string>();

            foreach (Material_0007 m in g.materialList.materialList)
            {
                if (m.texture != null)
                {
                    string textureName = m.texture.diffuseTextureName.stringString;
                    materialList.Add(textureName);
                }
                else
                {
                    materialList.Add(DefaultTexture);
                }
            }

            if ((g.geometryStruct.geometryFlags2 & GeometryFlags2.isNativeGeometry) != 0)
            {
                AddNativeData(device, g.geometryExtension, materialList, transformMatrix);
                return;
            }

            List <Vector3>       vertexList1   = new List <Vector3>();
            List <Vector3>       normalList    = new List <Vector3>();
            List <Vector2>       textCoordList = new List <Vector2>();
            List <SharpDX.Color> colorList     = new List <SharpDX.Color>();

            if ((g.geometryStruct.geometryFlags & GeometryFlags.hasVertexPositions) != 0)
            {
                MorphTarget m = g.geometryStruct.morphTargets[0];
                foreach (Vertex3 v in m.vertices)
                {
                    Vector3 pos = (Vector3)Vector3.Transform(new Vector3(v.X, v.Y, v.Z), transformMatrix);
                    vertexList1.Add(pos);
                    vertexListG.Add(pos);
                }
            }

            if ((g.geometryStruct.geometryFlags & GeometryFlags.hasNormals) != 0)
            {
                for (int i = 0; i < vertexList1.Count; i++)
                {
                    normalList.Add(new Vector3(g.geometryStruct.morphTargets[0].normals[i].X, g.geometryStruct.morphTargets[0].normals[i].Y, g.geometryStruct.morphTargets[0].normals[i].Z));
                }
            }

            if ((g.geometryStruct.geometryFlags & GeometryFlags.hasVertexColors) != 0)
            {
                for (int i = 0; i < vertexList1.Count; i++)
                {
                    RenderWareFile.Color c = g.geometryStruct.vertexColors[i];
                    colorList.Add(new SharpDX.Color(c.R, c.G, c.B, c.A));
                }
            }
            else
            {
                for (int i = 0; i < vertexList1.Count; i++)
                {
                    colorList.Add(new SharpDX.Color(1f, 1f, 1f, 1f));
                }
            }

            if ((g.geometryStruct.geometryFlags & GeometryFlags.hasTextCoords) != 0)
            {
                for (int i = 0; i < vertexList1.Count; i++)
                {
                    Vertex2 tc = g.geometryStruct.textCoords[i];
                    textCoordList.Add(new Vector2(tc.X, tc.Y));
                }
            }
            else
            {
                for (int i = 0; i < vertexList1.Count; i++)
                {
                    textCoordList.Add(new Vector2());
                }
            }

            List <SharpSubSet> SubsetList = new List <SharpSubSet>();
            List <int>         indexList  = new List <int>();
            int previousIndexCount        = 0;

            for (int i = 0; i < materialList.Count; i++)
            {
                foreach (Triangle t in g.geometryStruct.triangles)
                {
                    if (t.materialIndex == i)
                    {
                        indexList.Add(t.vertex1);
                        indexList.Add(t.vertex2);
                        indexList.Add(t.vertex3);

                        triangleList.Add(new Triangle(t.materialIndex, (ushort)(t.vertex1 + triangleListOffset), (ushort)(t.vertex2 + triangleListOffset), (ushort)(t.vertex3 + triangleListOffset)));
                    }
                }

                if (indexList.Count - previousIndexCount > 0)
                {
                    SubsetList.Add(new SharpSubSet(previousIndexCount, indexList.Count - previousIndexCount,
                                                   TextureManager.GetTextureFromDictionary(materialList[i]), materialList[i]));
                }

                previousIndexCount = indexList.Count();
            }

            triangleListOffset += vertexList1.Count;

            if (SubsetList.Count > 0)
            {
                VertexColoredTextured[] vertices = new VertexColoredTextured[vertexList1.Count];
                for (int i = 0; i < vertices.Length; i++)
                {
                    vertices[i] = new VertexColoredTextured(vertexList1[i], textCoordList[i], colorList[i]);
                }
                AddToMeshList(SharpMesh.Create(device, vertices, indexList.ToArray(), SubsetList));
            }
            else
            {
                AddToMeshList(null);
            }
        }
Ejemplo n.º 6
0
        private void AddAtomic(SharpDevice device, AtomicSector_0009 AtomicSector, List <string> MaterialList)
        {
            if (AtomicSector.atomicSectorStruct.isNativeData)
            {
                AddNativeData(device, AtomicSector.atomicSectorExtension, MaterialList, Matrix.Identity);
                return;
            }

            List <VertexColoredTextured> vertexList = new List <VertexColoredTextured>();

            foreach (Vertex3 v in AtomicSector.atomicSectorStruct.vertexArray)
            {
                vertexList.Add(new VertexColoredTextured(new Vector3(v.X, v.Y, v.Z), new Vector2(), new SharpDX.Color()));
                vertexListG.Add(new Vector3(v.X, v.Y, v.Z));
            }

            for (int i = 0; i < vertexList.Count; i++)
            {
                RenderWareFile.Color c = AtomicSector.atomicSectorStruct.colorArray[i];

                VertexColoredTextured v = vertexList[i];
                v.Color       = new SharpDX.Color(c.R, c.G, c.B, c.A);
                vertexList[i] = v;
            }

            for (int i = 0; i < vertexList.Count; i++)
            {
                Vertex2 tc = AtomicSector.atomicSectorStruct.uvArray[i];

                VertexColoredTextured v = vertexList[i];
                v.TextureCoordinate = new Vector2(tc.X, tc.Y);
                vertexList[i]       = v;
            }

            List <SharpSubSet> SubsetList = new List <SharpSubSet>();
            List <int>         indexList  = new List <int>();
            int previousIndexCount        = 0;

            for (int i = 0; i < MaterialList.Count; i++)
            {
                for (int j = 0; j < AtomicSector.atomicSectorStruct.triangleArray.Length; j++) // each (Triangle t in AtomicSector.atomicStruct.triangleArray)
                {
                    Triangle t = AtomicSector.atomicSectorStruct.triangleArray[j];
                    if (t.materialIndex == i)
                    {
                        indexList.Add(t.vertex1);
                        indexList.Add(t.vertex2);
                        indexList.Add(t.vertex3);

                        triangleList.Add(new Triangle(t.materialIndex, (ushort)(t.vertex1 + triangleListOffset), (ushort)(t.vertex2 + triangleListOffset), (ushort)(t.vertex3 + triangleListOffset)));
                    }
                }

                if (indexList.Count - previousIndexCount > 0)
                {
                    SubsetList.Add(new SharpSubSet(previousIndexCount, indexList.Count - previousIndexCount,
                                                   TextureManager.GetTextureFromDictionary(MaterialList[i]), MaterialList[i]));
                }

                previousIndexCount = indexList.Count();
            }

            triangleListOffset += AtomicSector.atomicSectorStruct.vertexArray.Length;

            if (SubsetList.Count > 0)
            {
                AddToMeshList(SharpMesh.Create(device, vertexList.ToArray(), indexList.ToArray(), SubsetList));
            }
        }
Ejemplo n.º 7
0
        void AddGeometry(Geometry_000F g)
        {
            List <string> MaterialList = new List <string>();

            foreach (Material_0007 m in g.materialList.materialList)
            {
                if (m.texture != null)
                {
                    string textureName = m.texture.diffuseTextureName.stringString;
                    if (!MaterialList.Contains(textureName))
                    {
                        MaterialList.Add(textureName);
                    }
                }
                else
                {
                    MaterialList.Add(DefaultTexture);
                }
            }

            if (g.geometryStruct.geometryFlags2 == 0x0101)
            {
                AddNativeData(g.geometryExtension, MaterialList);
                return;
            }

            List <VertexColoredTextured> vertexList = new List <VertexColoredTextured>();

            if ((g.geometryStruct.geometryFlags & (int)GeometryFlags.hasVertexPositions) != 0)
            {
                foreach (Vertex3 v in g.geometryStruct.morphTargets[0].vertices)
                {
                    vertexList.Add(new VertexColoredTextured(new Vector3(v.X, v.Y, v.Z),
                                                             new Vector2(),
                                                             SharpDX.Color.White
                                                             ));
                    this.vertexList.Add(new Vector3(v.X, v.Y, v.Z));
                }
            }

            if ((g.geometryStruct.geometryFlags & (int)GeometryFlags.hasVertexColors) != 0)
            {
                for (int i = 0; i < vertexList.Count; i++)
                {
                    RenderWareFile.Color c = g.geometryStruct.vertexColors[i];

                    VertexColoredTextured v = vertexList[i];
                    v.Color       = new SharpDX.Color(c.R, c.G, c.B, c.A);
                    vertexList[i] = v;
                }
            }
            else
            {
                for (int i = 0; i < vertexList.Count; i++)
                {
                    VertexColoredTextured v = vertexList[i];
                    v.Color       = SharpDX.Color.White;
                    vertexList[i] = v;
                }
            }

            if ((g.geometryStruct.geometryFlags & (int)GeometryFlags.hasTextCoords) != 0)
            {
                for (int i = 0; i < vertexList.Count; i++)
                {
                    TextCoord tc = g.geometryStruct.textCoords[i];

                    VertexColoredTextured v = vertexList[i];
                    v.TextureCoordinate = new Vector2(tc.X, tc.Y);
                    vertexList[i]       = v;
                }
            }

            List <SharpSubSet> SubsetList = new List <SharpSubSet>();
            List <int>         indexList  = new List <int>();
            int previousIndexCount        = 0;

            for (int i = 0; i < MaterialList.Count; i++)
            {
                foreach (Triangle t in g.geometryStruct.triangles)
                {
                    if (t.materialIndex == i)
                    {
                        indexList.Add(t.vertex1);
                        indexList.Add(t.vertex2);
                        indexList.Add(t.vertex3);

                        triangleList.Add(t);
                    }
                }

                if (indexList.Count - previousIndexCount > 0)
                {
                    if (BSPRenderer.TextureStream.ContainsKey(MaterialList[i]))
                    {
                        SubsetList.Add(new SharpSubSet(previousIndexCount, indexList.Count - previousIndexCount, BSPRenderer.TextureStream[MaterialList[i]]));
                    }
                    else
                    {
                        SubsetList.Add(new SharpSubSet(previousIndexCount, indexList.Count - previousIndexCount, BSPRenderer.whiteDefault));
                    }
                }

                previousIndexCount = indexList.Count();
            }

            if (SubsetList.Count > 0)
            {
                meshList.Add(SharpMesh.Create(SharpRenderer.device, vertexList.ToArray(), indexList.ToArray(), SubsetList));
            }
        }
Ejemplo n.º 8
0
        void AddAtomic(AtomicSector_0009 AtomicSector)
        {
            if (AtomicSector.atomicStruct.isNativeData)
            {
                AddNativeData(AtomicSector.atomicExtension, MaterialList);
                return;
            }

            List <VertexColoredTextured> vertexList = new List <VertexColoredTextured>();

            foreach (Vertex3 v in AtomicSector.atomicStruct.vertexArray)
            {
                vertexList.Add(new VertexColoredTextured(new Vector3(v.X, v.Y, v.Z), new Vector2(), new SharpDX.Color()));
                this.vertexList.Add(new Vector3(v.X, v.Y, v.Z));
            }

            if (!isShadowCollision)
            {
                for (int i = 0; i < vertexList.Count; i++)
                {
                    RenderWareFile.Color c = AtomicSector.atomicStruct.colorArray[i];

                    VertexColoredTextured v = vertexList[i];
                    v.Color       = new SharpDX.Color(c.R, c.G, c.B, c.A);
                    vertexList[i] = v;
                }

                for (int i = 0; i < vertexList.Count; i++)
                {
                    TextCoord tc = AtomicSector.atomicStruct.uvArray[i];

                    VertexColoredTextured v = vertexList[i];
                    v.TextureCoordinate = new Vector2(tc.X, tc.Y);
                    vertexList[i]       = v;
                }
            }

            List <SharpSubSet> SubsetList = new List <SharpSubSet>();
            List <int>         indexList  = new List <int>();
            int previousIndexCount        = 0;

            for (int i = 0; i < MaterialList.Count; i++)
            {
                for (int j = 0; j < AtomicSector.atomicStruct.triangleArray.Length; j++) // each (Triangle t in AtomicSector.atomicStruct.triangleArray)
                {
                    Triangle t = AtomicSector.atomicStruct.triangleArray[j];
                    if (t.materialIndex == i)
                    {
                        indexList.Add(t.vertex1);
                        indexList.Add(t.vertex2);
                        indexList.Add(t.vertex3);

                        if (isShadowCollision)
                        {
                            RenderWareFile.Color c     = RenderWareFile.Color.FromString(MaterialList[i]);
                            SharpDX.Color        color = new SharpDX.Color(c.R, c.G, c.B, c.A);

                            VertexColoredTextured v1 = vertexList[t.vertex1];
                            v1.Color = color;
                            vertexList[t.vertex1] = v1;

                            VertexColoredTextured v2 = vertexList[t.vertex2];
                            v2.Color = color;
                            vertexList[t.vertex2] = v2;

                            VertexColoredTextured v3 = vertexList[t.vertex3];
                            v3.Color = color;
                            vertexList[t.vertex3] = v3;
                        }

                        triangleList.Add(t);
                    }
                }

                if (indexList.Count - previousIndexCount > 0)
                {
                    if (BSPRenderer.TextureStream.ContainsKey(MaterialList[i]))
                    {
                        SubsetList.Add(new SharpSubSet(previousIndexCount, indexList.Count - previousIndexCount, BSPRenderer.TextureStream[MaterialList[i]]));
                    }
                    else
                    {
                        SubsetList.Add(new SharpSubSet(previousIndexCount, indexList.Count - previousIndexCount, BSPRenderer.whiteDefault));
                    }
                }

                previousIndexCount = indexList.Count();
            }

            if (SubsetList.Count > 0)
            {
                meshList.Add(SharpMesh.Create(SharpRenderer.device, vertexList.ToArray(), indexList.ToArray(), SubsetList));
            }
        }
        private void AddGeometry(SharpDevice device, Geometry_000F g, Matrix transformMatrix)
        {
            List <string> MaterialList = new List <string>();

            foreach (Material_0007 m in g.materialList.materialList)
            {
                if (m.texture != null)
                {
                    string textureName = m.texture.diffuseTextureName.stringString;
                    if (!MaterialList.Contains(textureName))
                    {
                        MaterialList.Add(textureName);
                    }
                }
                else
                {
                    MaterialList.Add(DefaultTexture);
                }
            }

            if ((g.geometryStruct.geometryFlags2 & GeometryFlags2.isNativeGeometry) != 0)
            {
                AddNativeData(device, g.geometryExtension, MaterialList, transformMatrix);
                return;
            }

            List <VertexColoredTextured> vertexList = new List <VertexColoredTextured>();

            if ((g.geometryStruct.geometryFlags & GeometryFlags.hasVertexPositions) != 0)
            {
                foreach (Vertex3 v in g.geometryStruct.morphTargets[0].vertices)
                {
                    Vector3 Position = (Vector3)Vector3.Transform(new Vector3(v.X, v.Y, v.Z), transformMatrix);

                    vertexList.Add(new VertexColoredTextured(Position, new Vector2(), SharpDX.Color.White));
                    vertexListG.Add(Position);
                }
            }

            if ((g.geometryStruct.geometryFlags & GeometryFlags.hasVertexColors) != 0)
            {
                for (int i = 0; i < vertexList.Count; i++)
                {
                    RenderWareFile.Color c = g.geometryStruct.vertexColors[i];

                    VertexColoredTextured v = vertexList[i];
                    v.Color       = new SharpDX.Color(c.R, c.G, c.B, c.A);
                    vertexList[i] = v;
                }
            }
            else
            {
                for (int i = 0; i < vertexList.Count; i++)
                {
                    VertexColoredTextured v = vertexList[i];
                    v.Color       = SharpDX.Color.White;
                    vertexList[i] = v;
                }
            }

            if ((g.geometryStruct.geometryFlags & GeometryFlags.hasTextCoords) != 0)
            {
                for (int i = 0; i < vertexList.Count; i++)
                {
                    Vertex2 tc = g.geometryStruct.textCoords[i];

                    VertexColoredTextured v = vertexList[i];
                    v.TextureCoordinate = new Vector2(tc.X, tc.Y);
                    vertexList[i]       = v;
                }
            }

            List <SharpSubSet> SubsetList = new List <SharpSubSet>();
            List <int>         indexList  = new List <int>();
            int previousIndexCount        = 0;

            for (int i = 0; i < MaterialList.Count; i++)
            {
                foreach (Triangle t in g.geometryStruct.triangles)
                {
                    if (t.materialIndex == i)
                    {
                        indexList.Add(t.vertex1);
                        indexList.Add(t.vertex2);
                        indexList.Add(t.vertex3);

                        triangleList.Add(new Triangle(t.materialIndex, (ushort)(t.vertex1 + triangleListOffset), (ushort)(t.vertex2 + triangleListOffset), (ushort)(t.vertex3 + triangleListOffset)));
                    }
                }

                if (indexList.Count - previousIndexCount > 0)
                {
                    SubsetList.Add(new SharpSubSet(previousIndexCount, indexList.Count - previousIndexCount,
                                                   TextureManager.GetTextureFromDictionary(MaterialList[i]), MaterialList[i]));
                }

                previousIndexCount = indexList.Count();
            }

            triangleListOffset += vertexList.Count;

            if (SubsetList.Count > 0)
            {
                meshList.Add(SharpMesh.Create(device, vertexList.ToArray(), indexList.ToArray(), SubsetList));
            }
        }