Voxel Game With Procedural Mesh How Does Work UVs

I’ve been working with procedural meshes for some time and there are a couple of things you need to know.

  1. First to answer your question about UVs. The uvs are a 2D unwrapped version of your mesh, but they exist only on a space between X = 0, Y = 0 and X = 1, Y = 1. The idea is pretty straight forward if your are working with boxes, a la minecraft style, for each of your terrain voxels the uvs for each of its faces should be:
    (0,0),(1,0),(1,1),(0,1).
    That way the face of the quad will take up ALL of the material surface and its textures.

  2. When creating a procedural mesh, each mesh section gets a material index assigned to it. So if you are creating your whole terrain as a single mesh section you will be able to assign a single material to it. What you need to do is assign a mesh section for each one of your boxes (voxels), and to each of the voxel faces that requiere a different material applied to it (so 6 mesh sections for a single voxel). You could optimize this by using more complex UVs so you could pack all 6 faces on different part of the UV map (think of the typical paper dice).

This is an example (very basic) on how you could set your texture and the uvs for your voxels:

283258-uv-example.png

In this image you can see that each voxel takes 6 spaces in the texture. each square represents a face of you cube.
So, for Face 1 you would set your UV as: (0,0),(0.25, 0),(0.25,0.25),(0,0.25). Face 2 as: (0.25, 0),(0.5,0),(0.5,0.25),(0, 0.25). The same for each of your faces.

Hope you can take something from this and find it useful. Make it a great day!