Skip to content

Commit 3bcb1ac

Browse files
committed
Update CombineMeshes extension to match behavior
1 parent 27bf7d7 commit 3bcb1ac

File tree

1 file changed

+17
-6
lines changed

1 file changed

+17
-6
lines changed

Runtime/Extensions/MeshFilterExtensions.cs

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@
55

66
namespace Zigurous.Graphics
77
{
8+
/// <summary>
9+
/// Extension methods for mesh filters.
10+
/// </summary>
811
public static class MeshFilterExtensions
912
{
1013
/// <summary>
@@ -48,24 +51,32 @@ public static void SaveMesh(this MeshFilter filter, string assetName)
4851
/// Combines the meshes of the mesh filters into one mesh.
4952
/// </summary>
5053
/// <param name="filters">The mesh filters to combine.</param>
54+
/// <param name="combinedMeshName">The name of the new combined mesh.</param>
5155
/// <param name="optimizeMesh">Optimizes the combined mesh data to improve rendering performance.</param>
5256
/// <param name="recalculateBounds">Recalculates the bounding volume of the combined mesh.</param>
5357
/// <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)
5559
{
5660
CombineInstance[] combine = new CombineInstance[filters.Length];
5761

62+
int submesh = 0;
63+
5864
for (int i = 0; i < filters.Length; i++)
5965
{
60-
MeshFilter child = filters[i];
66+
MeshFilter filter = filters[i];
67+
68+
if (filter.mesh == null) {
69+
continue;
70+
}
71+
6172
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;
6576
}
6677

6778
Mesh combinedMesh = new Mesh();
68-
combinedMesh.name = "Combined Mesh";
79+
combinedMesh.name = combinedMeshName;
6980
combinedMesh.CombineMeshes(combine);
7081

7182
if (optimizeMesh) {

0 commit comments

Comments
 (0)