MeshCombined.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. using UnityEngine;
  2. using System.Collections.Generic;
  3. namespace LunarCatsStudio.SuperCombiner
  4. {
  5. /// <summary>
  6. /// Mesh Combined
  7. /// Each mesh is a part of the combined result.
  8. /// Either you combine only materials, then this is a list of new created meshes
  9. /// Or you combine meshes, they may be split if size exceeds 65k vertices
  10. /// </summary>
  11. [System.Serializable]
  12. public class MeshCombined
  13. {
  14. // List of name of the original game objects combined
  15. public List<string> names = new List<string>();
  16. // List of instance Id for each original game object combined
  17. public List<int> instanceIds = new List<int>();
  18. // List of indexes for combined meshes
  19. public List<CombineInstanceIndexes> indexes = new List<CombineInstanceIndexes>();
  20. // Editor display parameter
  21. public bool showMeshCombined;
  22. /// <summary>
  23. /// Removes a mesh (given by the instanceID of the gameObject on which it is attached) from the combined mesh
  24. /// </summary>
  25. /// <param name="instanceID"></param>
  26. public Mesh RemoveMesh(int instanceID, Mesh mesh)
  27. {
  28. if (instanceIds.Contains(instanceID))
  29. {
  30. int index = instanceIds.IndexOf(instanceID);
  31. Vector3[] vertices = mesh.vertices;
  32. Vector3[] newVertices = new Vector3[mesh.vertexCount - indexes[index].vertexCount];
  33. int[] triangles = mesh.triangles;
  34. int[] newTriangles = new int[triangles.Length - indexes[index].triangleCount];
  35. Vector4[] tangents = mesh.tangents;
  36. Vector4[] newTangents = new Vector4[mesh.tangents.Length - indexes[index].vertexCount];
  37. Vector2[] uv = mesh.uv;
  38. Vector2[] newUv = new Vector2[newVertices.Length];
  39. Vector2[] uv2 = mesh.uv2;
  40. Vector2[] newUv2 = new Vector2[newVertices.Length];
  41. // Assign new vertices, uv and uv2
  42. for (int i = 0; i < newVertices.Length; i++)
  43. {
  44. if (i < indexes[index].firstVertexIndex)
  45. {
  46. newVertices[i] = vertices[i];
  47. newUv[i] = uv[i];
  48. newUv2[i] = uv2[i];
  49. newTangents[i] = tangents[i];
  50. }
  51. else
  52. {
  53. newVertices[i] = vertices[i + indexes[index].vertexCount];
  54. newUv[i] = uv[i + indexes[index].vertexCount];
  55. newUv2[i] = uv2[i + indexes[index].vertexCount];
  56. newTangents[i] = tangents[i + indexes[index].vertexCount];
  57. }
  58. }
  59. // Assign new triangles
  60. for (int i = 0; i < newTriangles.Length; i++)
  61. {
  62. if (i < indexes[index].firstTriangleIndex)
  63. {
  64. newTriangles[i] = triangles[i];
  65. }
  66. else
  67. {
  68. newTriangles[i] = triangles[i + indexes[index].triangleCount] - indexes[index].vertexCount;
  69. }
  70. }
  71. // Offset all vertices and triangles of meshes placed after the instanceID's mesh
  72. for (int i = index; i < indexes.Count; i++)
  73. {
  74. indexes[i].MoveIndexes(indexes[index].vertexCount, indexes[index].triangleCount);
  75. }
  76. // Delete the mesh from the list
  77. indexes.RemoveAt(index);
  78. instanceIds.RemoveAt(index);
  79. names.RemoveAt(index);
  80. // Reasign new vertices, triangles and uvs to the mesh
  81. mesh.Clear();
  82. mesh.vertices = new List<Vector3>(newVertices).ToArray();
  83. mesh.SetTriangles(newTriangles, 0);
  84. mesh.tangents = newTangents;
  85. mesh.uv = newUv;
  86. mesh.uv2 = newUv2;
  87. mesh.RecalculateBounds();
  88. mesh.RecalculateNormals();
  89. }
  90. return mesh;
  91. }
  92. }
  93. }