MegaWave.cs 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. using UnityEngine;
  2. [AddComponentMenu("Modifiers/Wave")]
  3. public class MegaWave : MegaModifier
  4. {
  5. public float amp = 0.0f;
  6. public float amp2 = 0.0f;
  7. public float flex = 1.0f;
  8. public float wave = 1.0f;
  9. public float phase = 0.0f;
  10. public float Decay = 0.0f;
  11. public float dir = 0.0f;
  12. public bool animate = false;
  13. public float Speed = 1.0f;
  14. public int divs = 4;
  15. public int numSegs = 4;
  16. public int numSides = 4;
  17. float time = 0.0f;
  18. float dy = 0.0f;
  19. float dist = 0.0f;
  20. public override string ModName() { return "Wave"; }
  21. public override string GetHelpURL() { return "?page_id=357"; }
  22. public override Vector3 Map(int i, Vector3 p)
  23. {
  24. p = tm.MultiplyPoint3x4(p);
  25. float u = Mathf.Abs(2.0f * p.x / dist);
  26. u = u * u;
  27. p.z += flex * MegaUtils.WaveFunc(p.y, time, amp * (1.0f - u) + amp2 * u, wave, phase, dy);
  28. return invtm.MultiplyPoint3x4(p);
  29. }
  30. float t = 0.0f; // TODO: put t in modifier and have as general anim value, maybe even modifiers as a context
  31. Matrix4x4 mat = new Matrix4x4();
  32. public override bool ModLateUpdate(MegaModContext mc)
  33. {
  34. if ( animate )
  35. {
  36. if ( Application.isPlaying )
  37. t += Time.deltaTime * Speed;
  38. phase = t;
  39. }
  40. return Prepare(mc);
  41. }
  42. public override bool Prepare(MegaModContext mc)
  43. {
  44. mat = Matrix4x4.identity;
  45. MegaMatrix.RotateZ(ref mat, Mathf.Deg2Rad * dir);
  46. SetAxis(mat);
  47. dy = Decay / 1000.0f;
  48. dist = (wave / 10.0f) * 4.0f * 5.0f; //float(numSides);
  49. if ( dist == 0.0f )
  50. dist = 1.0f;
  51. return true;
  52. }
  53. void BuildMesh(float t)
  54. {
  55. Vector3 pos = Vector3.zero;
  56. Vector3 last = Vector3.zero;
  57. float Dy = wave / (float)divs;
  58. float Dx = Dy * 4;
  59. int den = (int)(Dx * numSides * 0.5f);
  60. float starty = -(float)numSegs / 2.0f * Dy;
  61. float startx = -(float)numSides / 2.0f * Dx;
  62. for ( int i = 0; i <= numSides; i++ )
  63. {
  64. pos.x = startx + Dx * (float)i;
  65. float u = Mathf.Abs(pos.x / ((den != 0) ? den : 0.00001f));
  66. u = u * u;
  67. for ( int j = 0; j <= numSegs; j++ )
  68. {
  69. pos.y = starty + (float)j * Dy;
  70. pos.z = MegaUtils.WaveFunc(pos.y, t, amp * (1.0f - u) + amp2 * u, wave, phase, Decay / 1000.0f);
  71. if ( j > 0 )
  72. Gizmos.DrawLine(last, pos);
  73. last = pos;
  74. }
  75. }
  76. for ( int j = 0; j <= numSegs; j++ )
  77. {
  78. pos.y = starty + (float)j * Dy;
  79. for ( int i = 0; i <= numSides; i++ )
  80. {
  81. pos.x = startx + Dx * (float)i;
  82. float u = Mathf.Abs(pos.x / ((den != 0) ? den : 0.00001f));
  83. u = u * u;
  84. pos.z = MegaUtils.WaveFunc(pos.y, t, amp * (1.0f - u) + amp2 * u, wave, phase, Decay / 1000.0f);
  85. if ( i > 0 )
  86. Gizmos.DrawLine(last, pos);
  87. last = pos;
  88. }
  89. }
  90. }
  91. public override void DrawGizmo(MegaModContext context)
  92. {
  93. Gizmos.color = Color.yellow;
  94. Matrix4x4 gtm = Matrix4x4.identity;
  95. Vector3 pos = gizmoPos;
  96. pos.x = -pos.x;
  97. pos.y = -pos.y;
  98. pos.z = -pos.z;
  99. Vector3 scl = gizmoScale;
  100. scl.x = 1.0f - (scl.x - 1.0f);
  101. scl.y = 1.0f - (scl.y - 1.0f);
  102. gtm.SetTRS(pos, Quaternion.Euler(gizmoRot), scl);
  103. Gizmos.matrix = transform.localToWorldMatrix * gtm;
  104. BuildMesh(t);
  105. }
  106. }