Noise.h 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. #pragma once
  2. #include "vmath.hpp"
  3. namespace CurlNoise
  4. {
  5. using namespace Vectormath::Aos;
  6. //-----------------------------------------------------------------------------------------------
  7. // Return structure for noise sampling containing scalar float and 3D derivative
  8. struct NoiseSample
  9. {
  10. public:
  11. float value;
  12. Vector3 derivative;
  13. NoiseSample& operator+= (const NoiseSample& s)
  14. {
  15. value += s.value;
  16. derivative += s.derivative;
  17. return *this;
  18. }
  19. };
  20. inline NoiseSample operator+(NoiseSample a, float b) {
  21. a.value += b;
  22. return a;
  23. }
  24. inline NoiseSample operator + (float a, NoiseSample b) {
  25. b.value += a;
  26. return b;
  27. }
  28. inline NoiseSample operator + (NoiseSample a, NoiseSample b) {
  29. a.value += b.value;
  30. a.derivative += b.derivative;
  31. return a;
  32. }
  33. inline NoiseSample operator - (NoiseSample a, float b) {
  34. a.value -= b;
  35. return a;
  36. }
  37. inline NoiseSample operator - (float a, NoiseSample b) {
  38. b.value = a - b.value;
  39. b.derivative = -b.derivative;
  40. return b;
  41. }
  42. inline NoiseSample operator - (NoiseSample a, NoiseSample b) {
  43. a.value -= b.value;
  44. a.derivative -= b.derivative;
  45. return a;
  46. }
  47. inline NoiseSample operator * (NoiseSample a, float b) {
  48. a.value *= b;
  49. a.derivative *= b;
  50. return a;
  51. }
  52. inline NoiseSample operator * (float a, NoiseSample b) {
  53. b.value *= a;
  54. b.derivative *= a;
  55. return b;
  56. }
  57. inline NoiseSample operator * (NoiseSample a, NoiseSample b) {
  58. a.derivative = a.derivative * b.value + b.derivative * a.value;
  59. a.value *= b.value;
  60. return a;
  61. }
  62. //-----------------------------------------------------------------------------------------------
  63. struct PerlinNoise3
  64. {
  65. public:
  66. static NoiseSample Noise(Vector3 point,
  67. float frequency = 1.f,
  68. int octaves = 1,
  69. float lacunarity = 0.f,
  70. float persistence = 0.f);
  71. //inline float operator()(float x, float y, float z) const;
  72. //inline float operator()(const vmath::Vector3 &x) const { return (*this)(x[0], x[1], x[2]); }
  73. protected:
  74. static NoiseSample EvaluateNoise(const Vector3& point, float frequency);
  75. inline static float Dot(Vector3 g, float x, float y, float z) {
  76. return g.getX() * x + g.getY() * y + g.getZ() * z;
  77. }
  78. inline static float Smooth(float t) {
  79. // 6t^5 - 15t^4 + 10t^3
  80. return t * t * t * (t * (t * 6.0f - 15.0f) + 10.0f);
  81. }
  82. inline static float SmoothDerivative(float t) {
  83. // 30t^4 - 60t^3 + 30t^2
  84. return 30.0f * t * t * (t * (t - 2.0f) + 1.0f);
  85. }
  86. };
  87. } // namespace CurlNoise