MegaFlow.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571
  1. using UnityEngine;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.IO;
  5. [System.Serializable]
  6. public class Ribbon
  7. {
  8. public List<Vector3> points = new List<Vector3>();
  9. public List<Color> cols = new List<Color>();
  10. }
  11. // I dont think real flow has support for vector fields so may not need here, only in particle cache systems
  12. public enum MegaFlowDataSource
  13. {
  14. FumeFX,
  15. MegaFlow,
  16. FGA,
  17. RealFlow,
  18. }
  19. public enum MegaFlowParticleFileType
  20. {
  21. RealFlow,
  22. }
  23. public enum MegaFlowAxis
  24. {
  25. X,
  26. Y,
  27. Z,
  28. }
  29. [AddComponentMenu("MegaFlow/Source")]
  30. [ExecuteInEditMode]
  31. public class MegaFlow : MonoBehaviour
  32. {
  33. static public float AXIS_SIZE = 1.0f;
  34. static Color[] Cols = {
  35. new Color(0.0f, 0.0f, 1.0f, 1.0f),
  36. new Color(0.0f, 1.0f, 1.0f),
  37. new Color(0.0f, 1.0f, 0.0f),
  38. new Color(1.0f, 1.0f, 0.0f),
  39. new Color(1.0f, 0.0f, 0.0f),
  40. };
  41. public Gradient velcols; // = new Gradient();
  42. public bool showvelmag = true;
  43. public bool showcellmag = false;
  44. public float minvel = 0.0f;
  45. public float maxvel = 1.0f;
  46. public string file = "";
  47. public MegaFlowFrame flow;
  48. public int frame = 0;
  49. public float smokeThreshold = 1.0f;
  50. public float velThreshold = 0.0f;
  51. public bool showVel = true;
  52. public bool showSmoke = true;
  53. public bool shownotselected = true;
  54. public bool Slice = true;
  55. public bool showgrid = false;
  56. public int Position = 0;
  57. public int Thickness = 1;
  58. public Vector3 ribpos = Vector3.zero;
  59. public float Dt = 0.01f;
  60. public float Density = 1.22f;
  61. public float Area = 0.01f;
  62. public float Reynolds = 20.0f;
  63. public float Mass = 0.001f;
  64. public int LineStep = 1;
  65. public float SizeZ = 1.0f;
  66. public int StepZ = 1;
  67. public float SizeY = 1.0f;
  68. public int StepY = 1;
  69. public Vector3 Gravity = Vector3.zero;
  70. public float Duration = 12.0f;
  71. public MegaFlowAxis Plane = MegaFlowAxis.Y;
  72. public float Scale = 1.0f;
  73. public bool showRibbon = false;
  74. public Color gridColor = Color.white;
  75. public Color gridColor1 = Color.white;
  76. public float ribbonAlpha = 1.0f;
  77. public float velAlpha = 1.0f;
  78. public float velLen = 1.0f;
  79. public float velScl = 1.0f;
  80. public float smokeAlpha = 1.0f;
  81. public int calculated = 0;
  82. public float calcTime = 0.0f;
  83. public List<Ribbon> ribbons = new List<Ribbon>();
  84. public float floor = 0.0f;
  85. public bool showcells = false;
  86. public List<MegaFlowFrame> frames = new List<MegaFlowFrame>();
  87. public MegaFlowDataSource datasource = MegaFlowDataSource.MegaFlow;
  88. public bool sequence = false;
  89. public int firstframe = 0;
  90. public int lastframe = 1;
  91. public int framestep = 1;
  92. public int skip = 0;
  93. public bool animate = false;
  94. public float animspeed = 1.0f;
  95. public float animtime = 1.0f;
  96. public float animlength = 5.0f;
  97. public float ribbonscale = 1.0f;
  98. public float cellalpha = 0.5f;
  99. public Vector3 handlepos = Vector3.zero;
  100. public Transform fluidPos;
  101. public int memoryuse = 0;
  102. public bool optimized = false;
  103. public bool showdisplayparams = false;
  104. public bool showribbonparams = false;
  105. public bool showanimparams = false;
  106. public bool textureoptions = false;
  107. public int texturewidth = 128;
  108. public int textureheight = 128;
  109. public int texturedepth = 128;
  110. public bool showadvparams = false;
  111. public int samplex = 128;
  112. public int sampley = 128;
  113. public int samplez = 128;
  114. public int decform = 0;
  115. public string namesplit = "";
  116. [ContextMenu("Help")]
  117. public void Help()
  118. {
  119. Application.OpenURL("http://www.west-racing.com/mf/?page_id=5894");
  120. }
  121. void Awake()
  122. {
  123. if ( velcols == null )
  124. {
  125. velcols = new Gradient();
  126. GradientColorKey[] keys = new GradientColorKey[5];
  127. for ( int i = 0; i < 5; i++ )
  128. {
  129. keys[i].color = Cols[i];
  130. keys[i].time = (float)i / 4.0f;
  131. }
  132. GradientAlphaKey[] akeys = new GradientAlphaKey[2];
  133. akeys[0].alpha = 1.0f;
  134. akeys[0].time = 0.0f;
  135. akeys[1].alpha = 1.0f;
  136. akeys[1].time = 0.0f;
  137. velcols.SetKeys(keys, akeys);
  138. }
  139. }
  140. void Start()
  141. {
  142. Prepare();
  143. }
  144. public void OptimizeData()
  145. {
  146. for ( int i = 0; i < frames.Count; i++ )
  147. {
  148. MegaFlowFrame frame = frames[i];
  149. if ( frame.optvel == null || frame.optvel.Count == 0 )
  150. frame.Optimize();
  151. }
  152. CalcMemUse();
  153. }
  154. void OnDrawGizmos()
  155. {
  156. Gizmos.color = Color.grey;
  157. Gizmos.DrawIcon(transform.position, "MegaFlowIcon.png", true);
  158. }
  159. public void Animate()
  160. {
  161. if ( animate && frames.Count > 1 )
  162. {
  163. animtime += Time.deltaTime * animspeed;
  164. if ( animtime > animlength )
  165. animtime -= animlength;
  166. if ( animtime < 0.0f )
  167. animtime += animlength;
  168. frame = (int)((animtime / animlength) * frames.Count);
  169. }
  170. }
  171. void Update()
  172. {
  173. if ( frames.Count > 0 )
  174. {
  175. Animate();
  176. frame = Mathf.Clamp(frame, 0, frames.Count - 1);
  177. flow = frames[frame];
  178. if ( flow.SampleVel == null )
  179. {
  180. if ( flow.optimized )
  181. {
  182. if ( flow.gridDim2[2] == 1 )
  183. {
  184. flow.SampleVel = flow.SampleVelOpt;
  185. flow.GetGridVel = flow.GetGridVelOptXY;
  186. }
  187. else
  188. {
  189. flow.SampleVel = flow.SampleVelOpt;
  190. flow.GetGridVel = flow.GetGridVelOpt;
  191. }
  192. }
  193. else
  194. {
  195. if ( flow.gridDim2[2] == 1 )
  196. {
  197. flow.SampleVel = flow.SampleVelFloat;
  198. flow.GetGridVel = flow.GetGridVelFloatXY;
  199. }
  200. else
  201. {
  202. flow.SampleVel = flow.SampleVelFloat;
  203. flow.GetGridVel = flow.GetGridVelFloat;
  204. }
  205. }
  206. }
  207. }
  208. }
  209. public void SetFrame(int f)
  210. {
  211. if ( frames.Count > 0 )
  212. {
  213. f = Mathf.Clamp(f, 0, frames.Count - 1);
  214. flow = frames[f];
  215. frame = f;
  216. }
  217. else
  218. {
  219. frame = 0;
  220. flow = null;
  221. }
  222. }
  223. public void AddFrame(MegaFlowFrame frame)
  224. {
  225. if ( frame )
  226. {
  227. if ( frames == null )
  228. frames = new List<MegaFlowFrame>();
  229. frames.Add(frame);
  230. if ( flow == null )
  231. flow = frame;
  232. }
  233. }
  234. public void DestroyFrame(int f)
  235. {
  236. if ( f >= 0 && f < frames.Count )
  237. {
  238. MegaFlowFrame frame = frames[f];
  239. frames.RemoveAt(f);
  240. if ( flow == frame )
  241. {
  242. if ( frames.Count > 0 )
  243. flow = frames[0];
  244. else
  245. flow = null;
  246. }
  247. if ( Application.isEditor )
  248. DestroyImmediate(frame);
  249. else
  250. Destroy(frame);
  251. }
  252. }
  253. public void DestroyFrames()
  254. {
  255. flow = null;
  256. for ( int i = 0; i < frames.Count; i++ )
  257. {
  258. MegaFlowFrame frame = frames[i];
  259. if ( Application.isEditor )
  260. DestroyImmediate(frame);
  261. else
  262. Destroy(frame);
  263. }
  264. frames.Clear();
  265. GC.Collect();
  266. }
  267. public void Prepare()
  268. {
  269. Matrix4x4 wltm = transform.worldToLocalMatrix;
  270. for ( int i = 0; i < frames.Count; i++ )
  271. frames[i].Prepare(wltm);
  272. }
  273. public void CalcMemUse()
  274. {
  275. memoryuse = 0;
  276. for ( int i = 0; i < frames.Count; i++ )
  277. {
  278. MegaFlowFrame frame = frames[i];
  279. if ( frame.optimized )
  280. frame.memory = frame.gridDim2[0] * frame.gridDim2[1] * frame.gridDim2[2] * 3;
  281. else
  282. frame.memory = frame.gridDim2[0] * frame.gridDim2[1] * frame.gridDim2[2] * 12;
  283. memoryuse += frame.memory;
  284. }
  285. }
  286. Matrix4x4 gettm = Matrix4x4.identity;
  287. Matrix4x4 invgettm = Matrix4x4.identity;
  288. public Color GetCol(float alpha)
  289. {
  290. return velcols.Evaluate(alpha);
  291. }
  292. public void SetMatrix()
  293. {
  294. Matrix4x4 offtm = Matrix4x4.TRS((flow.size * 0.5f) + flow.offset, Quaternion.identity, Vector3.one);
  295. gettm = offtm * transform.worldToLocalMatrix;
  296. invgettm = gettm.inverse;
  297. }
  298. public Vector3 GetGridVelWorld(Vector3 pos, ref bool inbounds)
  299. {
  300. return invgettm.MultiplyVector(flow.GetGridVel(gettm.MultiplyPoint3x4(pos), ref inbounds) * Scale);
  301. }
  302. public Vector3 GetGridVel(Vector3 pos, ref bool inbounds)
  303. {
  304. return flow.GetGridVel(pos, ref inbounds) * Scale;
  305. }
  306. public Vector3 SampleVel(int x, int y, int z)
  307. {
  308. return flow.SampleVel(x, y, z) * Scale;
  309. }
  310. static public void DrawEdge(Vector3 p1, Vector3 p2)
  311. {
  312. Vector3 last = p1;
  313. Vector3 pos = Vector3.zero;
  314. for ( int i = 1; i <= 10; i++ )
  315. {
  316. pos = p1 + ((p2 - p1) * ((float)i / (float)10));
  317. if ( (i & 1) == 0 )
  318. Gizmos.color = Color.green;
  319. else
  320. Gizmos.color = Color.yellow;
  321. Gizmos.DrawLine(last, pos);
  322. last = pos;
  323. }
  324. Gizmos.color = Color.white;
  325. }
  326. static public void DrawBox(Vector3 min, Vector3 max)
  327. {
  328. Vector3[] corners = new Vector3[8];
  329. corners[0] = new Vector3(min.x, min.y, min.z);
  330. corners[1] = new Vector3(min.x, max.y, min.z);
  331. corners[2] = new Vector3(max.x, max.y, min.z);
  332. corners[3] = new Vector3(max.x, min.y, min.z);
  333. corners[4] = new Vector3(min.x, min.y, max.z);
  334. corners[5] = new Vector3(min.x, max.y, max.z);
  335. corners[6] = new Vector3(max.x, max.y, max.z);
  336. corners[7] = new Vector3(max.x, min.y, max.z);
  337. DrawEdge(corners[0], corners[1]);
  338. DrawEdge(corners[1], corners[2]);
  339. DrawEdge(corners[2], corners[3]);
  340. DrawEdge(corners[3], corners[0]);
  341. DrawEdge(corners[4], corners[5]);
  342. DrawEdge(corners[5], corners[6]);
  343. DrawEdge(corners[6], corners[7]);
  344. DrawEdge(corners[7], corners[4]);
  345. DrawEdge(corners[0], corners[4]);
  346. DrawEdge(corners[1], corners[5]);
  347. DrawEdge(corners[2], corners[6]);
  348. DrawEdge(corners[3], corners[7]);
  349. }
  350. public void DrawGizmo()
  351. {
  352. if ( flow )
  353. {
  354. //Matrix4x4 offtm = Matrix4x4.TRS(-flow.size * 0.5f, Quaternion.identity, Vector3.one);
  355. Matrix4x4 offtm = Matrix4x4.TRS((-flow.size * 0.5f) + flow.offset, Quaternion.identity, Vector3.one);
  356. Gizmos.matrix = transform.localToWorldMatrix * offtm;
  357. Vector3 min = Vector3.zero;
  358. Vector3 max = flow.size;
  359. DrawBox(min, max);
  360. Gizmos.matrix = Matrix4x4.identity;
  361. }
  362. }
  363. public Texture3D Create3DTexture(int width, int height, int depth, int frame)
  364. {
  365. MegaFlowFrame f = frames[frame];
  366. if ( width == 0 || height == 0 || depth == 0 )
  367. return null;
  368. width = Mathf.ClosestPowerOfTwo(width);
  369. height = Mathf.ClosestPowerOfTwo(height);
  370. depth = Mathf.ClosestPowerOfTwo(depth);
  371. Texture3D tex = new Texture3D(width, height, depth, TextureFormat.RGB24, false);
  372. tex.wrapMode = TextureWrapMode.Repeat;
  373. tex.anisoLevel = 0;
  374. Color[] cols = new Color[width * height * depth];
  375. float max = 0.0f;
  376. for ( int i = 0; i < f.vel.Count; i++ )
  377. {
  378. float m = f.vel[i].sqrMagnitude;
  379. if ( m > max )
  380. max = m;
  381. }
  382. float len = Mathf.Sqrt(max);
  383. Vector3 p;
  384. Color c = Color.white;
  385. bool inbounds = false;
  386. #if false
  387. for ( int x = 0; x < width; x++ )
  388. {
  389. p.x = ((float)x / (float)width) * f.size.x;
  390. for ( int y = 0; y < height; y++ )
  391. {
  392. p.y = ((float)y / (float)height) * f.size.y;
  393. for ( int z = 0; z < depth; z++ )
  394. {
  395. p.z = ((float)z / (float)depth) * f.size.z;
  396. Vector3 vel = f.GetGridVel(p, ref inbounds);
  397. vel /= len;
  398. c.r = (vel.x * 0.5f) + 0.5f;
  399. c.g = (vel.y * 0.5f) + 0.5f;
  400. c.b = (vel.z * 0.5f) + 0.5f;
  401. cols[(x * depth * height) + (z * height) + y] = c;
  402. }
  403. }
  404. }
  405. #endif
  406. int ix = 0;
  407. for ( int z = 0; z < depth; z++ )
  408. {
  409. p.z = ((float)z / (float)depth) * f.size.z;
  410. for ( int y = 0; y < height; y++ )
  411. {
  412. p.y = ((float)y / (float)height) * f.size.y;
  413. for ( int x = 0; x < width; x++ )
  414. {
  415. p.x = ((float)x / (float)width) * f.size.x;
  416. Vector3 vel = f.GetGridVel(p, ref inbounds);
  417. vel /= len;
  418. c.r = (vel.x * 0.5f) + 0.5f;
  419. c.g = (vel.y * 0.5f) + 0.5f;
  420. c.b = (vel.z * 0.5f) + 0.5f;
  421. cols[ix++] = c;
  422. }
  423. }
  424. }
  425. tex.SetPixels(cols);
  426. tex.Apply();
  427. return tex;
  428. }
  429. public void NormalizeFrame(int f)
  430. {
  431. MegaFlowFrame fr = frames[f];
  432. float max = 0.0f;
  433. for ( int i = 0; i < fr.vel.Count; i++ )
  434. {
  435. float vs = fr.vel[i].magnitude;
  436. if ( vs > max )
  437. max = vs;
  438. }
  439. for ( int i = 0; i < fr.vel.Count; i++ )
  440. fr.vel[i] = fr.vel[i] / max;
  441. }
  442. public void Resample(MegaFlowFrame sf, int gx, int gy, int gz)
  443. {
  444. MegaFlowFrame newf = ScriptableObject.CreateInstance<MegaFlowFrame>();
  445. newf.gridDim2[0] = gx;
  446. newf.gridDim2[1] = gy;
  447. newf.gridDim2[2] = gz;
  448. newf.size = sf.size;
  449. newf.gsize = newf.size;
  450. // griddim should have a name change
  451. newf.spacing.x = newf.size.x / newf.gridDim2[0];
  452. newf.spacing.y = newf.size.y / newf.gridDim2[1];
  453. newf.spacing.z = newf.size.z / newf.gridDim2[2];
  454. newf.oos.x = 1.0f / newf.spacing.x;
  455. newf.oos.y = 1.0f / newf.spacing.y;
  456. newf.oos.z = 1.0f / newf.spacing.z;
  457. float adjx = sf.spacing.x * 0.5f;
  458. float adjy = sf.spacing.y * 0.5f;
  459. float adjz = sf.spacing.z * 0.5f;
  460. bool inbounds = false;
  461. Prepare();
  462. Vector3[] cells = new Vector3[gx * gy * gz];
  463. Vector3 p;
  464. for ( int x = 0; x < gx; x++ )
  465. {
  466. p.x = (((float)x / (float)gx) * sf.size.x) + adjx;
  467. for ( int y = 0; y < gy; y++ )
  468. {
  469. p.y = (((float)y / (float)gy) * sf.size.y) + adjy;
  470. for ( int z = 0; z < gz; z++ )
  471. {
  472. p.z = (((float)z / (float)gz) * sf.size.z) + adjz;
  473. cells[(x * gz * gy) + (z * gy) + y] = sf.GetGridVel(p, ref inbounds);
  474. }
  475. }
  476. }
  477. newf.vel.AddRange(cells);
  478. AddFrame(newf);
  479. }
  480. }