|
| 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 | +} |
0 commit comments