Intro to procedural UV wrapping

I’m writing code the procedurally generates some shapes, by generating triangles in the mesh. Now I’d like to try to figure out how UV mapping works so I can start to procedurally tie in textures to it.

Can anyone recommend some good tutorials to get started with? Especially ones that might talk about tying into the UV mapping through unity directly and C-sharp.

Setting the UVs themselves is no different from setting the normals or mesh colors, just set mesh.uvs = uvs, with the same number of UVs as mesh vertices, so if you’re already generating triangles you’re 90% there.

As for generating the UVs, that’s an endlessly deep topic. I’d suggest starting with tri-planar mapping – look at the face normal for each face, and whatever axis is the largest, project the UVs on the other dimensions. for example if the Y axis is the largest component in the normal, that means the face is mostly point up, use the XZ components of each vertex as UVs. If you do this for every face you end up with a reasonably good mapping for a tiling texture.

From there just try to generate mapping more specific to the geometry you’re generating.