Blender V5.3
gpu_shader_material_fractal_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#pragma once
6
8
9template<typename T>
10float noise_fbm(T co,
11 float detail,
12 float roughness,
13 float lacunarity,
14 float /*offset*/,
15 float /*gain*/,
16 bool normalize)
17{
18 T p = co;
19 float fscale = 1.0f;
20 float amp = 1.0f;
21 float maxamp = 0.0f;
22 float sum = 0.0f;
23
24 for (int i = 0; i <= int(detail); i++) {
25 float t = snoise(fscale * p);
26 sum += t * amp;
27 maxamp += amp;
28 amp *= roughness;
29 fscale *= lacunarity;
30 }
31 float rmd = detail - floor(detail);
32 if (rmd != 0.0f) {
33 float t = snoise(fscale * p);
34 float sum2 = sum + t * amp;
35 return normalize ? mix(0.5f * sum / maxamp + 0.5f, 0.5f * sum2 / (maxamp + amp) + 0.5f, rmd) :
36 mix(sum, sum2, rmd);
37 }
38 return normalize ? 0.5f * sum / maxamp + 0.5f : sum;
39}
40
41template float noise_fbm<float>(float, float, float, float, float, float, bool);
42template float noise_fbm<float2>(float2, float, float, float, float, float, bool);
43template float noise_fbm<float3>(float3, float, float, float, float, float, bool);
44template float noise_fbm<float4>(float4, float, float, float, float, float, bool);
45
46template<typename T>
48 float detail,
49 float roughness,
50 float lacunarity,
51 float /*offset*/,
52 float /*gain*/,
53 bool /*normalize*/)
54{
55 T p = co;
56 float value = 1.0f;
57 float pwr = 1.0f;
58
59 for (int i = 0; i <= int(detail); i++) {
60 value *= (pwr * snoise(p) + 1.0f);
61 pwr *= roughness;
62 p *= lacunarity;
63 }
64
65 float rmd = detail - floor(detail);
66 if (rmd != 0.0f) {
67 value *= (rmd * pwr * snoise(p) + 1.0f); /* correct? */
68 }
69
70 return value;
71}
72
73template float noise_multi_fractal<float>(float, float, float, float, float, float, bool);
74template float noise_multi_fractal<float2>(float2, float, float, float, float, float, bool);
75template float noise_multi_fractal<float3>(float3, float, float, float, float, float, bool);
76template float noise_multi_fractal<float4>(float4, float, float, float, float, float, bool);
77
78template<typename T>
80 float detail,
81 float roughness,
82 float lacunarity,
83 float offset,
84 float /*gain*/,
85 bool /*normalize*/)
86{
87 T p = co;
88 float pwr = roughness;
89
90 /* first unscaled octave of function; later octaves are scaled */
91 float value = offset + snoise(p);
92 p *= lacunarity;
93
94 for (int i = 1; i <= int(detail); i++) {
95 float increment = (snoise(p) + offset) * pwr * value;
96 value += increment;
97 pwr *= roughness;
98 p *= lacunarity;
99 }
100
101 float rmd = detail - floor(detail);
102 if (rmd != 0.0f) {
103 float increment = (snoise(p) + offset) * pwr * value;
104 value += rmd * increment;
105 }
106
107 return value;
108}
109
110template float noise_hetero_terrain<float>(float, float, float, float, float, float, bool);
111template float noise_hetero_terrain<float2>(float2, float, float, float, float, float, bool);
112template float noise_hetero_terrain<float3>(float3, float, float, float, float, float, bool);
113template float noise_hetero_terrain<float4>(float4, float, float, float, float, float, bool);
114
115template<typename T>
117 float detail,
118 float roughness,
119 float lacunarity,
120 float offset,
121 float gain,
122 bool /*normalize*/)
123{
124 T p = co;
125 float pwr = 1.0f;
126 float value = 0.0f;
127 float weight = 1.0f;
128
129 for (int i = 0; (weight > 0.001f) && (i <= int(detail)); i++) {
130 weight = min(weight, 1.0f);
131
132 float signal = (snoise(p) + offset) * pwr;
133 pwr *= roughness;
134 value += weight * signal;
135 weight *= gain * signal;
136 p *= lacunarity;
137 }
138
139 float rmd = detail - floor(detail);
140 if ((rmd != 0.0f) && (weight > 0.001f)) {
141 weight = min(weight, 1.0f);
142 float signal = (snoise(p) + offset) * pwr;
143 value += rmd * weight * signal;
144 }
145
146 return value;
147}
148
149template float noise_hybrid_multi_fractal<float>(float, float, float, float, float, float, bool);
150template float noise_hybrid_multi_fractal<float2>(float2, float, float, float, float, float, bool);
151template float noise_hybrid_multi_fractal<float3>(float3, float, float, float, float, float, bool);
152template float noise_hybrid_multi_fractal<float4>(float4, float, float, float, float, float, bool);
153
154template<typename T>
156 float detail,
157 float roughness,
158 float lacunarity,
159 float offset,
160 float gain,
161 bool /*normalize*/)
162{
163 T p = co;
164 float pwr = roughness;
165
166 float signal = offset - abs(snoise(p));
167 signal *= signal;
168 float value = signal;
169 float weight = 1.0f;
170
171 for (int i = 1; i <= int(detail); i++) {
172 p *= lacunarity;
173 weight = clamp(signal * gain, 0.0f, 1.0f);
174 signal = offset - abs(snoise(p));
175 signal *= signal;
176 signal *= weight;
177 value += signal * pwr;
178 pwr *= roughness;
179 }
180
181 return value;
182}
183
184template float noise_ridged_multi_fractal<float>(float, float, float, float, float, float, bool);
185template float noise_ridged_multi_fractal<float2>(float2, float, float, float, float, float, bool);
186template float noise_ridged_multi_fractal<float3>(float3, float, float, float, float, float, bool);
187template float noise_ridged_multi_fractal<float4>(float4, float, float, float, float, float, bool);
VecBase< float, D > normalize(VecOp< float, D >) RET
#define abs
#define floor
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)
#define T
#define mix
#define min(a, b)
Definition sort.cc:38