MegaNoise.cs 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. using UnityEngine;
  2. [AddComponentMenu("Modifiers/Noise")]
  3. public class MegaNoise : 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 Vector3 Strength = new Vector3(0.0f, 0.0f, 0.0f);
  13. MegaPerlin iperlin = MegaPerlin.Instance;
  14. float time = 0.0f;
  15. float scale;
  16. float rt;
  17. Vector3 sp = Vector3.zero;
  18. Vector3 d = Vector3.zero;
  19. public override string ModName() { return "Noise"; }
  20. public override string GetHelpURL() { return "?page_id=262"; }
  21. public override void Modify(MegaModifiers mc)
  22. {
  23. for ( int i = 0; i < verts.Length; i++ )
  24. {
  25. Vector3 p = tm.MultiplyPoint3x4(verts[i]);
  26. sp.x = p.x * scale + 0.5f;
  27. sp.y = p.y * scale + 0.5f;
  28. sp.z = p.z * scale + 0.5f;
  29. if ( Fractal )
  30. {
  31. d.x = iperlin.fBm1(sp.y, sp.z, time, rt, 2.0f, Iterations);
  32. d.y = iperlin.fBm1(sp.x, sp.z, time, rt, 2.0f, Iterations);
  33. d.z = iperlin.fBm1(sp.x, sp.y, time, rt, 2.0f, Iterations);
  34. }
  35. else
  36. {
  37. d.x = iperlin.Noise(sp.y, sp.z, time);
  38. d.y = iperlin.Noise(sp.x, sp.z, time);
  39. d.z = iperlin.Noise(sp.x, sp.y, time);
  40. }
  41. p.x += d.x * Strength.x;
  42. p.y += d.y * Strength.y;
  43. p.z += d.z * Strength.z;
  44. sverts[i] = invtm.MultiplyPoint3x4(p);
  45. }
  46. }
  47. public override void ModStart(MegaModifiers mc)
  48. {
  49. }
  50. public override Vector3 Map(int i, Vector3 p)
  51. {
  52. p = tm.MultiplyPoint3x4(p);
  53. float spx = p.x * scale + 0.5f;
  54. float spy = p.y * scale + 0.5f;
  55. float spz = p.z * scale + 0.5f;
  56. float dx,dy,dz;
  57. if ( Fractal )
  58. {
  59. dx = iperlin.fBm1(spy, spz, time, rt, 2.0f, Iterations);
  60. dy = iperlin.fBm1(spx, spz, time, rt, 2.0f, Iterations);
  61. dz = iperlin.fBm1(spx, spy, time, rt, 2.0f, Iterations);
  62. }
  63. else
  64. {
  65. dx = iperlin.Noise(spy, spz, time);
  66. dy = iperlin.Noise(spx, spz, time);
  67. dz = iperlin.Noise(spx, spy, time);
  68. }
  69. p.x += dx * Strength.x;
  70. p.y += dy * Strength.y;
  71. p.z += dz * Strength.z;
  72. return invtm.MultiplyPoint3x4(p);
  73. }
  74. public override bool ModLateUpdate(MegaModContext mc)
  75. {
  76. if ( Animate )
  77. {
  78. if ( Application.isPlaying )
  79. Phase += Time.deltaTime * Freq;
  80. }
  81. time = Phase;
  82. return Prepare(mc);
  83. }
  84. public override bool Prepare(MegaModContext mc)
  85. {
  86. // Need this in a GetDeformer type method, then drawgizmo can be common
  87. if ( Scale == 0.0f )
  88. scale = 0.000001f;
  89. else
  90. scale = 1.0f / Scale;
  91. rt = 1.0f - Rough;
  92. return true;
  93. }
  94. }