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