Ejemplo n.º 1
0
        private static void ExportGeometryToOBJ(StreamWriter writer, Geometry_000F g, ref int untexturedMaterials)
        {
            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("default");
                }
            }

            GeometryStruct_0001 gs = g.geometryStruct;

            if (g.materialList.materialList[0].materialStruct.isTextured != 0)
            {
                writer.WriteLine("g obj_" + g.materialList.materialList[0].texture.diffuseTextureName.stringString);
                writer.WriteLine("usemtl " + g.materialList.materialList[0].texture.diffuseTextureName.stringString + "_m");
            }
            else
            {
                writer.WriteLine("g obj_default_" + untexturedMaterials.ToString());
                writer.WriteLine("usemtl default_" + untexturedMaterials.ToString() + "_m");
                untexturedMaterials++;
            }
            writer.WriteLine();

            if (gs.geometryFlags2 == 0x0101)
            {
                WriteNativeData(writer, g);
                return;
            }

            foreach (MorphTarget m in gs.morphTargets)
            {
                if (m.hasVertices != 0)
                {
                    foreach (Vertex3 v in m.vertices)
                    {
                        writer.WriteLine("v " + v.X.ToString() + " " + v.Y.ToString() + " " + v.Z.ToString());
                    }
                    writer.WriteLine();
                }

                if (m.hasNormals != 0)
                {
                    foreach (Vertex3 vn in m.normals)
                    {
                        writer.WriteLine("vn " + vn.X.ToString() + " " + vn.Y.ToString() + " " + vn.Z.ToString());
                    }
                    writer.WriteLine();
                }

                if ((gs.geometryFlags & (int)GeometryFlags.hasVertexColors) != 0)
                {
                    foreach (Color c in gs.vertexColors)
                    {
                        writer.WriteLine("vc " + c.R.ToString() + " " + c.G.ToString() + " " + c.B.ToString() + " " + c.A.ToString());
                    }
                    writer.WriteLine();
                }

                if ((gs.geometryFlags & (int)GeometryFlags.hasTextCoords) != 0)
                {
                    foreach (TextCoord tc in gs.textCoords)
                    {
                        writer.WriteLine("vt " + tc.X.ToString() + " " + tc.Y.ToString());
                    }
                    writer.WriteLine();
                }

                foreach (Triangle t in gs.triangles)
                {
                    List <char> v1 = new List <char>(8);
                    List <char> v2 = new List <char>(8);
                    List <char> v3 = new List <char>(8);

                    int n1 = t.vertex1 + totalVertexIndices;
                    int n2 = t.vertex2 + totalVertexIndices;
                    int n3 = t.vertex3 + totalVertexIndices;

                    if (m.hasVertices != 0)
                    {
                        v1.AddRange(n1.ToString());
                        v2.AddRange(n2.ToString());
                        v3.AddRange(n3.ToString());
                    }
                    if (((gs.geometryFlags & (int)GeometryFlags.hasTextCoords) != 0) & (m.hasNormals != 0))
                    {
                        v1.AddRange("/" + n1.ToString() + "/" + n1.ToString());
                        v2.AddRange("/" + n2.ToString() + "/" + n2.ToString());
                        v3.AddRange("/" + n3.ToString() + "/" + n3.ToString());
                    }
                    else if ((gs.geometryFlags & (int)GeometryFlags.hasTextCoords) != 0)
                    {
                        v1.AddRange("/" + n1.ToString());
                        v2.AddRange("/" + n2.ToString());
                        v3.AddRange("/" + n3.ToString());
                    }
                    else if (m.hasNormals != 0)
                    {
                        v1.AddRange("//" + n1.ToString());
                        v2.AddRange("//" + n2.ToString());
                        v3.AddRange("//" + n3.ToString());
                    }
                    writer.WriteLine("f " + new string(v1.ToArray()) + " " + new string(v2.ToArray()) + " " + new string(v3.ToArray()));
                }

                totalVertexIndices += m.vertices.Count();
                writer.WriteLine();
            }
        }
