private StringWriter BuildWavefrontStringForTileObjectMesh(TmxMesh mesh)
        {
            Logger.WriteVerbose("Building mesh obj file for tile: '{0}.obj'", mesh.UniqueMeshName);
            GenericListDatabase <Vertex3> vertexDatabase = new GenericListDatabase <Vertex3>();
            HashIndexOf <PointF>          uvDatabase     = new HashIndexOf <PointF>();
            StringBuilder faces = new StringBuilder();


            // Get the single tile associated with this mesh
            TmxTile tmxTile = this.tmxMap.Tiles[mesh.TileIds[0]];

            var vertices = CalculateFaceVertices_TileObject(tmxTile.TileSize, tmxTile.Offset);
            var uvs      = CalculateFaceTextureCoordinates(tmxTile, false, false, false);

            // TileObjects have zero depth on their vertices. Their GameObject parent will set depth.
            FaceVertices faceVertices = new FaceVertices {
                Vertices = vertices, Depth_z = 0.0f
            };

            // Adds vertices and uvs to the database as we build the face strings
            string v0 = String.Format("{0}/{1}/1", vertexDatabase.AddToDatabase(faceVertices.V0) + 1, uvDatabase.Add(uvs[0]) + 1);
            string v1 = String.Format("{0}/{1}/1", vertexDatabase.AddToDatabase(faceVertices.V1) + 1, uvDatabase.Add(uvs[1]) + 1);
            string v2 = String.Format("{0}/{1}/1", vertexDatabase.AddToDatabase(faceVertices.V2) + 1, uvDatabase.Add(uvs[2]) + 1);
            string v3 = String.Format("{0}/{1}/1", vertexDatabase.AddToDatabase(faceVertices.V3) + 1, uvDatabase.Add(uvs[3]) + 1);

            faces.AppendFormat("f {0} {1} {2} {3}\n", v0, v1, v2, v3);

            // We have all the data we need to build the wavefront file format
            return(CreateWavefrontWriter(mesh, vertexDatabase, uvDatabase, faces));
        }
        private StringWriter BuildWavefrontStringForLayerMesh(TmxLayer layer, TmxMesh mesh, IEnumerable <int> horizontalRange, IEnumerable <int> verticalRange)
        {
            Logger.WriteVerbose("Building mesh obj file for '{0}'", mesh.UniqueMeshName);
            GenericListDatabase <Vertex3> vertexDatabase = new GenericListDatabase <Vertex3>();
            HashIndexOf <PointF>          uvDatabase     = new HashIndexOf <PointF>();
            StringBuilder faces = new StringBuilder();

            foreach (int y in verticalRange)
            {
                foreach (int x in horizontalRange)
                {
                    int  tileIndex = layer.GetTileIndex(x, y);
                    uint tileId    = mesh.GetTileIdAt(tileIndex);

                    // Skip blank tiles
                    if (tileId == 0)
                    {
                        continue;
                    }

                    TmxTile tile = this.tmxMap.Tiles[TmxMath.GetTileIdWithoutFlags(tileId)];

                    // What are the vertex and texture coorindates of this face on the mesh?
                    var position = this.tmxMap.GetMapPositionAt(x, y, tile);
                    var vertices = CalculateFaceVertices(position, tile.TileSize);

                    // If we're using depth shaders then we'll need to set a depth value of this face
                    float depth_z = 0.0f;
                    if (Tiled2Unity.Settings.DepthBufferEnabled)
                    {
                        depth_z = CalculateFaceDepth(position.Y + tmxMap.TileHeight, tmxMap.MapSizeInPixels.Height);
                    }

                    FaceVertices faceVertices = new FaceVertices {
                        Vertices = vertices, Depth_z = depth_z
                    };

                    // Is the tile being flipped or rotated (needed for texture cooridinates)
                    bool flipDiagonal   = TmxMath.IsTileFlippedDiagonally(tileId);
                    bool flipHorizontal = TmxMath.IsTileFlippedHorizontally(tileId);
                    bool flipVertical   = TmxMath.IsTileFlippedVertically(tileId);
                    var  uvs            = CalculateFaceTextureCoordinates(tile, flipDiagonal, flipHorizontal, flipVertical);

                    // Adds vertices and uvs to the database as we build the face strings
                    string v0 = String.Format("{0}/{1}/1", vertexDatabase.AddToDatabase(faceVertices.V0) + 1, uvDatabase.Add(uvs[0]) + 1);
                    string v1 = String.Format("{0}/{1}/1", vertexDatabase.AddToDatabase(faceVertices.V1) + 1, uvDatabase.Add(uvs[1]) + 1);
                    string v2 = String.Format("{0}/{1}/1", vertexDatabase.AddToDatabase(faceVertices.V2) + 1, uvDatabase.Add(uvs[2]) + 1);
                    string v3 = String.Format("{0}/{1}/1", vertexDatabase.AddToDatabase(faceVertices.V3) + 1, uvDatabase.Add(uvs[3]) + 1);
                    faces.AppendFormat("f {0} {1} {2} {3}\n", v0, v1, v2, v3);
                }
            }

            // We have all the data we need to build the wavefront file format
            return(CreateWavefrontWriter(mesh, vertexDatabase, uvDatabase, faces));
        }
