Blender V5.3
node_shader_vector_math.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 "BLI_math_vector.hh"
13
15
17#include "NOD_math_functions.hh"
18#include "NOD_multi_function.hh"
21
22#include "RNA_enum_types.hh"
23
25#include "UI_resources.hh"
26
27namespace blender {
28
30
32{
33 b.is_function_node();
34 b.add_input<decl::Vector>("Vector"_ustr)
35 .min(-10000.0f)
36 .max(10000.0f)
37 .label_fn([](const bNode &node) {
38 switch (node.custom1) {
40 return IFACE_("Base");
41 default:
42 return IFACE_("Vector");
43 }
44 });
45 b.add_input<decl::Vector>("Vector"_ustr, "Vector_001"_ustr)
46 .min(-10000.0f)
47 .max(10000.0f)
48 .label_fn([](const bNode &node) {
49 switch (node.custom1) {
51 return IFACE_("Exponent");
53 return IFACE_("Multiplier");
55 return IFACE_("Incident");
57 return IFACE_("Max");
59 return IFACE_("Increment");
60 default:
61 return IFACE_("Vector");
62 }
63 });
64 b.add_input<decl::Vector>("Vector"_ustr, "Vector_002"_ustr)
65 .min(-10000.0f)
66 .max(10000.0f)
67 .label_fn([](const bNode &node) {
68 switch (node.custom1) {
70 return IFACE_("Addend");
72 return IFACE_("Reference");
74 return IFACE_("Min");
75 default:
76 return IFACE_("Vector");
77 }
78 });
79 b.add_input<decl::Float>("Scale"_ustr)
80 .default_value(1.0f)
81 .min(-10000.0f)
82 .max(10000.0f)
83 .label_fn([](const bNode &node) {
84 switch (node.custom1) {
86 default:
87 return IFACE_("Scale");
89 return IFACE_("IOR");
90 }
91 });
92 b.add_output<decl::Vector>("Vector"_ustr);
93 b.add_output<decl::Float>("Value"_ustr);
94}
95
96static void node_shader_buts_vect_math(ui::Layout &layout, bContext * /*C*/, PointerRNA *ptr)
97{
98 layout.prop(ptr, "operation", ui::ITEM_R_SPLIT_EMPTY_NAME, "", ICON_NONE);
99}
100
102{
103 bNodeSocket *socket_2 = bke::node_find_socket(node, SOCK_IN, "Vector_001"_ustr);
104 BLI_assert(socket_2 != nullptr);
105 bNodeSocketValueVector *in_vector_2 = socket_2->default_value_typed<bNodeSocketValueVector>();
106
107 bNodeSocket *socket_3 = bke::node_find_socket(node, SOCK_IN, "Vector_002"_ustr);
108 BLI_assert(socket_3 != nullptr);
109 bNodeSocketValueVector *in_vector_3 = socket_3->default_value_typed<bNodeSocketValueVector>();
110
111 switch (mode) {
116 for (int i = 0; i < in_vector_2->dimensions; i++) {
117 in_vector_2->value[i] = 1.0f;
118 }
119 break;
120 }
122 BLI_assert(in_vector_2->dimensions == in_vector_3->dimensions);
123 for (int i = 0; i < in_vector_3->dimensions; i++) {
124 in_vector_2->value[i] = 1.0f;
125 in_vector_3->value[i] = 0.0f;
126 }
127 break;
128 }
129
130 default:
131 /* Use the default defined in the node declaration otherwise. */
132 break;
133 }
134}
135
137 public:
141 {
142 bNode &node = params.add_node("ShaderNodeVectorMath"_ustr);
143 node.custom1 = mode;
146 }
147};
148
150{
151 if (!params.node_tree().typeinfo->validate_link(params.other_socket().type, SOCK_VECTOR)) {
152 return;
153 }
154
155 const int weight = ELEM(params.other_socket().type, SOCK_VECTOR, SOCK_RGBA) ? 0 : -1;
156
157 for (const EnumPropertyItem *item = rna_enum_node_vec_math_items; item->identifier != nullptr;
158 item++)
159 {
160 if (item->name != nullptr && item->identifier[0] != '\0') {
161 if ((params.in_out() == SOCK_OUT) && ELEM(item->value,
165 {
167 SocketSearchOp{"Value"_ustr, NodeVectorMathOperation(item->value)},
168 weight);
169 }
170 else {
172 SocketSearchOp{"Vector"_ustr, NodeVectorMathOperation(item->value)},
173 weight);
174 }
175 }
176 }
177}
178
179static const char *gpu_shader_get_name(int mode)
180{
181 switch (mode) {
183 return "vector_math_add";
185 return "vector_math_subtract";
187 return "vector_math_multiply";
189 return "vector_math_divide";
190
192 return "vector_math_cross";
194 return "vector_math_project";
196 return "vector_math_reflect";
198 return "vector_math_dot";
199
201 return "vector_math_distance";
203 return "vector_math_length";
205 return "vector_math_scale";
207 return "vector_math_normalize";
208
210 return "vector_math_snap";
212 return "vector_math_round";
214 return "vector_math_floor";
216 return "vector_math_ceil";
218 return "vector_math_modulo";
220 return "vector_math_fraction";
222 return "vector_math_absolute";
224 return "vector_math_minimum";
226 return "vector_math_maximum";
228 return "vector_math_wrap";
230 return "vector_math_sine";
232 return "vector_math_cosine";
234 return "vector_math_tangent";
236 return "vector_math_refract";
238 return "vector_math_faceforward";
240 return "vector_math_multiply_add";
242 return "vector_math_power";
244 return "vector_math_sign";
245 }
246
247 return nullptr;
248}
249
251 bNode *node,
252 bNodeExecData * /*execdata*/,
255{
256 /* Can't emit 'a / a' (see #162948). So we have to fold it. */
257 if (in[0].link != nullptr && in[0].link == in[1].link) {
258 switch (node->custom1) {
260 return GPU_stack_link(mat, node, "vector_math_divide_self", in, out);
262 return GPU_stack_link(mat, node, "vector_math_snap_self", in, out);
263 default:
264 break;
265 }
266 }
267
268 const char *name = gpu_shader_get_name(node->custom1);
269 if (name != nullptr) {
270 return GPU_stack_link(mat, node, name, in, out);
271 }
272
273 return 0;
274}
275
277{
278 bNodeSocket *sockB = static_cast<bNodeSocket *>(BLI_findlink(&node->inputs, 1));
279 bNodeSocket *sockC = static_cast<bNodeSocket *>(BLI_findlink(&node->inputs, 2));
280 bNodeSocket *sockScale = bke::node_find_socket(*node, SOCK_IN, "Scale"_ustr);
281
282 bNodeSocket *sockVector = bke::node_find_socket(*node, SOCK_OUT, "Vector"_ustr);
283 bNodeSocket *sockValue = bke::node_find_socket(*node, SOCK_OUT, "Value"_ustr);
284
286 *sockB,
287 !ELEM(node->custom1,
301 *sockC,
302 ELEM(node->custom1,
307 *ntree, *sockScale, ELEM(node->custom1, NODE_VECTOR_MATH_SCALE, NODE_VECTOR_MATH_REFRACT));
309 *sockVector,
310 !ELEM(node->custom1,
315 *sockValue,
316 ELEM(node->custom1,
320}
321
322static const mf::MultiFunction *get_multi_function(const bNode &node)
323{
324 const NodeVectorMathOperation operation = NodeVectorMathOperation(node.custom1);
326 if (!info) {
327 return nullptr;
328 }
330}
331
333{
334 const mf::MultiFunction *fn = get_multi_function(builder.node());
335 builder.set_matching_fn(fn);
336}
337
339{
340 using namespace value_elem;
342 switch (op) {
347 VectorElem output_elem;
348 output_elem.merge(params.get_input_elem<VectorElem>("Vector"_ustr));
349 output_elem.merge(params.get_input_elem<VectorElem>("Vector_001"_ustr));
350 params.set_output_elem("Vector"_ustr, output_elem);
351 break;
352 }
354 VectorElem output_elem;
355 output_elem.merge(params.get_input_elem<VectorElem>("Vector"_ustr));
356 if (params.get_input_elem<FloatElem>("Scale"_ustr)) {
357 output_elem = VectorElem::all();
358 }
359 params.set_output_elem("Vector"_ustr, output_elem);
360 }
361 default:
362 break;
363 }
364}
365
367{
369 switch (op) {
375 params.set_input_elem("Vector"_ustr,
376 params.get_output_elem<value_elem::VectorElem>("Vector"_ustr));
377 break;
378 }
379 default:
380 break;
381 }
382}
383
385{
387 const UString first_input_id = "Vector"_ustr;
388 const UString second_input_id = "Vector_001"_ustr;
389 const UString scale_input_id = "Scale"_ustr;
390 const UString output_vector_id = "Vector"_ustr;
391 switch (op) {
393 params.set_input(first_input_id,
394 params.get_output<float3>(output_vector_id) -
395 params.get_input<float3>(second_input_id));
396 break;
397 }
399 params.set_input(first_input_id,
400 params.get_output<float3>(output_vector_id) +
401 params.get_input<float3>(second_input_id));
402 break;
403 }
405 params.set_input(first_input_id,
406 math::safe_divide(params.get_output<float3>(output_vector_id),
407 params.get_input<float3>(second_input_id)));
408 break;
409 }
411 params.set_input(first_input_id,
412 params.get_output<float3>(output_vector_id) *
413 params.get_input<float3>(second_input_id));
414 break;
415 }
417 params.set_input(first_input_id,
418 math::safe_divide(params.get_output<float3>(output_vector_id),
419 float3(params.get_input<float>(scale_input_id))));
420 break;
421 }
422 default: {
423 break;
424 }
425 }
426}
427
429#ifdef WITH_MATERIALX
430{
431 auto op = node_->custom1;
432 NodeItem res = empty();
433 const NodeItem null_vec = val(MaterialX::Vector3(0.0f));
434
435 /* Single operand operations */
436 NodeItem x = get_input_value(0, NodeItem::Type::Vector3);
437
438 switch (op) {
440 res = x.sin();
441 break;
443 res = x.cos();
444 break;
446 res = x.tan();
447 break;
449 res = x.abs();
450 break;
452 res = (x + val(0.5f)).floor();
453 break;
455 res = x.floor();
456 break;
458 res = x.ceil();
459 break;
461 res = x % val(1.0f);
462 break;
464 res = x.length();
465 break;
467 NodeItem length = x.length();
468 res = length.if_else(NodeItem::CompareOp::Eq, val(0.0f), null_vec, x / length);
469 break;
470 }
471
472 default: {
473 /* 2-operand operations */
474 NodeItem y = get_input_value(1, NodeItem::Type::Vector3);
475 NodeItem w = get_input_value(3, NodeItem::Type::Float);
476
477 switch (op) {
479 res = x + y;
480 break;
482 res = x - y;
483 break;
485 res = x * y;
486 break;
488 res = x / y;
489 break;
491 res = x.min(y);
492 break;
494 res = x.max(y);
495 break;
497 res = x % y;
498 break;
500 res = (x / y).floor() * y;
501 break;
503 res = create_node("crossproduct", NodeItem::Type::Vector3, {{"in1", x}, {"in2", y}});
504 break;
506 res = x.dotproduct(y);
507 break;
509 NodeItem len_sq = y.dotproduct(y);
510 res = len_sq.if_else(
511 NodeItem::CompareOp::NotEq, val(0.0f), (x.dotproduct(y) / len_sq) * y, null_vec);
512 break;
513 }
515 /* TODO: use <reflect> node in MaterialX 1.38.9 */
516 res = x - val(2.0f) * y.dotproduct(x) * y;
517 break;
519 res = (y - x).length();
520 break;
522 res = x * w;
523 break;
524
525 default: {
526 /* 3-operand operations */
527 NodeItem z = get_input_value(2, NodeItem::Type::Vector3);
528
529 switch (op) {
531 res = x * y + z;
532 break;
534 /* TODO: use <refract> node in MaterialX 1.38.9 */
535 NodeItem dot_yx = y.dotproduct(x);
536 NodeItem k = val(1.0f) - (w * w * (val(1.0f) - (dot_yx * dot_yx)));
537 NodeItem r = w * x - ((w * dot_yx + k.sqrt()) * y);
538 res = k.if_else(NodeItem::CompareOp::GreaterEq, val(0.0f), r, null_vec);
539 break;
540 }
542 res = z.dotproduct(y).if_else(NodeItem::CompareOp::GreaterEq, val(0.0f), -x, x);
543 break;
544 }
546 NodeItem range = (y - z);
547 NodeItem if_branch = x - (range * ((x - z) / range).floor());
548
549 res = create_node("combine3", NodeItem::Type::Vector3);
550 std::vector<std::string> inputs = {"in1", "in2", "in3"};
551
552 for (size_t i = 0; i < inputs.size(); ++i) {
553 res.set_input(
554 inputs[i],
555 range[i].if_else(NodeItem::CompareOp::NotEq, val(0.0f), if_branch[i], z[i]));
556 }
557 break;
558 }
559 default:
561 }
562 }
563 }
564 }
565 }
566
567 return res;
568}
569#endif
571
572} // namespace nodes::node_shader_vector_math_cc
573
575{
576 namespace file_ns = nodes::node_shader_vector_math_cc;
577
578 static bke::bNodeType ntype;
579
580 common_node_type_base(&ntype, "ShaderNodeVectorMath"_ustr, SH_NODE_VECTOR_MATH);
581 ntype.ui_name = "Vector Math";
582 ntype.ui_description = "Perform vector math operation";
583 ntype.enum_name_legacy = "VECT_MATH";
585 ntype.declare = file_ns::sh_node_vector_math_declare;
586 ntype.draw_buttons = file_ns::node_shader_buts_vect_math;
588 ntype.gpu_fn = file_ns::gpu_shader_vector_math;
589 ntype.updatefunc = file_ns::node_shader_update_vector_math;
590 ntype.build_multi_function = file_ns::sh_node_vector_math_build_multi_function;
591 ntype.gather_link_search_ops = file_ns::sh_node_vector_math_gather_link_searches;
592 ntype.materialx_fn = file_ns::node_shader_materialx;
593 ntype.eval_elem = file_ns::node_eval_elem;
594 ntype.eval_inverse_elem = file_ns::node_eval_inverse_elem;
595 ntype.eval_inverse = file_ns::node_eval_inverse;
596
598}
599
600} // namespace blender
#define NODE_CLASS_OP_VECTOR
Definition BKE_node.hh:481
#define SH_NODE_VECTOR_MATH
#define BLT_I18NCONTEXT_ID_NODETREE
#define CTX_IFACE_(context, msgid)
#define IFACE_(msgid)
#define BLI_assert_unreachable()
#define ELEM(...)
SIMD_FORCE_INLINE const btScalar & z() const
Return the z value.
Definition btQuadWord.h:117
SIMD_FORCE_INLINE const btScalar & w() const
Return the w value.
Definition btQuadWord.h:119
SIMD_FORCE_INLINE btScalar length() const
Return the length of the vector.
Definition btVector3.h:257
void add_item(std::string socket_name, SocketLinkOperation::LinkSocketFn fn, int weight=0)
void update_and_connect_available_socket(bNode &new_node, UString socket_name)
void set_matching_fn(const mf::MultiFunction *fn)
std::unique_ptr< CustomSocketLabelFn > label_fn
void set_input(const UString identifier, T value)
T get_input_elem(const UString identifier) const
void set_output_elem(const UString identifier, T elem)
void set_input_elem(const UString identifier, T elem)
#define floor
float length(VecOp< float, D >) RET
void node_register_type(bNodeType &ntype)
Definition node.cc:2920
void node_set_socket_availability(bNodeTree &ntree, bNodeSocket &sock, bool is_available)
Definition node.cc:5499
bNodeSocket * node_find_socket(bNode &node, eNodeSocketInOut in_out, UString identifier)
Definition node.cc:3036
const MultiFunction & lookup(UString id)
T safe_divide(const T &a, const T &b)
static void sh_node_vector_math_build_multi_function(NodeMultiFunctionBuilder &builder)
static int gpu_shader_vector_math(GPUMaterial *mat, bNode *node, bNodeExecData *, GPUNodeStack *in, GPUNodeStack *out)
static void sh_node_vector_math_gather_link_searches(GatherLinkSearchOpParams &params)
static void node_eval_elem(value_elem::ElemEvalParams &params)
static const mf::MultiFunction * get_multi_function(const bNode &node)
static void node_eval_inverse(inverse_eval::InverseEvalParams &params)
static void node_shader_update_vector_math(bNodeTree *ntree, bNode *node)
static void sh_node_vector_math_declare(NodeDeclarationBuilder &b)
static void node_shader_buts_vect_math(ui::Layout &layout, bContext *, PointerRNA *ptr)
static void node_eval_inverse_elem(value_elem::InverseElemEvalParams &params)
static void vector_math_input_defaults(bNode &node, const NodeVectorMathOperation mode)
const FloatMathOperationInfo * get_float3_math_operation_info(const int operation)
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,...)
void node_vector_math_label(const bNodeTree *, const bNode *node, char *label, int label_maxncpy)
Definition node_util.cc:171
void * BLI_findlink(const ListBase *listbase, int number) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1)
Definition listbase.cc:541
void register_node_type_sh_vect_math()
const EnumPropertyItem rna_enum_node_vec_math_items[]
@ NODE_VECTOR_MATH_COSINE
@ NODE_VECTOR_MATH_SINE
@ NODE_VECTOR_MATH_MAXIMUM
@ NODE_VECTOR_MATH_FLOOR
@ NODE_VECTOR_MATH_MINIMUM
@ NODE_VECTOR_MATH_MULTIPLY_ADD
@ NODE_VECTOR_MATH_REFLECT
@ NODE_VECTOR_MATH_ROUND
@ NODE_VECTOR_MATH_TANGENT
@ NODE_VECTOR_MATH_PROJECT
@ NODE_VECTOR_MATH_SCALE
@ NODE_VECTOR_MATH_POWER
@ NODE_VECTOR_MATH_CROSS_PRODUCT
@ NODE_VECTOR_MATH_DIVIDE
@ NODE_VECTOR_MATH_NORMALIZE
@ NODE_VECTOR_MATH_SUBTRACT
@ NODE_VECTOR_MATH_DOT_PRODUCT
@ NODE_VECTOR_MATH_SNAP
@ NODE_VECTOR_MATH_DISTANCE
@ NODE_VECTOR_MATH_LENGTH
@ NODE_VECTOR_MATH_ADD
@ NODE_VECTOR_MATH_MODULO
@ NODE_VECTOR_MATH_FRACTION
@ NODE_VECTOR_MATH_CEIL
@ NODE_VECTOR_MATH_SIGN
@ NODE_VECTOR_MATH_WRAP
@ NODE_VECTOR_MATH_FACEFORWARD
@ NODE_VECTOR_MATH_REFRACT
@ NODE_VECTOR_MATH_MULTIPLY
@ NODE_VECTOR_MATH_ABSOLUTE
VecBase< float, 3 > float3
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 min(a, b)
Definition sort.cc:38
eNodeSocketDatatype type
bke::bNodeTreeType * typeinfo
bool(*) validate_link(eNodeSocketDatatype from, eNodeSocketDatatype to)
Definition BKE_node.hh:566
Defines a node type.
Definition BKE_node.hh:265
NodeInverseElemEvalFunction eval_inverse_elem
Definition BKE_node.hh:434
NodeMaterialXFunction materialx_fn
Definition BKE_node.hh:375
void(*) draw_buttons(ui::Layout &, bContext *C, PointerRNA *ptr)
Definition BKE_node.hh:290
NodeInverseEvalFunction eval_inverse
Definition BKE_node.hh:441
std::string ui_description
Definition BKE_node.hh:271
void(*) updatefunc(bNodeTree *ntree, bNode *node)
Definition BKE_node.hh:312
void(*) labelfunc(const bNodeTree *ntree, const bNode *node, char *label, int label_maxncpy)
Definition BKE_node.hh:301
NodeGPUExecFunction gpu_fn
Definition BKE_node.hh:373
NodeElemEvalFunction eval_elem
Definition BKE_node.hh:428
NodeMultiFunctionBuildFunction build_multi_function
Definition BKE_node.hh:382
const char * enum_name_legacy
Definition BKE_node.hh:274
NodeGatherSocketLinkOperationsFunction gather_link_search_ops
Definition BKE_node.hh:409
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)