Blender V5.3
gabor.h
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2024 Blender Foundation
2 *
3 * SPDX-License-Identifier: Apache-2.0 */
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.3 (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.4
18 * (2019): 1-13.
19 */
20
21#pragma once
22
24#include "kernel/svm/util.h"
25
26#include "util/hash.h"
27
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 // NOLINT
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 the
54 * 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 const float frequency,
65 const float orientation)
66{
67 const float distance_squared = dot(position, position);
68 const float hann_window = 0.5f + 0.5f * cosf(M_PI_F * distance_squared);
69 const float gaussian_envelop = expf(-M_PI_F * distance_squared);
70 const float windowed_gaussian_envelope = gaussian_envelop * hann_window;
71
72 const float angle = 2.0f * M_PI_F * dot(position, polar_to_cartesian(frequency, orientation));
73 return polar_to_cartesian(windowed_gaussian_envelope, angle);
74}
75
113{
114 const float integral_of_gabor_squared = 0.25f;
115 const float second_moment = 0.5f;
116 return sqrtf(IMPULSES_COUNT * second_moment * integral_of_gabor_squared);
117}
118
119/* Computes the Gabor noise value at the given position for the given cell. This is essentially the
120 * sum in Equation (8) in the original Gabor noise paper, where we sum Gabor kernels sampled at a
121 * random position with a random weight. The orientation of the kernel is constant for anisotropic
122 * noise while it is random for isotropic noise. The original Gabor noise paper mentions that the
123 * weights should be uniformly distributed in the [-1, 1] range, however, Tavernier's paper showed
124 * that using a Bernoulli distribution yields better results, so that is what we do. */
126 const float2 position,
127 const float frequency,
128 const float isotropy,
129 const float base_orientation)
130
131{
132 float2 noise = make_float2(0.0f, 0.0f);
133 for (int i = 0; i < IMPULSES_COUNT; ++i) {
134 /* Compute unique seeds for each of the needed random variables. */
135 const float3 seed_for_orientation = make_float3(cell.x, cell.y, i * 3);
136 const float3 seed_for_kernel_center = make_float3(cell.x, cell.y, i * 3 + 1);
137 const float3 seed_for_weight = make_float3(cell.x, cell.y, i * 3 + 2);
138
139 /* For isotropic noise, add a random orientation amount, while for anisotropic noise, use the
140 * base orientation. Linearly interpolate between the two cases using the isotropy factor. Note
141 * that the random orientation range spans pi as opposed to two pi, that's because the Gabor
142 * kernel is symmetric around pi. */
143 const float random_orientation = (hash_float3_to_float(seed_for_orientation) - 0.5f) * M_PI_F;
144 const float orientation = base_orientation + random_orientation * isotropy;
145
146 const float2 kernel_center = hash_float3_to_float2(seed_for_kernel_center);
147 const float2 position_in_kernel_space = position - kernel_center;
148
149 /* The kernel is windowed beyond the unit distance, so early exit with a zero for points that
150 * are further than a unit radius. */
151 if (dot(position_in_kernel_space, position_in_kernel_space) >= 1.0f) {
152 continue;
153 }
154
155 /* We either add or subtract the Gabor kernel based on a Bernoulli distribution of equal
156 * probability. */
157 const float weight = hash_float3_to_float(seed_for_weight) < 0.5f ? -1.0f : 1.0f;
158
159 noise += weight * compute_2d_gabor_kernel(position_in_kernel_space, frequency, orientation);
160 }
161 return noise;
162}
163
164/* Computes the Gabor noise value by dividing the space into a grid and evaluating the Gabor noise
165 * in the space of each cell of the 3x3 cell neighborhood. */
167 const float frequency,
168 const float isotropy,
169 const float base_orientation)
170{
171 const float2 cell_position = floor(coordinates);
172 const float2 local_position = coordinates - cell_position;
173
174 float2 sum = make_float2(0.0f, 0.0f);
175 for (int j = -1; j <= 1; j++) {
176 for (int i = -1; i <= 1; i++) {
177 const float2 cell_offset = make_float2(i, j);
178 const float2 current_cell_position = cell_position + cell_offset;
179 const float2 position_in_cell_space = local_position - cell_offset;
181 current_cell_position, position_in_cell_space, frequency, isotropy, base_orientation);
182 }
183 }
184
185 return sum;
186}
187
188/* Identical to compute_2d_gabor_kernel, except it is evaluated in 3D space. Notice that Equation
189 * (6) in the original Gabor noise paper computes the frequency vector using (cos(w_0), sin(w_0)),
190 * which we also do in the 2D variant, however, for 3D, the orientation is already a unit frequency
191 * vector, so we just need to scale it by the frequency value. */
193 const float frequency,
194 const float3 orientation)
195{
196 const float distance_squared = dot(position, position);
197 const float hann_window = 0.5f + 0.5f * cosf(M_PI_F * distance_squared);
198 const float gaussian_envelop = expf(-M_PI_F * distance_squared);
199 const float windowed_gaussian_envelope = gaussian_envelop * hann_window;
200
201 const float3 frequency_vector = frequency * orientation;
202 const float angle = 2.0f * M_PI_F * dot(position, frequency_vector);
203 return polar_to_cartesian(windowed_gaussian_envelope, angle);
204}
205
206/* Identical to compute_2d_gabor_standard_deviation except we do triple integration in 3D. The only
207 * difference is the denominator in the integral expression, which is `2^{5 / 2}` for the 3D case
208 * instead of 4 for the 2D case. Similarly, the limit evaluates to `1 / (4 * sqrt(2))`. */
210{
211 const float integral_of_gabor_squared = 1.0f / (4.0f * M_SQRT2_F);
212 const float second_moment = 0.5f;
213 return sqrtf(IMPULSES_COUNT * second_moment * integral_of_gabor_squared);
214}
215
216/* Computes the orientation of the Gabor kernel such that it is constant for anisotropic
217 * noise while it is random for isotropic noise. We randomize in spherical coordinates for a
218 * uniform distribution. */
220 const float isotropy,
221 const float4 seed)
222{
223 /* Return the base orientation in case we are completely anisotropic. */
224 if (isotropy == 0.0f) {
225 return orientation;
226 }
227
228 /* Compute the orientation in spherical coordinates. */
229 float inclination = acosf(orientation.z);
230 float azimuth = (orientation.y < 0.0f ? -1.0f : 1.0f) *
231 acosf(orientation.x / len(make_float2(orientation.x, orientation.y)));
232
233 /* For isotropic noise, add a random orientation amount, while for anisotropic noise, use the
234 * base orientation. Linearly interpolate between the two cases using the isotropy factor. Note
235 * that the random orientation range is to pi as opposed to two pi, that's because the Gabor
236 * kernel is symmetric around pi. */
237 const float2 random_angles = hash_float4_to_float2(seed) * M_PI_F;
238 inclination += random_angles.x * isotropy;
239 azimuth += random_angles.y * isotropy;
240
241 /* Convert back to Cartesian coordinates. */
242 return spherical_to_direction(inclination, azimuth);
243}
244
246 const float3 position,
247 const float frequency,
248 const float isotropy,
249 const float3 base_orientation)
250
251{
252 float2 noise = make_float2(0.0f, 0.0f);
253 for (int i = 0; i < IMPULSES_COUNT; ++i) {
254 /* Compute unique seeds for each of the needed random variables. */
255 const float4 seed_for_orientation = make_float4(cell, i * 3);
256 const float4 seed_for_kernel_center = make_float4(cell, i * 3 + 1);
257 const float4 seed_for_weight = make_float4(cell, i * 3 + 2);
258
259 const float3 orientation = compute_3d_orientation(
260 base_orientation, isotropy, seed_for_orientation);
261
262 const float3 kernel_center = hash_float4_to_float3(seed_for_kernel_center);
263 const float3 position_in_kernel_space = position - kernel_center;
264
265 /* The kernel is windowed beyond the unit distance, so early exit with a zero for points that
266 * are further than a unit radius. */
267 if (dot(position_in_kernel_space, position_in_kernel_space) >= 1.0f) {
268 continue;
269 }
270
271 /* We either add or subtract the Gabor kernel based on a Bernoulli distribution of equal
272 * probability. */
273 const float weight = hash_float4_to_float(seed_for_weight) < 0.5f ? -1.0f : 1.0f;
274
275 noise += weight * compute_3d_gabor_kernel(position_in_kernel_space, frequency, orientation);
276 }
277 return noise;
278}
279
280/* Identical to compute_2d_gabor_noise but works in the 3D neighborhood of the noise. */
282 const float frequency,
283 const float isotropy,
284 const float3 base_orientation)
285{
286 const float3 cell_position = floor(coordinates);
287 const float3 local_position = coordinates - cell_position;
288
289 float2 sum = make_float2(0.0f, 0.0f);
290 for (int k = -1; k <= 1; k++) {
291 for (int j = -1; j <= 1; j++) {
292 for (int i = -1; i <= 1; i++) {
293 const float3 cell_offset = make_float3(i, j, k);
294 const float3 current_cell_position = cell_position + cell_offset;
295 const float3 position_in_cell_space = local_position - cell_offset;
297 current_cell_position, position_in_cell_space, frequency, isotropy, base_orientation);
298 }
299 }
300 }
301
302 return sum;
303}
304
307{
308 const float3 coordinates = stack_load_float3(stack, node.coordinates);
309
310 const float scale = stack_load(stack, node.scale);
311 float frequency = stack_load(stack, node.frequency);
312 const float anisotropy = stack_load(stack, node.anisotropy);
313 const float orientation_2d = stack_load(stack, node.orientation_2d);
314 const float3 orientation_3d = stack_load(stack, node.orientation_3d);
315
316 const float3 scaled_coordinates = coordinates * scale;
317 const float isotropy = 1.0f - clamp(anisotropy, 0.0f, 1.0f);
318 frequency = max(0.001f, frequency);
319
320 float2 phasor = make_float2(0.0f, 0.0f);
321 float standard_deviation = 1.0f;
322 switch (node.gabor_type) {
323 case NODE_GABOR_TYPE_2D: {
324 phasor = compute_2d_gabor_noise(make_float2(scaled_coordinates.x, scaled_coordinates.y),
325 frequency,
326 isotropy,
327 orientation_2d);
328 standard_deviation = compute_2d_gabor_standard_deviation();
329 break;
330 }
331 case NODE_GABOR_TYPE_3D: {
332 const float3 orientation = normalize(orientation_3d);
333 phasor = compute_3d_gabor_noise(scaled_coordinates, frequency, isotropy, orientation);
334 standard_deviation = compute_3d_gabor_standard_deviation();
335 break;
336 }
337 }
338
339 /* Normalize the noise by dividing by six times the standard deviation, which was determined
340 * empirically. */
341 const float normalization_factor = 6.0f * standard_deviation;
342
343 /* As discussed in compute_2d_gabor_kernel, we use the imaginary part of the phasor as the Gabor
344 * value. But remap to [0, 1] from [-1, 1]. */
345 if (stack_valid(node.value_offset)) {
346 stack_store_float(stack, node.value_offset, (phasor.y / normalization_factor) * 0.5f + 0.5f);
347 }
348
349 /* Compute the phase based on equation (9) in Tricard's paper. But remap the phase into the
350 * [0, 1] range. */
351 if (stack_valid(node.phase_offset)) {
352 const float phase = (atan2f(phasor.y, phasor.x) + M_PI_F) / (2.0f * M_PI_F);
353 stack_store_float(stack, node.phase_offset, phase);
354 }
355
356 /* Compute the intensity based on equation (8) in Tricard's paper. */
357 if (stack_valid(node.intensity_offset)) {
358 stack_store_float(stack, node.intensity_offset, len(phasor) / normalization_factor);
359 }
360}
361
btScalar max
SIMD_FORCE_INLINE btScalar angle(const btVector3 &v) const
Return the angle between this and another vector.
Definition btVector3.h:356
ccl_device_inline void stack_store_float(ccl_private float *stack, const uint a, const float f)
ccl_device_inline float3 stack_load_float3(const ccl_private float *stack, const uint a)
ccl_device_inline bool stack_valid(const uint a)
ccl_device_inline T stack_load(const ccl_private float *stack, const uint a)
ccl_device float3 spherical_to_direction(const float theta, const float phi)
ccl_device_inline float2 polar_to_cartesian(const float r, const float phi)
#define ccl_restrict
#define M_SQRT2_F
#define ccl_private
#define ccl_global
#define ccl_device_noinline
#define expf(x)
#define CCL_NAMESPACE_END
ccl_device_forceinline float3 make_float3(const float x, const float y, const float z)
#define acosf(x)
ccl_device_noinline void svm_node_tex_gabor(ccl_private float *ccl_restrict stack, const ccl_global SVMNodeTexGabor &ccl_restrict node)
Definition gabor.h:305
#define position
#define frequency(freq)
VecBase< float, D > normalize(VecOp< float, D >) RET
float dot(VecOp< float, D >, VecOp< float, D >) RET
#define floor
constexpr T clamp(T, U, U) RET
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)
float3x3 scale(float3x3 mat, float2 scale)
float distance_squared(VecT a, VecT b)
ccl_device_inline float2 hash_float3_to_float2(const float3 k)
Definition hash.h:328
ccl_device_inline float hash_float3_to_float(const float3 k)
Definition hash.h:227
ccl_device_inline float hash_float4_to_float(const float4 k)
Definition hash.h:232
ccl_device_inline float2 hash_float4_to_float2(const float4 k)
Definition hash.h:334
ccl_device_inline float3 hash_float4_to_float3(const float4 k)
Definition hash.h:314
@ NODE_GABOR_TYPE_2D
@ NODE_GABOR_TYPE_3D
ccl_device_inline dual1 len(const ccl_private dual< T > &a)
Definition math_dual.h:263
#define sqrtf
#define ccl_device
#define make_float2
#define make_float4
#define M_PI_F
#define cosf
#define atan2f
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