MegaStretch.cs 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. using UnityEngine;
  2. [AddComponentMenu("Modifiers/Stretch")]
  3. public class MegaStretch : MegaModifier
  4. {
  5. public float amount = 0.0f;
  6. public bool doRegion = false;
  7. public float to = 0.0f;
  8. public float from = 0.0f;
  9. public float amplify = 0.0f;
  10. public MegaAxis axis = MegaAxis.X;
  11. float heightMax = 0.0f;
  12. float heightMin = 0.0f;
  13. float amplifier = 0.0f;
  14. Matrix4x4 mat = new Matrix4x4();
  15. public bool useheightaxis = false;
  16. public MegaAxis axis1 = MegaAxis.X;
  17. float ovh = 0.0f;
  18. public override string ModName() { return "Stretch"; }
  19. public override string GetHelpURL() { return "?page_id=334"; }
  20. void CalcBulge(MegaAxis axis, float stretch, float amplify)
  21. {
  22. amount = stretch;
  23. amplifier = (amplify >= 0.0f) ? amplify + 1.0f : 1.0f / (-amplify + 1.0f);
  24. if ( !doRegion )
  25. {
  26. MegaAxis ax = axis;
  27. switch ( ax )
  28. {
  29. case MegaAxis.X:
  30. heightMin = bbox.min.x;
  31. heightMax = bbox.max.x;
  32. break;
  33. case MegaAxis.Z:
  34. heightMin = bbox.min.y;
  35. heightMax = bbox.max.y;
  36. break;
  37. case MegaAxis.Y:
  38. heightMin = bbox.min.z;
  39. heightMax = bbox.max.z;
  40. break;
  41. }
  42. }
  43. else
  44. {
  45. heightMin = from;
  46. heightMax = to;
  47. }
  48. ovh = 1.0f / (heightMax - heightMin);
  49. }
  50. public override Vector3 Map(int i, Vector3 p)
  51. {
  52. float normHeight;
  53. float xyScale, zScale;
  54. if ( amount == 0.0f || (heightMax - heightMin == 0) )
  55. return p;
  56. if ( (doRegion) && (to - from == 0.0f) )
  57. return p;
  58. p = tm.MultiplyPoint3x4(p);
  59. if ( doRegion && p.y > to )
  60. normHeight = (to - heightMin) * ovh; /// (heightMax - heightMin);
  61. else if ( doRegion && p.y < from )
  62. normHeight = (from - heightMin) * ovh; /// (heightMax - heightMin);
  63. else
  64. normHeight = (p.y - heightMin) * ovh; /// (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 fraction = (((a * normHeight) - a) * normHeight) + 1.0f;
  77. p.x *= fraction;
  78. p.z *= fraction;
  79. if ( doRegion && p.y < from )
  80. p.y += (zScale - 1.0f) * from;
  81. else if ( doRegion && p.y <= to )
  82. p.y *= zScale;
  83. else if ( doRegion && p.y > to )
  84. p.y += (zScale - 1.0f) * to;
  85. else
  86. p.y *= zScale;
  87. p = invtm.MultiplyPoint3x4(p);
  88. return p;
  89. }
  90. public override bool ModLateUpdate(MegaModContext mc)
  91. {
  92. return Prepare(mc);
  93. }
  94. public override bool Prepare(MegaModContext mc)
  95. {
  96. mat = Matrix4x4.identity;
  97. switch ( axis )
  98. {
  99. case MegaAxis.X: MegaMatrix.RotateZ(ref mat, Mathf.PI * 0.5f); break;
  100. case MegaAxis.Y: MegaMatrix.RotateX(ref mat, -Mathf.PI * 0.5f); break;
  101. case MegaAxis.Z: break;
  102. }
  103. SetAxis(mat);
  104. CalcBulge(axis, amount, amplify);
  105. return true;
  106. }
  107. public override void ExtraGizmo(MegaModContext mc)
  108. {
  109. if ( doRegion )
  110. DrawFromTo(axis, from, to, mc);
  111. }
  112. }