- Home /
How do I apply a tilesheet to a procedural mesh and prevent bleeding?
I have the following code and I am struggling to wrap my head around UV mapping on a procedural plane made up for vertices and triangles to form a grid. I have changed he direction of the mesh to form out from 0,0 of the gameobject. Here is how I am making my mesh:
public float tUnit = 10.0f;
floorMap = new int[width,height];
mesh = GetComponent<MeshFilter>().mesh;
for(int x = 0; x < width; x++) {
for(int y = 0; y < height; y++) {
floorMap[x,y] = Random.Range(0,2);
MakeTile(x,y);
}
}
}
void MakeTile(int x, int y) {
newVertices.Add(new Vector3(x,0,y));
newVertices.Add(new Vector3(x + 1,0,y));
newVertices.Add(new Vector3(x + 1,0,y + 1));
newVertices.Add(new Vector3(x,0,y + 1));
newTriangles.Add((squareCount * 4) + 3);
newTriangles.Add((squareCount * 4) + 1);
newTriangles.Add((squareCount * 4) + 0);
newTriangles.Add((squareCount * 4) + 3);
newTriangles.Add((squareCount * 4) + 2);
newTriangles.Add((squareCount * 4) + 1);
squareCount++;
}
As you can see in the order I am adding the triangles it is a little different than common methods of doing this. This is just so that the mesh expands out from the gameobject's 0,0 position correctly.
I am using the following function to convert a single int value (0 to 99) to the correct area of the tilesheet image I am using to place on the 3d mesh:
Vector2 TextureMap(int _number) {
return new Vector2((_number - (_number % 10)) / 10,_number % 10);
}
For each of the tiles, I am looping through the following code:
Vector2 uv = TextureMap(floorMap[x,y]);
Vector2 uv1 = new Vector2(1 / tUnit,0);
Vector2 uv2 = new Vector2(1 / tUnit,1 / tUnit);
Vector2 uv3 = new Vector2(0,1 / tUnit);
Vector2 uv4 = new Vector2(0,0);
newUV.Add(uv1);
newUV.Add(uv2);
newUV.Add(uv3);
newUV.Add(uv4);
This works to some degree but doesn't take into consideration the actual offset of the tile from the tilesheet that I want to use. It just does the very first tile. Also the bigger part of the problem is that it seems to bleed the texture from the one next to it creating ugly lines between each square. I know that I have to use the Vector2 I am receiving back from my TextureMap function to offset to the right tile. Research also suggests that I need to implement "half pixel correction" into this method but I am unsure of how to implement each of these 2 things. I have tried a number of solutions and I believe the core problem I am having is that by swapping the order I am creating my triangles I am confusing myself too much. Making my problem too specific to directly implement solutions found on other questions on UA.
Any help would be appreciated.
Your answer
Follow this Question
Related Questions
UV Tiling/repeat 1 Answer
Animated tiles and blending between different tiles on a mesh tilemap 0 Answers
Procedural Mesh UVing 2 Answers
Updating UV in imported mesh without having to re-material 0 Answers