Beispiel #3
0
        private void ProcessLine(string line)
        {
            var parts = line.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);

            if (parts.Length <= 0)
            {
                return;
            }

            switch (parts[0])
            {
            case "v":
                var vertex = new GeometricVertex();
                vertex.ProcessData(parts, _maxValue);
                GeometricVertices.Add(vertex);
                break;

            case "f":
                var face = new FaceVertex();
                face.ProcessData(parts);
                FaceVertices.Add(face);
                break;

            case "vt":
                var textureVertex = new TextureVertex();
                textureVertex.ProcessData(parts);
                TextureVertices.Add(textureVertex);
                break;

            case "vn":
                var normalVertex = new NormalVertex();
                normalVertex.ProcessData(parts);
                NormalVertices.Add(normalVertex);
                break;
            }
        }
        // Creates the text for a Wavefront OBJ file for the TmxMap
        private StringWriter BuildObjString()
        {
            HashIndexOf<Vertex3> vertexDatabase = new HashIndexOf<Vertex3>();
            HashIndexOf<PointF> uvDatabase = new HashIndexOf<PointF>();

            float mapLogicalHeight = this.tmxMap.MapSizeInPixels().Height;

            // Go through every face of every mesh of every visible layer and collect vertex and texture coordinate indices as you go
            int groupCount = 0;
            StringBuilder faceBuilder = new StringBuilder();
            foreach (var layer in this.tmxMap.Layers)
            {
                if (layer.Visible != true)
                    continue;

                if (layer.Ignore == TmxLayer.IgnoreSettings.Visual)
                    continue;

                // We're going to use this layer
                ++groupCount;

                // Enumerate over the tiles in the direction given by the draw order of the map
                var verticalRange = (this.tmxMap.DrawOrderVertical == 1) ? Enumerable.Range(0, layer.Height) : Enumerable.Range(0, layer.Height).Reverse();
                var horizontalRange = (this.tmxMap.DrawOrderHorizontal == 1) ? Enumerable.Range(0, layer.Width) : Enumerable.Range(0, layer.Width).Reverse();

                foreach (TmxMesh mesh in layer.Meshes)
                {
                    Logger.WriteLine("Writing '{0}' mesh group", mesh.UniqueMeshName);
                    faceBuilder.AppendFormat("\ng {0}\n", mesh.UniqueMeshName);

                    foreach (int y in verticalRange)
                    {
                        foreach (int x in horizontalRange)
                        {
                            int tileIndex = layer.GetTileIndex(x, y);
                            uint tileId = mesh.GetTileIdAt(tileIndex);

                            // Skip blank tiles
                            if (tileId == 0)
                                continue;

                            TmxTile tile = this.tmxMap.Tiles[TmxMath.GetTileIdWithoutFlags(tileId)];

                            // What are the vertex and texture coorindates of this face on the mesh?
                            var position = this.tmxMap.GetMapPositionAt(x, y);
                            var vertices = CalculateFaceVertices(position, tile.TileSize, this.tmxMap.TileHeight, tile.Offset);

                            // If we're using depth shaders then we'll need to set a depth value of this face
                            float depth_z = 0.0f;
                            if (Tiled2Unity.Settings.DepthBufferEnabled)
                            {
                                depth_z = position.Y / mapLogicalHeight * -1.0f;
                            }

                            FaceVertices faceVertices = new FaceVertices { Vertices = vertices, Depth_z = depth_z };

                            // Is the tile being flipped or rotated (needed for texture cooridinates)
                            bool flipDiagonal = TmxMath.IsTileFlippedDiagonally(tileId);
                            bool flipHorizontal = TmxMath.IsTileFlippedHorizontally(tileId);
                            bool flipVertical = TmxMath.IsTileFlippedVertically(tileId);
                            var uvs = CalculateFaceTextureCoordinates(tile, flipDiagonal, flipHorizontal, flipVertical);

                            // Adds vertices and uvs to the database as we build the face strings
                            string v0 = String.Format("{0}/{1}/1", vertexDatabase.Add(faceVertices.V0) + 1, uvDatabase.Add(uvs[0]) + 1);
                            string v1 = String.Format("{0}/{1}/1", vertexDatabase.Add(faceVertices.V1) + 1, uvDatabase.Add(uvs[1]) + 1);
                            string v2 = String.Format("{0}/{1}/1", vertexDatabase.Add(faceVertices.V2) + 1, uvDatabase.Add(uvs[2]) + 1);
                            string v3 = String.Format("{0}/{1}/1", vertexDatabase.Add(faceVertices.V3) + 1, uvDatabase.Add(uvs[3]) + 1);
                            faceBuilder.AppendFormat("f {0} {1} {2} {3}\n", v0, v1, v2, v3);
                        }
                    }
                }
            }

            // Now go through any tile objects we may have and write them out as face groups as well
            foreach (var tmxMesh in this.tmxMap.GetUniqueListOfVisibleObjectTileMeshes())
            {
                // We're going to use this tile object
                groupCount++;

                Logger.WriteLine("Writing '{0}' tile group", tmxMesh.UniqueMeshName);
                faceBuilder.AppendFormat("\ng {0}\n", tmxMesh.UniqueMeshName);

                // Get the single tile associated with this mesh
                TmxTile tmxTile = this.tmxMap.Tiles[tmxMesh.TileIds[0]];

                var vertices = CalculateFaceVertices_TileObject(tmxTile.TileSize, tmxTile.Offset);
                var uvs = CalculateFaceTextureCoordinates(tmxTile, false, false, false);

                // TileObjects have zero depth on their vertices. Their GameObject parent will set depth.
                FaceVertices faceVertices = new FaceVertices { Vertices = vertices, Depth_z = 0.0f };

                // Adds vertices and uvs to the database as we build the face strings
                string v0 = String.Format("{0}/{1}/1", vertexDatabase.Add(faceVertices.V0) + 1, uvDatabase.Add(uvs[0]) + 1);
                string v1 = String.Format("{0}/{1}/1", vertexDatabase.Add(faceVertices.V1) + 1, uvDatabase.Add(uvs[1]) + 1);
                string v2 = String.Format("{0}/{1}/1", vertexDatabase.Add(faceVertices.V2) + 1, uvDatabase.Add(uvs[2]) + 1);
                string v3 = String.Format("{0}/{1}/1", vertexDatabase.Add(faceVertices.V3) + 1, uvDatabase.Add(uvs[3]) + 1);
                faceBuilder.AppendFormat("f {0} {1} {2} {3}\n", v0, v1, v2, v3);
            }

            // All of our faces have been built and vertex and uv databases have been filled.
            // Start building out the obj file
            StringWriter objWriter = new StringWriter();
            objWriter.WriteLine("# Wavefront OBJ file automatically generated by Tiled2Unity");
            objWriter.WriteLine();

            Logger.WriteLine("Writing face vertices");
            objWriter.WriteLine("# Vertices (Count = {0})", vertexDatabase.List.Count());
            foreach (var v in vertexDatabase.List)
            {
                objWriter.WriteLine("v {0} {1} {2}", v.X, v.Y, v.Z);
            }
            objWriter.WriteLine();

            Logger.WriteLine("Writing face uv coordinates");
            objWriter.WriteLine("# Texture cooridinates (Count = {0})", uvDatabase.List.Count());
            foreach (var uv in uvDatabase.List)
            {
                objWriter.WriteLine("vt {0} {1}", uv.X, uv.Y);
            }
            objWriter.WriteLine();

            // Write the one indexed normal
            objWriter.WriteLine("# Normal");
            objWriter.WriteLine("vn 0 0 -1");
            objWriter.WriteLine();

            // Now we can copy over the string used to build the databases
            objWriter.WriteLine("# Groups (Count = {0})", groupCount);
            objWriter.WriteLine(faceBuilder.ToString());

            return objWriter;
        }
        // Creates the text for a Wavefront OBJ file for the TmxMap
        private StringWriter BuildObjString()
        {
            HashIndexOf <Vertex3> vertexDatabase = new HashIndexOf <Vertex3>();
            HashIndexOf <PointF>  uvDatabase     = new HashIndexOf <PointF>();

            float mapLogicalHeight = this.tmxMap.MapSizeInPixels().Height;

            // Go through every face of every mesh of every visible layer and collect vertex and texture coordinate indices as you go
            int           groupCount  = 0;
            StringBuilder faceBuilder = new StringBuilder();

            foreach (var layer in this.tmxMap.Layers)
            {
                if (layer.Visible != true)
                {
                    continue;
                }

                if (layer.Ignore == TmxLayer.IgnoreSettings.Visual)
                {
                    continue;
                }

                // We're going to use this layer
                ++groupCount;

                // Enumerate over the tiles in the direction given by the draw order of the map
                var verticalRange   = (this.tmxMap.DrawOrderVertical == 1) ? Enumerable.Range(0, layer.Height) : Enumerable.Range(0, layer.Height).Reverse();
                var horizontalRange = (this.tmxMap.DrawOrderHorizontal == 1) ? Enumerable.Range(0, layer.Width) : Enumerable.Range(0, layer.Width).Reverse();

                foreach (TmxMesh mesh in layer.Meshes)
                {
                    Program.WriteLine("Writing '{0}' mesh group", mesh.UniqueMeshName);
                    faceBuilder.AppendFormat("\ng {0}\n", mesh.UniqueMeshName);

                    foreach (int y in verticalRange)
                    {
                        foreach (int x in horizontalRange)
                        {
                            int  tileIndex = layer.GetTileIndex(x, y);
                            uint tileId    = mesh.GetTileIdAt(tileIndex);

                            // Skip blank tiles
                            if (tileId == 0)
                            {
                                continue;
                            }

                            TmxTile tile = this.tmxMap.Tiles[TmxMath.GetTileIdWithoutFlags(tileId)];

                            // What are the vertex and texture coorindates of this face on the mesh?
                            var position = this.tmxMap.GetMapPositionAt(x, y);
                            var vertices = CalculateFaceVertices(position, tile.TileSize, this.tmxMap.TileHeight, tile.Offset);

                            // If we're using depth shaders then we'll need to set a depth value of this face
                            float depth_z = 0.0f;
                            if (Program.DepthBufferEnabled)
                            {
                                depth_z = position.Y / mapLogicalHeight * -1.0f;
                            }

                            FaceVertices faceVertices = new FaceVertices {
                                Vertices = vertices, Depth_z = depth_z
                            };

                            // Is the tile being flipped or rotated (needed for texture cooridinates)
                            bool flipDiagonal   = TmxMath.IsTileFlippedDiagonally(tileId);
                            bool flipHorizontal = TmxMath.IsTileFlippedHorizontally(tileId);
                            bool flipVertical   = TmxMath.IsTileFlippedVertically(tileId);
                            var  uvs            = CalculateFaceTextureCoordinates(tile, flipDiagonal, flipHorizontal, flipVertical);

                            // Adds vertices and uvs to the database as we build the face strings
                            string v0 = String.Format("{0}/{1}/1", vertexDatabase.Add(faceVertices.V0) + 1, uvDatabase.Add(uvs[0]) + 1);
                            string v1 = String.Format("{0}/{1}/1", vertexDatabase.Add(faceVertices.V1) + 1, uvDatabase.Add(uvs[1]) + 1);
                            string v2 = String.Format("{0}/{1}/1", vertexDatabase.Add(faceVertices.V2) + 1, uvDatabase.Add(uvs[2]) + 1);
                            string v3 = String.Format("{0}/{1}/1", vertexDatabase.Add(faceVertices.V3) + 1, uvDatabase.Add(uvs[3]) + 1);
                            faceBuilder.AppendFormat("f {0} {1} {2} {3}\n", v0, v1, v2, v3);
                        }
                    }
                }
            }

            // Now go through any tile objects we may have and write them out as face groups as well
            foreach (var tmxMesh in this.tmxMap.GetUniqueListOfVisibleObjectTileMeshes())
            {
                // We're going to use this tile object
                groupCount++;

                Program.WriteLine("Writing '{0}' tile group", tmxMesh.UniqueMeshName);
                faceBuilder.AppendFormat("\ng {0}\n", tmxMesh.UniqueMeshName);

                // Get the single tile associated with this mesh
                TmxTile tmxTile = this.tmxMap.Tiles[tmxMesh.TileIds[0]];

                var vertices = CalculateFaceVertices_TileObject(tmxTile.TileSize, tmxTile.Offset);
                var uvs      = CalculateFaceTextureCoordinates(tmxTile, false, false, false);

                // TileObjects have zero depth on their vertices. Their GameObject parent will set depth.
                FaceVertices faceVertices = new FaceVertices {
                    Vertices = vertices, Depth_z = 0.0f
                };

                // Adds vertices and uvs to the database as we build the face strings
                string v0 = String.Format("{0}/{1}/1", vertexDatabase.Add(faceVertices.V0) + 1, uvDatabase.Add(uvs[0]) + 1);
                string v1 = String.Format("{0}/{1}/1", vertexDatabase.Add(faceVertices.V1) + 1, uvDatabase.Add(uvs[1]) + 1);
                string v2 = String.Format("{0}/{1}/1", vertexDatabase.Add(faceVertices.V2) + 1, uvDatabase.Add(uvs[2]) + 1);
                string v3 = String.Format("{0}/{1}/1", vertexDatabase.Add(faceVertices.V3) + 1, uvDatabase.Add(uvs[3]) + 1);
                faceBuilder.AppendFormat("f {0} {1} {2} {3}\n", v0, v1, v2, v3);
            }

            // All of our faces have been built and vertex and uv databases have been filled.
            // Start building out the obj file
            StringWriter objWriter = new StringWriter();

            objWriter.WriteLine("# Wavefront OBJ file automatically generated by Tiled2Unity");
            objWriter.WriteLine();

            Program.WriteLine("Writing face vertices");
            objWriter.WriteLine("# Vertices (Count = {0})", vertexDatabase.List.Count());
            foreach (var v in vertexDatabase.List)
            {
                objWriter.WriteLine("v {0} {1} {2}", v.X, v.Y, v.Z);
            }
            objWriter.WriteLine();

            Program.WriteLine("Writing face uv coordinates");
            objWriter.WriteLine("# Texture cooridinates (Count = {0})", uvDatabase.List.Count());
            foreach (var uv in uvDatabase.List)
            {
                objWriter.WriteLine("vt {0} {1}", uv.X, uv.Y);
            }
            objWriter.WriteLine();

            // Write the one indexed normal
            objWriter.WriteLine("# Normal");
            objWriter.WriteLine("vn 0 0 -1");
            objWriter.WriteLine();

            // Now we can copy over the string used to build the databases
            objWriter.WriteLine("# Groups (Count = {0})", groupCount);
            objWriter.WriteLine(faceBuilder.ToString());

            return(objWriter);
        }
