- Home /
UV Tiling/repeat
I understand how UV coordinates works with regard to e.g. a simple quad - each vertex has a UV coordinate that ranges from 0,0 to 1,1 etc.
I'm procedurally generating a TileMap at the moment, where each tile contains two quads. I have a texture that I want to fill 2 quads, then do the same for each tile. Every other row is offset by one quad (half a tile - see pic below).
My question is: how would I map the texture correctly using Mesh.uv
?
At the moment I'm doing something like this:
for (int z = 0; z < Height; z++)
{
for (int x = 0; x < Width; x++)
{
lstUVs.Add((UVs.BottomLeft)+new Vector2(x,z));
lstUVs.Add((UVs.TopLeft) + new Vector2(x, z) );
lstUVs.Add((UVs.TopMiddle) + new Vector2(x, z) );
lstUVs.Add((UVs.BottomMiddle) + new Vector2(x, z));
lstUVs.Add((UVs.BottomMiddle) + new Vector2(x, z));
lstUVs.Add((UVs.TopMiddle) + new Vector2(x, z) );
lstUVs.Add((UVs.TopRight) + new Vector2(x, z) );
lstUVs.Add((UVs.BottomRight) + new Vector2(x, z));
}
}
mesh.uv = lstUVs.ToArray();
UVs.TopLeft
etc are all just some static Vector2
s containing e.g. 0,0, 0,1 etc depending which 'corner' they are.
That code works for the first row (the texture is displayed correctly), but all the other rows don't work (see attached pic).
I have checked that all the UV coordinates match up with the correct vertices during debug, it all looks correct to me in the data structure, so my conclusion is that I must be misunderstanding how UV coordinates work; to me it almost looks like the rows are 'sharing' the Z vertexes.
I tried applying a 0.5f X offset to the 'bottom' UV of each tile on an odd row, but that just screwed up the first row and made no difference to the rest.
So I'm guessing I have some misunderstanding of how UVs work; can someone enlighten me?
Thanks
*yes, the half-tile offset of the geometry every other row is intentional. The diagonal slant of the texture is not.
EDIT: If I remove the geometry offset from the 'odd' rows (so it's just a simple grid), my code works flawlessly - the texture tiles as it should. I have no idea why the offset screws it up.
Answer by AxeSlash · Apr 09, 2015 at 02:14 PM
I figured this out; in case anyone else has similar problems, I was setting mesh.triangles incorrectly; I made the mistake of thinking that a Vector3 was a reference type, and was using List.IndexOf to get the index of a vertex. Hence, it would return the first vertex with the same X, Y and Z coordinates as the one I passed it, instead of the correct instance of that Vector3, which existed further down the list.