- Home /
How to set Tile UVs of a grid that is one mesh and shares vertices
I have a grid of tiles. The tiles share vertices as one mesh. I want to have 1 texture that is referenced from every tile to determine UVs of each tile depending on tile type. The attached image is the example texture sheet I'm using. Here are the scripts any help would be nice.
using System.Collections; using System.Collections.Generic; using UnityEngine;
[RequireComponent(typeof(MeshFilter), typeof(MeshRenderer))] public class Grid : MonoBehaviour { private Mesh mesh; private Vector3[] vertices; private Vector2[] uv; public Tile[] tileArray; public int xSize, ySize; public float unitsPerTile = 1; public int tilePerLineOfTextureX; public int tilePerLineOfTextureY;
private void Awake()
{
GenerateGrid();
}
private void GenerateGrid()
{
GetComponent<MeshFilter>().mesh = mesh = new Mesh();
mesh.name = "Procedural Grid";
vertices = new Vector3[(xSize + 1) * (ySize + 1)];
for (int i = 0, y = 0; y <= ySize; y++)
{
for (int x = 0; x <= xSize; x++, i++)
{
vertices[i] = new Vector3(x * unitsPerTile, y * unitsPerTile);
}
}
mesh.vertices = vertices;
tileArray = new Tile[xSize * ySize];
int[] triangles = new int[xSize * ySize * 6];
for (int ti = 0, vi = 0, y = 0; y < ySize; y++, vi++)
{
for (int x = 0; x < xSize; x++, ti += 6, vi++)
{
triangles[ti] = vi;
triangles[ti + 3] = triangles[ti + 2] = vi + 1;
triangles[ti + 4] = triangles[ti + 1] = vi + xSize + 1;
triangles[ti + 5] = vi + xSize + 2;
tileArray[ti / 6] = new Tile(7, x + 1, y + 1);
}
}
mesh.vertices = vertices;
GenerateUVs();
mesh.triangles = triangles;
mesh.RecalculateBounds();
mesh.RecalculateNormals();
}
private void GenerateUVs()
{
Vector2[] uv = new Vector2[tileArray.Length * 4];
float xIndent = ((float)1 / tilePerLineOfTextureX);
float yIndent = ((float)1 / tilePerLineOfTextureY);
for (int i = 0, t = 0; i < uv.Length; t ++, i += 4)
{
float x = ((float)tileArray[t].tileType % tilePerLineOfTextureX) / (tilePerLineOfTextureX);
float y = Mathf.Floor(tileArray[t].tileType / tilePerLineOfTextureX) / (tilePerLineOfTextureY);
uv[i] = new Vector2(x + xIndent, y);
uv[i + 1] = new Vector2(x, y);
uv[i + 2] = new Vector2(x + xIndent, y + yIndent);
uv[i + 3] = new Vector2(x, y + yIndent);
//print((float)uv[i].x);
//print((float)uv[i].y);
//print((float)uv[i + 1].x);
//print((float)uv[i + 1].y);
//print((float)uv[i + 2].x);
//print((float)uv[i + 2].y);
//print((float)uv[i + 3].x);
//print((float)uv[i + 3].y);
}
mesh.uv = uv;
for (int i = 0; i < uv.Length; i++)
{
//print((float)uv[i].x);
//print((float)uv[i].y);
}
}
}
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class Tile {
public int tileType;
public int xPoz;
public int yPoz;
public Tile(int typeOfTile, int x, int y)
{
tileType = typeOfTile;
xPoz = x;
yPoz = y;
}
}
Answer by Bunny83 · Oct 27, 2017 at 12:05 PM
You can not share vertices if the vertices that are supposed to be shared should have difference vertex attributes. Vertec attributes are: position, normal, uv, uv2, uv3, uv4, tangents, colors and even boneweights. If any of those attributes (if they exist) should be different for two vertices they need to be seperate vertices. A cube mesn has 8 "logical" vertices but it requires 24 vertices to actually become a cube. At each corner you have 3 different vertices all with the same position but different normals and maybe different UV coordinates in order to texture each side seperately.
Since it seems you want to map each "tile" to a completely different portion of the texture the 4 corners of each tile need to have seperate vertices. Also your UV addressing method doesn't make much sense. You access 4 vertices in a row and try to assign rectangular UV coordinates to them. However you have layed out your vertices in rows. So 4 consecutive vertices almost always are in a staight line. To access the correct vertices you would need to do the same as you did when creating your triangles. Two vertices are in line "y" and the other two vertices are in line "y+1". However as i said you can't share vertices so you need to create 4 seperate vertices for each tile.
It might be easier to lay them out in packs of 4 in the vertices array. If the mesh only consists of "quads" you can also use Mesh.SetIndices with MeshTopology.Quads. That way you only need 4 indices instead of 6 for each quad. It also simplifies the index generation if you create the vertices the way i mentioned before.
To sum up:
You need seperate vertices for each tile. That means you need
4 * xTileCount * yTileCount
vertices. Note i said "TileCount", not vertex row or column count.To create the vertices you just do a double for loop for x and y (as you're currently do, just not for vertex rows / columns but the actual tilecount) and inside the loop you create the 4 vertices for each tile and add them linearly to the vertex array.
Using Quads the index generation would be as simple as
for(int I = 0; i < vertexCount; i++) indices[i] = i;
You could create the UV right in the same loop as you generate the vertex positions.