载入中...
搜索中...
未找到
NoiseField.cpp
浏览该文件的文档.
2
3#include <algorithm>
4#include <cmath>
5
6namespace eve::procgen {
7namespace {
8
9float fade(float t) { return t * t * (3.f - 2.f * t); }
10float fade5(float t) { return t * t * t * (t * (t * 6.f - 15.f) + 10.f); }
11
12int wrapIndex(int i, int period) {
13 if (period <= 0) return i;
14 int m = i % period;
15 if (m < 0) m += period;
16 return m;
17}
18
19void rotate2(float& x, float& y) {
20 // Fixed rotation ≈ 0.5 rad (cos/sin) — same trick as noisecreater smoke fBm.
21 constexpr float c = 0.87758256189f; // cos(0.5)
22 constexpr float s = 0.47942553860f; // sin(0.5)
23 const float nx = c * x - s * y;
24 const float ny = s * x + c * y;
25 x = nx;
26 y = ny;
27}
28
29float metricDist(float dx, float dy, WorleyMetric metric) {
30 switch (metric) {
31 case WorleyMetric::Manhattan: return std::fabs(dx) + std::fabs(dy);
32 case WorleyMetric::Chebyshev: return std::max(std::fabs(dx), std::fabs(dy));
34 default: return std::sqrt(dx * dx + dy * dy);
35 }
36}
37
38float metricDist3(float dx, float dy, float dz, WorleyMetric metric) {
39 switch (metric) {
40 case WorleyMetric::Manhattan: return std::fabs(dx) + std::fabs(dy) + std::fabs(dz);
41 case WorleyMetric::Chebyshev: return std::max(std::fabs(dx), std::max(std::fabs(dy), std::fabs(dz)));
43 default: return std::sqrt(dx * dx + dy * dy + dz * dz);
44 }
45}
46
47void worleySorted2(const NoiseField& field, float x, float y, float jitter, WorleyMetric metric, float& f1, float& f2) {
48 jitter = std::clamp(jitter, 0.f, 1.f);
49 const int ix = int(std::floor(x));
50 const int iy = int(std::floor(y));
51 f1 = 1e9f;
52 f2 = 1e9f;
53 for (int j = -1; j <= 1; ++j) {
54 for (int i = -1; i <= 1; ++i) {
55 const int cx = ix + i;
56 const int cy = iy + j;
57 const float px = float(cx) + 0.5f + (field.hash01(cx, cy) - 0.5f) * jitter;
58 const float py = float(cy) + 0.5f + (field.hash01(cx * 7 + 3, cy * 13 + 5) - 0.5f) * jitter;
59 const float d = metricDist(x - px, y - py, metric);
60 if (d < f1) {
61 f2 = f1;
62 f1 = d;
63 } else if (d < f2) {
64 f2 = d;
65 }
66 }
67 }
68}
69
70constexpr float kHexFactor = 0.86602540378f; // sqrt(3)/2
71
72float positiveMod(float v, float m) {
73 float r = std::fmod(v, m);
74 return r < 0.f ? r + m : r;
75}
76
77void nearestHexCell(float x, float y, float& cellX, float& cellY) {
78 // noisecreater HexNoiseFragment nearestHexCell.
79 const float gposX = x / kHexFactor;
80 const float gposY = y;
81 float hx = std::floor(gposX);
82 float hy = std::floor(gposY);
83 const float sy = (positiveMod(hx + 1.f, 4.f) >= 2.f) ? 1.f : 0.f;
84 hx += positiveMod(hx, 2.f);
85 hy += positiveMod(hy + sy, 2.f);
86 const float gdx = gposX - hx;
87 const float gdy = gposY - hy;
88 if (std::fabs(gdx) * (kHexFactor * kHexFactor) + std::fabs(gdy) * 0.5f > 1.f) {
89 hx += (gdx >= 0.f ? 1.f : -1.f) * 2.f;
90 hy += (gdy >= 0.f ? 1.f : -1.f);
91 }
92 cellX = hx;
93 cellY = hy;
94}
95
96float hexDistLocal(float px, float py) {
97 px = std::fabs(px);
98 py = std::fabs(py);
99 return std::max(px * kHexFactor + py * 0.5f, py) - 1.f;
100}
101
102float gyroidRaw(float x, float y, float z) {
103 return std::sin(x) * std::cos(y) + std::sin(y) * std::cos(z) + std::sin(z) * std::cos(x);
104}
105
106float remapSigned01(float n) { return std::clamp(n * 0.5f + 0.5f, 0.f, 1.f); }
107
108} // namespace
109
110float NoiseField::hash01(int ix, int iy) const {
111 ix = wrapIndex(ix, periodX);
112 iy = wrapIndex(iy, periodY);
113 uint32_t h = seed;
114 h ^= uint32_t(ix) * 374761393u;
115 h ^= uint32_t(iy) * 668265263u;
116 h = (h ^ (h >> 13)) * 1274126177u;
117 h ^= h >> 16;
118 return float(h & 0x00FFFFFFu) / float(0x00FFFFFFu);
119}
120
121float NoiseField::whiteNoise(int ix, int iy) const { return hash01(ix, iy); }
122
123float NoiseField::valueNoise(float x, float y) const {
124 const int x0 = int(std::floor(x));
125 const int y0 = int(std::floor(y));
126 const float fx = fade(x - float(x0));
127 const float fy = fade(y - float(y0));
128 const float n00 = hash01(x0, y0);
129 const float n10 = hash01(x0 + 1, y0);
130 const float n01 = hash01(x0, y0 + 1);
131 const float n11 = hash01(x0 + 1, y0 + 1);
132 const float nx0 = n00 + (n10 - n00) * fx;
133 const float nx1 = n01 + (n11 - n01) * fx;
134 return nx0 + (nx1 - nx0) * fy;
135}
136
137float NoiseField::perlinNoise(float x, float y) const {
138 const int x0 = int(std::floor(x));
139 const int y0 = int(std::floor(y));
140 const float dx = x - float(x0);
141 const float dy = y - float(y0);
142 const float fx = fade5(dx);
143 const float fy = fade5(dy);
144
145 auto grad = [&](int ix, int iy, float px, float py) -> float {
146 static const float gx[8] = {1.f, -1.f, 1.f, -1.f, 1.f, -1.f, 0.f, 0.f};
147 static const float gy[8] = {1.f, 1.f, -1.f, -1.f, 0.f, 0.f, 1.f, -1.f};
148 const int h = int(hash01(ix, iy) * 7.999f) & 7;
149 return gx[h] * (px - float(ix)) + gy[h] * (py - float(iy));
150 };
151
152 const float n00 = grad(x0, y0, x, y);
153 const float n10 = grad(x0 + 1, y0, x, y);
154 const float n01 = grad(x0, y0 + 1, x, y);
155 const float n11 = grad(x0 + 1, y0 + 1, x, y);
156 const float nx0 = n00 + (n10 - n00) * fx;
157 const float nx1 = n01 + (n11 - n01) * fx;
158 const float n = nx0 + (nx1 - nx0) * fy;
159 return std::clamp(n * 0.5f + 0.5f, 0.f, 1.f);
160}
161
162float NoiseField::simplexNoise(float x, float y) const {
163 constexpr float K1 = 0.36602540378f;
164 constexpr float K2 = 0.2113248654f;
165
166 const float s = (x + y) * K1;
167 const int i = int(std::floor(x + s));
168 const int j = int(std::floor(y + s));
169 const float t = float(i + j) * K2;
170 const float x0 = x - (float(i) - t);
171 const float y0 = y - (float(j) - t);
172
173 const int i1 = x0 > y0 ? 1 : 0;
174 const int j1 = x0 > y0 ? 0 : 1;
175
176 const float x1 = x0 - float(i1) + K2;
177 const float y1 = y0 - float(j1) + K2;
178 const float x2 = x0 - 1.f + 2.f * K2;
179 const float y2 = y0 - 1.f + 2.f * K2;
180
181 auto gradDot = [&](int ix, int iy, float px, float py) -> float {
182 static const float gx[8] = {1.f, -1.f, 1.f, -1.f, 1.f, -1.f, 0.f, 0.f};
183 static const float gy[8] = {1.f, 1.f, -1.f, -1.f, 0.f, 0.f, 1.f, -1.f};
184 const int h = int(hash01(ix, iy) * 7.999f) & 7;
185 return gx[h] * px + gy[h] * py;
186 };
187
188 float n0 = 0.f, n1 = 0.f, n2 = 0.f;
189 float t0 = 0.5f - x0 * x0 - y0 * y0;
190 if (t0 > 0.f) {
191 t0 *= t0;
192 n0 = t0 * t0 * gradDot(i, j, x0, y0);
193 }
194 float t1 = 0.5f - x1 * x1 - y1 * y1;
195 if (t1 > 0.f) {
196 t1 *= t1;
197 n1 = t1 * t1 * gradDot(i + i1, j + j1, x1, y1);
198 }
199 float t2 = 0.5f - x2 * x2 - y2 * y2;
200 if (t2 > 0.f) {
201 t2 *= t2;
202 n2 = t2 * t2 * gradDot(i + 1, j + 1, x2, y2);
203 }
204 return std::clamp(70.f * (n0 + n1 + n2) * 0.5f + 0.5f, 0.f, 1.f);
205}
206
207float NoiseField::worleyF1(float x, float y, float jitter, WorleyMetric metric) const {
208 float f1 = 0.f, f2 = 0.f;
209 worleySorted2(*this, x, y, jitter, metric, f1, f2);
210 return f1;
211}
212
213float NoiseField::worleyF2(float x, float y, float jitter, WorleyMetric metric) const {
214 float f1 = 0.f, f2 = 0.f;
215 worleySorted2(*this, x, y, jitter, metric, f1, f2);
216 return f2;
217}
218
219float NoiseField::worleyNoise(float x, float y, float jitter, WorleyMetric metric) const {
220 return std::clamp(1.f - worleyF1(x, y, jitter, metric), 0.f, 1.f);
221}
222
223float NoiseField::worleyEdge(float x, float y, float jitter, WorleyMetric metric) const {
224 float f1 = 0.f, f2 = 0.f;
225 worleySorted2(*this, x, y, jitter, metric, f1, f2);
226 return std::max(0.f, f2 - f1);
227}
228
229float NoiseField::worleyCombine(float x, float y, float jitter, WorleyMetric metric) const {
230 float f1 = 0.f, f2 = 0.f;
231 worleySorted2(*this, x, y, jitter, metric, f1, f2);
232 return std::clamp(1.f - 0.5f * (f1 + f2), 0.f, 1.f);
233}
234
235float NoiseField::worleySmooth(float x, float y, float smoothness, float jitter) const {
236 smoothness = std::max(1e-4f, smoothness);
237 jitter = std::clamp(jitter, 0.f, 1.f);
238 const int ix = int(std::floor(x));
239 const int iy = int(std::floor(y));
240 // Soft-min of squared Euclidean distances → metaball look.
241 float acc = 0.f;
242 for (int j = -1; j <= 1; ++j) {
243 for (int i = -1; i <= 1; ++i) {
244 const int cx = ix + i;
245 const int cy = iy + j;
246 const float px = float(cx) + 0.5f + (hash01(cx, cy) - 0.5f) * jitter;
247 const float py = float(cy) + 0.5f + (hash01(cx * 7 + 3, cy * 13 + 5) - 0.5f) * jitter;
248 const float dx = x - px;
249 const float dy = y - py;
250 const float d2 = dx * dx + dy * dy;
251 acc += std::exp(-d2 / (smoothness * smoothness));
252 }
253 }
254 return std::clamp(1.f - std::exp(-acc), 0.f, 1.f);
255}
256
257float NoiseField::fbm(float x, float y, int octaves, float lacunarity, float gain) const {
258 octaves = std::max(1, octaves);
259 float sum = 0.f, amp = 1.f, norm = 0.f, freq = 1.f;
260 for (int i = 0; i < octaves; ++i) {
261 sum += valueNoise(x * freq, y * freq) * amp;
262 norm += amp;
263 amp *= gain;
264 freq *= lacunarity;
265 }
266 return norm > 0.f ? sum / norm : 0.f;
267}
268
269float NoiseField::fbmPerlin(float x, float y, int octaves, float lacunarity, float gain) const {
270 octaves = std::max(1, octaves);
271 float sum = 0.f, amp = 1.f, norm = 0.f, freq = 1.f;
272 for (int i = 0; i < octaves; ++i) {
273 const float ox = float(i) * 17.8f;
274 const float oy = float(i) * 23.5f;
275 sum += perlinNoise(x * freq + ox, y * freq + oy) * amp;
276 norm += amp;
277 amp *= gain;
278 freq *= lacunarity;
279 }
280 return norm > 0.f ? sum / norm : 0.f;
281}
282
283float NoiseField::fbmSimplex(float x, float y, int octaves, float lacunarity, float gain) const {
284 octaves = std::max(1, octaves);
285 float sum = 0.f, amp = 1.f, norm = 0.f, freq = 1.f;
286 for (int i = 0; i < octaves; ++i) {
287 const float ox = float(i) * 13.1f;
288 const float oy = float(i) * 29.7f;
289 sum += simplexNoise(x * freq + ox, y * freq + oy) * amp;
290 norm += amp;
291 amp *= gain;
292 freq *= lacunarity;
293 }
294 return norm > 0.f ? sum / norm : 0.f;
295}
296
297float NoiseField::fbmRotated(float x, float y, int octaves, float lacunarity, float gain) const {
298 octaves = std::max(1, octaves);
299 float sum = 0.f, amp = 1.f, norm = 0.f;
300 float px = x, py = y;
301 for (int i = 0; i < octaves; ++i) {
302 sum += valueNoise(px, py) * amp;
303 norm += amp;
304 amp *= gain;
305 px *= lacunarity;
306 py *= lacunarity;
307 rotate2(px, py);
308 px += 100.f;
309 py += 100.f;
310 }
311 return norm > 0.f ? sum / norm : 0.f;
312}
313
314float NoiseField::ridged(float x, float y, int octaves, float lacunarity, float gain) const {
315 octaves = std::max(1, octaves);
316 float sum = 0.f, amp = 1.f, norm = 0.f, freq = 1.f;
317 for (int i = 0; i < octaves; ++i) {
318 float n = valueNoise(x * freq, y * freq);
319 n = 1.f - std::fabs(n * 2.f - 1.f);
320 sum += n * n * amp;
321 norm += amp;
322 amp *= gain;
323 freq *= lacunarity;
324 }
325 return norm > 0.f ? sum / norm : 0.f;
326}
327
328float NoiseField::ridgedPerlin(float x, float y, int octaves, float lacunarity, float gain) const {
329 octaves = std::max(1, octaves);
330 float sum = 0.f, amp = 1.f, norm = 0.f, freq = 1.f;
331 for (int i = 0; i < octaves; ++i) {
332 const float ox = float(i) * 11.3f;
333 const float oy = float(i) * 19.7f;
334 float n = perlinNoise(x * freq + ox, y * freq + oy);
335 n = 1.f - std::fabs(n * 2.f - 1.f);
336 sum += n * n * amp;
337 norm += amp;
338 amp *= gain;
339 freq *= lacunarity;
340 }
341 return norm > 0.f ? sum / norm : 0.f;
342}
343
344float NoiseField::billow(float x, float y, int octaves, float lacunarity, float gain) const {
345 octaves = std::max(1, octaves);
346 float sum = 0.f, amp = 1.f, norm = 0.f, freq = 1.f;
347 for (int i = 0; i < octaves; ++i) {
348 sum += std::fabs(valueNoise(x * freq, y * freq) * 2.f - 1.f) * amp;
349 norm += amp;
350 amp *= gain;
351 freq *= lacunarity;
352 }
353 return norm > 0.f ? sum / norm : 0.f;
354}
355
356float NoiseField::billowPerlin(float x, float y, int octaves, float lacunarity, float gain) const {
357 octaves = std::max(1, octaves);
358 float sum = 0.f, amp = 1.f, norm = 0.f, freq = 1.f;
359 for (int i = 0; i < octaves; ++i) {
360 const float ox = float(i) * 15.4f;
361 const float oy = float(i) * 27.1f;
362 sum += std::fabs(perlinNoise(x * freq + ox, y * freq + oy) * 2.f - 1.f) * amp;
363 norm += amp;
364 amp *= gain;
365 freq *= lacunarity;
366 }
367 return norm > 0.f ? sum / norm : 0.f;
368}
369
370float NoiseField::swiss(float x, float y, int octaves, float lacunarity, float gain, float warpStrength) const {
371 octaves = std::max(1, octaves);
372 float sum = 0.f, amp = 1.f, norm = 0.f, freq = 1.f;
373 float px = x, py = y;
374 for (int i = 0; i < octaves; ++i) {
375 const float n = valueNoise(px, py) * 2.f - 1.f;
376 sum += n * amp;
377 norm += amp;
378 // Warp subsequent octaves by the running signed sum (Swiss / eroded look).
379 freq *= lacunarity;
380 px = x * freq + sum * warpStrength;
381 py = y * freq + sum * warpStrength;
382 amp *= gain;
383 }
384 return norm > 0.f ? remapSigned01(sum / norm) : 0.5f;
385}
386
387float NoiseField::hybrid(float x, float y, int octaves, float lacunarity, float gain, float offset) const {
388 octaves = std::max(1, octaves);
389 float freq = 1.f;
390 float signal = (valueNoise(x, y) + offset) * gain;
391 float result = signal;
392 float weight = std::clamp(signal, 0.f, 1.f);
393 float amp = gain;
394 float norm = 1.f;
395 for (int i = 1; i < octaves; ++i) {
396 freq *= lacunarity;
397 amp *= gain;
398 signal = (valueNoise(x * freq, y * freq) + offset) * gain;
399 result += weight * signal * amp;
400 norm += amp;
401 weight = std::clamp(weight * signal, 0.f, 1.f);
402 }
403 return norm > 0.f ? std::clamp(result / (norm * (1.f + offset)), 0.f, 1.f) : 0.f;
404}
405
406float NoiseField::perlinWorley(float x, float y, int octaves, float jitter) const {
407 const float w = worleyNoise(x, y, jitter);
408 const float p = fbmPerlin(x, y, std::max(1, octaves));
409 return std::clamp(w * p, 0.f, 1.f);
410}
411
412float NoiseField::warp(float x, float y, float amp, int octaves) const {
413 const float wx = fbm(x + 19.1f, y + 7.3f, octaves) - 0.5f;
414 const float wy = fbm(x + 5.2f, y + 31.7f, octaves) - 0.5f;
415 return fbm(x + wx * amp, y + wy * amp, octaves);
416}
417
418float NoiseField::warpDeep(float x, float y, float amp, int octaves) const {
419 const float qx = fbm(x, y, octaves);
420 const float qy = fbm(x + 5.2f, y + 1.3f, octaves);
421 const float rx = fbm(x + amp * qx + 1.7f, y + amp * qy + 9.2f, octaves);
422 const float ry = fbm(x + amp * qx + 8.3f, y + amp * qy + 2.8f, octaves);
423 return fbm(x + amp * rx, y + amp * ry, octaves);
424}
425
426float NoiseField::curlNoiseX(float x, float y, float eps) const {
427 eps = std::max(eps, 1e-6f);
428 // curl(φ).x = dφ/dy
429 return (valueNoise(x, y + eps) - valueNoise(x, y - eps)) / (2.f * eps);
430}
431
432float NoiseField::curlNoiseY(float x, float y, float eps) const {
433 eps = std::max(eps, 1e-6f);
434 // curl(φ).y = -dφ/dx
435 return -(valueNoise(x + eps, y) - valueNoise(x - eps, y)) / (2.f * eps);
436}
437
438float NoiseField::hexDistance(float x, float y) const {
439 float cx = 0.f, cy = 0.f;
440 nearestHexCell(x, y, cx, cy);
441 const float ox = x - cx * kHexFactor;
442 const float oy = y - cy;
443 return std::fabs(hexDistLocal(ox, oy));
444}
445
446float NoiseField::hexValue(float x, float y) const {
447 float cx = 0.f, cy = 0.f;
448 nearestHexCell(x, y, cx, cy);
449 return hash01(int(std::floor(cx)), int(std::floor(cy)));
450}
451
452float NoiseField::hexEdge(float x, float y, float width, float soft) const {
453 const float d = hexDistance(x, y);
454 const float band = std::max(soft, 1e-4f);
455 // 1 on edge when d ≈ 0 within width (noisecreater lineMask).
456 const float t = std::clamp((d - width) / band, 0.f, 1.f);
457 return 1.f - (t * t * (3.f - 2.f * t));
458}
459
460float NoiseField::gyroid(float x, float y, float z) const {
461 // Seed perturbs phase so different fields don't share zeros.
462 const float p = float(seed) * 0.001f;
463 return remapSigned01(gyroidRaw(x + p, y + p * 1.3f, z + p * 0.7f) / 1.5f);
464}
465
466float NoiseField::gyroidFbm(float x, float y, float z, int octaves) const {
467 octaves = std::max(1, std::min(octaves, 8));
468 float result = 0.f;
469 float a = 0.5f;
470 float seedZ = z;
471 float norm = 0.f;
472 const float ph = float(seed) * 0.001f;
473 for (int i = 0; i < octaves; ++i) {
474 seedZ += result * 0.5f;
475 result += std::fabs(gyroidRaw((x + ph) / a, (y + ph) / a, (seedZ + ph) / a)) * a;
476 norm += a;
477 a *= 0.5f;
478 }
479 return norm > 0.f ? std::clamp(result / (norm * 1.5f), 0.f, 1.f) : 0.f;
480}
481
482float NoiseField::jordan(float x, float y, int octaves, float lacunarity, float gain, float warpStrength) const {
483 octaves = std::max(1, octaves);
484 float sum = 0.f, amp = 1.f, norm = 0.f, freq = 1.f;
485 float dx = 0.f, dy = 0.f;
486 constexpr float kEps = 1e-3f;
487 for (int i = 0; i < octaves; ++i) {
488 const float px = x * freq + dx * warpStrength;
489 const float py = y * freq + dy * warpStrength;
490 const float n = valueNoise(px, py) * 2.f - 1.f;
491 const float dnx = (valueNoise(px + kEps, py) - valueNoise(px - kEps, py)) / (2.f * kEps);
492 const float dny = (valueNoise(px, py + kEps) - valueNoise(px, py - kEps)) / (2.f * kEps);
493 sum += n * amp;
494 norm += amp;
495 dx += dnx * amp;
496 dy += dny * amp;
497 amp *= gain;
498 freq *= lacunarity;
499 }
500 return norm > 0.f ? remapSigned01(sum / norm) : 0.5f;
501}
502
503float NoiseField::gabor(float x, float y, float dirX, float dirY, float frequency) const {
504 // noisecreater gavoronoi3: 3×3 Gaussian-weighted cos carriers.
505 const int ix = int(std::floor(x));
506 const int iy = int(std::floor(y));
507 const float fx = x - float(ix);
508 const float fy = y - float(iy);
509 constexpr float kCellVar = 0.8f;
510 constexpr float kDirVar = 0.4f;
511 float va = 0.f;
512 float wt = 0.f;
513 for (int j = -1; j <= 1; ++j) {
514 for (int i = -1; i <= 1; ++i) {
515 const int cx = ix + i;
516 const int cy = iy + j;
517 const float hx = (hash01(cx, cy) - 0.5f);
518 const float hy = (hash01(cx * 3 + 11, cy * 7 + 5) - 0.5f);
519 const float ox = float(i) - 0.5f;
520 const float oy = float(j) - 0.5f;
521 const float ppx = fx + ox - hx;
522 const float ppy = fy + oy - hy;
523 const float d2 = ppx * ppx + ppy * ppy;
524 const float w = std::exp(-d2 * 4.f);
525 float dx = kDirVar * hx + dirX;
526 float dy = kDirVar * hy + dirY;
527 wt += w;
528 va += std::cos((ppx * dx + ppy * dy) * frequency / kCellVar) * w;
529 }
530 }
531 return wt > 0.f ? remapSigned01(va / wt) : 0.5f;
532}
533
534float NoiseField::gaborFbm(float x, float y, int octaves, float dirX, float dirY) const {
535 octaves = std::max(1, std::min(octaves, 8));
536 float sum = 0.f, freq = 1.f, norm = 0.f, amp = 1.f;
537 float px = x, py = y;
538 for (int i = 0; i < octaves; ++i) {
539 const float n = gabor(px * freq, py * freq, dirX, dirY) * 2.f - 1.f;
540 sum += std::fabs(n) * amp;
541 norm += amp;
542 px += -0.01f * sum;
543 py += 0.07f * sum;
544 amp *= 0.5f;
545 freq *= 2.2f;
546 }
547 return norm > 0.f ? std::clamp(sum / norm, 0.f, 1.f) : 0.f;
548}
549
550float NoiseField::valueDx(float x, float y, float eps) const {
551 eps = std::max(eps, 1e-6f);
552 return (valueNoise(x + eps, y) - valueNoise(x - eps, y)) / (2.f * eps);
553}
554
555float NoiseField::valueDy(float x, float y, float eps) const {
556 eps = std::max(eps, 1e-6f);
557 return (valueNoise(x, y + eps) - valueNoise(x, y - eps)) / (2.f * eps);
558}
559
560float NoiseField::valueSlope(float x, float y, float eps) const {
561 const float dx = valueDx(x, y, eps);
562 const float dy = valueDy(x, y, eps);
563 return std::sqrt(dx * dx + dy * dy);
564}
565
566float NoiseField::isoLine(float x, float y, int levels, float width) const {
567 levels = std::max(1, levels);
568 width = std::max(1e-4f, width);
569 const float n = valueNoise(x, y);
570 // Distance to nearest contour in level space, then soft line mask.
571 const float scaled = n * float(levels);
572 const float v = std::fabs(std::fmod(scaled + 0.5f, 1.f) - 0.5f);
573 const float t = std::clamp(v / width, 0.f, 1.f);
574 return 1.f - (t * t * (3.f - 2.f * t));
575}
576
577float NoiseField::banding(float x, float y, int bands) const {
578 bands = std::max(1, bands);
579 const float n = valueNoise(x, y);
580 const float step = 1.f / float(bands);
581 return std::floor(n * float(bands)) * step;
582}
583
584float NoiseField::fbmColor(float x, float y, int channel, int octaves, float lacunarity, float gain) const {
585 // Fixed decorrelated offsets per RGB-style channel (noisecreater FBM color idea).
586 channel = ((channel % 3) + 3) % 3;
587 static constexpr float kOx[3] = {0.f, 19.1f, 5.2f};
588 static constexpr float kOy[3] = {0.f, 7.3f, 31.7f};
589 return fbm(x + kOx[channel], y + kOy[channel], octaves, lacunarity, gain);
590}
591
592// --- NoiseField3 -----------------------------------------------------------
593
594float NoiseField3::hash01(int ix, int iy, int iz) const {
595 ix = wrapIndex(ix, periodX);
596 iy = wrapIndex(iy, periodY);
597 iz = wrapIndex(iz, periodZ);
598 uint32_t h = seed;
599 h ^= uint32_t(ix) * 374761393u;
600 h ^= uint32_t(iy) * 668265263u;
601 h ^= uint32_t(iz) * 2246822519u;
602 h = (h ^ (h >> 13)) * 1274126177u;
603 h ^= h >> 16;
604 return float(h & 0x00FFFFFFu) / float(0x00FFFFFFu);
605}
606
607float NoiseField3::whiteNoise(int ix, int iy, int iz) const { return hash01(ix, iy, iz); }
608
609float NoiseField3::valueNoise(float x, float y, float z) const {
610 const int x0 = int(std::floor(x));
611 const int y0 = int(std::floor(y));
612 const int z0 = int(std::floor(z));
613 const float fx = fade(x - float(x0));
614 const float fy = fade(y - float(y0));
615 const float fz = fade(z - float(z0));
616
617 const float n000 = hash01(x0, y0, z0);
618 const float n100 = hash01(x0 + 1, y0, z0);
619 const float n010 = hash01(x0, y0 + 1, z0);
620 const float n110 = hash01(x0 + 1, y0 + 1, z0);
621 const float n001 = hash01(x0, y0, z0 + 1);
622 const float n101 = hash01(x0 + 1, y0, z0 + 1);
623 const float n011 = hash01(x0, y0 + 1, z0 + 1);
624 const float n111 = hash01(x0 + 1, y0 + 1, z0 + 1);
625
626 const float nx00 = n000 + (n100 - n000) * fx;
627 const float nx10 = n010 + (n110 - n010) * fx;
628 const float nx01 = n001 + (n101 - n001) * fx;
629 const float nx11 = n011 + (n111 - n011) * fx;
630 const float nxy0 = nx00 + (nx10 - nx00) * fy;
631 const float nxy1 = nx01 + (nx11 - nx01) * fy;
632 return nxy0 + (nxy1 - nxy0) * fz;
633}
634
635float NoiseField3::perlinNoise(float x, float y, float z) const {
636 const int x0 = int(std::floor(x));
637 const int y0 = int(std::floor(y));
638 const int z0 = int(std::floor(z));
639 const float dx = x - float(x0);
640 const float dy = y - float(y0);
641 const float dz = z - float(z0);
642 const float fx = fade5(dx);
643 const float fy = fade5(dy);
644 const float fz = fade5(dz);
645
646 auto grad = [&](int ix, int iy, int iz, float px, float py, float pz) -> float {
647 // 12 edge midpoints of a cube (Perlin 2002).
648 static const float g[12][3] = {{1, 1, 0}, {-1, 1, 0}, {1, -1, 0}, {-1, -1, 0}, {1, 0, 1}, {-1, 0, 1},
649 {1, 0, -1}, {-1, 0, -1}, {0, 1, 1}, {0, -1, 1}, {0, 1, -1}, {0, -1, -1}};
650 const int h = int(hash01(ix, iy, iz) * 11.999f) % 12;
651 return g[h][0] * (px - float(ix)) + g[h][1] * (py - float(iy)) + g[h][2] * (pz - float(iz));
652 };
653
654 const float n000 = grad(x0, y0, z0, x, y, z);
655 const float n100 = grad(x0 + 1, y0, z0, x, y, z);
656 const float n010 = grad(x0, y0 + 1, z0, x, y, z);
657 const float n110 = grad(x0 + 1, y0 + 1, z0, x, y, z);
658 const float n001 = grad(x0, y0, z0 + 1, x, y, z);
659 const float n101 = grad(x0 + 1, y0, z0 + 1, x, y, z);
660 const float n011 = grad(x0, y0 + 1, z0 + 1, x, y, z);
661 const float n111 = grad(x0 + 1, y0 + 1, z0 + 1, x, y, z);
662
663 const float nx00 = n000 + (n100 - n000) * fx;
664 const float nx10 = n010 + (n110 - n010) * fx;
665 const float nx01 = n001 + (n101 - n001) * fx;
666 const float nx11 = n011 + (n111 - n011) * fx;
667 const float nxy0 = nx00 + (nx10 - nx00) * fy;
668 const float nxy1 = nx01 + (nx11 - nx01) * fy;
669 const float n = nxy0 + (nxy1 - nxy0) * fz;
670 return std::clamp(n * 0.5f + 0.5f, 0.f, 1.f);
671}
672
673float NoiseField3::simplexNoise(float x, float y, float z) const {
674 // Ashima / webgl-noise 3D simplex.
675 constexpr float F3 = 1.f / 3.f;
676 constexpr float G3 = 1.f / 6.f;
677
678 const float s = (x + y + z) * F3;
679 const int i = int(std::floor(x + s));
680 const int j = int(std::floor(y + s));
681 const int k = int(std::floor(z + s));
682 const float t = float(i + j + k) * G3;
683 const float x0 = x - (float(i) - t);
684 const float y0 = y - (float(j) - t);
685 const float z0 = z - (float(k) - t);
686
687 int i1, j1, k1, i2, j2, k2;
688 if (x0 >= y0) {
689 if (y0 >= z0) {
690 i1 = 1;
691 j1 = 0;
692 k1 = 0;
693 i2 = 1;
694 j2 = 1;
695 k2 = 0;
696 } else if (x0 >= z0) {
697 i1 = 1;
698 j1 = 0;
699 k1 = 0;
700 i2 = 1;
701 j2 = 0;
702 k2 = 1;
703 } else {
704 i1 = 0;
705 j1 = 0;
706 k1 = 1;
707 i2 = 1;
708 j2 = 0;
709 k2 = 1;
710 }
711 } else {
712 if (y0 < z0) {
713 i1 = 0;
714 j1 = 0;
715 k1 = 1;
716 i2 = 0;
717 j2 = 1;
718 k2 = 1;
719 } else if (x0 < z0) {
720 i1 = 0;
721 j1 = 1;
722 k1 = 0;
723 i2 = 0;
724 j2 = 1;
725 k2 = 1;
726 } else {
727 i1 = 0;
728 j1 = 1;
729 k1 = 0;
730 i2 = 1;
731 j2 = 1;
732 k2 = 0;
733 }
734 }
735
736 const float x1 = x0 - float(i1) + G3;
737 const float y1 = y0 - float(j1) + G3;
738 const float z1 = z0 - float(k1) + G3;
739 const float x2 = x0 - float(i2) + 2.f * G3;
740 const float y2 = y0 - float(j2) + 2.f * G3;
741 const float z2 = z0 - float(k2) + 2.f * G3;
742 const float x3 = x0 - 1.f + 3.f * G3;
743 const float y3 = y0 - 1.f + 3.f * G3;
744 const float z3 = z0 - 1.f + 3.f * G3;
745
746 auto gradDot = [&](int ix, int iy, int iz, float px, float py, float pz) -> float {
747 static const float g[12][3] = {{1, 1, 0}, {-1, 1, 0}, {1, -1, 0}, {-1, -1, 0}, {1, 0, 1}, {-1, 0, 1},
748 {1, 0, -1}, {-1, 0, -1}, {0, 1, 1}, {0, -1, 1}, {0, 1, -1}, {0, -1, -1}};
749 const int h = int(hash01(ix, iy, iz) * 11.999f) % 12;
750 return g[h][0] * px + g[h][1] * py + g[h][2] * pz;
751 };
752
753 auto contrib = [&](float px, float py, float pz, int ix, int iy, int iz) -> float {
754 float t = 0.6f - px * px - py * py - pz * pz;
755 if (t < 0.f) return 0.f;
756 t *= t;
757 return t * t * gradDot(ix, iy, iz, px, py, pz);
758 };
759
760 const float n = 32.f * (contrib(x0, y0, z0, i, j, k) + contrib(x1, y1, z1, i + i1, j + j1, k + k1) +
761 contrib(x2, y2, z2, i + i2, j + j2, k + k2) + contrib(x3, y3, z3, i + 1, j + 1, k + 1));
762 return std::clamp(n * 0.5f + 0.5f, 0.f, 1.f);
763}
764
765float NoiseField3::worleyF1(float x, float y, float z, float jitter, WorleyMetric metric) const {
766 jitter = std::clamp(jitter, 0.f, 1.f);
767 const int ix = int(std::floor(x));
768 const int iy = int(std::floor(y));
769 const int iz = int(std::floor(z));
770 float f1 = 1e9f;
771 for (int k = -1; k <= 1; ++k) {
772 for (int j = -1; j <= 1; ++j) {
773 for (int i = -1; i <= 1; ++i) {
774 const int cx = ix + i;
775 const int cy = iy + j;
776 const int cz = iz + k;
777 const float px = float(cx) + 0.5f + (hash01(cx, cy, cz) - 0.5f) * jitter;
778 const float py = float(cy) + 0.5f + (hash01(cx + 17, cy + 3, cz + 5) - 0.5f) * jitter;
779 const float pz = float(cz) + 0.5f + (hash01(cx + 7, cy + 13, cz + 19) - 0.5f) * jitter;
780 f1 = std::min(f1, metricDist3(x - px, y - py, z - pz, metric));
781 }
782 }
783 }
784 return f1;
785}
786
787float NoiseField3::worleyNoise(float x, float y, float z, float jitter, WorleyMetric metric) const {
788 return std::clamp(1.f - worleyF1(x, y, z, jitter, metric), 0.f, 1.f);
789}
790
791float NoiseField3::fbm(float x, float y, float z, int octaves, float lacunarity, float gain) const {
792 octaves = std::max(1, octaves);
793 float sum = 0.f, amp = 1.f, norm = 0.f, freq = 1.f;
794 for (int i = 0; i < octaves; ++i) {
795 sum += valueNoise(x * freq, y * freq, z * freq) * amp;
796 norm += amp;
797 amp *= gain;
798 freq *= lacunarity;
799 }
800 return norm > 0.f ? sum / norm : 0.f;
801}
802
803float NoiseField3::fbmPerlin(float x, float y, float z, int octaves, float lacunarity, float gain) const {
804 octaves = std::max(1, octaves);
805 float sum = 0.f, amp = 1.f, norm = 0.f, freq = 1.f;
806 for (int i = 0; i < octaves; ++i) {
807 sum += perlinNoise(x * freq + float(i) * 17.8f, y * freq + float(i) * 23.5f, z * freq + float(i) * 11.1f) * amp;
808 norm += amp;
809 amp *= gain;
810 freq *= lacunarity;
811 }
812 return norm > 0.f ? sum / norm : 0.f;
813}
814
815float NoiseField3::ridged(float x, float y, float z, int octaves, float lacunarity, float gain) const {
816 octaves = std::max(1, octaves);
817 float sum = 0.f, amp = 1.f, norm = 0.f, freq = 1.f;
818 for (int i = 0; i < octaves; ++i) {
819 float n = valueNoise(x * freq, y * freq, z * freq);
820 n = 1.f - std::fabs(n * 2.f - 1.f);
821 sum += n * n * amp;
822 norm += amp;
823 amp *= gain;
824 freq *= lacunarity;
825 }
826 return norm > 0.f ? sum / norm : 0.f;
827}
828
829float NoiseField3::billow(float x, float y, float z, int octaves, float lacunarity, float gain) const {
830 octaves = std::max(1, octaves);
831 float sum = 0.f, amp = 1.f, norm = 0.f, freq = 1.f;
832 for (int i = 0; i < octaves; ++i) {
833 sum += std::fabs(valueNoise(x * freq, y * freq, z * freq) * 2.f - 1.f) * amp;
834 norm += amp;
835 amp *= gain;
836 freq *= lacunarity;
837 }
838 return norm > 0.f ? sum / norm : 0.f;
839}
840
841float NoiseField3::gyroid(float x, float y, float z) const {
842 const float p = float(seed) * 0.001f;
843 return remapSigned01(gyroidRaw(x + p, y + p * 1.3f, z + p * 0.7f) / 1.5f);
844}
845
846float NoiseField3::gyroidFbm(float x, float y, float z, int octaves) const {
847 octaves = std::max(1, std::min(octaves, 8));
848 float result = 0.f;
849 float a = 0.5f;
850 float seedZ = z;
851 float norm = 0.f;
852 const float ph = float(seed) * 0.001f;
853 for (int i = 0; i < octaves; ++i) {
854 seedZ += result * 0.5f;
855 result += std::fabs(gyroidRaw((x + ph) / a, (y + ph) / a, (seedZ + ph) / a)) * a;
856 norm += a;
857 a *= 0.5f;
858 }
859 return norm > 0.f ? std::clamp(result / (norm * 1.5f), 0.f, 1.f) : 0.f;
860}
861
862float NoiseField3::perlinWorley(float x, float y, float z, int octaves, float jitter) const {
863 return std::clamp(worleyNoise(x, y, z, jitter) * fbmPerlin(x, y, z, std::max(1, octaves)), 0.f, 1.f);
864}
865
866} // namespace eve::procgen
double z
double y
double m[16]
double x
float w
Definition AnimClip.cpp:738
const std::string & s
float cx
Definition CardTypes.cpp:36
float cy
Definition CardTypes.cpp:37
glm::vec4 p[6]
tensor::Graph g
Definition GpuGraph.cpp:7
float soft[4]
uint32_t i1
Definition Grass.cpp:61
uint32_t i2
Definition Grass.cpp:61
glm::vec3 n
Definition Grass.cpp:63
double r
float v
std::int32_t c
int h
std::vector< Colorf > px
std::uint32_t width
size_t offset
Poco::JSON::Object::Ptr result
MeleePoint3 a
Definition MeleeHit.cpp:40
glm::dvec3 dx
glm::dvec3 dz
float d
float t
float dy
Definition Shadow.cpp:151
float step
Definition TreeMesh.cpp:451
double weight
double oy
double ox
double gain
const Value * field(const Value &value, const char *key)
Borrow an optional object field.
WorleyMetric
Distance metric for Worley / cellular noise.
Definition NoiseField.h:10
float whiteNoise(int ix, int iy, int iz) const
float valueNoise(float x, float y, float z) const
float fbmPerlin(float x, float y, float z, int octaves=4, float lacunarity=2.f, float gain=0.5f) const
float worleyF1(float x, float y, float z, float jitter=1.f, WorleyMetric metric=WorleyMetric::Euclidean) const
float billow(float x, float y, float z, int octaves=4, float lacunarity=2.f, float gain=0.5f) const
float ridged(float x, float y, float z, int octaves=4, float lacunarity=2.f, float gain=0.5f) const
float simplexNoise(float x, float y, float z) const
float fbm(float x, float y, float z, int octaves=4, float lacunarity=2.f, float gain=0.5f) const
float gyroid(float x, float y, float z) const
float hash01(int ix, int iy, int iz) const
float gyroidFbm(float x, float y, float z, int octaves=4) const
float perlinWorley(float x, float y, float z, int octaves=3, float jitter=1.f) const
float worleyNoise(float x, float y, float z, float jitter=1.f, WorleyMetric metric=WorleyMetric::Euclidean) const
float perlinNoise(float x, float y, float z) const
float hexValue(float x, float y) const
Hash of the nearest hex cell, in [0, 1].
float fbm(float x, float y, int octaves=4, float lacunarity=2.f, float gain=0.5f) const
Value-noise fBm.
float worleySmooth(float x, float y, float smoothness=0.25f, float jitter=1.f) const
Smooth-min Worley (metaball / soft cellular).
float gyroidFbm(float x, float y, float z, int octaves=4) const
Absolute-gyroid fBm (noisecreater gyroid fbm), remapped to [0, 1].
float worleyCombine(float x, float y, float jitter=1.f, WorleyMetric metric=WorleyMetric::Euclidean) const
(F1 + F2) * 0.5 remapped via clamp(1 - d, 0, 1).
float gyroid(float x, float y, float z) const
Minimal gyroid field at (x, y, z): dot(sin(p), cos(p.yzx)), remapped to [0, 1].
float worleyNoise(float x, float y, float jitter=1.f, WorleyMetric metric=WorleyMetric::Euclidean) const
Texture-friendly Worley: clamp(1 - F1, 0, 1). Matches NoiseCreator's 1 - minDist cellular look.
float billow(float x, float y, int octaves=4, float lacunarity=2.f, float gain=0.5f) const
Billow / turbulence (absolute value-noise fBm), remapped to [0, 1]. Matches noisecreater SampleNoiseB...
float valueDy(float x, float y, float eps=1e-3f) const
∂/∂y of value noise via central differences.
float isoLine(float x, float y, int levels=8, float width=0.04f) const
Contour / isoline mask of value noise in [0, 1] (1 on lines).
float valueSlope(float x, float y, float eps=1e-3f) const
Gradient magnitude of value noise.
float simplexNoise(float x, float y) const
2D Simplex noise remapped to [0, 1]. Same family as noisecreater SampleNoiseA (Ashima-style corners +...
float fbmSimplex(float x, float y, int octaves=4, float lacunarity=2.f, float gain=0.5f) const
Simplex fBm with uncorrelated octave offsets.
float curlNoiseY(float x, float y, float eps=1e-3f) const
Divergence-free curl Y of value-noise potential (finite differences).
float fbmColor(float x, float y, int channel, int octaves=4, float lacunarity=2.f, float gain=0.5f) const
Decorrelated fBm channel for RGB-style color noise (channel 0/1/2). Same shape as fbm with fixed per-...
float worleyF2(float x, float y, float jitter=1.f, WorleyMetric metric=WorleyMetric::Euclidean) const
Worley F2 (second-nearest) distance.
float gabor(float x, float y, float dirX=0.f, float dirY=0.f, float frequency=6.2831853f) const
Gabor / Voronoi-mix wavelet (noisecreater gavoronoi 3×3), remapped to [0, 1].
float valueDx(float x, float y, float eps=1e-3f) const
∂/∂x of value noise via central differences (slope / erosion helper). Not remapped to [0,...
float ridged(float x, float y, int octaves=4, float lacunarity=2.f, float gain=0.5f) const
Ridged multifractal on value noise.
float jordan(float x, float y, int octaves=4, float lacunarity=2.f, float gain=0.5f, float warpStrength=0.35f) const
Musgrave Jordan turbulence: warp by value-noise derivatives. Completes the Swiss / Hybrid / Jordan Mu...
float gaborFbm(float x, float y, int octaves=4, float dirX=0.f, float dirY=0.f) const
Absolute-Gabor fBm remapped to [0, 1] (noisecreater Gabor swirl layers).
float whiteNoise(int ix, int iy) const
White noise at integer lattice point (alias of hash01).
float banding(float x, float y, int bands=5) const
Quantized banding of value noise in [0, 1].
float fbmPerlin(float x, float y, int octaves=4, float lacunarity=2.f, float gain=0.5f) const
Perlin fBm with uncorrelated octave offsets.
float warp(float x, float y, float amp, int octaves=3) const
Single-pass domain warp of value fBm.
float hexEdge(float x, float y, float width=0.05f, float soft=0.02f) const
Hex edge line mask in [0, 1] (1 on edge). Matches noisecreater hexNoise edge band.
float perlinWorley(float x, float y, int octaves=3, float jitter=1.f) const
Perlin–Worley hybrid for cloud / soft cellular shapes. Result is worleyNoise * fbmPerlin (NoiseCreato...
float worleyEdge(float x, float y, float jitter=1.f, WorleyMetric metric=WorleyMetric::Euclidean) const
Worley F2 - F1 edge distance (cell borders).
float hybrid(float x, float y, int octaves=4, float lacunarity=2.f, float gain=0.5f, float offset=0.7f) const
Musgrave hybrid multifractal.
float hexDistance(float x, float y) const
Distance to nearest hex edge (noisecreater hexDist field), typically [0, ~1]. Pointy-top lattice; sca...
float billowPerlin(float x, float y, int octaves=4, float lacunarity=2.f, float gain=0.5f) const
Billow built from Perlin octaves.
float warpDeep(float x, float y, float amp, int octaves=3) const
Two-pass iq-style domain warp (q then r), then sample fBm. Matches noisecreater DomainWarp / Smoke ne...
float hash01(int ix, int iy) const
Lattice hash mapped to [0, 1].
float perlinNoise(float x, float y) const
Classic 2D Perlin gradient noise, remapped to [0, 1].
float swiss(float x, float y, int octaves=4, float lacunarity=2.f, float gain=0.5f, float warpStrength=0.15f) const
Musgrave Swiss turbulence: each octave warps by the running sum. Good for eroded / alpine terrain....
float ridgedPerlin(float x, float y, int octaves=4, float lacunarity=2.f, float gain=0.5f) const
Ridged multifractal on Perlin.
float worleyF1(float x, float y, float jitter=1.f, WorleyMetric metric=WorleyMetric::Euclidean) const
Worley / cellular F1 distance (cell size 1).
float curlNoiseX(float x, float y, float eps=1e-3f) const
Divergence-free curl X of value-noise potential (finite differences). Pair with curlNoiseY for partic...
float fbmRotated(float x, float y, int octaves=4, float lacunarity=2.f, float gain=0.5f) const
Rotated value-noise fBm (axial-bias reduction). Each octave applies a fixed 2D rotation like noisecre...
float valueNoise(float x, float y) const
Smooth value noise.