Blender V5.3
node_shader_tex_noise.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2005 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
9#include "node_shader_util.hh"
10#include "node_util.hh"
11
12#include "BKE_texture.h"
13
14#include "BLI_noise.hh"
15
16#include "NOD_multi_function.hh"
17
18#include "RNA_access.hh"
19
21#include "UI_resources.hh"
22
23namespace blender {
24
26
28
30{
31 b.is_function_node();
32
33 const int dimensions = b.node_or_null() ? node_storage(*b.node_or_null()).dimensions : 3;
34 const bool is_compositor = b.tree_or_null() && b.tree_or_null()->type == NTREE_COMPOSIT;
35 const NodeDefaultInputType default_input_type =
38 b.add_input<decl::Vector>("Vector"_ustr)
39 .default_input_type(default_input_type)
40 .available(dimensions != 1);
41
42 b.add_input<decl::Float>("W"_ustr)
43 .min(-1000.0f)
44 .max(1000.0f)
45 .available(ELEM(dimensions, 1, 4))
46 .make_available([](bNode &node) {
47 /* Default to 1 instead of 4, because it is much faster. */
48 node_storage(node).dimensions = 1;
49 });
50
51 b.add_input<decl::Float>("Scale"_ustr)
52 .min(-1000.0f)
53 .max(1000.0f)
54 .default_value(5.0f)
55 .description("Scale of the base noise octave");
56 b.add_input<decl::Float>("Detail"_ustr)
57 .min(0.0f)
58 .max(15.0f)
59 .default_value(2.0f)
60 .description(
61 "The number of noise octaves. Higher values give more detailed noise but increase "
62 "render "
63 "time");
64 b.add_input<decl::Float>("Roughness"_ustr)
65 .min(0.0f)
66 .max(1.0f)
67 .default_value(0.5f)
68 .subtype(PROP_FACTOR)
69 .description(
70 "Blend factor between an octave and its previous one. A value of zero corresponds to "
71 "zero detail");
72 b.add_input<decl::Float>("Lacunarity"_ustr)
73 .min(0.0f)
74 .max(1000.0f)
75 .default_value(2.0f)
76 .description(
77 "The difference between the scale of each two consecutive octaves. Larger values "
78 "corresponds to larger scale for higher octaves");
79
81 b.node_or_null() ? eNodeNoiseTexture_Type(node_storage(*b.node_or_null()).type) :
83 b.add_input<decl::Float>("Offset"_ustr)
84 .min(-1000.0f)
85 .max(1000.0f)
86 .default_value(0.0f)
87 .available(!ELEM(noise_type, SHD_NOISE_MULTIFRACTAL, SHD_NOISE_FBM))
88 .make_available([](bNode &node) { node_storage(node).type = SHD_NOISE_RIDGED_MULTIFRACTAL; })
89 .description(
90 "An added offset to each octave, determines the level where the highest octave will "
91 "appear");
92 b.add_input<decl::Float>("Gain"_ustr)
93 .min(0.0f)
94 .max(1000.0f)
95 .default_value(1.0f)
97 .make_available([](bNode &node) { node_storage(node).type = SHD_NOISE_RIDGED_MULTIFRACTAL; })
98 .description("An extra multiplier to tune the magnitude of octaves");
99 b.add_input<decl::Float>("Distortion"_ustr)
100 .min(-1000.0f)
101 .max(1000.0f)
102 .default_value(0.0f)
103 .description("Amount of distortion");
104 b.add_output<decl::Float>("Factor"_ustr, "Fac"_ustr).no_muted_links();
105 b.add_output<decl::Color>("Color"_ustr).no_muted_links();
106}
107
108static void node_shader_buts_tex_noise(ui::Layout &layout, bContext * /*C*/, PointerRNA *ptr)
109{
110 layout.prop(ptr, "noise_dimensions", ui::ITEM_R_SPLIT_EMPTY_NAME, "", ICON_NONE);
111 layout.prop(ptr, "noise_type", ui::ITEM_R_SPLIT_EMPTY_NAME, "", ICON_NONE);
112 if (ELEM(RNA_enum_get(ptr, "noise_type"), SHD_NOISE_FBM)) {
113 layout.prop(ptr, "normalize", ui::ITEM_R_SPLIT_EMPTY_NAME, std::nullopt, ICON_NONE);
114 }
115}
116
118{
119 NodeTexNoise *tex = MEM_new<NodeTexNoise>(__func__);
122 tex->dimensions = node_tree->type == NTREE_COMPOSIT ? 2 : 3;
123 tex->type = SHD_NOISE_FBM;
124 tex->normalize = true;
125
126 node->storage = tex;
127}
128
129static const char *gpu_shader_get_name(const int dimensions, const int type)
130{
131 BLI_assert(dimensions > 0 && dimensions < 5);
132 BLI_assert(type >= 0 && type < 5);
133
134 switch (type) {
136 return std::array{"node_noise_tex_multi_fractal_1d",
137 "node_noise_tex_multi_fractal_2d",
138 "node_noise_tex_multi_fractal_3d",
139 "node_noise_tex_multi_fractal_4d"}[dimensions - 1];
140 case SHD_NOISE_FBM:
141 return std::array{"node_noise_tex_fbm_1d",
142 "node_noise_tex_fbm_2d",
143 "node_noise_tex_fbm_3d",
144 "node_noise_tex_fbm_4d"}[dimensions - 1];
146 return std::array{"node_noise_tex_hybrid_multi_fractal_1d",
147 "node_noise_tex_hybrid_multi_fractal_2d",
148 "node_noise_tex_hybrid_multi_fractal_3d",
149 "node_noise_tex_hybrid_multi_fractal_4d"}[dimensions - 1];
151 return std::array{"node_noise_tex_ridged_multi_fractal_1d",
152 "node_noise_tex_ridged_multi_fractal_2d",
153 "node_noise_tex_ridged_multi_fractal_3d",
154 "node_noise_tex_ridged_multi_fractal_4d"}[dimensions - 1];
156 return std::array{"node_noise_tex_hetero_terrain_1d",
157 "node_noise_tex_hetero_terrain_2d",
158 "node_noise_tex_hetero_terrain_3d",
159 "node_noise_tex_hetero_terrain_4d"}[dimensions - 1];
160 }
161 return nullptr;
162}
163
165 bNode *node,
166 bNodeExecData * /*execdata*/,
169{
172
173 const NodeTexNoise &storage = node_storage(*node);
174 float normalize = storage.normalize;
175 float compute_color = out[1].hasoutput;
176
177 const char *name = gpu_shader_get_name(storage.dimensions, storage.type);
178 return GPU_stack_link(
179 mat, node, name, in, out, GPU_constant(&normalize), GPU_constant(&compute_color));
180}
181
182class NoiseFunction : public mf::MultiFunction {
183 private:
184 int dimensions_;
185 int type_;
186 bool normalize_;
187
188 public:
222
223 static mf::Signature create_signature(int dimensions, int type)
224 {
225 mf::Signature signature;
226 mf::SignatureBuilder builder{"Noise", signature};
227
228 if (ELEM(dimensions, 2, 3, 4)) {
229 builder.single_input<float3>("Vector");
230 }
231 if (ELEM(dimensions, 1, 4)) {
232 builder.single_input<float>("W");
233 }
234
235 builder.single_input<float>("Scale");
236 builder.single_input<float>("Detail");
237 builder.single_input<float>("Roughness");
238 builder.single_input<float>("Lacunarity");
239 if (ELEM(type,
243 {
244 builder.single_input<float>("Offset");
245 }
247 builder.single_input<float>("Gain");
248 }
249 builder.single_input<float>("Distortion");
250
251 builder.single_output<float>("Fac", mf::ParamFlag::SupportsUnusedOutput);
252 builder.single_output<ColorGeometry4f>("Color", mf::ParamFlag::SupportsUnusedOutput);
253
254 return signature;
255 }
256
257 void call(const IndexMask &mask, mf::Params params, mf::Context /*context*/) const override
258 {
259 int param = ELEM(dimensions_, 2, 3, 4) + ELEM(dimensions_, 1, 4);
260 const VArray<float> &scale = params.readonly_single_input<float>(param++, "Scale");
261 const VArray<float> &detail = params.readonly_single_input<float>(param++, "Detail");
262 const VArray<float> &roughness = params.readonly_single_input<float>(param++, "Roughness");
263 const VArray<float> &lacunarity = params.readonly_single_input<float>(param++, "Lacunarity");
264 /* Initialize to any other variable when unused to avoid unnecessary conditionals. */
265 const VArray<float> &offset = ELEM(type_,
269 params.readonly_single_input<float>(param++, "Offset") :
270 scale;
271 /* Initialize to any other variable when unused to avoid unnecessary conditionals. */
272 const VArray<float> &gain = ELEM(type_,
275 params.readonly_single_input<float>(param++, "Gain") :
276 scale;
277 const VArray<float> &distortion = params.readonly_single_input<float>(param++, "Distortion");
278
279 MutableSpan<float> r_factor = params.uninitialized_single_output_if_required<float>(param++,
280 "Fac");
282 params.uninitialized_single_output_if_required<ColorGeometry4f>(param++, "Color");
283
284 const bool compute_factor = !r_factor.is_empty();
285 const bool compute_color = !r_color.is_empty();
286
287 switch (dimensions_) {
288 case 1: {
289 const VArray<float> &w = params.readonly_single_input<float>(0, "W");
290 if (compute_color) {
291 mask.foreach_index([&](const int64_t i) {
292 const float position = w[i] * scale[i];
294 position,
295 math::clamp(detail[i], 0.0f, 15.0f),
296 math::max(roughness[i], 0.0f),
297 lacunarity[i],
298 offset[i],
299 gain[i],
300 distortion[i],
301 type_,
302 normalize_);
303 r_color[i] = ColorGeometry4f(c[0], c[1], c[2], 1.0f);
304 if (compute_factor) {
305 r_factor[i] = c[0];
306 }
307 });
308 }
309 else if (compute_factor) {
310 mask.foreach_index([&](const int64_t i) {
311 const float position = w[i] * scale[i];
313 math::clamp(detail[i], 0.0f, 15.0f),
314 math::max(roughness[i], 0.0f),
315 lacunarity[i],
316 offset[i],
317 gain[i],
318 distortion[i],
319 type_,
320 normalize_);
321 });
322 }
323 break;
324 }
325 case 2: {
326 const VArray<float3> &vector = params.readonly_single_input<float3>(0, "Vector");
327 if (compute_color) {
328 mask.foreach_index([&](const int64_t i) {
329 const float2 position = float2(vector[i] * scale[i]);
331 position,
332 math::clamp(detail[i], 0.0f, 15.0f),
333 math::max(roughness[i], 0.0f),
334 lacunarity[i],
335 offset[i],
336 gain[i],
337 distortion[i],
338 type_,
339 normalize_);
340 r_color[i] = ColorGeometry4f(c[0], c[1], c[2], 1.0f);
341 if (compute_factor) {
342 r_factor[i] = c[0];
343 }
344 });
345 }
346 else if (compute_factor) {
347 mask.foreach_index([&](const int64_t i) {
348 const float2 position = float2(vector[i] * scale[i]);
350 math::clamp(detail[i], 0.0f, 15.0f),
351 math::max(roughness[i], 0.0f),
352 lacunarity[i],
353 offset[i],
354 gain[i],
355 distortion[i],
356 type_,
357 normalize_);
358 });
359 }
360 break;
361 }
362 case 3: {
363 const VArray<float3> &vector = params.readonly_single_input<float3>(0, "Vector");
364 if (compute_color) {
365 mask.foreach_index([&](const int64_t i) {
366 const float3 position = vector[i] * scale[i];
368 position,
369 math::clamp(detail[i], 0.0f, 15.0f),
370 math::max(roughness[i], 0.0f),
371 lacunarity[i],
372 offset[i],
373 gain[i],
374 distortion[i],
375 type_,
376 normalize_);
377 r_color[i] = ColorGeometry4f(c[0], c[1], c[2], 1.0f);
378 if (compute_factor) {
379 r_factor[i] = c[0];
380 }
381 });
382 }
383 else if (compute_factor) {
384 mask.foreach_index([&](const int64_t i) {
385 const float3 position = vector[i] * scale[i];
387 math::clamp(detail[i], 0.0f, 15.0f),
388 math::max(roughness[i], 0.0f),
389 lacunarity[i],
390 offset[i],
391 gain[i],
392 distortion[i],
393 type_,
394 normalize_);
395 });
396 }
397 break;
398 }
399 case 4: {
400 const VArray<float3> &vector = params.readonly_single_input<float3>(0, "Vector");
401 const VArray<float> &w = params.readonly_single_input<float>(1, "W");
402 if (compute_color) {
403 mask.foreach_index([&](const int64_t i) {
404 const float3 position_vector = vector[i] * scale[i];
405 const float position_w = w[i] * scale[i];
406 const float4 position{
407 position_vector[0], position_vector[1], position_vector[2], position_w};
409 position,
410 math::clamp(detail[i], 0.0f, 15.0f),
411 math::max(roughness[i], 0.0f),
412 lacunarity[i],
413 offset[i],
414 gain[i],
415 distortion[i],
416 type_,
417 normalize_);
418 r_color[i] = ColorGeometry4f(c[0], c[1], c[2], 1.0f);
419 if (compute_factor) {
420 r_factor[i] = c[0];
421 }
422 });
423 }
424 else if (compute_factor) {
425 mask.foreach_index([&](const int64_t i) {
426 const float3 position_vector = vector[i] * scale[i];
427 const float position_w = w[i] * scale[i];
428 const float4 position{
429 position_vector[0], position_vector[1], position_vector[2], position_w};
431 math::clamp(detail[i], 0.0f, 15.0f),
432 math::max(roughness[i], 0.0f),
433 lacunarity[i],
434 offset[i],
435 gain[i],
436 distortion[i],
437 type_,
438 normalize_);
439 });
440 }
441 break;
442 }
443 }
444 }
445
447 {
448 ExecutionHints hints;
449 hints.allocates_array = false;
450 hints.min_grain_size = 100;
451 return hints;
452 }
453
454 void hash_unique(UniqueHashBytes &hash) const override
455 {
456 static constexpr int8_t id = 0;
457 hash.add(&id);
458 hash.add(dimensions_);
459 hash.add(type_);
460 hash.add(normalize_);
461 }
462};
463
465{
466 const NodeTexNoise &storage = node_storage(builder.node());
468 storage.dimensions, storage.type, storage.normalize);
469}
470
472#ifdef WITH_MATERIALX
473{
474 /* NOTE: Some inputs aren't supported by MaterialX. */
475 NodeItem scale = get_input_value("Scale", NodeItem::Type::Float);
476 NodeItem detail = get_input_default("Detail", NodeItem::Type::Float);
477 NodeItem lacunarity = get_input_value("Lacunarity", NodeItem::Type::Float);
478 /* Empirically, higher octaves lead to NaNs on e.g. Metal and NVIDIA. */
479 const int octaves = int(math::clamp(detail.value->asA<float>(), 1.0f, 13.0f));
480
481 NodeItem position = create_node("position", NodeItem::Type::Vector3);
483
484 return create_node(
485 "fractal3d",
486 STREQ(socket_out_->identifier, "Fac") ? NodeItem::Type::Float : NodeItem::Type::Color3,
487 {{"position", position}, {"octaves", val(octaves)}, {"lacunarity", lacunarity}});
488}
489#endif
491
492} // namespace nodes::node_shader_tex_noise_cc
493
495{
496 namespace file_ns = nodes::node_shader_tex_noise_cc;
497
498 static bke::bNodeType ntype;
499
500 common_node_type_base(&ntype, "ShaderNodeTexNoise"_ustr, SH_NODE_TEX_NOISE);
501 ntype.ui_name = "Noise Texture";
502 ntype.ui_description = "Generate fractal Perlin noise";
503 ntype.enum_name_legacy = "TEX_NOISE";
505 ntype.declare = file_ns::sh_node_tex_noise_declare;
506 ntype.draw_buttons = file_ns::node_shader_buts_tex_noise;
507 ntype.initfunc = file_ns::node_shader_init_tex_noise;
510 ntype.gpu_fn = file_ns::node_shader_gpu_tex_noise;
511 ntype.build_multi_function = file_ns::sh_node_noise_build_multi_function;
512 ntype.materialx_fn = file_ns::node_shader_materialx;
514
516}
517
518} // namespace blender
#define NODE_STORAGE_FUNCS(StorageT)
Definition BKE_node.hh:1353
#define NODE_CLASS_TEXTURE
Definition BKE_node.hh:488
#define SH_NODE_TEX_NOISE
#define ELEM(...)
#define STREQ(a, b)
btScalar max
SIMD_FORCE_INLINE const btScalar & w() const
Return the w value.
Definition btQuadWord.h:119
void set_signature(const Signature *signature)
static mf::Signature create_signature(int dimensions, int type)
NoiseFunction(int dimensions, int type, bool normalize)
void call(const IndexMask &mask, mf::Params params, mf::Context) const override
void hash_unique(UniqueHashBytes &hash) const override
#define storage(slot, qualifiers)
#define position
VecBase< float, D > normalize(VecOp< float, D >) RET
float3x3 scale(float3x3 mat, float2 scale)
void node_register_type(bNodeType &ntype)
Definition node.cc:2920
void node_type_storage(bNodeType &ntype, std::optional< StringRefNull > storagename, void(*freefunc)(bNode *node), void(*copyfunc)(bNodeTree *dest_ntree, bNode *dest_node, const bNode *src_node))
Definition node.cc:6166
T clamp(const T &a, const T &min, const T &max)
T max(const T &a, const T &b)
static void node_shader_init_tex_noise(bNodeTree *node_tree, bNode *node)
static const char * gpu_shader_get_name(const int dimensions, const int type)
static void sh_node_noise_build_multi_function(NodeMultiFunctionBuilder &builder)
static int node_shader_gpu_tex_noise(GPUMaterial *mat, bNode *node, bNodeExecData *, GPUNodeStack *in, GPUNodeStack *out)
static void node_shader_buts_tex_noise(ui::Layout &layout, bContext *, PointerRNA *ptr)
static void sh_node_tex_noise_declare(NodeDeclarationBuilder &b)
float3 perlin_float3_fractal_distorted(float position, float detail, float roughness, float lacunarity, float offset, float gain, float distortion, int type, bool normalize)
Definition noise.cc:999
float perlin_fractal_distorted(T position, float detail, float roughness, float lacunarity, float offset, float gain, float distortion, int type, bool normalize)
Definition noise.cc:945
BMesh Iterators.
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
bool GPU_stack_link(GPUMaterial *mat, const bNode *node, const char *name, GPUNodeStack *in, GPUNodeStack *out,...)
__int64 int64_t
Definition BKE_text.h:90
int RNA_enum_get(PointerRNA *ptr, const char *name)
void register_node_type_sh_tex_noise()
@ SHD_NOISE_RIDGED_MULTIFRACTAL
@ SHD_NOISE_HETERO_TERRAIN
@ SHD_NOISE_HYBRID_MULTIFRACTAL
@ SHD_NOISE_MULTIFRACTAL
VecBase< float, 4 > float4
GPUNodeLink * GPU_constant(const float *num)
VecBase< float, 2 > float2
void node_shader_gpu_tex_mapping(GPUMaterial *mat, bNode *node, GPUNodeStack *in, GPUNodeStack *)
void node_shader_gpu_default_tex_coord(GPUMaterial *mat, bNode *node, GPUNodeLink **link)
void BKE_texture_mapping_default(struct TexMapping *texmap, int type)
Definition texture.cc:234
void node_copy_standard_storage(bNodeTree *, bNode *dest_node, const bNode *src_node)
Definition node_util.cc:57
ColorSceneLinear4f< eAlpha::Premultiplied > ColorGeometry4f
void node_free_standard_storage(bNode *node)
Definition node_util.cc:45
VecBase< float, 3 > float3
void BKE_texture_colormapping_default(struct ColorMapping *colormap)
Definition texture.cc:337
@ PROP_FACTOR
Definition RNA_types.hh:308
signed char int8_t
Definition BKE_text.h:76
void common_node_type_base(bke::bNodeType *ntype, UString idname, const std::optional< int16_t > legacy_type)
#define NODE_SHADER_MATERIALX_BEGIN
#define NODE_SHADER_MATERIALX_END
#define hash
Definition noise_c.cc:156
#define min(a, b)
Definition sort.cc:38
eNodeTree_Type type
static constexpr int _160
Definition BKE_node.hh:246
Defines a node type.
Definition BKE_node.hh:265
NodeMaterialXFunction materialx_fn
Definition BKE_node.hh:375
void(*) draw_buttons(ui::Layout &, bContext *C, PointerRNA *ptr)
Definition BKE_node.hh:290
std::string ui_description
Definition BKE_node.hh:271
NodeGPUExecFunction gpu_fn
Definition BKE_node.hh:373
NodeMultiFunctionBuildFunction build_multi_function
Definition BKE_node.hh:382
void(*) initfunc(bNodeTree *ntree, bNode *node)
Definition BKE_node.hh:320
const char * enum_name_legacy
Definition BKE_node.hh:274
NodeDeclareFunction declare
Definition BKE_node.hh:393
void prop(PointerRNA *ptr, PropertyRNA *prop, int index, int value, eUI_Item_Flag flag, std::optional< StringRef > name_opt, int icon, std::optional< StringRef > placeholder=std::nullopt)