MegaSkewWarp.cs 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. 
  2. using UnityEngine;
  3. [AddComponentMenu("Modifiers/Warps/Skew")]
  4. public class MegaSkewWarp : 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 dir = 0.0f;
  11. public MegaAxis axis = MegaAxis.X;
  12. Matrix4x4 mat = new Matrix4x4();
  13. float amountOverLength = 0.0f;
  14. public override string WarpName() { return "Skew"; }
  15. public override string GetIcon() { return "MegaSkew icon.png"; }
  16. public override string GetHelpURL() { return "?page_id=2571"; }
  17. public override Vector3 Map(int i, Vector3 p)
  18. {
  19. p = tm.MultiplyPoint3x4(p);
  20. Vector3 ip = p;
  21. float dist = p.magnitude;
  22. float dcy = Mathf.Exp(-totaldecay * Mathf.Abs(dist));
  23. float z = p.y;
  24. if ( doRegion )
  25. {
  26. if ( p.y < from )
  27. z = from;
  28. else
  29. if ( p.y > to )
  30. z = to;
  31. }
  32. p.x -= z * amountOverLength;
  33. p = Vector3.Lerp(ip, p, dcy);
  34. return invtm.MultiplyPoint3x4(p);
  35. }
  36. public override bool Prepare(float decay)
  37. {
  38. tm = transform.worldToLocalMatrix;
  39. invtm = tm.inverse;
  40. if ( from > 0.0f )
  41. from = 0.0f;
  42. if ( to < 0.0f )
  43. to = 0.0f;
  44. mat = Matrix4x4.identity;
  45. switch ( axis )
  46. {
  47. case MegaAxis.X: MegaMatrix.RotateZ(ref mat, Mathf.PI * 0.5f); break;
  48. case MegaAxis.Y: MegaMatrix.RotateX(ref mat, -Mathf.PI * 0.5f); break;
  49. case MegaAxis.Z: break;
  50. }
  51. MegaMatrix.RotateY(ref mat, Mathf.Deg2Rad * dir);
  52. SetAxis(mat);
  53. float len = 0.0f;
  54. if ( !doRegion )
  55. {
  56. switch ( axis )
  57. {
  58. case MegaAxis.X: len = Width; break;
  59. case MegaAxis.Z: len = Height; break;
  60. case MegaAxis.Y: len = Length; break;
  61. }
  62. }
  63. else
  64. len = to - from;
  65. if ( len == 0.0f )
  66. len = 0.000001f;
  67. amountOverLength = amount / len;
  68. totaldecay = Decay + decay;
  69. if ( totaldecay < 0.0f )
  70. totaldecay = 0.0f;
  71. return true;
  72. }
  73. public override void ExtraGizmo()
  74. {
  75. if ( doRegion )
  76. DrawFromTo(axis, from, to);
  77. }
  78. }