MegaFlowLegacyParticle.cs 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482
  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/Legacy Particle Control")]
  9. public class MegaFlowLegacyParticle : MonoBehaviour
  10. {
  11. public MegaFlow source;
  12. MegaFlowFrame frame;
  13. public int framenum;
  14. //public MegaFlow source1;
  15. MegaFlowFrame frame1;
  16. public int framenum1;
  17. public Vector3 vel = Vector3.zero;
  18. float t = 0.0f;
  19. [Range(0.01f, 2.0f)]
  20. public float mass = 0.02f;
  21. public float area = 0.2f;
  22. public float dt = 0.01f;
  23. public float scale = 1.0f;
  24. public int maxparticles = 1000;
  25. Matrix4x4 tm;
  26. Matrix4x4 invtm;
  27. Matrix4x4 tm1;
  28. Matrix4x4 invtm1;
  29. float tdt;
  30. float coef;
  31. float oomass;
  32. int Cores = 0;
  33. float usedt = 0.0f;
  34. public ParticleEmitter particle;
  35. Particle[] particles;
  36. [Range(0.0f, 1.0f)]
  37. public float framealpha = 0.0f;
  38. public bool interp = false;
  39. [ContextMenu("Help")]
  40. public void Help()
  41. {
  42. Application.OpenURL("http://www.west-racing.com/mf/?page_id=5960");
  43. }
  44. public void SetFrame(int f)
  45. {
  46. if ( source )
  47. {
  48. if ( f >= 0 && f < source.frames.Count )
  49. {
  50. frame = source.frames[f];
  51. framenum = f;
  52. }
  53. }
  54. }
  55. public void SetFrame1(int f)
  56. {
  57. if ( source )
  58. {
  59. if ( f >= 0 && f < source.frames.Count )
  60. {
  61. frame1 = source.frames[f];
  62. framenum1 = f;
  63. }
  64. }
  65. }
  66. void RunSim(int start, int end)
  67. {
  68. Vector3 tvel;
  69. Vector3 acc;
  70. Vector3 dir;
  71. if ( interp && framealpha != 0.0f )
  72. {
  73. RunSimNew(start, end);
  74. return;
  75. }
  76. float scl = source.Scale * scale;
  77. for ( int i = start; i < end; i++ )
  78. {
  79. Vector3 pos = particles[i].position;
  80. Vector3 vel = particles[i].velocity;
  81. float duration = tdt;
  82. bool inbounds = true;
  83. Vector3 flowpos = tm.MultiplyPoint3x4(pos);
  84. while ( duration > 0.0001f )
  85. {
  86. Vector3 airvel = invtm.MultiplyVector(frame.GetGridVel(flowpos, ref inbounds));
  87. if ( inbounds )
  88. {
  89. airvel.x *= scl;
  90. airvel.y *= scl;
  91. airvel.z *= scl;
  92. tvel.x = airvel.x - vel.x;
  93. tvel.y = airvel.y - vel.y;
  94. tvel.z = airvel.z - vel.z;
  95. float l = (tvel.x * tvel.x) + (tvel.y * tvel.y) + (tvel.z * tvel.z);
  96. if ( l != 0.0f )
  97. {
  98. l = Mathf.Sqrt(l);
  99. float df = coef * l;
  100. l = 1.0f / l;
  101. dir.x = tvel.x * l;
  102. dir.y = tvel.y * l;
  103. dir.z = tvel.z * l;
  104. float dfm = df * oomass;
  105. acc.x = dir.x * dfm;
  106. acc.y = dir.y * dfm;
  107. acc.z = dir.z * dfm;
  108. vel.x += acc.x * usedt;
  109. vel.y += acc.y * usedt;
  110. vel.z += acc.z * usedt;
  111. }
  112. }
  113. pos.x += vel.x * usedt;
  114. pos.y += vel.y * usedt;
  115. pos.z += vel.z * usedt;
  116. duration -= usedt;
  117. }
  118. particles[i].position = pos;
  119. particles[i].velocity = vel;
  120. }
  121. }
  122. void RunSimNew(int start, int end)
  123. {
  124. Vector3 tvel;
  125. Vector3 acc;
  126. Vector3 dir;
  127. float scl = source.Scale * scale;
  128. for ( int i = start; i < end; i++ )
  129. {
  130. Vector3 pos = particles[i].position;
  131. Vector3 vel = particles[i].velocity;
  132. float duration = tdt;
  133. bool inbounds = true;
  134. bool inbounds1 = true;
  135. Vector3 flowpos = tm.MultiplyPoint3x4(pos);
  136. Vector3 flowpos1 = tm1.MultiplyPoint3x4(pos);
  137. while ( duration > 0.0001f )
  138. {
  139. Vector3 airvel = invtm.MultiplyVector(frame.GetGridVel(flowpos, ref inbounds));
  140. if ( inbounds )
  141. {
  142. Vector3 airvel1 = invtm1.MultiplyVector(frame1.GetGridVel(flowpos1, ref inbounds1));
  143. if ( inbounds1 )
  144. {
  145. airvel = Vector3.Lerp(airvel, airvel1, framealpha);
  146. airvel.x *= scl;
  147. airvel.y *= scl;
  148. airvel.z *= scl;
  149. tvel.x = airvel.x - vel.x;
  150. tvel.y = airvel.y - vel.y;
  151. tvel.z = airvel.z - vel.z;
  152. float l = (tvel.x * tvel.x) + (tvel.y * tvel.y) + (tvel.z * tvel.z);
  153. if ( l != 0.0f )
  154. {
  155. l = Mathf.Sqrt(l);
  156. float df = coef * l;
  157. l = 1.0f / l;
  158. dir.x = tvel.x * l;
  159. dir.y = tvel.y * l;
  160. dir.z = tvel.z * l;
  161. float dfm = df * oomass;
  162. acc.x = dir.x * dfm;
  163. acc.y = dir.y * dfm;
  164. acc.z = dir.z * dfm;
  165. vel.x += acc.x * usedt;
  166. vel.y += acc.y * usedt;
  167. vel.z += acc.z * usedt;
  168. }
  169. }
  170. }
  171. pos.x += vel.x * usedt;
  172. pos.y += vel.y * usedt;
  173. pos.z += vel.z * usedt;
  174. duration -= usedt;
  175. }
  176. particles[i].position = pos;
  177. particles[i].velocity = vel;
  178. }
  179. }
  180. void Start()
  181. {
  182. if ( Cores == 0 )
  183. Cores = SystemInfo.processorCount - 1;
  184. if ( particle == null )
  185. particle = GetComponent<ParticleEmitter>();
  186. particles = new Particle[maxparticles];
  187. }
  188. #if !UNITY_FLASH && !UNITY_PS3 && !UNITY_METRO && !UNITY_WP8
  189. public bool UseThreading = false;
  190. public class MegaFlowTaskInfo
  191. {
  192. public volatile int start;
  193. public volatile int end;
  194. public AutoResetEvent pauseevent;
  195. public Thread _thread;
  196. }
  197. public MegaFlowTaskInfo[] tasks;
  198. void MakeThreads()
  199. {
  200. if ( Cores > 0 )
  201. {
  202. isRunning = true;
  203. tasks = new MegaFlowTaskInfo[Cores];
  204. for ( int i = 0; i < Cores; i++ )
  205. {
  206. tasks[i] = new MegaFlowTaskInfo();
  207. tasks[i].pauseevent = new AutoResetEvent(false);
  208. tasks[i]._thread = new Thread(DoWork);
  209. tasks[i]._thread.Start(tasks[i]);
  210. }
  211. }
  212. }
  213. void Update()
  214. {
  215. if ( source && particle && source.frames.Count > 0 )
  216. {
  217. framenum = Mathf.Clamp(framenum, 0, source.frames.Count - 1);
  218. frame = source.frames[framenum];
  219. framenum1 = Mathf.Clamp(framenum1, 0, source.frames.Count - 1);
  220. frame1 = source.frames[framenum1];
  221. source.Prepare();
  222. tdt = Time.deltaTime;
  223. //Matrix4x4 offtm = Matrix4x4.TRS(frame.size * 0.5f, Quaternion.identity, Vector3.one);
  224. Matrix4x4 offtm = Matrix4x4.TRS((frame.size * 0.5f) + frame.offset, Quaternion.identity, Vector3.one);
  225. tm = offtm * source.transform.worldToLocalMatrix;
  226. invtm = tm.inverse;
  227. offtm = Matrix4x4.TRS((frame1.size * 0.5f) + frame1.offset, Quaternion.identity, Vector3.one);
  228. tm1 = offtm * source.transform.worldToLocalMatrix;
  229. invtm1 = tm1.inverse;
  230. float p = source.Density;
  231. float A = area;
  232. float Re = source.Reynolds;
  233. oomass = 1.0f / mass;
  234. coef = 1.0f * p * A * Mathf.Pow(Re, -0.5f);
  235. t += Time.deltaTime;
  236. usedt = dt;
  237. if ( usedt > tdt )
  238. usedt = tdt;
  239. int count = particle.particleCount;
  240. particles = particle.particles;
  241. if ( !UseThreading || Cores < 1 || !Application.isPlaying )
  242. RunSim(0, count);
  243. else
  244. {
  245. if ( Cores == 0 )
  246. Cores = SystemInfo.processorCount - 1;
  247. if ( tasks == null )
  248. MakeThreads();
  249. int step = count / (Cores + 1);
  250. if ( Cores > 0 )
  251. {
  252. int index = step;
  253. for ( int i = 0; i < tasks.Length; i++ )
  254. {
  255. tasks[i].start = index;
  256. tasks[i].end = index + step;
  257. index += step;
  258. }
  259. tasks[Cores - 1].end = count;
  260. for ( int i = 0; i < tasks.Length; i++ )
  261. tasks[i].pauseevent.Set();
  262. }
  263. RunSim(0, step);
  264. WaitJobs();
  265. }
  266. particle.particles = particles;
  267. }
  268. }
  269. void WaitJobs()
  270. {
  271. if ( Cores > 0 )
  272. {
  273. int count = 0;
  274. bool wait = false;
  275. do
  276. {
  277. wait = false;
  278. for ( int i = 0; i < tasks.Length; i++ )
  279. {
  280. if ( tasks[i].end > 0 )
  281. {
  282. wait = true;
  283. break;
  284. }
  285. }
  286. if ( wait )
  287. {
  288. count++;
  289. Thread.Sleep(0);
  290. }
  291. } while ( wait );
  292. }
  293. }
  294. void OnApplicationQuit()
  295. {
  296. if ( Application.isPlaying )
  297. {
  298. isRunning = false;
  299. if ( tasks != null )
  300. {
  301. for ( int i = 0; i < tasks.Length; i++ )
  302. {
  303. tasks[i].pauseevent.Set();
  304. while ( tasks[i]._thread.IsAlive )
  305. {
  306. }
  307. }
  308. }
  309. tasks = null;
  310. }
  311. }
  312. static bool isRunning = true;
  313. void DoWork(object info)
  314. {
  315. MegaFlowTaskInfo inf = (MegaFlowTaskInfo)info;
  316. while ( isRunning )
  317. {
  318. inf.pauseevent.WaitOne(Timeout.Infinite, false);
  319. RunSim(inf.start, inf.end);
  320. inf.end = 0;
  321. }
  322. }
  323. #else
  324. void Update()
  325. {
  326. if ( source && particle && source.frames.Count > 0 )
  327. {
  328. //tdt = Time.deltaTime;
  329. //tm = source.transform.worldToLocalMatrix;
  330. //invtm = source.transform.localToWorldMatrix;
  331. framenum = Mathf.Clamp(framenum, 0, source.frames.Count - 1);
  332. frame = source.frames[framenum];
  333. framenum1 = Mathf.Clamp(framenum1, 0, source.frames.Count - 1);
  334. frame1 = source.frames[framenum1];
  335. source.Prepare();
  336. tdt = Time.deltaTime;
  337. //Matrix4x4 offtm = Matrix4x4.TRS(frame.size * 0.5f, Quaternion.identity, Vector3.one);
  338. Matrix4x4 offtm = Matrix4x4.TRS((frame.size * 0.5f) + frame.offset, Quaternion.identity, Vector3.one);
  339. tm = offtm * source.transform.worldToLocalMatrix;
  340. invtm = tm.inverse;
  341. offtm = Matrix4x4.TRS((frame1.size * 0.5f) + frame1.offset, Quaternion.identity, Vector3.one);
  342. tm1 = offtm * source.transform.worldToLocalMatrix;
  343. invtm1 = tm1.inverse;
  344. float p = source.Density;
  345. float A = Area;
  346. float Re = source.Reynolds;
  347. coef = 1.0f * p * A * Mathf.Pow(Re, -0.5f);
  348. t += Time.deltaTime;
  349. int count = particle.particleCount;
  350. particles = particle.particles;
  351. RunSim(0, count);
  352. particle.particles = particles;
  353. }
  354. }
  355. #endif
  356. public void UpdateSim()
  357. {
  358. if ( source && particle && source.frames.Count > 0 )
  359. {
  360. frame = source.frames[framenum];
  361. tdt = Time.deltaTime;
  362. //Matrix4x4 offtm = Matrix4x4.TRS(frame.size * 0.5f, Quaternion.identity, Vector3.one);
  363. Matrix4x4 offtm = Matrix4x4.TRS((frame.size * 0.5f) + frame.offset, Quaternion.identity, Vector3.one);
  364. tm = offtm * source.transform.worldToLocalMatrix;
  365. invtm = tm.inverse;
  366. float p = source.Density;
  367. float A = area;
  368. float Re = source.Reynolds;
  369. coef = 1.0f * p * A * Mathf.Pow(Re, -0.5f);
  370. t += Time.deltaTime;
  371. int count = particle.particleCount;
  372. particles = particle.particles;
  373. RunSim(0, count);
  374. particle.particles = particles;
  375. }
  376. }
  377. }
  378. #endif