MegaRipple.cs 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. using UnityEngine;
  2. using System.IO;
  3. [AddComponentMenu("Modifiers/Ripple")]
  4. public class MegaRipple : MegaModifier
  5. {
  6. public float amp = 0.0f;
  7. public float amp2 = 0.0f;
  8. public float flex = 1.0f;
  9. public float wave = 1.0f;
  10. public float phase = 0.0f;
  11. public float Decay = 0.0f;
  12. public bool animate = false;
  13. public float Speed = 1.0f;
  14. public float radius = 1.0f;
  15. public int segments = 10;
  16. public int circles = 4;
  17. float time = 0.0f;
  18. float dy = 0.0f;
  19. public override string ModName() { return "Ripple"; }
  20. public override string GetHelpURL() { return "?page_id=308"; }
  21. public override Vector3 Map(int i, Vector3 p)
  22. {
  23. p = tm.MultiplyPoint3x4(p);
  24. float a;
  25. if ( amp != amp2 )
  26. {
  27. float len = p.magnitude;
  28. if ( len == 0.0f )
  29. a = amp;
  30. else
  31. {
  32. float u = (Mathf.Acos(p.x / len)) / Mathf.PI;
  33. u = (u > 0.5f) ? (1.0f - u) : u;
  34. u *= 2.0f;
  35. u = Mathf.SmoothStep(0.0f, 1.0f, u);
  36. a = amp * (1.0f - u) + amp2 * u;
  37. }
  38. }
  39. else
  40. a = amp;
  41. float oldZ = p.y;
  42. p.y = 0.0f;
  43. float r = p.magnitude;
  44. p.y = oldZ + flex * MegaUtils.WaveFunc(r, time, a, wave, phase, dy);
  45. return invtm.MultiplyPoint3x4(p);
  46. }
  47. float t = 0.0f;
  48. public override bool ModLateUpdate(MegaModContext mc)
  49. {
  50. if ( animate )
  51. {
  52. float dt = Time.deltaTime;
  53. if ( dt == 0.0f )
  54. dt = 0.01f;
  55. if ( Application.isPlaying )
  56. t += dt * Speed;
  57. phase = t;
  58. }
  59. return Prepare(mc);
  60. }
  61. public override bool Prepare(MegaModContext mc)
  62. {
  63. dy = Decay / 1000.0f;
  64. return true;
  65. }
  66. Vector3 GetPos(float u, float radius)
  67. {
  68. Vector3 pos = Vector3.zero;
  69. pos.x = radius * Mathf.Cos(u * Mathf.PI * 2.0f);
  70. pos.z = radius * Mathf.Sin(u * Mathf.PI * 2.0f);
  71. float u2 = (u > 0.5f) ? (u - 0.5f) : u;
  72. u2 = (u2 > 0.25f) ? (0.5f - u2) : u2;
  73. u2 = u2 * 4.0f;
  74. u2 = u2 * u2;
  75. pos.y = MegaUtils.WaveFunc(radius, t, amp * (1.0f - u2) + amp2 * u2, wave, phase, dy);
  76. return pos;
  77. }
  78. void MakeCircle(float t, float radius, float r1, float a1, float a2, float w, float s, float d, int numCircleSegs)
  79. {
  80. Vector3 last = Vector3.zero;
  81. Vector3 pos = Vector3.zero;
  82. Vector3 pos1 = Vector3.zero;
  83. Vector3 first = Vector3.zero;
  84. for ( int i = 0; i < numCircleSegs; i++ )
  85. {
  86. float u = (float)i / (float)numCircleSegs;
  87. pos = GetPos(u, radius);
  88. pos1 = GetPos(u, r1);
  89. if ( (i & 1) == 1 )
  90. {
  91. Gizmos.color = gizCol1;
  92. }
  93. else
  94. Gizmos.color = gizCol2;
  95. if ( i > 0 )
  96. {
  97. Gizmos.DrawLine(last, pos);
  98. }
  99. else
  100. first = pos;
  101. Gizmos.DrawLine(pos1, pos);
  102. last = pos;
  103. }
  104. Gizmos.DrawLine(last, first);
  105. }
  106. public override void DrawGizmo(MegaModContext context)
  107. {
  108. Gizmos.color = Color.yellow;
  109. Matrix4x4 gtm = Matrix4x4.identity;
  110. Vector3 pos = gizmoPos;
  111. pos.x = -pos.x;
  112. pos.y = -pos.y;
  113. pos.z = -pos.z;
  114. Vector3 scl = gizmoScale;
  115. scl.x = 1.0f - (scl.x - 1.0f);
  116. scl.y = 1.0f - (scl.y - 1.0f);
  117. gtm.SetTRS(pos, Quaternion.Euler(gizmoRot), scl);
  118. Gizmos.matrix = transform.localToWorldMatrix * gtm;
  119. float r1 = 0.0f;
  120. for ( int i = 0; i < circles; i++ )
  121. {
  122. float r = ((float)i / (float)circles) * radius;
  123. MakeCircle(t, r, r1, amp, amp2, wave, phase, dy, segments);
  124. r1 = r;
  125. }
  126. }
  127. }