PerlinNoise.cs 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. 
  2. // =================================
  3. // Namespaces.
  4. // =================================
  5. using UnityEngine;
  6. // =================================
  7. // Define namespace.
  8. // =================================
  9. namespace MirzaBeig
  10. {
  11. namespace ParticleSystems
  12. {
  13. // =================================
  14. // Classes.
  15. // =================================
  16. [System.Serializable]
  17. public class PerlinNoise
  18. {
  19. public void init()
  20. {
  21. // Don't make the range values too large, else floating point precision will result in jitter.
  22. offset.x = Random.Range(-32.0f, 32.0f);
  23. offset.y = Random.Range(-32.0f, 32.0f);
  24. }
  25. //public PerlinNoise()
  26. //{
  27. // offset.x = Random.Range(0.0f, 99999.0f);
  28. // offset.y = Random.Range(0.0f, 99999.0f);
  29. //}
  30. public float GetValue(float time)
  31. {
  32. float noiseTime = time * frequency;
  33. return (Mathf.PerlinNoise(noiseTime + offset.x, noiseTime + offset.y) - 0.5f) * amplitude;
  34. }
  35. Vector2 offset;
  36. public float amplitude = 1.0f;
  37. public float frequency = 1.0f;
  38. public bool unscaledTime;
  39. }
  40. // ...
  41. [System.Serializable]
  42. public class PerlinNoiseXYZ
  43. {
  44. public void init()
  45. {
  46. x.init();
  47. y.init();
  48. z.init();
  49. }
  50. public Vector3 GetXYZ(float time)
  51. {
  52. float frequencyScaledTime = time * frequencyScale;
  53. return new Vector3(x.GetValue(frequencyScaledTime), y.GetValue(frequencyScaledTime), z.GetValue(frequencyScaledTime)) * amplitudeScale;
  54. }
  55. public PerlinNoise x;
  56. public PerlinNoise y;
  57. public PerlinNoise z;
  58. public bool unscaledTime;
  59. public float amplitudeScale = 1.0f;
  60. public float frequencyScale = 1.0f;
  61. }
  62. // =================================
  63. // End namespace.
  64. // =================================
  65. }
  66. }
  67. // =================================
  68. // --END-- //
  69. // =================================