How do I UV-map a procedurally generated mesh(like a mine-craft chunk but with optimized geometry)?

I have a mesh generated based on voxel-data. Think mine-craft. Now I have optimized the geometry by combining faces. So I have a bunch of different sized rectangles instead of even sized squares. How do I get these textured based on what type of voxel they represent?

If every square had one face it would be easy, I just could use a texture atlas and scale + offset each face uv based on voxel. BUT I’m dealing with rectangles.

The solution I’m currently working on involves splitting each face type into a submesh or different mesh and then applying a different material to each one. But I’m not sure that this is going to be efficient, let’s say I’ve got 32 different tiletypes in a chunk I’d end up with 32 submeshes per chunk. Not sure how well Unity is going to handle this.

Any ideas how to get this working with something like a texture atlas instead?

Maybe there is something we could do with a shader?

Any suggestions appreciated.

If you want to use a texture atlas then you’d need to get rid of the optimization and just use 1 quad per voxel. So it’s a question of whether you want to save draw calls or vertices. I’d guess which is better would depend on the kind of world you’d typically have; if there are large flat areas with the same texture, and you’re saving tons of vertices by using rectangles, then probably it would be better to use submeshes.

some things to research-

Combine children extended version
export object script

the planar uv script-

if you have objects divided into planar surfaces, you can calculate the UVs for each plane individually with that.

I just put an extra name/tag on some of my tiles to say if they pointed North South East West up down, and used 6 cases of the above formula to UV them perfectly.

also there is a script that automatically makes texture Atlas somewhere on this forum.

Normally, if you textured your model before combining it/deleting triangles that are duplicates, well the texture information would still be there when you combine it and when you create rectangles.

feel free to post any code for merging squares into rectangles, it would be useful for me also! I’d love to see it!

Edit--------------

I can’t believe it I commented in length and then I got a call, and I pressed post your answer and disappeared everything.

Texture Atlas doesn’t make mistakes, you specify the part of the texture instead of the file name. when the code allows for the atlas it’s fine.

Check this awesome page, it has Kubik projection which is what I had in mind a vaguely for my project and also for cubes it should be very good. I can probably paste the code afterwards because I’m probably doing it today. modo 501 Inline Help System

the other way to do it is convert the normals to UVs using the render function from Unifi wiki mc blob marching cubes code.

I divided the function into 2, to render the vertices and triangles, And the UVs afterwards:

/*Unity and Sample Specific*/
private void renderMesh()
{
    int i;   

   /*Clear the Vertices that don't have any real information assigned to them */
   for(i=0;i<vertP;i++) {fv<em>=newVertex<em>;fn_=newNormal*;*_</em></em> 

}
for(i=0;i<triP;i++) {ft_=newTri*;}_
_for(i=triP;i<ft.Length;i++) {ft=0;}
Mesh mesh=((MeshFilter) GetComponent(“MeshFilter”)).mesh;
mesh.vertices = fv ;
mesh.uv = fuv;
mesh.triangles = ft;
mesh.normals = fn;
/For Disco Ball Effect/
mesh.RecalculateNormals();
}
private void nls()
{
int i;
Mesh mesh=((MeshFilter) GetComponent(“MeshFilter”)).mesh;
fn = mesh.normals;
/Clear the Vertices that don’t have any real information assigned to them /
for(i=0;i<vertP;i++) {_

_fuv=tada2[tadac2++];_

_Vector3 fuvt=transform.TransformPoint(fn).normalized;_
//fuv.x=(fuvt.x+1f).5f;fuv_.y=(fuvt.y+1f).5f;}
fuv.x=fn.x;fuv.y=fn.y;}
for(i=vertP;i<fv.Length;i++) {fv[0]=0;fn[0]=0;fuv[0]=0;
fv[1]=0;fn[1]=0;fuv[1]=0;_

_fv[2]=0;}
//Mesh mesh=((MeshFilter) GetComponent(“MeshFilter”)).mesh;
mesh.uv = fuv;
gameObject.AddComponent(“MeshCollider”);
/For Disco Ball Effect/
//mesh.RecalculateNormals();
}*_