MegaWaveWarp.cs 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. using UnityEngine;
  2. [AddComponentMenu("Modifiers/Warps/Wave")]
  3. public class MegaWaveWarp : MegaWarp
  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 bool animate = false;
  11. public float Speed = 1.0f;
  12. public int divs = 4;
  13. public int numSegs = 4;
  14. public int numSides = 4;
  15. float time = 0.0f;
  16. float dy = 0.0f;
  17. float dist = 0.0f;
  18. float t = 0.0f; // TODO: put t in modifier and have as general anim value, maybe even modifiers as a context
  19. public override string WarpName() { return "Wave"; }
  20. public override string GetIcon() { return "MegaWave icon.png"; }
  21. public override string GetHelpURL() { return "?page_id=380"; }
  22. public override Vector3 Map(int i, Vector3 p)
  23. {
  24. p = tm.MultiplyPoint3x4(p);
  25. Vector3 ip = p;
  26. float dist = p.magnitude;
  27. float dcy = Mathf.Exp(-totaldecay * Mathf.Abs(dist));
  28. float u = Mathf.Abs(2.0f * p.x / dist);
  29. u = u * u;
  30. p.z += flex * MegaUtils.WaveFunc(p.y, time, amp * (1.0f - u) + amp2 * u, wave, phase, dy);
  31. p = Vector3.Lerp(ip, p, dcy);
  32. return invtm.MultiplyPoint3x4(p);
  33. }
  34. void Update()
  35. {
  36. if ( animate )
  37. {
  38. if ( Application.isPlaying )
  39. t += Time.deltaTime * Speed;
  40. phase = t;
  41. }
  42. }
  43. public override bool Prepare(float decay)
  44. {
  45. tm = transform.worldToLocalMatrix;
  46. invtm = tm.inverse;
  47. dist = (wave / 10.0f) * 4.0f * 5.0f;
  48. if ( dist == 0.0f )
  49. dist = 1.0f;
  50. dy = Decay / 1000.0f;
  51. totaldecay = dy + decay;
  52. if ( totaldecay < 0.0f )
  53. totaldecay = 0.0f;
  54. return true;
  55. }
  56. void BuildMesh(float t)
  57. {
  58. Vector3 pos = Vector3.zero;
  59. Vector3 last = Vector3.zero;
  60. float Dy = wave / (float)divs;
  61. float Dx = Dy * 4;
  62. int den = (int)(Dx * numSides * 0.5f);
  63. float starty = -(float)numSegs / 2.0f * Dy;
  64. float startx = -(float)numSides / 2.0f * Dx;
  65. Gizmos.color = gCol1;
  66. for ( int i = 0; i <= numSides; i++ )
  67. {
  68. pos.x = startx + Dx * (float)i;
  69. float u = Mathf.Abs(pos.x / ((den != 0) ? den : 0.00001f));
  70. u = u * u;
  71. for ( int j = 0; j <= numSegs; j++ )
  72. {
  73. pos.y = starty + (float)j * Dy;
  74. pos.z = MegaUtils.WaveFunc(pos.y, t, amp * (1.0f - u) + amp2 * u, wave, phase, Decay / 1000.0f);
  75. if ( j > 0 )
  76. Gizmos.DrawLine(last, pos);
  77. last = pos;
  78. }
  79. }
  80. Gizmos.color = gCol2;
  81. for ( int j = 0; j <= numSegs; j++ )
  82. {
  83. pos.y = starty + (float)j * Dy;
  84. for ( int i = 0; i <= numSides; i++ )
  85. {
  86. pos.x = startx + Dx * (float)i;
  87. float u = Mathf.Abs(pos.x / ((den != 0) ? den : 0.00001f));
  88. u = u * u;
  89. pos.z = MegaUtils.WaveFunc(pos.y, t, amp * (1.0f - u) + amp2 * u, wave, phase, Decay / 1000.0f);
  90. if ( i > 0 )
  91. Gizmos.DrawLine(last, pos);
  92. last = pos;
  93. }
  94. }
  95. }
  96. public override void DrawGizmo(Color col)
  97. {
  98. SetGizCols(col.a);
  99. tm = Matrix4x4.identity;
  100. invtm = tm.inverse;
  101. if ( !Prepare(0.0f) )
  102. return;
  103. tm = tm * transform.localToWorldMatrix; // * tm;
  104. invtm = tm.inverse;
  105. Gizmos.matrix = transform.localToWorldMatrix;
  106. BuildMesh(t);
  107. }
  108. }