MegaNoiseWarp.cs 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. using UnityEngine;
  2. [AddComponentMenu("Modifiers/Warps/Noise")]
  3. public class MegaNoiseWarp : MegaWarp
  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 d = new Vector3();
  18. public override string WarpName() { return "Noise"; }
  19. public override string GetIcon() { return "MegaNoise icon.png"; }
  20. public override string GetHelpURL() { return "?page_id=2576"; }
  21. Vector3 sp = Vector3.zero;
  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. sp.x = p.x * scale + 0.5f;
  29. sp.y = p.y * scale + 0.5f;
  30. sp.z = p.z * scale + 0.5f;
  31. if ( Fractal )
  32. {
  33. d.x = iperlin.fBm1(sp.y, sp.z, time, rt, 2.0f, Iterations);
  34. d.y = iperlin.fBm1(sp.x, sp.z, time, rt, 2.0f, Iterations);
  35. d.z = iperlin.fBm1(sp.x, sp.y, time, rt, 2.0f, Iterations);
  36. }
  37. else
  38. {
  39. d.x = iperlin.Noise(sp.y, sp.z, time);
  40. d.y = iperlin.Noise(sp.x, sp.z, time);
  41. d.z = iperlin.Noise(sp.x, sp.y, time);
  42. }
  43. p.x += d.x * Strength.x;
  44. p.y += d.y * Strength.y;
  45. p.z += d.z * Strength.z;
  46. p.x = ip.x + ((p.x - ip.x) * dcy);
  47. p.y = ip.y + ((p.y - ip.y) * dcy);
  48. p.z = ip.z + ((p.z - ip.z) * dcy);
  49. return invtm.MultiplyPoint3x4(p); // + Vector3.Scale(d, Strength));
  50. }
  51. void Update()
  52. {
  53. if ( Animate )
  54. {
  55. if ( Application.isPlaying )
  56. Phase += Time.deltaTime * Freq;
  57. }
  58. time = Phase;
  59. }
  60. public override bool Prepare(float decay)
  61. {
  62. tm = transform.worldToLocalMatrix;
  63. invtm = tm.inverse;
  64. if ( Scale == 0.0f )
  65. scale = 0.000001f;
  66. else
  67. scale = 1.0f / Scale;
  68. rt = 1.0f - Rough;
  69. totaldecay = Decay + decay;
  70. if ( totaldecay < 0.0f )
  71. totaldecay = 0.0f;
  72. return true;
  73. }
  74. }