MegaFlowParticle.cs 9.6 KB

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