procedurals
Procedural generation building blocks
Loading...
Searching...
No Matches
Noise.hpp
Go to the documentation of this file.
1
11#pragma once
12
13#include "Smoothing.hpp"
14#include <cmath>
15#include <cstdint>
16#include <random>
17
31float noise2D(float x, float y, uint64_t seed = 0) noexcept {
32 int xi = static_cast<int>(std::floor(x));
33 int yi = static_cast<int>(std::floor(y));
34 float tx = x - xi;
35 float ty = y - yi;
36
37 float v00 = random2D(xi, yi, seed);
38 float v10 = random2D(xi + 1, yi, seed);
39 float v01 = random2D(xi, yi + 1, seed);
40 float v11 = random2D(xi + 1, yi + 1, seed);
41
42 float a = InterpolateEaseInOut(v00, v10, tx);
43 float b = InterpolateEaseInOut(v01, v11, tx);
44 return InterpolateEaseInOut(a, b, ty);
45}
46
54inline float fbm2D(float x, float y, int octaves = 5, uint64_t seed = 0, float gain = 0.5f, float lacunarity = 2.0f) noexcept {
55 float value = 0.0f;
56 float amplitude = 1.0f;
57 float frequency = 1.0f;
58
59 for (int i = 0; i < octaves; ++i) {
60 value += amplitude * noise2D(x * frequency, y * frequency, seed + i * 1337); //1337 is used to increase the seed diff between each octaves
61 frequency *= lacunarity;
62 amplitude *= gain;
63 }
64
65 return value; // range ~[-1, 1]
66}
67
68inline float warp2D(float x, float y, uint64_t seed = 0, float strength = 2.0f) noexcept {
69 float warpX = noise2D(x * 0.5f, y * 0.5f, seed + 11);
70 float warpY = noise2D(x * 0.5f + 100.0f, y * 0.5f + 100.0f, seed + 23);
71 return noise2D(x + warpX * strength, y + warpY * strength, seed + 99);
72}
73
80inline float ridge2D(float x, float y, int octaves = 5, float gain = 0.5f, float lacunarity = 2.0f, uint64_t seed = 0) noexcept {
81 auto ridge = [](float n) { return 1.0f - std::fabs(n); };
82
83 float result = 0.0f;
84 float amplitude = 0.5f;
85 float frequency = 1.0f;
86
87 for (int i = 0; i < octaves; ++i) {
88 float n = ridge(noise2D(x * frequency, y * frequency, seed + i * 1000));
89 result += n * amplitude;
90 frequency *= lacunarity;
91 amplitude *= gain;
92 }
93
94 return result;
95}
96
97inline float rarity2D(float x, float y, float rarity = 0.01f, uint64_t seed = 0) {
98 float r = random2D(x, y, seed); // does it exist?
99
100 if (r > rarity) return 0.0f; // 0 = nothing
101 return random2D(x, y, seed + 999); // (0, 1] = which one
102}
103
104inline float worley2D(float x, float y, uint64_t seed = 0) noexcept {
105 int xi = (int)floor(x);
106 int yi = (int)floor(y);
107 float minDist = 9999.0f;
108
109 // On parcourt les cellules voisines pour trouver le point le plus proche
110 for (int j = -1; j <= 1; ++j) {
111 for (int i = -1; i <= 1; ++i) {
112 int cx = xi + i;
113 int cy = yi + j;
114
115 // point aléatoire dans la cellule
116 float fx = random2D(cx, cy, seed);
117 float fy = random2D(cx, cy, seed + 1337);
118
119 // position absolue du point
120 float px = cx + fx;
121 float py = cy + fy;
122
123 float dx = px - x;
124 float dy = py - y;
125 float dist = sqrt(dx*dx + dy*dy);
126
127 if (dist < minDist)
128 minDist = dist;
129 }
130 }
131
132 return minDist; // souvent dans [0,1] après normalisation
133}
134
135// -------------------- Simplex 2D --------------------
136
137// initPerm using deterministic RNG
138inline void initPerm(uint8_t* perm, uint64_t seed) {
139 // Fill 0..255
140 for(int i=0;i<256;i++) perm[i]=i;
141
142 // Shuffle using deterministic random2D or mix64
143 for(int i=255;i>0;i--){
144 // use a procedural hash as deterministic "random" index
145 uint64_t h = mix64(seed + i * GOLDEN_64); // mix64 is your procedural hash RNG
146 int j = int(h % (i+1)); // map to 0..i
147 std::swap(perm[i], perm[j]);
148 }
149
150 // Duplicate for wrap-around
151 for(int i=0;i<256;i++) perm[i+256]=perm[i];
152}
153
154inline float grad(int hash, float x, float y) {
155 int h = hash & 7; // 8 directions
156 float u = h < 4 ? x : y;
157 float v = h < 4 ? y : x;
158 return ((h & 1) ? -u : u) + ((h & 2) ? -2.0f*v : 2.0f*v);
159}
160
161inline float simplex2D(float xin, float yin, uint64_t seed = 0) {
162 static uint8_t perm[512];
163 static bool initialized=false;
164 if(!initialized){ initPerm(perm, seed); initialized=true; }
165
166 const float F2 = 0.5f*(sqrt(3.0f)-1.0f);
167 float s = (xin + yin) * F2;
168 int i = floor(xin + s);
169 int j = floor(yin + s);
170 const float G2 = (3.0f - sqrt(3.0f))/6.0f;
171 float t = (i + j) * G2;
172 float X0 = i - t, Y0 = j - t;
173 float x0 = xin - X0, y0 = yin - Y0;
174
175 int i1 = (x0 > y0) ? 1 : 0;
176 int j1 = (x0 > y0) ? 0 : 1;
177
178 float x1 = x0 - i1 + G2;
179 float y1 = y0 - j1 + G2;
180 float x2 = x0 - 1.0f + 2.0f * G2;
181 float y2 = y0 - 1.0f + 2.0f * G2;
182
183 int ii = i & 255;
184 int jj = j & 255;
185
186 float n0,n1,n2;
187
188 float t0 = 0.5f - x0*x0 - y0*y0;
189 n0 = (t0<0)?0:pow(t0,4) * grad(perm[ii+perm[jj]], x0, y0);
190
191 float t1 = 0.5f - x1*x1 - y1*y1;
192 n1 = (t1<0)?0:pow(t1,4) * grad(perm[ii+i1+perm[jj+j1]], x1, y1);
193
194 float t2 = 0.5f - x2*x2 - y2*y2;
195 n2 = (t2<0)?0:pow(t2,4) * grad(perm[ii+1+perm[jj+1]], x2, y2);
196
197 return (70.0f * (n0 + n1 + n2)) * 0.5 + 0.5; // valeur brute [0,1] approximative
198}
float grad(int hash, float x, float y)
Definition Noise.hpp:154
float random2D(int x, int y, uint64_t seed=0) noexcept
Definition Deterministic.hpp:67
float rarity2D(float x, float y, float rarity=0.01f, uint64_t seed=0)
Definition Noise.hpp:97
float simplex2D(float xin, float yin, uint64_t seed=0)
Definition Noise.hpp:161
float warp2D(float x, float y, uint64_t seed=0, float strength=2.0f) noexcept
Definition Noise.hpp:68
float ridge2D(float x, float y, int octaves=5, float gain=0.5f, float lacunarity=2.0f, uint64_t seed=0) noexcept
Ridged Fractal Noise (inverted fBm)
Definition Noise.hpp:80
float worley2D(float x, float y, uint64_t seed=0) noexcept
Definition Noise.hpp:104
constexpr uint64_t GOLDEN_64
Definition Deterministic.hpp:22
void initPerm(uint8_t *perm, uint64_t seed)
Definition Noise.hpp:138
float InterpolateEaseInOut(float a, float b, float t) noexcept
Definition Smoothing.hpp:36
uint64_t mix64(uint64_t x) noexcept
Definition Deterministic.hpp:40
float noise2D(float x, float y, uint64_t seed=0) noexcept
2D deterministic value noise in [-1, 1].
Definition Noise.hpp:31
float fbm2D(float x, float y, int octaves=5, uint64_t seed=0, float gain=0.5f, float lacunarity=2.0f) noexcept
Fractal Brownian Motion (fBm)
Definition Noise.hpp:54