Blender V5.3
gpu_shader_material_tex_noise.bsl.hh
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2019-2023 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
5/* The following offset functions generate random offsets to be added to texture
6 * coordinates to act as a seed since the noise functions don't have seed values.
7 * A seed value is needed for generating distortion textures and color outputs.
8 * The offset's components are in the range [100, 200], not too high to cause
9 * bad precision and not too small to be noticeable. We use float seed because
10 * OSL only support float hashes.
11 */
12
13#pragma once
14
18
19float random_float_offset(float seed)
20{
21 return 100.0f + hash_float_to_float(seed) * 100.0f;
22}
23
25{
26 return float2(100.0f + hash_vec2_to_float(float2(seed, 0.0f)) * 100.0f,
27 100.0f + hash_vec2_to_float(float2(seed, 1.0f)) * 100.0f);
28}
29
31{
32 return float3(100.0f + hash_vec2_to_float(float2(seed, 0.0f)) * 100.0f,
33 100.0f + hash_vec2_to_float(float2(seed, 1.0f)) * 100.0f,
34 100.0f + hash_vec2_to_float(float2(seed, 2.0f)) * 100.0f);
35}
36
38{
39 return float4(100.0f + hash_vec2_to_float(float2(seed, 0.0f)) * 100.0f,
40 100.0f + hash_vec2_to_float(float2(seed, 1.0f)) * 100.0f,
41 100.0f + hash_vec2_to_float(float2(seed, 2.0f)) * 100.0f,
42 100.0f + hash_vec2_to_float(float2(seed, 3.0f)) * 100.0f);
43}
44
45/* Templated random offset resolution based on coordinate type */
46
47template<typename T> T random_offset(float /*seed*/)
48{
49 return T(0);
50}
51
52template<> float random_offset<float>(float seed)
53{
54 return random_float_offset(seed);
55}
56template<> float2 random_offset<float2>(float seed)
57{
58 return random_vec2_offset(seed);
59}
60template<> float3 random_offset<float3>(float seed)
61{
62 return random_vec3_offset(seed);
63}
64template<> float4 random_offset<float4>(float seed)
65{
66 return random_vec4_offset(seed);
67}
68
69float distort_point(float p, float distortion)
70{
71 if (distortion != 0.0f) {
72 p += snoise(p + random_float_offset(0.0f)) * distortion;
73 }
74 return p;
75}
76
77float2 distort_point(float2 p, float distortion)
78{
79 if (distortion != 0.0f) {
80 p += float2(snoise(p + random_vec2_offset(0.0f)) * distortion,
81 snoise(p + random_vec2_offset(1.0f)) * distortion);
82 }
83 return p;
84}
85
86float3 distort_point(float3 p, float distortion)
87{
88 if (distortion != 0.0f) {
89 p += float3(snoise(p + random_vec3_offset(0.0f)) * distortion,
90 snoise(p + random_vec3_offset(1.0f)) * distortion,
91 snoise(p + random_vec3_offset(2.0f)) * distortion);
92 }
93 return p;
94}
95
96float4 distort_point(float4 p, float distortion)
97{
98 if (distortion != 0.0f) {
99 p += float4(snoise(p + random_vec4_offset(0.0f)) * distortion,
100 snoise(p + random_vec4_offset(1.0f)) * distortion,
101 snoise(p + random_vec4_offset(2.0f)) * distortion,
102 snoise(p + random_vec4_offset(3.0f)) * distortion);
103 }
104 return p;
105}
106
107/* Used to offset the color seed to not overlap with the distort_point seeds. */
108
109float color_seed_offset(float /*co*/)
110{
111 return 1.0f;
112}
114{
115 return 2.0f;
116}
118{
119 return 3.0f;
120}
122{
123 return 4.0f;
124}
125
126/* Noise functor wrappers. */
127
128template<typename T> struct NoiseFBM {
129 static float noise_fn(T co,
130 float detail,
131 float roughness,
132 float lacunarity,
133 float offset,
134 float gain,
135 bool normalize)
136 {
137 return noise_fbm(co, detail, roughness, lacunarity, offset, gain, normalize);
138 }
139};
140
141template struct NoiseFBM<float>;
142template struct NoiseFBM<float2>;
143template struct NoiseFBM<float3>;
144template struct NoiseFBM<float4>;
145
146template<typename T> struct NoiseMultiFractal {
147 static float noise_fn(T co,
148 float detail,
149 float roughness,
150 float lacunarity,
151 float offset,
152 float gain,
153 bool normalize)
154 {
155 return noise_multi_fractal(co, detail, roughness, lacunarity, offset, gain, normalize);
156 }
157};
158
159template struct NoiseMultiFractal<float>;
160template struct NoiseMultiFractal<float2>;
161template struct NoiseMultiFractal<float3>;
162template struct NoiseMultiFractal<float4>;
163
164template<typename T> struct NoiseHeteroTerrain {
165 static float noise_fn(T co,
166 float detail,
167 float roughness,
168 float lacunarity,
169 float offset,
170 float gain,
171 bool normalize)
172 {
173 return noise_hetero_terrain(co, detail, roughness, lacunarity, offset, gain, normalize);
174 }
175};
176
177template struct NoiseHeteroTerrain<float>;
178template struct NoiseHeteroTerrain<float2>;
179template struct NoiseHeteroTerrain<float3>;
180template struct NoiseHeteroTerrain<float4>;
181
182template<typename T> struct NoiseHybridMultiFractal {
183 static float noise_fn(T co,
184 float detail,
185 float roughness,
186 float lacunarity,
187 float offset,
188 float gain,
189 bool normalize)
190 {
191 return noise_hybrid_multi_fractal(co, detail, roughness, lacunarity, offset, gain, normalize);
192 }
193};
194
195template struct NoiseHybridMultiFractal<float>;
196template struct NoiseHybridMultiFractal<float2>;
197template struct NoiseHybridMultiFractal<float3>;
198template struct NoiseHybridMultiFractal<float4>;
199
200template<typename T> struct NoiseRidgedMultiFractal {
201 static float noise_fn(T co,
202 float detail,
203 float roughness,
204 float lacunarity,
205 float offset,
206 float gain,
207 bool normalize)
208 {
209 return noise_ridged_multi_fractal(co, detail, roughness, lacunarity, offset, gain, normalize);
210 }
211};
212
213template struct NoiseRidgedMultiFractal<float>;
214template struct NoiseRidgedMultiFractal<float2>;
215template struct NoiseRidgedMultiFractal<float3>;
216template struct NoiseRidgedMultiFractal<float4>;
217
218/* Unified Distortion Function Template. */
219
220template<typename T, typename NoiseT>
222 float detail,
223 float roughness,
224 float lacunarity,
225 float offset,
226 float gain,
227 float distortion,
228 float normalize,
229 float compute_color,
230 float &value,
231 float4 &color)
232{
233 detail = clamp(detail, 0.0f, 15.0f);
234 roughness = max(roughness, 0.0f);
235
236 p = distort_point(p, distortion);
237
238 bool norm = (normalize != 0.0f);
239 value = NoiseT::noise_fn(p, detail, roughness, lacunarity, offset, gain, norm);
240
241 if (compute_color != 0.0f) {
242 float seed_base = color_seed_offset(p);
243 color = float4(value,
244 NoiseT::noise_fn(p + random_offset<T>(seed_base + 0.0f),
245 detail,
246 roughness,
247 lacunarity,
248 offset,
249 gain,
250 norm),
251 NoiseT::noise_fn(p + random_offset<T>(seed_base + 1.0f),
252 detail,
253 roughness,
254 lacunarity,
255 offset,
256 gain,
257 norm),
258 1.0f);
259 }
260}
261
263 float, float, float, float, float, float, float, float, float, float &, float4 &);
265 float2, float, float, float, float, float, float, float, float, float &, float4 &);
267 float3, float, float, float, float, float, float, float, float, float &, float4 &);
269 float4, float, float, float, float, float, float, float, float, float &, float4 &);
270
272 float, float, float, float, float, float, float, float, float, float &, float4 &);
274 float2, float, float, float, float, float, float, float, float, float &, float4 &);
276 float3, float, float, float, float, float, float, float, float, float &, float4 &);
278 float4, float, float, float, float, float, float, float, float, float &, float4 &);
279
281 float, float, float, float, float, float, float, float, float, float &, float4 &);
283 float2, float, float, float, float, float, float, float, float, float &, float4 &);
285 float3, float, float, float, float, float, float, float, float, float &, float4 &);
287 float4, float, float, float, float, float, float, float, float, float &, float4 &);
288
290 float, float, float, float, float, float, float, float, float, float &, float4 &);
292 float2, float, float, float, float, float, float, float, float, float &, float4 &);
294 float3, float, float, float, float, float, float, float, float, float &, float4 &);
296 float4, float, float, float, float, float, float, float, float, float &, float4 &);
297
299 float, float, float, float, float, float, float, float, float, float &, float4 &);
301 float2, float, float, float, float, float, float, float, float, float &, float4 &);
303 float3, float, float, float, float, float, float, float, float, float &, float4 &);
305 float4, float, float, float, float, float, float, float, float, float &, float4 &);
306
307/* Noise fBM */
308
309[[node]]
311 float w,
312 float scale,
313 float detail,
314 float roughness,
315 float lacunarity,
316 float offset,
317 float gain,
318 float distortion,
319 float normalize,
320 float compute_color,
321 float &value,
322 float4 &color)
323{
325 detail,
326 roughness,
327 lacunarity,
328 offset,
329 gain,
330 distortion,
331 normalize,
332 compute_color,
333 value,
334 color);
335}
336
337[[node]]
339 float /*w*/,
340 float scale,
341 float detail,
342 float roughness,
343 float lacunarity,
344 float offset,
345 float gain,
346 float distortion,
347 float normalize,
348 float compute_color,
349 float &value,
350 float4 &color)
351{
353 detail,
354 roughness,
355 lacunarity,
356 offset,
357 gain,
358 distortion,
359 normalize,
360 compute_color,
361 value,
362 color);
363}
364
365[[node]]
367 float /*w*/,
368 float scale,
369 float detail,
370 float roughness,
371 float lacunarity,
372 float offset,
373 float gain,
374 float distortion,
375 float normalize,
376 float compute_color,
377 float &value,
378 float4 &color)
379{
381 detail,
382 roughness,
383 lacunarity,
384 offset,
385 gain,
386 distortion,
387 normalize,
388 compute_color,
389 value,
390 color);
391}
392
393[[node]]
395 float w,
396 float scale,
397 float detail,
398 float roughness,
399 float lacunarity,
400 float offset,
401 float gain,
402 float distortion,
403 float normalize,
404 float compute_color,
405 float &value,
406 float4 &color)
407{
409 detail,
410 roughness,
411 lacunarity,
412 offset,
413 gain,
414 distortion,
415 normalize,
416 compute_color,
417 value,
418 color);
419}
420
421/* Noise Multi-fractal. */
422
423[[node]]
425 float w,
426 float scale,
427 float detail,
428 float roughness,
429 float lacunarity,
430 float offset,
431 float gain,
432 float distortion,
433 float normalize,
434 float compute_color,
435 float &value,
436 float4 &color)
437{
439 detail,
440 roughness,
441 lacunarity,
442 offset,
443 gain,
444 distortion,
445 normalize,
446 compute_color,
447 value,
448 color);
449}
450
451[[node]]
453 float /*w*/,
454 float scale,
455 float detail,
456 float roughness,
457 float lacunarity,
458 float offset,
459 float gain,
460 float distortion,
461 float normalize,
462 float compute_color,
463 float &value,
464 float4 &color)
465{
467 detail,
468 roughness,
469 lacunarity,
470 offset,
471 gain,
472 distortion,
473 normalize,
474 compute_color,
475 value,
476 color);
477}
478
479[[node]]
481 float /*w*/,
482 float scale,
483 float detail,
484 float roughness,
485 float lacunarity,
486 float offset,
487 float gain,
488 float distortion,
489 float normalize,
490 float compute_color,
491 float &value,
492 float4 &color)
493{
495 detail,
496 roughness,
497 lacunarity,
498 offset,
499 gain,
500 distortion,
501 normalize,
502 compute_color,
503 value,
504 color);
505}
506
507[[node]]
509 float w,
510 float scale,
511 float detail,
512 float roughness,
513 float lacunarity,
514 float offset,
515 float gain,
516 float distortion,
517 float normalize,
518 float compute_color,
519 float &value,
520 float4 &color)
521{
523 detail,
524 roughness,
525 lacunarity,
526 offset,
527 gain,
528 distortion,
529 normalize,
530 compute_color,
531 value,
532 color);
533}
534
535/* Noise Hetero Terrain */
536
537[[node]]
539 float w,
540 float scale,
541 float detail,
542 float roughness,
543 float lacunarity,
544 float offset,
545 float gain,
546 float distortion,
547 float normalize,
548 float compute_color,
549 float &value,
550 float4 &color)
551{
553 detail,
554 roughness,
555 lacunarity,
556 offset,
557 gain,
558 distortion,
559 normalize,
560 compute_color,
561 value,
562 color);
563}
564
565[[node]]
567 float /*w*/,
568 float scale,
569 float detail,
570 float roughness,
571 float lacunarity,
572 float offset,
573 float gain,
574 float distortion,
575 float normalize,
576 float compute_color,
577 float &value,
578 float4 &color)
579{
581 detail,
582 roughness,
583 lacunarity,
584 offset,
585 gain,
586 distortion,
587 normalize,
588 compute_color,
589 value,
590 color);
591}
592
593[[node]]
595 float /*w*/,
596 float scale,
597 float detail,
598 float roughness,
599 float lacunarity,
600 float offset,
601 float gain,
602 float distortion,
603 float normalize,
604 float compute_color,
605 float &value,
606 float4 &color)
607{
609 detail,
610 roughness,
611 lacunarity,
612 offset,
613 gain,
614 distortion,
615 normalize,
616 compute_color,
617 value,
618 color);
619}
620
621[[node]]
623 float w,
624 float scale,
625 float detail,
626 float roughness,
627 float lacunarity,
628 float offset,
629 float gain,
630 float distortion,
631 float normalize,
632 float compute_color,
633 float &value,
634 float4 &color)
635{
637 detail,
638 roughness,
639 lacunarity,
640 offset,
641 gain,
642 distortion,
643 normalize,
644 compute_color,
645 value,
646 color);
647}
648
649/* Noise Hybrid Multi-fractal. */
650
651[[node]]
653 float w,
654 float scale,
655 float detail,
656 float roughness,
657 float lacunarity,
658 float offset,
659 float gain,
660 float distortion,
661 float normalize,
662 float compute_color,
663 float &value,
664 float4 &color)
665{
667 detail,
668 roughness,
669 lacunarity,
670 offset,
671 gain,
672 distortion,
673 normalize,
674 compute_color,
675 value,
676 color);
677}
678
679[[node]]
681 float /*w*/,
682 float scale,
683 float detail,
684 float roughness,
685 float lacunarity,
686 float offset,
687 float gain,
688 float distortion,
689 float normalize,
690 float compute_color,
691 float &value,
692 float4 &color)
693{
695 detail,
696 roughness,
697 lacunarity,
698 offset,
699 gain,
700 distortion,
701 normalize,
702 compute_color,
703 value,
704 color);
705}
706
707[[node]]
709 float /*w*/,
710 float scale,
711 float detail,
712 float roughness,
713 float lacunarity,
714 float offset,
715 float gain,
716 float distortion,
717 float normalize,
718 float compute_color,
719 float &value,
720 float4 &color)
721{
723 detail,
724 roughness,
725 lacunarity,
726 offset,
727 gain,
728 distortion,
729 normalize,
730 compute_color,
731 value,
732 color);
733}
734
735[[node]]
737 float w,
738 float scale,
739 float detail,
740 float roughness,
741 float lacunarity,
742 float offset,
743 float gain,
744 float distortion,
745 float normalize,
746 float compute_color,
747 float &value,
748 float4 &color)
749{
751 detail,
752 roughness,
753 lacunarity,
754 offset,
755 gain,
756 distortion,
757 normalize,
758 compute_color,
759 value,
760 color);
761}
762
763/* Noise Ridged Multi-fractal. */
764
765[[node]]
767 float w,
768 float scale,
769 float detail,
770 float roughness,
771 float lacunarity,
772 float offset,
773 float gain,
774 float distortion,
775 float normalize,
776 float compute_color,
777 float &value,
778 float4 &color)
779{
781 detail,
782 roughness,
783 lacunarity,
784 offset,
785 gain,
786 distortion,
787 normalize,
788 compute_color,
789 value,
790 color);
791}
792
793[[node]]
795 float /*w*/,
796 float scale,
797 float detail,
798 float roughness,
799 float lacunarity,
800 float offset,
801 float gain,
802 float distortion,
803 float normalize,
804 float compute_color,
805 float &value,
806 float4 &color)
807{
809 detail,
810 roughness,
811 lacunarity,
812 offset,
813 gain,
814 distortion,
815 normalize,
816 compute_color,
817 value,
818 color);
819}
820
821[[node]]
823 float /*w*/,
824 float scale,
825 float detail,
826 float roughness,
827 float lacunarity,
828 float offset,
829 float gain,
830 float distortion,
831 float normalize,
832 float compute_color,
833 float &value,
834 float4 &color)
835{
837 detail,
838 roughness,
839 lacunarity,
840 offset,
841 gain,
842 distortion,
843 normalize,
844 compute_color,
845 value,
846 color);
847}
848
849[[node]]
851 float w,
852 float scale,
853 float detail,
854 float roughness,
855 float lacunarity,
856 float offset,
857 float gain,
858 float distortion,
859 float normalize,
860 float compute_color,
861 float &value,
862 float4 &color)
863{
865 detail,
866 roughness,
867 lacunarity,
868 offset,
869 gain,
870 distortion,
871 normalize,
872 compute_color,
873 value,
874 color);
875}
btScalar max
SIMD_FORCE_INLINE const btScalar & w() const
Return the w value.
Definition btQuadWord.h:119
SIMD_FORCE_INLINE btScalar norm() const
Return the norm (length) of the vector.
Definition btVector3.h:263
float hash_float_to_float(float k)
float hash_vec2_to_float(float2 k)
VecBase< float, D > normalize(VecOp< float, D >) RET
constexpr T clamp(T, U, U) RET
float noise_fbm(T co, float detail, float roughness, float lacunarity, float, float, bool normalize)
float noise_multi_fractal(T co, float detail, float roughness, float lacunarity, float, float, bool)
float noise_hybrid_multi_fractal(T co, float detail, float roughness, float lacunarity, float offset, float gain, bool)
float noise_ridged_multi_fractal(T co, float detail, float roughness, float lacunarity, float offset, float gain, bool)
float noise_hetero_terrain(T co, float detail, float roughness, float lacunarity, float offset, float, bool)
float snoise(float p)
void node_noise_tex_ridged_multi_fractal_3d(float3 co, float, float scale, float detail, float roughness, float lacunarity, float offset, float gain, float distortion, float normalize, float compute_color, float &value, float4 &color)
float distort_point(float p, float distortion)
void node_noise_tex_hetero_terrain_4d(float3 co, float w, float scale, float detail, float roughness, float lacunarity, float offset, float gain, float distortion, float normalize, float compute_color, float &value, float4 &color)
void node_noise_tex_multi_fractal_4d(float3 co, float w, float scale, float detail, float roughness, float lacunarity, float offset, float gain, float distortion, float normalize, float compute_color, float &value, float4 &color)
void node_noise_tex_fbm_1d(float3, float w, float scale, float detail, float roughness, float lacunarity, float offset, float gain, float distortion, float normalize, float compute_color, float &value, float4 &color)
float2 random_vec2_offset(float seed)
void node_noise_tex_multi_fractal_2d(float3 co, float, float scale, float detail, float roughness, float lacunarity, float offset, float gain, float distortion, float normalize, float compute_color, float &value, float4 &color)
void node_noise_tex_hybrid_multi_fractal_2d(float3 co, float, float scale, float detail, float roughness, float lacunarity, float offset, float gain, float distortion, float normalize, float compute_color, float &value, float4 &color)
void node_noise_tex_hybrid_multi_fractal_3d(float3 co, float, float scale, float detail, float roughness, float lacunarity, float offset, float gain, float distortion, float normalize, float compute_color, float &value, float4 &color)
void node_noise_tex_hybrid_multi_fractal_1d(float3, float w, float scale, float detail, float roughness, float lacunarity, float offset, float gain, float distortion, float normalize, float compute_color, float &value, float4 &color)
float4 random_vec4_offset(float seed)
void node_noise_tex_fbm_3d(float3 co, float, float scale, float detail, float roughness, float lacunarity, float offset, float gain, float distortion, float normalize, float compute_color, float &value, float4 &color)
float random_float_offset(float seed)
void node_noise_tex_multi_fractal_1d(float3, float w, float scale, float detail, float roughness, float lacunarity, float offset, float gain, float distortion, float normalize, float compute_color, float &value, float4 &color)
void node_noise_tex_fbm_2d(float3 co, float, float scale, float detail, float roughness, float lacunarity, float offset, float gain, float distortion, float normalize, float compute_color, float &value, float4 &color)
void node_noise_tex_ridged_multi_fractal_4d(float3 co, float w, float scale, float detail, float roughness, float lacunarity, float offset, float gain, float distortion, float normalize, float compute_color, float &value, float4 &color)
float3 random_vec3_offset(float seed)
void node_noise_tex_hetero_terrain_2d(float3 co, float, float scale, float detail, float roughness, float lacunarity, float offset, float gain, float distortion, float normalize, float compute_color, float &value, float4 &color)
void node_noise_tex_hybrid_multi_fractal_4d(float3 co, float w, float scale, float detail, float roughness, float lacunarity, float offset, float gain, float distortion, float normalize, float compute_color, float &value, float4 &color)
void noise_fractal_distorted(T p, float detail, float roughness, float lacunarity, float offset, float gain, float distortion, float normalize, float compute_color, float &value, float4 &color)
void node_noise_tex_hetero_terrain_3d(float3 co, float, float scale, float detail, float roughness, float lacunarity, float offset, float gain, float distortion, float normalize, float compute_color, float &value, float4 &color)
float color_seed_offset(float)
void node_noise_tex_ridged_multi_fractal_2d(float3 co, float, float scale, float detail, float roughness, float lacunarity, float offset, float gain, float distortion, float normalize, float compute_color, float &value, float4 &color)
void node_noise_tex_ridged_multi_fractal_1d(float3, float w, float scale, float detail, float roughness, float lacunarity, float offset, float gain, float distortion, float normalize, float compute_color, float &value, float4 &color)
void node_noise_tex_hetero_terrain_1d(float3, float w, float scale, float detail, float roughness, float lacunarity, float offset, float gain, float distortion, float normalize, float compute_color, float &value, float4 &color)
void node_noise_tex_fbm_4d(float3 co, float w, float scale, float detail, float roughness, float lacunarity, float offset, float gain, float distortion, float normalize, float compute_color, float &value, float4 &color)
void node_noise_tex_multi_fractal_3d(float3 co, float, float scale, float detail, float roughness, float lacunarity, float offset, float gain, float distortion, float normalize, float compute_color, float &value, float4 &color)
float3x3 scale(float3x3 mat, float2 scale)
#define T
KERNEL_FEATURE_LIGHT_PASSES KERNEL_FEATURE_PATH_GUIDING float
static float noise_fn(T co, float detail, float roughness, float lacunarity, float offset, float gain, bool normalize)
static float noise_fn(T co, float detail, float roughness, float lacunarity, float offset, float gain, bool normalize)
static float noise_fn(T co, float detail, float roughness, float lacunarity, float offset, float gain, bool normalize)
static float noise_fn(T co, float detail, float roughness, float lacunarity, float offset, float gain, bool normalize)
static float noise_fn(T co, float detail, float roughness, float lacunarity, float offset, float gain, bool normalize)