Layer.renderVertices C# (CSharp) Method

renderVertices() public method

public renderVertices ( ) : List
return List
    public List<Vector3> renderVertices()
    {
        int dataIndex = 0;
        //_currentLayerID = _currentLayerID * 10;
        float z = _currentLayerID * -10;
        List<Vector3> vertices = new List<Vector3> ();
        for (int i = 1; i <= _height; i++) {
            for (int j = 1; j <= _width; j++) {
                string dataValue = _data [dataIndex].ToString ().Trim ();
                if (dataValue != "0") {
                    vertices.AddRange (new Vector3[] {
                            new Vector3 (_tileset.width * (j + 1), _tileset.height * (-i + 1), z),
                            new Vector3 (_tileset.width * (j + 1), _tileset.height * -i, z),
                            new Vector3 (_tileset.width * j, _tileset.height * (-i + 1), z),
                            new Vector3 (_tileset.width * j, _tileset.height * -i, z),
                        });
                    _vertexCount += 4;
                }
                dataIndex++;
            }
        }
        return vertices;
    }

Usage Example

Example #1
0
    public Mesh CreateMesh()
    {
        // We use the currentLayer ID to order them on the Z axis.
        int currentLayerID = 0;

        // UsedVertices is used to maintain a count of the vertices between
        //layers so when you call renderTriangles, it nows where to start.
        int usedVertices = 0;

        TileSet tileset = null;
        List<Vector3> vertices = new List<Vector3> ();
        List<Vector2> uv = new List<Vector2> ();
        List<int> triangles = new List<int> ();
        Mesh mesh = new Mesh ();

        // What we're doing here is simple: Load the xml file from the attributes
        // And start parsing. Once it finds a tileset element it creates the tileset object
        // (it will be the first thing it encounters)
        XmlDocument xmldoc = new XmlDocument ();
        xmldoc.Load (new StringReader (tilemap.text));
        XmlNodeList nodelist = xmldoc.DocumentElement.ChildNodes;
        foreach (XmlNode outerNode in nodelist) {
            switch (outerNode.Name) {
            case "tileset":
                // Basically we just grab the data from the xml and build a Tileset object
                // To avoid problems with the collision tileset since we only store one tileset
                // we ignore anything with Collision inside.
                if (outerNode.Attributes ["name"].InnerText.IndexOf ("Collision") == -1) {
                    XmlNode imageNode = outerNode.SelectSingleNode ("image");
                    int firstGID = int.Parse (outerNode.Attributes ["firstgid"].InnerText);
                    int width = int.Parse (outerNode.Attributes ["tilewidth"].InnerText);
                    int height = int.Parse (outerNode.Attributes ["tileheight"].InnerText);
                    int imageWidth = int.Parse (imageNode.Attributes ["width"].InnerText);
                    int imageheight = int.Parse (imageNode.Attributes ["height"].InnerText);
                    int tileBorder = 0;
                    if (outerNode.Attributes ["spacing"] != null)
                        tileBorder = int.Parse (outerNode.Attributes ["spacing"].InnerText);
                    tileset = new TileSet (firstGID, width, height, imageWidth, imageheight, tileBorder);
                }
                break;
            case "layer":
                // First we build the layer object and then just call the renderVertices
                // renderUV and renderTriangles to build our textured mesh.
                // Finally we store the vertexcount and +1 to the currentLayerID.
                // like in the tileset, we avoid using the "collision_" layers since
                // they only contain collision info
                if (outerNode.Attributes ["name"].InnerText.IndexOf ("collision_") == -1) {
                    XmlNode dataNode = outerNode.SelectSingleNode ("data");
                    int layerWidth = int.Parse (outerNode.Attributes ["width"].InnerText);
                    int layerHeight = int.Parse (outerNode.Attributes ["height"].InnerText);
                    string csvData = dataNode.InnerText;
                    Layer currentLayer = new Layer (tileset, csvData, currentLayerID, layerWidth, layerHeight);
                    vertices.AddRange (currentLayer.renderVertices ());
                    uv.AddRange (currentLayer.renderUv ());
                    triangles.AddRange (currentLayer.renderTriangles (usedVertices, usedVertices+currentLayer.vertexCount));
                    usedVertices += currentLayer.vertexCount;
                }
                currentLayerID += 1;
                break;
            }
        }

        mesh.vertices = vertices.ToArray ();
        mesh.uv = uv.ToArray ();
        mesh.triangles = triangles.ToArray ();
        return mesh;
    }
All Usage Examples Of Layer::renderVertices