Guidance with setting mesh.uv

I am hoping to get some guidance with texturing a custom mesh.

I am creating a custom mesh, its just a 10 x 10 plane based on the code for the heightmap scene in the procedural example. This works well.

What I want to do is take a texture I have (ie a tilesheet) and then from my map array, assign different parts of the texture to different parts of the mesh.

I’m not sure now though how to translate the uv texture to the mesh. I guess Im trying to get a UV rectangle ( x, y, w, h) mapped onto the mesh at coordinates / faces I can control.

A picture is worth a 1000 words so I am attaching what I wish to do.

Many thanks for any pointers or guidance.

  • First of all, make sure that each quad in your 10x10 mesh has unique vertices (not shared with any other quad). Perhaps your mesh already has that, but it’s quite likely your mesh shares vertices between quads for performance reasons. While shared vertices are nice, they do not allow the uv-mapping you need. If you need to change this, it will also have significant impact on your mesh deform code.

  • Then traverse all 10x10 quads and assign the 4 corner coordinates of their corresponding rectangle in the texture to the the uv of each vertex of the quad.

You will need to re-assign the uv of a quad whenever its mapping changes. You do not have to reassign when the mesh deforms.