Applying a texture to a mesh for a tilemap

I have a mesh that is created via code, it is a flat-ish plane (there are heights on some tiles). It is made for a tilemap. I have some tile data layed out but I understand there are several ways of applying a texture to the mesh. The two ways I know of would be to essentially double the number of vertices and then I can UV map a texture atlas, or to just create a texture that spans the entire map from a color array. What are your thoughts on this, what other methods are there? How would you compare these two methods that I know of?

Here is the mesh generation code if you wanted to have a look:

//generate mesh data
Vector3[] verticies = new Vector3[(x + 1) * (y + 1)];
int[] triangles = new int[(3 * 2) * (x * y)];
Vector3[] normals = new Vector3[verticies.Length];
Vector2[] uv = new Vector2[verticies.Length];

for (int i=0; i<x+1; i+=1) //verticies, normals, and uv maps
{
    for (int ii=0; ii<y+1; ii+=1)
    {
        verticies[ii * (x + 1) + i] = new Vector3(i * tileSize.x, 0f, ii * tileSize.y) - new Vector3(Mathf.Floor((x * tileSize.x) / 2), Random.Range(0f, .1f), Mathf.Floor((y * tileSize.y) / 2));
        normals[ii * (x + 1) + i] = Vector3.up;
        uv[ii * (x + 1) + i] = new Vector2((float)i / x, (float)ii / y);
    }
}

for (int i=0; i<x; i+=1) //triangles
{
    for (int ii=0; ii<y; ii+=1)
    {
        int tileIndex = (ii * x + i) * 6;
        triangles[tileIndex + 0] = ii * (x + 1) + i;
        triangles[tileIndex + 1] = (ii + 1) * (x + 1) + i;
        triangles[tileIndex + 2] = (ii + 1) * (x + 1) + (i + 1);

        triangles[tileIndex + 3] = ii * (x + 1) + i;
        triangles[tileIndex + 4] = (ii + 1) * (x + 1) + (i + 1);
        triangles[tileIndex + 5] = ii * (x + 1) + (i + 1);
        }
}

x and y are the tile map size, and tileSize is a vector2 that controls the size of each tile

And as always, thanks for your help in advance Unity Community :slight_smile:

This is probably my favourite. If you’re targeting DX11 you could also just output points, and let the GPU render quads at those points using shaders.

This would probably limit the resolution of the textures pretty heavily, or force you to split into a lot more mesh chunks. If you’re aiming for a single color per tile, you’d probably be better off just setting vertex colors rather than using a texture.

I have been working on something similar, although I’d been trying to use vertex colours to act like a splat map for smoother texture transitions than strict tiles. I haven’t been able to find (or write) a splat-type shader that works on Meshes instead of Terrains, though.

Currently I am using the UV map method with a texture atlas for the tiles. The problem I am having now is that there are small lines in between every tile. Not entirely sure why.


Look like a problem you guys would recognize? When I just use a single tile texture in stead of the atlas (though I still use the atlas code) the lines vanish.

Re: Lines between tiles - I’m not sure if you’re still looking for an answer to this, but I recently solved a similar-looking problem. The issue seemed to be that Unity has a hard time applying the textures precisely, so I reworked my atlas to have a pixel or two of “bleed” around each sprite. Solved it for me and appeared to be the best option after a reasonable amount of research.

See more info here: Tile Map Tearing Problems - Unity Engine - Unity Discussions