MegaTaperWarp.cs 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. 
  2. using UnityEngine;
  3. [AddComponentMenu("Modifiers/Warps/Taper")]
  4. public class MegaTaperWarp : MegaWarp
  5. {
  6. public override string WarpName() { return "Taper"; }
  7. public override string GetIcon() { return "MegaTaper icon.png"; }
  8. public override string GetHelpURL() { return "?page_id=2566"; }
  9. public float amount = 0.0f;
  10. public bool doRegion = false;
  11. public float to = 0.0f;
  12. public float from = 0.0f;
  13. public float dir = 0.0f;
  14. public MegaAxis axis = MegaAxis.X;
  15. public MegaEffectAxis EAxis = MegaEffectAxis.X;
  16. Matrix4x4 mat = new Matrix4x4();
  17. public float crv = 0.0f;
  18. public bool sym = false;
  19. bool doX = false;
  20. bool doY = false;
  21. float k1;
  22. float k2;
  23. float l;
  24. void SetK(float K1, float K2)
  25. {
  26. k1 = K1;
  27. k2 = K2;
  28. }
  29. public override Vector3 Map(int i, Vector3 p)
  30. {
  31. float z;
  32. if ( l == 0.0f )
  33. return p;
  34. p = tm.MultiplyPoint3x4(p);
  35. Vector3 ip = p;
  36. float dist = p.magnitude;
  37. float dcy = Mathf.Exp(-totaldecay * Mathf.Abs(dist));
  38. if ( doRegion )
  39. {
  40. if ( p.y < from )
  41. z = from / l;
  42. else
  43. {
  44. if ( p.y > to )
  45. z = to / l;
  46. else
  47. z = p.y / l;
  48. }
  49. }
  50. else
  51. z = p.y / l;
  52. if ( sym && z < 0.0f )
  53. z = -z;
  54. float f = 1.0f + z * k1 + k2 * z * (1.0f - z);
  55. if ( doX )
  56. p.x *= f;
  57. if ( doY )
  58. p.z *= f;
  59. p = Vector3.Lerp(ip, p, dcy);
  60. return invtm.MultiplyPoint3x4(p);
  61. }
  62. public override bool Prepare(float decay)
  63. {
  64. tm = transform.worldToLocalMatrix;
  65. invtm = tm.inverse;
  66. switch ( EAxis )
  67. {
  68. case MegaEffectAxis.X: doX = true; doY = false; break;
  69. case MegaEffectAxis.Y: doX = false; doY = true; break;
  70. case MegaEffectAxis.XY: doX = true; doY = true; break;
  71. }
  72. mat = Matrix4x4.identity;
  73. switch ( axis )
  74. {
  75. case MegaAxis.X: MegaMatrix.RotateZ(ref mat, Mathf.PI * 0.5f); l = Width; break;
  76. case MegaAxis.Y: MegaMatrix.RotateX(ref mat, -Mathf.PI * 0.5f); l = Length; break;
  77. case MegaAxis.Z: l = Height; break;
  78. }
  79. MegaMatrix.RotateY(ref mat, Mathf.Deg2Rad * dir);
  80. SetAxis(mat);
  81. SetK(amount, crv);
  82. totaldecay = Decay + decay;
  83. if ( totaldecay < 0.0f )
  84. totaldecay = 0.0f;
  85. return true;
  86. }
  87. public override void ExtraGizmo()
  88. {
  89. if ( doRegion )
  90. DrawFromTo(axis, from, to);
  91. }
  92. }