MegaMorph.cs 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320
  1. using UnityEngine;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. #if false
  5. public class MegaMorphBase : MegaModifier
  6. {
  7. public List<MegaMorphChan> chanBank = new List<MegaMorphChan>();
  8. public MegaMorphAnimType animtype = MegaMorphAnimType.Bezier;
  9. public override void PostCopy(MegaModifier src)
  10. {
  11. MegaMorphBase mor = (MegaMorphBase)src;
  12. chanBank = new List<MegaMorphChan>();
  13. for ( int c = 0; c < mor.chanBank.Count; c++ )
  14. {
  15. MegaMorphChan chan = new MegaMorphChan();
  16. MegaMorphChan.Copy(mor.chanBank[c], chan);
  17. chanBank.Add(chan);
  18. }
  19. }
  20. public string[] GetChannelNames()
  21. {
  22. string[] names = new string[chanBank.Count];
  23. for ( int i = 0; i < chanBank.Count; i++ )
  24. names[i] = chanBank[i].mName;
  25. return names;
  26. }
  27. public MegaMorphChan GetChannel(string name)
  28. {
  29. for ( int i = 0; i < chanBank.Count; i++ )
  30. {
  31. if ( chanBank[i].mName == name )
  32. return chanBank[i];
  33. }
  34. return null;
  35. }
  36. public int NumChannels()
  37. {
  38. return chanBank.Count;
  39. }
  40. public void SetPercent(int i, float percent)
  41. {
  42. if ( i >= 0 && i < chanBank.Count )
  43. chanBank[i].Percent = percent;
  44. }
  45. public void SetPercentLim(int i, float alpha)
  46. {
  47. if ( i >= 0 && i < chanBank.Count )
  48. {
  49. if ( chanBank[i].mUseLimit )
  50. chanBank[i].Percent = chanBank[i].mSpinmin + ((chanBank[i].mSpinmax - chanBank[i].mSpinmin) * alpha);
  51. else
  52. chanBank[i].Percent = alpha * 100.0f;
  53. }
  54. }
  55. public void SetPercent(int i, float percent, float speed)
  56. {
  57. chanBank[i].SetTarget(percent, speed);
  58. }
  59. public void ResetPercent(int[] channels, float speed)
  60. {
  61. for ( int i = 0; i < channels.Length; i++ )
  62. {
  63. int chan = channels[i];
  64. chanBank[chan].SetTarget(0.0f, speed);
  65. }
  66. }
  67. public float GetPercent(int i)
  68. {
  69. if ( i >= 0 && i < chanBank.Count )
  70. return chanBank[i].Percent;
  71. return 0.0f;
  72. }
  73. public void SetAnim(float t)
  74. {
  75. if ( animtype == MegaMorphAnimType.Bezier )
  76. {
  77. for ( int i = 0; i < chanBank.Count; i++ )
  78. {
  79. if ( chanBank[i].control != null )
  80. {
  81. if ( chanBank[i].control.Times != null )
  82. {
  83. if ( chanBank[i].control.Times.Length > 0 )
  84. chanBank[i].Percent = chanBank[i].control.GetFloat(t); //, 0.0f, 100.0f);
  85. }
  86. }
  87. }
  88. }
  89. else
  90. {
  91. for ( int i = 0; i < chanBank.Count; i++ )
  92. {
  93. if ( chanBank[i].control != null )
  94. {
  95. if ( chanBank[i].control.Times != null )
  96. {
  97. if ( chanBank[i].control.Times.Length > 0 )
  98. chanBank[i].Percent = chanBank[i].control.GetHermiteFloat(t); //, 0.0f, 100.0f);
  99. }
  100. }
  101. }
  102. }
  103. }
  104. [System.Serializable]
  105. public class MegaMorphBlend
  106. {
  107. public float t;
  108. public float weight;
  109. }
  110. public int numblends;
  111. public List<MegaMorphBlend> blends; // = new List<MegaMorphBlend>();
  112. public void SetAnimBlend(float t, float weight)
  113. {
  114. if ( blends == null )
  115. {
  116. blends = new List<MegaMorphBlend>();
  117. for ( int i = 0; i < 4; i++ )
  118. {
  119. blends.Add(new MegaMorphBlend());
  120. }
  121. }
  122. blends[numblends].t = t;
  123. blends[numblends].weight = weight;
  124. numblends++;
  125. }
  126. public void ClearBlends()
  127. {
  128. numblends = 0;
  129. }
  130. public void SetChannels()
  131. {
  132. float tweight = 0.0f;
  133. for ( int i = 0; i < numblends; i++ )
  134. {
  135. tweight += blends[i].weight;
  136. }
  137. for ( int b = 0; b < numblends; b++ )
  138. {
  139. for ( int c = 0; c < chanBank.Count; c++ )
  140. {
  141. if ( animtype == MegaMorphAnimType.Bezier )
  142. {
  143. if ( chanBank[c].control != null )
  144. {
  145. if ( chanBank[c].control.Times != null )
  146. {
  147. if ( chanBank[c].control.Times.Length > 0 )
  148. {
  149. if ( b == 0 )
  150. chanBank[c].Percent = chanBank[c].control.GetFloat(blends[b].t) * (blends[b].weight / tweight); //, 0.0f, 100.0f);
  151. else
  152. chanBank[c].Percent += chanBank[c].control.GetFloat(blends[b].t) * (blends[b].weight / tweight); //, 0.0f, 100.0f);
  153. }
  154. }
  155. }
  156. }
  157. else
  158. {
  159. if ( chanBank[c].control != null )
  160. {
  161. if ( chanBank[c].control.Times != null )
  162. {
  163. if ( chanBank[c].control.Times.Length > 0 )
  164. {
  165. if ( b == 0 )
  166. chanBank[c].Percent = chanBank[c].control.GetHermiteFloat(blends[b].t) * (blends[b].weight / tweight); //, 0.0f, 100.0f);
  167. else
  168. chanBank[c].Percent += chanBank[c].control.GetHermiteFloat(blends[b].t) * (blends[b].weight / tweight); //, 0.0f, 100.0f);
  169. }
  170. }
  171. }
  172. }
  173. }
  174. }
  175. }
  176. }
  177. #endif
  178. public enum MegaMorphAnimType
  179. {
  180. Bezier,
  181. Hermite,
  182. }
  183. [AddComponentMenu("Modifiers/Morph")]
  184. public class MegaMorph : MegaMorphBase
  185. {
  186. public bool UseLimit;
  187. public float Max;
  188. public float Min;
  189. public Vector3[] oPoints;
  190. public int[] mapping;
  191. public float importScale = 1.0f;
  192. public bool flipyz = false;
  193. public bool negx = false;
  194. [HideInInspector]
  195. public float tolerance = 0.0001f;
  196. public bool showmapping = false;
  197. public float mappingSize = 0.001f;
  198. public int mapStart = 0;
  199. public int mapEnd = 0;
  200. public Vector3[] dif; // changed to public, check it doesnt break anything
  201. static Vector3[] endpoint = new Vector3[4];
  202. static Vector3[] splinepoint = new Vector3[4];
  203. static Vector3[] temppoint = new Vector3[2];
  204. Vector3[] p1;
  205. Vector3[] p2;
  206. Vector3[] p3;
  207. Vector3[] p4;
  208. public List<float> pers = new List<float>(4);
  209. public override string ModName() { return "Morph"; }
  210. public override string GetHelpURL() { return "?page_id=257"; }
  211. [HideInInspector]
  212. public int compressedmem = 0;
  213. [HideInInspector]
  214. public int compressedmem1 = 0;
  215. [HideInInspector]
  216. public int memuse = 0;
  217. // This should be a MegaModifiers method, so on Start/Awake run through and regrab all verts
  218. // and then for each modifier call a PS3Remap method
  219. // Actually we could leave systems in place and have an end method that build a PS3 mesh, this would mean
  220. // we get the overhead of building a new vertex array but should be more than offset by not modifying dup
  221. // verts
  222. // So at start we use current verts and then remap against freshly fetched vert data
  223. public void PS3Remap()
  224. {
  225. }
  226. public override bool ModLateUpdate(MegaModContext mc)
  227. {
  228. if ( animate )
  229. {
  230. if ( Application.isPlaying )
  231. animtime += Time.deltaTime * speed;
  232. switch ( repeatMode )
  233. {
  234. case MegaRepeatMode.Loop: animtime = Mathf.Repeat(animtime, looptime); break;
  235. //case RepeatMode.PingPong: animtime = Mathf.PingPong(animtime, looptime); break;
  236. case MegaRepeatMode.Clamp: animtime = Mathf.Clamp(animtime, 0.0f, looptime); break;
  237. }
  238. //animtime = Mathf.Repeat(animtime, looptime);
  239. SetAnim(animtime);
  240. }
  241. if ( dif == null )
  242. {
  243. dif = new Vector3[mc.mod.verts.Length];
  244. }
  245. return Prepare(mc);
  246. }
  247. public bool animate = false;
  248. public float atime = 0.0f;
  249. public float animtime = 0.0f;
  250. public float looptime = 0.0f;
  251. public MegaRepeatMode repeatMode = MegaRepeatMode.Loop;
  252. public float speed = 1.0f;
  253. public override bool Prepare(MegaModContext mc)
  254. {
  255. if ( chanBank != null && chanBank.Count > 0 )
  256. return true;
  257. return false;
  258. }
  259. // Find the closest and not use a threshold, use sqr distance
  260. int FindVert(Vector3 vert)
  261. {
  262. float closest = Vector3.SqrMagnitude(oPoints[0] - vert);
  263. int find = 0;
  264. for ( int i = 0; i < oPoints.Length; i++ )
  265. {
  266. float dif = Vector3.SqrMagnitude(oPoints[i] - vert);
  267. if ( dif < closest )
  268. {
  269. closest = dif;
  270. find = i;
  271. }
  272. }
  273. return find; //0;
  274. }
  275. void DoMapping(Mesh mesh)
  276. {
  277. mapping = new int[mesh.vertexCount];
  278. for ( int v = 0; v < mesh.vertexCount; v++ )
  279. {
  280. Vector3 vert = mesh.vertices[v];
  281. vert.x = -vert.x;
  282. mapping[v] = FindVert(vert);
  283. }
  284. }
  285. public void DoMapping(Vector3[] verts)
  286. {
  287. mapping = new int[verts.Length];
  288. for ( int v = 0; v < verts.Length; v++ )
  289. {
  290. mapping[v] = FindVert(verts[v]);
  291. }
  292. }
  293. // Only need to call if a Percent value has changed on a Channel or a target, so flag for a change
  294. void SetVerts(int j, Vector3[] p)
  295. {
  296. switch ( j )
  297. {
  298. case 0: p1 = p; break;
  299. case 1: p2 = p; break;
  300. case 2: p3 = p; break;
  301. case 3: p4 = p; break;
  302. }
  303. }
  304. void SetVerts(MegaMorphChan chan, int j, Vector3[] p)
  305. {
  306. switch ( j )
  307. {
  308. case 0: chan.p1 = p; break;
  309. case 1: chan.p2 = p; break;
  310. case 2: chan.p3 = p; break;
  311. case 3: chan.p4 = p; break;
  312. }
  313. }
  314. // Seperate function for compressed data, when we compress we should be able to tell if its worth it
  315. // as we will need to dup opoints etc
  316. static int framenum;
  317. // oPoints whould be verts
  318. public override void Modify(MegaModifiers mc)
  319. {
  320. if ( nonMorphedVerts != null && nonMorphedVerts.Length > 1 )
  321. {
  322. ModifyCompressed(mc);
  323. return;
  324. }
  325. framenum++;
  326. mc.ChangeSourceVerts();
  327. float fChannelPercent;
  328. Vector3 delt;
  329. // cycle through channels, searching for ones to use
  330. bool firstchan = true;
  331. bool morphed = false;
  332. float min = 0.0f;
  333. float max = 100.0f;
  334. if ( UseLimit )
  335. {
  336. min = Min;
  337. max = Max;
  338. }
  339. for ( int i = 0; i < chanBank.Count; i++ )
  340. {
  341. MegaMorphChan chan = chanBank[i];
  342. chan.UpdatePercent();
  343. if ( UseLimit )
  344. fChannelPercent = Mathf.Clamp(chan.Percent, min, max); //chan.mSpinmin, chan.mSpinmax);
  345. else
  346. {
  347. if ( chan.mUseLimit )
  348. fChannelPercent = Mathf.Clamp(chan.Percent, chan.mSpinmin, chan.mSpinmax);
  349. else
  350. fChannelPercent = Mathf.Clamp(chan.Percent, 0.0f, 100.0f);
  351. }
  352. //fChannelPercent *= chan.weight;
  353. if ( fChannelPercent != 0.0f || (fChannelPercent == 0.0f && chan.fChannelPercent != 0.0f) )
  354. {
  355. chan.fChannelPercent = fChannelPercent;
  356. if ( chan.mTargetCache != null && chan.mTargetCache.Count > 0 && chan.mActiveOverride ) //&& fChannelPercent != 0.0f )
  357. {
  358. morphed = true;
  359. if ( chan.mUseLimit ) //|| glUseLimit )
  360. {
  361. }
  362. if ( firstchan )
  363. {
  364. firstchan = false;
  365. for ( int pointnum = 0; pointnum < oPoints.Length; pointnum++ )
  366. dif[pointnum] = oPoints[pointnum];
  367. }
  368. if ( chan.mTargetCache.Count == 1 )
  369. {
  370. for ( int pointnum = 0; pointnum < oPoints.Length; pointnum++ )
  371. {
  372. delt = chan.mDeltas[pointnum];
  373. dif[pointnum].x += delt.x * fChannelPercent;
  374. dif[pointnum].y += delt.y * fChannelPercent;
  375. dif[pointnum].z += delt.z * fChannelPercent;
  376. }
  377. }
  378. else
  379. {
  380. int totaltargs = chan.mTargetCache.Count; // + 1; // + 1;
  381. float fProgression = fChannelPercent; //Mathf.Clamp(fChannelPercent, 0.0f, 100.0f);
  382. int segment = 1;
  383. while ( segment <= totaltargs && fProgression >= chan.GetTargetPercent(segment - 2) )
  384. segment++;
  385. if ( segment > totaltargs )
  386. segment = totaltargs;
  387. p4 = oPoints;
  388. if ( segment == 1 )
  389. {
  390. p1 = oPoints;
  391. p2 = chan.mTargetCache[0].points; // mpoints
  392. p3 = chan.mTargetCache[1].points;
  393. }
  394. else
  395. {
  396. if ( segment == totaltargs )
  397. {
  398. int targnum = totaltargs - 1;
  399. for ( int j = 2; j >= 0; j-- )
  400. {
  401. targnum--;
  402. if ( targnum == -2 )
  403. SetVerts(j, oPoints);
  404. else
  405. SetVerts(j, chan.mTargetCache[targnum + 1].points);
  406. }
  407. }
  408. else
  409. {
  410. int targnum = segment;
  411. for ( int j = 3; j >= 0; j-- )
  412. {
  413. targnum--;
  414. if ( targnum == -2 )
  415. SetVerts(j, oPoints);
  416. else
  417. SetVerts(j, chan.mTargetCache[targnum + 1].points);
  418. }
  419. }
  420. }
  421. float targetpercent1 = chan.GetTargetPercent(segment - 3);
  422. float targetpercent2 = chan.GetTargetPercent(segment - 2);
  423. float top = fProgression - targetpercent1;
  424. float bottom = targetpercent2 - targetpercent1;
  425. float u = top / bottom;
  426. {
  427. for ( int pointnum = 0; pointnum < oPoints.Length; pointnum++ )
  428. {
  429. Vector3 vert = oPoints[pointnum];
  430. float length;
  431. Vector3 progession;
  432. endpoint[0] = p1[pointnum];
  433. endpoint[1] = p2[pointnum];
  434. endpoint[2] = p3[pointnum];
  435. endpoint[3] = p4[pointnum];
  436. if ( segment == 1 )
  437. {
  438. splinepoint[0] = endpoint[0];
  439. splinepoint[3] = endpoint[1];
  440. temppoint[1] = endpoint[2] - endpoint[0];
  441. temppoint[0] = endpoint[1] - endpoint[0];
  442. length = temppoint[1].sqrMagnitude;
  443. if ( length == 0.0f )
  444. {
  445. splinepoint[1] = endpoint[0];
  446. splinepoint[2] = endpoint[1];
  447. }
  448. else
  449. {
  450. splinepoint[2] = endpoint[1] - (Vector3.Dot(temppoint[0], temppoint[1]) * chan.mCurvature / length) * temppoint[1];
  451. splinepoint[1] = endpoint[0] + chan.mCurvature * (splinepoint[2] - endpoint[0]);
  452. }
  453. }
  454. else
  455. {
  456. if ( segment == totaltargs )
  457. {
  458. splinepoint[0] = endpoint[1];
  459. splinepoint[3] = endpoint[2];
  460. temppoint[1] = endpoint[2] - endpoint[0];
  461. temppoint[0] = endpoint[1] - endpoint[2];
  462. length = temppoint[1].sqrMagnitude;
  463. if ( length == 0.0f )
  464. {
  465. splinepoint[1] = endpoint[0];
  466. splinepoint[2] = endpoint[1];
  467. }
  468. else
  469. {
  470. splinepoint[1] = endpoint[1] - (Vector3.Dot(temppoint[1], temppoint[0]) * chan.mCurvature / length) * temppoint[1];
  471. splinepoint[2] = endpoint[2] + chan.mCurvature * (splinepoint[1] - endpoint[2]);
  472. }
  473. }
  474. else
  475. {
  476. temppoint[1] = endpoint[2] - endpoint[0];
  477. temppoint[0] = endpoint[1] - endpoint[0];
  478. length = temppoint[1].sqrMagnitude;
  479. splinepoint[0] = endpoint[1];
  480. splinepoint[3] = endpoint[2];
  481. if ( length == 0.0f )
  482. splinepoint[1] = endpoint[0];
  483. else
  484. splinepoint[1] = endpoint[1] + (Vector3.Dot(temppoint[0], temppoint[1]) * chan.mCurvature / length) * temppoint[1];
  485. temppoint[1] = endpoint[3] - endpoint[1];
  486. temppoint[0] = endpoint[2] - endpoint[1];
  487. length = temppoint[1].sqrMagnitude;
  488. if ( length == 0.0f )
  489. splinepoint[2] = endpoint[1];
  490. else
  491. splinepoint[2] = endpoint[2] - (Vector3.Dot(temppoint[0], temppoint[1]) * chan.mCurvature / length) * temppoint[1];
  492. }
  493. }
  494. MegaUtils.Bez3D(out progession, ref splinepoint, u);
  495. dif[pointnum].x += (progession.x - vert.x) * chan.weight; //delt;
  496. dif[pointnum].y += (progession.y - vert.y) * chan.weight; //delt;
  497. dif[pointnum].z += (progession.z - vert.z) * chan.weight; //delt;
  498. }
  499. }
  500. }
  501. }
  502. }
  503. }
  504. if ( morphed )
  505. {
  506. for ( int i = 0; i < mapping.Length; i++ )
  507. sverts[i] = dif[mapping[i]];
  508. }
  509. else
  510. {
  511. for ( int i = 0; i < verts.Length; i++ )
  512. sverts[i] = verts[i];
  513. }
  514. }
  515. bool Changed(int v, int c)
  516. {
  517. for ( int t = 0; t < chanBank[c].mTargetCache.Count; t++ )
  518. {
  519. if ( !oPoints[v].Equals(chanBank[c].mTargetCache[t].points[v]) )
  520. return true;
  521. }
  522. return false;
  523. }
  524. // Move compression to editor script so not included in releases
  525. // Option for compression, per channel or per morph, will effect multicore support
  526. // morph compression means simple mt split
  527. // TODO: have a threshold for differences
  528. // Do we compress per channel or whole mesh, channel would be best
  529. // Can only compress once as we are destroying data, so if vert counts dont match dont recompress
  530. public void Compress()
  531. {
  532. if ( oPoints != null )
  533. {
  534. List<int> altered = new List<int>();
  535. int count = 0;
  536. for ( int c = 0; c < chanBank.Count; c++ )
  537. {
  538. altered.Clear();
  539. for ( int v = 0; v < oPoints.Length; v++ )
  540. {
  541. if ( Changed(v, c) )
  542. altered.Add(v);
  543. }
  544. count += altered.Count;
  545. }
  546. Debug.Log("Compressed will only morph " + count + " points instead of " + (oPoints.Length * chanBank.Count));
  547. compressedmem = count * 12;
  548. }
  549. }
  550. public void ModifyCompressed(MegaModifiers mc)
  551. {
  552. framenum++;
  553. mc.ChangeSourceVerts();
  554. float fChannelPercent;
  555. Vector3 delt;
  556. // cycle through channels, searching for ones to use
  557. bool firstchan = true;
  558. bool morphed = false;
  559. for ( int i = 0; i < chanBank.Count; i++ )
  560. {
  561. MegaMorphChan chan = chanBank[i];
  562. chan.UpdatePercent();
  563. if ( chan.mUseLimit )
  564. fChannelPercent = Mathf.Clamp(chan.Percent, chan.mSpinmin, chan.mSpinmax);
  565. else
  566. fChannelPercent = Mathf.Clamp(chan.Percent, 0.0f, 100.0f);
  567. if ( fChannelPercent != 0.0f || (fChannelPercent == 0.0f && chan.fChannelPercent != 0.0f) )
  568. {
  569. chan.fChannelPercent = fChannelPercent;
  570. if ( chan.mTargetCache != null && chan.mTargetCache.Count > 0 && chan.mActiveOverride ) //&& fChannelPercent != 0.0f )
  571. {
  572. morphed = true;
  573. if ( chan.mUseLimit ) //|| glUseLimit )
  574. {
  575. }
  576. // New bit
  577. if ( firstchan )
  578. {
  579. firstchan = false;
  580. // Save a int array of morphedpoints and use that, then only dealing with changed info
  581. for ( int pointnum = 0; pointnum < morphedVerts.Length; pointnum++ )
  582. {
  583. // this will change when we remove points
  584. int p = morphedVerts[pointnum];
  585. dif[p] = oPoints[p]; //morphedVerts[pointnum]];
  586. }
  587. }
  588. // end new
  589. if ( chan.mTargetCache.Count == 1 )
  590. {
  591. // Save a int array of morphedpoints and use that, then only dealing with changed info
  592. for ( int pointnum = 0; pointnum < morphedVerts.Length; pointnum++ )
  593. {
  594. int p = morphedVerts[pointnum];
  595. delt = chan.mDeltas[p]; //morphedVerts[pointnum]];
  596. //delt = chan.mDeltas[pointnum]; //morphedVerts[pointnum]];
  597. dif[p].x += delt.x * fChannelPercent;
  598. dif[p].y += delt.y * fChannelPercent;
  599. dif[p].z += delt.z * fChannelPercent;
  600. }
  601. }
  602. else
  603. {
  604. int totaltargs = chan.mTargetCache.Count; // + 1; // + 1;
  605. float fProgression = fChannelPercent; //Mathf.Clamp(fChannelPercent, 0.0f, 100.0f);
  606. int segment = 1;
  607. while ( segment <= totaltargs && fProgression >= chan.GetTargetPercent(segment - 2) )
  608. segment++;
  609. if ( segment > totaltargs )
  610. segment = totaltargs;
  611. p4 = oPoints;
  612. if ( segment == 1 )
  613. {
  614. p1 = oPoints;
  615. p2 = chan.mTargetCache[0].points; // mpoints
  616. p3 = chan.mTargetCache[1].points;
  617. }
  618. else
  619. {
  620. if ( segment == totaltargs )
  621. {
  622. int targnum = totaltargs - 1;
  623. for ( int j = 2; j >= 0; j-- )
  624. {
  625. targnum--;
  626. if ( targnum == -2 )
  627. SetVerts(j, oPoints);
  628. else
  629. SetVerts(j, chan.mTargetCache[targnum + 1].points);
  630. }
  631. }
  632. else
  633. {
  634. int targnum = segment;
  635. for ( int j = 3; j >= 0; j-- )
  636. {
  637. targnum--;
  638. if ( targnum == -2 )
  639. SetVerts(j, oPoints);
  640. else
  641. SetVerts(j, chan.mTargetCache[targnum + 1].points);
  642. }
  643. }
  644. }
  645. float targetpercent1 = chan.GetTargetPercent(segment - 3);
  646. float targetpercent2 = chan.GetTargetPercent(segment - 2);
  647. float top = fProgression - targetpercent1;
  648. float bottom = targetpercent2 - targetpercent1;
  649. float u = top / bottom;
  650. for ( int pointnum = 0; pointnum < morphedVerts.Length; pointnum++ )
  651. {
  652. int p = morphedVerts[pointnum];
  653. Vector3 vert = oPoints[p]; //pointnum];
  654. float length;
  655. Vector3 progession;
  656. endpoint[0] = p1[p];
  657. endpoint[1] = p2[p];
  658. endpoint[2] = p3[p];
  659. endpoint[3] = p4[p];
  660. if ( segment == 1 )
  661. {
  662. splinepoint[0] = endpoint[0];
  663. splinepoint[3] = endpoint[1];
  664. temppoint[1] = endpoint[2] - endpoint[0];
  665. temppoint[0] = endpoint[1] - endpoint[0];
  666. length = temppoint[1].sqrMagnitude;
  667. if ( length == 0.0f )
  668. {
  669. splinepoint[1] = endpoint[0];
  670. splinepoint[2] = endpoint[1];
  671. }
  672. else
  673. {
  674. splinepoint[2] = endpoint[1] - (Vector3.Dot(temppoint[0], temppoint[1]) * chan.mCurvature / length) * temppoint[1];
  675. splinepoint[1] = endpoint[0] + chan.mCurvature * (splinepoint[2] - endpoint[0]);
  676. }
  677. }
  678. else
  679. {
  680. if ( segment == totaltargs )
  681. {
  682. splinepoint[0] = endpoint[1];
  683. splinepoint[3] = endpoint[2];
  684. temppoint[1] = endpoint[2] - endpoint[0];
  685. temppoint[0] = endpoint[1] - endpoint[2];
  686. length = temppoint[1].sqrMagnitude;
  687. if ( length == 0.0f )
  688. {
  689. splinepoint[1] = endpoint[0];
  690. splinepoint[2] = endpoint[1];
  691. }
  692. else
  693. {
  694. splinepoint[1] = endpoint[1] - (Vector3.Dot(temppoint[1], temppoint[0]) * chan.mCurvature / length) * temppoint[1];
  695. splinepoint[2] = endpoint[2] + chan.mCurvature * (splinepoint[1] - endpoint[2]);
  696. }
  697. }
  698. else
  699. {
  700. temppoint[1] = endpoint[2] - endpoint[0];
  701. temppoint[0] = endpoint[1] - endpoint[0];
  702. length = temppoint[1].sqrMagnitude;
  703. splinepoint[0] = endpoint[1];
  704. splinepoint[3] = endpoint[2];
  705. if ( length == 0.0f )
  706. splinepoint[1] = endpoint[0];
  707. else
  708. splinepoint[1] = endpoint[1] + (Vector3.Dot(temppoint[0], temppoint[1]) * chan.mCurvature / length) * temppoint[1];
  709. temppoint[1] = endpoint[3] - endpoint[1];
  710. temppoint[0] = endpoint[2] - endpoint[1];
  711. length = temppoint[1].sqrMagnitude;
  712. if ( length == 0.0f )
  713. splinepoint[2] = endpoint[1];
  714. else
  715. splinepoint[2] = endpoint[2] - (Vector3.Dot(temppoint[0], temppoint[1]) * chan.mCurvature / length) * temppoint[1];
  716. }
  717. }
  718. MegaUtils.Bez3D(out progession, ref splinepoint, u);
  719. dif[p].x += progession.x - vert.x;
  720. dif[p].y += progession.y - vert.y;
  721. dif[p].z += progession.z - vert.z;
  722. }
  723. }
  724. }
  725. }
  726. }
  727. if ( morphed )
  728. {
  729. for ( int i = 0; i < morphMappingFrom.Length; i++ )
  730. sverts[morphMappingTo[i]] = dif[morphMappingFrom[i]];
  731. for ( int i = 0; i < nonMorphMappingFrom.Length; i++ )
  732. sverts[nonMorphMappingTo[i]] = oPoints[nonMorphMappingFrom[i]];
  733. }
  734. else
  735. {
  736. for ( int i = 0; i < verts.Length; i++ )
  737. sverts[i] = verts[i];
  738. }
  739. }
  740. // Build compressed data
  741. public int[] nonMorphedVerts;
  742. public int[] morphedVerts;
  743. public int[] morphMappingFrom;
  744. public int[] morphMappingTo;
  745. public int[] nonMorphMappingFrom;
  746. public int[] nonMorphMappingTo;
  747. [ContextMenu("Compress Morphs")]
  748. public void BuildCompress()
  749. {
  750. bool[] altered = new bool[oPoints.Length];
  751. int count = 0;
  752. for ( int c = 0; c < chanBank.Count; c++ )
  753. {
  754. for ( int v = 0; v < oPoints.Length; v++ )
  755. {
  756. if ( Changed(v, c) )
  757. altered[v] = true;
  758. }
  759. }
  760. for ( int i = 0; i < altered.Length; i++ )
  761. {
  762. if ( altered[i] )
  763. count++;
  764. }
  765. morphedVerts = new int[count];
  766. nonMorphedVerts = new int[oPoints.Length - count];
  767. int mindex = 0;
  768. int nmindex = 0;
  769. List<int> mappedFrom = new List<int>();
  770. List<int> mappedTo = new List<int>();
  771. for ( int i = 0; i < oPoints.Length; i++ )
  772. {
  773. if ( altered[i] )
  774. {
  775. morphedVerts[mindex++] = i;
  776. for ( int m = 0; m < mapping.Length; m++ )
  777. {
  778. if ( mapping[m] == i )
  779. {
  780. mappedFrom.Add(i);
  781. mappedTo.Add(m);
  782. }
  783. }
  784. }
  785. else
  786. nonMorphedVerts[nmindex++] = i;
  787. }
  788. morphMappingFrom = mappedFrom.ToArray();
  789. morphMappingTo = mappedTo.ToArray();
  790. mappedFrom.Clear();
  791. mappedTo.Clear();
  792. for ( int i = 0; i < oPoints.Length; i++ )
  793. {
  794. if ( !altered[i] )
  795. {
  796. for ( int m = 0; m < mapping.Length; m++ )
  797. {
  798. if ( mapping[m] == i )
  799. {
  800. mappedFrom.Add(i);
  801. mappedTo.Add(m);
  802. }
  803. }
  804. }
  805. }
  806. nonMorphMappingFrom = mappedFrom.ToArray();
  807. nonMorphMappingTo = mappedTo.ToArray();
  808. compressedmem = morphedVerts.Length * chanBank.Count * 12;
  809. }
  810. // Threaded version
  811. Vector3[] _verts;
  812. Vector3[] _sverts;
  813. public override void PrepareMT(MegaModifiers mc, int cores)
  814. {
  815. PrepareForMT(mc, cores);
  816. }
  817. public override void DoWork(MegaModifiers mc, int index, int start, int end, int cores)
  818. {
  819. ModifyCompressedMT(mc, index, cores);
  820. }
  821. public void PrepareForMT(MegaModifiers mc, int cores)
  822. {
  823. if ( setStart == null )
  824. BuildMorphVertInfo(cores);
  825. // cycle through channels, searching for ones to use
  826. mtmorphed = false;
  827. for ( int i = 0; i < chanBank.Count; i++ )
  828. {
  829. MegaMorphChan chan = chanBank[i];
  830. chan.UpdatePercent();
  831. float fChannelPercent;
  832. if ( chan.mUseLimit )
  833. fChannelPercent = Mathf.Clamp(chan.Percent, chan.mSpinmin, chan.mSpinmax);
  834. else
  835. fChannelPercent = Mathf.Clamp(chan.Percent, 0.0f, 100.0f);
  836. if ( fChannelPercent != 0.0f || (fChannelPercent == 0.0f && chan.fChannelPercent != 0.0f) )
  837. {
  838. chan.fChannelPercent = fChannelPercent;
  839. if ( chan.mTargetCache != null && chan.mTargetCache.Count > 0 && chan.mActiveOverride )
  840. {
  841. mtmorphed = true;
  842. if ( chan.mTargetCache.Count > 1 )
  843. {
  844. int totaltargs = chan.mTargetCache.Count; // + 1; // + 1;
  845. chan.fProgression = chan.fChannelPercent; //Mathf.Clamp(fChannelPercent, 0.0f, 100.0f);
  846. chan.segment = 1;
  847. while ( chan.segment <= totaltargs && chan.fProgression >= chan.GetTargetPercent(chan.segment - 2) )
  848. chan.segment++;
  849. if ( chan.segment > totaltargs )
  850. chan.segment = totaltargs;
  851. chan.p4 = oPoints;
  852. if ( chan.segment == 1 )
  853. {
  854. chan.p1 = oPoints;
  855. chan.p2 = chan.mTargetCache[0].points; // mpoints
  856. chan.p3 = chan.mTargetCache[1].points;
  857. }
  858. else
  859. {
  860. if ( chan.segment == totaltargs )
  861. {
  862. int targnum = totaltargs - 1;
  863. for ( int j = 2; j >= 0; j-- )
  864. {
  865. targnum--;
  866. if ( targnum == -2 )
  867. SetVerts(chan, j, oPoints);
  868. else
  869. SetVerts(chan, j, chan.mTargetCache[targnum + 1].points);
  870. }
  871. }
  872. else
  873. {
  874. int targnum = chan.segment;
  875. for ( int j = 3; j >= 0; j-- )
  876. {
  877. targnum--;
  878. if ( targnum == -2 )
  879. SetVerts(chan, j, oPoints);
  880. else
  881. SetVerts(chan, j, chan.mTargetCache[targnum + 1].points);
  882. }
  883. }
  884. }
  885. }
  886. }
  887. }
  888. }
  889. if ( !mtmorphed )
  890. {
  891. for ( int i = 0; i < verts.Length; i++ )
  892. sverts[i] = verts[i];
  893. }
  894. else
  895. {
  896. //if ( firstchan )
  897. //{
  898. //firstchan = false;
  899. for ( int pointnum = 0; pointnum < morphedVerts.Length; pointnum++ )
  900. {
  901. int p = morphedVerts[pointnum];
  902. dif[p] = oPoints[p];
  903. }
  904. //}
  905. }
  906. }
  907. bool mtmorphed;
  908. public void ModifyCompressedMT(MegaModifiers mc, int tindex, int cores)
  909. {
  910. if ( !mtmorphed )
  911. return;
  912. int step = morphedVerts.Length / cores;
  913. int startvert = (tindex * step);
  914. int endvert = startvert + step;
  915. if ( tindex == cores - 1 )
  916. endvert = morphedVerts.Length;
  917. framenum++;
  918. Vector3 delt;
  919. // cycle through channels, searching for ones to use
  920. //bool firstchan = true;
  921. Vector3[] endpoint = new Vector3[4]; // These in channel class
  922. Vector3[] splinepoint = new Vector3[4];
  923. Vector3[] temppoint = new Vector3[2];
  924. for ( int i = 0; i < chanBank.Count; i++ )
  925. {
  926. MegaMorphChan chan = chanBank[i];
  927. if ( chan.fChannelPercent != 0.0f )
  928. {
  929. if ( chan.mTargetCache != null && chan.mTargetCache.Count > 0 && chan.mActiveOverride ) //&& fChannelPercent != 0.0f )
  930. {
  931. #if false
  932. if ( firstchan )
  933. {
  934. firstchan = false;
  935. for ( int pointnum = startvert; pointnum < endvert; pointnum++ )
  936. {
  937. int p = morphedVerts[pointnum];
  938. dif[p] = oPoints[p];
  939. }
  940. }
  941. #endif
  942. if ( chan.mTargetCache.Count == 1 )
  943. {
  944. for ( int pointnum = startvert; pointnum < endvert; pointnum++ )
  945. {
  946. int p = morphedVerts[pointnum];
  947. delt = chan.mDeltas[p];
  948. dif[p].x += delt.x * chan.fChannelPercent;
  949. dif[p].y += delt.y * chan.fChannelPercent;
  950. dif[p].z += delt.z * chan.fChannelPercent;
  951. }
  952. }
  953. else
  954. {
  955. float targetpercent1 = chan.GetTargetPercent(chan.segment - 3);
  956. float targetpercent2 = chan.GetTargetPercent(chan.segment - 2);
  957. float top = chan.fProgression - targetpercent1;
  958. float bottom = targetpercent2 - targetpercent1;
  959. float u = top / bottom;
  960. for ( int pointnum = startvert; pointnum < endvert; pointnum++ )
  961. {
  962. int p = morphedVerts[pointnum];
  963. Vector3 vert = oPoints[p]; //pointnum];
  964. float length;
  965. Vector3 progession;
  966. endpoint[0] = chan.p1[p];
  967. endpoint[1] = chan.p2[p];
  968. endpoint[2] = chan.p3[p];
  969. endpoint[3] = chan.p4[p];
  970. if ( chan.segment == 1 )
  971. {
  972. splinepoint[0] = endpoint[0];
  973. splinepoint[3] = endpoint[1];
  974. temppoint[1] = endpoint[2] - endpoint[0];
  975. temppoint[0] = endpoint[1] - endpoint[0];
  976. length = temppoint[1].sqrMagnitude;
  977. if ( length == 0.0f )
  978. {
  979. splinepoint[1] = endpoint[0];
  980. splinepoint[2] = endpoint[1];
  981. }
  982. else
  983. {
  984. splinepoint[2] = endpoint[1] - (Vector3.Dot(temppoint[0], temppoint[1]) * chan.mCurvature / length) * temppoint[1];
  985. splinepoint[1] = endpoint[0] + chan.mCurvature * (splinepoint[2] - endpoint[0]);
  986. }
  987. }
  988. else
  989. {
  990. if ( chan.segment == chan.mTargetCache.Count ) //chan.totaltargs )
  991. {
  992. splinepoint[0] = endpoint[1];
  993. splinepoint[3] = endpoint[2];
  994. temppoint[1] = endpoint[2] - endpoint[0];
  995. temppoint[0] = endpoint[1] - endpoint[2];
  996. length = temppoint[1].sqrMagnitude;
  997. if ( length == 0.0f )
  998. {
  999. splinepoint[1] = endpoint[0];
  1000. splinepoint[2] = endpoint[1];
  1001. }
  1002. else
  1003. {
  1004. splinepoint[1] = endpoint[1] - (Vector3.Dot(temppoint[1], temppoint[0]) * chan.mCurvature / length) * temppoint[1];
  1005. splinepoint[2] = endpoint[2] + chan.mCurvature * (splinepoint[1] - endpoint[2]);
  1006. }
  1007. }
  1008. else
  1009. {
  1010. temppoint[1] = endpoint[2] - endpoint[0];
  1011. temppoint[0] = endpoint[1] - endpoint[0];
  1012. length = temppoint[1].sqrMagnitude;
  1013. splinepoint[0] = endpoint[1];
  1014. splinepoint[3] = endpoint[2];
  1015. if ( length == 0.0f )
  1016. splinepoint[1] = endpoint[0];
  1017. else
  1018. splinepoint[1] = endpoint[1] + (Vector3.Dot(temppoint[0], temppoint[1]) * chan.mCurvature / length) * temppoint[1];
  1019. temppoint[1] = endpoint[3] - endpoint[1];
  1020. temppoint[0] = endpoint[2] - endpoint[1];
  1021. length = temppoint[1].sqrMagnitude;
  1022. if ( length == 0.0f )
  1023. splinepoint[2] = endpoint[1];
  1024. else
  1025. splinepoint[2] = endpoint[2] - (Vector3.Dot(temppoint[0], temppoint[1]) * chan.mCurvature / length) * temppoint[1];
  1026. }
  1027. }
  1028. MegaUtils.Bez3D(out progession, ref splinepoint, u);
  1029. dif[p].x += progession.x - vert.x;
  1030. dif[p].y += progession.y - vert.y;
  1031. dif[p].z += progession.z - vert.z;
  1032. }
  1033. }
  1034. }
  1035. }
  1036. }
  1037. if ( mtmorphed )
  1038. {
  1039. //for ( int i = setStart[tindex]; i < setEnd[tindex]; i++ )
  1040. //sverts[morphMappingTo[i]] = dif[morphMappingFrom[i]];
  1041. //for ( int i = copyStart[tindex]; i < copyEnd[tindex]; i++ )
  1042. //sverts[nonMorphMappingTo[i]] = oPoints[nonMorphMappingFrom[i]];
  1043. }
  1044. }
  1045. public override void DoneMT(MegaModifiers mod)
  1046. {
  1047. if ( mtmorphed )
  1048. {
  1049. for ( int i = 0; i < morphMappingFrom.Length; i++ )
  1050. sverts[morphMappingTo[i]] = dif[morphMappingFrom[i]];
  1051. for ( int i = 0; i < nonMorphMappingFrom.Length; i++ )
  1052. sverts[nonMorphMappingTo[i]] = oPoints[nonMorphMappingFrom[i]];
  1053. }
  1054. }
  1055. int[] setStart;
  1056. int[] setEnd;
  1057. int[] copyStart;
  1058. int[] copyEnd;
  1059. int Find(int index)
  1060. {
  1061. int f = morphedVerts[index];
  1062. for ( int i = 0; i < morphMappingFrom.Length; i++ )
  1063. {
  1064. if ( morphMappingFrom[i] > f )
  1065. return i;
  1066. }
  1067. return morphMappingFrom.Length - 1;
  1068. }
  1069. void BuildMorphVertInfo(int cores)
  1070. {
  1071. int step = morphedVerts.Length / cores;
  1072. setStart = new int[cores];
  1073. setEnd = new int[cores];
  1074. copyStart = new int[cores];
  1075. copyEnd = new int[cores];
  1076. int start = 0;
  1077. int fv = 0;
  1078. for ( int i = 0; i < cores; i++ )
  1079. {
  1080. setStart[i] = start;
  1081. if ( i < cores - 1 )
  1082. {
  1083. setEnd[i] = Find(fv + step);
  1084. }
  1085. start = setEnd[i];
  1086. fv += step;
  1087. }
  1088. setEnd[cores - 1] = morphMappingFrom.Length;
  1089. // copys can be simple split as nothing happens to them
  1090. start = 0;
  1091. step = nonMorphMappingFrom.Length / cores;
  1092. for ( int i = 0; i < cores; i++ )
  1093. {
  1094. copyStart[i] = start;
  1095. copyEnd[i] = start + step;
  1096. start += step;
  1097. }
  1098. copyEnd[cores - 1] = nonMorphMappingFrom.Length;
  1099. }
  1100. public void SetAnimTime(float t)
  1101. {
  1102. animtime = t;
  1103. switch ( repeatMode )
  1104. {
  1105. case MegaRepeatMode.Loop: animtime = Mathf.Repeat(animtime, looptime); break;
  1106. //case RepeatMode.PingPong: animtime = Mathf.PingPong(animtime, looptime); break;
  1107. case MegaRepeatMode.Clamp: animtime = Mathf.Clamp(animtime, 0.0f, looptime); break;
  1108. }
  1109. SetAnim(animtime);
  1110. }
  1111. }