MegaBend.cs 3.7 KB

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