Beispiel #6
0
 private static void UpdateFaceVertices(List <int> translationIndex, List <int> translationOffset, ref FaceVertices faceVertices)
 {
     faceVertices.index1 = translationIndex[faceVertices.index1] + translationOffset[faceVertices.index1];
     faceVertices.index2 = translationIndex[faceVertices.index2] + translationOffset[faceVertices.index2];
     faceVertices.index3 = translationIndex[faceVertices.index3] + translationOffset[faceVertices.index3];
     faceVertices.index4 = translationIndex[faceVertices.index4] + translationOffset[faceVertices.index4];
 }
Beispiel #7
0
        public Geoset1300(BinaryReader br)
        {
            TotalSize = br.ReadUInt32();
            long end = TotalSize + br.BaseStream.Position;

            //Vertices
            if (br.HasTag("VRTX"))
            {
                NrOfVertices = br.ReadUInt32();
                for (int i = 0; i < NrOfVertices; i++)
                {
                    Vertices.Add(new CVector3(br));
                }
            }

            //Normals
            if (br.HasTag("NRMS"))
            {
                NrOfNormals = br.ReadUInt32();
                for (int i = 0; i < NrOfNormals; i++)
                {
                    Normals.Add(new CVector3(br));
                }
            }

            //TexCoords
            if (br.HasTag("UVAS"))
            {
                NrOfTexCoords = br.ReadUInt32();                 //Amount of groups
                for (int i = 0; i < NrOfNormals * NrOfTexCoords; i++)
                {
                    TexCoords.Add(new CVector2(br));
                }
            }

            //Face Group Type
            if (br.HasTag("PTYP"))
            {
                NrOfFaceTypeGroups = br.ReadUInt32();
                FaceTypes.AddRange(br.ReadBytes((int)NrOfFaceTypeGroups));
            }

            //Face Groups
            if (br.HasTag("PCNT"))
            {
                NrOfFaceGroups = br.ReadUInt32();
                for (int i = 0; i < NrOfFaceGroups; i++)
                {
                    FaceGroups.Add(br.ReadUInt32());
                }
            }

            //Indexes
            if (br.HasTag("PVTX"))
            {
                NrOfFaceVertices = br.ReadUInt32();
                for (int i = 0; i < NrOfFaceVertices / 3; i++)
                {
                    FaceVertices.Add(new CVertex(br));
                }
            }

            //Vertex Groups
            if (br.HasTag("GNDX"))
            {
                NrOfVertexGroupIndices = br.ReadUInt32();
                VertexGroupIndices.AddRange(br.ReadBytes((int)NrOfVertexGroupIndices));
            }

            //Matrix Groups
            if (br.HasTag("MTGC"))
            {
                NrOfMatrixGroups = br.ReadUInt32();
                for (int i = 0; i < NrOfMatrixGroups; i++)
                {
                    MatrixGroups.Add(br.ReadUInt32());
                }
            }

            //Matrix Indexes
            if (br.HasTag("MATS"))
            {
                NrOfMatrixIndexes = br.ReadUInt32();
                for (int i = 0; i < NrOfMatrixIndexes; i++)
                {
                    MatrixIndexes.Add(br.ReadUInt32());
                }
            }

            //Bone Indexes
            if (br.HasTag("BIDX"))
            {
                NrOfBoneIndexes = br.ReadUInt32();
                for (int i = 0; i < NrOfBoneIndexes; i++)
                {
                    BoneIndexes.Add(br.ReadUInt32());
                }
            }

            //Bone Weights
            if (br.HasTag("BWGT"))
            {
                NrOfBoneWeights = br.ReadUInt32();
                for (int i = 0; i < NrOfBoneWeights; i++)
                {
                    BoneWeights.Add(br.ReadUInt32());
                }
            }

            MaterialId     = br.ReadUInt32();
            SelectionGroup = br.ReadUInt32();
            Unselectable   = br.ReadUInt32() == 1;
            Bounds         = new CExtent(br);

            //Extents
            NrOfExtents = br.ReadUInt32();
            for (int i = 0; i < NrOfExtents; i++)
            {
                Extents.Add(new CExtent(br));
            }

            //Grouped Vertices
            for (int i = 0; i < NrOfVertices; i++)
            {
                if (!GroupedVertices.ContainsKey(VertexGroupIndices[i]))
                {
                    GroupedVertices.Add(VertexGroupIndices[i], new List <CVector3>());
                }

                GroupedVertices[VertexGroupIndices[i]].Add(Vertices[i]);
            }
        }
