apply a box uv mapping to a mesh at runtime

I am looking for any hint how to apply a box uv mapping to a mesh. The documentation example covers just a basic planar mapping and I can not manage how to convert it to a box mapping.

Suppose I put a box primitive onto a scene and when I apply the uv to it:

uvs[j] = Vector2(- vertices[j].x, - vertices[j].z  + vertices[j].y); //I get textures except left/right plane
			
uvs[j] = Vector2(-vertices[j].x - vertices[j].z , vertices[j].y ); //I get textures except top/bottom plane

So I was trying to combine it with try/error method, but no satisfactory results

This is a very involved task. Basically you’d need to roll you’re own uv autounwrap which is no small feat.

  1. you would have to parse the mesh and divide the polygons into groups that align to each respective direction. Basically get the normal of each polygon and determine the axis that it differs least.

  2. Map each polygon in each group using a basic planar mapping (see unity tutorial).

  3. Stitch grouped polygons to identify unique islands (connected uvs that don’t connect to other sets of uvs). It’s important not to try and stitch polygons across groups as this will severely complicate the problem.

  4. Pack resulting uv islands into the 0 to 1 UV range.

  5. Have a coffee.

So before you get upset at me for not giving you just a chunk of code to copy and paste… I would like to reiterate that all those steps are significant problems in their own. I believe you’re going to have to tackle this one a little first. Unless of course there’s some sample code out there or someone has already completed this and are willing to share.

Maybe break this question up into 3 or more individual questions?