MegaGlobeWarp.cs 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. using UnityEngine;
  2. [AddComponentMenu("Modifiers/Warps/Globe")]
  3. public class MegaGlobeWarp : MegaWarp
  4. {
  5. public float dir = -90.0f;
  6. public float dir1 = -90.0f;
  7. public MegaAxis axis = MegaAxis.X;
  8. public MegaAxis axis1 = MegaAxis.Z;
  9. Matrix4x4 mat = new Matrix4x4();
  10. public bool twoaxis = true;
  11. Matrix4x4 tm1 = new Matrix4x4();
  12. Matrix4x4 invtm1 = new Matrix4x4();
  13. public float r = 0.0f;
  14. public float r1 = 0.0f;
  15. public float radius = 10.0f;
  16. public bool linkRadii = true;
  17. public float radius1 = 10.0f;
  18. public override string WarpName() { return "Globe"; }
  19. public override string GetHelpURL() { return "?page_id=3752"; }
  20. public override Vector3 Map(int i, Vector3 p)
  21. {
  22. if ( r == 0.0f )
  23. return p;
  24. p = tm.MultiplyPoint3x4(p); // tm may have an offset gizmo etc
  25. Vector3 ip = p;
  26. float dist = p.magnitude;
  27. float dcy = Mathf.Exp(-totaldecay * Mathf.Abs(dist));
  28. float x = p.x;
  29. float y = p.y;
  30. float yr = (y / r); // * amplify;
  31. float c = Mathf.Cos(Mathf.PI - yr);
  32. float s = Mathf.Sin(Mathf.PI - yr);
  33. float px = r * c + r - x * c;
  34. p.x = px;
  35. float pz = r * s - x * s;
  36. p.y = pz;
  37. p = Vector3.Lerp(ip, p, dcy);
  38. p = invtm.MultiplyPoint3x4(p);
  39. if ( twoaxis )
  40. {
  41. p = tm1.MultiplyPoint3x4(p); // tm may have an offset gizmo etc
  42. x = p.x;
  43. y = p.y;
  44. yr = (y / r1); // * amplify;
  45. c = Mathf.Cos(Mathf.PI - yr);
  46. s = Mathf.Sin(Mathf.PI - yr);
  47. px = r1 * c + r1 - x * c;
  48. p.x = px;
  49. pz = r1 * s - x * s;
  50. p.y = pz;
  51. p = Vector3.Lerp(ip, p, dcy);
  52. p = invtm1.MultiplyPoint3x4(p);
  53. }
  54. return p;
  55. }
  56. void Calc()
  57. {
  58. tm = transform.worldToLocalMatrix;
  59. invtm = tm.inverse;
  60. mat = Matrix4x4.identity;
  61. tm1 = tm;
  62. invtm1 = invtm;
  63. switch ( axis )
  64. {
  65. case MegaAxis.X: MegaMatrix.RotateZ(ref mat, Mathf.PI * 0.5f); break;
  66. case MegaAxis.Y: MegaMatrix.RotateX(ref mat, -Mathf.PI * 0.5f); break;
  67. case MegaAxis.Z: break;
  68. }
  69. MegaMatrix.RotateY(ref mat, Mathf.Deg2Rad * dir);
  70. SetAxis(mat);
  71. mat = Matrix4x4.identity;
  72. switch ( axis1 )
  73. {
  74. case MegaAxis.X: MegaMatrix.RotateZ(ref mat, Mathf.PI * 0.5f); break;
  75. case MegaAxis.Y: MegaMatrix.RotateX(ref mat, -Mathf.PI * 0.5f); break;
  76. case MegaAxis.Z: break;
  77. }
  78. MegaMatrix.RotateY(ref mat, Mathf.Deg2Rad * dir1);
  79. Matrix4x4 itm = mat.inverse;
  80. tm1 = mat * tm1;
  81. invtm1 = invtm1 * itm;
  82. r = -radius;
  83. if ( linkRadii )
  84. r1 = -radius;
  85. else
  86. r1 = -radius1;
  87. }
  88. public override bool Prepare(float decay)
  89. {
  90. Calc();
  91. totaldecay = Decay + decay;
  92. if ( totaldecay < 0.0f )
  93. totaldecay = 0.0f;
  94. return true;
  95. }
  96. }