- Home /
How do I assign a mesh's UVs so that part of a part of a mesh is one part of the texture and another part of the mesh is another part of the texture
I'm using procedural generation to make a grid of tiles for a terraria like game. Right now I'm struggling because my plan was to use one texture that has every type of tile texture and give each tile (which are all apart of the same mesh) a tile type value and assign the UVs of the tile accordingly so that the texture of the tile is only one tile.
I basically want to do what this guy is proposing (http://answers.unity3d.com/questions/1389827/how-do-i-use-many-different-tile-textures-for-one.html) but I can't figure out how to map Uvs from one texture because I have more items in the uv array than the vertices array.
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(8, 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);
}
mesh.uv = uv;
}
} 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;
}
}
Your answer
Follow this Question
Related Questions
Creating caves in voxel procedural mesh 0 Answers
Procedural Mesh From Random List Of Veritces 2 Answers
How to set Tile UVs of a grid that is one mesh and shares vertices 1 Answer
What is wrong with this mesh editing code? 0 Answers
How to apply PBR material to Procedurally generated Mesh? 1 Answer