MegaTwistWarp.cs 2.7 KB

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