diff options
| author | Mel <einebeere@gmail.com> | 2023-06-12 17:09:55 +0200 |
|---|---|---|
| committer | Mel <einebeere@gmail.com> | 2023-06-12 17:14:03 +0200 |
| commit | d0de60dc33df75fbcacb53a09568b14d0fd48cb9 (patch) | |
| tree | 7aefdbb81f114552881834bd5b0d842bc2bdb691 /src/Math/Perlin.hpp | |
| parent | 23b0bc4d1ddc9fad3c32e8257497ddd13ac6a155 (diff) | |
| download | meowcraft-d0de60dc33df75fbcacb53a09568b14d0fd48cb9.tar.zst meowcraft-d0de60dc33df75fbcacb53a09568b14d0fd48cb9.zip | |
Multithreaded world generation with Perlin
Diffstat (limited to 'src/Math/Perlin.hpp')
| -rw-r--r-- | src/Math/Perlin.hpp | 38 |
1 files changed, 38 insertions, 0 deletions
diff --git a/src/Math/Perlin.hpp b/src/Math/Perlin.hpp new file mode 100644 index 0000000..c31697b --- /dev/null +++ b/src/Math/Perlin.hpp @@ -0,0 +1,38 @@ +#pragma once + +#include "Common.hpp" + +namespace Math::Perlin { + +float raw(Vector<2> pos); +float raw(Vector<3> pos); + +template <size_t D> +struct Noise { + static_assert(D > 1 && D < 4); + + Vector<D> offset{20000.0f}; + float scale = 15.0f; + + uint octaves = 3; + float persistence = 0.3f; + + float at(Vector<D> pos) const { + float result = 0; + float max = 0; + float frequency = 1; + float amplitude = 1; + + for (uint octave = 0; octave < octaves; octave++) { + result += raw((pos + offset).abs() / scale * frequency) * amplitude; + max += amplitude; + + frequency *= 2; + amplitude *= persistence; + } + + return result / max; + } +}; + +} \ No newline at end of file |
