I have a general question about how people handle applying textures to tilemaps. I realize I can load data from a sprite, add it to an atlas, and then use those textures. However, it seems like each meshrenderer can only have one material, and one texture.
Does this mean that if I had a 100x100 tilemap, that I’d have to apply only one texture to the whole thing? So, for that one texture, I’d have to set groups of pixels using texture.SetPixels?
For example, a 10x10 tilemap, where each tile is 32 pixels.
First tile is a grass tile. So I set pixels 0,0, 32, 32 to load from a texture in my atlas.
Next tile is rock tile. So I set pixels 32, 0, 64, 32 to load from a texture in my atlas.
Is there no better way of doing this? Do I have to handle everything through SetPixels? Is there no way I can just tell the material to use a certain texture at a certain location?
Any advice on how this is generally handled would be greatly appreciated. Thanks again.
verts is the vertex positions in space - this is not what you want. You want to set the UVs. There is plenty of literature out there - but essentially they are an array of Vector2’s that represent a position on a texture in a 0-1 range.
As much as I appreciate your help, I’m afraid that still doesn’t explain how I use an array of UV’s to map textures to my mesh. I’m looking everywhere online to see what I can find, but there doesn’t seem to be anything that ties UV’s to textures.
In terms of Unity - verts, uv, and normals are all arrays that need to be the same size. Unity looks per-index to gather information about a mesh in order to draw it (or rather - send it to the GPU so the GPU can draw it).
So if you make a simple triangle - it has a Vector3[3] for verts and a Vector2[3] for uv. verts[0] uses uv[0] for its texture coordinates - so on and so on.
UV values are from 0-1, like I said. Think of it as a “percentage” of the texture dimension. So say I have a 1024x1024 texture that has 4 512x512 sprites on it. To map a quad to one of those sprites I’d set my UVs to (0,0), (0, 0.5), (0.5, 0), and (0.5, 0.5). The 0.5 is “50%” of the texture heigh/width or 512 pixels in this case. This is done so that resizing the texture has no effect on how that texture maps to a mesh. I could make a 2048x2048 texture with 4 1024x1024 sprites on it and the mesh would still map correctly.