summary refs log tree commit diff
path: root/src/Math/Perlin.hpp
blob: b28f9926b29aa6cb722100beaaeeb7ff21b34708 (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
39
#pragma once

#include "Common.hpp"

namespace Math::Perlin {

Real raw(Vector<2> pos);
Real raw(Vector<3> pos);

template <USize D>
struct Noise {
    static_assert(D > 1 && D < 4);

    Vector<D> offset{20000.0f};
    Real scale = 15.0f;

    UInt octaves = 3;
    Real persistence = 0.3f;
    Real lacunarity = 2.0f;

    Real at(Vector<D> pos) const {
        Real result = 0;
        Real max = 0;
        Real frequency = 1;
        Real amplitude = 1;

        for (UInt octave = 0; octave < octaves; octave++) {
            result += raw((pos + offset).abs() / scale * frequency) * amplitude;
            max += amplitude;

            frequency *= lacunarity;
            amplitude *= persistence;
        }

        return result / max;
    }
};

}