Blender V5.3
noise.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2009-2010 Sony Pictures Imageworks Inc., et al.
2 * All Rights Reserved. (BSD-3-Clause).
3 * SPDX-FileCopyrightText: 2011 Blender Authors (GPL-2.0-or-later).
4 *
5 * SPDX-License-Identifier: GPL-2.0-or-later AND BSD-3-Clause */
6
10
11#include <algorithm>
12#include <cfloat>
13#include <cmath>
14#include <cstdint>
15
16#include "BLI_math_base.hh"
17#include "BLI_math_base_c.hh"
19#include "BLI_math_vector.hh"
20#include "BLI_noise.hh"
21#include "BLI_utildefines.hh"
22
23/* Some noise functions integer overflow as part of expected operation. */
24#ifdef __SANITIZE_ADDRESS__
25/* CLANG also supports GNU extensions. */
26# if defined(__GNUC__) || defined(__clang__)
27# define ATTR_NO_SIGNED_INT_OVERFLOW [[gnu::no_sanitize("signed-integer-overflow")]]
28# else
29/* TODO: MSVC, others as needed. */
30# define ATTR_NO_SIGNED_INT_OVERFLOW
31# endif
32#else
33# define ATTR_NO_SIGNED_INT_OVERFLOW
34#endif
35
36namespace blender::noise {
37
38/* -------------------------------------------------------------------- */
43
45{
46 return (x << k) | (x >> (32 - k));
47}
48
50{
51 a -= c;
52 a ^= hash_bit_rotate(c, 4);
53 c += b;
54 b -= a;
55 b ^= hash_bit_rotate(a, 6);
56 a += c;
57 c -= b;
58 c ^= hash_bit_rotate(b, 8);
59 b += a;
60 a -= c;
61 a ^= hash_bit_rotate(c, 16);
62 c += b;
63 b -= a;
64 b ^= hash_bit_rotate(a, 19);
65 a += c;
66 c -= b;
67 c ^= hash_bit_rotate(b, 4);
68 b += a;
69}
70
72{
73 c ^= b;
74 c -= hash_bit_rotate(b, 14);
75 a ^= c;
76 a -= hash_bit_rotate(c, 11);
77 b ^= a;
78 b -= hash_bit_rotate(a, 25);
79 c ^= b;
80 c -= hash_bit_rotate(b, 16);
81 a ^= c;
82 a -= hash_bit_rotate(c, 4);
83 b ^= a;
84 b -= hash_bit_rotate(a, 14);
85 c ^= b;
86 c -= hash_bit_rotate(b, 24);
87}
88
90{
91 uint32_t a, b, c;
92 a = b = c = 0xdeadbeef + (1 << 2) + 13;
93
94 a += kx;
95 hash_bit_final(a, b, c);
96
97 return c;
98}
99
101{
102 uint32_t a, b, c;
103 a = b = c = 0xdeadbeef + (2 << 2) + 13;
104
105 b += ky;
106 a += kx;
107 hash_bit_final(a, b, c);
108
109 return c;
110}
111
113{
114 uint32_t a, b, c;
115 a = b = c = 0xdeadbeef + (3 << 2) + 13;
116
117 c += kz;
118 b += ky;
119 a += kx;
120 hash_bit_final(a, b, c);
121
122 return c;
123}
124
126{
127 uint32_t a, b, c;
128 a = b = c = 0xdeadbeef + (4 << 2) + 13;
129
130 a += kx;
131 b += ky;
132 c += kz;
133 hash_bit_mix(a, b, c);
134
135 a += kw;
136 hash_bit_final(a, b, c);
137
138 return c;
139}
140
142{
143 union {
144 uint32_t i;
145 float f;
146 } u;
147 u.f = f;
148 return u.i;
149}
150
151/* PCG 2D, 3D and 4D hash functions,
152 * from "Hash Functions for GPU Rendering" JCGT 2020
153 * https://jcgt.org/published/0009/03/02/
154 *
155 * Slightly modified to only use signed integers,
156 * so that they can also be implemented in OSL. */
157
159static inline int2 hash_pcg2d_i(int2 v)
160{
161 v = v * int2(1664525) + int2(1013904223);
162 v.x += v.y * 1664525;
163 v.y += v.x * 1664525;
164 v = v ^ (v >> 16);
165 v.x += v.y * 1664525;
166 v.y += v.x * 1664525;
167 return v;
168}
169
171static inline int3 hash_pcg3d_i(int3 v)
172{
173 v = v * int3(1664525) + int3(1013904223);
174 v.x += v.y * v.z;
175 v.y += v.z * v.x;
176 v.z += v.x * v.y;
177 v = v ^ (v >> 16);
178 v.x += v.y * v.z;
179 v.y += v.z * v.x;
180 v.z += v.x * v.y;
181 return v;
182}
183
185static inline int4 hash_pcg4d_i(int4 v)
186{
187 v = v * int4(1664525) + int4(1013904223);
188 v.x += v.y * v.w;
189 v.y += v.z * v.x;
190 v.z += v.x * v.y;
191 v.w += v.y * v.z;
192 v = v ^ (v >> 16);
193 v.x += v.y * v.w;
194 v.y += v.z * v.x;
195 v.z += v.x * v.y;
196 v.w += v.y * v.z;
197 return v;
198}
199
201{
202 return hash(float_as_uint(kx));
203}
204
206{
207 return hash(float_as_uint(k.x), float_as_uint(k.y));
208}
209
211{
212 return hash(float_as_uint(k.x), float_as_uint(k.y), float_as_uint(k.z));
213}
214
216{
217 return hash(float_as_uint(k.x), float_as_uint(k.y), float_as_uint(k.z), float_as_uint(k.w));
218}
219
221{
222 return hash(hash_float(k.x), hash_float(k.y), hash_float(k.z), hash_float(k.w));
223}
224
225/* Hashing a number of uint32_t into a float in the range [0, 1]. */
226
228{
229 return float(k) / float(0xFFFFFFFFu);
230}
231
233{
234 return uint_to_float_01(hash(kx));
235}
236
238{
239 return uint_to_float_01(hash(kx, ky));
240}
241
243{
244 return uint_to_float_01(hash(kx, ky, kz));
245}
246
248{
249 return uint_to_float_01(hash(kx, ky, kz, kw));
250}
251
252/* Hashing a number of floats into a float in the range [0, 1]. */
253
255{
256 return uint_to_float_01(hash_float(k));
257}
258
260{
261 return uint_to_float_01(hash_float(k));
262}
263
265{
266 return uint_to_float_01(hash_float(k));
267}
268
270{
271 return uint_to_float_01(hash_float(k));
272}
273
278
280{
281 return float2(hash_float_to_float(float3(k.x, k.y, k.z)),
282 hash_float_to_float(float3(k.z, k.x, k.y)));
283}
284
286{
287 return float2(hash_float_to_float(float4(k.x, k.y, k.z, k.w)),
288 hash_float_to_float(float4(k.z, k.x, k.w, k.y)));
289}
290
292{
293 return float3(hash_float_to_float(k),
295 hash_float_to_float(float2(k, 2.0)));
296}
297
299{
300 return float3(hash_float_to_float(k),
301 hash_float_to_float(float3(k.x, k.y, 1.0)),
302 hash_float_to_float(float3(k.x, k.y, 2.0)));
303}
304
306{
307 return float3(hash_float_to_float(k),
308 hash_float_to_float(float4(k.x, k.y, k.z, 1.0)),
309 hash_float_to_float(float4(k.x, k.y, k.z, 2.0)));
310}
311
313{
314 return float3(hash_float_to_float(k),
315 hash_float_to_float(float4(k.z, k.x, k.w, k.y)),
316 hash_float_to_float(float4(k.w, k.z, k.y, k.x)));
317}
318
320{
321 return float4(hash_float_to_float(k),
322 hash_float_to_float(float4(k.w, k.x, k.y, k.z)),
323 hash_float_to_float(float4(k.z, k.w, k.x, k.y)),
324 hash_float_to_float(float4(k.y, k.z, k.w, k.x)));
325}
326
327/* Hashing a number of integers into floats in [0..1] range. */
328
329static inline float int_to_float_01(int32_t k)
330{
331 return float(k & 0x7fffffff) * (1.0f / float(0x7fffffff));
332}
333
335{
336 int2 h = hash_pcg2d_i(k);
337 return float2(int_to_float_01(h.x), int_to_float_01(h.y));
338}
339
341{
342 int3 h = hash_pcg3d_i(k);
343 return float3(int_to_float_01(h.x), int_to_float_01(h.y), int_to_float_01(h.z));
344}
345
347{
348 int4 h = hash_pcg4d_i(k);
349 return float4(
351}
352
354{
355 return hash_int3_to_float3(int3(k.x, k.y, 0));
356}
357
359{
360 return hash_int4_to_float4(k).xyz();
361}
362
364
365/* -------------------------------------------------------------------- */
374
375/* Linear Interpolation. */
376template<typename T> T static mix(T v0, T v1, float x)
377{
378 return (1 - x) * v0 + x * v1;
379}
380
381/* Bilinear Interpolation:
382 *
383 * v2 v3
384 * @ + + + + @ y
385 * + + ^
386 * + + |
387 * + + |
388 * @ + + + + @ @------> x
389 * v0 v1
390 */
391BLI_INLINE float mix(float v0, float v1, float v2, float v3, float x, float y)
392{
393 float x1 = 1.0 - x;
394 return (1.0 - y) * (v0 * x1 + v1 * x) + y * (v2 * x1 + v3 * x);
395}
396
397/* Trilinear Interpolation:
398 *
399 * v6 v7
400 * @ + + + + + + @
401 * +\ +\
402 * + \ + \
403 * + \ + \
404 * + \ v4 + \ v5
405 * + @ + + + +++ + @ z
406 * + + + + y ^
407 * v2 @ + +++ + + + @ v3 + \ |
408 * \ + \ + \ |
409 * \ + \ + \|
410 * \ + \ + +---------> x
411 * \+ \+
412 * @ + + + + + + @
413 * v0 v1
414 */
415BLI_INLINE float mix(float v0,
416 float v1,
417 float v2,
418 float v3,
419 float v4,
420 float v5,
421 float v6,
422 float v7,
423 float x,
424 float y,
425 float z)
426{
427 float x1 = 1.0 - x;
428 float y1 = 1.0 - y;
429 float z1 = 1.0 - z;
430 return z1 * (y1 * (v0 * x1 + v1 * x) + y * (v2 * x1 + v3 * x)) +
431 z * (y1 * (v4 * x1 + v5 * x) + y * (v6 * x1 + v7 * x));
432}
433
434/* Quadrilinear Interpolation. */
435BLI_INLINE float mix(float v0,
436 float v1,
437 float v2,
438 float v3,
439 float v4,
440 float v5,
441 float v6,
442 float v7,
443 float v8,
444 float v9,
445 float v10,
446 float v11,
447 float v12,
448 float v13,
449 float v14,
450 float v15,
451 float x,
452 float y,
453 float z,
454 float w)
455{
456 return mix(mix(v0, v1, v2, v3, v4, v5, v6, v7, x, y, z),
457 mix(v8, v9, v10, v11, v12, v13, v14, v15, x, y, z),
458 w);
459}
460
461BLI_INLINE float fade(float t)
462{
463 return t * t * t * (t * (t * 6.0 - 15.0) + 10.0);
464}
465
467{
468 return (condition != 0u) ? -value : value;
469}
470
472{
473 uint32_t h = hash & 15u;
474 float g = 1u + (h & 7u);
475 return negate_if(g, h & 8u) * x;
476}
477
478BLI_INLINE float noise_grad(uint32_t hash, float x, float y)
479{
480 uint32_t h = hash & 7u;
481 float u = h < 4u ? x : y;
482 float v = 2.0 * (h < 4u ? y : x);
483 return negate_if(u, h & 1u) + negate_if(v, h & 2u);
484}
485
486BLI_INLINE float noise_grad(uint32_t hash, float x, float y, float z)
487{
488 uint32_t h = hash & 15u;
489 float u = h < 8u ? x : y;
490 float vt = ELEM(h, 12u, 14u) ? x : z;
491 float v = h < 4u ? y : vt;
492 return negate_if(u, h & 1u) + negate_if(v, h & 2u);
493}
494
495BLI_INLINE float noise_grad(uint32_t hash, float x, float y, float z, float w)
496{
497 uint32_t h = hash & 31u;
498 float u = h < 24u ? x : y;
499 float v = h < 16u ? y : z;
500 float s = h < 8u ? z : w;
501 return negate_if(u, h & 1u) + negate_if(v, h & 2u) + negate_if(s, h & 4u);
502}
503
504BLI_INLINE float floor_fraction(float x, int &i)
505{
506 float x_floor = math::floor(x);
507 i = int(x_floor);
508 return x - x_floor;
509}
510
512{
513 int X;
514
515 float fx = floor_fraction(position, X);
516
517 float u = fade(fx);
518
519 float r = mix(noise_grad(hash(X), fx), noise_grad(hash(X + 1), fx - 1.0), u);
520
521 return r;
522}
523
525{
526 int X, Y;
527
528 float fx = floor_fraction(position.x, X);
529 float fy = floor_fraction(position.y, Y);
530
531 float u = fade(fx);
532 float v = fade(fy);
533
534 float r = mix(noise_grad(hash(X, Y), fx, fy),
535 noise_grad(hash(X + 1, Y), fx - 1.0, fy),
536 noise_grad(hash(X, Y + 1), fx, fy - 1.0),
537 noise_grad(hash(X + 1, Y + 1), fx - 1.0, fy - 1.0),
538 u,
539 v);
540
541 return r;
542}
543
545{
546 int X, Y, Z;
547
548 float fx = floor_fraction(position.x, X);
549 float fy = floor_fraction(position.y, Y);
550 float fz = floor_fraction(position.z, Z);
551
552 float u = fade(fx);
553 float v = fade(fy);
554 float w = fade(fz);
555
556 float r = mix(noise_grad(hash(X, Y, Z), fx, fy, fz),
557 noise_grad(hash(X + 1, Y, Z), fx - 1, fy, fz),
558 noise_grad(hash(X, Y + 1, Z), fx, fy - 1, fz),
559 noise_grad(hash(X + 1, Y + 1, Z), fx - 1, fy - 1, fz),
560 noise_grad(hash(X, Y, Z + 1), fx, fy, fz - 1),
561 noise_grad(hash(X + 1, Y, Z + 1), fx - 1, fy, fz - 1),
562 noise_grad(hash(X, Y + 1, Z + 1), fx, fy - 1, fz - 1),
563 noise_grad(hash(X + 1, Y + 1, Z + 1), fx - 1, fy - 1, fz - 1),
564 u,
565 v,
566 w);
567
568 return r;
569}
570
572{
573 int X, Y, Z, W;
574
575 float fx = floor_fraction(position.x, X);
576 float fy = floor_fraction(position.y, Y);
577 float fz = floor_fraction(position.z, Z);
578 float fw = floor_fraction(position.w, W);
579
580 float u = fade(fx);
581 float v = fade(fy);
582 float t = fade(fz);
583 float s = fade(fw);
584
585 float r = mix(
586 noise_grad(hash(X, Y, Z, W), fx, fy, fz, fw),
587 noise_grad(hash(X + 1, Y, Z, W), fx - 1.0, fy, fz, fw),
588 noise_grad(hash(X, Y + 1, Z, W), fx, fy - 1.0, fz, fw),
589 noise_grad(hash(X + 1, Y + 1, Z, W), fx - 1.0, fy - 1.0, fz, fw),
590 noise_grad(hash(X, Y, Z + 1, W), fx, fy, fz - 1.0, fw),
591 noise_grad(hash(X + 1, Y, Z + 1, W), fx - 1.0, fy, fz - 1.0, fw),
592 noise_grad(hash(X, Y + 1, Z + 1, W), fx, fy - 1.0, fz - 1.0, fw),
593 noise_grad(hash(X + 1, Y + 1, Z + 1, W), fx - 1.0, fy - 1.0, fz - 1.0, fw),
594 noise_grad(hash(X, Y, Z, W + 1), fx, fy, fz, fw - 1.0),
595 noise_grad(hash(X + 1, Y, Z, W + 1), fx - 1.0, fy, fz, fw - 1.0),
596 noise_grad(hash(X, Y + 1, Z, W + 1), fx, fy - 1.0, fz, fw - 1.0),
597 noise_grad(hash(X + 1, Y + 1, Z, W + 1), fx - 1.0, fy - 1.0, fz, fw - 1.0),
598 noise_grad(hash(X, Y, Z + 1, W + 1), fx, fy, fz - 1.0, fw - 1.0),
599 noise_grad(hash(X + 1, Y, Z + 1, W + 1), fx - 1.0, fy, fz - 1.0, fw - 1.0),
600 noise_grad(hash(X, Y + 1, Z + 1, W + 1), fx, fy - 1.0, fz - 1.0, fw - 1.0),
601 noise_grad(hash(X + 1, Y + 1, Z + 1, W + 1), fx - 1.0, fy - 1.0, fz - 1.0, fw - 1.0),
602 u,
603 v,
604 t,
605 s);
606
607 return r;
608}
609
610/* Signed versions of perlin noise in the range [-1, 1]. The scale values were computed
611 * experimentally by the OSL developers to remap the noise output to the correct range. */
612
614{
615 float precision_correction = 0.5f * float(math::abs(position) >= 1000000.0f);
616 /* Repeat Perlin noise texture every 100000.0 on each axis to prevent floating point
617 * representation issues. */
618 position = math::mod(position, 100000.0f) + precision_correction;
619
620 return perlin_noise(position) * 0.2500f;
621}
622
624{
625 float2 precision_correction = 0.5f * float2(float(math::abs(position.x) >= 1000000.0f),
626 float(math::abs(position.y) >= 1000000.0f));
627 /* Repeat Perlin noise texture every 100000.0f on each axis to prevent floating point
628 * representation issues. This causes discontinuities every 100000.0f, however at such scales
629 * this usually shouldn't be noticeable. */
630 position = math::mod(position, 100000.0f) + precision_correction;
631
632 return perlin_noise(position) * 0.6616f;
633}
634
636{
637 float3 precision_correction = 0.5f * float3(float(math::abs(position.x) >= 1000000.0f),
638 float(math::abs(position.y) >= 1000000.0f),
639 float(math::abs(position.z) >= 1000000.0f));
640 /* Repeat Perlin noise texture every 100000.0f on each axis to prevent floating point
641 * representation issues. This causes discontinuities every 100000.0f, however at such scales
642 * this usually shouldn't be noticeable. */
643 position = math::mod(position, 100000.0f) + precision_correction;
644
645 return perlin_noise(position) * 0.9820f;
646}
647
649{
650 float4 precision_correction = 0.5f * float4(float(math::abs(position.x) >= 1000000.0f),
651 float(math::abs(position.y) >= 1000000.0f),
652 float(math::abs(position.z) >= 1000000.0f),
653 float(math::abs(position.w) >= 1000000.0f));
654 /* Repeat Perlin noise texture every 100000.0f on each axis to prevent floating point
655 * representation issues. This causes discontinuities every 100000.0f, however at such scales
656 * this usually shouldn't be noticeable. */
657 position = math::mod(position, 100000.0f) + precision_correction;
658
659 return perlin_noise(position) * 0.8344f;
660}
661
662/* Positive versions of perlin noise in the range [0, 1]. */
663
664float perlin(float position)
665{
666 return perlin_signed(position) / 2.0f + 0.5f;
667}
668
670{
671 return perlin_signed(position) / 2.0f + 0.5f;
672}
673
675{
676 return perlin_signed(position) / 2.0f + 0.5f;
677}
678
680{
681 return perlin_signed(position) / 2.0f + 0.5f;
682}
683
684/* Fractal perlin noise. */
685
686/* fBM = Fractal Brownian Motion */
687template<typename T>
688#if defined(_MSC_VER) && _MSC_VER >= 1930
689/* The MSVC 2022 optimizer generates bad code for perlin_fractal_distorted when perlin_fbm gets
690 * inlined leading to incorrect results and failing tests that rely on perlin noise. For now just
691 * disable inlining for this function until we can get the compiler fixed. */
693#endif
694 float perlin_fbm(T p,
695 const float detail,
696 const float roughness,
697 const float lacunarity,
698 const bool normalize)
699{
700 float fscale = 1.0f;
701 float amp = 1.0f;
702 float maxamp = 0.0f;
703 float sum = 0.0f;
704
705 for (int i = 0; i <= int(detail); i++) {
706 float t = perlin_signed(fscale * p);
707 sum += t * amp;
708 maxamp += amp;
709 amp *= roughness;
710 fscale *= lacunarity;
711 }
712 float rmd = detail - std::floor(detail);
713 if (rmd != 0.0f) {
714 float t = perlin_signed(fscale * p);
715 float sum2 = sum + t * amp;
716 return normalize ? mix(0.5f * sum / maxamp + 0.5f, 0.5f * sum2 / (maxamp + amp) + 0.5f, rmd) :
717 mix(sum, sum2, rmd);
718 }
719 return normalize ? 0.5f * sum / maxamp + 0.5f : sum;
720}
721
722/* Explicit instantiation for Wave Texture. */
723template float perlin_fbm<float3>(float3 p,
724 const float detail,
725 const float roughness,
726 const float lacunarity,
727 const bool normalize);
728
729template float perlin_fbm<float2>(float2 p,
730 const float detail,
731 const float roughness,
732 const float lacunarity,
733 const bool normalize);
734
735template<typename T>
736float perlin_multi_fractal(T p, const float detail, const float roughness, const float lacunarity)
737{
738 float value = 1.0f;
739 float pwr = 1.0f;
740
741 for (int i = 0; i <= int(detail); i++) {
742 value *= (pwr * perlin_signed(p) + 1.0f);
743 pwr *= roughness;
744 p *= lacunarity;
745 }
746
747 const float rmd = detail - floorf(detail);
748 if (rmd != 0.0f) {
749 value *= (rmd * pwr * perlin_signed(p) + 1.0f); /* correct? */
750 }
751
752 return value;
753}
754
755template<typename T>
757 T p, const float detail, const float roughness, const float lacunarity, const float offset)
758{
759 float pwr = roughness;
760
761 /* First unscaled octave of function; later octaves are scaled. */
762 float value = offset + perlin_signed(p);
763 p *= lacunarity;
764
765 for (int i = 1; i <= int(detail); i++) {
766 float increment = (perlin_signed(p) + offset) * pwr * value;
767 value += increment;
768 pwr *= roughness;
769 p *= lacunarity;
770 }
771
772 const float rmd = detail - floorf(detail);
773 if (rmd != 0.0f) {
774 float increment = (perlin_signed(p) + offset) * pwr * value;
775 value += rmd * increment;
776 }
777
778 return value;
779}
780
781template<typename T>
783 const float detail,
784 const float roughness,
785 const float lacunarity,
786 const float offset,
787 const float gain)
788{
789 float pwr = 1.0f;
790 float value = 0.0f;
791 float weight = 1.0f;
792
793 for (int i = 0; (weight > 0.001f) && (i <= int(detail)); i++) {
794 weight = std::min(weight, 1.0f);
795
796 float signal = (perlin_signed(p) + offset) * pwr;
797 pwr *= roughness;
798 value += weight * signal;
799 weight *= gain * signal;
800 p *= lacunarity;
801 }
802
803 const float rmd = detail - floorf(detail);
804 if ((rmd != 0.0f) && (weight > 0.001f)) {
805 weight = std::min(weight, 1.0f);
806 float signal = (perlin_signed(p) + offset) * pwr;
807 value += rmd * weight * signal;
808 }
809
810 return value;
811}
812
813template<typename T>
815 const float detail,
816 const float roughness,
817 const float lacunarity,
818 const float offset,
819 const float gain)
820{
821 float pwr = roughness;
822
823 float signal = offset - std::abs(perlin_signed(p));
824 signal *= signal;
825 float value = signal;
826 float weight = 1.0f;
827
828 for (int i = 1; i <= int(detail); i++) {
829 p *= lacunarity;
830 weight = std::clamp(signal * gain, 0.0f, 1.0f);
831 signal = offset - std::abs(perlin_signed(p));
832 signal *= signal;
833 signal *= weight;
834 value += signal * pwr;
835 pwr *= roughness;
836 }
837
838 return value;
839}
840
841enum {
847};
848
849template<typename T>
851 float detail,
852 float roughness,
853 float lacunarity,
854 float offset,
855 float gain,
856 int type,
857 bool normalize)
858{
859 switch (type) {
861 return perlin_multi_fractal<T>(p, detail, roughness, lacunarity);
862 }
864 return perlin_fbm<T>(p, detail, roughness, lacunarity, normalize);
865 }
867 return perlin_hybrid_multi_fractal<T>(p, detail, roughness, lacunarity, offset, gain);
868 }
870 return perlin_ridged_multi_fractal<T>(p, detail, roughness, lacunarity, offset, gain);
871 }
873 return perlin_hetero_terrain<T>(p, detail, roughness, lacunarity, offset);
874 }
875 default: {
876 return 0.0;
877 }
878 }
879}
880
881/* The following offset functions generate random offsets to be added to
882 * positions to act as a seed since the noise functions don't have seed values.
883 * The offset's components are in the range [100, 200], not too high to cause
884 * bad precision and not too small to be noticeable. We use float seed because
885 * OSL only supports float hashes and we need to maintain compatibility with it.
886 */
887
889{
890 return 100.0f + hash_float_to_float(seed) * 100.0f;
891}
892
894{
895 return float2(100.0f + hash_float_to_float(float2(seed, 0.0f)) * 100.0f,
896 100.0f + hash_float_to_float(float2(seed, 1.0f)) * 100.0f);
897}
898
900{
901 return float3(100.0f + hash_float_to_float(float2(seed, 0.0f)) * 100.0f,
902 100.0f + hash_float_to_float(float2(seed, 1.0f)) * 100.0f,
903 100.0f + hash_float_to_float(float2(seed, 2.0f)) * 100.0f);
904}
905
907{
908 return float4(100.0f + hash_float_to_float(float2(seed, 0.0f)) * 100.0f,
909 100.0f + hash_float_to_float(float2(seed, 1.0f)) * 100.0f,
910 100.0f + hash_float_to_float(float2(seed, 2.0f)) * 100.0f,
911 100.0f + hash_float_to_float(float2(seed, 3.0f)) * 100.0f);
912}
913
914/* Perlin noises to be added to the position to distort other noises. */
915
916BLI_INLINE float perlin_distortion(float position, float strength)
917{
918 return perlin_signed(position + random_float_offset(0.0)) * strength;
919}
920
922{
923 return float2(perlin_signed(position + random_float2_offset(0.0f)) * strength,
924 perlin_signed(position + random_float2_offset(1.0f)) * strength);
925}
926
928{
929 return float3(perlin_signed(position + random_float3_offset(0.0f)) * strength,
930 perlin_signed(position + random_float3_offset(1.0f)) * strength,
931 perlin_signed(position + random_float3_offset(2.0f)) * strength);
932}
933
935{
936 return float4(perlin_signed(position + random_float4_offset(0.0f)) * strength,
937 perlin_signed(position + random_float4_offset(1.0f)) * strength,
938 perlin_signed(position + random_float4_offset(2.0f)) * strength,
939 perlin_signed(position + random_float4_offset(3.0f)) * strength);
940}
941
942/* Distorted fractal perlin noise. */
943
944template<typename T>
946 float detail,
947 float roughness,
948 float lacunarity,
949 float offset,
950 float gain,
951 float distortion,
952 int type,
953 bool normalize)
954{
955 position += perlin_distortion(position, distortion);
956 return perlin_select<T>(position, detail, roughness, lacunarity, offset, gain, type, normalize);
957}
958
960 float detail,
961 float roughness,
962 float lacunarity,
963 float offset,
964 float gain,
965 float distortion,
966 int type,
967 bool normalize);
969 float detail,
970 float roughness,
971 float lacunarity,
972 float offset,
973 float gain,
974 float distortion,
975 int type,
976 bool normalize);
978 float detail,
979 float roughness,
980 float lacunarity,
981 float offset,
982 float gain,
983 float distortion,
984 int type,
985 bool normalize);
987 float detail,
988 float roughness,
989 float lacunarity,
990 float offset,
991 float gain,
992 float distortion,
993 int type,
994 bool normalize);
995
996/* Distorted fractal perlin noise that outputs a float3. The arbitrary seeds are for
997 * compatibility with shading functions. */
998
1000 float detail,
1001 float roughness,
1002 float lacunarity,
1003 float offset,
1004 float gain,
1005 float distortion,
1006 int type,
1007 bool normalize)
1008{
1009 position += perlin_distortion(position, distortion);
1010 return float3(
1011 perlin_select<float>(position, detail, roughness, lacunarity, offset, gain, type, normalize),
1013 detail,
1014 roughness,
1015 lacunarity,
1016 offset,
1017 gain,
1018 type,
1019 normalize),
1021 detail,
1022 roughness,
1023 lacunarity,
1024 offset,
1025 gain,
1026 type,
1027 normalize));
1028}
1029
1031 float detail,
1032 float roughness,
1033 float lacunarity,
1034 float offset,
1035 float gain,
1036 float distortion,
1037 int type,
1038 bool normalize)
1039{
1040 position += perlin_distortion(position, distortion);
1042 position, detail, roughness, lacunarity, offset, gain, type, normalize),
1044 detail,
1045 roughness,
1046 lacunarity,
1047 offset,
1048 gain,
1049 type,
1050 normalize),
1052 detail,
1053 roughness,
1054 lacunarity,
1055 offset,
1056 gain,
1057 type,
1058 normalize));
1059}
1060
1062 float detail,
1063 float roughness,
1064 float lacunarity,
1065 float offset,
1066 float gain,
1067 float distortion,
1068 int type,
1069 bool normalize)
1070{
1071 position += perlin_distortion(position, distortion);
1073 position, detail, roughness, lacunarity, offset, gain, type, normalize),
1075 detail,
1076 roughness,
1077 lacunarity,
1078 offset,
1079 gain,
1080 type,
1081 normalize),
1083 detail,
1084 roughness,
1085 lacunarity,
1086 offset,
1087 gain,
1088 type,
1089 normalize));
1090}
1091
1093 float detail,
1094 float roughness,
1095 float lacunarity,
1096 float offset,
1097 float gain,
1098 float distortion,
1099 int type,
1100 bool normalize)
1101{
1102 position += perlin_distortion(position, distortion);
1104 position, detail, roughness, lacunarity, offset, gain, type, normalize),
1106 detail,
1107 roughness,
1108 lacunarity,
1109 offset,
1110 gain,
1111 type,
1112 normalize),
1114 detail,
1115 roughness,
1116 lacunarity,
1117 offset,
1118 gain,
1119 type,
1120 normalize));
1121}
1122
1124
1125/* -------------------------------------------------------------------- */
1144
1145/* Ensure to align with DNA. */
1146
1147enum {
1152};
1153
1154enum {
1160};
1161
1162/* ***** Distances ***** */
1163
1164float voronoi_distance(const float a, const float b)
1165{
1166 return std::abs(b - a);
1167}
1168
1169float voronoi_distance(const float2 a, const float2 b, const VoronoiParams &params)
1170{
1171 switch (params.metric) {
1173 return math::distance(a, b);
1175 return std::abs(a.x - b.x) + std::abs(a.y - b.y);
1177 return std::max(std::abs(a.x - b.x), std::abs(a.y - b.y));
1179 return std::pow(std::pow(std::abs(a.x - b.x), params.exponent) +
1180 std::pow(std::abs(a.y - b.y), params.exponent),
1181 1.0f / params.exponent);
1182 default:
1184 break;
1185 }
1186 return 0.0f;
1187}
1188
1189float voronoi_distance(const float3 a, const float3 b, const VoronoiParams &params)
1190{
1191 switch (params.metric) {
1193 return math::distance(a, b);
1195 return std::abs(a.x - b.x) + std::abs(a.y - b.y) + std::abs(a.z - b.z);
1197 return std::max({std::abs(a.x - b.x), std::abs(a.y - b.y), std::abs(a.z - b.z)});
1199 return std::pow(std::pow(std::abs(a.x - b.x), params.exponent) +
1200 std::pow(std::abs(a.y - b.y), params.exponent) +
1201 std::pow(std::abs(a.z - b.z), params.exponent),
1202 1.0f / params.exponent);
1203 default:
1205 break;
1206 }
1207 return 0.0f;
1208}
1209
1210float voronoi_distance(const float4 a, const float4 b, const VoronoiParams &params)
1211{
1212 switch (params.metric) {
1214 return math::distance(a, b);
1216 return std::abs(a.x - b.x) + std::abs(a.y - b.y) + std::abs(a.z - b.z) + std::abs(a.w - b.w);
1218 return std::max(
1219 {std::abs(a.x - b.x), std::abs(a.y - b.y), std::abs(a.z - b.z), std::abs(a.w - b.w)});
1221 return std::pow(std::pow(std::abs(a.x - b.x), params.exponent) +
1222 std::pow(std::abs(a.y - b.y), params.exponent) +
1223 std::pow(std::abs(a.z - b.z), params.exponent) +
1224 std::pow(std::abs(a.w - b.w), params.exponent),
1225 1.0f / params.exponent);
1226 default:
1228 break;
1229 }
1230 return 0.0f;
1231}
1232
1233/* Possibly cheaper/faster version of Voronoi distance, in a way that does not change
1234 * logic of "which distance is the closest?". */
1235template<typename T>
1236static float voronoi_distance_bound(const T a, const T b, const VoronoiParams &params)
1237{
1238 if (params.metric == NOISE_SHD_VORONOI_EUCLIDEAN) {
1239 return math::length_squared(a - b);
1240 }
1241 return voronoi_distance(a, b, params);
1242}
1243
1244/* **** 1D Voronoi **** */
1245
1246float4 voronoi_position(const float coord)
1247{
1248 return {0.0f, 0.0f, 0.0f, coord};
1249}
1250
1251VoronoiOutput voronoi_f1(const VoronoiParams &params, const float coord)
1252{
1253 float cellPosition = floorf(coord);
1254 float localPosition = coord - cellPosition;
1255
1256 float minDistance = FLT_MAX;
1257 float targetOffset = 0.0f;
1258 float targetPosition = 0.0f;
1259 for (int i = -1; i <= 1; i++) {
1260 float cellOffset = i;
1261 float pointPosition = cellOffset +
1262 hash_float_to_float(cellPosition + cellOffset) * params.randomness;
1263 float distanceToPoint = voronoi_distance(pointPosition, localPosition);
1264 if (distanceToPoint < minDistance) {
1265 targetOffset = cellOffset;
1266 minDistance = distanceToPoint;
1267 targetPosition = pointPosition;
1268 }
1269 }
1270
1271 VoronoiOutput octave;
1272 octave.distance = minDistance;
1273 octave.color = hash_float_to_float3(cellPosition + targetOffset);
1274 octave.position = voronoi_position(targetPosition + cellPosition);
1275 return octave;
1276}
1277
1279 const float coord,
1280 const bool calc_color)
1281{
1282 float cellPosition = floorf(coord);
1283 float localPosition = coord - cellPosition;
1284
1285 float smoothDistance = 0.0f;
1286 float smoothPosition = 0.0f;
1287 float3 smoothColor = {0.0f, 0.0f, 0.0f};
1288 float h = -1.0f;
1289 for (int i = -2; i <= 2; i++) {
1290 float cellOffset = i;
1291 float pointPosition = cellOffset +
1292 hash_float_to_float(cellPosition + cellOffset) * params.randomness;
1293 float distanceToPoint = voronoi_distance(pointPosition, localPosition);
1294 h = h == -1.0f ?
1295 1.0f :
1296 smoothstep(
1297 0.0f, 1.0f, 0.5f + 0.5f * (smoothDistance - distanceToPoint) / params.smoothness);
1298 float correctionFactor = params.smoothness * h * (1.0f - h);
1299 smoothDistance = mix(smoothDistance, distanceToPoint, h) - correctionFactor;
1300 correctionFactor /= 1.0f + 3.0f * params.smoothness;
1301 float3 cellColor = hash_float_to_float3(cellPosition + cellOffset);
1302 if (calc_color) {
1303 /* Only compute Color output if necessary, as it is very expensive. */
1304 smoothColor = mix(smoothColor, cellColor, h) - correctionFactor;
1305 }
1306 smoothPosition = mix(smoothPosition, pointPosition, h) - correctionFactor;
1307 }
1308
1309 VoronoiOutput octave;
1310 octave.distance = smoothDistance;
1311 octave.color = smoothColor;
1312 octave.position = voronoi_position(cellPosition + smoothPosition);
1313 return octave;
1314}
1315
1316VoronoiOutput voronoi_f2(const VoronoiParams &params, const float coord)
1317{
1318 float cellPosition = floorf(coord);
1319 float localPosition = coord - cellPosition;
1320
1321 float distanceF1 = FLT_MAX;
1322 float distanceF2 = FLT_MAX;
1323 float offsetF1 = 0.0f;
1324 float positionF1 = 0.0f;
1325 float offsetF2 = 0.0f;
1326 float positionF2 = 0.0f;
1327 for (int i = -1; i <= 1; i++) {
1328 float cellOffset = i;
1329 float pointPosition = cellOffset +
1330 hash_float_to_float(cellPosition + cellOffset) * params.randomness;
1331 float distanceToPoint = voronoi_distance(pointPosition, localPosition);
1332 if (distanceToPoint < distanceF1) {
1333 distanceF2 = distanceF1;
1334 distanceF1 = distanceToPoint;
1335 offsetF2 = offsetF1;
1336 offsetF1 = cellOffset;
1337 positionF2 = positionF1;
1338 positionF1 = pointPosition;
1339 }
1340 else if (distanceToPoint < distanceF2) {
1341 distanceF2 = distanceToPoint;
1342 offsetF2 = cellOffset;
1343 positionF2 = pointPosition;
1344 }
1345 }
1346
1347 VoronoiOutput octave;
1348 octave.distance = distanceF2;
1349 octave.color = hash_float_to_float3(cellPosition + offsetF2);
1350 octave.position = voronoi_position(positionF2 + cellPosition);
1351 return octave;
1352}
1353
1354float voronoi_distance_to_edge(const VoronoiParams &params, const float coord)
1355{
1356 float cellPosition = floorf(coord);
1357 float localPosition = coord - cellPosition;
1358
1359 float midPointPosition = hash_float_to_float(cellPosition) * params.randomness;
1360 float leftPointPosition = -1.0f + hash_float_to_float(cellPosition - 1.0f) * params.randomness;
1361 float rightPointPosition = 1.0f + hash_float_to_float(cellPosition + 1.0f) * params.randomness;
1362 float distanceToMidLeft = fabsf((midPointPosition + leftPointPosition) / 2.0f - localPosition);
1363 float distanceToMidRight = fabsf((midPointPosition + rightPointPosition) / 2.0f - localPosition);
1364
1365 return math::min(distanceToMidLeft, distanceToMidRight);
1366}
1367
1368float voronoi_n_sphere_radius(const VoronoiParams &params, const float coord)
1369{
1370 float cellPosition = floorf(coord);
1371 float localPosition = coord - cellPosition;
1372
1373 float closestPoint = 0.0f;
1374 float closestPointOffset = 0.0f;
1375 float minDistance = FLT_MAX;
1376 for (int i = -1; i <= 1; i++) {
1377 float cellOffset = i;
1378 float pointPosition = cellOffset +
1379 hash_float_to_float(cellPosition + cellOffset) * params.randomness;
1380 float distanceToPoint = fabsf(pointPosition - localPosition);
1381 if (distanceToPoint < minDistance) {
1382 minDistance = distanceToPoint;
1383 closestPoint = pointPosition;
1384 closestPointOffset = cellOffset;
1385 }
1386 }
1387
1388 minDistance = FLT_MAX;
1389 float closestPointToClosestPoint = 0.0f;
1390 for (int i = -1; i <= 1; i++) {
1391 if (i == 0) {
1392 continue;
1393 }
1394 float cellOffset = i + closestPointOffset;
1395 float pointPosition = cellOffset +
1396 hash_float_to_float(cellPosition + cellOffset) * params.randomness;
1397 float distanceToPoint = fabsf(closestPoint - pointPosition);
1398 if (distanceToPoint < minDistance) {
1399 minDistance = distanceToPoint;
1400 closestPointToClosestPoint = pointPosition;
1401 }
1402 }
1403
1404 return fabsf(closestPointToClosestPoint - closestPoint) / 2.0f;
1405}
1406
1407/* **** 2D Voronoi **** */
1408
1410{
1411 return {coord.x, coord.y, 0.0f, 0.0f};
1412}
1413
1415{
1416 float2 cellPosition_f = math::floor(coord);
1417 float2 localPosition = coord - cellPosition_f;
1418 int2 cellPosition = int2(cellPosition_f);
1419
1420 float minDistance = FLT_MAX;
1421 int2 targetOffset = {0, 0};
1422 float2 targetPosition = {0.0f, 0.0f};
1423 for (int j = -1; j <= 1; j++) {
1424 for (int i = -1; i <= 1; i++) {
1425 int2 cellOffset(i, j);
1426 float2 pointPosition = float2(cellOffset) +
1427 hash_int2_to_float2(cellPosition + cellOffset) * params.randomness;
1428 float distanceToPoint = voronoi_distance_bound(pointPosition, localPosition, params);
1429 if (distanceToPoint < minDistance) {
1430 targetOffset = cellOffset;
1431 minDistance = distanceToPoint;
1432 targetPosition = pointPosition;
1433 }
1434 }
1435 }
1436
1437 VoronoiOutput octave;
1438 octave.distance = voronoi_distance(targetPosition, localPosition, params);
1439 octave.color = hash_int2_to_float3(cellPosition + targetOffset);
1440 octave.position = voronoi_position(targetPosition + cellPosition_f);
1441 return octave;
1442}
1443
1445 const float2 coord,
1446 const bool calc_color)
1447{
1448 float2 cellPosition_f = math::floor(coord);
1449 float2 localPosition = coord - cellPosition_f;
1450 int2 cellPosition = int2(cellPosition_f);
1451
1452 float smoothDistance = 0.0f;
1453 float3 smoothColor = {0.0f, 0.0f, 0.0f};
1454 float2 smoothPosition = {0.0f, 0.0f};
1455 float h = -1.0f;
1456 for (int j = -2; j <= 2; j++) {
1457 for (int i = -2; i <= 2; i++) {
1458 int2 cellOffset(i, j);
1459 float2 pointPosition = float2(cellOffset) +
1460 hash_int2_to_float2(cellPosition + cellOffset) * params.randomness;
1461 float distanceToPoint = voronoi_distance(pointPosition, localPosition, params);
1462 h = h == -1.0f ?
1463 1.0f :
1464 smoothstep(0.0f,
1465 1.0f,
1466 0.5f + 0.5f * (smoothDistance - distanceToPoint) / params.smoothness);
1467 float correctionFactor = params.smoothness * h * (1.0f - h);
1468 smoothDistance = mix(smoothDistance, distanceToPoint, h) - correctionFactor;
1469 correctionFactor /= 1.0f + 3.0f * params.smoothness;
1470 if (calc_color) {
1471 /* Only compute Color output if necessary, as it is very expensive. */
1472 float3 cellColor = hash_int2_to_float3(cellPosition + cellOffset);
1473 smoothColor = mix(smoothColor, cellColor, h) - correctionFactor;
1474 }
1475 smoothPosition = mix(smoothPosition, pointPosition, h) - correctionFactor;
1476 }
1477 }
1478
1479 VoronoiOutput octave;
1480 octave.distance = smoothDistance;
1481 octave.color = smoothColor;
1482 octave.position = voronoi_position(cellPosition_f + smoothPosition);
1483 return octave;
1484}
1485
1487{
1488 float2 cellPosition_f = math::floor(coord);
1489 float2 localPosition = coord - cellPosition_f;
1490 int2 cellPosition = int2(cellPosition_f);
1491
1492 float distanceF1 = FLT_MAX;
1493 float distanceF2 = FLT_MAX;
1494 int2 offsetF1 = {0, 0};
1495 float2 positionF1 = {0.0f, 0.0f};
1496 int2 offsetF2 = {0, 0};
1497 float2 positionF2 = {0.0f, 0.0f};
1498 for (int j = -1; j <= 1; j++) {
1499 for (int i = -1; i <= 1; i++) {
1500 int2 cellOffset(i, j);
1501 float2 pointPosition = float2(cellOffset) +
1502 hash_int2_to_float2(cellPosition + cellOffset) * params.randomness;
1503 float distanceToPoint = voronoi_distance(pointPosition, localPosition, params);
1504 if (distanceToPoint < distanceF1) {
1505 distanceF2 = distanceF1;
1506 distanceF1 = distanceToPoint;
1507 offsetF2 = offsetF1;
1508 offsetF1 = cellOffset;
1509 positionF2 = positionF1;
1510 positionF1 = pointPosition;
1511 }
1512 else if (distanceToPoint < distanceF2) {
1513 distanceF2 = distanceToPoint;
1514 offsetF2 = cellOffset;
1515 positionF2 = pointPosition;
1516 }
1517 }
1518 }
1519
1520 VoronoiOutput octave;
1521 octave.distance = distanceF2;
1522 octave.color = hash_int2_to_float3(cellPosition + offsetF2);
1523 octave.position = voronoi_position(positionF2 + cellPosition_f);
1524 return octave;
1525}
1526
1527float voronoi_distance_to_edge(const VoronoiParams &params, const float2 coord)
1528{
1529 float2 cellPosition_f = math::floor(coord);
1530 float2 localPosition = coord - cellPosition_f;
1531 int2 cellPosition = int2(cellPosition_f);
1532
1533 float2 vectorToClosest = {0.0f, 0.0f};
1534 float minDistance = FLT_MAX;
1535 for (int j = -1; j <= 1; j++) {
1536 for (int i = -1; i <= 1; i++) {
1537 int2 cellOffset(i, j);
1538 float2 vectorToPoint = float2(cellOffset) +
1539 hash_int2_to_float2(cellPosition + cellOffset) * params.randomness -
1540 localPosition;
1541 float distanceToPoint = math::dot(vectorToPoint, vectorToPoint);
1542 if (distanceToPoint < minDistance) {
1543 minDistance = distanceToPoint;
1544 vectorToClosest = vectorToPoint;
1545 }
1546 }
1547 }
1548
1549 minDistance = FLT_MAX;
1550 for (int j = -1; j <= 1; j++) {
1551 for (int i = -1; i <= 1; i++) {
1552 int2 cellOffset(i, j);
1553 float2 vectorToPoint = float2(cellOffset) +
1554 hash_int2_to_float2(cellPosition + cellOffset) * params.randomness -
1555 localPosition;
1556 float2 perpendicularToEdge = vectorToPoint - vectorToClosest;
1557 if (math::dot(perpendicularToEdge, perpendicularToEdge) > 0.0001f) {
1558 float distanceToEdge = math::dot((vectorToClosest + vectorToPoint) / 2.0f,
1559 math::normalize(perpendicularToEdge));
1560 minDistance = math::min(minDistance, distanceToEdge);
1561 }
1562 }
1563 }
1564
1565 return minDistance;
1566}
1567
1568float voronoi_n_sphere_radius(const VoronoiParams &params, const float2 coord)
1569{
1570 float2 cellPosition_f = math::floor(coord);
1571 float2 localPosition = coord - cellPosition_f;
1572 int2 cellPosition = int2(cellPosition_f);
1573
1574 float2 closestPoint = {0.0f, 0.0f};
1575 int2 closestPointOffset = {0, 0};
1576 float minDistanceSq = FLT_MAX;
1577 for (int j = -1; j <= 1; j++) {
1578 for (int i = -1; i <= 1; i++) {
1579 int2 cellOffset(i, j);
1580 float2 pointPosition = float2(cellOffset) +
1581 hash_int2_to_float2(cellPosition + cellOffset) * params.randomness;
1582 float distanceToPointSq = math::length_squared(pointPosition - localPosition);
1583 if (distanceToPointSq < minDistanceSq) {
1584 minDistanceSq = distanceToPointSq;
1585 closestPoint = pointPosition;
1586 closestPointOffset = cellOffset;
1587 }
1588 }
1589 }
1590
1591 minDistanceSq = FLT_MAX;
1592 float2 closestPointToClosestPoint = {0.0f, 0.0f};
1593 for (int j = -1; j <= 1; j++) {
1594 for (int i = -1; i <= 1; i++) {
1595 if (i == 0 && j == 0) {
1596 continue;
1597 }
1598 int2 cellOffset = int2(i, j) + closestPointOffset;
1599 float2 pointPosition = float2(cellOffset) +
1600 hash_int2_to_float2(cellPosition + cellOffset) * params.randomness;
1601 float distanceToPointSq = math::length_squared(closestPoint - pointPosition);
1602 if (distanceToPointSq < minDistanceSq) {
1603 minDistanceSq = distanceToPointSq;
1604 closestPointToClosestPoint = pointPosition;
1605 }
1606 }
1607 }
1608
1609 return math::distance(closestPointToClosestPoint, closestPoint) / 2.0f;
1610}
1611
1612/* **** 3D Voronoi **** */
1613
1615{
1616 return {coord.x, coord.y, coord.z, 0.0f};
1617}
1618
1620{
1621 float3 cellPosition_f = math::floor(coord);
1622 float3 localPosition = coord - cellPosition_f;
1623 int3 cellPosition = int3(cellPosition_f);
1624
1625 float minDistance = FLT_MAX;
1626 int3 targetOffset = {0, 0, 0};
1627 float3 targetPosition = {0.0f, 0.0f, 0.0f};
1628 for (int k = -1; k <= 1; k++) {
1629 for (int j = -1; j <= 1; j++) {
1630 for (int i = -1; i <= 1; i++) {
1631 int3 cellOffset(i, j, k);
1632 float3 pointPosition = float3(cellOffset) +
1633 hash_int3_to_float3(cellPosition + cellOffset) * params.randomness;
1634 float distanceToPoint = voronoi_distance_bound(pointPosition, localPosition, params);
1635 if (distanceToPoint < minDistance) {
1636 targetOffset = cellOffset;
1637 minDistance = distanceToPoint;
1638 targetPosition = pointPosition;
1639 }
1640 }
1641 }
1642 }
1643
1644 VoronoiOutput octave;
1645 octave.distance = voronoi_distance(targetPosition, localPosition, params);
1646 octave.color = hash_int3_to_float3(cellPosition + targetOffset);
1647 octave.position = voronoi_position(targetPosition + cellPosition_f);
1648 return octave;
1649}
1650
1652 const float3 coord,
1653 const bool calc_color)
1654{
1655 float3 cellPosition_f = math::floor(coord);
1656 float3 localPosition = coord - cellPosition_f;
1657 int3 cellPosition = int3(cellPosition_f);
1658
1659 float smoothDistance = 0.0f;
1660 float3 smoothColor = {0.0f, 0.0f, 0.0f};
1661 float3 smoothPosition = {0.0f, 0.0f, 0.0f};
1662 float h = -1.0f;
1663 for (int k = -2; k <= 2; k++) {
1664 for (int j = -2; j <= 2; j++) {
1665 for (int i = -2; i <= 2; i++) {
1666 int3 cellOffset(i, j, k);
1667 float3 pointPosition = float3(cellOffset) +
1668 hash_int3_to_float3(cellPosition + cellOffset) * params.randomness;
1669 float distanceToPoint = voronoi_distance(pointPosition, localPosition, params);
1670 h = h == -1.0f ?
1671 1.0f :
1672 smoothstep(0.0f,
1673 1.0f,
1674 0.5f + 0.5f * (smoothDistance - distanceToPoint) / params.smoothness);
1675 float correctionFactor = params.smoothness * h * (1.0f - h);
1676 smoothDistance = mix(smoothDistance, distanceToPoint, h) - correctionFactor;
1677 correctionFactor /= 1.0f + 3.0f * params.smoothness;
1678 if (calc_color) {
1679 /* Only compute Color output if necessary, as it is very expensive. */
1680 float3 cellColor = hash_int3_to_float3(cellPosition + cellOffset);
1681 smoothColor = mix(smoothColor, cellColor, h) - correctionFactor;
1682 }
1683 smoothPosition = mix(smoothPosition, pointPosition, h) - correctionFactor;
1684 }
1685 }
1686 }
1687
1688 VoronoiOutput octave;
1689 octave.distance = smoothDistance;
1690 octave.color = smoothColor;
1691 octave.position = voronoi_position(cellPosition_f + smoothPosition);
1692 return octave;
1693}
1694
1696{
1697 float3 cellPosition_f = math::floor(coord);
1698 float3 localPosition = coord - cellPosition_f;
1699 int3 cellPosition = int3(cellPosition_f);
1700
1701 float distanceF1 = FLT_MAX;
1702 float distanceF2 = FLT_MAX;
1703 int3 offsetF1 = {0, 0, 0};
1704 float3 positionF1 = {0.0f, 0.0f, 0.0f};
1705 int3 offsetF2 = {0, 0, 0};
1706 float3 positionF2 = {0.0f, 0.0f, 0.0f};
1707 for (int k = -1; k <= 1; k++) {
1708 for (int j = -1; j <= 1; j++) {
1709 for (int i = -1; i <= 1; i++) {
1710 int3 cellOffset(i, j, k);
1711 float3 pointPosition = float3(cellOffset) +
1712 hash_int3_to_float3(cellPosition + cellOffset) * params.randomness;
1713 float distanceToPoint = voronoi_distance(pointPosition, localPosition, params);
1714 if (distanceToPoint < distanceF1) {
1715 distanceF2 = distanceF1;
1716 distanceF1 = distanceToPoint;
1717 offsetF2 = offsetF1;
1718 offsetF1 = cellOffset;
1719 positionF2 = positionF1;
1720 positionF1 = pointPosition;
1721 }
1722 else if (distanceToPoint < distanceF2) {
1723 distanceF2 = distanceToPoint;
1724 offsetF2 = cellOffset;
1725 positionF2 = pointPosition;
1726 }
1727 }
1728 }
1729 }
1730
1731 VoronoiOutput octave;
1732 octave.distance = distanceF2;
1733 octave.color = hash_int3_to_float3(cellPosition + offsetF2);
1734 octave.position = voronoi_position(positionF2 + cellPosition_f);
1735 return octave;
1736}
1737
1738float voronoi_distance_to_edge(const VoronoiParams &params, const float3 coord)
1739{
1740 float3 cellPosition_f = math::floor(coord);
1741 float3 localPosition = coord - cellPosition_f;
1742 int3 cellPosition = int3(cellPosition_f);
1743
1744 float3 vectorToClosest = {0.0f, 0.0f, 0.0f};
1745 float minDistance = FLT_MAX;
1746 for (int k = -1; k <= 1; k++) {
1747 for (int j = -1; j <= 1; j++) {
1748 for (int i = -1; i <= 1; i++) {
1749 int3 cellOffset(i, j, k);
1750 float3 vectorToPoint = float3(cellOffset) +
1751 hash_int3_to_float3(cellPosition + cellOffset) * params.randomness -
1752 localPosition;
1753 float distanceToPoint = math::dot(vectorToPoint, vectorToPoint);
1754 if (distanceToPoint < minDistance) {
1755 minDistance = distanceToPoint;
1756 vectorToClosest = vectorToPoint;
1757 }
1758 }
1759 }
1760 }
1761
1762 minDistance = FLT_MAX;
1763 for (int k = -1; k <= 1; k++) {
1764 for (int j = -1; j <= 1; j++) {
1765 for (int i = -1; i <= 1; i++) {
1766 int3 cellOffset(i, j, k);
1767 float3 vectorToPoint = float3(cellOffset) +
1768 hash_int3_to_float3(cellPosition + cellOffset) * params.randomness -
1769 localPosition;
1770 float3 perpendicularToEdge = vectorToPoint - vectorToClosest;
1771 if (math::dot(perpendicularToEdge, perpendicularToEdge) > 0.0001f) {
1772 float distanceToEdge = math::dot((vectorToClosest + vectorToPoint) / 2.0f,
1773 math::normalize(perpendicularToEdge));
1774 minDistance = math::min(minDistance, distanceToEdge);
1775 }
1776 }
1777 }
1778 }
1779
1780 return minDistance;
1781}
1782
1783float voronoi_n_sphere_radius(const VoronoiParams &params, const float3 coord)
1784{
1785 float3 cellPosition_f = math::floor(coord);
1786 float3 localPosition = coord - cellPosition_f;
1787 int3 cellPosition = int3(cellPosition_f);
1788
1789 float3 closestPoint = {0.0f, 0.0f, 0.0f};
1790 int3 closestPointOffset = {0, 0, 0};
1791 float minDistanceSq = FLT_MAX;
1792 for (int k = -1; k <= 1; k++) {
1793 for (int j = -1; j <= 1; j++) {
1794 for (int i = -1; i <= 1; i++) {
1795 int3 cellOffset(i, j, k);
1796 float3 pointPosition = float3(cellOffset) +
1797 hash_int3_to_float3(cellPosition + cellOffset) * params.randomness;
1798 float distanceToPointSq = math::length_squared(pointPosition - localPosition);
1799 if (distanceToPointSq < minDistanceSq) {
1800 minDistanceSq = distanceToPointSq;
1801 closestPoint = pointPosition;
1802 closestPointOffset = cellOffset;
1803 }
1804 }
1805 }
1806 }
1807
1808 minDistanceSq = FLT_MAX;
1809 float3 closestPointToClosestPoint = {0.0f, 0.0f, 0.0f};
1810 for (int k = -1; k <= 1; k++) {
1811 for (int j = -1; j <= 1; j++) {
1812 for (int i = -1; i <= 1; i++) {
1813 if (i == 0 && j == 0 && k == 0) {
1814 continue;
1815 }
1816 int3 cellOffset = int3(i, j, k) + closestPointOffset;
1817 float3 pointPosition = float3(cellOffset) +
1818 hash_int3_to_float3(cellPosition + cellOffset) * params.randomness;
1819 float distanceToPointSq = math::length_squared(closestPoint - pointPosition);
1820 if (distanceToPointSq < minDistanceSq) {
1821 minDistanceSq = distanceToPointSq;
1822 closestPointToClosestPoint = pointPosition;
1823 }
1824 }
1825 }
1826 }
1827
1828 return math::distance(closestPointToClosestPoint, closestPoint) / 2.0f;
1829}
1830
1831/* **** 4D Voronoi **** */
1832
1834{
1835 return coord;
1836}
1837
1839{
1840 float4 cellPosition_f = math::floor(coord);
1841 float4 localPosition = coord - cellPosition_f;
1842 int4 cellPosition = int4(cellPosition_f);
1843
1844 float minDistance = FLT_MAX;
1845 int4 targetOffset = {0, 0, 0, 0};
1846 float4 targetPosition = {0.0f, 0.0f, 0.0f, 0.0f};
1847 for (int u = -1; u <= 1; u++) {
1848 for (int k = -1; k <= 1; k++) {
1849 for (int j = -1; j <= 1; j++) {
1850 for (int i = -1; i <= 1; i++) {
1851 int4 cellOffset(i, j, k, u);
1852 float4 pointPosition = float4(cellOffset) +
1853 hash_int4_to_float4(cellPosition + cellOffset) *
1854 params.randomness;
1855 float distanceToPoint = voronoi_distance_bound(pointPosition, localPosition, params);
1856 if (distanceToPoint < minDistance) {
1857 targetOffset = cellOffset;
1858 minDistance = distanceToPoint;
1859 targetPosition = pointPosition;
1860 }
1861 }
1862 }
1863 }
1864 }
1865
1866 VoronoiOutput octave;
1867 octave.distance = voronoi_distance(targetPosition, localPosition, params);
1868 octave.color = hash_int4_to_float3(cellPosition + targetOffset);
1869 octave.position = voronoi_position(targetPosition + cellPosition_f);
1870 return octave;
1871}
1872
1874 const float4 coord,
1875 const bool calc_color)
1876{
1877 float4 cellPosition_f = math::floor(coord);
1878 float4 localPosition = coord - cellPosition_f;
1879 int4 cellPosition = int4(cellPosition_f);
1880
1881 float smoothDistance = 0.0f;
1882 float3 smoothColor = {0.0f, 0.0f, 0.0f};
1883 float4 smoothPosition = {0.0f, 0.0f, 0.0f, 0.0f};
1884 float h = -1.0f;
1885 for (int u = -2; u <= 2; u++) {
1886 for (int k = -2; k <= 2; k++) {
1887 for (int j = -2; j <= 2; j++) {
1888 for (int i = -2; i <= 2; i++) {
1889 int4 cellOffset(i, j, k, u);
1890 float4 pointPosition = float4(cellOffset) +
1891 hash_int4_to_float4(cellPosition + cellOffset) *
1892 params.randomness;
1893 float distanceToPoint = voronoi_distance(pointPosition, localPosition, params);
1894 h = h == -1.0f ?
1895 1.0f :
1896 smoothstep(0.0f,
1897 1.0f,
1898 0.5f + 0.5f * (smoothDistance - distanceToPoint) / params.smoothness);
1899 float correctionFactor = params.smoothness * h * (1.0f - h);
1900 smoothDistance = mix(smoothDistance, distanceToPoint, h) - correctionFactor;
1901 correctionFactor /= 1.0f + 3.0f * params.smoothness;
1902 if (calc_color) {
1903 /* Only compute Color output if necessary, as it is very expensive. */
1904 float3 cellColor = hash_int4_to_float3(cellPosition + cellOffset);
1905 smoothColor = mix(smoothColor, cellColor, h) - correctionFactor;
1906 }
1907 smoothPosition = mix(smoothPosition, pointPosition, h) - correctionFactor;
1908 }
1909 }
1910 }
1911 }
1912
1913 VoronoiOutput octave;
1914 octave.distance = smoothDistance;
1915 octave.color = smoothColor;
1916 octave.position = voronoi_position(cellPosition_f + smoothPosition);
1917 return octave;
1918}
1919
1921{
1922 float4 cellPosition_f = math::floor(coord);
1923 float4 localPosition = coord - cellPosition_f;
1924 int4 cellPosition = int4(cellPosition_f);
1925
1926 float distanceF1 = FLT_MAX;
1927 float distanceF2 = FLT_MAX;
1928 int4 offsetF1 = {0, 0, 0, 0};
1929 float4 positionF1 = {0.0f, 0.0f, 0.0f, 0.0f};
1930 int4 offsetF2 = {0, 0, 0, 0};
1931 float4 positionF2 = {0.0f, 0.0f, 0.0f, 0.0f};
1932 for (int u = -1; u <= 1; u++) {
1933 for (int k = -1; k <= 1; k++) {
1934 for (int j = -1; j <= 1; j++) {
1935 for (int i = -1; i <= 1; i++) {
1936 int4 cellOffset(i, j, k, u);
1937 float4 pointPosition = float4(cellOffset) +
1938 hash_int4_to_float4(cellPosition + cellOffset) *
1939 params.randomness;
1940 float distanceToPoint = voronoi_distance(pointPosition, localPosition, params);
1941 if (distanceToPoint < distanceF1) {
1942 distanceF2 = distanceF1;
1943 distanceF1 = distanceToPoint;
1944 offsetF2 = offsetF1;
1945 offsetF1 = cellOffset;
1946 positionF2 = positionF1;
1947 positionF1 = pointPosition;
1948 }
1949 else if (distanceToPoint < distanceF2) {
1950 distanceF2 = distanceToPoint;
1951 offsetF2 = cellOffset;
1952 positionF2 = pointPosition;
1953 }
1954 }
1955 }
1956 }
1957 }
1958
1959 VoronoiOutput octave;
1960 octave.distance = distanceF2;
1961 octave.color = hash_int4_to_float3(cellPosition + offsetF2);
1962 octave.position = voronoi_position(positionF2 + cellPosition_f);
1963 return octave;
1964}
1965
1966float voronoi_distance_to_edge(const VoronoiParams &params, const float4 coord)
1967{
1968 float4 cellPosition_f = math::floor(coord);
1969 float4 localPosition = coord - cellPosition_f;
1970 int4 cellPosition = int4(cellPosition_f);
1971
1972 float4 vectorToClosest = {0.0f, 0.0f, 0.0f, 0.0f};
1973 float minDistance = FLT_MAX;
1974 for (int u = -1; u <= 1; u++) {
1975 for (int k = -1; k <= 1; k++) {
1976 for (int j = -1; j <= 1; j++) {
1977 for (int i = -1; i <= 1; i++) {
1978 int4 cellOffset(i, j, k, u);
1979 float4 vectorToPoint = float4(cellOffset) +
1980 hash_int4_to_float4(cellPosition + cellOffset) *
1981 params.randomness -
1982 localPosition;
1983 float distanceToPoint = math::dot(vectorToPoint, vectorToPoint);
1984 if (distanceToPoint < minDistance) {
1985 minDistance = distanceToPoint;
1986 vectorToClosest = vectorToPoint;
1987 }
1988 }
1989 }
1990 }
1991 }
1992
1993 minDistance = FLT_MAX;
1994 for (int u = -1; u <= 1; u++) {
1995 for (int k = -1; k <= 1; k++) {
1996 for (int j = -1; j <= 1; j++) {
1997 for (int i = -1; i <= 1; i++) {
1998 int4 cellOffset(i, j, k, u);
1999 float4 vectorToPoint = float4(cellOffset) +
2000 hash_int4_to_float4(cellPosition + cellOffset) *
2001 params.randomness -
2002 localPosition;
2003 float4 perpendicularToEdge = vectorToPoint - vectorToClosest;
2004 if (math::dot(perpendicularToEdge, perpendicularToEdge) > 0.0001f) {
2005 float distanceToEdge = math::dot((vectorToClosest + vectorToPoint) / 2.0f,
2006 math::normalize(perpendicularToEdge));
2007 minDistance = math::min(minDistance, distanceToEdge);
2008 }
2009 }
2010 }
2011 }
2012 }
2013
2014 return minDistance;
2015}
2016
2017float voronoi_n_sphere_radius(const VoronoiParams &params, const float4 coord)
2018{
2019 float4 cellPosition_f = math::floor(coord);
2020 float4 localPosition = coord - cellPosition_f;
2021 int4 cellPosition = int4(cellPosition_f);
2022
2023 float4 closestPoint = {0.0f, 0.0f, 0.0f, 0.0f};
2024 int4 closestPointOffset = {0, 0, 0, 0};
2025 float minDistanceSq = FLT_MAX;
2026 for (int u = -1; u <= 1; u++) {
2027 for (int k = -1; k <= 1; k++) {
2028 for (int j = -1; j <= 1; j++) {
2029 for (int i = -1; i <= 1; i++) {
2030 int4 cellOffset(i, j, k, u);
2031 float4 pointPosition = float4(cellOffset) +
2032 hash_int4_to_float4(cellPosition + cellOffset) *
2033 params.randomness;
2034 float distanceToPointSq = math::length_squared(pointPosition - localPosition);
2035 if (distanceToPointSq < minDistanceSq) {
2036 minDistanceSq = distanceToPointSq;
2037 closestPoint = pointPosition;
2038 closestPointOffset = cellOffset;
2039 }
2040 }
2041 }
2042 }
2043 }
2044
2045 minDistanceSq = FLT_MAX;
2046 float4 closestPointToClosestPoint = {0.0f, 0.0f, 0.0f, 0.0f};
2047 for (int u = -1; u <= 1; u++) {
2048 for (int k = -1; k <= 1; k++) {
2049 for (int j = -1; j <= 1; j++) {
2050 for (int i = -1; i <= 1; i++) {
2051 if (i == 0 && j == 0 && k == 0 && u == 0) {
2052 continue;
2053 }
2054 int4 cellOffset = int4(i, j, k, u) + closestPointOffset;
2055 float4 pointPosition = float4(cellOffset) +
2056 hash_int4_to_float4(cellPosition + cellOffset) *
2057 params.randomness;
2058 float distanceToPointSq = math::length_squared(closestPoint - pointPosition);
2059 if (distanceToPointSq < minDistanceSq) {
2060 minDistanceSq = distanceToPointSq;
2061 closestPointToClosestPoint = pointPosition;
2062 }
2063 }
2064 }
2065 }
2066 }
2067
2068 return math::distance(closestPointToClosestPoint, closestPoint) / 2.0f;
2069}
2070
2071/* **** Fractal Voronoi **** */
2072
2073/* The fractalization logic is the same as for fBM Noise, except that some additions are replaced
2074 * by lerps. */
2075template<typename T>
2077 const T coord,
2078 const bool calc_color /* Only used to optimize Smooth F1 */)
2079{
2080 float amplitude = 1.0f;
2081 float max_amplitude = 0.0f;
2082 float scale = 1.0f;
2083
2085 const bool zero_input = params.detail == 0.0f || params.roughness == 0.0f;
2086
2087 for (int i = 0; i <= ceilf(params.detail); ++i) {
2088 VoronoiOutput octave = (params.feature == NOISE_SHD_VORONOI_F2) ?
2089 voronoi_f2(params, coord * scale) :
2091 params.smoothness != 0.0f) ?
2092 voronoi_smooth_f1(params, coord * scale, calc_color) :
2093 voronoi_f1(params, coord * scale);
2094
2095 if (zero_input) {
2096 max_amplitude = 1.0f;
2097 output = octave;
2098 break;
2099 }
2100 if (i <= params.detail) {
2101 max_amplitude += amplitude;
2102 output.distance += octave.distance * amplitude;
2103 output.color += octave.color * amplitude;
2104 output.position = mix(output.position, octave.position / scale, amplitude);
2105 scale *= params.lacunarity;
2106 amplitude *= params.roughness;
2107 }
2108 else {
2109 float remainder = params.detail - floorf(params.detail);
2110 if (remainder != 0.0f) {
2111 max_amplitude = mix(max_amplitude, max_amplitude + amplitude, remainder);
2112 output.distance = mix(
2113 output.distance, output.distance + octave.distance * amplitude, remainder);
2114 output.color = mix(output.color, output.color + octave.color * amplitude, remainder);
2115 output.position = mix(
2116 output.position, mix(output.position, octave.position / scale, amplitude), remainder);
2117 }
2118 }
2119 }
2120
2121 if (params.normalize) {
2122 output.distance /= max_amplitude * params.max_distance;
2123 output.color /= max_amplitude;
2124 }
2125
2126 output.position = (params.scale != 0.0f) ? output.position / params.scale :
2127 float4{0.0f, 0.0f, 0.0f, 0.0f};
2128
2129 return output;
2130}
2131
2132/* The fractalization logic is the same as for fBM Noise, except that some additions are replaced
2133 * by lerps. */
2134template<typename T>
2135float fractal_voronoi_distance_to_edge(const VoronoiParams &params, const T coord)
2136{
2137 float amplitude = 1.0f;
2138 float max_amplitude = params.max_distance;
2139 float scale = 1.0f;
2140 float distance = 8.0f;
2141
2142 const bool zero_input = params.detail == 0.0f || params.roughness == 0.0f;
2143
2144 for (int i = 0; i <= ceilf(params.detail); ++i) {
2145 const float octave_distance = voronoi_distance_to_edge(params, coord * scale);
2146
2147 if (zero_input) {
2148 distance = octave_distance;
2149 break;
2150 }
2151 if (i <= params.detail) {
2152 max_amplitude = mix(max_amplitude, params.max_distance / scale, amplitude);
2153 distance = mix(distance, math::min(distance, octave_distance / scale), amplitude);
2154 scale *= params.lacunarity;
2155 amplitude *= params.roughness;
2156 }
2157 else {
2158 float remainder = params.detail - floorf(params.detail);
2159 if (remainder != 0.0f) {
2160 float lerp_amplitude = mix(max_amplitude, params.max_distance / scale, amplitude);
2161 max_amplitude = mix(max_amplitude, lerp_amplitude, remainder);
2162 float lerp_distance = mix(
2163 distance, math::min(distance, octave_distance / scale), amplitude);
2164 distance = mix(distance, math::min(distance, lerp_distance), remainder);
2165 }
2166 }
2167 }
2168
2169 if (params.normalize) {
2170 distance /= max_amplitude;
2171 }
2172
2173 return distance;
2174}
2175
2176/* Explicit function template instantiation */
2177
2179 const float coord,
2180 const bool calc_color);
2182 const float2 coord,
2183 const bool calc_color);
2185 const float3 coord,
2186 const bool calc_color);
2188 const float4 coord,
2189 const bool calc_color);
2190
2192 const float coord);
2194 const float2 coord);
2196 const float3 coord);
2198 const float4 coord);
2200
2201/* -------------------------------------------------------------------- */
2220
2221/* The original Gabor noise paper specifies that the impulses count for each cell should be
2222 * computed by sampling a Poisson distribution whose mean is the impulse density. However,
2223 * Tavernier's paper showed that stratified Poisson point sampling is better assuming the weights
2224 * are sampled using a Bernoulli distribution, as shown in Figure (3). By stratified sampling, they
2225 * mean a constant number of impulses per cell, so the stratification is the grid itself in that
2226 * sense, as described in the supplementary material of the paper. */
2227static constexpr int gabor_impulses_count = 8;
2228
2229/* Computes a 2D Gabor kernel based on Equation (6) in the original Gabor noise paper. Where the
2230 * frequency argument is the F_0 parameter and the orientation argument is the w_0 parameter. We
2231 * assume the Gaussian envelope has a unit magnitude, that is, K = 1. That is because we will
2232 * eventually normalize the final noise value to the unit range, so the multiplication by the
2233 * magnitude will be canceled by the normalization. Further, we also assume a unit Gaussian width,
2234 * that is, a = 1. That is because it does not provide much artistic control. It follows that the
2235 * Gaussian will be truncated at pi.
2236 *
2237 * To avoid the discontinuities caused by the aforementioned truncation, the Gaussian is windowed
2238 * using a Hann window, that is because contrary to the claim made in the original Gabor paper,
2239 * truncating the Gaussian produces significant artifacts especially when differentiated for bump
2240 * mapping. The Hann window is C1 continuous and has limited effect on the shape of the Gaussian,
2241 * so it felt like an appropriate choice.
2242 *
2243 * Finally, instead of computing the Gabor value directly, we instead use the complex phasor
2244 * formulation described in section 3.1.1 in Tricard's paper. That's done to be able to compute the
2245 * phase and intensity of the Gabor noise after summation based on equations (8) and (9). The
2246 * return value of the Gabor kernel function is then a complex number whose real value is the
2247 * value computed in the original Gabor noise paper, and whose imaginary part is the sine
2248 * counterpart of the real part, which is the only extra computation in the new formulation.
2249 *
2250 * Note that while the original Gabor noise paper uses the cosine part of the phasor, that is, the
2251 * real part of the phasor, we use the sine part instead, that is, the imaginary part of the
2252 * phasor, as suggested by Tavernier's paper in "Section 3.3. Instance stationarity and
2253 * normalization", to ensure a zero mean, which should help with normalization. */
2255 const float frequency,
2256 const float orientation)
2257{
2259 const float hann_window = 0.5f + 0.5f * math::cos(std::numbers::pi * distance_squared);
2260 const float gaussian_envelop = math::exp(-std::numbers::pi * distance_squared);
2261 const float windowed_gaussian_envelope = gaussian_envelop * hann_window;
2262
2263 const float2 frequency_vector = frequency * float2(cos(orientation), sin(orientation));
2264 const float angle = 2.0f * std::numbers::pi * math::dot(position, frequency_vector);
2265 const float2 phasor = float2(math::cos(angle), math::sin(angle));
2266
2267 return windowed_gaussian_envelope * phasor;
2268}
2269
2307{
2308 const float integral_of_gabor_squared = 0.25f;
2309 const float second_moment = 0.5f;
2310 return math::sqrt(gabor_impulses_count * second_moment * integral_of_gabor_squared);
2311}
2312
2313/* Computes the Gabor noise value at the given position for the given cell. This is essentially the
2314 * sum in Equation (8) in the original Gabor noise paper, where we sum Gabor kernels sampled at a
2315 * random position with a random weight. The orientation of the kernel is constant for anisotropic
2316 * noise while it is random for isotropic noise. The original Gabor noise paper mentions that the
2317 * weights should be uniformly distributed in the [-1, 1] range, however, Tavernier's paper showed
2318 * that using a Bernoulli distribution yields better results, so that is what we do. */
2320 const float2 position,
2321 const float frequency,
2322 const float isotropy,
2323 const float base_orientation)
2324
2325{
2326 float2 noise(0.0f);
2327 for (const int i : IndexRange(gabor_impulses_count)) {
2328 /* Compute unique seeds for each of the needed random variables. */
2329 const float3 seed_for_orientation(cell.x, cell.y, i * 3);
2330 const float3 seed_for_kernel_center(cell.x, cell.y, i * 3 + 1);
2331 const float3 seed_for_weight(cell.x, cell.y, i * 3 + 2);
2332
2333 /* For isotropic noise, add a random orientation amount, while for anisotropic noise, use the
2334 * base orientation. Linearly interpolate between the two cases using the isotropy factor. Note
2335 * that the random orientation range spans pi as opposed to two pi, that's because the Gabor
2336 * kernel is symmetric around pi. */
2337 const float random_orientation = (noise::hash_float_to_float(seed_for_orientation) - 0.5f) *
2338 std::numbers::pi;
2339 const float orientation = base_orientation + random_orientation * isotropy;
2340
2341 const float2 kernel_center = noise::hash_float_to_float2(seed_for_kernel_center);
2342 const float2 position_in_kernel_space = position - kernel_center;
2343
2344 /* The kernel is windowed beyond the unit distance, so early exit with a zero for points that
2345 * are further than a unit radius. */
2346 if (math::length_squared(position_in_kernel_space) >= 1.0f) {
2347 continue;
2348 }
2349
2350 /* We either add or subtract the Gabor kernel based on a Bernoulli distribution of equal
2351 * probability. */
2352 const float weight = noise::hash_float_to_float(seed_for_weight) < 0.5f ? -1.0f : 1.0f;
2353
2354 noise += weight * compute_2d_gabor_kernel(position_in_kernel_space, frequency, orientation);
2355 }
2356 return noise;
2357}
2358
2359/* Computes the Gabor noise value by dividing the space into a grid and evaluating the Gabor noise
2360 * in the space of each cell of the 3x3 cell neighborhood. */
2361static float2 compute_2d_gabor_noise(const float2 coordinates,
2362 const float frequency,
2363 const float isotropy,
2364 const float base_orientation)
2365{
2366 const float2 cell_position = math::floor(coordinates);
2367 const float2 local_position = coordinates - cell_position;
2368
2369 float2 sum(0.0f);
2370 for (int j = -1; j <= 1; j++) {
2371 for (int i = -1; i <= 1; i++) {
2372 const float2 cell_offset = float2(i, j);
2373 const float2 current_cell_position = cell_position + cell_offset;
2374 const float2 position_in_cell_space = local_position - cell_offset;
2376 current_cell_position, position_in_cell_space, frequency, isotropy, base_orientation);
2377 }
2378 }
2379
2380 return sum;
2381}
2382
2383/* Identical to compute_2d_gabor_kernel, except it is evaluated in 3D space. Notice that Equation
2384 * (6) in the original Gabor noise paper computes the frequency vector using (cos(w_0), sin(w_0)),
2385 * which we also do in the 2D variant, however, for 3D, the orientation is already a unit frequency
2386 * vector, so we just need to scale it by the frequency value. */
2388 const float frequency,
2389 const float3 orientation)
2390{
2392 const float hann_window = 0.5f + 0.5f * math::cos(std::numbers::pi * distance_squared);
2393 const float gaussian_envelop = math::exp(-std::numbers::pi * distance_squared);
2394 const float windowed_gaussian_envelope = gaussian_envelop * hann_window;
2395
2396 const float3 frequency_vector = frequency * orientation;
2397 const float angle = 2.0f * std::numbers::pi * math::dot(position, frequency_vector);
2398 const float2 phasor = float2(math::cos(angle), math::sin(angle));
2399
2400 return windowed_gaussian_envelope * phasor;
2401}
2402
2403/* Identical to compute_2d_gabor_standard_deviation except we do triple integration in 3D. The only
2404 * difference is the denominator in the integral expression, which is 2^{5 / 2} for the 3D case
2405 * instead of 4 for the 2D case. Similarly, the limit evaluates to 1 / (4 * sqrt(2)). */
2407{
2408 const float integral_of_gabor_squared = 1.0f / (4.0f * std::numbers::sqrt2);
2409 const float second_moment = 0.5f;
2410 return math::sqrt(gabor_impulses_count * second_moment * integral_of_gabor_squared);
2411}
2412
2413/* Computes the orientation of the Gabor kernel such that it is constant for anisotropic
2414 * noise while it is random for isotropic noise. We randomize in spherical coordinates for a
2415 * uniform distribution. */
2416static float3 compute_3d_orientation(const float3 orientation,
2417 const float isotropy,
2418 const float4 seed)
2419{
2420 /* Return the base orientation in case we are completely anisotropic. */
2421 if (isotropy == 0.0) {
2422 return orientation;
2423 }
2424
2425 /* Compute the orientation in spherical coordinates. */
2426 float inclination = math::acos(orientation.z);
2427 float azimuth = math::sign(orientation.y) *
2428 math::acos(orientation.x / math::length(float2(orientation.x, orientation.y)));
2429
2430 /* For isotropic noise, add a random orientation amount, while for anisotropic noise, use the
2431 * base orientation. Linearly interpolate between the two cases using the isotropy factor. Note
2432 * that the random orientation range is to pi as opposed to two pi, that's because the Gabor
2433 * kernel is symmetric around pi. */
2434 const float2 random_angles = noise::hash_float_to_float2(seed) * std::numbers::pi;
2435 inclination += random_angles.x * isotropy;
2436 azimuth += random_angles.y * isotropy;
2437
2438 /* Convert back to Cartesian coordinates, */
2439 return float3(math::sin(inclination) * math::cos(azimuth),
2440 math::sin(inclination) * math::sin(azimuth),
2441 math::cos(inclination));
2442}
2443
2445 const float3 position,
2446 const float frequency,
2447 const float isotropy,
2448 const float3 base_orientation)
2449
2450{
2451 float2 noise(0.0f);
2452 for (const int i : IndexRange(gabor_impulses_count)) {
2453 /* Compute unique seeds for each of the needed random variables. */
2454 const float4 seed_for_orientation(cell.x, cell.y, cell.z, i * 3);
2455 const float4 seed_for_kernel_center(cell.x, cell.y, cell.z, i * 3 + 1);
2456 const float4 seed_for_weight(cell.x, cell.y, cell.z, i * 3 + 2);
2457
2458 const float3 orientation = compute_3d_orientation(
2459 base_orientation, isotropy, seed_for_orientation);
2460
2461 const float3 kernel_center = noise::hash_float_to_float3(seed_for_kernel_center);
2462 const float3 position_in_kernel_space = position - kernel_center;
2463
2464 /* The kernel is windowed beyond the unit distance, so early exit with a zero for points that
2465 * are further than a unit radius. */
2466 if (math::length_squared(position_in_kernel_space) >= 1.0f) {
2467 continue;
2468 }
2469
2470 /* We either add or subtract the Gabor kernel based on a Bernoulli distribution of equal
2471 * probability. */
2472 const float weight = noise::hash_float_to_float(seed_for_weight) < 0.5f ? -1.0f : 1.0f;
2473
2474 noise += weight * compute_3d_gabor_kernel(position_in_kernel_space, frequency, orientation);
2475 }
2476 return noise;
2477}
2478
2479/* Identical to compute_2d_gabor_noise but works in the 3D neighborhood of the noise. */
2480static float2 compute_3d_gabor_noise(const float3 coordinates,
2481 const float frequency,
2482 const float isotropy,
2483 const float3 base_orientation)
2484{
2485 const float3 cell_position = math::floor(coordinates);
2486 const float3 local_position = coordinates - cell_position;
2487
2488 float2 sum(0.0f);
2489 for (int k = -1; k <= 1; k++) {
2490 for (int j = -1; j <= 1; j++) {
2491 for (int i = -1; i <= 1; i++) {
2492 const float3 cell_offset = float3(i, j, k);
2493 const float3 current_cell_position = cell_position + cell_offset;
2494 const float3 position_in_cell_space = local_position - cell_offset;
2496 current_cell_position, position_in_cell_space, frequency, isotropy, base_orientation);
2497 }
2498 }
2499 }
2500
2501 return sum;
2502}
2503
2504void gabor(const float2 coordinates,
2505 const float scale,
2506 const float frequency,
2507 const float anisotropy,
2508 const float orientation,
2509 float *r_value,
2510 float *r_phase,
2511 float *r_intensity)
2512{
2513 const float2 scaled_coordinates = coordinates * scale;
2514 const float isotropy = 1.0f - math::clamp(anisotropy, 0.0f, 1.0f);
2515 const float sanitized_frequency = math::max(0.001f, frequency);
2516
2517 const float2 phasor = compute_2d_gabor_noise(
2518 scaled_coordinates, sanitized_frequency, isotropy, orientation);
2519 const float standard_deviation = compute_2d_gabor_standard_deviation();
2520
2521 /* Normalize the noise by dividing by six times the standard deviation, which was determined
2522 * empirically. */
2523 const float normalization_factor = 6.0f * standard_deviation;
2524
2525 /* As discussed in compute_2d_gabor_kernel, we use the imaginary part of the phasor as the Gabor
2526 * value. But remap to [0, 1] from [-1, 1]. */
2527 if (r_value) {
2528 *r_value = (phasor.y / normalization_factor) * 0.5f + 0.5f;
2529 }
2530
2531 /* Compute the phase based on equation (9) in Tricard's paper. But remap the phase into the
2532 * [0, 1] range. */
2533 if (r_phase) {
2534 *r_phase = (math::atan2(phasor.y, phasor.x) + std::numbers::pi) / (2.0f * std::numbers::pi);
2535 }
2536
2537 /* Compute the intensity based on equation (8) in Tricard's paper. */
2538 if (r_intensity) {
2539 *r_intensity = math::length(phasor) / normalization_factor;
2540 }
2541}
2542
2543void gabor(const float3 coordinates,
2544 const float scale,
2545 const float frequency,
2546 const float anisotropy,
2547 const float3 orientation,
2548 float *r_value,
2549 float *r_phase,
2550 float *r_intensity)
2551{
2552 const float3 scaled_coordinates = coordinates * scale;
2553 const float isotropy = 1.0f - math::clamp(anisotropy, 0.0f, 1.0f);
2554 const float sanitized_frequency = math::max(0.001f, frequency);
2555
2556 const float3 normalized_orientation = math::normalize(orientation);
2557 const float2 phasor = compute_3d_gabor_noise(
2558 scaled_coordinates, sanitized_frequency, isotropy, normalized_orientation);
2559 const float standard_deviation = compute_3d_gabor_standard_deviation();
2560
2561 /* Normalize the noise by dividing by six times the standard deviation, which was determined
2562 * empirically. */
2563 const float normalization_factor = 6.0f * standard_deviation;
2564
2565 /* As discussed in compute_2d_gabor_kernel, we use the imaginary part of the phasor as the Gabor
2566 * value. But remap to [0, 1] from [-1, 1]. */
2567 if (r_value) {
2568 *r_value = (phasor.y / normalization_factor) * 0.5f + 0.5f;
2569 }
2570
2571 /* Compute the phase based on equation (9) in Tricard's paper. But remap the phase into the
2572 * [0, 1] range. */
2573 if (r_phase) {
2574 *r_phase = (math::atan2(phasor.y, phasor.x) + std::numbers::pi) / (2.0f * std::numbers::pi);
2575 }
2576
2577 /* Compute the intensity based on equation (8) in Tricard's paper. */
2578 if (r_intensity) {
2579 *r_intensity = math::length(phasor) / normalization_factor;
2580 }
2581}
2582
2584
2585} // namespace blender::noise
#define Z
#define Y
#define BLI_assert_unreachable()
#define BLI_INLINE
#define BLI_NOINLINE
#define ELEM(...)
#define output
SIMD_FORCE_INLINE const btScalar & z() const
Return the z value.
Definition btQuadWord.h:117
SIMD_FORCE_INLINE const btScalar & w() const
Return the w value.
Definition btQuadWord.h:119
SIMD_FORCE_INLINE btScalar angle(const btVector3 &v) const
Return the angle between this and another vector.
Definition btVector3.h:356
constexpr IndexRange()=default
#define FLT_MAX
#define position
#define frequency(freq)
#define condition(cond)
#define sin
#define cos
VecBase< float, D > normalize(VecOp< float, D >) RET
float distance(float, float) RET
float3x3 scale(float3x3 mat, float2 scale)
float distance_squared(VecT a, VecT b)
#define T
T length_squared(const VecBase< T, Size > &a)
T cos(const AngleRadianBase< T > &a)
T clamp(const T &a, const T &min, const T &max)
T acos(const T &a)
T sqrt(const T &a)
T sign(const T &a)
T floor(const T &a)
T distance(const T &a, const T &b)
T length(const VecBase< T, Size > &a)
T exp(const T &x)
T dot(const QuaternionBase< T > &a, const QuaternionBase< T > &b)
T min(const T &a, const T &b)
T atan2(const T &y, const T &x)
MatBase< T, NumCol, NumRow > normalize(const MatBase< T, NumCol, NumRow > &a)
T sin(const AngleRadianBase< T > &a)
T max(const T &a, const T &b)
T mod(const T &a, const T &b)
T abs(const T &a)
static ATTR_NO_SIGNED_INT_OVERFLOW int4 hash_pcg4d_i(int4 v)
Definition noise.cc:185
static float int_to_float_01(int32_t k)
Definition noise.cc:329
@ NOISE_SHD_VORONOI_MANHATTAN
Definition noise.cc:1149
@ NOISE_SHD_VORONOI_MINKOWSKI
Definition noise.cc:1151
@ NOISE_SHD_VORONOI_EUCLIDEAN
Definition noise.cc:1148
@ NOISE_SHD_VORONOI_CHEBYCHEV
Definition noise.cc:1150
float3 perlin_float3_fractal_distorted(float position, float detail, float roughness, float lacunarity, float offset, float gain, float distortion, int type, bool normalize)
Definition noise.cc:999
BLI_INLINE float negate_if(float value, uint32_t condition)
Definition noise.cc:466
static float3 hash_int3_to_float3(int3 k)
Definition noise.cc:340
BLI_INLINE float uint_to_float_01(uint32_t k)
Definition noise.cc:227
@ NOISE_SHD_PERLIN_MULTIFRACTAL
Definition noise.cc:842
@ NOISE_SHD_PERLIN_FBM
Definition noise.cc:843
@ NOISE_SHD_PERLIN_RIDGED_MULTIFRACTAL
Definition noise.cc:845
@ NOISE_SHD_PERLIN_HYBRID_MULTIFRACTAL
Definition noise.cc:844
@ NOISE_SHD_PERLIN_HETERO_TERRAIN
Definition noise.cc:846
float perlin_ridged_multi_fractal(T p, const float detail, const float roughness, const float lacunarity, const float offset, const float gain)
Definition noise.cc:814
static float2 compute_3d_gabor_noise(const float3 coordinates, const float frequency, const float isotropy, const float3 base_orientation)
Definition noise.cc:2480
uint32_t hash_float(float kx)
Definition noise.cc:200
static float compute_3d_gabor_standard_deviation()
Definition noise.cc:2406
static float2 compute_2d_gabor_noise_cell(const float2 cell, const float2 position, const float frequency, const float isotropy, const float base_orientation)
Definition noise.cc:2319
BLI_INLINE float perlin_distortion(float position, float strength)
Definition noise.cc:916
float hash_float_to_float(float k)
Definition noise.cc:254
float perlin_signed(float position)
Definition noise.cc:613
void gabor(const float2 coordinates, const float scale, const float frequency, const float anisotropy, const float orientation, float *r_value, float *r_phase, float *r_intensity)
Definition noise.cc:2504
BLI_INLINE void hash_bit_mix(uint32_t &a, uint32_t &b, uint32_t &c)
Definition noise.cc:49
float4 voronoi_position(const float coord)
Definition noise.cc:1246
BLI_INLINE float fade(float t)
Definition noise.cc:461
VoronoiOutput voronoi_smooth_f1(const VoronoiParams &params, const float coord, const bool calc_color)
Definition noise.cc:1278
float voronoi_n_sphere_radius(const VoronoiParams &params, const float coord)
Definition noise.cc:1368
static float2 compute_2d_gabor_noise(const float2 coordinates, const float frequency, const float isotropy, const float base_orientation)
Definition noise.cc:2361
float perlin(float position)
Definition noise.cc:664
BLI_INLINE float random_float_offset(float seed)
Definition noise.cc:888
static float voronoi_distance_bound(const T a, const T b, const VoronoiParams &params)
Definition noise.cc:1236
BLI_INLINE float floor_fraction(float x, int &i)
Definition noise.cc:504
float voronoi_distance_to_edge(const VoronoiParams &params, const float coord)
Definition noise.cc:1354
BLI_INLINE uint32_t float_as_uint(float f)
Definition noise.cc:141
static float4 hash_int4_to_float4(int4 k)
Definition noise.cc:346
static float2 hash_int2_to_float2(int2 k)
Definition noise.cc:334
BLI_INLINE float3 random_float3_offset(float seed)
Definition noise.cc:899
static float3 hash_int2_to_float3(int2 k)
Definition noise.cc:353
static float2 compute_3d_gabor_noise_cell(const float3 cell, const float3 position, const float frequency, const float isotropy, const float3 base_orientation)
Definition noise.cc:2444
static float3 hash_int4_to_float3(int4 k)
Definition noise.cc:358
static float2 compute_2d_gabor_kernel(const float2 position, const float frequency, const float orientation)
Definition noise.cc:2254
VoronoiOutput fractal_voronoi_x_fx(const VoronoiParams &params, const T coord, const bool calc_color)
Definition noise.cc:2076
float voronoi_distance(const float a, const float b)
Definition noise.cc:1164
static float2 compute_3d_gabor_kernel(const float3 position, const float frequency, const float3 orientation)
Definition noise.cc:2387
BLI_INLINE float2 random_float2_offset(float seed)
Definition noise.cc:893
float perlin_fbm(T p, float detail, float roughness, float lacunarity, bool normalize)
Definition noise.cc:694
VoronoiOutput voronoi_f2(const VoronoiParams &params, const float coord)
Definition noise.cc:1316
float3 hash_float_to_float3(float k)
Definition noise.cc:291
uint32_t hash(uint32_t kx)
Definition noise.cc:89
BLI_INLINE float4 random_float4_offset(float seed)
Definition noise.cc:906
BLI_INLINE float perlin_noise(float position)
Definition noise.cc:511
static ATTR_NO_SIGNED_INT_OVERFLOW int3 hash_pcg3d_i(int3 v)
Definition noise.cc:171
float fractal_voronoi_distance_to_edge(const VoronoiParams &params, const T coord)
Definition noise.cc:2135
BLI_INLINE void hash_bit_final(uint32_t &a, uint32_t &b, uint32_t &c)
Definition noise.cc:71
float4 hash_float_to_float4(float4 k)
Definition noise.cc:319
float perlin_hybrid_multi_fractal(T p, const float detail, const float roughness, const float lacunarity, const float offset, const float gain)
Definition noise.cc:782
static float compute_2d_gabor_standard_deviation()
Definition noise.cc:2306
float perlin_select(T p, float detail, float roughness, float lacunarity, float offset, float gain, int type, bool normalize)
Definition noise.cc:850
BLI_INLINE float noise_grad(uint32_t hash, float x)
Definition noise.cc:471
VoronoiOutput voronoi_f1(const VoronoiParams &params, const float coord)
Definition noise.cc:1251
float perlin_multi_fractal(T p, const float detail, const float roughness, const float lacunarity)
Definition noise.cc:736
static constexpr int gabor_impulses_count
Definition noise.cc:2227
@ NOISE_SHD_VORONOI_N_SPHERE_RADIUS
Definition noise.cc:1159
@ NOISE_SHD_VORONOI_SMOOTH_F1
Definition noise.cc:1157
@ NOISE_SHD_VORONOI_F2
Definition noise.cc:1156
@ NOISE_SHD_VORONOI_DISTANCE_TO_EDGE
Definition noise.cc:1158
@ NOISE_SHD_VORONOI_F1
Definition noise.cc:1155
static T mix(T v0, T v1, float x)
Definition noise.cc:376
float perlin_fractal_distorted(T position, float detail, float roughness, float lacunarity, float offset, float gain, float distortion, int type, bool normalize)
Definition noise.cc:945
static ATTR_NO_SIGNED_INT_OVERFLOW int2 hash_pcg2d_i(int2 v)
Definition noise.cc:159
BLI_INLINE uint32_t hash_bit_rotate(uint32_t x, uint32_t k)
Definition noise.cc:44
float perlin_hetero_terrain(T p, const float detail, const float roughness, const float lacunarity, const float offset)
Definition noise.cc:756
float2 hash_float_to_float2(float2 k)
Definition noise.cc:274
static float3 compute_3d_orientation(const float3 orientation, const float isotropy, const float4 seed)
Definition noise.cc:2416
float hash_to_float(uint32_t kx)
Definition noise.cc:232
VecBase< int32_t, 4 > int4
ATTR_WARN_UNUSED_RESULT const BMVert * v
MatBase< float, 4, 4 > float4x4
VecBase< float, 4 > float4
unsigned int uint32_t
Definition BKE_text.h:81
VecBase< int32_t, 2 > int2
VecBase< float, 2 > float2
VecBase< int32_t, 3 > int3
MINLINE float smoothstep(float edge0, float edge1, float x)
ATTR_WARN_UNUSED_RESULT const BMVert * v2
signed int int32_t
Definition BKE_text.h:78
VecBase< float, 3 > float3
#define X(TYPE)
#define ATTR_NO_SIGNED_INT_OVERFLOW
Definition noise.cc:33
#define floorf
#define fabsf
#define ceilf
KERNEL_FEATURE_LIGHT_PASSES KERNEL_FEATURE_PATH_GUIDING float