MegaTwist.cs 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. using UnityEngine;
  2. [AddComponentMenu("Modifiers/Twist")]
  3. public class MegaTwist : MegaModifier
  4. {
  5. public float angle = 0.0f;
  6. public bool doRegion = false;
  7. public float from = 0.0f;
  8. public float to = 0.0f;
  9. public float Bias = 0.0f;
  10. public MegaAxis axis = MegaAxis.X;
  11. bool doBias = false;
  12. float height = 0.0f;
  13. float angleOverHeight = 0.0f;
  14. float theAngle;
  15. float bias;
  16. Matrix4x4 mat = new Matrix4x4();
  17. public override string ModName() { return "Twist"; }
  18. public override string GetHelpURL() { return "?page_id=341"; }
  19. void CalcHeight(MegaAxis axis, float angle, MegaBox3 bbx)
  20. {
  21. switch ( axis )
  22. {
  23. case MegaAxis.X: height = bbx.max.x - bbx.min.x; break;
  24. case MegaAxis.Z: height = bbx.max.y - bbx.min.y; break;
  25. case MegaAxis.Y: height = bbx.max.z - bbx.min.z; break;
  26. }
  27. if ( height == 0.0f )
  28. {
  29. theAngle = 0.0f;
  30. angleOverHeight = 0.0f;
  31. }
  32. else
  33. {
  34. theAngle = angle;
  35. angleOverHeight = angle / height;
  36. }
  37. }
  38. public override Vector3 Map(int i, Vector3 p)
  39. {
  40. float z, a;
  41. if ( theAngle == 0.0f )
  42. return p;
  43. p = tm.MultiplyPoint3x4(p);
  44. float x = p.x;
  45. float y = p.z;
  46. if ( doRegion )
  47. {
  48. if ( p.y < from )
  49. z = from;
  50. else
  51. {
  52. if ( p.y > to )
  53. z = to;
  54. else
  55. z = p.y;
  56. }
  57. }
  58. else
  59. z = p.y;
  60. if ( doBias )
  61. {
  62. float u = z / height;
  63. a = theAngle * (float)Mathf.Pow(Mathf.Abs(u), bias);
  64. if ( u < 0.0f )
  65. a = -a;
  66. }
  67. else
  68. a = z * angleOverHeight;
  69. float cosine = Mathf.Cos(Mathf.Deg2Rad * a);
  70. float sine = Mathf.Sin(Mathf.Deg2Rad * a);
  71. p.x = cosine * x + sine * y;
  72. p.z = -sine * x + cosine * y;
  73. p = invtm.MultiplyPoint3x4(p);
  74. return p;
  75. }
  76. public override bool ModLateUpdate(MegaModContext mc)
  77. {
  78. return Prepare(mc);
  79. }
  80. public override bool Prepare(MegaModContext mc)
  81. {
  82. mat = Matrix4x4.identity;
  83. switch ( axis )
  84. {
  85. case MegaAxis.X: MegaMatrix.RotateZ(ref mat, Mathf.PI * 0.5f); break;
  86. case MegaAxis.Y: MegaMatrix.RotateX(ref mat, -Mathf.PI * 0.5f); break;
  87. case MegaAxis.Z: break;
  88. }
  89. SetAxis(mat);
  90. if ( Bias != 0.0f )
  91. {
  92. bias = 1.0f - (Bias + 100.0f) / 200.0f;
  93. if ( bias < 0.00001f )
  94. bias = 0.00001f;
  95. if ( bias > 0.99999f )
  96. bias = 0.99999f;
  97. bias = Mathf.Log(bias) / Mathf.Log(0.5f);
  98. doBias = true;
  99. }
  100. else
  101. {
  102. bias = 1.0f;
  103. doBias = false;
  104. }
  105. if ( from > to ) from = to;
  106. if ( to < from ) to = from;
  107. CalcHeight(axis, angle, mc.bbox);
  108. return true;
  109. }
  110. public override void ExtraGizmo(MegaModContext mc)
  111. {
  112. if ( doRegion )
  113. DrawFromTo(axis, from, to, mc);
  114. }
  115. }