MegaPointCacheEditor.cs 25 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109
  1. using UnityEngine;
  2. using UnityEditor;
  3. using System.IO;
  4. using System.Collections.Generic;
  5. // Support anim uvs here as well
  6. [CanEditMultipleObjects, CustomEditor(typeof(MegaPointCache))]
  7. public class MegaPointCacheEditor : MegaModifierEditor
  8. {
  9. static string lastpath = " ";
  10. public delegate bool ParseBinCallbackType(BinaryReader br, string id);
  11. public delegate void ParseClassCallbackType(string classname, BinaryReader br);
  12. MegaModifiers mods;
  13. List<MegaPCVert> Verts = new List<MegaPCVert>();
  14. // Mapping values
  15. float tolerance = 0.0001f;
  16. float scl = 1.0f;
  17. bool flipyz = false;
  18. bool negx = false;
  19. bool negz = false; // 8 cases now
  20. string Read(BinaryReader br, int count)
  21. {
  22. byte[] buf = br.ReadBytes(count);
  23. return System.Text.Encoding.ASCII.GetString(buf, 0, buf.Length);
  24. }
  25. struct MCCFrame
  26. {
  27. public Vector3[] points;
  28. }
  29. // Maya format
  30. void LoadMCC()
  31. {
  32. //MegaPointCache am = (MegaPointCache)target;
  33. //mods = am.gameObject.GetComponent<MegaModifiers>();
  34. string filename = EditorUtility.OpenFilePanel("Maya Cache File", lastpath, "mc");
  35. if ( filename == null || filename.Length < 1 )
  36. return;
  37. LoadMCC(filename);
  38. }
  39. #if false
  40. public void LoadMCC(string filename)
  41. {
  42. MegaPointCache am = (MegaPointCache)target;
  43. mods = am.gameObject.GetComponent<MegaModifiers>();
  44. if ( mods == null )
  45. {
  46. Debug.LogWarning("You need to add a Mega Modify Object component first!");
  47. return;
  48. }
  49. lastpath = filename;
  50. FileStream fs = new FileStream(filename, FileMode.Open, FileAccess.Read, System.IO.FileShare.Read);
  51. BinaryReader br = new BinaryReader(fs);
  52. string id = Read(br, 4);
  53. if ( id != "FOR4" )
  54. {
  55. Debug.Log("wrong file");
  56. return;
  57. }
  58. int offset = MegaParse.ReadMotInt(br);
  59. br.ReadBytes(offset);
  60. List<MCCFrame> frames = new List<MCCFrame>();
  61. while ( true )
  62. {
  63. string btag = Read(br, 4);
  64. if ( btag == "" )
  65. break;
  66. if ( btag != "FOR4" )
  67. {
  68. Debug.Log("File format error");
  69. return;
  70. }
  71. int blocksize = MegaParse.ReadMotInt(br);
  72. int bytesread = 0;
  73. btag = Read(br, 4);
  74. if ( btag != "MYCH" )
  75. {
  76. Debug.Log("File format error");
  77. return;
  78. }
  79. bytesread += 4;
  80. btag = Read(br, 4);
  81. if ( btag != "TIME" )
  82. {
  83. Debug.Log("File format error");
  84. return;
  85. }
  86. bytesread += 4;
  87. br.ReadBytes(4);
  88. bytesread += 4;
  89. int time = MegaParse.ReadMotInt(br);
  90. bytesread += 4;
  91. am.maxtime = (float)time / 6000.0f;
  92. while ( bytesread < blocksize )
  93. {
  94. btag = Read(br, 4);
  95. if ( btag != "CHNM" )
  96. {
  97. Debug.Log("chm error");
  98. return;
  99. }
  100. bytesread += 4;
  101. int chnmsize = MegaParse.ReadMotInt(br);
  102. bytesread += 4;
  103. int mask = 3;
  104. int chnmsizetoread = (chnmsize + mask) & (~mask);
  105. //byte[] channelname = br.ReadBytes(chnmsize);
  106. br.ReadBytes(chnmsize);
  107. int paddingsize = chnmsizetoread - chnmsize;
  108. if ( paddingsize > 0 )
  109. br.ReadBytes(paddingsize);
  110. bytesread += chnmsizetoread;
  111. btag = Read(br, 4);
  112. if ( btag != "SIZE" )
  113. {
  114. Debug.Log("Size error");
  115. return;
  116. }
  117. bytesread += 4;
  118. br.ReadBytes(4);
  119. bytesread += 4;
  120. int arraylength = MegaParse.ReadMotInt(br);
  121. bytesread += 4;
  122. MCCFrame frame = new MCCFrame();
  123. frame.points = new Vector3[arraylength];
  124. string dataformattag = Read(br, 4);
  125. int bufferlength = MegaParse.ReadMotInt(br);
  126. bytesread += 8;
  127. if ( dataformattag == "FVCA" )
  128. {
  129. if ( bufferlength != arraylength * 3 * 4 )
  130. {
  131. Debug.Log("buffer len error");
  132. return;
  133. }
  134. for ( int i = 0; i < arraylength; i++ )
  135. {
  136. frame.points[i].x = MegaParse.ReadMotFloat(br);
  137. frame.points[i].y = MegaParse.ReadMotFloat(br);
  138. frame.points[i].z = MegaParse.ReadMotFloat(br);
  139. }
  140. bytesread += arraylength * 3 * 4;
  141. }
  142. else
  143. {
  144. if ( dataformattag == "DVCA" )
  145. {
  146. if ( bufferlength != arraylength * 3 * 8 )
  147. {
  148. Debug.Log("buffer len error");
  149. return;
  150. }
  151. for ( int i = 0; i < arraylength; i++ )
  152. {
  153. frame.points[i].x = (float)MegaParse.ReadMotDouble(br);
  154. frame.points[i].y = (float)MegaParse.ReadMotDouble(br);
  155. frame.points[i].z = (float)MegaParse.ReadMotDouble(br);
  156. }
  157. bytesread += arraylength * 3 * 8;
  158. }
  159. else
  160. {
  161. Debug.Log("Format Error");
  162. return;
  163. }
  164. }
  165. frames.Add(frame);
  166. }
  167. }
  168. // Build table
  169. am.Verts = new MegaPCVert[frames[0].points.Length];
  170. for ( int i = 0; i < am.Verts.Length; i++ )
  171. {
  172. am.Verts[i] = new MegaPCVert();
  173. am.Verts[i].points = new Vector3[frames.Count];
  174. for ( int p = 0; p < am.Verts[i].points.Length; p++ )
  175. am.Verts[i].points[p] = frames[p].points[i];
  176. }
  177. BuildData(mods, am, filename);
  178. br.Close();
  179. }
  180. #else
  181. public void LoadMCC(string filename)
  182. {
  183. MegaPointCache am = (MegaPointCache)target;
  184. oldverts = am.Verts;
  185. mods = am.gameObject.GetComponent<MegaModifiers>();
  186. if ( mods == null )
  187. {
  188. Debug.LogWarning("You need to add a Mega Modify Object component first!");
  189. return;
  190. }
  191. lastpath = filename;
  192. FileStream fs = new FileStream(filename, FileMode.Open, FileAccess.Read, System.IO.FileShare.Read);
  193. BinaryReader br = new BinaryReader(fs);
  194. string id = Read(br, 4);
  195. if ( id != "FOR4" )
  196. {
  197. Debug.Log("wrong file");
  198. return;
  199. }
  200. int offset = MegaParse.ReadMotInt(br);
  201. br.ReadBytes(offset);
  202. List<MCCFrame> frames = new List<MCCFrame>();
  203. int buflen = 0;
  204. //int clen = 0;
  205. while ( true )
  206. {
  207. string btag = Read(br, 4);
  208. if ( btag == "" )
  209. break;
  210. if ( btag != "FOR4" )
  211. {
  212. Debug.Log("File format error");
  213. return;
  214. }
  215. int blocksize = MegaParse.ReadMotInt(br);
  216. int bytesread = 0;
  217. btag = Read(br, 4);
  218. if ( btag != "MYCH" )
  219. {
  220. Debug.Log("File format error");
  221. return;
  222. }
  223. bytesread += 4;
  224. btag = Read(br, 4);
  225. if ( btag != "TIME" )
  226. {
  227. Debug.Log("File format error");
  228. return;
  229. }
  230. bytesread += 4;
  231. br.ReadBytes(4);
  232. bytesread += 4;
  233. int time = MegaParse.ReadMotInt(br);
  234. bytesread += 4;
  235. am.maxtime = (float)time / 6000.0f;
  236. while ( bytesread < blocksize )
  237. {
  238. btag = Read(br, 4);
  239. if ( btag != "CHNM" )
  240. {
  241. Debug.Log("chm error");
  242. return;
  243. }
  244. bytesread += 4;
  245. int chnmsize = MegaParse.ReadMotInt(br);
  246. bytesread += 4;
  247. int mask = 3;
  248. int chnmsizetoread = (chnmsize + mask) & (~mask);
  249. //byte[] channelname = br.ReadBytes(chnmsize);
  250. br.ReadBytes(chnmsize);
  251. int paddingsize = chnmsizetoread - chnmsize;
  252. if ( paddingsize > 0 )
  253. br.ReadBytes(paddingsize);
  254. bytesread += chnmsizetoread;
  255. btag = Read(br, 4);
  256. if ( btag != "SIZE" )
  257. {
  258. Debug.Log("Size error");
  259. return;
  260. }
  261. bytesread += 4;
  262. br.ReadBytes(4);
  263. bytesread += 4;
  264. int arraylength = MegaParse.ReadMotInt(br);
  265. bytesread += 4;
  266. MCCFrame frame = new MCCFrame();
  267. frame.points = new Vector3[arraylength];
  268. string dataformattag = Read(br, 4);
  269. int bufferlength = MegaParse.ReadMotInt(br);
  270. //Debug.Log("buflen " + bufferlength);
  271. if ( buflen == 0 )
  272. {
  273. buflen = bufferlength;
  274. }
  275. bytesread += 8;
  276. if ( dataformattag == "FVCA" )
  277. {
  278. if ( bufferlength != arraylength * 3 * 4 )
  279. {
  280. Debug.Log("buffer len error");
  281. return;
  282. }
  283. for ( int i = 0; i < arraylength; i++ )
  284. {
  285. frame.points[i].x = MegaParse.ReadMotFloat(br);
  286. frame.points[i].y = MegaParse.ReadMotFloat(br);
  287. frame.points[i].z = MegaParse.ReadMotFloat(br);
  288. }
  289. bytesread += arraylength * 3 * 4;
  290. }
  291. else
  292. {
  293. if ( dataformattag == "DVCA" )
  294. {
  295. if ( bufferlength != arraylength * 3 * 8 )
  296. {
  297. Debug.Log("buffer len error");
  298. return;
  299. }
  300. for ( int i = 0; i < arraylength; i++ )
  301. {
  302. frame.points[i].x = (float)MegaParse.ReadMotDouble(br);
  303. frame.points[i].y = (float)MegaParse.ReadMotDouble(br);
  304. frame.points[i].z = (float)MegaParse.ReadMotDouble(br);
  305. }
  306. bytesread += arraylength * 3 * 8;
  307. }
  308. else
  309. {
  310. Debug.Log("Format Error");
  311. return;
  312. }
  313. }
  314. if ( buflen == bufferlength )
  315. frames.Add(frame);
  316. }
  317. }
  318. //Debug.Log("frames " + frames.Count);
  319. // Build table
  320. am.Verts = new MegaPCVert[frames[0].points.Length];
  321. //Debug.Log("am.Verts " + am.Verts.Length);
  322. for ( int i = 0; i < am.Verts.Length; i++ )
  323. {
  324. am.Verts[i] = new MegaPCVert();
  325. am.Verts[i].points = new Vector3[frames.Count];
  326. for ( int p = 0; p < am.Verts[i].points.Length; p++ )
  327. {
  328. //Debug.Log("i " + i + " p " + p);
  329. am.Verts[i].points[p] = frames[p].points[i];
  330. }
  331. }
  332. BuildData(mods, am, filename);
  333. br.Close();
  334. }
  335. #endif
  336. void LoadMDD()
  337. {
  338. //MegaPointCache am = (MegaPointCache)target;
  339. //mods = am.gameObject.GetComponent<MegaModifiers>();
  340. string filename = EditorUtility.OpenFilePanel("Motion Designer File", lastpath, "mdd");
  341. if ( filename == null || filename.Length < 1 )
  342. return;
  343. LoadMDD(filename);
  344. }
  345. public void LoadMDD(string filename)
  346. {
  347. MegaPointCache am = (MegaPointCache)target;
  348. oldverts = am.Verts;
  349. mods = am.gameObject.GetComponent<MegaModifiers>();
  350. if ( mods == null)
  351. {
  352. Debug.LogWarning("You need to add a Mega Modify Object component first!");
  353. return;
  354. }
  355. lastpath = filename;
  356. // Clear what we have
  357. Verts.Clear();
  358. FileStream fs = new FileStream(filename, FileMode.Open, FileAccess.Read, System.IO.FileShare.Read);
  359. BinaryReader br = new BinaryReader(fs);
  360. int numSamples = MegaParse.ReadMotInt(br);
  361. int numPoints = MegaParse.ReadMotInt(br);
  362. float t = 0.0f;
  363. for ( int i = 0; i < numSamples; i++ )
  364. t = MegaParse.ReadMotFloat(br);
  365. am.maxtime = t;
  366. am.Verts = new MegaPCVert[numPoints];
  367. for ( int i = 0; i < am.Verts.Length; i++ )
  368. {
  369. am.Verts[i] = new MegaPCVert();
  370. am.Verts[i].points = new Vector3[numSamples];
  371. }
  372. for ( int i = 0; i < numSamples; i++ )
  373. {
  374. for ( int v = 0; v < numPoints; v++ )
  375. {
  376. am.Verts[v].points[i].x = MegaParse.ReadMotFloat(br);
  377. am.Verts[v].points[i].y = MegaParse.ReadMotFloat(br);
  378. am.Verts[v].points[i].z = MegaParse.ReadMotFloat(br);
  379. }
  380. }
  381. BuildData(mods, am, filename);
  382. br.Close();
  383. }
  384. public bool DoAdjustFirst = false;
  385. public bool uselastframe = false;
  386. public int mapframe = 0;
  387. MegaPCVert[] oldverts;
  388. void BuildData(MegaModifiers mods, MegaPointCache am, string filename)
  389. {
  390. bool domapping = true;
  391. if ( am.havemapping )
  392. domapping = EditorUtility.DisplayDialog("Point Cache Mapping", "Replace Existing Mapping?", "Yes", "No");
  393. if ( !domapping )
  394. {
  395. if ( DoAdjustFirst )
  396. AdjustVertsSimple(mods, am);
  397. }
  398. else
  399. {
  400. if ( DoAdjustFirst )
  401. AdjustVerts(mods, am);
  402. }
  403. // Build vector3[] of base verts
  404. Vector3[] baseverts = new Vector3[am.Verts.Length];
  405. int findex = 0;
  406. if ( uselastframe )
  407. {
  408. findex = am.Verts[0].points.Length - 1;
  409. }
  410. for ( int i = 0; i < am.Verts.Length; i++ )
  411. baseverts[i] = am.Verts[i].points[findex];
  412. #if false
  413. for ( int i = 0; i < 32; i++ )
  414. {
  415. Debug.Log("vert " + mods.verts[i].ToString("0.000"));
  416. }
  417. for ( int i = 0; i < 32; i++ )
  418. {
  419. Debug.Log("pc " + baseverts[i].ToString("0.000"));
  420. }
  421. #endif
  422. if ( domapping )
  423. {
  424. if ( !TryMapping1(baseverts, mods.verts) )
  425. {
  426. EditorUtility.DisplayDialog("Mapping Failed!", "Mapping of " + System.IO.Path.GetFileNameWithoutExtension(filename) + " failed!", "OK");
  427. EditorUtility.ClearProgressBar();
  428. am.havemapping = false;
  429. return;
  430. }
  431. am.negx = negx;
  432. am.negz = negz;
  433. am.flipyz = flipyz;
  434. am.scl = scl;
  435. }
  436. else
  437. {
  438. negx = am.negx;
  439. negz = am.negz;
  440. flipyz = am.flipyz;
  441. scl = am.scl;
  442. }
  443. am.havemapping = true;
  444. // Remap vertices
  445. for ( int i = 0; i < am.Verts.Length; i++ )
  446. {
  447. for ( int v = 0; v < am.Verts[i].points.Length; v++ )
  448. {
  449. if ( negx )
  450. am.Verts[i].points[v].x = -am.Verts[i].points[v].x;
  451. if ( flipyz )
  452. {
  453. float z = am.Verts[i].points[v].z;
  454. am.Verts[i].points[v].z = am.Verts[i].points[v].y;
  455. am.Verts[i].points[v].y = z;
  456. }
  457. if ( negz )
  458. am.Verts[i].points[v].z = -am.Verts[i].points[v].z;
  459. am.Verts[i].points[v] = am.Verts[i].points[v] * scl;
  460. }
  461. }
  462. if ( domapping )
  463. {
  464. for ( int i = 0; i < am.Verts.Length; i++ )
  465. {
  466. am.Verts[i].indices = FindVerts(am.Verts[i].points[findex]);
  467. if ( am.Verts[i].indices.Length == 0 )
  468. {
  469. EditorUtility.DisplayDialog("Final Mapping Failed!", "Mapping of " + System.IO.Path.GetFileNameWithoutExtension(filename) + " failed!", "OK");
  470. EditorUtility.ClearProgressBar();
  471. return;
  472. }
  473. }
  474. }
  475. else
  476. {
  477. for ( int i = 0; i < am.Verts.Length; i++ )
  478. {
  479. am.Verts[i].indices = oldverts[i].indices;
  480. }
  481. }
  482. oldverts = null;
  483. }
  484. public void LoadFile(string filename)
  485. {
  486. string ext = System.IO.Path.GetExtension(filename);
  487. ext = ext.ToLower();
  488. switch ( ext )
  489. {
  490. case ".pc2":
  491. LoadPC2(filename);
  492. break;
  493. case ".mdd":
  494. LoadMDD(filename);
  495. break;
  496. case ".mc":
  497. LoadMCC(filename);
  498. break;
  499. }
  500. }
  501. void LoadPC2()
  502. {
  503. //MegaPointCache am = (MegaPointCache)target;
  504. //mods = am.gameObject.GetComponent<MegaModifiers>();
  505. string filename = EditorUtility.OpenFilePanel("Point Cache File", lastpath, "pc2");
  506. if ( filename == null || filename.Length < 1 )
  507. return;
  508. LoadPC2(filename);
  509. }
  510. public void LoadPC2(string filename)
  511. {
  512. MegaPointCache am = (MegaPointCache)target;
  513. oldverts = am.Verts;
  514. mods = am.gameObject.GetComponent<MegaModifiers>();
  515. if ( mods == null )
  516. {
  517. Debug.LogWarning("You need to add a Mega Modify Object component first!");
  518. return;
  519. }
  520. lastpath = filename;
  521. // Clear what we have
  522. Verts.Clear();
  523. FileStream fs = new FileStream(filename, FileMode.Open, FileAccess.Read, System.IO.FileShare.Read);
  524. long len = fs.Length;
  525. BinaryReader br = new BinaryReader(fs);
  526. string sig = MegaParse.ReadStr(br);
  527. if ( sig != "POINTCACHE2" )
  528. {
  529. EditorUtility.DisplayDialog("PC2 Importer", "The selected file does not appear to be a valid PC2 File", "Ok");
  530. br.Close();
  531. return;
  532. }
  533. int fileVersion = br.ReadInt32();
  534. if ( fileVersion != 1 )
  535. {
  536. br.Close();
  537. return;
  538. }
  539. int numPoints = br.ReadInt32();
  540. br.ReadSingle();
  541. br.ReadSingle();
  542. int numSamples = br.ReadInt32();
  543. long csamples = (len - 24) / (numPoints * 12);
  544. numSamples = (int)csamples;
  545. am.Verts = new MegaPCVert[numPoints];
  546. for ( int i = 0; i < am.Verts.Length; i++ )
  547. {
  548. am.Verts[i] = new MegaPCVert();
  549. am.Verts[i].points = new Vector3[numSamples];
  550. }
  551. for ( int i = 0; i < numSamples; i++ )
  552. {
  553. for ( int v = 0; v < numPoints; v++ )
  554. am.Verts[v].points[i] = MegaParse.ReadP3(br);
  555. }
  556. BuildData(mods, am, filename);
  557. br.Close();
  558. }
  559. // Utils methods
  560. int[] FindVerts(Vector3 p)
  561. {
  562. List<int> indices = new List<int>();
  563. for ( int i = 0; i < mods.verts.Length; i++ )
  564. {
  565. //float dist = Vector3.Distance(p, mods.verts[i]);
  566. float dist = Vector3.SqrMagnitude(p - mods.verts[i]);
  567. if ( dist < tolerance ) //0.0001f ) //mods.verts[i].Equals(p) )
  568. indices.Add(i);
  569. }
  570. return indices.ToArray();
  571. }
  572. Vector3 Extents(Vector3[] verts, out Vector3 min, out Vector3 max)
  573. {
  574. Vector3 extent = Vector3.zero;
  575. min = Vector3.zero;
  576. max = Vector3.zero;
  577. if ( verts != null && verts.Length > 0 )
  578. {
  579. min = verts[0];
  580. max = verts[0];
  581. for ( int i = 1; i < verts.Length; i++ )
  582. {
  583. if ( verts[i].x < min.x ) min.x = verts[i].x;
  584. if ( verts[i].y < min.y ) min.y = verts[i].y;
  585. if ( verts[i].z < min.z ) min.z = verts[i].z;
  586. if ( verts[i].x > max.x ) max.x = verts[i].x;
  587. if ( verts[i].y > max.y ) max.y = verts[i].y;
  588. if ( verts[i].z > max.z ) max.z = verts[i].z;
  589. }
  590. extent = max - min;
  591. }
  592. return extent;
  593. }
  594. Vector3 Extents(MegaPCVert[] verts, out Vector3 min, out Vector3 max)
  595. {
  596. Vector3 extent = Vector3.zero;
  597. min = Vector3.zero;
  598. max = Vector3.zero;
  599. if ( verts != null && verts.Length > 0 )
  600. {
  601. min = verts[0].points[0];
  602. max = verts[0].points[0];
  603. for ( int i = 1; i < verts.Length; i++ )
  604. {
  605. Vector3 p = verts[i].points[0];
  606. if ( p.x < min.x ) min.x = p.x;
  607. if ( p.y < min.y ) min.y = p.y;
  608. if ( p.z < min.z ) min.z = p.z;
  609. if ( p.x > max.x ) max.x = p.x;
  610. if ( p.y > max.y ) max.y = p.y;
  611. if ( p.z > max.z ) max.z = p.z;
  612. }
  613. extent = max - min;
  614. }
  615. return extent;
  616. }
  617. bool showadvanced = false;
  618. public override void OnInspectorGUI()
  619. {
  620. MegaPointCache am = (MegaPointCache)target;
  621. DoAdjustFirst = EditorGUILayout.Toggle("Mapping Adjust", DoAdjustFirst);
  622. uselastframe = EditorGUILayout.Toggle("Use Last Frame", uselastframe);
  623. EditorGUILayout.BeginHorizontal();
  624. if ( GUILayout.Button("Import PC2") )
  625. {
  626. LoadPC2();
  627. EditorUtility.SetDirty(target);
  628. }
  629. if ( GUILayout.Button("Import MDD") )
  630. {
  631. LoadMDD();
  632. EditorUtility.SetDirty(target);
  633. }
  634. if ( GUILayout.Button("Import MC") )
  635. {
  636. LoadMCC();
  637. EditorUtility.SetDirty(target);
  638. }
  639. EditorGUILayout.EndHorizontal();
  640. // Basic mod stuff
  641. showmodparams = EditorGUILayout.Foldout(showmodparams, "Modifier Common Params");
  642. if ( showmodparams )
  643. CommonModParamsBasic(am);
  644. // Advanced
  645. showadvanced = EditorGUILayout.Foldout(showadvanced, "Advanced Params");
  646. if ( showadvanced )
  647. {
  648. tolerance = EditorGUILayout.FloatField("Map Tolerance", tolerance * 100.0f) / 100.0f;
  649. if ( am.Verts != null && am.Verts.Length > 0 ) //[0] != null )
  650. {
  651. am.showmapping = EditorGUILayout.BeginToggleGroup("Show Mapping", am.showmapping);
  652. am.mapStart = EditorGUILayout.IntSlider("StartVert", am.mapStart, 0, am.Verts.Length);
  653. am.mapEnd = EditorGUILayout.IntSlider("endVert", am.mapEnd, 0, am.Verts.Length);
  654. am.mappingSize = EditorGUILayout.Slider("Size", am.mappingSize, 0.0005f, 10.01f);
  655. EditorGUILayout.EndToggleGroup();
  656. }
  657. //morph.tolerance = EditorGUILayout.Slider("Tolerance", morph.tolerance, 0.0f, 0.01f);
  658. }
  659. am.time = EditorGUILayout.FloatField("Time", am.time);
  660. am.maxtime = EditorGUILayout.FloatField("Loop Time", am.maxtime);
  661. am.animated = EditorGUILayout.Toggle("Animated", am.animated);
  662. am.framedelay = EditorGUILayout.Toggle("Frame Delay", am.framedelay);
  663. am.ignoreFirst = EditorGUILayout.Toggle("Ignore First Frame", am.ignoreFirst);
  664. am.speed = EditorGUILayout.FloatField("Speed", am.speed);
  665. am.LoopMode = (MegaRepeatMode)EditorGUILayout.EnumPopup("Loop Mode", am.LoopMode);
  666. am.interpMethod = (MegaInterpMethod)EditorGUILayout.EnumPopup("Interp Method", am.interpMethod);
  667. am.blendMode = (MegaBlendAnimMode)EditorGUILayout.EnumPopup("Blend Mode", am.blendMode);
  668. if ( am.blendMode == MegaBlendAnimMode.Additive )
  669. am.weight = EditorGUILayout.FloatField("Weight", am.weight);
  670. if ( am.Verts != null && am.Verts.Length > 0 )
  671. {
  672. int mem = am.Verts.Length * am.Verts[0].points.Length * 12;
  673. EditorGUILayout.LabelField("Memory: ", (mem / 1024) + "KB");
  674. }
  675. if ( GUI.changed )
  676. EditorUtility.SetDirty(target);
  677. }
  678. // From here down could move to util class
  679. int FindVert(Vector3 vert, Vector3[] tverts, float tolerance, float scl, bool flipyz, bool negx, bool negz, int vn)
  680. {
  681. int find = 0;
  682. if ( negx )
  683. vert.x = -vert.x;
  684. if ( flipyz )
  685. {
  686. float z = vert.z;
  687. vert.z = vert.y;
  688. vert.y = z;
  689. }
  690. if ( negz )
  691. vert.z = -vert.z;
  692. vert /= scl;
  693. float closest = Vector3.SqrMagnitude(tverts[0] - vert);
  694. for ( int i = 0; i < tverts.Length; i++ )
  695. {
  696. float dif = Vector3.SqrMagnitude(tverts[i] - vert);
  697. if ( dif < closest )
  698. {
  699. closest = dif;
  700. find = i;
  701. }
  702. }
  703. if ( closest > tolerance ) //0.0001f ) // not exact
  704. {
  705. return -1;
  706. }
  707. return find; //0;
  708. }
  709. bool DoMapping(Vector3[] verts, Vector3[] tverts, float scale, bool flipyz, bool negx, bool negz)
  710. {
  711. int count = 400;
  712. for ( int i = 0; i < verts.Length; i++ )
  713. {
  714. float a = (float)i / (float)verts.Length;
  715. count--;
  716. if ( count < 0 )
  717. {
  718. //Debug.Log("map " + i + " vert " + verts[i].ToString("0.00000"));
  719. EditorUtility.DisplayProgressBar("Mapping", "Mapping vertex " + i, a);
  720. count = 400;
  721. }
  722. int mapping = FindVert(verts[i], tverts, tolerance, scale, flipyz, negx, negz, i);
  723. if ( mapping == -1 )
  724. {
  725. // Failed
  726. EditorUtility.ClearProgressBar();
  727. return false;
  728. }
  729. }
  730. EditorUtility.ClearProgressBar();
  731. return true;
  732. }
  733. // Out of this we need scl, negx, negz and flipyz
  734. bool TryMapping1(Vector3[] tverts, Vector3[] verts)
  735. {
  736. //for ( int i = 0; i < 8; i++ )
  737. // Debug.Log("cache vert " + tverts[i].ToString("0.00000"));
  738. //for ( int i = 0; i < 8; i++ )
  739. // Debug.Log("mesh vert " + verts[i].ToString("0.00000"));
  740. // Get extents for mod verts and for imported meshes, if not the same then scale
  741. Vector3 min1,max1;
  742. Vector3 min2,max2;
  743. Vector3 ex1 = Extents(verts, out min1, out max1);
  744. Vector3 ex2 = Extents(tverts, out min2, out max2);
  745. //Debug.Log("mesh extents " + ex1.ToString("0.00000"));
  746. //Debug.Log("cache extents " + ex2.ToString("0.00000"));
  747. //Debug.Log("mesh min " + min1.ToString("0.00000"));
  748. //Debug.Log("cache min " + min2.ToString("0.00000"));
  749. //Debug.Log("mesh max " + max1.ToString("0.00000"));
  750. //Debug.Log("cache max " + max2.ToString("0.00000"));
  751. int largest1 = MegaUtils.LargestComponent(ex1);
  752. int largest2 = MegaUtils.LargestComponent(ex2);
  753. //Debug.Log(largest1 + " " + largest2);
  754. scl = ex1[largest1] / ex2[largest2];
  755. //Debug.Log("scl " + scl.ToString("0.0000"));
  756. //Vector3 offset = verts[0] - (tverts[0] * scl);
  757. //Debug.Log("Offset " + offset.ToString("0.00000"));
  758. // need min max on all axis so we can produce an offset to add
  759. int map = 0;
  760. for ( map = 0; map < 8; map++ )
  761. {
  762. flipyz = ((map & 4) != 0);
  763. negx = ((map & 2) != 0);
  764. negz = ((map & 1) != 0);
  765. bool mapped = DoMapping(verts, tverts, scl, flipyz, negx, negz);
  766. if ( mapped )
  767. break;
  768. }
  769. if ( map == 8 ) // We couldnt find any mapping
  770. return false;
  771. //Debug.Log("scl " + scl + " negx " + negx + " negz " + negz + " flipyz " + flipyz);
  772. return true;
  773. }
  774. void AdjustVertsSimple(MegaModifiers mods, MegaPointCache am)
  775. {
  776. if ( am.Verts != null )
  777. {
  778. Vector3[] baseverts = new Vector3[am.Verts.Length];
  779. for ( int i = 0; i < am.Verts.Length; i++ )
  780. baseverts[i] = am.Verts[i].points[0];
  781. for ( int i = 0; i < am.Verts.Length; i++ )
  782. {
  783. for ( int j = 0; j < am.Verts[i].points.Length; j++ )
  784. {
  785. Vector3 p = am.Verts[i].points[j] * am.adjustscl;
  786. am.Verts[i].points[j] = p - am.adjustoff;
  787. }
  788. }
  789. }
  790. }
  791. void AdjustVerts(MegaModifiers mods, MegaPointCache am)
  792. {
  793. if ( am.Verts != null )
  794. {
  795. Vector3[] baseverts = new Vector3[am.Verts.Length];
  796. for ( int i = 0; i < am.Verts.Length; i++ )
  797. baseverts[i] = am.Verts[i].points[0];
  798. Vector3 min1,max1;
  799. Vector3 min2,max2;
  800. Vector3 ex1 = Extents(mods.verts, out min1, out max1);
  801. Vector3 ex2 = Extents(baseverts, out min2, out max2);
  802. //Debug.Log("mesh extents " + ex1.ToString("0.00000"));
  803. //Debug.Log("cache extents " + ex2.ToString("0.00000"));
  804. //Debug.Log("mesh min " + min1.ToString("0.00000"));
  805. //Debug.Log("cache min " + min2.ToString("0.00000"));
  806. //Debug.Log("mesh max " + max1.ToString("0.00000"));
  807. //Debug.Log("cache max " + max2.ToString("0.00000"));
  808. int largest1 = MegaUtils.LargestComponent(ex1);
  809. int largest2 = MegaUtils.LargestComponent(ex2);
  810. //Debug.Log(largest1 + " " + largest2);
  811. am.adjustscl = ex1[largest1] / ex2[largest2];
  812. //Debug.Log("scl " + scl1.ToString("0.0000"));
  813. //off = verts[0] - (tverts[0] * scl);
  814. am.adjustoff = (min2 * am.adjustscl) - min1;
  815. //Debug.Log("scl " + am.adjustscl);
  816. //Debug.Log("off " + am.adjustoff);
  817. for ( int i = 0; i < am.Verts.Length; i++ )
  818. {
  819. for ( int j = 0; j < am.Verts[i].points.Length; j++ )
  820. {
  821. Vector3 p = am.Verts[i].points[j] * am.adjustscl;
  822. am.Verts[i].points[j] = p - am.adjustoff;
  823. }
  824. }
  825. }
  826. }
  827. MegaModifyObject mo = null;
  828. //public void OnSceneGUI()
  829. public override void DrawSceneGUI()
  830. {
  831. MegaPointCache mod = (MegaPointCache)target;
  832. if ( mod.showmapping )
  833. {
  834. if ( mod.Verts != null && mod.Verts[0] != null )
  835. {
  836. float vsize = mod.mappingSize;
  837. float vsize1 = vsize * 0.75f;
  838. Matrix4x4 tm = mod.gameObject.transform.localToWorldMatrix;
  839. Handles.matrix = tm; //Matrix4x4.identity;
  840. Handles.color = Color.green;
  841. if ( mo == null )
  842. mo = mod.gameObject.GetComponent<MegaModifyObject>();
  843. if ( mo )
  844. {
  845. for ( int i = 0; i < mo.verts.Length; i++ )
  846. {
  847. Vector3 p = mo.verts[i];
  848. MegaHandles.DotCap(i, p, Quaternion.identity, vsize);
  849. }
  850. }
  851. if ( mod.mapEnd >= mod.Verts.Length )
  852. mod.mapEnd = mod.Verts.Length - 1;
  853. if ( mod.mapStart > mod.mapEnd )
  854. mod.mapStart = mod.mapEnd;
  855. Handles.color = Color.white; //red;
  856. int findex = 0;
  857. if ( uselastframe )
  858. {
  859. findex = mod.Verts[0].points.Length - 1;
  860. }
  861. for ( int i = mod.mapStart; i < mod.mapEnd; i++ )
  862. {
  863. Vector3 p = mod.Verts[i].points[findex];
  864. MegaHandles.DotCap(i, p, Quaternion.identity, vsize1);
  865. }
  866. Handles.matrix = Matrix4x4.identity;
  867. }
  868. }
  869. }
  870. }