MegaMorphRef.cs 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952
  1. using UnityEngine;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. [AddComponentMenu("Modifiers/Morph Ref")]
  5. public class MegaMorphRef : MegaMorphBase
  6. {
  7. public MegaMorph source;
  8. public bool UseLimit;
  9. public float Max;
  10. public float Min;
  11. public int[] mapping;
  12. public float importScale = 1.0f;
  13. public bool flipyz = false;
  14. public bool negx = false;
  15. [HideInInspector]
  16. public float tolerance = 0.0001f;
  17. public bool showmapping = false;
  18. public float mappingSize = 0.001f;
  19. public int mapStart = 0;
  20. public int mapEnd = 0;
  21. Vector3[] dif;
  22. static Vector3[] endpoint = new Vector3[4];
  23. static Vector3[] splinepoint = new Vector3[4];
  24. static Vector3[] temppoint = new Vector3[2];
  25. Vector3[] p1;
  26. Vector3[] p2;
  27. Vector3[] p3;
  28. Vector3[] p4;
  29. public List<float> pers = new List<float>(4);
  30. public override string ModName() { return "Morph Ref"; }
  31. public override string GetHelpURL() { return "?page_id=257"; }
  32. [HideInInspector]
  33. public int compressedmem = 0;
  34. [HideInInspector]
  35. public int compressedmem1 = 0;
  36. [HideInInspector]
  37. public int memuse = 0;
  38. public void SetSource(MegaMorph src)
  39. {
  40. source = src;
  41. if ( source )
  42. {
  43. if ( chanBank == null )
  44. chanBank = new List<MegaMorphChan>();
  45. chanBank.Clear();
  46. for ( int i = 0; i < source.chanBank.Count; i++ )
  47. {
  48. MegaMorphChan ch = new MegaMorphChan();
  49. ch.control = source.chanBank[i].control;
  50. ch.Percent = source.chanBank[i].Percent;
  51. ch.mName = source.chanBank[i].mName;
  52. chanBank.Add(ch);
  53. }
  54. }
  55. }
  56. public override bool ModLateUpdate(MegaModContext mc)
  57. {
  58. if ( source )
  59. {
  60. if ( animate )
  61. {
  62. if ( Application.isPlaying )
  63. animtime += Time.deltaTime * speed;
  64. switch ( repeatMode )
  65. {
  66. case MegaRepeatMode.Loop: animtime = Mathf.Repeat(animtime, looptime); break;
  67. case MegaRepeatMode.Clamp: animtime = Mathf.Clamp(animtime, 0.0f, looptime); break;
  68. }
  69. SetAnim(animtime);
  70. }
  71. if ( dif == null )
  72. {
  73. dif = new Vector3[mc.mod.verts.Length];
  74. }
  75. }
  76. return Prepare(mc);
  77. }
  78. public bool animate = false;
  79. public float atime = 0.0f;
  80. public float animtime = 0.0f;
  81. public float looptime = 0.0f;
  82. public MegaRepeatMode repeatMode = MegaRepeatMode.Loop;
  83. public float speed = 1.0f;
  84. public override bool Prepare(MegaModContext mc)
  85. {
  86. if ( source )
  87. return source.Prepare(mc);
  88. return false;
  89. }
  90. // Only need to call if a Percent value has changed on a Channel or a target, so flag for a change
  91. void SetVerts(int j, Vector3[] p)
  92. {
  93. switch ( j )
  94. {
  95. case 0: p1 = p; break;
  96. case 1: p2 = p; break;
  97. case 2: p3 = p; break;
  98. case 3: p4 = p; break;
  99. }
  100. }
  101. void SetVerts(MegaMorphChan chan, int j, Vector3[] p)
  102. {
  103. switch ( j )
  104. {
  105. case 0: chan.p1 = p; break;
  106. case 1: chan.p2 = p; break;
  107. case 2: chan.p3 = p; break;
  108. case 3: chan.p4 = p; break;
  109. }
  110. }
  111. static int framenum;
  112. // oPoints whould be verts
  113. public override void Modify(MegaModifiers mc)
  114. {
  115. if ( source == null )
  116. return;
  117. if ( source.nonMorphedVerts != null && source.nonMorphedVerts.Length > 1 )
  118. {
  119. ModifyCompressed(mc);
  120. return;
  121. }
  122. framenum++;
  123. mc.ChangeSourceVerts();
  124. float fChannelPercent;
  125. Vector3 delt;
  126. // cycle through channels, searching for ones to use
  127. bool firstchan = true;
  128. bool morphed = false;
  129. float min = 0.0f;
  130. float max = 100.0f;
  131. if ( UseLimit )
  132. {
  133. min = Min;
  134. max = Max;
  135. }
  136. for ( int i = 0; i < source.chanBank.Count; i++ )
  137. {
  138. MegaMorphChan chan = source.chanBank[i];
  139. // This needs to be local chan list
  140. chan.UpdatePercent();
  141. if ( UseLimit )
  142. {
  143. fChannelPercent = Mathf.Clamp(chan.Percent, min, max); //chan.mSpinmin, chan.mSpinmax);
  144. }
  145. else
  146. {
  147. if ( chan.mUseLimit )
  148. fChannelPercent = Mathf.Clamp(chan.Percent, chan.mSpinmin, chan.mSpinmax);
  149. else
  150. fChannelPercent = Mathf.Clamp(chan.Percent, 0.0f, 100.0f);
  151. }
  152. if ( fChannelPercent != 0.0f || (fChannelPercent == 0.0f && chan.fChannelPercent != 0.0f) )
  153. {
  154. chan.fChannelPercent = fChannelPercent;
  155. if ( chan.mTargetCache != null && chan.mTargetCache.Count > 0 && chan.mActiveOverride ) //&& fChannelPercent != 0.0f )
  156. {
  157. morphed = true;
  158. if ( chan.mUseLimit ) //|| glUseLimit )
  159. {
  160. }
  161. if ( firstchan )
  162. {
  163. firstchan = false;
  164. for ( int pointnum = 0; pointnum < source.oPoints.Length; pointnum++ )
  165. {
  166. dif[pointnum] = source.oPoints[pointnum];
  167. }
  168. }
  169. if ( chan.mTargetCache.Count == 1 )
  170. {
  171. for ( int pointnum = 0; pointnum < source.oPoints.Length; pointnum++ )
  172. {
  173. delt = chan.mDeltas[pointnum];
  174. dif[pointnum].x += delt.x * fChannelPercent;
  175. dif[pointnum].y += delt.y * fChannelPercent;
  176. dif[pointnum].z += delt.z * fChannelPercent;
  177. }
  178. }
  179. else
  180. {
  181. int totaltargs = chan.mTargetCache.Count; // + 1; // + 1;
  182. float fProgression = fChannelPercent; //Mathf.Clamp(fChannelPercent, 0.0f, 100.0f);
  183. int segment = 1;
  184. while ( segment <= totaltargs && fProgression >= chan.GetTargetPercent(segment - 2) )
  185. segment++;
  186. if ( segment > totaltargs )
  187. segment = totaltargs;
  188. p4 = source.oPoints;
  189. if ( segment == 1 )
  190. {
  191. p1 = source.oPoints;
  192. p2 = chan.mTargetCache[0].points; // mpoints
  193. p3 = chan.mTargetCache[1].points;
  194. }
  195. else
  196. {
  197. if ( segment == totaltargs )
  198. {
  199. int targnum = totaltargs - 1;
  200. for ( int j = 2; j >= 0; j-- )
  201. {
  202. targnum--;
  203. if ( targnum == -2 )
  204. SetVerts(j, source.oPoints);
  205. else
  206. SetVerts(j, chan.mTargetCache[targnum + 1].points);
  207. }
  208. }
  209. else
  210. {
  211. int targnum = segment;
  212. for ( int j = 3; j >= 0; j-- )
  213. {
  214. targnum--;
  215. if ( targnum == -2 )
  216. SetVerts(j, source.oPoints);
  217. else
  218. SetVerts(j, chan.mTargetCache[targnum + 1].points);
  219. }
  220. }
  221. }
  222. float targetpercent1 = chan.GetTargetPercent(segment - 3);
  223. float targetpercent2 = chan.GetTargetPercent(segment - 2);
  224. float top = fProgression - targetpercent1;
  225. float bottom = targetpercent2 - targetpercent1;
  226. float u = top / bottom;
  227. {
  228. for ( int pointnum = 0; pointnum < source.oPoints.Length; pointnum++ )
  229. {
  230. Vector3 vert = source.oPoints[pointnum];
  231. float length;
  232. Vector3 progession;
  233. endpoint[0] = p1[pointnum];
  234. endpoint[1] = p2[pointnum];
  235. endpoint[2] = p3[pointnum];
  236. endpoint[3] = p4[pointnum];
  237. if ( segment == 1 )
  238. {
  239. splinepoint[0] = endpoint[0];
  240. splinepoint[3] = endpoint[1];
  241. temppoint[1] = endpoint[2] - endpoint[0];
  242. temppoint[0] = endpoint[1] - endpoint[0];
  243. length = temppoint[1].sqrMagnitude;
  244. if ( length == 0.0f )
  245. {
  246. splinepoint[1] = endpoint[0];
  247. splinepoint[2] = endpoint[1];
  248. }
  249. else
  250. {
  251. splinepoint[2] = endpoint[1] - (Vector3.Dot(temppoint[0], temppoint[1]) * chan.mCurvature / length) * temppoint[1];
  252. splinepoint[1] = endpoint[0] + chan.mCurvature * (splinepoint[2] - endpoint[0]);
  253. }
  254. }
  255. else
  256. {
  257. if ( segment == totaltargs )
  258. {
  259. splinepoint[0] = endpoint[1];
  260. splinepoint[3] = endpoint[2];
  261. temppoint[1] = endpoint[2] - endpoint[0];
  262. temppoint[0] = endpoint[1] - endpoint[2];
  263. length = temppoint[1].sqrMagnitude;
  264. if ( length == 0.0f )
  265. {
  266. splinepoint[1] = endpoint[0];
  267. splinepoint[2] = endpoint[1];
  268. }
  269. else
  270. {
  271. splinepoint[1] = endpoint[1] - (Vector3.Dot(temppoint[1], temppoint[0]) * chan.mCurvature / length) * temppoint[1];
  272. splinepoint[2] = endpoint[2] + chan.mCurvature * (splinepoint[1] - endpoint[2]);
  273. }
  274. }
  275. else
  276. {
  277. temppoint[1] = endpoint[2] - endpoint[0];
  278. temppoint[0] = endpoint[1] - endpoint[0];
  279. length = temppoint[1].sqrMagnitude;
  280. splinepoint[0] = endpoint[1];
  281. splinepoint[3] = endpoint[2];
  282. if ( length == 0.0f )
  283. splinepoint[1] = endpoint[0];
  284. else
  285. splinepoint[1] = endpoint[1] + (Vector3.Dot(temppoint[0], temppoint[1]) * chan.mCurvature / length) * temppoint[1];
  286. temppoint[1] = endpoint[3] - endpoint[1];
  287. temppoint[0] = endpoint[2] - endpoint[1];
  288. length = temppoint[1].sqrMagnitude;
  289. if ( length == 0.0f )
  290. splinepoint[2] = endpoint[1];
  291. else
  292. splinepoint[2] = endpoint[2] - (Vector3.Dot(temppoint[0], temppoint[1]) * chan.mCurvature / length) * temppoint[1];
  293. }
  294. }
  295. MegaUtils.Bez3D(out progession, ref splinepoint, u);
  296. dif[pointnum].x += progession.x - vert.x; //delt;
  297. dif[pointnum].y += progession.y - vert.y; //delt;
  298. dif[pointnum].z += progession.z - vert.z; //delt;
  299. }
  300. }
  301. }
  302. }
  303. }
  304. }
  305. if ( morphed )
  306. {
  307. for ( int i = 0; i < source.mapping.Length; i++ )
  308. sverts[i] = dif[source.mapping[i]];
  309. }
  310. else
  311. {
  312. for ( int i = 0; i < verts.Length; i++ )
  313. sverts[i] = verts[i];
  314. }
  315. }
  316. bool Changed(int v, int c)
  317. {
  318. for ( int t = 0; t < source.chanBank[c].mTargetCache.Count; t++ )
  319. {
  320. if ( !source.oPoints[v].Equals(source.chanBank[c].mTargetCache[t].points[v]) )
  321. return true;
  322. }
  323. return false;
  324. }
  325. public void ModifyCompressed(MegaModifiers mc)
  326. {
  327. framenum++;
  328. mc.ChangeSourceVerts();
  329. float fChannelPercent;
  330. Vector3 delt;
  331. // cycle through channels, searching for ones to use
  332. bool firstchan = true;
  333. bool morphed = false;
  334. for ( int i = 0; i < source.chanBank.Count; i++ )
  335. {
  336. MegaMorphChan chan = source.chanBank[i];
  337. MegaMorphChan lchan = chanBank[i]; // copy of source banks with out the vert data
  338. chan.UpdatePercent();
  339. if ( chan.mUseLimit )
  340. fChannelPercent = Mathf.Clamp(lchan.Percent, chan.mSpinmin, chan.mSpinmax);
  341. else
  342. fChannelPercent = Mathf.Clamp(lchan.Percent, 0.0f, 100.0f);
  343. if ( fChannelPercent != 0.0f || (fChannelPercent == 0.0f && chan.fChannelPercent != 0.0f) )
  344. {
  345. chan.fChannelPercent = fChannelPercent;
  346. if ( chan.mTargetCache != null && chan.mTargetCache.Count > 0 && chan.mActiveOverride ) //&& fChannelPercent != 0.0f )
  347. {
  348. morphed = true;
  349. if ( chan.mUseLimit ) //|| glUseLimit )
  350. {
  351. }
  352. // New bit
  353. if ( firstchan )
  354. {
  355. firstchan = false;
  356. // Save a int array of morphedpoints and use that, then only dealing with changed info
  357. for ( int pointnum = 0; pointnum < source.morphedVerts.Length; pointnum++ )
  358. {
  359. // this will change when we remove points
  360. int p = source.morphedVerts[pointnum];
  361. dif[p] = source.oPoints[p]; //morphedVerts[pointnum]];
  362. }
  363. }
  364. // end new
  365. if ( chan.mTargetCache.Count == 1 )
  366. {
  367. // Save a int array of morphedpoints and use that, then only dealing with changed info
  368. for ( int pointnum = 0; pointnum < source.morphedVerts.Length; pointnum++ )
  369. {
  370. int p = source.morphedVerts[pointnum];
  371. delt = chan.mDeltas[p]; //morphedVerts[pointnum]];
  372. //delt = chan.mDeltas[pointnum]; //morphedVerts[pointnum]];
  373. dif[p].x += delt.x * fChannelPercent;
  374. dif[p].y += delt.y * fChannelPercent;
  375. dif[p].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 = source.oPoints;
  388. if ( segment == 1 )
  389. {
  390. p1 = source.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, source.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, source.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. for ( int pointnum = 0; pointnum < source.morphedVerts.Length; pointnum++ )
  427. {
  428. int p = source.morphedVerts[pointnum];
  429. Vector3 vert = source.oPoints[p]; //pointnum];
  430. float length;
  431. Vector3 progession;
  432. endpoint[0] = p1[p];
  433. endpoint[1] = p2[p];
  434. endpoint[2] = p3[p];
  435. endpoint[3] = p4[p];
  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[p].x += progession.x - vert.x;
  496. dif[p].y += progession.y - vert.y;
  497. dif[p].z += progession.z - vert.z;
  498. }
  499. }
  500. }
  501. }
  502. }
  503. if ( morphed )
  504. {
  505. for ( int i = 0; i < source.morphMappingFrom.Length; i++ )
  506. sverts[source.morphMappingTo[i]] = dif[source.morphMappingFrom[i]];
  507. for ( int i = 0; i < source.nonMorphMappingFrom.Length; i++ )
  508. sverts[source.nonMorphMappingTo[i]] = source.oPoints[source.nonMorphMappingFrom[i]];
  509. }
  510. else
  511. {
  512. for ( int i = 0; i < verts.Length; i++ )
  513. sverts[i] = verts[i];
  514. }
  515. }
  516. // Threaded version
  517. Vector3[] _verts;
  518. Vector3[] _sverts;
  519. public override void PrepareMT(MegaModifiers mc, int cores)
  520. {
  521. PrepareForMT(mc, cores);
  522. }
  523. public override void DoWork(MegaModifiers mc, int index, int start, int end, int cores)
  524. {
  525. ModifyCompressedMT(mc, index, cores);
  526. }
  527. public void PrepareForMT(MegaModifiers mc, int cores)
  528. {
  529. if ( setStart == null )
  530. BuildMorphVertInfo(cores);
  531. // cycle through channels, searching for ones to use
  532. mtmorphed = false;
  533. for ( int i = 0; i < source.chanBank.Count; i++ )
  534. {
  535. MegaMorphChan chan = source.chanBank[i];
  536. chan.UpdatePercent();
  537. //float fChannelPercent = Mathf.Clamp(chan.Percent, 0.0f, 100.0f);
  538. float fChannelPercent;
  539. if ( chan.mUseLimit )
  540. fChannelPercent = Mathf.Clamp(chan.Percent, chan.mSpinmin, chan.mSpinmax);
  541. else
  542. fChannelPercent = Mathf.Clamp(chan.Percent, 0.0f, 100.0f);
  543. if ( fChannelPercent != 0.0f || (fChannelPercent == 0.0f && chan.fChannelPercent != 0.0f) )
  544. {
  545. chan.fChannelPercent = fChannelPercent;
  546. if ( chan.mTargetCache != null && chan.mTargetCache.Count > 0 && chan.mActiveOverride )
  547. {
  548. mtmorphed = true;
  549. if ( chan.mTargetCache.Count > 1 )
  550. {
  551. int totaltargs = chan.mTargetCache.Count; // + 1; // + 1;
  552. chan.fProgression = chan.fChannelPercent; //Mathf.Clamp(fChannelPercent, 0.0f, 100.0f);
  553. chan.segment = 1;
  554. while ( chan.segment <= totaltargs && chan.fProgression >= chan.GetTargetPercent(chan.segment - 2) )
  555. chan.segment++;
  556. if ( chan.segment > totaltargs )
  557. chan.segment = totaltargs;
  558. chan.p4 = source.oPoints;
  559. if ( chan.segment == 1 )
  560. {
  561. chan.p1 = source.oPoints;
  562. chan.p2 = chan.mTargetCache[0].points; // mpoints
  563. chan.p3 = chan.mTargetCache[1].points;
  564. }
  565. else
  566. {
  567. if ( chan.segment == totaltargs )
  568. {
  569. int targnum = totaltargs - 1;
  570. for ( int j = 2; j >= 0; j-- )
  571. {
  572. targnum--;
  573. if ( targnum == -2 )
  574. SetVerts(chan, j, source.oPoints);
  575. else
  576. SetVerts(chan, j, chan.mTargetCache[targnum + 1].points);
  577. }
  578. }
  579. else
  580. {
  581. int targnum = chan.segment;
  582. for ( int j = 3; j >= 0; j-- )
  583. {
  584. targnum--;
  585. if ( targnum == -2 )
  586. SetVerts(chan, j, source.oPoints);
  587. else
  588. SetVerts(chan, j, chan.mTargetCache[targnum + 1].points);
  589. }
  590. }
  591. }
  592. }
  593. }
  594. }
  595. }
  596. if ( !mtmorphed )
  597. {
  598. for ( int i = 0; i < verts.Length; i++ )
  599. sverts[i] = verts[i];
  600. }
  601. }
  602. bool mtmorphed;
  603. public void ModifyCompressedMT(MegaModifiers mc, int tindex, int cores)
  604. {
  605. if ( !mtmorphed )
  606. return;
  607. int step = source.morphedVerts.Length / cores;
  608. int startvert = (tindex * step);
  609. int endvert = startvert + step;
  610. if ( tindex == cores - 1 )
  611. endvert = source.morphedVerts.Length;
  612. framenum++;
  613. Vector3 delt;
  614. // cycle through channels, searching for ones to use
  615. bool firstchan = true;
  616. Vector3[] endpoint = new Vector3[4]; // These in channel class
  617. Vector3[] splinepoint = new Vector3[4];
  618. Vector3[] temppoint = new Vector3[2];
  619. for ( int i = 0; i < chanBank.Count; i++ )
  620. {
  621. MegaMorphChan chan = chanBank[i];
  622. if ( chan.fChannelPercent != 0.0f )
  623. {
  624. if ( chan.mTargetCache != null && chan.mTargetCache.Count > 0 && chan.mActiveOverride ) //&& fChannelPercent != 0.0f )
  625. {
  626. if ( firstchan )
  627. {
  628. firstchan = false;
  629. for ( int pointnum = startvert; pointnum < endvert; pointnum++ )
  630. {
  631. int p = source.morphedVerts[pointnum];
  632. dif[p] = source.oPoints[p];
  633. }
  634. }
  635. if ( chan.mTargetCache.Count == 1 )
  636. {
  637. {
  638. for ( int pointnum = startvert; pointnum < endvert; pointnum++ )
  639. {
  640. int p = source.morphedVerts[pointnum];
  641. delt = chan.mDeltas[p];
  642. dif[p].x += delt.x * chan.fChannelPercent;
  643. dif[p].y += delt.y * chan.fChannelPercent;
  644. dif[p].z += delt.z * chan.fChannelPercent;
  645. }
  646. }
  647. }
  648. else
  649. {
  650. float targetpercent1 = chan.GetTargetPercent(chan.segment - 3);
  651. float targetpercent2 = chan.GetTargetPercent(chan.segment - 2);
  652. float top = chan.fProgression - targetpercent1;
  653. float bottom = targetpercent2 - targetpercent1;
  654. float u = top / bottom;
  655. for ( int pointnum = startvert; pointnum < endvert; pointnum++ )
  656. {
  657. int p = source.morphedVerts[pointnum];
  658. Vector3 vert = source.oPoints[p]; //pointnum];
  659. float length;
  660. Vector3 progession;
  661. endpoint[0] = chan.p1[p];
  662. endpoint[1] = chan.p2[p];
  663. endpoint[2] = chan.p3[p];
  664. endpoint[3] = chan.p4[p];
  665. if ( chan.segment == 1 )
  666. {
  667. splinepoint[0] = endpoint[0];
  668. splinepoint[3] = endpoint[1];
  669. temppoint[1] = endpoint[2] - endpoint[0];
  670. temppoint[0] = endpoint[1] - endpoint[0];
  671. length = temppoint[1].sqrMagnitude;
  672. if ( length == 0.0f )
  673. {
  674. splinepoint[1] = endpoint[0];
  675. splinepoint[2] = endpoint[1];
  676. }
  677. else
  678. {
  679. splinepoint[2] = endpoint[1] - (Vector3.Dot(temppoint[0], temppoint[1]) * chan.mCurvature / length) * temppoint[1];
  680. splinepoint[1] = endpoint[0] + chan.mCurvature * (splinepoint[2] - endpoint[0]);
  681. }
  682. }
  683. else
  684. {
  685. if ( chan.segment == chan.mTargetCache.Count ) //chan.totaltargs )
  686. {
  687. splinepoint[0] = endpoint[1];
  688. splinepoint[3] = endpoint[2];
  689. temppoint[1] = endpoint[2] - endpoint[0];
  690. temppoint[0] = endpoint[1] - endpoint[2];
  691. length = temppoint[1].sqrMagnitude;
  692. if ( length == 0.0f )
  693. {
  694. splinepoint[1] = endpoint[0];
  695. splinepoint[2] = endpoint[1];
  696. }
  697. else
  698. {
  699. splinepoint[1] = endpoint[1] - (Vector3.Dot(temppoint[1], temppoint[0]) * chan.mCurvature / length) * temppoint[1];
  700. splinepoint[2] = endpoint[2] + chan.mCurvature * (splinepoint[1] - endpoint[2]);
  701. }
  702. }
  703. else
  704. {
  705. temppoint[1] = endpoint[2] - endpoint[0];
  706. temppoint[0] = endpoint[1] - endpoint[0];
  707. length = temppoint[1].sqrMagnitude;
  708. splinepoint[0] = endpoint[1];
  709. splinepoint[3] = endpoint[2];
  710. if ( length == 0.0f )
  711. splinepoint[1] = endpoint[0];
  712. else
  713. splinepoint[1] = endpoint[1] + (Vector3.Dot(temppoint[0], temppoint[1]) * chan.mCurvature / length) * temppoint[1];
  714. temppoint[1] = endpoint[3] - endpoint[1];
  715. temppoint[0] = endpoint[2] - endpoint[1];
  716. length = temppoint[1].sqrMagnitude;
  717. if ( length == 0.0f )
  718. splinepoint[2] = endpoint[1];
  719. else
  720. splinepoint[2] = endpoint[2] - (Vector3.Dot(temppoint[0], temppoint[1]) * chan.mCurvature / length) * temppoint[1];
  721. }
  722. }
  723. MegaUtils.Bez3D(out progession, ref splinepoint, u);
  724. dif[p].x += progession.x - vert.x;
  725. dif[p].y += progession.y - vert.y;
  726. dif[p].z += progession.z - vert.z;
  727. }
  728. }
  729. }
  730. }
  731. }
  732. if ( mtmorphed )
  733. {
  734. for ( int i = setStart[tindex]; i < setEnd[tindex]; i++ )
  735. sverts[source.morphMappingTo[i]] = dif[source.morphMappingFrom[i]];
  736. for ( int i = copyStart[tindex]; i < copyEnd[tindex]; i++ )
  737. sverts[source.nonMorphMappingTo[i]] = source.oPoints[source.nonMorphMappingFrom[i]];
  738. }
  739. }
  740. int[] setStart;
  741. int[] setEnd;
  742. int[] copyStart;
  743. int[] copyEnd;
  744. int Find(int index)
  745. {
  746. int f = source.morphedVerts[index];
  747. for ( int i = 0; i < source.morphMappingFrom.Length; i++ )
  748. {
  749. if ( source.morphMappingFrom[i] > f )
  750. return i;
  751. }
  752. return source.morphMappingFrom.Length - 1;
  753. }
  754. void BuildMorphVertInfo(int cores)
  755. {
  756. int step = source.morphedVerts.Length / cores;
  757. setStart = new int[cores];
  758. setEnd = new int[cores];
  759. copyStart = new int[cores];
  760. copyEnd = new int[cores];
  761. int start = 0;
  762. int fv = 0;
  763. for ( int i = 0; i < cores; i++ )
  764. {
  765. setStart[i] = start;
  766. if ( i < cores - 1 )
  767. {
  768. setEnd[i] = Find(fv + step);
  769. }
  770. start = setEnd[i];
  771. fv += step;
  772. }
  773. setEnd[cores - 1] = source.morphMappingFrom.Length;
  774. // copys can be simple split as nothing happens to them
  775. start = 0;
  776. step = source.nonMorphMappingFrom.Length / cores;
  777. for ( int i = 0; i < cores; i++ )
  778. {
  779. copyStart[i] = start;
  780. copyEnd[i] = start + step;
  781. start += step;
  782. }
  783. copyEnd[cores - 1] = source.nonMorphMappingFrom.Length;
  784. }
  785. public void SetAnimTime(float t)
  786. {
  787. animtime = t;
  788. switch ( repeatMode )
  789. {
  790. case MegaRepeatMode.Loop: animtime = Mathf.Repeat(animtime, looptime); break;
  791. //case RepeatMode.PingPong: animtime = Mathf.PingPong(animtime, looptime); break;
  792. case MegaRepeatMode.Clamp: animtime = Mathf.Clamp(animtime, 0.0f, looptime); break;
  793. }
  794. SetAnim(animtime);
  795. }
  796. }