Blender V4.3
noisetex.h
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2011-2022 Blender Foundation
2 *
3 * SPDX-License-Identifier: Apache-2.0 */
4
5#pragma once
6
8
10
11/* The following offset functions generate random offsets to be added to texture
12 * coordinates to act as a seed since the noise functions don't have seed values.
13 * A seed value is needed for generating distortion textures and color outputs.
14 * The offset's components are in the range [100, 200], not too high to cause
15 * bad precision and not too small to be noticeable. We use float seed because
16 * OSL only support float hashes.
17 */
18
20{
21 return 100.0f + hash_float_to_float(seed) * 100.0f;
22}
23
25{
26 return make_float2(100.0f + hash_float2_to_float(make_float2(seed, 0.0f)) * 100.0f,
27 100.0f + hash_float2_to_float(make_float2(seed, 1.0f)) * 100.0f);
28}
29
31{
32 return make_float3(100.0f + hash_float2_to_float(make_float2(seed, 0.0f)) * 100.0f,
33 100.0f + hash_float2_to_float(make_float2(seed, 1.0f)) * 100.0f,
34 100.0f + hash_float2_to_float(make_float2(seed, 2.0f)) * 100.0f);
35}
36
38{
39 return make_float4(100.0f + hash_float2_to_float(make_float2(seed, 0.0f)) * 100.0f,
40 100.0f + hash_float2_to_float(make_float2(seed, 1.0f)) * 100.0f,
41 100.0f + hash_float2_to_float(make_float2(seed, 2.0f)) * 100.0f,
42 100.0f + hash_float2_to_float(make_float2(seed, 3.0f)) * 100.0f);
43}
44
45template<typename T>
47 float detail,
48 float roughness,
49 float lacunarity,
50 float offset,
51 float gain,
52 int type,
53 bool normalize)
54{
55 switch ((NodeNoiseType)type) {
57 return noise_multi_fractal(p, detail, roughness, lacunarity);
58 }
59 case NODE_NOISE_FBM: {
60 return noise_fbm(p, detail, roughness, lacunarity, normalize);
61 }
63 return noise_hybrid_multi_fractal(p, detail, roughness, lacunarity, offset, gain);
64 }
66 return noise_ridged_multi_fractal(p, detail, roughness, lacunarity, offset, gain);
67 }
69 return noise_hetero_terrain(p, detail, roughness, lacunarity, offset);
70 }
71 default: {
73 return 0.0;
74 }
75 }
76}
77
79 float detail,
80 float roughness,
81 float lacunarity,
82 float offset,
83 float gain,
84 float distortion,
85 int type,
86 bool normalize,
87 bool color_is_needed,
88 ccl_private float *value,
89 ccl_private float3 *color)
90{
91 float p = co;
92 if (distortion != 0.0f) {
93 p += snoise_1d(p + random_float_offset(0.0f)) * distortion;
94 }
95
96 *value = noise_select(p, detail, roughness, lacunarity, offset, gain, type, normalize);
97 if (color_is_needed) {
98 *color = make_float3(*value,
100 detail,
101 roughness,
102 lacunarity,
103 offset,
104 gain,
105 type,
106 normalize),
108 detail,
109 roughness,
110 lacunarity,
111 offset,
112 gain,
113 type,
114 normalize));
115 }
116}
117
119 float detail,
120 float roughness,
121 float lacunarity,
122 float offset,
123 float gain,
124 float distortion,
125 int type,
126 bool normalize,
127 bool color_is_needed,
128 ccl_private float *value,
129 ccl_private float3 *color)
130{
131 float2 p = co;
132 if (distortion != 0.0f) {
133 p += make_float2(snoise_2d(p + random_float2_offset(0.0f)) * distortion,
134 snoise_2d(p + random_float2_offset(1.0f)) * distortion);
135 }
136
137 *value = noise_select(p, detail, roughness, lacunarity, offset, gain, type, normalize);
138 if (color_is_needed) {
139 *color = make_float3(*value,
141 detail,
142 roughness,
143 lacunarity,
144 offset,
145 gain,
146 type,
147 normalize),
149 detail,
150 roughness,
151 lacunarity,
152 offset,
153 gain,
154 type,
155 normalize));
156 }
157}
158
160 float detail,
161 float roughness,
162 float lacunarity,
163 float offset,
164 float gain,
165 float distortion,
166 int type,
167 bool normalize,
168 bool color_is_needed,
169 ccl_private float *value,
170 ccl_private float3 *color)
171{
172 float3 p = co;
173 if (distortion != 0.0f) {
174 p += make_float3(snoise_3d(p + random_float3_offset(0.0f)) * distortion,
175 snoise_3d(p + random_float3_offset(1.0f)) * distortion,
176 snoise_3d(p + random_float3_offset(2.0f)) * distortion);
177 }
178
179 *value = noise_select(p, detail, roughness, lacunarity, offset, gain, type, normalize);
180 if (color_is_needed) {
181 *color = make_float3(*value,
183 detail,
184 roughness,
185 lacunarity,
186 offset,
187 gain,
188 type,
189 normalize),
191 detail,
192 roughness,
193 lacunarity,
194 offset,
195 gain,
196 type,
197 normalize));
198 }
199}
200
202 float detail,
203 float roughness,
204 float lacunarity,
205 float offset,
206 float gain,
207 float distortion,
208 int type,
209 bool normalize,
210 bool color_is_needed,
211 ccl_private float *value,
212 ccl_private float3 *color)
213{
214 float4 p = co;
215 if (distortion != 0.0f) {
216 p += make_float4(snoise_4d(p + random_float4_offset(0.0f)) * distortion,
217 snoise_4d(p + random_float4_offset(1.0f)) * distortion,
218 snoise_4d(p + random_float4_offset(2.0f)) * distortion,
219 snoise_4d(p + random_float4_offset(3.0f)) * distortion);
220 }
221
222 *value = noise_select(p, detail, roughness, lacunarity, offset, gain, type, normalize);
223 if (color_is_needed) {
224 *color = make_float3(*value,
226 detail,
227 roughness,
228 lacunarity,
229 offset,
230 gain,
231 type,
232 normalize),
234 detail,
235 roughness,
236 lacunarity,
237 offset,
238 gain,
239 type,
240 normalize));
241 }
242}
243
246 ccl_private float *stack,
247 uint offsets1,
248 uint offsets2,
249 uint offsets3,
250 int node_offset)
251{
252 uint vector_stack_offset, w_stack_offset, scale_stack_offset, detail_stack_offset;
253 uint roughness_stack_offset, lacunarity_stack_offset, offset_stack_offset, gain_stack_offset;
254 uint distortion_stack_offset, value_stack_offset, color_stack_offset;
255
257 offsets1, &vector_stack_offset, &w_stack_offset, &scale_stack_offset, &detail_stack_offset);
258 svm_unpack_node_uchar4(offsets2,
259 &roughness_stack_offset,
260 &lacunarity_stack_offset,
261 &offset_stack_offset,
262 &gain_stack_offset);
264 offsets3, &distortion_stack_offset, &value_stack_offset, &color_stack_offset);
265
266 uint4 defaults1 = read_node(kg, &node_offset);
267 uint4 defaults2 = read_node(kg, &node_offset);
268 uint4 properties = read_node(kg, &node_offset);
269
270 uint dimensions = properties.x;
271 uint type = properties.y;
272 uint normalize = properties.z;
273
274 float3 vector = stack_load_float3(stack, vector_stack_offset);
275 float w = stack_load_float_default(stack, w_stack_offset, defaults1.x);
276 float scale = stack_load_float_default(stack, scale_stack_offset, defaults1.y);
277 float detail = stack_load_float_default(stack, detail_stack_offset, defaults1.z);
278 float roughness = stack_load_float_default(stack, roughness_stack_offset, defaults1.w);
279 float lacunarity = stack_load_float_default(stack, lacunarity_stack_offset, defaults2.x);
280 float offset = stack_load_float_default(stack, offset_stack_offset, defaults2.y);
281 float gain = stack_load_float_default(stack, gain_stack_offset, defaults2.z);
282 float distortion = stack_load_float_default(stack, distortion_stack_offset, defaults2.w);
283
284 detail = clamp(detail, 0.0f, 15.0f);
285 roughness = fmaxf(roughness, 0.0f);
286
287 vector *= scale;
288 w *= scale;
289
290 float value;
292 switch (dimensions) {
293 case 1:
295 detail,
296 roughness,
297 lacunarity,
298 offset,
299 gain,
300 distortion,
301 type,
302 normalize,
303 stack_valid(color_stack_offset),
304 &value,
305 &color);
306 break;
307 case 2:
309 detail,
310 roughness,
311 lacunarity,
312 offset,
313 gain,
314 distortion,
315 type,
316 normalize,
317 stack_valid(color_stack_offset),
318 &value,
319 &color);
320 break;
321 case 3:
323 detail,
324 roughness,
325 lacunarity,
326 offset,
327 gain,
328 distortion,
329 type,
330 normalize,
331 stack_valid(color_stack_offset),
332 &value,
333 &color);
334 break;
335 case 4:
337 detail,
338 roughness,
339 lacunarity,
340 offset,
341 gain,
342 distortion,
343 type,
344 normalize,
345 stack_valid(color_stack_offset),
346 &value,
347 &color);
348 break;
349 default:
350 kernel_assert(0);
351 }
352
353 if (stack_valid(value_stack_offset)) {
354 stack_store_float(stack, value_stack_offset, value);
355 }
356 if (stack_valid(color_stack_offset)) {
357 stack_store_float3(stack, color_stack_offset, color);
358 }
359 return node_offset;
360}
361
unsigned int uint
Group Output data from inside of a node group A color picker Mix two input colors RGB to Convert a color s luminance to a grayscale value Generate a normal vector and a dot product Brightness Control the brightness and contrast of the input color Vector Map input vector components with curves Camera Retrieve information about the camera and how it relates to the current shading point s position Clamp a value between a minimum and a maximum Vector Perform vector math operation Invert Invert a color
SIMD_FORCE_INLINE const btScalar & w() const
Return the w value.
Definition btQuadWord.h:119
static unsigned long seed
Definition btSoftBody.h:39
SIMD_FORCE_INLINE btVector3 & normalize()
Normalize this vector x^2 + y^2 + z^2 = 1.
Definition btVector3.h:303
#define kernel_assert(cond)
const KernelGlobalsCPU *ccl_restrict KernelGlobals
#define ccl_device
#define ccl_private
#define ccl_device_inline
#define ccl_device_noinline
#define CCL_NAMESPACE_END
ccl_device_forceinline float4 make_float4(const float x, const float y, const float z, const float w)
ccl_device_forceinline float3 make_float3(const float x, const float y, const float z)
#define fmaxf(x, y)
ccl_device_forceinline float2 make_float2(const float x, const float y)
ccl_device_noinline float noise_multi_fractal(float p, float detail, float roughness, float lacunarity)
ccl_device_noinline float noise_ridged_multi_fractal(float p, float detail, float roughness, float lacunarity, float offset, float gain)
ccl_device_noinline float noise_hetero_terrain(float p, float detail, float roughness, float lacunarity, float offset)
ccl_device_noinline float noise_hybrid_multi_fractal(float p, float detail, float roughness, float lacunarity, float offset, float gain)
CCL_NAMESPACE_BEGIN ccl_device_noinline float noise_fbm(float p, float detail, float roughness, float lacunarity, bool normalize)
ccl_device_inline float hash_float2_to_float(float2 k)
Definition hash.h:163
ccl_device_inline void stack_store_float3(ccl_private float *stack, uint a, float3 f)
CCL_NAMESPACE_BEGIN ccl_device_inline float3 stack_load_float3(ccl_private float *stack, uint a)
ccl_device_inline uint4 read_node(KernelGlobals kg, ccl_private int *offset)
ccl_device_forceinline void svm_unpack_node_uchar3(uint i, ccl_private uint *x, ccl_private uint *y, ccl_private uint *z)
ccl_device_inline float stack_load_float_default(ccl_private float *stack, uint a, uint value)
ccl_device_inline void stack_store_float(ccl_private float *stack, uint a, float f)
ccl_device_forceinline void svm_unpack_node_uchar4(uint i, ccl_private uint *x, ccl_private uint *y, ccl_private uint *z, ccl_private uint *w)
ccl_device_inline bool stack_valid(uint a)
NodeNoiseType
@ NODE_NOISE_FBM
@ NODE_NOISE_HYBRID_MULTIFRACTAL
@ NODE_NOISE_RIDGED_MULTIFRACTAL
@ NODE_NOISE_HETERO_TERRAIN
@ NODE_NOISE_MULTIFRACTAL
ShaderData
float hash_float_to_float(float k)
Definition node_hash.h:13
ccl_device_inline float snoise_1d(float p)
Definition noise.h:685
ccl_device_inline float snoise_2d(float2 p)
Definition noise.h:701
ccl_device_inline float snoise_3d(float3 p)
Definition noise.h:718
ccl_device_inline float snoise_4d(float4 p)
Definition noise.h:736
ccl_device_inline float4 random_float4_offset(float seed)
Definition noisetex.h:37
CCL_NAMESPACE_BEGIN ccl_device_inline float random_float_offset(float seed)
Definition noisetex.h:19
ccl_device void noise_texture_2d(float2 co, float detail, float roughness, float lacunarity, float offset, float gain, float distortion, int type, bool normalize, bool color_is_needed, ccl_private float *value, ccl_private float3 *color)
Definition noisetex.h:118
ccl_device void noise_texture_1d(float co, float detail, float roughness, float lacunarity, float offset, float gain, float distortion, int type, bool normalize, bool color_is_needed, ccl_private float *value, ccl_private float3 *color)
Definition noisetex.h:78
ccl_device_inline float2 random_float2_offset(float seed)
Definition noisetex.h:24
ccl_device void noise_texture_3d(float3 co, float detail, float roughness, float lacunarity, float offset, float gain, float distortion, int type, bool normalize, bool color_is_needed, ccl_private float *value, ccl_private float3 *color)
Definition noisetex.h:159
ccl_device_inline float3 random_float3_offset(float seed)
Definition noisetex.h:30
ccl_device_noinline int svm_node_tex_noise(KernelGlobals kg, ccl_private ShaderData *sd, ccl_private float *stack, uint offsets1, uint offsets2, uint offsets3, int node_offset)
Definition noisetex.h:244
ccl_device float noise_select(T p, float detail, float roughness, float lacunarity, float offset, float gain, int type, bool normalize)
Definition noisetex.h:46
ccl_device void noise_texture_4d(float4 co, float detail, float roughness, float lacunarity, float offset, float gain, float distortion, int type, bool normalize, bool color_is_needed, ccl_private float *value, ccl_private float3 *color)
Definition noisetex.h:201
uint x
Definition types_uint4.h:15
uint y
Definition types_uint4.h:15
uint z
Definition types_uint4.h:15
uint w
Definition types_uint4.h:15
ccl_device_inline int clamp(int a, int mn, int mx)
Definition util/math.h:379