MegaFlowFumeFX.cs 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  1. using UnityEngine;
  2. using System.IO;
  3. public class MegaFlowFumeFX
  4. {
  5. public static MegaFlowFrame LoadFrame(string filename, int frame, string namesplit)
  6. {
  7. int decformat = 0;
  8. MegaFlowFrame flow = null;
  9. string dir= Path.GetDirectoryName(filename);
  10. string file = Path.GetFileNameWithoutExtension(filename);
  11. file = MegaFlowFGA.MakeFileName(file, ref decformat);
  12. #if false
  13. char[] splits = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' };
  14. string[] names;
  15. if ( namesplit.Length > 0 )
  16. {
  17. names = file.Split(namesplit[0]);
  18. names[0] += namesplit[0];
  19. }
  20. else
  21. names = file.Split(splits);
  22. if ( names.Length > 0 )
  23. {
  24. string newfname = dir + "/" + names[0] + frame.ToString("0000") + ".fxd";
  25. flow = LoadFrame(newfname);
  26. }
  27. #else
  28. if ( file.Length > 0 )
  29. {
  30. string newfname = dir + "/" + file + frame.ToString("D" + decformat) + ".fxd";
  31. flow = LoadFrame(newfname);
  32. }
  33. #endif
  34. return flow;
  35. }
  36. #if false
  37. public static MegaFlowFrame LoadFrame(string filename, int frame)
  38. {
  39. MegaFlowFrame flow = null;
  40. char[] splits = { '_' };
  41. string fname = filename; // use unity get path
  42. string[] names = fname.Split(splits);
  43. if ( names.Length > 0 )
  44. {
  45. string newfname = names[0] + "_" + frame.ToString("0000") + ".fxd";
  46. flow = LoadFrame(newfname);
  47. }
  48. return flow;
  49. }
  50. #endif
  51. public static MegaFlowFrame LoadFrame(string filename)
  52. {
  53. MegaFlowFrame flow = null;
  54. if ( File.Exists(filename) )
  55. {
  56. flow = ScriptableObject.CreateInstance<MegaFlowFrame>();
  57. Load(flow, filename);
  58. }
  59. return flow;
  60. }
  61. static public void Load(MegaFlowFrame flow, string filename)
  62. {
  63. FileStream fs = new FileStream(filename, FileMode.Open, FileAccess.Read, System.IO.FileShare.Read);
  64. BinaryReader br = new BinaryReader(fs);
  65. if ( br != null )
  66. {
  67. flow.Init();
  68. Parse(flow, br);
  69. br.Close();
  70. }
  71. fs.Close();
  72. }
  73. static public void Parse(MegaFlowFrame flow, BinaryReader br)
  74. {
  75. int head = br.ReadInt16(); // head val
  76. flow.framenumber = br.ReadInt32();
  77. flow.fval = br.ReadSingle();
  78. flow.spacing.x = br.ReadSingle();
  79. flow.spacing.y = flow.spacing.x;
  80. flow.spacing.z = flow.spacing.x;
  81. flow.oos.x = 1.0f / flow.spacing.x;
  82. flow.oos.y = 1.0f / flow.spacing.y;
  83. flow.oos.z = 1.0f / flow.spacing.z;
  84. flow.size.x = br.ReadSingle();
  85. flow.size.z = br.ReadSingle();
  86. flow.size.y = br.ReadSingle();
  87. flow.gsize.x = br.ReadSingle();
  88. flow.gsize.z = br.ReadSingle();
  89. flow.gsize.y = br.ReadSingle();
  90. flow.gridDim[0] = br.ReadInt32();
  91. flow.gridDim[2] = br.ReadInt32();
  92. flow.gridDim[1] = br.ReadInt32();
  93. flow.gridDim1[0] = br.ReadInt32();
  94. flow.gridDim1[2] = br.ReadInt32();
  95. flow.gridDim1[1] = br.ReadInt32();
  96. flow.gridDim2[0] = br.ReadInt32();
  97. flow.gridDim2[2] = br.ReadInt32();
  98. flow.gridDim2[1] = br.ReadInt32();
  99. flow.gridDim2[0] = flow.gridDim[0];
  100. flow.gridDim2[1] = flow.gridDim[1];
  101. flow.gridDim2[2] = flow.gridDim[2];
  102. flow.somebool = br.ReadBoolean();
  103. flow.flags = br.ReadInt32();
  104. if ( head == 31 )
  105. br.ReadInt32();
  106. flow.fval1 = br.ReadSingle();
  107. if ( (flow.flags & 8) != 0 )
  108. ParseSmoke(flow, br);
  109. else
  110. Debug.Log("No Smoke");
  111. if ( (flow.flags & 0x20) != 0 )
  112. ParseVelocity(flow, br);
  113. else
  114. Debug.LogWarning("No Velocity Data in FXD file, no flow imported, please ensure FXD file has velocity data present.");
  115. ParseGrid(flow, br);
  116. }
  117. static public bool ReadChunk(BinaryReader br)
  118. {
  119. uint[] chunk = new uint[4];
  120. chunk[0] = br.ReadUInt32();
  121. chunk[1] = br.ReadUInt32();
  122. chunk[2] = br.ReadUInt32();
  123. chunk[3] = br.ReadUInt32();
  124. if ( chunk[0] == 0xaaaaaaaa && chunk[1] == 0xbbbbbbbb && chunk[2] == 0xcccccccc && chunk[3] == 0xdddddddd )
  125. return true;
  126. return false;
  127. }
  128. static public void ParseSmoke(MegaFlowFrame flow, BinaryReader br)
  129. {
  130. if ( ReadChunk(br) )
  131. {
  132. int count = br.ReadInt32();
  133. while ( count > 0 )
  134. {
  135. ushort cw = br.ReadUInt16();
  136. count--;
  137. if ( cw == 0xffff )
  138. {
  139. ushort zc = br.ReadUInt16();
  140. count--;
  141. for ( int i = 0; i < zc; i++ )
  142. flow.smoke.Add(0.0f);
  143. }
  144. else
  145. {
  146. float val = (float)cw / 65535.0f; //32768.0f; // / (float)cw;
  147. flow.smoke.Add(val);
  148. }
  149. }
  150. br.ReadSingle(); // val1 float
  151. }
  152. }
  153. // ff = do next byte of zeros then read words till
  154. // 7fff = do next word of zeros
  155. // Guess any shortfall in length is padded with zeroes
  156. static public void ParseVelocity(MegaFlowFrame flow, BinaryReader br)
  157. {
  158. int len = flow.gridDim2[0] * flow.gridDim2[1] * flow.gridDim2[2];
  159. for ( int j = 0; j < 3; j++ )
  160. {
  161. if ( ReadChunk(br) )
  162. {
  163. int count = br.ReadInt32();
  164. int index = 0;
  165. while ( count > 0 )
  166. {
  167. ushort data = br.ReadUInt16();
  168. count--;
  169. if ( data == 0x7fff )
  170. {
  171. ushort zc = br.ReadUInt16();
  172. count--;
  173. for ( int z = 0; z < zc; z++ )
  174. {
  175. if ( j == 0 )
  176. {
  177. flow.vel.Add(Vector3.zero);
  178. index++;
  179. }
  180. else
  181. {
  182. Vector3 v = flow.vel[index];
  183. switch ( j )
  184. {
  185. case 0: v.x = 0.0f; break;
  186. case 1: v.z = 0.0f; break;
  187. case 2: v.y = 0.0f; break;
  188. }
  189. flow.vel[index++] = v;
  190. }
  191. }
  192. }
  193. else
  194. {
  195. short v = (short)data;
  196. float val = (float)v / 32767.0f; //32768.0f
  197. if ( j == 0 )
  198. {
  199. flow.vel.Add(new Vector3(val, 0.0f, 0.0f));
  200. index++;
  201. }
  202. else
  203. {
  204. Vector3 v1 = flow.vel[index];
  205. switch ( j )
  206. {
  207. case 0: v1.x = val; break;
  208. case 1: v1.z = val; break;
  209. case 2: v1.y = val; break;
  210. }
  211. flow.vel[index++] = v1;
  212. }
  213. }
  214. }
  215. for ( int p = index; p < len; p++ )
  216. {
  217. if ( j == 0 )
  218. {
  219. flow.vel.Add(Vector3.zero);
  220. index++;
  221. }
  222. else
  223. {
  224. Vector3 v1 = flow.vel[index];
  225. switch ( j )
  226. {
  227. case 0: v1.x = 0.0f; break;
  228. case 1: v1.z = 0.0f; break;
  229. case 2: v1.y = 0.0f; break;
  230. }
  231. flow.vel[index++] = v1;
  232. }
  233. }
  234. }
  235. }
  236. }
  237. static public void ParseForce(MegaFlowFrame flow, BinaryReader br)
  238. {
  239. }
  240. static public void ParseGrid(MegaFlowFrame flow, BinaryReader br)
  241. {
  242. #if false
  243. if ( ReadChunk(br) )
  244. {
  245. for ( int i = 0; i < flow.gridDim[0] * flow.gridDim[1] * flow.gridDim[2]; i++ )
  246. flow.grid.Add(0);
  247. int length = br.ReadInt32();
  248. byte[] data = br.ReadBytes(length);
  249. int index = 0;
  250. int si = 0;
  251. while ( si < length )
  252. {
  253. byte ch = data[si++];
  254. if ( si >= length )
  255. break;
  256. if ( ch == 255 )
  257. index += data[si++];
  258. else
  259. flow.grid[index] = ch;
  260. }
  261. }
  262. #endif
  263. }
  264. }