summary refs log tree commit diff
path: root/src/Math/Perlin.hpp
blob: c31697b87e48c8ba9deedcfd97b7f40810e8c99b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
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;
    }
};

}