|
5 | 5 |
|
6 | 6 | namespace Zigurous.Graphics |
7 | 7 | { |
| 8 | + /// <summary> |
| 9 | + /// Extension methods for mesh filters. |
| 10 | + /// </summary> |
8 | 11 | public static class MeshFilterExtensions |
9 | 12 | { |
10 | 13 | /// <summary> |
@@ -48,24 +51,32 @@ public static void SaveMesh(this MeshFilter filter, string assetName) |
48 | 51 | /// Combines the meshes of the mesh filters into one mesh. |
49 | 52 | /// </summary> |
50 | 53 | /// <param name="filters">The mesh filters to combine.</param> |
| 54 | + /// <param name="combinedMeshName">The name of the new combined mesh.</param> |
51 | 55 | /// <param name="optimizeMesh">Optimizes the combined mesh data to improve rendering performance.</param> |
52 | 56 | /// <param name="recalculateBounds">Recalculates the bounding volume of the combined mesh.</param> |
53 | 57 | /// <returns>The combined mesh.</returns> |
54 | | - public static Mesh CombineMeshes(this MeshFilter[] filters, bool optimizeMesh = true, bool recalculateBounds = true) |
| 58 | + public static Mesh CombineMeshes(this MeshFilter[] filters, string combinedMeshName = "Combined Mesh", bool optimizeMesh = true, bool recalculateBounds = true) |
55 | 59 | { |
56 | 60 | CombineInstance[] combine = new CombineInstance[filters.Length]; |
57 | 61 |
|
| 62 | + int submesh = 0; |
| 63 | + |
58 | 64 | for (int i = 0; i < filters.Length; i++) |
59 | 65 | { |
60 | | - MeshFilter child = filters[i]; |
| 66 | + MeshFilter filter = filters[i]; |
| 67 | + |
| 68 | + if (filter.mesh == null) { |
| 69 | + continue; |
| 70 | + } |
| 71 | + |
61 | 72 | CombineInstance instance = new CombineInstance(); |
62 | | - instance.mesh = child.mesh; |
63 | | - instance.transform = Matrix4x4.TRS(child.transform.localPosition, child.transform.localRotation, child.transform.localScale); |
64 | | - combine[i] = instance; |
| 73 | + instance.mesh = filter.mesh; |
| 74 | + instance.transform = filter.transform.localToWorldMatrix; |
| 75 | + combine[submesh++] = instance; |
65 | 76 | } |
66 | 77 |
|
67 | 78 | Mesh combinedMesh = new Mesh(); |
68 | | - combinedMesh.name = "Combined Mesh"; |
| 79 | + combinedMesh.name = combinedMeshName; |
69 | 80 | combinedMesh.CombineMeshes(combine); |
70 | 81 |
|
71 | 82 | if (optimizeMesh) { |
|
0 commit comments