MegaPathDeform.cs 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  1. using UnityEngine;
  2. using System.IO;
  3. [AddComponentMenu("Modifiers/Path Deform")]
  4. public class MegaPathDeform : MegaModifier
  5. {
  6. public float percent = 0.0f;
  7. public float stretch = 1.0f;
  8. public float twist = 0.0f;
  9. public float rotate = 0.0f;
  10. public MegaAxis axis = MegaAxis.X;
  11. public bool flip = false;
  12. public MegaShape path = null;
  13. public bool animate = false;
  14. public float speed = 1.0f;
  15. public bool drawpath = false;
  16. public float tangent = 1.0f;
  17. [HideInInspector]
  18. public Matrix4x4 mat = new Matrix4x4();
  19. public bool UseTwistCurve = false;
  20. public AnimationCurve twistCurve = new AnimationCurve(new Keyframe(0, 0), new Keyframe(1, 1));
  21. public bool UseStretchCurve = false;
  22. public AnimationCurve stretchCurve = new AnimationCurve(new Keyframe(0, 1), new Keyframe(1, 1));
  23. public override string ModName() { return "PathDeform"; }
  24. public override string GetHelpURL() { return "?page_id=273"; }
  25. public Vector3 Up = Vector3.up;
  26. public int curve = 0;
  27. public bool usedist = false;
  28. public float distance = 0.0f;
  29. public MegaLoopMode loopmode = MegaLoopMode.None;
  30. Vector3 start;
  31. Quaternion tw = Quaternion.identity;
  32. float usepercent;
  33. float usetan;
  34. float ovlen;
  35. public override Vector3 Map(int i, Vector3 p)
  36. {
  37. p = tm.MultiplyPoint3x4(p); // Dont need either, so saving 3 vector mat mults but gaining a mat mult
  38. float alpha;
  39. float tws = 0.0f;
  40. if ( UseStretchCurve )
  41. {
  42. float str = stretchCurve.Evaluate(Mathf.Repeat(p.z * ovlen + usepercent, 1.0f)) * stretch;
  43. alpha = (p.z * ovlen * str) + usepercent; //(percent / 100.0f); // can precalc this
  44. }
  45. else
  46. alpha = (p.z * ovlen * stretch) + usepercent; //(percent / 100.0f); // can precalc this
  47. Vector3 ps = path.InterpCurve3D(curve, alpha, path.normalizedInterp, ref tws) - start;
  48. Vector3 ps1 = path.InterpCurve3D(curve, alpha + usetan, path.normalizedInterp) - start;
  49. if ( path.splines[curve].closed )
  50. alpha = Mathf.Repeat(alpha, 1.0f);
  51. else
  52. alpha = Mathf.Clamp01(alpha);
  53. if ( UseTwistCurve )
  54. {
  55. float twst = twistCurve.Evaluate(alpha) * twist;
  56. tw = Quaternion.AngleAxis(twst + tws, Vector3.forward);
  57. }
  58. else
  59. tw = Quaternion.AngleAxis(tws + (twist * alpha), Vector3.forward);
  60. Vector3 relativePos = ps1 - ps;
  61. Quaternion rotation = Quaternion.LookRotation(relativePos, Up) * tw;
  62. //wtm.SetTRS(ps, rotation, Vector3.one);
  63. Matrix4x4 wtm = Matrix4x4.identity;
  64. MegaMatrix.SetTR(ref wtm, ps, rotation);
  65. wtm = mat * wtm;
  66. p.z = 0.0f;
  67. return wtm.MultiplyPoint3x4(p);
  68. }
  69. public override void ModStart(MegaModifiers mc)
  70. {
  71. }
  72. public override bool ModLateUpdate(MegaModContext mc)
  73. {
  74. if ( animate )
  75. {
  76. if ( Application.isPlaying )
  77. percent += speed * Time.deltaTime;
  78. if ( usedist )
  79. distance = percent * 0.01f * path.splines[curve].length;
  80. }
  81. return Prepare(mc);
  82. }
  83. public override bool Prepare(MegaModContext mc)
  84. {
  85. if ( path != null )
  86. {
  87. if ( curve >= path.splines.Count )
  88. curve = 0;
  89. if ( usedist )
  90. percent = distance / path.splines[curve].length * 100.0f;
  91. usepercent = percent / 100.0f;
  92. switch ( loopmode )
  93. {
  94. case MegaLoopMode.Clamp: usepercent = Mathf.Clamp01(usepercent); break;
  95. case MegaLoopMode.Loop: usepercent = Mathf.Repeat(usepercent, 1.0f); break;
  96. case MegaLoopMode.PingPong: usepercent = Mathf.PingPong(usepercent, 1.0f); break;
  97. }
  98. ovlen = (1.0f / path.splines[curve].length); // * stretch;
  99. usetan = (tangent * 0.01f);
  100. mat = Matrix4x4.identity;
  101. switch ( axis )
  102. {
  103. case MegaAxis.Z: MegaMatrix.RotateX(ref mat, -Mathf.PI * 0.5f); break;
  104. }
  105. MegaMatrix.RotateZ(ref mat, Mathf.Deg2Rad * rotate);
  106. SetAxis(mat);
  107. start = path.splines[curve].knots[0].p;
  108. Vector3 p1 = path.InterpCurve3D(0, 0.01f, path.normalizedInterp);
  109. Vector3 up = Vector3.zero;
  110. switch ( axis )
  111. {
  112. case MegaAxis.X: up = Vector3.left; break;
  113. case MegaAxis.Y: up = Vector3.back; break;
  114. case MegaAxis.Z: up = Vector3.up; break;
  115. }
  116. Quaternion lrot = Quaternion.identity;
  117. if ( flip )
  118. up = -up;
  119. lrot = Quaternion.FromToRotation(p1 - start, up);
  120. mat.SetTRS(Vector3.zero, lrot, Vector3.one);
  121. return true;
  122. }
  123. return false;
  124. }
  125. public void OnDrawGizmos()
  126. {
  127. if ( drawpath )
  128. Display(this);
  129. }
  130. // Mmm should be in gizmo code
  131. void Display(MegaPathDeform pd)
  132. {
  133. if ( pd.path != null )
  134. {
  135. // Need to do a lookat on first point to get the direction
  136. pd.mat = Matrix4x4.identity;
  137. Vector3 p = pd.path.splines[curve].knots[0].p;
  138. Vector3 p1 = pd.path.InterpCurve3D(curve, 0.01f, pd.path.normalizedInterp);
  139. Vector3 up = Vector3.zero;
  140. switch ( axis )
  141. {
  142. case MegaAxis.X: up = Vector3.left; break;
  143. case MegaAxis.Y: up = Vector3.back; break;
  144. case MegaAxis.Z: up = Vector3.up; break;
  145. }
  146. Quaternion lrot = Quaternion.identity;
  147. if ( flip )
  148. up = -up;
  149. lrot = Quaternion.FromToRotation(p1 - p, up);
  150. pd.mat.SetTRS(Vector3.zero, lrot, Vector3.one);
  151. Matrix4x4 mat = pd.transform.localToWorldMatrix * pd.mat;
  152. for ( int s = 0; s < pd.path.splines.Count; s++ )
  153. {
  154. float ldist = pd.path.stepdist * 0.1f;
  155. if ( ldist < 0.01f )
  156. ldist = 0.01f;
  157. float ds = pd.path.splines[s].length / (pd.path.splines[s].length / ldist);
  158. int c = 0;
  159. int k = -1;
  160. int lk = -1;
  161. Vector3 first = pd.path.splines[s].Interpolate(0.0f, pd.path.normalizedInterp, ref lk) - p;
  162. for ( float dist = ds; dist < pd.path.splines[s].length; dist += ds )
  163. {
  164. float alpha = dist / pd.path.splines[s].length;
  165. Vector3 pos = pd.path.splines[s].Interpolate(alpha, pd.path.normalizedInterp, ref k) - p;
  166. if ( (c & 1) == 1 )
  167. Gizmos.color = pd.path.col1;
  168. else
  169. Gizmos.color = pd.path.col2;
  170. if ( k != lk )
  171. {
  172. for ( lk = lk + 1; lk <= k; lk++ )
  173. {
  174. Gizmos.DrawLine(mat.MultiplyPoint(first), mat.MultiplyPoint(pd.path.splines[s].knots[lk].p - p));
  175. first = pd.path.splines[s].knots[lk].p - p;
  176. }
  177. }
  178. lk = k;
  179. Gizmos.DrawLine(mat.MultiplyPoint(first), mat.MultiplyPoint(pos));
  180. c++;
  181. first = pos;
  182. }
  183. if ( (c & 1) == 1 )
  184. Gizmos.color = pd.path.col1;
  185. else
  186. Gizmos.color = pd.path.col2;
  187. if ( pd.path.splines[s].closed )
  188. {
  189. Vector3 pos = pd.path.splines[s].Interpolate(0.0f, pd.path.normalizedInterp, ref k) - p;
  190. Gizmos.DrawLine(mat.MultiplyPoint(first), mat.MultiplyPoint(pos));
  191. }
  192. }
  193. Vector3 p0 = pd.path.InterpCurve3D(curve, (percent / 100.0f), pd.path.normalizedInterp) - p;
  194. p1 = pd.path.InterpCurve3D(curve, (percent / 100.0f) + (tangent * 0.01f), pd.path.normalizedInterp) - p;
  195. Gizmos.color = Color.blue;
  196. Vector3 sz = new Vector3(pd.path.KnotSize * 0.01f, pd.path.KnotSize * 0.01f, pd.path.KnotSize * 0.01f);
  197. Gizmos.DrawCube(mat.MultiplyPoint(p0), sz);
  198. Gizmos.DrawCube(mat.MultiplyPoint(p1), sz);
  199. }
  200. }
  201. public override void DrawGizmo(MegaModContext context)
  202. {
  203. if ( !Prepare(context) )
  204. return;
  205. Vector3 min = context.bbox.min;
  206. Vector3 max = context.bbox.max;
  207. if ( context.mod.sourceObj != null )
  208. Gizmos.matrix = context.mod.sourceObj.transform.localToWorldMatrix; // * gtm;
  209. else
  210. Gizmos.matrix = transform.localToWorldMatrix; // * gtm;
  211. corners[0] = new Vector3(min.x, min.y, min.z);
  212. corners[1] = new Vector3(min.x, max.y, min.z);
  213. corners[2] = new Vector3(max.x, max.y, min.z);
  214. corners[3] = new Vector3(max.x, min.y, min.z);
  215. corners[4] = new Vector3(min.x, min.y, max.z);
  216. corners[5] = new Vector3(min.x, max.y, max.z);
  217. corners[6] = new Vector3(max.x, max.y, max.z);
  218. corners[7] = new Vector3(max.x, min.y, max.z);
  219. DrawEdge(corners[0], corners[1]);
  220. DrawEdge(corners[1], corners[2]);
  221. DrawEdge(corners[2], corners[3]);
  222. DrawEdge(corners[3], corners[0]);
  223. DrawEdge(corners[4], corners[5]);
  224. DrawEdge(corners[5], corners[6]);
  225. DrawEdge(corners[6], corners[7]);
  226. DrawEdge(corners[7], corners[4]);
  227. DrawEdge(corners[0], corners[4]);
  228. DrawEdge(corners[1], corners[5]);
  229. DrawEdge(corners[2], corners[6]);
  230. DrawEdge(corners[3], corners[7]);
  231. ExtraGizmo(context);
  232. }
  233. }