Beispiel #8
0
        private static void AppendTexturedFaces(Dictionary <BlockFaceTexture, List <TexturedFace> > texturedFaces,
                                                TextureCoordinateDictionary textureCoordinates,
                                                Block blockType, Volume volume, Face face, FaceVertices faceVertices)
        {
            BlockFaceTexture blockFaceTexture = blockType.GetFaceTexture(face);

            TexturedFace        texturedFace = new TexturedFace(volume, face, faceVertices);
            List <TexturedFace> texturedFacesList;

            if (texturedFaces.TryGetValue(blockFaceTexture, out texturedFacesList))
            {
                texturedFacesList.Add(texturedFace);
            }
            else
            {
                texturedFacesList = new List <TexturedFace>();
                texturedFacesList.Add(texturedFace);
                texturedFaces.Add(blockFaceTexture, texturedFacesList);
            }
            textureCoordinates.EnsureExists(texturedFace.textureMapping);
        }
Beispiel #9
0
        public override string ToString()
        {
            const int maxDecimalPlaces = 6;

            StringBuilder sb = new StringBuilder();

            for (int idx = 0; idx < vertices.Count; idx++)
            {
                CoordinateDecimal coord = vertices[idx];
                sb.AppendLine(string.Format("v {0:0.0#####} {1:0.0#####} {2:0.0#####}",
                                            Math.Round(coord.X, maxDecimalPlaces),
                                            Math.Round(coord.Y, maxDecimalPlaces),
                                            Math.Round(coord.Z, maxDecimalPlaces)));
            }

            for (int idx = 0; idx < textureCoordinates.mappingList.Count; idx++)
            {
                Point point = textureCoordinates.mappingList[idx];
                sb.AppendLine(string.Format("vt {0:0.0#####} {1:0.0#####}",
                                            point.X, point.Y));
            }

            foreach (KeyValuePair <string, List <FaceVertices> > pair in collisionBoxes)
            {
                sb.AppendLine("g " + pair.Key);
                List <FaceVertices> listOfTexturedFaces = pair.Value;
                for (int idx = 0; idx < listOfTexturedFaces.Count; idx++)
                {
                    FaceVertices faceVertices = listOfTexturedFaces[idx];


                    sb.AppendLine(string.Format("f {0} {1} {2} {3}",
                                                1 + faceVertices.index1,
                                                1 + faceVertices.index2,
                                                1 + faceVertices.index3,
                                                1 + faceVertices.index4
                                                ));
                }
            }

            foreach (KeyValuePair <BlockFaceTexture, List <TexturedFace> > pair in texturedFaces)
            {
                sb.AppendLine("g " + pair.Key.ToString());
                sb.AppendLine("usemtl " + pair.Key.ToString());
                List <TexturedFace> listOfTexturedFaces = pair.Value;
                for (int idx = 0; idx < listOfTexturedFaces.Count; idx++)
                {
                    TexturedFace texturedFace = listOfTexturedFaces[idx];

                    int texIndex1;
                    int texIndex2;
                    int texIndex3;
                    int texIndex4;
                    textureCoordinates.GetMapping(texturedFace.textureMapping,
                                                  out texIndex1, out texIndex2, out texIndex3, out texIndex4);

                    sb.AppendLine(string.Format("f {0}/{4} {1}/{5} {2}/{6} {3}/{7}",
                                                1 + texturedFace.faceVertices.index1,
                                                1 + texturedFace.faceVertices.index2,
                                                1 + texturedFace.faceVertices.index3,
                                                1 + texturedFace.faceVertices.index4,
                                                1 + texIndex1,
                                                1 + texIndex2,
                                                1 + texIndex3,
                                                1 + texIndex4
                                                ));
                }
            }



            return(sb.ToString());
        }