MegaCurveDeform.cs 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. using UnityEngine;
  2. using System.Threading;
  3. [AddComponentMenu("Modifiers/Curve Deform")]
  4. public class MegaCurveDeform : MegaModifier
  5. {
  6. public MegaAxis axis = MegaAxis.X;
  7. public AnimationCurve defCurve = new AnimationCurve(new Keyframe(0.0f, 0.0f), new Keyframe(0.5f, 0.0f), new Keyframe(1.0f, 0.0f));
  8. public float MaxDeviation = 1.0f;
  9. float width = 0.0f;
  10. int ax;
  11. public float Pos = 0.0f;
  12. public bool UsePos = false;
  13. Keyframe key = new Keyframe();
  14. public override string ModName() { return "CurveDeform"; }
  15. public override string GetHelpURL() { return "?page_id=655"; }
  16. static object resourceLock = new object();
  17. public override void DoWork(MegaModifiers mc, int index, int start, int end, int cores)
  18. {
  19. if ( selection != null )
  20. {
  21. DoWorkWeighted(mc, index, start, end, cores);
  22. return;
  23. }
  24. for ( int i = start; i < end; i++ )
  25. sverts[i] = MapMT(i, verts[i]);
  26. }
  27. public override void DoWorkWeighted(MegaModifiers mc, int index, int start, int end, int cores)
  28. {
  29. for ( int i = start; i < end; i++ )
  30. {
  31. Vector3 p = verts[i];
  32. float w = selection[i]; //[(int)weightChannel];
  33. if ( w > 0.001f )
  34. {
  35. Vector3 mp = MapMT(i, verts[i]);
  36. sverts[i].x = p.x + (mp.x - p.x) * w;
  37. sverts[i].y = p.y + (mp.y - p.y) * w;
  38. sverts[i].z = p.z + (mp.z - p.z) * w;
  39. }
  40. else
  41. sverts[i] = p; //verts[i];
  42. }
  43. }
  44. public Vector3 MapMT(int i, Vector3 p)
  45. {
  46. p = tm.MultiplyPoint3x4(p);
  47. float alpha = (p[ax] - bbox.min[ax]) / width;
  48. if ( UsePos )
  49. alpha += Pos;
  50. Monitor.Enter(resourceLock);
  51. p.y += defCurve.Evaluate(alpha) * MaxDeviation;
  52. Monitor.Exit(resourceLock);
  53. return invtm.MultiplyPoint3x4(p);
  54. }
  55. public override Vector3 Map(int i, Vector3 p)
  56. {
  57. p = tm.MultiplyPoint3x4(p);
  58. float alpha = (p[ax] - bbox.min[ax]) / width;
  59. if ( UsePos )
  60. alpha += Pos;
  61. p.y += defCurve.Evaluate(alpha) * MaxDeviation;
  62. return invtm.MultiplyPoint3x4(p);
  63. }
  64. public override bool ModLateUpdate(MegaModContext mc)
  65. {
  66. return Prepare(mc);
  67. }
  68. public override bool Prepare(MegaModContext mc)
  69. {
  70. ax = (int)axis;
  71. width = bbox.max[ax] - bbox.min[ax];
  72. return true;
  73. }
  74. public float GetPos(float alpha)
  75. {
  76. float y = defCurve.Evaluate(alpha);
  77. return y;
  78. }
  79. public void SetKey(int index, float t, float v, float intan, float outtan)
  80. {
  81. key.time = t;
  82. key.value = v;
  83. key.inTangent = intan;
  84. key.outTangent = outtan;
  85. defCurve.MoveKey(index, key);
  86. }
  87. }