MegaFlowParticleMoving.cs 6.8 KB

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