Ejemplo n.º 2
0
        private static void ExportGeometryToOBJ(StreamWriter writer, Geometry_000F g, ref List <Triangle> triangleList, ref int totalVertexIndices, ref int untexturedMaterials, bool flipUVs)
        {
            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("default");
                }
            }

            GeometryStruct_0001 gs = g.geometryStruct;

            if (gs.geometryFlags2 == (GeometryFlags2)0x0101)
            {
                GetNativeTriangleList(writer, g.geometryExtension, materialList, ref triangleList, ref totalVertexIndices, flipUVs);
                return;
            }

            if (g.materialList.materialList[0].materialStruct.isTextured != 0)
            {
                string mn = g.materialList.materialList[0].texture.diffuseTextureName.stringString;

                if (string.IsNullOrEmpty(lastMaterial) || lastMaterial != mn)
                {
                    writer.WriteLine("g obj_" + mn);
                    writer.WriteLine("usemtl " + mn + "_m");
                }

                lastMaterial = mn;
            }
            else
            {
                writer.WriteLine("g obj_default_" + untexturedMaterials.ToString());
                writer.WriteLine("usemtl default_" + untexturedMaterials.ToString() + "_m");
                untexturedMaterials++;
            }
            writer.WriteLine();

            foreach (MorphTarget m in gs.morphTargets)
            {
                if (m.hasVertices != 0)
                {
                    foreach (Vertex3 v in m.vertices)
                    {
                        writer.WriteLine("v " + v.X.ToString() + " " + v.Y.ToString() + " " + v.Z.ToString());
                    }
                    writer.WriteLine();
                }

                if (m.hasNormals != 0)
                {
                    foreach (Vertex3 vn in m.normals)
                    {
                        writer.WriteLine("vn " + vn.X.ToString() + " " + vn.Y.ToString() + " " + vn.Z.ToString());
                    }
                    writer.WriteLine();
                }

                if ((gs.geometryFlags & GeometryFlags.hasVertexColors) != 0)
                {
                    foreach (Color c in gs.vertexColors)
                    {
                        writer.WriteLine("vc " + c.R.ToString() + " " + c.G.ToString() + " " + c.B.ToString() + " " + c.A.ToString());
                    }
                    writer.WriteLine();
                }

                if ((gs.geometryFlags & GeometryFlags.hasTextCoords) != 0)
                {
                    foreach (Vertex2 tc in gs.textCoords)
                    {
                        writer.WriteLine("vt " + tc.X.ToString() + " " + tc.Y.ToString());
                    }
                    writer.WriteLine();
                }

                foreach (RenderWareFile.Triangle t in gs.triangles)
                {
                    List <char> v1 = new List <char>(8);
                    List <char> v2 = new List <char>(8);
                    List <char> v3 = new List <char>(8);

                    int n1 = t.vertex1 + totalVertexIndices;
                    int n2 = t.vertex2 + totalVertexIndices;
                    int n3 = t.vertex3 + totalVertexIndices;

                    if (m.hasVertices != 0)
                    {
                        v1.AddRange(n1.ToString());
                        v2.AddRange(n2.ToString());
                        v3.AddRange(n3.ToString());
                    }
                    if (((gs.geometryFlags & GeometryFlags.hasTextCoords) != 0) & (m.hasNormals != 0))
                    {
                        v1.AddRange("/" + n1.ToString() + "/" + n1.ToString());
                        v2.AddRange("/" + n2.ToString() + "/" + n2.ToString());
                        v3.AddRange("/" + n3.ToString() + "/" + n3.ToString());
                    }
                    else if ((gs.geometryFlags & GeometryFlags.hasTextCoords) != 0)
                    {
                        v1.AddRange("/" + n1.ToString());
                        v2.AddRange("/" + n2.ToString());
                        v3.AddRange("/" + n3.ToString());
                    }
                    else if (m.hasNormals != 0)
                    {
                        v1.AddRange("//" + n1.ToString());
                        v2.AddRange("//" + n2.ToString());
                        v3.AddRange("//" + n3.ToString());
                    }
                    writer.WriteLine("f " + new string(v1.ToArray()) + " " + new string(v2.ToArray()) + " " + new string(v3.ToArray()));
                }

                totalVertexIndices += m.vertices.Count();
                writer.WriteLine();
            }
        }