MeshHelper.cs 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. using System.Collections.Generic;
  2. using UnityEngine;
  3. using UnityEngine.Profiling;
  4. namespace Coffee.UIParticleExtensions
  5. {
  6. internal static class MeshHelper
  7. {
  8. public static List<bool> activeMeshIndices { get; private set; }
  9. private static readonly List<CombineInstanceEx> s_CachedInstance;
  10. private static int count;
  11. public static void Init()
  12. {
  13. activeMeshIndices = new List<bool>();
  14. }
  15. static MeshHelper()
  16. {
  17. s_CachedInstance = new List<CombineInstanceEx>(8);
  18. for (var i = 0; i < 8; i++)
  19. {
  20. s_CachedInstance.Add(new CombineInstanceEx());
  21. }
  22. }
  23. private static CombineInstanceEx Get(int index, long hash)
  24. {
  25. if (0 < count && s_CachedInstance[count - 1].hash == hash)
  26. return s_CachedInstance[count - 1];
  27. if (s_CachedInstance.Count <= count)
  28. {
  29. var newInst = new CombineInstanceEx();
  30. s_CachedInstance.Add(newInst);
  31. }
  32. var inst = s_CachedInstance[count];
  33. inst.hash = hash;
  34. if (inst.index != -1) return inst;
  35. inst.index = index;
  36. count++;
  37. return inst;
  38. }
  39. public static Mesh GetTemporaryMesh()
  40. {
  41. return MeshPool.Rent();
  42. }
  43. public static void Push(int index, long hash, Mesh mesh, Matrix4x4 transform)
  44. {
  45. if (mesh.vertexCount <= 0)
  46. {
  47. DiscardTemporaryMesh(mesh);
  48. return;
  49. }
  50. Profiler.BeginSample("[UIParticle] MeshHelper > Get CombineInstanceEx");
  51. var inst = Get(index, hash);
  52. Profiler.EndSample();
  53. Profiler.BeginSample("[UIParticle] MeshHelper > Push To Mesh Helper");
  54. inst.Push(mesh, transform);
  55. Profiler.EndSample();
  56. activeMeshIndices[inst.index] = true;
  57. }
  58. public static void Clear()
  59. {
  60. count = 0;
  61. activeMeshIndices.Clear();
  62. foreach (var inst in s_CachedInstance)
  63. {
  64. inst.Clear();
  65. }
  66. }
  67. public static void CombineMesh(Mesh result)
  68. {
  69. if (count == 0) return;
  70. for (var i = 0; i < count; i++)
  71. {
  72. Profiler.BeginSample("[UIParticle] MeshHelper > Combine Mesh Internal");
  73. s_CachedInstance[i].Combine();
  74. Profiler.EndSample();
  75. }
  76. Profiler.BeginSample("[UIParticle] MeshHelper > Combine Mesh");
  77. var cis = CombineInstanceArrayPool.Get(s_CachedInstance, count);
  78. result.CombineMeshes(cis, false, true);
  79. cis.Clear();
  80. Profiler.EndSample();
  81. result.RecalculateBounds();
  82. }
  83. public static void DiscardTemporaryMesh(Mesh mesh)
  84. {
  85. MeshPool.Return(mesh);
  86. }
  87. }
  88. }