MegaGlobe.cs 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. using UnityEngine;
  2. [AddComponentMenu("Modifiers/Globe")]
  3. public class MegaGlobe : MegaModifier
  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 ModName() { return "Globe"; }
  19. public override string GetHelpURL() { return "?page_id=3752"; }
  20. // Virtual method for all mods
  21. public override void SetValues(MegaModifier mod)
  22. {
  23. MegaBend bm = (MegaBend)mod;
  24. dir = bm.dir;
  25. axis = bm.axis;
  26. }
  27. public override Vector3 Map(int i, Vector3 p)
  28. {
  29. if ( r == 0.0f )
  30. return p;
  31. p = tm.MultiplyPoint3x4(p); // tm may have an offset gizmo etc
  32. float x = p.x;
  33. float y = p.y;
  34. float yr = (y / r); // * amplify;
  35. float c = Mathf.Cos(Mathf.PI - yr);
  36. float s = Mathf.Sin(Mathf.PI - yr);
  37. float px = r * c + r - x * c;
  38. p.x = px;
  39. float pz = r * s - x * s;
  40. p.y = pz;
  41. p = invtm.MultiplyPoint3x4(p);
  42. if ( twoaxis )
  43. {
  44. p = tm1.MultiplyPoint3x4(p); // tm may have an offset gizmo etc
  45. x = p.x;
  46. y = p.y;
  47. yr = (y / r1); // * amplify;
  48. c = Mathf.Cos(Mathf.PI - yr);
  49. s = Mathf.Sin(Mathf.PI - yr);
  50. px = r1 * c + r1 - x * c;
  51. p.x = px;
  52. pz = r1 * s - x * s;
  53. p.y = pz;
  54. p = invtm1.MultiplyPoint3x4(p);
  55. }
  56. return p;
  57. }
  58. void Calc()
  59. {
  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 ModLateUpdate(MegaModContext mc)
  89. {
  90. return Prepare(mc);
  91. }
  92. public override bool Prepare(MegaModContext mc)
  93. {
  94. Calc();
  95. return true;
  96. }
  97. }