MegaVertNoise.cs 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. using UnityEngine;
  2. [AddComponentMenu("Modifiers/Vertical Noise")]
  3. public class MegaVertNoise : MegaModifier
  4. {
  5. public float Scale = 1.0f;
  6. public bool Fractal = false;
  7. public float Freq = 0.25f;
  8. public float Iterations = 6.0f;
  9. public bool Animate = false;
  10. public float Phase = 0.0f;
  11. public float Rough = 0.0f;
  12. public float Strength = 0.0f;
  13. MegaPerlin iperlin = MegaPerlin.Instance;
  14. float time = 0.0f;
  15. float scale;
  16. float rt;
  17. public float decay = 0.0f;
  18. public override string ModName() { return "Vertical Noise"; }
  19. public override string GetHelpURL() { return "?page_id=1063"; }
  20. public override void Modify(MegaModifiers mc)
  21. {
  22. for ( int i = 0; i < verts.Length; i++ )
  23. {
  24. Vector3 p = tm.MultiplyPoint3x4(verts[i]);
  25. float ipy = p.y;
  26. float spx = p.x * scale + 0.5f; //half;
  27. float spz = p.z * scale + 0.5f; //half;
  28. float dist = Mathf.Sqrt(p.x * p.x + p.z * p.z);
  29. float dcy = Mathf.Exp(-decay * Mathf.Abs(dist));
  30. float dy = 0.0f;
  31. if ( Fractal )
  32. dy = iperlin.fBm1(spx, spz, time, rt, 2.0f, Iterations);
  33. else
  34. dy = iperlin.Noise(spx, spz, time);
  35. p.y += dy * Strength;
  36. p.y = ipy + ((p.y - ipy) * dcy);
  37. sverts[i] = invtm.MultiplyPoint3x4(p);
  38. }
  39. }
  40. public override void ModStart(MegaModifiers mc)
  41. {
  42. }
  43. public override Vector3 Map(int i, Vector3 p)
  44. {
  45. p = tm.MultiplyPoint3x4(p);
  46. float ipy = p.y;
  47. float dist = Mathf.Sqrt(p.x * p.x + p.z * p.z);
  48. float dcy = Mathf.Exp(-decay * Mathf.Abs(dist));
  49. float spx = p.x * scale + 0.5f;
  50. float spz = p.z * scale + 0.5f;
  51. float dy = 0.0f;
  52. if ( Fractal )
  53. dy = iperlin.fBm1(spx, spz, time, rt, 2.0f, Iterations);
  54. else
  55. dy = iperlin.Noise(spx, spz, time);
  56. p.y += dy * Strength;
  57. p.y = ipy + ((p.y - ipy) * dcy);
  58. return invtm.MultiplyPoint3x4(p); // + Vector3.Scale(d, Strength));
  59. }
  60. public override bool ModLateUpdate(MegaModContext mc)
  61. {
  62. if ( Animate )
  63. {
  64. if ( Application.isPlaying )
  65. Phase += Time.deltaTime * Freq;
  66. }
  67. time = Phase;
  68. return Prepare(mc);
  69. }
  70. public override bool Prepare(MegaModContext mc)
  71. {
  72. // Need this in a GetDeformer type method, then drawgizmo can be common
  73. if ( Scale == 0.0f )
  74. scale = 0.000001f;
  75. else
  76. scale = 1.0f / Scale;
  77. rt = 1.0f - Rough;
  78. return true;
  79. }
  80. }