MegaWavingWarp.cs 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. using UnityEngine;
  2. [AddComponentMenu("Modifiers/Warps/Waving")]
  3. public class MegaWavingWarp : MegaWarp
  4. {
  5. public float amp = 0.01f;
  6. public float flex = 1.0f;
  7. public float wave = 1.0f;
  8. public float phase = 0.0f;
  9. public bool animate = false;
  10. public float Speed = 1.0f;
  11. public MegaAxis waveaxis = MegaAxis.X;
  12. float time = 0.0f;
  13. float dy = 0.0f;
  14. int ix = 0;
  15. int iz = 2;
  16. float t = 0.0f;
  17. public override string WarpName() { return "Waving"; }
  18. public override string GetHelpURL() { return "?page_id=308"; }
  19. static public float WaveFunc(float radius, float t, float amp, float waveLen, float phase, float decay)
  20. {
  21. float ang = Mathf.PI * 2.0f * (radius / waveLen + phase);
  22. return amp * Mathf.Sin(ang) * Mathf.Exp(-decay * Mathf.Abs(radius));
  23. }
  24. public override Vector3 Map(int i, Vector3 p)
  25. {
  26. p = tm.MultiplyPoint3x4(p);
  27. float u = Mathf.Abs(2.0f * p[iz]); //.z); // / dist);
  28. u = u * u;
  29. p[ix] += flex * WaveFunc(p[iz], time, amp * u, wave, phase, totaldecay); //dy);
  30. return invtm.MultiplyPoint3x4(p);
  31. }
  32. void Update()
  33. {
  34. if ( animate )
  35. {
  36. float dt = Time.deltaTime;
  37. if ( dt == 0.0f )
  38. dt = 0.01f;
  39. if ( Application.isPlaying )
  40. t += dt * Speed;
  41. phase = t;
  42. }
  43. }
  44. public override bool Prepare(float decay)
  45. {
  46. tm = transform.worldToLocalMatrix;
  47. invtm = tm.inverse;
  48. if ( wave == 0.0f )
  49. wave = 0.000001f;
  50. dy = Decay / 1000.0f;
  51. totaldecay = dy + decay;
  52. if ( totaldecay < 0.0f )
  53. totaldecay = 0.0f;
  54. switch ( waveaxis )
  55. {
  56. case MegaAxis.X:
  57. ix = 0;
  58. iz = 2;
  59. break;
  60. case MegaAxis.Y:
  61. ix = 1;
  62. iz = 2;
  63. break;
  64. case MegaAxis.Z:
  65. ix = 2;
  66. iz = 0;
  67. break;
  68. }
  69. return true;
  70. }
  71. }