- Home /
Rounding Error in Texture Creation?
Hi there,
I am creating a simple 2D tile game. To approach the task of creating lots of tiles at runtime I have decided to create a mesh and apply a texture created procedurally over top. To accomplish this I have a reference tileMap which contains every sort of tile. I then assign the UVs to the map. This method works fairly well but a gap appears between certain tiles. Any idea why?
Below I inserted 2 functions, buildTexture is creating the texture itself and chopTileMap returns a 2D array with Vector2s which are unique to each tile. This array can then be used such that the array of Vector2s and index 0 will return the 4 coordinates of the bottom left tile in my tile map.
void buildTexture(){
Mesh mesh = GetComponent<MeshFilter>().mesh;
Vector2[][] tileMap = chopTileMap();
List<Vector2> uvList = new List<Vector2>();
mesh.GetUVs(0, uvList);
Vector2[] uvs = uvList.ToArray();
//To set a UV it has to be given the fraction regarding to the tile map out of 1
//tileMap is set as tileMap[tileID][Point on tile (0-3)]
//for each UV, assign the set of 4 to points on the tile map
for (int r = 0; r < rowCount; r++){
for (int c = 0; c < colCount; c++){
//int tileIndex = Random.Range(0, tileMap.Length-1);
int tileIndex = 2;
uvs[((r * colCount + c) * 4) + 0] = tileMap[tileIndex][0];
uvs[((r * colCount + c) * 4) + 1] = tileMap[tileIndex][1];
uvs[((r * colCount + c) * 4) + 2] = tileMap[tileIndex][2];
uvs[((r * colCount + c) * 4) + 3] = tileMap[tileIndex][3];
}
}
textureMap.Apply();
mesh.uv = uvs;
MeshRenderer meshRenderer = GetComponent<MeshRenderer>();
meshRenderer.materials[0].mainTexture = textureMap;
}
Vector2[][] chopTileMap(){
int numColsOfMap = textureMap.width / tileResolution;
int numRowsOfMap = textureMap.height / tileResolution;
Vector2[][] tiles = new Vector2[numColsOfMap * numRowsOfMap][];
for (int r = 0; r < numRowsOfMap; r++){
for (int c = 0; c < numColsOfMap; c++){
tiles[r * numColsOfMap + c] = new Vector2[4];
tiles[r * numColsOfMap + c][0] = new Vector2((float)(c) / numColsOfMap, (float)(r) / numRowsOfMap);
tiles[r * numColsOfMap + c][1] = new Vector2((float)(c+1) / numColsOfMap, (float)(r) / numRowsOfMap);
tiles[r * numColsOfMap + c][2] = new Vector2((float)(c) / numColsOfMap, (float)(r+1) / numRowsOfMap);
tiles[r * numColsOfMap + c][3] = new Vector2((float)(c+1) / numColsOfMap, (float)(r+1) / numRowsOfMap);
}
}
return tiles;
}
Notice the blue seams in the texture below.. They are quite hard to see but the most predominant one is vertical next to the player and are much more noticeable in the game.
Your answer
Follow this Question
Related Questions
Create planar UVs on procedurally generated mesh 1 Answer
Do I need to split my mesh up to properly UV texture it? 1 Answer
How do I go about texturing a flat-shaded generated mesh? 1 Answer
How to texture walls in procedurally generated mesh? 1 Answer
Realtime mesh deformation or texturing 0 Answers