MegaZStretchWarp.cs 3.0 KB

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