ParticleSystemExtensions.cs 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. using System;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using Object = UnityEngine.Object;
  5. namespace Coffee.UIParticleInternal
  6. {
  7. internal static class ParticleSystemExtensions
  8. {
  9. private static ParticleSystem.Particle[] s_TmpParticles = new ParticleSystem.Particle[2048];
  10. public static ParticleSystem.Particle[] GetParticleArray(int size)
  11. {
  12. if (s_TmpParticles.Length < size)
  13. {
  14. while (s_TmpParticles.Length < size)
  15. {
  16. size = Mathf.NextPowerOfTwo(size);
  17. }
  18. s_TmpParticles = new ParticleSystem.Particle[size];
  19. }
  20. return s_TmpParticles;
  21. }
  22. public static void ValidateShape(this ParticleSystem self)
  23. {
  24. var shape = self.shape;
  25. if (shape.enabled && shape.alignToDirection)
  26. {
  27. if (Mathf.Approximately(shape.scale.x * shape.scale.y * shape.scale.z, 0))
  28. {
  29. if (Mathf.Approximately(shape.scale.x, 0))
  30. {
  31. shape.scale.Set(0.0001f, shape.scale.y, shape.scale.z);
  32. }
  33. else if (Mathf.Approximately(shape.scale.y, 0))
  34. {
  35. shape.scale.Set(shape.scale.x, 0.0001f, shape.scale.z);
  36. }
  37. else if (Mathf.Approximately(shape.scale.z, 0))
  38. {
  39. shape.scale.Set(shape.scale.x, shape.scale.y, 0.0001f);
  40. }
  41. }
  42. }
  43. }
  44. public static bool CanBakeMesh(this ParticleSystemRenderer self)
  45. {
  46. // #69: Editor crashes when mesh is set to null when `ParticleSystem.RenderMode = Mesh`
  47. if (self.renderMode == ParticleSystemRenderMode.Mesh && self.mesh == null) return false;
  48. // #61: When `ParticleSystem.RenderMode = None`, an error occurs
  49. if (self.renderMode == ParticleSystemRenderMode.None) return false;
  50. return true;
  51. }
  52. public static ParticleSystemSimulationSpace GetActualSimulationSpace(this ParticleSystem self)
  53. {
  54. var main = self.main;
  55. var space = main.simulationSpace;
  56. if (space == ParticleSystemSimulationSpace.Custom && !main.customSimulationSpace)
  57. {
  58. space = ParticleSystemSimulationSpace.Local;
  59. }
  60. return space;
  61. }
  62. public static bool IsLocalSpace(this ParticleSystem self)
  63. {
  64. return GetActualSimulationSpace(self) == ParticleSystemSimulationSpace.Local;
  65. }
  66. public static bool IsWorldSpace(this ParticleSystem self)
  67. {
  68. return GetActualSimulationSpace(self) == ParticleSystemSimulationSpace.World;
  69. }
  70. public static void SortForRendering(this List<ParticleSystem> self, Transform transform, bool sortByMaterial)
  71. {
  72. self.Sort((a, b) =>
  73. {
  74. var aRenderer = a.GetComponent<ParticleSystemRenderer>();
  75. var bRenderer = b.GetComponent<ParticleSystemRenderer>();
  76. // Render queue: ascending
  77. var aMat = aRenderer.sharedMaterial ? aRenderer.sharedMaterial : aRenderer.trailMaterial;
  78. var bMat = bRenderer.sharedMaterial ? bRenderer.sharedMaterial : bRenderer.trailMaterial;
  79. if (!aMat && !bMat) return 0;
  80. if (!aMat) return -1;
  81. if (!bMat) return 1;
  82. if (sortByMaterial)
  83. {
  84. return aMat.GetInstanceID() - bMat.GetInstanceID();
  85. }
  86. if (aMat.renderQueue != bMat.renderQueue)
  87. {
  88. return aMat.renderQueue - bMat.renderQueue;
  89. }
  90. // Sorting layer: ascending
  91. if (aRenderer.sortingLayerID != bRenderer.sortingLayerID)
  92. {
  93. return SortingLayer.GetLayerValueFromID(aRenderer.sortingLayerID) -
  94. SortingLayer.GetLayerValueFromID(bRenderer.sortingLayerID);
  95. }
  96. // Sorting order: ascending
  97. if (aRenderer.sortingOrder != bRenderer.sortingOrder)
  98. {
  99. return aRenderer.sortingOrder - bRenderer.sortingOrder;
  100. }
  101. // Z position & sortingFudge: descending
  102. var aTransform = a.transform;
  103. var bTransform = b.transform;
  104. var aPos = transform.InverseTransformPoint(aTransform.position).z + aRenderer.sortingFudge;
  105. var bPos = transform.InverseTransformPoint(bTransform.position).z + bRenderer.sortingFudge;
  106. if (!Mathf.Approximately(aPos, bPos))
  107. {
  108. return (int)Mathf.Sign(bPos - aPos);
  109. }
  110. return (int)Mathf.Sign(GetIndex(self, a) - GetIndex(self, b));
  111. });
  112. }
  113. private static int GetIndex(IList<ParticleSystem> list, Object ps)
  114. {
  115. for (var i = 0; i < list.Count; i++)
  116. {
  117. if (list[i].GetInstanceID() == ps.GetInstanceID())
  118. {
  119. return i;
  120. }
  121. }
  122. return 0;
  123. }
  124. public static Texture2D GetTextureForSprite(this ParticleSystem self)
  125. {
  126. if (!self) return null;
  127. // Get sprite's texture.
  128. var tsaModule = self.textureSheetAnimation;
  129. if (!tsaModule.enabled || tsaModule.mode != ParticleSystemAnimationMode.Sprites) return null;
  130. for (var i = 0; i < tsaModule.spriteCount; i++)
  131. {
  132. var sprite = tsaModule.GetSprite(i);
  133. if (!sprite) continue;
  134. return sprite.GetActualTexture();
  135. }
  136. return null;
  137. }
  138. public static void Exec(this List<ParticleSystem> self, Action<ParticleSystem> action)
  139. {
  140. foreach (var p in self)
  141. {
  142. if (!p) continue;
  143. action.Invoke(p);
  144. }
  145. }
  146. public static ParticleSystem GetMainEmitter(this ParticleSystem self, List<ParticleSystem> list)
  147. {
  148. if (!self || list == null || list.Count == 0) return null;
  149. for (var i = 0; i < list.Count; i++)
  150. {
  151. var parent = list[i];
  152. if (parent != self && IsSubEmitterOf(self, parent)) return parent;
  153. }
  154. return null;
  155. }
  156. public static bool IsSubEmitterOf(this ParticleSystem self, ParticleSystem parent)
  157. {
  158. if (!self || !parent) return false;
  159. var subEmitters = parent.subEmitters;
  160. var count = subEmitters.subEmittersCount;
  161. for (var i = 0; i < count; i++)
  162. {
  163. if (subEmitters.GetSubEmitterSystem(i) == self) return true;
  164. }
  165. return false;
  166. }
  167. }
  168. }