MegaFlowRealFlow.cs 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  1. 
  2. using UnityEngine;
  3. using System.Collections.Generic;
  4. using System.IO;
  5. // This is also a seq of files for particle system and I presume for mesh stuff if we do it
  6. public class MegaFlowRealFlow
  7. {
  8. static public Vector3 ReadVector3(BinaryReader br)
  9. {
  10. Vector3 p = Vector3.zero;
  11. p.x = br.ReadSingle();
  12. p.y = br.ReadSingle();
  13. p.z = br.ReadSingle();
  14. return p;
  15. }
  16. // We should interpolate the frames
  17. // Looks like there will be a file per frame, so need to load all in directory etc, then build anim data from there
  18. public void LoadFrames(string path)
  19. {
  20. string[] files = Directory.GetFiles(path);
  21. frames.Clear();
  22. for ( int i = 0; i < files.Length; i++ )
  23. {
  24. string ext = Path.GetExtension(files[i]);
  25. if ( ext == ".bin" )
  26. {
  27. //string filename = Path.GetFileNameWithoutExtension(files[i]);
  28. MegaFlowParticleFrame frame = Load(files[i]);
  29. frames.Add(frame);
  30. }
  31. }
  32. }
  33. List<MegaFlowParticleFrame> frames = new List<MegaFlowParticleFrame>();
  34. MegaFlowParticleFrame Load(string filename)
  35. {
  36. FileStream fs = new FileStream(filename, FileMode.Open, FileAccess.Read, System.IO.FileShare.Read);
  37. BinaryReader br = new BinaryReader(fs);
  38. if ( br != null )
  39. {
  40. //ParticleFrame frame = new ParticleFrame();
  41. MegaFlowParticleFrame frame = MegaFlowParticleFrame.Parse(br);
  42. //DebugSD();
  43. br.Close();
  44. return frame;
  45. }
  46. return null;
  47. }
  48. #if false
  49. // Have the ParticleSystem.Particles array in here then it is just a case of setting it
  50. public class ParticleFrame
  51. {
  52. public string fluidname;
  53. public ushort version;
  54. public float scale;
  55. public int ftype;
  56. public float etime;
  57. public int fnum;
  58. public int fps;
  59. public int numparticles;
  60. public float radius;
  61. public Vector3 pressure;
  62. public Vector3 speed;
  63. public Vector3 temperature;
  64. public Vector3 emitpos;
  65. public Vector3 emitrot;
  66. public Vector3 emitscl;
  67. public ParticleSystem.Particle[] particles1;
  68. public RealFlowParticleData[] particles;
  69. static public ParticleFrame Parse(BinaryReader br)
  70. {
  71. uint code = br.ReadUInt32();
  72. if ( code == 0xFABADA )
  73. {
  74. ParticleFrame frame = new ParticleFrame();
  75. frame.fluidname = br.ReadBytes(250).ToString();
  76. frame.version = br.ReadUInt16();
  77. frame.scale = br.ReadSingle();
  78. frame.ftype = br.ReadInt32();
  79. frame.etime = br.ReadSingle();
  80. frame.fnum = br.ReadInt32();
  81. frame.fps = br.ReadInt32();
  82. frame.numparticles = br.ReadInt32();
  83. frame.radius = br.ReadSingle();
  84. frame.pressure = ReadVector3(br);
  85. frame.speed = ReadVector3(br);
  86. frame.temperature = ReadVector3(br);
  87. if ( frame.version >= 7.0f )
  88. {
  89. frame.emitpos = ReadVector3(br);
  90. frame.emitrot = ReadVector3(br);
  91. frame.emitscl = ReadVector3(br);
  92. }
  93. frame.particles = new RealFlowParticleData[frame.numparticles];
  94. for ( int i = 0; i < frame.numparticles; i++ )
  95. {
  96. RealFlowParticleData p = RealFlowParticleData.Parse(br, frame.version);
  97. frame.particles[i] = p;
  98. }
  99. return frame;
  100. }
  101. return null;
  102. }
  103. }
  104. [System.Serializable]
  105. public class RealFlowParticleData
  106. {
  107. public Vector3 pos;
  108. public Vector3 vel;
  109. public Vector3 frc;
  110. public Vector3 vor;
  111. public Vector3 norm;
  112. public int numneigh;
  113. public Vector3 uvvector;
  114. public ushort info;
  115. public float elapsedtime;
  116. public float isotime;
  117. public float viscosity;
  118. public float density;
  119. public float pressure;
  120. public float mass;
  121. public float temperature;
  122. public uint id;
  123. public static RealFlowParticleData Parse(BinaryReader br, float version)
  124. {
  125. RealFlowParticleData p = new RealFlowParticleData();
  126. //uint code = br.ReadUInt32();
  127. p.pos = ReadVector3(br);
  128. p.vel = ReadVector3(br);
  129. p.frc = ReadVector3(br);
  130. p.vor = ReadVector3(br);
  131. p.norm = ReadVector3(br);
  132. p.numneigh = br.ReadInt32();
  133. p.uvvector = ReadVector3(br);
  134. p.info = br.ReadUInt16();
  135. p.elapsedtime = br.ReadSingle();
  136. p.isotime = br.ReadSingle();
  137. p.viscosity = br.ReadSingle();
  138. p.density = br.ReadSingle();
  139. p.pressure = br.ReadSingle();
  140. p.mass = br.ReadSingle();
  141. p.temperature = br.ReadSingle();
  142. if ( version >= 12.0f )
  143. p.id = (uint)br.ReadUInt64();
  144. else
  145. p.id = br.ReadUInt32();
  146. return p;
  147. }
  148. }
  149. #endif
  150. }
  151. [System.Serializable]
  152. public class MegaFlowParticleData
  153. {
  154. public Vector3 pos;
  155. public Vector3 vel;
  156. public Vector3 frc;
  157. public Vector3 vor;
  158. public Vector3 norm;
  159. public int numneigh;
  160. public Vector3 uvvector;
  161. public ushort info;
  162. public float elapsedtime;
  163. public float isotime;
  164. public float viscosity;
  165. public float density;
  166. public float pressure;
  167. public float mass;
  168. public float temperature;
  169. public uint id;
  170. static public Vector3 ReadVector3(BinaryReader br)
  171. {
  172. Vector3 p = Vector3.zero;
  173. p.x = br.ReadSingle();
  174. p.y = br.ReadSingle();
  175. p.z = br.ReadSingle();
  176. return p;
  177. }
  178. public static MegaFlowParticleData Parse(BinaryReader br, float version)
  179. {
  180. MegaFlowParticleData p = new MegaFlowParticleData();
  181. //uint code = br.ReadUInt32();
  182. p.pos = ReadVector3(br);
  183. p.vel = ReadVector3(br);
  184. p.frc = ReadVector3(br);
  185. p.vor = ReadVector3(br);
  186. p.norm = ReadVector3(br);
  187. p.numneigh = br.ReadInt32();
  188. p.uvvector = ReadVector3(br);
  189. p.info = br.ReadUInt16();
  190. p.elapsedtime = br.ReadSingle();
  191. p.isotime = br.ReadSingle();
  192. p.viscosity = br.ReadSingle();
  193. p.density = br.ReadSingle();
  194. p.pressure = br.ReadSingle();
  195. p.mass = br.ReadSingle();
  196. p.temperature = br.ReadSingle();
  197. if ( version >= 12.0f )
  198. p.id = (uint)br.ReadUInt64();
  199. else
  200. p.id = br.ReadUInt32();
  201. return p;
  202. }
  203. public static ParticleSystem.Particle LoadParticleData(BinaryReader br, float version)
  204. {
  205. ParticleSystem.Particle p = new ParticleSystem.Particle();
  206. p.position = ReadVector3(br);
  207. p.velocity = ReadVector3(br);
  208. ReadVector3(br); // frc
  209. ReadVector3(br); // vor
  210. ReadVector3(br); // norm
  211. br.ReadInt32(); // numneigh
  212. ReadVector3(br); // uvvector
  213. br.ReadUInt16(); // info
  214. br.ReadSingle();
  215. br.ReadSingle(); // isotime
  216. br.ReadSingle(); // viscosity
  217. br.ReadSingle(); // density
  218. br.ReadSingle(); // pressure
  219. br.ReadSingle(); // mass
  220. br.ReadSingle(); // temperature
  221. if ( version >= 12.0f )
  222. br.ReadUInt64();
  223. else
  224. br.ReadUInt32();
  225. return p;
  226. }
  227. }
  228. // Have the ParticleSystem.Particles array in here then it is just a case of setting it
  229. public class MegaFlowParticleFrame
  230. {
  231. public string fluidname;
  232. public ushort version;
  233. public float scale;
  234. public int ftype;
  235. public float etime;
  236. public int fnum;
  237. public int fps;
  238. public int numparticles;
  239. public float radius;
  240. public Vector3 pressure;
  241. public Vector3 speed;
  242. public Vector3 temperature;
  243. public Vector3 emitpos;
  244. public Vector3 emitrot;
  245. public Vector3 emitscl;
  246. public ParticleSystem.Particle[] particles1;
  247. public MegaFlowParticleData[] particles;
  248. static public Vector3 ReadVector3(BinaryReader br)
  249. {
  250. Vector3 p = Vector3.zero;
  251. p.x = br.ReadSingle();
  252. p.y = br.ReadSingle();
  253. p.z = br.ReadSingle();
  254. return p;
  255. }
  256. static public MegaFlowParticleFrame Parse(BinaryReader br)
  257. {
  258. uint code = br.ReadUInt32();
  259. if ( code == 0xFABADA )
  260. {
  261. MegaFlowParticleFrame frame = new MegaFlowParticleFrame();
  262. frame.fluidname = br.ReadBytes(250).ToString();
  263. frame.version = br.ReadUInt16();
  264. frame.scale = br.ReadSingle();
  265. frame.ftype = br.ReadInt32();
  266. frame.etime = br.ReadSingle();
  267. frame.fnum = br.ReadInt32();
  268. frame.fps = br.ReadInt32();
  269. frame.numparticles = br.ReadInt32();
  270. frame.radius = br.ReadSingle();
  271. frame.pressure = ReadVector3(br);
  272. frame.speed = ReadVector3(br);
  273. frame.temperature = ReadVector3(br);
  274. if ( frame.version >= 7.0f )
  275. {
  276. frame.emitpos = ReadVector3(br);
  277. frame.emitrot = ReadVector3(br);
  278. frame.emitscl = ReadVector3(br);
  279. }
  280. frame.particles = new MegaFlowParticleData[frame.numparticles];
  281. frame.particles1 = new ParticleSystem.Particle[frame.numparticles];
  282. for ( int i = 0; i < frame.numparticles; i++ )
  283. {
  284. ParticleSystem.Particle p = MegaFlowParticleData.LoadParticleData(br, frame.version);
  285. frame.particles1[i] = p;
  286. //MegaFlowParticleData p = MegaFlowParticleData.Parse(br, frame.version);
  287. //frame.particles[i] = p;
  288. }
  289. return frame;
  290. }
  291. return null;
  292. }
  293. }