MegaFlowParticleLegacyMoving.cs 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  1. using UnityEngine;
  2. #if UNITY_5_5 || UNITY_5_6 || UNITY_2017 || UNITY_2018 || UNITY_2019 || UNITY_2020
  3. #else
  4. using System.Collections.Generic;
  5. #if !UNITY_FLASH && !UNITY_PS3 && !UNITY_METRO && !UNITY_WP8
  6. using System.Threading;
  7. #endif
  8. [AddComponentMenu("MegaFlow/Particle Legacy Moving")]
  9. public class MegaFlowParticleLegacyMoving : MonoBehaviour
  10. {
  11. public MegaFlowMovingSource msource;
  12. MegaFlowFrame frame;
  13. public int framenum;
  14. [Range(0.01f, 2.0f)]
  15. public float mass = 0.02f;
  16. public float area = 0.2f;
  17. public float dt = 0.01f;
  18. public float scale = 1.0f;
  19. public int maxparticles = 1000;
  20. public ParticleEmitter particle;
  21. Particle[] particles;
  22. Matrix4x4 tm;
  23. Matrix4x4 invtm;
  24. float tdt;
  25. float coef;
  26. float oomass;
  27. int Cores = 0;
  28. float usedt = 0.0f;
  29. public float speed = 0.0f;
  30. public float gravity = 0.0f;
  31. [ContextMenu("Help")]
  32. public void Help()
  33. {
  34. Application.OpenURL("http://www.west-racing.com/mf/?page_id=5896");
  35. }
  36. public void SetFrame(int f)
  37. {
  38. if ( msource && msource.source )
  39. {
  40. if ( f >= 0 && f < msource.source.frames.Count )
  41. {
  42. frame = msource.source.frames[f];
  43. framenum = f;
  44. }
  45. }
  46. }
  47. // Flow source should be normalized
  48. void RunSim(int start, int end)
  49. {
  50. Vector3 tvel;
  51. Vector3 acc;
  52. Vector3 dir;
  53. float fvel = 0.0f;
  54. float falloff = 0.0f;
  55. int mframe = 0;
  56. Matrix4x4 ftm = invtm;
  57. Vector3 flowpos = Vector3.zero;
  58. for ( int i = start; i < end; i++ )
  59. {
  60. Vector3 pos = particles[i].position;
  61. Vector3 vel = particles[i].velocity;
  62. float duration = tdt;
  63. bool inbounds = true;
  64. // Not really correct to do here but it is close enough and will save a lot of cpu time
  65. //flowpos = msource.FindFlowPos(pos, ref inbounds, ref ftm, ref fvel);
  66. flowpos = msource.FindFlowPos(pos, ref inbounds, ref ftm, ref fvel, ref mframe, ref falloff);
  67. while ( duration > 0.0001f )
  68. {
  69. if ( inbounds )
  70. {
  71. Vector3 airvel = ftm.MultiplyVector(frame.GetGridVel(flowpos, ref inbounds));
  72. if ( inbounds )
  73. {
  74. float scl = scale * fvel * falloff;
  75. airvel.x *= scl;
  76. airvel.y *= scl;
  77. airvel.z *= scl;
  78. if ( airvel.sqrMagnitude > 0.1f )
  79. {
  80. tvel.x = airvel.x - vel.x;
  81. tvel.y = airvel.y - vel.y;
  82. tvel.z = airvel.z - vel.z;
  83. float l = (tvel.x * tvel.x) + (tvel.y * tvel.y) + (tvel.z * tvel.z);
  84. if ( l != 0.0f )
  85. {
  86. //Debug.Log("got one");
  87. l = Mathf.Sqrt(l);
  88. float df = coef * l;
  89. l = 1.0f / l;
  90. dir.x = tvel.x * l;
  91. dir.y = tvel.y * l;
  92. dir.z = tvel.z * l;
  93. float dfm = df * oomass;
  94. acc.x = dir.x * dfm;
  95. acc.y = dir.y * dfm + gravity;
  96. acc.z = dir.z * dfm;
  97. vel.x += acc.x * usedt;
  98. vel.y += acc.y * usedt;
  99. vel.z += acc.z * usedt;
  100. pos.x += vel.x * usedt;
  101. pos.y += vel.y * usedt;
  102. pos.z += vel.z * usedt;
  103. }
  104. }
  105. else
  106. {
  107. vel.y += gravity * usedt;
  108. pos.y += vel.y * usedt;
  109. }
  110. }
  111. else
  112. {
  113. vel.y += gravity * usedt;
  114. pos.y += vel.y * usedt;
  115. }
  116. }
  117. else
  118. {
  119. vel.y += gravity * usedt;
  120. pos.y += vel.y * usedt;
  121. }
  122. duration -= usedt;
  123. }
  124. particles[i].position = pos;
  125. particles[i].velocity = vel;
  126. }
  127. }
  128. void Start()
  129. {
  130. if ( Cores == 0 )
  131. Cores = SystemInfo.processorCount - 1;
  132. if ( particle == null )
  133. particle = GetComponent<ParticleEmitter>();
  134. particles = new Particle[maxparticles];
  135. }
  136. #if !UNITY_FLASH && !UNITY_PS3 && !UNITY_METRO && !UNITY_WP8
  137. public bool UseThreading = false;
  138. public MegaFlowTaskInfo[] tasks;
  139. void MakeThreads()
  140. {
  141. if ( Cores > 0 )
  142. {
  143. isRunning = true;
  144. tasks = new MegaFlowTaskInfo[Cores];
  145. for ( int i = 0; i < Cores; i++ )
  146. {
  147. tasks[i] = new MegaFlowTaskInfo();
  148. tasks[i].pauseevent = new AutoResetEvent(false);
  149. tasks[i]._thread = new Thread(DoWork);
  150. tasks[i]._thread.Start(tasks[i]);
  151. }
  152. }
  153. }
  154. void Update()
  155. {
  156. if ( msource && msource.source && particle )
  157. {
  158. msource.framenum = framenum;
  159. framenum = Mathf.Clamp(framenum, 0, msource.source.frames.Count - 1);
  160. frame = msource.source.frames[framenum];
  161. msource.source.Prepare();
  162. tdt = Time.deltaTime;
  163. Matrix4x4 offtm = Matrix4x4.TRS(frame.size * 0.5f, Quaternion.identity, Vector3.one);
  164. tm = offtm * msource.source.transform.worldToLocalMatrix;
  165. invtm = tm.inverse;
  166. float p = msource.source.Density;
  167. float A = area;
  168. float Re = msource.source.Reynolds;
  169. oomass = 1.0f / mass;
  170. coef = 1.0f * p * A * Mathf.Pow(Re, -0.5f);
  171. usedt = dt;
  172. if ( usedt > tdt )
  173. usedt = tdt;
  174. int count = particle.particleCount;
  175. particles = particle.particles;
  176. if ( !UseThreading || Cores < 1 || !Application.isPlaying )
  177. RunSim(0, count);
  178. else
  179. {
  180. if ( Cores == 0 )
  181. Cores = SystemInfo.processorCount - 1;
  182. if ( tasks == null )
  183. MakeThreads();
  184. int step = count / (Cores + 1);
  185. if ( Cores > 0 )
  186. {
  187. int index = step;
  188. for ( int i = 0; i < tasks.Length; i++ )
  189. {
  190. tasks[i].start = index;
  191. tasks[i].end = index + step;
  192. index += step;
  193. }
  194. tasks[Cores - 1].end = count;
  195. for ( int i = 0; i < tasks.Length; i++ )
  196. tasks[i].pauseevent.Set();
  197. }
  198. RunSim(0, step);
  199. WaitJobs();
  200. }
  201. particle.particles = particles;
  202. }
  203. }
  204. void WaitJobs()
  205. {
  206. if ( Cores > 0 )
  207. {
  208. int count = 0;
  209. bool wait = false;
  210. do
  211. {
  212. wait = false;
  213. for ( int i = 0; i < tasks.Length; i++ )
  214. {
  215. if ( tasks[i].end > 0 )
  216. {
  217. wait = true;
  218. break;
  219. }
  220. }
  221. if ( wait )
  222. {
  223. count++;
  224. Thread.Sleep(0);
  225. }
  226. } while ( wait );
  227. }
  228. }
  229. void OnApplicationQuit()
  230. {
  231. if ( Application.isPlaying )
  232. {
  233. isRunning = false;
  234. if ( tasks != null )
  235. {
  236. for ( int i = 0; i < tasks.Length; i++ )
  237. {
  238. tasks[i].pauseevent.Set();
  239. while ( tasks[i]._thread.IsAlive )
  240. {
  241. }
  242. }
  243. }
  244. tasks = null;
  245. }
  246. }
  247. static bool isRunning = true;
  248. void DoWork(object info)
  249. {
  250. MegaFlowTaskInfo inf = (MegaFlowTaskInfo)info;
  251. while ( isRunning )
  252. {
  253. inf.pauseevent.WaitOne(Timeout.Infinite, false);
  254. RunSim(inf.start, inf.end);
  255. inf.end = 0;
  256. }
  257. }
  258. #else
  259. void Update()
  260. {
  261. if ( msource && msource.source && particle )
  262. {
  263. msource.framenum = framenum;
  264. framenum = Mathf.Clamp(framenum, 0, msource.source.frames.Count - 1);
  265. frame = msource.source.frames[framenum];
  266. source.Prepare();
  267. tdt = Time.deltaTime;
  268. Matrix4x4 offtm = Matrix4x4.TRS(frame.size * 0.5f, Quaternion.identity, Vector3.one);
  269. tm = offtm * msource.source.transform.worldToLocalMatrix;
  270. invtm = tm.inverse;
  271. float p = msource.source.Density;
  272. float A = area;
  273. float Re = msource.source.Reynolds;
  274. oomass = 1.0f / mass;
  275. coef = 1.0f * p * A * Mathf.Pow(Re, -0.5f);
  276. usedt = dt;
  277. if ( usedt > tdt )
  278. usedt = tdt;
  279. int count = particle.particleCount;
  280. particles = particle.particles;
  281. RunSim(0, count);
  282. particle.particles = particles;
  283. }
  284. }
  285. #endif
  286. }
  287. #endif