Blender V5.3
node_shader_tex_voronoi.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).min(-1000.0f).max(1000.0f).default_value(5.0f);
52
54 b.node_or_null() ? node_storage(*b.node_or_null()).feature : SHD_VORONOI_F1);
55 b.add_input<decl::Float>("Detail"_ustr)
56 .min(0.0f)
57 .max(15.0f)
58 .default_value(0.0f)
59 .available(feature != SHD_VORONOI_N_SPHERE_RADIUS)
60 .make_available([](bNode &node) { node_storage(node).feature = SHD_VORONOI_F1; })
61 .description("The number of Voronoi layers to sum");
62 b.add_input<decl::Float>("Roughness"_ustr)
63 .min(0.0f)
64 .max(1.0f)
65 .default_value(0.5f)
66 .subtype(PROP_FACTOR)
67 .available(feature != SHD_VORONOI_N_SPHERE_RADIUS)
68 .make_available([](bNode &node) { node_storage(node).feature = SHD_VORONOI_F1; })
69 .description("The influence of a Voronoi layer relative to that of the previous layer");
70 b.add_input<decl::Float>("Lacunarity"_ustr)
71 .min(0.0f)
72 .max(1000.0f)
73 .default_value(2.0f)
74 .available(feature != SHD_VORONOI_N_SPHERE_RADIUS)
75 .make_available([](bNode &node) { node_storage(node).feature = SHD_VORONOI_F1; })
76 .description("The scale of a Voronoi layer relative to that of the previous layer");
77
78 b.add_input<decl::Float>("Smoothness"_ustr)
79 .min(0.0f)
80 .max(1.0f)
81 .default_value(1.0f)
82 .subtype(PROP_FACTOR)
83 .available(feature == SHD_VORONOI_SMOOTH_F1)
84 .make_available([](bNode &node) { node_storage(node).feature = SHD_VORONOI_SMOOTH_F1; });
85
86 const eNodeVoronoi_Dist distance_metric = eNodeVoronoi_Dist(
87 b.node_or_null() ? node_storage(*b.node_or_null()).distance : SHD_VORONOI_EUCLIDEAN);
88 b.add_input<decl::Float>("Exponent"_ustr)
89 .min(0.0f)
90 .max(32.0f)
91 .default_value(0.5f)
92 .available(distance_metric == SHD_VORONOI_MINKOWSKI && dimensions != 1 &&
94 .make_available([](bNode &node) { node_storage(node).distance = SHD_VORONOI_MINKOWSKI; });
95
96 b.add_input<decl::Float>("Randomness"_ustr)
97 .min(0.0f)
98 .max(1.0f)
99 .default_value(1.0f)
100 .subtype(PROP_FACTOR);
101
102 b.add_output<decl::Float>("Distance"_ustr)
103 .no_muted_links()
104 .available(feature != SHD_VORONOI_N_SPHERE_RADIUS);
105 b.add_output<decl::Color>("Color"_ustr)
106 .no_muted_links()
108 b.add_output<decl::Vector>("Position"_ustr)
109 .no_muted_links()
110 .available(feature != SHD_VORONOI_DISTANCE_TO_EDGE &&
111 feature != SHD_VORONOI_N_SPHERE_RADIUS && dimensions != 1);
112 b.add_output<decl::Float>("W"_ustr)
113 .no_muted_links()
114 .available(feature != SHD_VORONOI_DISTANCE_TO_EDGE &&
115 feature != SHD_VORONOI_N_SPHERE_RADIUS && ELEM(dimensions, 1, 4))
117 /* Default to 1 instead of 4, because it is much faster. */
118 node_storage(node).dimensions = 1;
119 });
120 b.add_output<decl::Float>("Radius"_ustr)
121 .no_muted_links()
122 .available(feature == SHD_VORONOI_N_SPHERE_RADIUS)
124 [](bNode &node) { node_storage(node).feature = SHD_VORONOI_N_SPHERE_RADIUS; });
125}
126
128{
129 layout.prop(ptr, "voronoi_dimensions", ui::ITEM_R_SPLIT_EMPTY_NAME, "", ICON_NONE);
130 layout.prop(ptr, "feature", ui::ITEM_R_SPLIT_EMPTY_NAME, "", ICON_NONE);
131 int feature = RNA_enum_get(ptr, "feature");
133 RNA_enum_get(ptr, "voronoi_dimensions") != 1)
134 {
135 layout.prop(ptr, "distance", ui::ITEM_R_SPLIT_EMPTY_NAME, "", ICON_NONE);
136 }
137 if (!ELEM(feature, SHD_VORONOI_N_SPHERE_RADIUS)) {
138 layout.prop(ptr, "normalize", ui::ITEM_R_SPLIT_EMPTY_NAME, std::nullopt, ICON_NONE);
139 }
140}
141
143{
144 NodeTexVoronoi *tex = MEM_new<NodeTexVoronoi>(__func__);
147 tex->dimensions = node_tree->type == NTREE_COMPOSIT ? 2 : 3;
149 tex->feature = SHD_VORONOI_F1;
150 tex->normalize = false;
151
152 node->storage = tex;
153}
154
155static const char *gpu_shader_get_name(const int feature, const int dimensions)
156{
157 BLI_assert(feature >= 0 && feature < 5);
158 BLI_assert(dimensions > 0 && dimensions < 5);
159
160 switch (feature) {
161 case SHD_VORONOI_F1:
162 return std::array{
163 "node_tex_voronoi_f1_1d",
164 "node_tex_voronoi_f1_2d",
165 "node_tex_voronoi_f1_3d",
166 "node_tex_voronoi_f1_4d",
167 }[dimensions - 1];
168 case SHD_VORONOI_F2:
169 return std::array{
170 "node_tex_voronoi_f2_1d",
171 "node_tex_voronoi_f2_2d",
172 "node_tex_voronoi_f2_3d",
173 "node_tex_voronoi_f2_4d",
174 }[dimensions - 1];
176 return std::array{
177 "node_tex_voronoi_smooth_f1_1d",
178 "node_tex_voronoi_smooth_f1_2d",
179 "node_tex_voronoi_smooth_f1_3d",
180 "node_tex_voronoi_smooth_f1_4d",
181 }[dimensions - 1];
183 return std::array{
184 "node_tex_voronoi_distance_to_edge_1d",
185 "node_tex_voronoi_distance_to_edge_2d",
186 "node_tex_voronoi_distance_to_edge_3d",
187 "node_tex_voronoi_distance_to_edge_4d",
188 }[dimensions - 1];
190 return std::array{
191 "node_tex_voronoi_n_sphere_radius_1d",
192 "node_tex_voronoi_n_sphere_radius_2d",
193 "node_tex_voronoi_n_sphere_radius_3d",
194 "node_tex_voronoi_n_sphere_radius_4d",
195 }[dimensions - 1];
196 }
197 return nullptr;
198}
199
201 bNode *node,
202 bNodeExecData * /*execdata*/,
205{
208
209 NodeTexVoronoi *tex = static_cast<NodeTexVoronoi *>(node->storage);
210 float metric = tex->distance;
211 float normalize = tex->normalize;
212
213 const char *name = gpu_shader_get_name(tex->feature, tex->dimensions);
214
215 return GPU_stack_link(mat, node, name, in, out, GPU_constant(&metric), GPU_constant(&normalize));
216}
217
218static mf::MultiFunction::ExecutionHints voronoi_execution_hints{50, false};
219
220class VoronoiMetricFunction : public mf::MultiFunction {
221 private:
222 int dimensions_;
223 int feature_;
224 int metric_;
225 bool normalize_;
226
227 public:
228 VoronoiMetricFunction(int dimensions, int feature, int metric, bool normalize)
229 : dimensions_(dimensions), feature_(feature), metric_(metric), normalize_(normalize)
230 {
231 BLI_assert(dimensions >= 1 && dimensions <= 4);
232 BLI_assert(feature >= 0 && feature <= 4);
233 if (ELEM(metric_, SHD_VORONOI_MINKOWSKI)) {
234 static std::array<mf::Signature, 12> signatures{
239
244
249 };
250 this->set_signature(&signatures[dimensions + feature * 4 - 1]);
251 }
252 else {
253 static std::array<mf::Signature, 12> signatures{
258
263
268 };
269 this->set_signature(&signatures[dimensions + feature * 4 - 1]);
270 }
271 }
272
273 static mf::Signature create_signature(int dimensions, int feature, int metric)
274 {
275 mf::Signature signature;
276 mf::SignatureBuilder builder{"voronoi_metric", signature};
277
278 if (ELEM(dimensions, 2, 3, 4)) {
279 builder.single_input<float3>("Vector");
280 }
281 if (ELEM(dimensions, 1, 4)) {
282 builder.single_input<float>("W");
283 }
284 builder.single_input<float>("Scale");
285 builder.single_input<float>("Detail");
286 builder.single_input<float>("Roughness");
287 builder.single_input<float>("Lacunarity");
288 if (feature == SHD_VORONOI_SMOOTH_F1) {
289 builder.single_input<float>("Smoothness");
290 }
291 if ((dimensions != 1) && (metric == SHD_VORONOI_MINKOWSKI)) {
292 builder.single_input<float>("Exponent");
293 }
294 builder.single_input<float>("Randomness");
295
296 builder.single_output<float>("Distance", mf::ParamFlag::SupportsUnusedOutput);
297 builder.single_output<ColorGeometry4f>("Color", mf::ParamFlag::SupportsUnusedOutput);
298 if (dimensions != 1) {
299 builder.single_output<float3>("Position", mf::ParamFlag::SupportsUnusedOutput);
300 }
301 if (ELEM(dimensions, 1, 4)) {
302 builder.single_output<float>("W", mf::ParamFlag::SupportsUnusedOutput);
303 }
304
305 return signature;
306 }
307
308 void call(const IndexMask &mask, mf::Params mf_params, mf::Context /*context*/) const override
309 {
310 auto get_vector = [&](int param_index) -> VArray<float3> {
311 return mf_params.readonly_single_input<float3>(param_index, "Vector");
312 };
313 auto get_w = [&](int param_index) -> VArray<float> {
314 return mf_params.readonly_single_input<float>(param_index, "W");
315 };
316 auto get_scale = [&](int param_index) -> VArray<float> {
317 return mf_params.readonly_single_input<float>(param_index, "Scale");
318 };
319 auto get_detail = [&](int param_index) -> VArray<float> {
320 return mf_params.readonly_single_input<float>(param_index, "Detail");
321 };
322 auto get_roughness = [&](int param_index) -> VArray<float> {
323 return mf_params.readonly_single_input<float>(param_index, "Roughness");
324 };
325 auto get_lacunarity = [&](int param_index) -> VArray<float> {
326 return mf_params.readonly_single_input<float>(param_index, "Lacunarity");
327 };
328 auto get_smoothness = [&](int param_index) -> VArray<float> {
329 return mf_params.readonly_single_input<float>(param_index, "Smoothness");
330 };
331 auto get_exponent = [&](int param_index) -> VArray<float> {
332 return mf_params.readonly_single_input<float>(param_index, "Exponent");
333 };
334 auto get_randomness = [&](int param_index) -> VArray<float> {
335 return mf_params.readonly_single_input<float>(param_index, "Randomness");
336 };
337 auto get_r_distance = [&](int param_index) -> MutableSpan<float> {
338 return mf_params.uninitialized_single_output_if_required<float>(param_index, "Distance");
339 };
340 auto get_r_color = [&](int param_index) -> MutableSpan<ColorGeometry4f> {
341 return mf_params.uninitialized_single_output_if_required<ColorGeometry4f>(param_index,
342 "Color");
343 };
344 auto get_r_position = [&](int param_index) -> MutableSpan<float3> {
345 return mf_params.uninitialized_single_output_if_required<float3>(param_index, "Position");
346 };
347 auto get_r_w = [&](int param_index) -> MutableSpan<float> {
348 return mf_params.uninitialized_single_output_if_required<float>(param_index, "W");
349 };
350
351 int param = 0;
352
353 const VArray<float3> &vector = !ELEM(dimensions_, 1) ? get_vector(param++) : VArray<float3>{};
354 const VArray<float> &w = ELEM(dimensions_, 1, 4) ? get_w(param++) : VArray<float>{};
355 const VArray<float> &scale = get_scale(param++);
356 const VArray<float> &detail = get_detail(param++);
357 const VArray<float> &roughness = get_roughness(param++);
358 const VArray<float> &lacunarity = get_lacunarity(param++);
359 const VArray<float> &smoothness = ELEM(feature_, SHD_VORONOI_SMOOTH_F1) ?
360 get_smoothness(param++) :
362 const VArray<float> &exponent = ELEM(metric_, SHD_VORONOI_MINKOWSKI) && !ELEM(dimensions_, 1) ?
363 get_exponent(param++) :
365 const VArray<float> &randomness = get_randomness(param++);
366 MutableSpan<float> r_distance = get_r_distance(param++);
367 MutableSpan<ColorGeometry4f> r_color = get_r_color(param++);
368 MutableSpan<float3> r_position = !ELEM(dimensions_, 1) ? get_r_position(param++) :
370 MutableSpan<float> r_w = ELEM(dimensions_, 1, 4) ? get_r_w(param++) : MutableSpan<float>{};
371 const bool calc_distance = !r_distance.is_empty();
372 const bool calc_color = !r_color.is_empty();
373 const bool calc_position = !r_position.is_empty();
374 const bool calc_w = !r_w.is_empty();
375
377 params.feature = feature_;
378 params.metric = metric_;
379 params.normalize = normalize_;
380
382 switch (dimensions_) {
383 case 1: {
384 mask.foreach_index([&](const int64_t i) {
385 params.scale = scale[i];
386 params.detail = detail[i];
387 params.roughness = roughness[i];
388 params.lacunarity = lacunarity[i];
389 params.smoothness = ELEM(feature_, SHD_VORONOI_SMOOTH_F1) ?
390 std::min(std::max(smoothness[i] / 2.0f, 0.0f), 0.5f) :
391 0.0f;
392 params.exponent = 0.0f;
393 params.randomness = std::min(std::max(randomness[i], 0.0f), 1.0f);
394 params.max_distance = (0.5f + 0.5f * params.randomness) *
395 ((params.feature == SHD_VORONOI_F2) ? 2.0f : 1.0f);
396
397 output = noise::fractal_voronoi_x_fx<float>(params, w[i] * params.scale, calc_color);
398 if (calc_distance) {
399 r_distance[i] = output.distance;
400 }
401 if (calc_color) {
402 r_color[i] = ColorGeometry4f(output.color.x, output.color.y, output.color.z, 1.0f);
403 }
404 if (calc_w) {
405 r_w[i] = output.position.w;
406 }
407 });
408 break;
409 }
410 case 2: {
411 mask.foreach_index([&](const int64_t i) {
412 params.scale = scale[i];
413 params.detail = detail[i];
414 params.roughness = roughness[i];
415 params.lacunarity = lacunarity[i];
416 params.smoothness = ELEM(feature_, SHD_VORONOI_SMOOTH_F1) ?
417 std::min(std::max(smoothness[i] / 2.0f, 0.0f), 0.5f) :
418 0.0f;
419 params.exponent = ELEM(metric_, SHD_VORONOI_MINKOWSKI) && !ELEM(dimensions_, 1) ?
420 exponent[i] :
421 0.0f;
422 params.randomness = std::min(std::max(randomness[i], 0.0f), 1.0f);
423 params.max_distance = noise::voronoi_distance(float2{0.0f, 0.0f},
424 float2(0.5f + 0.5f * params.randomness,
425 0.5f + 0.5f * params.randomness),
426 params) *
427 ((params.feature == SHD_VORONOI_F2) ? 2.0f : 1.0f);
428
430 params, float2{vector[i].x, vector[i].y} * params.scale, calc_color);
431 if (calc_distance) {
432 r_distance[i] = output.distance;
433 }
434 if (calc_color) {
435 r_color[i] = ColorGeometry4f(output.color.x, output.color.y, output.color.z, 1.0f);
436 }
437 if (calc_position) {
438 r_position[i] = float3{output.position.x, output.position.y, 0.0f};
439 }
440 });
441 break;
442 }
443 case 3: {
444 mask.foreach_index([&](const int64_t i) {
445 params.scale = scale[i];
446 params.detail = detail[i];
447 params.roughness = roughness[i];
448 params.lacunarity = lacunarity[i];
449 params.smoothness = ELEM(feature_, SHD_VORONOI_SMOOTH_F1) ?
450 std::min(std::max(smoothness[i] / 2.0f, 0.0f), 0.5f) :
451 0.0f;
452 params.exponent = ELEM(metric_, SHD_VORONOI_MINKOWSKI) && !ELEM(dimensions_, 1) ?
453 exponent[i] :
454 0.0f;
455 params.randomness = std::min(std::max(randomness[i], 0.0f), 1.0f);
456 params.max_distance = noise::voronoi_distance(float3{0.0f, 0.0f, 0.0f},
457 float3(0.5f + 0.5f * params.randomness,
458 0.5f + 0.5f * params.randomness,
459 0.5f + 0.5f * params.randomness),
460 params) *
461 ((params.feature == SHD_VORONOI_F2) ? 2.0f : 1.0f);
462
464 params, vector[i] * params.scale, calc_color);
465 if (calc_distance) {
466 r_distance[i] = output.distance;
467 }
468 if (calc_color) {
469 r_color[i] = ColorGeometry4f(output.color.x, output.color.y, output.color.z, 1.0f);
470 }
471 if (calc_position) {
472 r_position[i] = float3{output.position.x, output.position.y, output.position.z};
473 }
474 });
475 break;
476 }
477 case 4: {
478 mask.foreach_index([&](const int64_t i) {
479 params.scale = scale[i];
480 params.detail = detail[i];
481 params.roughness = roughness[i];
482 params.lacunarity = lacunarity[i];
483 params.smoothness = ELEM(feature_, SHD_VORONOI_SMOOTH_F1) ?
484 std::min(std::max(smoothness[i] / 2.0f, 0.0f), 0.5f) :
485 0.0f;
486 params.exponent = ELEM(metric_, SHD_VORONOI_MINKOWSKI) && !ELEM(dimensions_, 1) ?
487 exponent[i] :
488 0.0f;
489 params.randomness = std::min(std::max(randomness[i], 0.0f), 1.0f);
490 params.max_distance = noise::voronoi_distance(float4{0.0f, 0.0f, 0.0f, 0.0f},
491 float4(0.5f + 0.5f * params.randomness,
492 0.5f + 0.5f * params.randomness,
493 0.5f + 0.5f * params.randomness,
494 0.5f + 0.5f * params.randomness),
495 params) *
496 ((params.feature == SHD_VORONOI_F2) ? 2.0f : 1.0f);
497
499 params,
500 float4{vector[i].x, vector[i].y, vector[i].z, w[i]} * params.scale,
501 calc_color);
502 if (calc_distance) {
503 r_distance[i] = output.distance;
504 }
505 if (calc_color) {
506 r_color[i] = ColorGeometry4f(output.color.x, output.color.y, output.color.z, 1.0f);
507 }
508 if (calc_position) {
509 r_position[i] = float3{output.position.x, output.position.y, output.position.z};
510 }
511 if (calc_w) {
512 r_w[i] = output.position.w;
513 }
514 });
515 break;
516 }
517 }
518 }
519
520 void hash_unique(UniqueHashBytes &hash) const override
521 {
522 static constexpr int8_t id = 0;
523 hash.add(&id);
524 hash.add(dimensions_);
525 hash.add(feature_);
526 hash.add(metric_);
527 hash.add(normalize_);
528 }
529
531 {
533 }
534};
535
536class VoronoiDistToEdgeFunction : public mf::MultiFunction {
537 private:
538 int dimensions_;
539 bool normalize_;
540
541 public:
543 : dimensions_(dimensions), normalize_(normalize)
544 {
545 BLI_assert(dimensions >= 1 && dimensions <= 4);
546 static std::array<mf::Signature, 4> signatures{
551 };
552 this->set_signature(&signatures[dimensions - 1]);
553 }
554
555 static mf::Signature create_signature(int dimensions)
556 {
557 mf::Signature signature;
558 mf::SignatureBuilder builder{"voronoi_dist_to_edge", signature};
559
560 if (ELEM(dimensions, 2, 3, 4)) {
561 builder.single_input<float3>("Vector");
562 }
563 if (ELEM(dimensions, 1, 4)) {
564 builder.single_input<float>("W");
565 }
566 builder.single_input<float>("Scale");
567 builder.single_input<float>("Detail");
568 builder.single_input<float>("Roughness");
569 builder.single_input<float>("Lacunarity");
570 builder.single_input<float>("Randomness");
571
572 builder.single_output<float>("Distance");
573
574 return signature;
575 }
576
577 void call(const IndexMask &mask, mf::Params mf_params, mf::Context /*context*/) const override
578 {
579 auto get_vector = [&](int param_index) -> VArray<float3> {
580 return mf_params.readonly_single_input<float3>(param_index, "Vector");
581 };
582 auto get_w = [&](int param_index) -> VArray<float> {
583 return mf_params.readonly_single_input<float>(param_index, "W");
584 };
585 auto get_scale = [&](int param_index) -> VArray<float> {
586 return mf_params.readonly_single_input<float>(param_index, "Scale");
587 };
588 auto get_detail = [&](int param_index) -> VArray<float> {
589 return mf_params.readonly_single_input<float>(param_index, "Detail");
590 };
591 auto get_roughness = [&](int param_index) -> VArray<float> {
592 return mf_params.readonly_single_input<float>(param_index, "Roughness");
593 };
594 auto get_lacunarity = [&](int param_index) -> VArray<float> {
595 return mf_params.readonly_single_input<float>(param_index, "Lacunarity");
596 };
597 auto get_randomness = [&](int param_index) -> VArray<float> {
598 return mf_params.readonly_single_input<float>(param_index, "Randomness");
599 };
600 auto get_r_distance = [&](int param_index) -> MutableSpan<float> {
601 return mf_params.uninitialized_single_output<float>(param_index, "Distance");
602 };
603
604 int param = 0;
605
606 const VArray<float3> &vector = !ELEM(dimensions_, 1) ? get_vector(param++) : VArray<float3>{};
607 const VArray<float> &w = ELEM(dimensions_, 1, 4) ? get_w(param++) : VArray<float>{};
608 const VArray<float> &scale = get_scale(param++);
609 const VArray<float> &detail = get_detail(param++);
610 const VArray<float> &roughness = get_roughness(param++);
611 const VArray<float> &lacunarity = get_lacunarity(param++);
612 const VArray<float> &randomness = get_randomness(param++);
613 MutableSpan<float> r_distance = get_r_distance(param++);
614
616 params.normalize = normalize_;
617
618 switch (dimensions_) {
619 case 1: {
620 mask.foreach_index([&](const int64_t i) {
621 params.scale = scale[i];
622 params.detail = detail[i];
623 params.roughness = roughness[i];
624 params.lacunarity = lacunarity[i];
625 params.randomness = std::min(std::max(randomness[i], 0.0f), 1.0f);
626 params.max_distance = 0.5f + 0.5f * params.randomness;
627
629 w[i] * params.scale);
630 });
631 break;
632 }
633 case 2: {
634 mask.foreach_index([&](const int64_t i) {
635 params.scale = scale[i];
636 params.detail = detail[i];
637 params.roughness = roughness[i];
638 params.lacunarity = lacunarity[i];
639 params.randomness = std::min(std::max(randomness[i], 0.0f), 1.0f);
640 params.max_distance = 0.5f + 0.5f * params.randomness;
641
643 params, float2{vector[i].x, vector[i].y} * params.scale);
644 });
645 break;
646 }
647 case 3: {
648 mask.foreach_index([&](const int64_t i) {
649 params.scale = scale[i];
650 params.detail = detail[i];
651 params.roughness = roughness[i];
652 params.lacunarity = lacunarity[i];
653 params.randomness = std::min(std::max(randomness[i], 0.0f), 1.0f);
654 params.max_distance = 0.5f + 0.5f * params.randomness;
655
657 params, vector[i] * params.scale);
658 });
659 break;
660 }
661 case 4: {
662 mask.foreach_index([&](const int64_t i) {
663 params.scale = scale[i];
664 params.detail = detail[i];
665 params.roughness = roughness[i];
666 params.lacunarity = lacunarity[i];
667 params.randomness = std::min(std::max(randomness[i], 0.0f), 1.0f);
668 params.max_distance = 0.5f + 0.5f * params.randomness;
669
671 params, float4{vector[i].x, vector[i].y, vector[i].z, w[i]} * params.scale);
672 });
673 break;
674 }
675 }
676 }
677
678 void hash_unique(UniqueHashBytes &hash) const override
679 {
680 static constexpr int8_t id = 0;
681 hash.add(&id);
682 hash.add(dimensions_);
683 hash.add(normalize_);
684 }
685
687 {
689 }
690};
691
692class VoronoiNSphereFunction : public mf::MultiFunction {
693 private:
694 int dimensions_;
695
696 public:
697 VoronoiNSphereFunction(int dimensions) : dimensions_(dimensions)
698 {
699 BLI_assert(dimensions >= 1 && dimensions <= 4);
700 static std::array<mf::Signature, 4> signatures{
705 };
706 this->set_signature(&signatures[dimensions - 1]);
707 }
708
709 static mf::Signature create_signature(int dimensions)
710 {
711 mf::Signature signature;
712 mf::SignatureBuilder builder{"voronoi_n_sphere", signature};
713
714 if (ELEM(dimensions, 2, 3, 4)) {
715 builder.single_input<float3>("Vector");
716 }
717 if (ELEM(dimensions, 1, 4)) {
718 builder.single_input<float>("W");
719 }
720 builder.single_input<float>("Scale");
721 builder.single_input<float>("Randomness");
722
723 builder.single_output<float>("Radius");
724
725 return signature;
726 }
727
728 void call(const IndexMask &mask, mf::Params mf_params, mf::Context /*context*/) const override
729 {
730 auto get_vector = [&](int param_index) -> VArray<float3> {
731 return mf_params.readonly_single_input<float3>(param_index, "Vector");
732 };
733 auto get_w = [&](int param_index) -> VArray<float> {
734 return mf_params.readonly_single_input<float>(param_index, "W");
735 };
736 auto get_scale = [&](int param_index) -> VArray<float> {
737 return mf_params.readonly_single_input<float>(param_index, "Scale");
738 };
739 auto get_randomness = [&](int param_index) -> VArray<float> {
740 return mf_params.readonly_single_input<float>(param_index, "Randomness");
741 };
742 auto get_r_radius = [&](int param_index) -> MutableSpan<float> {
743 return mf_params.uninitialized_single_output<float>(param_index, "Radius");
744 };
745
746 int param = 0;
747
748 const VArray<float3> &vector = !ELEM(dimensions_, 1) ? get_vector(param++) : VArray<float3>{};
749 const VArray<float> &w = ELEM(dimensions_, 1, 4) ? get_w(param++) : VArray<float>{};
750 const VArray<float> &scale = get_scale(param++);
751 const VArray<float> &randomness = get_randomness(param++);
752 MutableSpan<float> r_radius = get_r_radius(param++);
753
755
756 switch (dimensions_) {
757 case 1: {
758 mask.foreach_index([&](const int64_t i) {
759 params.scale = scale[i];
760 params.randomness = std::min(std::max(randomness[i], 0.0f), 1.0f);
761
762 r_radius[i] = noise::voronoi_n_sphere_radius(params, w[i] * params.scale);
763 });
764 break;
765 }
766 case 2: {
767 mask.foreach_index([&](const int64_t i) {
768 params.scale = scale[i];
769 params.randomness = std::min(std::max(randomness[i], 0.0f), 1.0f);
770
771 r_radius[i] = noise::voronoi_n_sphere_radius(
772 params, float2{vector[i].x, vector[i].y} * params.scale);
773 });
774 break;
775 }
776 case 3: {
777 mask.foreach_index([&](const int64_t i) {
778 params.scale = scale[i];
779 params.randomness = std::min(std::max(randomness[i], 0.0f), 1.0f);
780
781 r_radius[i] = noise::voronoi_n_sphere_radius(params, vector[i] * params.scale);
782 });
783 break;
784 }
785 case 4: {
786 mask.foreach_index([&](const int64_t i) {
787 params.scale = scale[i];
788 params.randomness = std::min(std::max(randomness[i], 0.0f), 1.0f);
789
790 r_radius[i] = noise::voronoi_n_sphere_radius(
791 params, float4{vector[i].x, vector[i].y, vector[i].z, w[i]} * params.scale);
792 });
793 break;
794 }
795 }
796 }
797
798 void hash_unique(UniqueHashBytes &hash) const override
799 {
800 static constexpr int8_t id = 0;
801 hash.add(&id);
802 hash.add(dimensions_);
803 }
804
806 {
808 }
809};
810
812{
813 const NodeTexVoronoi &storage = node_storage(builder.node());
814 switch (storage.feature) {
817 storage.normalize);
818 break;
819 }
822 break;
823 }
824 default: {
826 storage.dimensions, storage.feature, storage.distance, storage.normalize);
827 break;
828 }
829 }
830}
831
832} // namespace nodes::node_shader_tex_voronoi_cc
833
835{
836 namespace file_ns = nodes::node_shader_tex_voronoi_cc;
837
838 static bke::bNodeType ntype;
839
840 common_node_type_base(&ntype, "ShaderNodeTexVoronoi"_ustr, SH_NODE_TEX_VORONOI);
841 ntype.ui_name = "Voronoi Texture";
842 ntype.ui_description =
843 "Generate Worley noise based on the distance to random points. Typically used to generate "
844 "textures such as stones, water, or biological cells";
845 ntype.enum_name_legacy = "TEX_VORONOI";
847 ntype.declare = file_ns::sh_node_tex_voronoi_declare;
848 ntype.draw_buttons = file_ns::node_shader_buts_tex_voronoi;
849 ntype.initfunc = file_ns::node_shader_init_tex_voronoi;
852 ntype.gpu_fn = file_ns::node_shader_gpu_tex_voronoi;
853 ntype.build_multi_function = file_ns::sh_node_voronoi_build_multi_function;
855
857}
858
859} // 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_VORONOI
#define ELEM(...)
btScalar max
#define output
SIMD_FORCE_INLINE const btScalar & w() const
Return the w value.
Definition btQuadWord.h:119
constexpr bool is_empty() const
Definition BLI_span.hh:515
void set_signature(const Signature *signature)
void make_available(bNode &node) const
void call(const IndexMask &mask, mf::Params mf_params, mf::Context) const override
void call(const IndexMask &mask, mf::Params mf_params, mf::Context) const override
static mf::Signature create_signature(int dimensions, int feature, int metric)
VoronoiMetricFunction(int dimensions, int feature, int metric, bool normalize)
void call(const IndexMask &mask, mf::Params mf_params, mf::Context) const override
#define storage(slot, qualifiers)
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
static const char * gpu_shader_get_name(const int feature, const int dimensions)
static void sh_node_tex_voronoi_declare(NodeDeclarationBuilder &b)
static void node_shader_buts_tex_voronoi(ui::Layout &layout, bContext *, PointerRNA *ptr)
static mf::MultiFunction::ExecutionHints voronoi_execution_hints
static void sh_node_voronoi_build_multi_function(NodeMultiFunctionBuilder &builder)
static int node_shader_gpu_tex_voronoi(GPUMaterial *mat, bNode *node, bNodeExecData *, GPUNodeStack *in, GPUNodeStack *out)
static void node_shader_init_tex_voronoi(bNodeTree *node_tree, bNode *node)
template VoronoiOutput fractal_voronoi_x_fx< float4 >(const VoronoiParams &params, const float4 coord, const bool calc_color)
template float fractal_voronoi_distance_to_edge< float >(const VoronoiParams &params, const float coord)
float voronoi_n_sphere_radius(const VoronoiParams &params, const float coord)
Definition noise.cc:1368
template VoronoiOutput fractal_voronoi_x_fx< float3 >(const VoronoiParams &params, const float3 coord, const bool calc_color)
float voronoi_distance(const float a, const float b)
Definition noise.cc:1164
template VoronoiOutput fractal_voronoi_x_fx< float >(const VoronoiParams &params, const float coord, const bool calc_color)
template float fractal_voronoi_distance_to_edge< float3 >(const VoronoiParams &params, const float3 coord)
template VoronoiOutput fractal_voronoi_x_fx< float2 >(const VoronoiParams &params, const float2 coord, const bool calc_color)
template float fractal_voronoi_distance_to_edge< float4 >(const VoronoiParams &params, const float4 coord)
template float fractal_voronoi_distance_to_edge< float2 >(const VoronoiParams &params, const float2 coord)
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_voronoi()
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
@ SHD_VORONOI_EUCLIDEAN
@ SHD_VORONOI_MINKOWSKI
void node_copy_standard_storage(bNodeTree *, bNode *dest_node, const bNode *src_node)
Definition node_util.cc:57
ColorSceneLinear4f< eAlpha::Premultiplied > ColorGeometry4f
@ SHD_VORONOI_SMOOTH_F1
@ SHD_VORONOI_DISTANCE_TO_EDGE
@ SHD_VORONOI_N_SPHERE_RADIUS
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 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
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)