MegaMorphOMatic.cs 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. using UnityEngine;
  2. using System.Collections.Generic;
  3. [System.Serializable]
  4. public class MOMVert
  5. {
  6. public int id;
  7. public float w;
  8. public Vector3 start;
  9. public Vector3 delta;
  10. }
  11. [System.Serializable]
  12. public class MegaMomVertMap
  13. {
  14. public int[] indices;
  15. }
  16. [AddComponentMenu("Modifiers/Morph-O-Matic")]
  17. public class MegaMorphOMatic : MegaMorphBase
  18. {
  19. public bool UseLimit;
  20. public float Max;
  21. public float Min;
  22. public float importScale = 1.0f;
  23. public bool flipyz = false;
  24. public bool negx = false;
  25. public bool glUseLimit = false;
  26. public float glMin = 0.0f;
  27. public float glMax = 1.0f;
  28. public float tolerance = 0.0001f;
  29. public bool animate = false;
  30. public float atime = 0.0f;
  31. public float animtime = 0.0f;
  32. public float looptime = 0.0f;
  33. public MegaRepeatMode repeatMode = MegaRepeatMode.Loop;
  34. public float speed = 1.0f;
  35. public Vector3[] oPoints; // Base points
  36. public MegaMomVertMap[] mapping;
  37. public override string ModName() { return "Morph-O-Matic"; }
  38. public override string GetHelpURL() { return "?page_id=1521"; }
  39. public override bool ModLateUpdate(MegaModContext mc)
  40. {
  41. if ( animate )
  42. {
  43. if ( Application.isPlaying )
  44. animtime += Time.deltaTime * speed;
  45. switch ( repeatMode )
  46. {
  47. case MegaRepeatMode.Loop: animtime = Mathf.Repeat(animtime, looptime); break;
  48. case MegaRepeatMode.Clamp: animtime = Mathf.Clamp(animtime, 0.0f, looptime); break;
  49. }
  50. SetAnim(animtime);
  51. }
  52. return Prepare(mc);
  53. }
  54. public override bool Prepare(MegaModContext mc)
  55. {
  56. if ( chanBank != null && chanBank.Count > 0 )
  57. return true;
  58. return false;
  59. }
  60. Vector3 Cubic(MegaMorphTarget t, int pointnum, float alpha)
  61. {
  62. // Linear for now, will have coefs in here for cubic
  63. Vector3 v = t.mompoints[pointnum].delta;
  64. v.x *= alpha;
  65. v.y *= alpha;
  66. v.z *= alpha;
  67. return v;
  68. }
  69. static public void Bez3D(out Vector3 b, ref Vector3[] p, float u)
  70. {
  71. Vector3 t01 = p[0] + (p[1] - p[0]) * u;
  72. Vector3 t12 = p[1] + (p[2] - p[1]) * u;
  73. Vector3 t02 = t01 + (t12 - t01) * u;
  74. t01 = p[2] + (p[3] - p[2]) * u;
  75. Vector3 t13 = t12 + (t01 - t12) * u;
  76. b = t02 + (t13 - t02) * u;
  77. }
  78. // We should just modify the internal points then map them out at the end
  79. public override void Modify(MegaModifiers mc)
  80. {
  81. verts.CopyTo(sverts, 0); // This should only blit totally untouched verts
  82. for ( int i = 0; i < chanBank.Count; i++ )
  83. {
  84. MegaMorphChan chan = chanBank[i];
  85. chan.UpdatePercent();
  86. float fChannelPercent = chan.Percent;
  87. // check for change since last frame on percent, if none just add in diff
  88. // Can we keep each chan delta then if not changed can just add it in
  89. if ( fChannelPercent == chan.fChannelPercent )
  90. {
  91. MegaMorphTarget trg = chan.mTargetCache[chan.targ];
  92. for ( int pointnum = 0; pointnum < trg.mompoints.Length; pointnum++ )
  93. {
  94. int p = trg.mompoints[pointnum].id;
  95. int c = mapping[p].indices.Length;
  96. Vector3 df = chan.diff[pointnum];
  97. for ( int m = 0; m < c; m++ )
  98. {
  99. int index = mapping[p].indices[m];
  100. sverts[index].x += df.x;
  101. sverts[index].y += df.y;
  102. sverts[index].z += df.z;
  103. }
  104. }
  105. }
  106. else
  107. {
  108. chan.fChannelPercent = fChannelPercent;
  109. if ( chan.mTargetCache != null && chan.mTargetCache.Count > 0 && chan.mActiveOverride )
  110. {
  111. if ( chan.mUseLimit || glUseLimit )
  112. {
  113. if ( glUseLimit )
  114. fChannelPercent = Mathf.Clamp(fChannelPercent, glMin, glMax);
  115. else
  116. fChannelPercent = Mathf.Clamp(fChannelPercent, chan.mSpinmin, chan.mSpinmax);
  117. }
  118. int targ = 0;
  119. float alpha = 0.0f;
  120. if ( fChannelPercent < chan.mTargetCache[0].percent )
  121. {
  122. targ = 0;
  123. //alpha = 0.0f;
  124. alpha = (fChannelPercent - chan.mTargetCache[targ].percent) / (chan.mTargetCache[targ + 1].percent - chan.mTargetCache[targ].percent);
  125. //Debug.Log("alpha " + alpha + " percent " + fChannelPercent);
  126. }
  127. else
  128. {
  129. int last = chan.mTargetCache.Count - 1;
  130. if ( fChannelPercent >= chan.mTargetCache[last].percent )
  131. {
  132. targ = last - 1;
  133. //alpha = 1.0f;
  134. alpha = (fChannelPercent - chan.mTargetCache[targ].percent) / (chan.mTargetCache[targ + 1].percent - chan.mTargetCache[targ].percent);
  135. }
  136. else
  137. {
  138. for ( int t = 1; t < chan.mTargetCache.Count; t++ )
  139. {
  140. if ( fChannelPercent < chan.mTargetCache[t].percent )
  141. {
  142. targ = t - 1;
  143. alpha = (fChannelPercent - chan.mTargetCache[targ].percent) / (chan.mTargetCache[t].percent - chan.mTargetCache[targ].percent);
  144. //Debug.Log("alpha1 " + alpha + " percent1 " + fChannelPercent);
  145. break;
  146. }
  147. }
  148. }
  149. }
  150. MegaMorphTarget trg = chan.mTargetCache[targ];
  151. chan.targ = targ;
  152. for ( int pointnum = 0; pointnum < trg.mompoints.Length; pointnum++ )
  153. {
  154. int p = trg.mompoints[pointnum].id;
  155. // Save so if chan doesnt change we dont need to recalc
  156. Vector3 df = trg.mompoints[pointnum].start;
  157. df.x += trg.mompoints[pointnum].delta.x * alpha;
  158. df.y += trg.mompoints[pointnum].delta.y * alpha;
  159. df.z += trg.mompoints[pointnum].delta.z * alpha;
  160. chan.diff[pointnum] = df;
  161. for ( int m = 0; m < mapping[p].indices.Length; m++ )
  162. {
  163. int index = mapping[p].indices[m];
  164. sverts[index].x += df.x;
  165. sverts[index].y += df.y;
  166. sverts[index].z += df.z;
  167. }
  168. }
  169. }
  170. }
  171. }
  172. }
  173. // Threaded version
  174. public override void PrepareMT(MegaModifiers mc, int cores)
  175. {
  176. }
  177. public override void DoWork(MegaModifiers mc, int index, int start, int end, int cores)
  178. {
  179. if ( index == 0 )
  180. Modify(mc);
  181. }
  182. }