From ad694f46d5708ae8ca6a42ae9dab8dd9563de0b8 Mon Sep 17 00:00:00 2001 From: Mel Date: Sat, 29 Oct 2022 21:44:56 +0200 Subject: Smooth out noise at noise seed borders --- src/Math/Noise.cpp | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) (limited to 'src/Math/Noise.cpp') diff --git a/src/Math/Noise.cpp b/src/Math/Noise.cpp index a174efe..1ef8ce5 100644 --- a/src/Math/Noise.cpp +++ b/src/Math/Noise.cpp @@ -17,14 +17,16 @@ float noise2d(Vector<2> pos) { {0.01f, {250.0f, 20.0f}, 0.5f}, }; + auto wrap = [](auto x) -> float { + float wx = std::fmod(x, NOISE_SEED_SIZE); + return wx < 0 ? wx + NOISE_SEED_SIZE : wx; + }; + auto total_weight = 0.0f; auto res = 0.0f; for (const auto& layer : layers) { auto pos_with_scale_and_offset = (pos - layer.offset) * layer.scale; - auto p = pos_with_scale_and_offset.map([](auto x) -> float { - float wx = std::fmod(x, NOISE_SEED_SIZE); - return wx < 0 ? wx + NOISE_SEED_SIZE : wx; - }); + auto p = pos_with_scale_and_offset.map(wrap); auto x = p.x(); auto y = p.y(); @@ -33,7 +35,9 @@ float noise2d(Vector<2> pos) { auto x2 = x1 + 1.0f; auto y1 = y2 + 1.0f; - auto value = [](float x, float y){ return noise_seed[(uint)x][(uint)y] / 255.0f; }; + auto value = [=](float x, float y){ + return noise_seed[(uint)wrap(x)][(uint)wrap(y)] / 255.0f; + }; auto q12 = value(x1, y2); auto q22 = value(x2, y2); -- cgit 1.4.1