public static Mesh Combine(MeshFilter[] filters, bool weld) { if (filters.Length == 0) { Debug.LogError("Mesh list is empty."); return(null); } Transform root = filters[0].transform; int meshCount = filters.Length; MeshExtended[] extended = new MeshExtended[meshCount]; for (int m = 0; m < meshCount; m++) { MeshFilter filter = filters[m]; extended[m] = new MeshExtended(); extended[m].Prepare(filter.sharedMesh, weld, root, filter.GetComponent <Transform>(), MeshExtended.SizeMethod.Bounds); } UVPackerSqr.Box[] boxes = UVPackerSqr.Pack(extended); Mesh combined = new Mesh(); List <Vector3> combinedVertices = new List <Vector3>(); List <Vector2> combinedUVs = new List <Vector2>(); List <Vector3> combinedNormals = new List <Vector3>(); List <int> combinedTris = new List <int>(); int triOffset = 0; for (int meshIndex = 0; meshIndex < meshCount; meshIndex++) { MeshExtended extmesh = boxes[meshIndex].Extended; Vector3[] vertices = extmesh.Vertices; Vector2[] uvs = boxes[meshIndex].PackedUVs; Vector3[] normals = extmesh.Normals; int[] triangles = extmesh.Triangles; int vertexCount = vertices.Length; int triCount = triangles.Length; for (int v = 0; v < vertexCount; v++) { combinedVertices.Add(vertices[v]); combinedUVs.Add(uvs[v]); combinedNormals.Add(normals[v]); } for (int t = 0; t < triCount; t++) { combinedTris.Add(triangles[t] + triOffset); } triOffset = combinedVertices.Count; } UVPacker.AdjustSpace(combinedUVs); combined.SetVertices(combinedVertices); combined.SetUVs(0, combinedUVs); combined.SetNormals(combinedNormals); combined.SetTriangles(combinedTris, 0); return(combined); }
public static Box FitUVs(MeshExtended extended, float x, float y, float side) { Vector2[] uvs = extended.UVs; int count = uvs.Length; Vector2[] packed = new Vector2[count]; float xMin = Mathf.Infinity; float xMax = -Mathf.Infinity; float yMin = Mathf.Infinity; float yMax = -Mathf.Infinity; foreach (Vector2 v2 in uvs) { if (v2.x < xMin) { xMin = v2.x; } if (v2.x > xMax) { xMax = v2.x; } if (v2.y < yMin) { yMin = v2.y; } if (v2.y > yMax) { yMax = v2.y; } } float width = xMax - xMin; float height = yMax - yMin; // debug: //float dminx = Mathf.Infinity; //float dmaxx = -Mathf.Infinity; //float dminy = Mathf.Infinity; //float dmaxy = -Mathf.Infinity; for (int u = 0; u < count; u++) { Vector2 uv = uvs[u]; packed[u].x = x + (uv.x - xMin) / width * side; packed[u].y = y + (uv.y - yMin) / height * side; // debug: //if(packed[u].x<dminx) { // dminx = packed[u].x; //} //if(packed[u].x>dmaxx) { // dmaxx = packed[u].x; //} //if(packed[u].y<dminy) { // dminy = packed[u].y; //} //if(packed[u].y>dmaxy) { // dmaxy = packed[u].y; //} } //debug: //if(dminx<x || dmaxx>x+side) { // Debug.Log("sx1="+x+", sx2= "+x+side+", x1="+dminx+", x2="+dmaxx); //} //if(dminy<y || dmaxy>y+side) { // Debug.Log("sy1="+y+", sy2= "+y+side+", y1="+dminy+", y2="+dmaxy); //} return(new Box { Extended = extended, PackedUVs = packed }); }