Voxel Game With Procedural Mesh How Does Work UVs

Hello community,

I’m working on a game in voxel like minecraft.

I found what do i need to make the mesh procedurally (procedural mesh component).
I already understand and work with vertex and triangle include triangle order.

My problem start with UVs, i understand that UVs is channel to apply material but i do not understand why they take a Vector2D as parameter and how to fill the array.

For now i try to optimize my mesh so for exemple if i got a big square i only use 4 vertex and 2 triangle but let’s imagine they need 2 material how can i achieve that? Cause in the documentation i found something that said the number of params in the uvs must be the same in the UVs.

Let s use minecraft as exemple you can have a plane surface taking 10x10 blocks but there can be all blocks possible dirt, stone,grass etc… so how this can work with only 4 vertex.

If you don t understand my question cause my english is to bad please let me know.

Wanting for answers thx.

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!