Skip to content

Commit 9df739c

Browse files
committed
Add script for combining children meshes
1 parent 4312761 commit 9df739c

File tree

3 files changed

+109
-2
lines changed

3 files changed

+109
-2
lines changed

Runtime/CombineChildrenMeshes.cs

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
using UnityEngine;
2+
3+
namespace Zigurous.Graphics
4+
{
5+
/// <summary>
6+
/// Combines the meshes of the children of the game object into one mesh.
7+
/// </summary>
8+
[RequireComponent(typeof(MeshFilter))]
9+
[AddComponentMenu("Zigurous/Graphics/Combine Children Meshes")]
10+
public sealed class CombineChildrenMeshes : MonoBehaviour
11+
{
12+
/// <summary>
13+
/// Combines the mesh on start, otherwise it needs to be called manually.
14+
/// </summary>
15+
[Tooltip("Combines the mesh on start, otherwise it needs to be called manually.")]
16+
public bool combineOnStart = true;
17+
18+
/// <summary>
19+
/// Removes the child meshes from the game object after combining.
20+
/// </summary>
21+
[Tooltip("Removes the child meshes from the game object after combining.")]
22+
public bool removeChildMeshes = true;
23+
24+
/// <summary>
25+
/// Optimizes the combined mesh data to improve rendering performance.
26+
/// </summary>
27+
[Tooltip("Optimizes the combined mesh data to improve rendering performance.")]
28+
public bool optimizeMesh = true;
29+
30+
/// <summary>
31+
/// Recalculates the bounding volume of the combined mesh.
32+
/// </summary>
33+
[Tooltip("Recalculates the bounding volume of the combined mesh.")]
34+
public bool recalculateBounds = true;
35+
36+
private void Start()
37+
{
38+
MeshFilter parent = GetComponent<MeshFilter>();
39+
40+
if (this.combineOnStart && parent != null) {
41+
parent.mesh = Combine();
42+
}
43+
}
44+
45+
/// <summary>
46+
/// Combines the meshes of the children of this game object.
47+
/// </summary>
48+
/// <returns>A new combined mesh.</returns>
49+
public Mesh Combine()
50+
{
51+
MeshFilter[] children = GetComponentsInChildren<MeshFilter>();
52+
CombineInstance[] combine = new CombineInstance[children.Length - 1];
53+
54+
int submesh = 0;
55+
56+
for (int i = 0; i < children.Length; i++)
57+
{
58+
MeshFilter child = children[i];
59+
60+
// Ignore the parent mesh
61+
if (child.transform == this.transform) {
62+
continue;
63+
}
64+
65+
// Create a mesh combine instance
66+
CombineInstance instance = new CombineInstance();
67+
instance.mesh = child.mesh;
68+
instance.transform = Matrix4x4.TRS(child.transform.localPosition, child.transform.localRotation, child.transform.localScale);
69+
combine[submesh++] = instance;
70+
71+
// Destroy the child mesh
72+
if (this.removeChildMeshes)
73+
{
74+
Destroy(child.GetComponent<MeshRenderer>());
75+
Destroy(child);
76+
}
77+
}
78+
79+
// Create a new mesh from all of the combined children
80+
Mesh combinedMesh = new Mesh();
81+
combinedMesh.CombineMeshes(combine);
82+
83+
if (this.optimizeMesh) {
84+
combinedMesh.Optimize();
85+
}
86+
87+
if (this.recalculateBounds) {
88+
combinedMesh.RecalculateBounds();
89+
}
90+
91+
return combinedMesh;
92+
}
93+
94+
}
95+
96+
}

Runtime/CombineChildrenMeshes.cs.meta

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Runtime/SaveMesh.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,9 @@ public sealed class SaveMesh : MonoBehaviour
2020
public string assetName;
2121

2222
/// <summary>
23-
/// Saves the mesh on start, otherwise it needs to be saved manually.
23+
/// Saves the mesh on start, otherwise it needs to be called manually.
2424
/// </summary>
25-
[Tooltip("Saves the mesh on start, otherwise it needs to be saved manually.")]
25+
[Tooltip("Saves the mesh on start, otherwise it needs to be called manually.")]
2626
public bool saveOnStart = true;
2727

2828
private void Reset()

0 commit comments

Comments
 (0)