ParticleAffectorMT.cs 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. 
  2. // =================================
  3. // Namespaces.
  4. // =================================
  5. using UnityEngine;
  6. using System.Threading;
  7. // =================================
  8. // Define namespace.
  9. // =================================
  10. namespace MirzaBeig
  11. {
  12. namespace Scripting
  13. {
  14. namespace Effects
  15. {
  16. // =================================
  17. // Classes.
  18. // =================================
  19. //[ExecuteInEditMode]
  20. [System.Serializable]
  21. public class ParticleAffectorMT : MonoBehaviour
  22. {
  23. // =================================
  24. // Nested classes and structures.
  25. // =================================
  26. // ...
  27. // =================================
  28. // Variables.
  29. // =================================
  30. // ...
  31. public float force = 1.0f;
  32. public float speed = 1.0f;
  33. new ParticleSystem particleSystem;
  34. //Vector3 particleSystemPosition;
  35. ParticleSystem.Particle[] particles;
  36. float randomX;
  37. float randomY;
  38. float randomZ;
  39. float offsetX;
  40. float offsetY;
  41. float offsetZ;
  42. float deltaTime;
  43. Thread t;
  44. readonly object locker = new object();
  45. //bool running;
  46. bool processing;
  47. bool isDoneAssigning;
  48. // =================================
  49. // Functions.
  50. // =================================
  51. // ...
  52. void Awake()
  53. {
  54. }
  55. // ...
  56. void Start()
  57. {
  58. particleSystem = GetComponent<ParticleSystem>();
  59. randomX = Random.Range(-32.0f, 32.0f);
  60. randomY = Random.Range(-32.0f, 32.0f);
  61. randomZ = Random.Range(-32.0f, 32.0f);
  62. //running = true;
  63. t = new Thread(process);
  64. t.Start();
  65. isDoneAssigning = true;
  66. }
  67. // ...
  68. void LateUpdate()
  69. {
  70. lock (locker)
  71. {
  72. if (!processing && isDoneAssigning)
  73. {
  74. // Init.
  75. //print(1);
  76. particles = new ParticleSystem.Particle[particleSystem.particleCount];
  77. particleSystem.GetParticles(particles);
  78. float time = Time.time;
  79. deltaTime = Time.deltaTime;
  80. offsetX = (time * speed) * randomX;
  81. offsetY = (time * speed) * randomY;
  82. offsetZ = (time * speed) * randomZ;
  83. //particleSystemPosition = particleSystem.transform.position;
  84. processing = true;
  85. isDoneAssigning = false;
  86. // Pause else particles may be lost or created between
  87. // time thread started, and time it finished.
  88. // If not paused, then re-assigning particles from before
  89. // may cause jitter as they are reset to the last known
  90. // positions, while also replacing potentially new particles.
  91. //particleSystem.Pause();
  92. }
  93. }
  94. if (t.ThreadState == ThreadState.Stopped)
  95. {
  96. t = new Thread(process);
  97. t.Start();
  98. }
  99. //process();
  100. lock (locker)
  101. {
  102. if (!processing && !isDoneAssigning)
  103. {
  104. // Assign.
  105. //print(3);
  106. //print("-----");
  107. particleSystem.SetParticles(particles, particles.Length);
  108. isDoneAssigning = true;
  109. //particleSystem.Play();
  110. }
  111. }
  112. }
  113. // ...
  114. void process()
  115. {
  116. //while (running)
  117. //{
  118. lock (locker)
  119. {
  120. if (processing)
  121. {
  122. // Process.
  123. //print(2);
  124. for (int i = 0; i < particles.Length; i++)
  125. {
  126. ParticleSystem.Particle particle = particles[i];
  127. //Vector3 particleWorldPosition = particleSystemPosition + particle.position;
  128. Vector3 particlePosition = particle.position;
  129. Vector3 force = new Vector3(
  130. Noise.perlin(offsetX + particlePosition.x, offsetX + particlePosition.y, offsetX + particlePosition.z),
  131. Noise.perlin(offsetY + particlePosition.x, offsetY + particlePosition.y, offsetY + particlePosition.z),
  132. Noise.perlin(offsetZ + particlePosition.x, offsetZ + particlePosition.y, offsetZ + particlePosition.z)
  133. ) * this.force;
  134. force *= deltaTime;
  135. particle.velocity += force;
  136. particles[i] = particle;
  137. }
  138. processing = false;
  139. }
  140. //Thread.Sleep(10);
  141. }
  142. //Thread.Sleep(10);
  143. //}
  144. }
  145. // ...
  146. void OnDisable()
  147. {
  148. //running = false;
  149. }
  150. void OnApplicationQuit()
  151. {
  152. //running = false;
  153. }
  154. // =================================
  155. // End functions.
  156. // =================================
  157. }
  158. // =================================
  159. // End namespace.
  160. // =================================
  161. }
  162. }
  163. }
  164. // =================================
  165. // --END-- //
  166. // =================================