MegaBendWarp.cs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. using UnityEngine;
  2. [AddComponentMenu("Modifiers/Warps/Bend")]
  3. public class MegaBendWarp : MegaWarp
  4. {
  5. public float angle = 0.0f;
  6. public float dir = 0.0f;
  7. public MegaAxis axis = MegaAxis.X;
  8. public bool doRegion = false;
  9. public float from = 0.0f;
  10. public float to = 0.0f;
  11. Matrix4x4 mat = new Matrix4x4();
  12. Matrix4x4 tmAbove = new Matrix4x4();
  13. Matrix4x4 tmBelow = new Matrix4x4();
  14. float r = 0.0f;
  15. public override string WarpName() { return "Bend"; }
  16. public override string GetIcon() { return "MegaBend icon.png"; }
  17. public override string GetHelpURL() { return "?page_id=2551"; }
  18. void CalcR(MegaAxis axis, float ang)
  19. {
  20. float len = 0.0f;
  21. if ( !doRegion )
  22. {
  23. switch ( axis )
  24. {
  25. case MegaAxis.X: len = Width; break;
  26. case MegaAxis.Z: len = Height; break;
  27. case MegaAxis.Y: len = Length; break;
  28. }
  29. }
  30. else
  31. len = to - from;
  32. if ( Mathf.Abs(ang) < 0.000001f )
  33. r = 0.0f;
  34. else
  35. r = len / ang;
  36. }
  37. public override Vector3 Map(int i, Vector3 p)
  38. {
  39. if ( r == 0.0f && !doRegion )
  40. return p;
  41. p = tm.MultiplyPoint3x4(p); // tm may have an offset gizmo etc
  42. if ( doRegion )
  43. {
  44. if ( p.y <= from )
  45. return invtm.MultiplyPoint3x4(tmBelow.MultiplyPoint3x4(p));
  46. else
  47. {
  48. if ( p.y >= to )
  49. return invtm.MultiplyPoint3x4(tmAbove.MultiplyPoint3x4(p));
  50. }
  51. }
  52. if ( r == 0.0f )
  53. return invtm.MultiplyPoint3x4(p);
  54. Vector3 ip = p;
  55. float dist = p.magnitude;
  56. float dcy = Mathf.Exp(-totaldecay * Mathf.Abs(dist));
  57. float x = p.x;
  58. float y = p.y;
  59. float yr = y / r;
  60. float c = Mathf.Cos(Mathf.PI - yr);
  61. float s = Mathf.Sin(Mathf.PI - yr);
  62. float px = r * c + r - x * c;
  63. p.x = px;
  64. float pz = r * s - x * s;
  65. p.y = pz;
  66. p = Vector3.Lerp(ip, p, dcy);
  67. p = invtm.MultiplyPoint3x4(p);
  68. return p;
  69. }
  70. void Calc()
  71. {
  72. tm = transform.worldToLocalMatrix;
  73. invtm = tm.inverse;
  74. mat = Matrix4x4.identity;
  75. switch ( axis )
  76. {
  77. case MegaAxis.X: MegaMatrix.RotateZ(ref mat, Mathf.PI * 0.5f); break;
  78. case MegaAxis.Y: MegaMatrix.RotateX(ref mat, -Mathf.PI * 0.5f); break;
  79. case MegaAxis.Z: break;
  80. }
  81. MegaMatrix.RotateY(ref mat, Mathf.Deg2Rad * dir);
  82. SetAxis(mat);
  83. CalcR(axis, Mathf.Deg2Rad * -angle);
  84. if ( doRegion )
  85. {
  86. doRegion = false;
  87. float len = to - from;
  88. float rat1, rat2;
  89. if ( len == 0.0f )
  90. rat1 = rat2 = 1.0f;
  91. else
  92. {
  93. rat1 = to / len;
  94. rat2 = from / len;
  95. }
  96. Vector3 pt;
  97. tmAbove = Matrix4x4.identity;
  98. MegaMatrix.Translate(ref tmAbove, 0.0f, -to, 0.0f);
  99. MegaMatrix.RotateZ(ref tmAbove, -Mathf.Deg2Rad * angle * rat1);
  100. MegaMatrix.Translate(ref tmAbove, 0.0f, to, 0.0f);
  101. pt = new Vector3(0.0f, to, 0.0f);
  102. MegaMatrix.Translate(ref tmAbove, tm.MultiplyPoint3x4(Map(0, invtm.MultiplyPoint3x4(pt))) - pt);
  103. tmBelow = Matrix4x4.identity;
  104. MegaMatrix.Translate(ref tmBelow, 0.0f, -from, 0.0f);
  105. MegaMatrix.RotateZ(ref tmBelow, -Mathf.Deg2Rad * angle * rat2);
  106. MegaMatrix.Translate(ref tmBelow, 0.0f, from, 0.0f);
  107. pt = new Vector3(0.0f, from, 0.0f);
  108. MegaMatrix.Translate(ref tmBelow, tm.MultiplyPoint3x4(Map(0, invtm.MultiplyPoint3x4(pt))) - pt);
  109. doRegion = true;
  110. }
  111. }
  112. public override bool Prepare(float decay)
  113. {
  114. Calc();
  115. totaldecay = Decay + decay;
  116. if ( totaldecay < 0.0f )
  117. totaldecay = 0.0f;
  118. return true;
  119. }
  120. public override void ExtraGizmo()
  121. {
  122. if ( doRegion )
  123. DrawFromTo(axis, from, to);
  124. }
  125. }