MegaSqueezeWarp.cs 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. using UnityEngine;
  2. [AddComponentMenu("Modifiers/Warps/Squeeze")]
  3. public class MegaSqueezeWarp : MegaWarp
  4. {
  5. public float amount = 0.0f;
  6. public float crv = 0.0f;
  7. public float radialamount = 0.0f;
  8. public float radialcrv = 0.0f;
  9. public bool doRegion = false;
  10. public float to = 0.0f;
  11. public float from = 0.0f;
  12. public MegaAxis axis = MegaAxis.Y;
  13. Matrix4x4 mat = new Matrix4x4();
  14. float k1;
  15. float k2;
  16. float k3;
  17. float k4;
  18. float l;
  19. float l2;
  20. float ovl;
  21. float ovl2;
  22. void SetK(float K1, float K2, float K3, float K4)
  23. {
  24. k1 = K1;
  25. k2 = K2;
  26. k3 = K3;
  27. k4 = K4;
  28. }
  29. public override string WarpName() { return "Squeeze"; }
  30. public override string GetIcon() { return "MegaStretch icon.png"; }
  31. public override string GetHelpURL() { return "?page_id=338"; }
  32. // Radial amount works on distance from pivot on the vertical axis, the lower the value the more effect
  33. // the other one works on distance from the vertical axis, the lower the value the more the effect
  34. public override Vector3 Map(int i, Vector3 p)
  35. {
  36. float z;
  37. p = tm.MultiplyPoint3x4(p);
  38. Vector3 ip = p;
  39. float dist = p.magnitude;
  40. float dcy = Mathf.Exp(-totaldecay * Mathf.Abs(dist));
  41. if ( l != 0.0f )
  42. {
  43. if ( doRegion )
  44. {
  45. if ( p.y < from )
  46. z = from * ovl;
  47. else
  48. {
  49. if ( p.y > to )
  50. z = to * ovl;
  51. else
  52. z = p.y * ovl;
  53. }
  54. }
  55. else
  56. z = Mathf.Abs(p.y * ovl);
  57. float f = 1.0f + z * k1 + k2 * z * (1.0f - z);
  58. p.y *= f;
  59. }
  60. if ( l2 != 0.0f )
  61. {
  62. float dist1 = Mathf.Sqrt(p.x * p.x + p.z * p.z);
  63. float xy = dist1 * ovl2;
  64. float f1 = 1.0f + xy * k3 + k4 * xy * (1.0f - xy);
  65. p.x *= f1;
  66. p.z *= f1;
  67. }
  68. p = Vector3.Lerp(ip, p, dcy);
  69. return invtm.MultiplyPoint3x4(p);
  70. }
  71. public override bool Prepare(float decay)
  72. {
  73. tm = transform.worldToLocalMatrix;
  74. invtm = tm.inverse;
  75. mat = Matrix4x4.identity;
  76. SetAxis(mat);
  77. SetK(amount, crv, radialamount, radialcrv);
  78. Vector3 size = Vector3.zero; //bbox.Size();
  79. size.x = Width;
  80. size.y = Height;
  81. size.z = Length;
  82. switch ( axis )
  83. {
  84. case MegaAxis.X:
  85. l = size[0]; //bbox.max[1] - bbox.min[1];
  86. l2 = Mathf.Sqrt(size[1] * size[1] + size[2] * size[2]);
  87. break;
  88. case MegaAxis.Y:
  89. l = size[1]; //bbox.max[1] - bbox.min[1];
  90. l2 = Mathf.Sqrt(size[0] * size[0] + size[2] * size[2]);
  91. break;
  92. case MegaAxis.Z:
  93. l = size[2]; //bbox.max[1] - bbox.min[1];
  94. l2 = Mathf.Sqrt(size[1] * size[1] + size[0] * size[0]);
  95. break;
  96. }
  97. if ( l != 0.0f )
  98. ovl = 1.0f / l;
  99. if ( l2 != 0.0f )
  100. ovl2 = 1.0f / l2;
  101. totaldecay = Decay + decay;
  102. if ( totaldecay < 0.0f )
  103. totaldecay = 0.0f;
  104. return true;
  105. }
  106. public override void ExtraGizmo()
  107. {
  108. if ( doRegion )
  109. DrawFromTo(MegaAxis.Z, from, to);
  110. }
  111. }