Blender V5.3
gpu_shader_material_tex_gabor.bsl.hh
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2024 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
5/* Implements Gabor noise based on the paper:
6 *
7 * Lagae, Ares, et al. "Procedural noise using sparse Gabor convolution." ACM Transactions on
8 * Graphics (TOG) 28.3f (2009): 1-10.
9 *
10 * But with the improvements from the paper:
11 *
12 * Tavernier, Vincent, et al. "Making gabor noise fast and normalized." Eurographics 2019-40th
13 * Annual Conference of the European Association for Computer Graphics. 2019.
14 *
15 * And compute the Phase and Intensity of the Gabor based on the paper:
16 *
17 * Tricard, Thibault, et al. "Procedural phasor noise." ACM Transactions on Graphics (TOG) 38.4f
18 * (2019): 1-13.
19 */
20
21#pragma once
22
26
27#define SHD_GABOR_TYPE_2D 0.0f
28#define SHD_GABOR_TYPE_3D 1.0f
29
30/* The original Gabor noise paper specifies that the impulses count for each cell should be
31 * computed by sampling a Poisson distribution whose mean is the impulse density. However,
32 * Tavernier's paper showed that stratified Poisson point sampling is better assuming the weights
33 * are sampled using a Bernoulli distribution, as shown in Figure (3). By stratified sampling, they
34 * mean a constant number of impulses per cell, so the stratification is the grid itself in that
35 * sense, as described in the supplementary material of the paper. */
36#define IMPULSES_COUNT 8
37
38/* Computes a 2D Gabor kernel based on Equation (6) in the original Gabor noise paper. Where the
39 * frequency argument is the F_0 parameter and the orientation argument is the w_0 parameter. We
40 * assume the Gaussian envelope has a unit magnitude, that is, K = 1. That is because we will
41 * eventually normalize the final noise value to the unit range, so the multiplication by the
42 * magnitude will be canceled by the normalization. Further, we also assume a unit Gaussian width,
43 * that is, a = 1. That is because it does not provide much artistic control. It follows that the
44 * Gaussian will be truncated at pi.
45 *
46 * To avoid the discontinuities caused by the aforementioned truncation, the Gaussian is windowed
47 * using a Hann window, that is because contrary to the claim made in the original Gabor paper,
48 * truncating the Gaussian produces significant artifacts especially when differentiated for bump
49 * mapping. The Hann window is C1 continuous and has limited effect on the shape of the Gaussian,
50 * so it felt like an appropriate choice.
51 *
52 * Finally, instead of computing the Gabor value directly, we instead use the complex phasor
53 * formulation described in section 3.1.1 in Tricard's paper. That's done to be able to compute
54 * the phase and intensity of the Gabor noise after summation based on equations (8) and (9). The
55 * return value of the Gabor kernel function is then a complex number whose real value is the
56 * value computed in the original Gabor noise paper, and whose imaginary part is the sine
57 * counterpart of the real part, which is the only extra computation in the new formulation.
58 *
59 * Note that while the original Gabor noise paper uses the cosine part of the phasor, that is, the
60 * real part of the phasor, we use the sine part instead, that is, the imaginary part of the
61 * phasor, as suggested by Tavernier's paper in "Section 3.3. Instance stationarity and
62 * normalization", to ensure a zero mean, which should help with normalization. */
64{
66 float hann_window = 0.5f + 0.5f * cos(M_PI * distance_squared);
67 float gaussian_envelop = exp(-M_PI * distance_squared);
68 float windowed_gaussian_envelope = gaussian_envelop * hann_window;
69
70 float2 frequency_vector = frequency * float2(cos(orientation), sin(orientation));
71 float angle = 2.0f * M_PI * dot(position, frequency_vector);
72 float2 phasor = float2(cos(angle), sin(angle));
73
74 return windowed_gaussian_envelope * phasor;
75}
76
77/* Computes the approximate standard deviation of the zero mean normal distribution representing
78 * the amplitude distribution of the noise based on Equation (9) in the original Gabor noise paper.
79 * For simplicity, the Hann window is ignored and the orientation is fixed since the variance is
80 * orientation invariant. We start integrating the squared Gabor kernel with respect to x:
81 *
82 * \int_{-\infty}^{-\infty} (e^{- \pi (x^2 + y^2)} cos(2 \pi f_0 x))^2 dx
83 *
84 * Which gives:
85 *
86 * \frac{(e^{2 \pi f_0^2}-1) e^{-2 \pi y^2 - 2 pi f_0^2}}{2^\frac{3}{2}}
87 *
88 * Then we similarly integrate with respect to y to get:
89 *
90 * \frac{1 - e^{-2 \pi f_0^2}}{4}
91 *
92 * Secondly, we note that the second moment of the weights distribution is 0.5 since it is a
93 * fair Bernoulli distribution. So the final standard deviation expression is square root the
94 * integral multiplied by the impulse density multiplied by the second moment.
95 *
96 * Note however that the integral is almost constant for all frequencies larger than one, and
97 * converges to an upper limit as the frequency approaches infinity, so we replace the expression
98 * with the following limit:
99 *
100 * \lim_{x \to \infty} \frac{1 - e^{-2 \pi f_0^2}}{4}
101 *
102 * To get an approximation of 0.25. */
104{
105 float integral_of_gabor_squared = 0.25f;
106 float second_moment = 0.5f;
107 return sqrt(IMPULSES_COUNT * second_moment * integral_of_gabor_squared);
108}
109
110/* Computes the Gabor noise value at the given position for the given cell. This is essentially the
111 * sum in Equation (8) in the original Gabor noise paper, where we sum Gabor kernels sampled at a
112 * random position with a random weight. The orientation of the kernel is constant for anisotropic
113 * noise while it is random for isotropic noise. The original Gabor noise paper mentions that the
114 * weights should be uniformly distributed in the [-1, 1] range, however, Tavernier's paper showed
115 * that using a Bernoulli distribution yields better results, so that is what we do. */
117 float2 cell, float2 position, float frequency, float isotropy, float base_orientation)
118
119{
120 float2 noise = float2(0.0f);
121 for (int i = 0; i < IMPULSES_COUNT; ++i) {
122 /* Compute unique seeds for each of the needed random variables. */
123 float3 seed_for_orientation = float3(cell, i * 3);
124 float3 seed_for_kernel_center = float3(cell, i * 3 + 1);
125 float3 seed_for_weight = float3(cell, i * 3 + 2);
126
127 /* For isotropic noise, add a random orientation amount, while for anisotropic noise, use the
128 * base orientation. Linearly interpolate between the two cases using the isotropy factor. Note
129 * that the random orientation range spans pi as opposed to two pi, that's because the Gabor
130 * kernel is symmetric around pi. */
131 float random_orientation = (hash_vec3_to_float(seed_for_orientation) - 0.5f) * M_PI;
132 float orientation = base_orientation + random_orientation * isotropy;
133
134 float2 kernel_center = hash_vec3_to_vec2(seed_for_kernel_center);
135 float2 position_in_kernel_space = position - kernel_center;
136
137 /* The kernel is windowed beyond the unit distance, so early exit with a zero for points that
138 * are further than a unit radius. */
139 if (length_squared(position_in_kernel_space) >= 1.0f) {
140 continue;
141 }
142
143 /* We either add or subtract the Gabor kernel based on a Bernoulli distribution of equal
144 * probability. */
145 float weight = hash_vec3_to_float(seed_for_weight) < 0.5f ? -1.0f : 1.0f;
146
147 noise += weight * compute_2d_gabor_kernel(position_in_kernel_space, frequency, orientation);
148 }
149 return noise;
150}
151
152/* Computes the Gabor noise value by dividing the space into a grid and evaluating the Gabor noise
153 * in the space of each cell of the 3x3 cell neighborhood. */
155 float frequency,
156 float isotropy,
157 float base_orientation)
158{
159 float2 cell_position = floor(coordinates);
160 float2 local_position = coordinates - cell_position;
161
162 float2 sum = float2(0.0f);
163 for (int j = -1; j <= 1; j++) {
164 for (int i = -1; i <= 1; i++) {
165 float2 cell_offset = float2(i, j);
166
167 float2 current_cell_position = cell_position + cell_offset;
168 float2 position_in_cell_space = local_position - cell_offset;
169
171 current_cell_position, position_in_cell_space, frequency, isotropy, base_orientation);
172 }
173 }
174
175 return sum;
176}
177
178/* Identical to compute_2d_gabor_kernel, except it is evaluated in 3D space. Notice that Equation
179 * (6) in the original Gabor noise paper computes the frequency vector using (cos(w_0), sin(w_0)),
180 * which we also do in the 2D variant, however, for 3D, the orientation is already a unit frequency
181 * vector, so we just need to scale it by the frequency value. */
183{
185 float hann_window = 0.5f + 0.5f * cos(M_PI * distance_squared);
186 float gaussian_envelop = exp(-M_PI * distance_squared);
187 float windowed_gaussian_envelope = gaussian_envelop * hann_window;
188
189 float3 frequency_vector = frequency * orientation;
190 float angle = 2.0f * M_PI * dot(position, frequency_vector);
191 float2 phasor = float2(cos(angle), sin(angle));
192
193 return windowed_gaussian_envelope * phasor;
194}
195
196/* Identical to compute_2d_gabor_standard_deviation except we do triple integration in 3D. The only
197 * difference is the denominator in the integral expression, which is 2^{5 / 2} for the 3D case
198 * instead of 4 for the 2D case. Similarly, the limit evaluates to 1 / (4 * sqrt(2)). */
200{
201 float integral_of_gabor_squared = 1.0f / (4.0f * M_SQRT2);
202 float second_moment = 0.5f;
203 return sqrt(IMPULSES_COUNT * second_moment * integral_of_gabor_squared);
204}
205
206/* Computes the orientation of the Gabor kernel such that it is constant for anisotropic
207 * noise while it is random for isotropic noise. We randomize in spherical coordinates for a
208 * uniform distribution. */
209float3 compute_3d_orientation(float3 orientation, float isotropy, float4 seed)
210{
211 /* Return the base orientation in case we are completely anisotropic. */
212 if (isotropy == 0.0f) {
213 return orientation;
214 }
215
216 /* Compute the orientation in spherical coordinates. */
217 float inclination = acos(orientation.z);
218 float azimuth = sign(orientation.y) * acos(orientation.x / length(orientation.xy));
219
220 /* For isotropic noise, add a random orientation amount, while for anisotropic noise, use the
221 * base orientation. Linearly interpolate between the two cases using the isotropy factor. Note
222 * that the random orientation range is to pi as opposed to two pi, that's because the Gabor
223 * kernel is symmetric around pi. */
224 float2 random_angles = hash_vec4_to_vec2(seed) * M_PI;
225 inclination += random_angles.x * isotropy;
226 azimuth += random_angles.y * isotropy;
227
228 /* Convert back to Cartesian coordinates, */
229 return float3(
230 sin(inclination) * cos(azimuth), sin(inclination) * sin(azimuth), cos(inclination));
231}
232
234 float3 cell, float3 position, float frequency, float isotropy, float3 base_orientation)
235
236{
237 float2 noise = float2(0.0f);
238 for (int i = 0; i < IMPULSES_COUNT; ++i) {
239 /* Compute unique seeds for each of the needed random variables. */
240 float4 seed_for_orientation = float4(cell, i * 3);
241 float4 seed_for_kernel_center = float4(cell, i * 3 + 1);
242 float4 seed_for_weight = float4(cell, i * 3 + 2);
243
244 float3 orientation = compute_3d_orientation(base_orientation, isotropy, seed_for_orientation);
245
246 float3 kernel_center = hash_vec4_to_vec3(seed_for_kernel_center);
247 float3 position_in_kernel_space = position - kernel_center;
248
249 /* The kernel is windowed beyond the unit distance, so early exit with a zero for points that
250 * are further than a unit radius. */
251 if (length_squared(position_in_kernel_space) >= 1.0f) {
252 continue;
253 }
254
255 /* We either add or subtract the Gabor kernel based on a Bernoulli distribution of equal
256 * probability. */
257 float weight = hash_vec4_to_float(seed_for_weight) < 0.5f ? -1.0f : 1.0f;
258
259 noise += weight * compute_3d_gabor_kernel(position_in_kernel_space, frequency, orientation);
260 }
261 return noise;
262}
263
264/* Identical to compute_2d_gabor_noise but works in the 3D neighborhood of the noise. */
266 float frequency,
267 float isotropy,
268 float3 base_orientation)
269{
270 float3 cell_position = floor(coordinates);
271 float3 local_position = coordinates - cell_position;
272
273 float2 sum = float2(0.0f);
274 for (int k = -1; k <= 1; k++) {
275 for (int j = -1; j <= 1; j++) {
276 for (int i = -1; i <= 1; i++) {
277 float3 cell_offset = float3(i, j, k);
278 float3 current_cell_position = cell_position + cell_offset;
279 float3 position_in_cell_space = local_position - cell_offset;
281 current_cell_position, position_in_cell_space, frequency, isotropy, base_orientation);
282 }
283 }
284 }
285
286 return sum;
287}
288
289[[node]]
290void node_tex_gabor(float3 coordinates,
291 float scale,
292 float frequency,
293 float anisotropy,
294 float orientation_2d,
295 float3 orientation_3d,
296 float type,
297 float &output_value,
298 float &output_phase,
299 float &output_intensity)
300{
301 float3 scaled_coordinates = coordinates * scale;
302 float isotropy = 1.0f - clamp(anisotropy, 0.0f, 1.0f);
303 frequency = max(0.001f, frequency);
304
305 float2 phasor = float2(0.0f);
306 float standard_deviation = 1.0f;
307 if (type == SHD_GABOR_TYPE_2D) {
308 phasor = compute_2d_gabor_noise(scaled_coordinates.xy, frequency, isotropy, orientation_2d);
309 standard_deviation = compute_2d_gabor_standard_deviation();
310 }
311 else if (type == SHD_GABOR_TYPE_3D) {
312 float3 orientation = normalize(orientation_3d);
313 phasor = compute_3d_gabor_noise(scaled_coordinates, frequency, isotropy, orientation);
314 standard_deviation = compute_3d_gabor_standard_deviation();
315 }
316
317 /* Normalize the noise by dividing by six times the standard deviation, which was determined
318 * empirically. */
319 float normalization_factor = 6.0f * standard_deviation;
320
321 /* As discussed in compute_2d_gabor_kernel, we use the imaginary part of the phasor as the Gabor
322 * value. But remap to [0, 1] from [-1, 1]. */
323 output_value = (phasor.y / normalization_factor) * 0.5f + 0.5f;
324
325 /* Compute the phase based on equation (9) in Tricard's paper. But remap the phase into the
326 * [0, 1] range. */
327 output_phase = (atan(phasor.y, phasor.x) + M_PI) / (2.0f * M_PI);
328
329 /* Compute the intensity based on equation (8) in Tricard's paper. */
330 output_intensity = length(phasor) / normalization_factor;
331}
#define M_SQRT2
#define M_PI
btScalar max
SIMD_FORCE_INLINE btScalar angle(const btVector3 &v) const
Return the angle between this and another vector.
Definition btVector3.h:356
float3 hash_vec4_to_vec3(float4 k)
float hash_vec4_to_float(float4 k)
float2 hash_vec3_to_vec2(float3 k)
float hash_vec3_to_float(float3 k)
float2 hash_vec4_to_vec2(float4 k)
#define position
#define frequency(freq)
#define atan
#define sin
#define exp
#define cos
VecBase< float, D > normalize(VecOp< float, D >) RET
float dot(VecOp< float, D >, VecOp< float, D >) RET
constexpr T sign(T) RET
#define floor
#define sqrt
constexpr T clamp(T, U, U) RET
float length(VecOp< float, D >) RET
#define acos
float noise(float p)
float compute_3d_gabor_standard_deviation()
float compute_2d_gabor_standard_deviation()
float2 compute_3d_gabor_kernel(float3 position, float frequency, float3 orientation)
float2 compute_2d_gabor_noise_cell(float2 cell, float2 position, float frequency, float isotropy, float base_orientation)
float2 compute_2d_gabor_noise(float2 coordinates, float frequency, float isotropy, float base_orientation)
float2 compute_3d_gabor_noise(float3 coordinates, float frequency, float isotropy, float3 base_orientation)
float2 compute_3d_gabor_noise_cell(float3 cell, float3 position, float frequency, float isotropy, float3 base_orientation)
float2 compute_2d_gabor_kernel(float2 position, float frequency, float orientation)
float3 compute_3d_orientation(float3 orientation, float isotropy, float4 seed)
void node_tex_gabor(float3 coordinates, float scale, float frequency, float anisotropy, float orientation_2d, float3 orientation_3d, float type, float &output_value, float &output_phase, float &output_intensity)
float3x3 scale(float3x3 mat, float2 scale)
float distance_squared(VecT a, VecT b)
float length_squared(VecT a)
float x
float y
float z
Definition sky_math.h:136
float y
Definition sky_math.h:136
float x
Definition sky_math.h:136