Layer.renderUv C# (CSharp) Method

renderUv() public method

public renderUv ( ) : List
return List
    public List<Vector2> renderUv()
    {
        List<Vector2> uv = new List<Vector2> ();
        int horizontalCellCount = _tileset.materialWidth / (_tileset.width + _tileset.tileBorder);
        int verticalCellCount = _tileset.materialHeight / (_tileset.height + _tileset.tileBorder);
        float cellWidth = ((float)_tileset.width / _tileset.materialWidth);
        float cellHeight = ((float)_tileset.height / _tileset.materialHeight);
        float borderWidth = ((float)_tileset.tileBorder / _tileset.materialWidth);
        float borderHeight = ((float)_tileset.tileBorder / _tileset.materialHeight);
        int totalCells = _width * _height;
        int dataValue;
        for (int i = 0; i < totalCells; i++) {
                dataValue = int.Parse(_data [i].ToString ().Trim ());
                if (dataValue != 0) {
                    dataValue = dataValue - _tileset.firstGID;
                    int posY = dataValue / verticalCellCount;
                    int posX = dataValue % horizontalCellCount;
                    float u = ((cellWidth + borderWidth) * posX) + borderWidth/2;
                    float v = 1.0f - ((cellHeight + borderHeight) * posY) - borderHeight/2;
                    uv.AddRange (new Vector2[] {
                        new Vector2 (u + cellWidth, v),
                        new Vector2 (u + cellWidth, v - cellHeight),
                        new Vector2 (u, v),
                        new Vector2 (u, v - cellHeight),
                    });
                }
        }
        return uv;
    }

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::renderUv