Blender  V3.3
node_shader_vector_math.cc
Go to the documentation of this file.
1 /* SPDX-License-Identifier: GPL-2.0-or-later
2  * Copyright 2005 Blender Foundation. All rights reserved. */
3 
8 #include "node_shader_util.hh"
9 
10 #include "NOD_math_functions.hh"
12 
13 #include "RNA_enum_types.h"
14 
15 #include "UI_interface.h"
16 #include "UI_resources.h"
17 
19 
21 {
22  b.is_function_node();
23  b.add_input<decl::Vector>(N_("Vector")).min(-10000.0f).max(10000.0f);
24  b.add_input<decl::Vector>(N_("Vector"), "Vector_001").min(-10000.0f).max(10000.0f);
25  b.add_input<decl::Vector>(N_("Vector"), "Vector_002").min(-10000.0f).max(10000.0f);
26  b.add_input<decl::Float>(N_("Scale")).default_value(1.0f).min(-10000.0f).max(10000.0f);
27  b.add_output<decl::Vector>(N_("Vector"));
28  b.add_output<decl::Float>(N_("Value"));
29 }
30 
32 {
33  uiItemR(layout, ptr, "operation", UI_ITEM_R_SPLIT_EMPTY_NAME, "", ICON_NONE);
34 }
35 
37  public:
38  std::string socket_name;
41  {
42  bNode &node = params.add_node("ShaderNodeVectorMath");
43  node.custom1 = mode;
44  params.update_and_connect_available_socket(node, socket_name);
45  }
46 };
47 
49 {
50  if (!params.node_tree().typeinfo->validate_link(
51  static_cast<eNodeSocketDatatype>(params.other_socket().type), SOCK_VECTOR)) {
52  return;
53  }
54 
55  const int weight = ELEM(params.other_socket().type, SOCK_VECTOR, SOCK_RGBA) ? 0 : -1;
56 
57  for (const EnumPropertyItem *item = rna_enum_node_vec_math_items; item->identifier != nullptr;
58  item++) {
59  if (item->name != nullptr && item->identifier[0] != '\0') {
60  if ((params.in_out() == SOCK_OUT) && ELEM(item->value,
64  params.add_item(IFACE_(item->name),
65  SocketSearchOp{"Value", (NodeVectorMathOperation)item->value},
66  weight);
67  }
68  else {
69  params.add_item(IFACE_(item->name),
70  SocketSearchOp{"Vector", (NodeVectorMathOperation)item->value},
71  weight);
72  }
73  }
74  }
75 }
76 
77 static const char *gpu_shader_get_name(int mode)
78 {
79  switch (mode) {
81  return "vector_math_add";
83  return "vector_math_subtract";
85  return "vector_math_multiply";
87  return "vector_math_divide";
88 
90  return "vector_math_cross";
92  return "vector_math_project";
94  return "vector_math_reflect";
96  return "vector_math_dot";
97 
99  return "vector_math_distance";
101  return "vector_math_length";
103  return "vector_math_scale";
105  return "vector_math_normalize";
106 
108  return "vector_math_snap";
110  return "vector_math_floor";
112  return "vector_math_ceil";
114  return "vector_math_modulo";
116  return "vector_math_fraction";
118  return "vector_math_absolute";
120  return "vector_math_minimum";
122  return "vector_math_maximum";
124  return "vector_math_wrap";
126  return "vector_math_sine";
128  return "vector_math_cosine";
130  return "vector_math_tangent";
132  return "vector_math_refract";
134  return "vector_math_faceforward";
136  return "vector_math_multiply_add";
137  }
138 
139  return nullptr;
140 }
141 
143  bNode *node,
144  bNodeExecData *UNUSED(execdata),
145  GPUNodeStack *in,
146  GPUNodeStack *out)
147 {
148  const char *name = gpu_shader_get_name(node->custom1);
149  if (name != nullptr) {
150  return GPU_stack_link(mat, node, name, in, out);
151  }
152 
153  return 0;
154 }
155 
157 {
158  bNodeSocket *sockB = (bNodeSocket *)BLI_findlink(&node->inputs, 1);
159  bNodeSocket *sockC = (bNodeSocket *)BLI_findlink(&node->inputs, 2);
160  bNodeSocket *sockScale = nodeFindSocket(node, SOCK_IN, "Scale");
161 
162  bNodeSocket *sockVector = nodeFindSocket(node, SOCK_OUT, "Vector");
163  bNodeSocket *sockValue = nodeFindSocket(node, SOCK_OUT, "Value");
164 
166  sockB,
167  !ELEM(node->custom1,
179  sockC,
180  ELEM(node->custom1,
187  sockVector,
188  !ELEM(node->custom1,
193  sockValue,
194  ELEM(node->custom1,
198 
199  /* Labels */
200  node_sock_label_clear(sockB);
201  node_sock_label_clear(sockC);
202  node_sock_label_clear(sockScale);
203  switch (node->custom1) {
205  node_sock_label(sockB, "Multiplier");
206  node_sock_label(sockC, "Addend");
207  break;
209  node_sock_label(sockB, "Incident");
210  node_sock_label(sockC, "Reference");
211  break;
213  node_sock_label(sockB, "Max");
214  node_sock_label(sockC, "Min");
215  break;
217  node_sock_label(sockB, "Increment");
218  break;
220  node_sock_label(sockScale, "Ior");
221  break;
223  node_sock_label(sockScale, "Scale");
224  break;
225  }
226 }
227 
229 {
231 
232  const fn::MultiFunction *multi_fn = nullptr;
233 
235  operation, [&](auto exec_preset, auto function, const FloatMathOperationInfo &info) {
237  info.title_case_name.c_str(), function, exec_preset};
238  multi_fn = &fn;
239  });
240  if (multi_fn != nullptr) {
241  return multi_fn;
242  }
243 
245  operation, [&](auto exec_preset, auto function, const FloatMathOperationInfo &info) {
247  info.title_case_name.c_str(), function, exec_preset};
248  multi_fn = &fn;
249  });
250  if (multi_fn != nullptr) {
251  return multi_fn;
252  }
253 
255  operation, [&](auto exec_preset, auto function, const FloatMathOperationInfo &info) {
257  info.title_case_name.c_str(), function, exec_preset};
258  multi_fn = &fn;
259  });
260  if (multi_fn != nullptr) {
261  return multi_fn;
262  }
263 
265  operation, [&](auto exec_preset, auto function, const FloatMathOperationInfo &info) {
267  info.title_case_name.c_str(), function, exec_preset};
268  multi_fn = &fn;
269  });
270  if (multi_fn != nullptr) {
271  return multi_fn;
272  }
273 
275  operation, [&](auto exec_preset, auto function, const FloatMathOperationInfo &info) {
277  info.title_case_name.c_str(), function, exec_preset};
278  multi_fn = &fn;
279  });
280  if (multi_fn != nullptr) {
281  return multi_fn;
282  }
283 
285  operation, [&](auto exec_preset, auto function, const FloatMathOperationInfo &info) {
287  info.title_case_name.c_str(), function, exec_preset};
288  multi_fn = &fn;
289  });
290  if (multi_fn != nullptr) {
291  return multi_fn;
292  }
293 
295  operation, [&](auto exec_preset, auto function, const FloatMathOperationInfo &info) {
297  info.title_case_name.c_str(), function, exec_preset};
298  multi_fn = &fn;
299  });
300  if (multi_fn != nullptr) {
301  return multi_fn;
302  }
303 
304  return nullptr;
305 }
306 
308 {
309  const fn::MultiFunction *fn = get_multi_function(builder.node());
310  builder.set_matching_fn(fn);
311 }
312 
313 } // namespace blender::nodes::node_shader_vector_math_cc
314 
316 {
317  namespace file_ns = blender::nodes::node_shader_vector_math_cc;
318 
319  static bNodeType ntype;
320 
329 
330  nodeRegisterType(&ntype);
331 }
void node_type_gpu(struct bNodeType *ntype, NodeGPUExecFunction gpu_fn)
Definition: node.cc:4465
void node_type_update(struct bNodeType *ntype, void(*updatefunc)(struct bNodeTree *ntree, struct bNode *node))
Definition: node.cc:4443
void nodeSetSocketAvailability(struct bNodeTree *ntree, struct bNodeSocket *sock, bool is_available)
Definition: node.cc:3664
struct bNodeSocket * nodeFindSocket(const struct bNode *node, eNodeSocketInOut in_out, const char *identifier)
#define NODE_CLASS_OP_VECTOR
Definition: BKE_node.h:348
void nodeRegisterType(struct bNodeType *ntype)
Definition: node.cc:1357
void * BLI_findlink(const struct ListBase *listbase, int number) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1)
#define UNUSED(x)
#define ELEM(...)
#define IFACE_(msgid)
NodeVectorMathOperation
@ NODE_VECTOR_MATH_NORMALIZE
@ NODE_VECTOR_MATH_LENGTH
@ NODE_VECTOR_MATH_CROSS_PRODUCT
@ NODE_VECTOR_MATH_CEIL
@ NODE_VECTOR_MATH_MODULO
@ NODE_VECTOR_MATH_ADD
@ NODE_VECTOR_MATH_COSINE
@ NODE_VECTOR_MATH_REFLECT
@ NODE_VECTOR_MATH_WRAP
@ NODE_VECTOR_MATH_REFRACT
@ NODE_VECTOR_MATH_DOT_PRODUCT
@ NODE_VECTOR_MATH_ABSOLUTE
@ NODE_VECTOR_MATH_DIVIDE
@ NODE_VECTOR_MATH_TANGENT
@ NODE_VECTOR_MATH_DISTANCE
@ NODE_VECTOR_MATH_FLOOR
@ NODE_VECTOR_MATH_SNAP
@ NODE_VECTOR_MATH_SINE
@ NODE_VECTOR_MATH_FRACTION
@ NODE_VECTOR_MATH_PROJECT
@ NODE_VECTOR_MATH_MULTIPLY
@ NODE_VECTOR_MATH_SCALE
@ NODE_VECTOR_MATH_MAXIMUM
@ NODE_VECTOR_MATH_FACEFORWARD
@ NODE_VECTOR_MATH_SUBTRACT
@ NODE_VECTOR_MATH_MULTIPLY_ADD
@ NODE_VECTOR_MATH_MINIMUM
@ SOCK_OUT
@ SOCK_IN
eNodeSocketDatatype
@ SOCK_VECTOR
@ SOCK_RGBA
bool GPU_stack_link(GPUMaterial *mat, struct bNode *node, const char *name, GPUNodeStack *in, GPUNodeStack *out,...)
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 Bright Control the brightness and contrast of the input color Vector Map an input vectors to used to fine tune the interpolation of the input 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 SH_NODE_VECTOR_MATH
@ UI_ITEM_R_SPLIT_EMPTY_NAME
void uiItemR(uiLayout *layout, struct PointerRNA *ptr, const char *propname, int flag, const char *name, int icon)
constexpr const char * c_str() const
void set_matching_fn(const MultiFunction *fn)
OperationNode * node
bNodeTree * ntree
uiWidgetBaseParameters params[MAX_WIDGET_BASE_BATCH]
static void sh_node_vector_math_build_multi_function(NodeMultiFunctionBuilder &builder)
static const fn::MultiFunction * get_multi_function(bNode &node)
static void sh_node_vector_math_gather_link_searches(GatherLinkSearchOpParams &params)
static void node_shader_buts_vect_math(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
static int gpu_shader_vector_math(GPUMaterial *mat, bNode *node, bNodeExecData *UNUSED(execdata), GPUNodeStack *in, GPUNodeStack *out)
static void node_shader_update_vector_math(bNodeTree *ntree, bNode *node)
static void sh_node_vector_math_declare(NodeDeclarationBuilder &b)
bool try_dispatch_float_math_fl3_fl3_to_fl3(const NodeVectorMathOperation operation, Callback &&callback)
bool try_dispatch_float_math_fl3_to_fl(const NodeVectorMathOperation operation, Callback &&callback)
bool try_dispatch_float_math_fl3_fl3_to_fl(const NodeVectorMathOperation operation, Callback &&callback)
bool try_dispatch_float_math_fl3_fl_to_fl3(const NodeVectorMathOperation operation, Callback &&callback)
bool try_dispatch_float_math_fl3_fl3_fl_to_fl3(const NodeVectorMathOperation operation, Callback &&callback)
bool try_dispatch_float_math_fl3_to_fl3(const NodeVectorMathOperation operation, Callback &&callback)
bool try_dispatch_float_math_fl3_fl3_fl3_to_fl3(const NodeVectorMathOperation operation, Callback &&callback)
static const pxr::TfToken out("out", pxr::TfToken::Immortal)
static const pxr::TfToken b("b", pxr::TfToken::Immortal)
void sh_fn_node_type_base(bNodeType *ntype, int type, const char *name, short nclass)
void register_node_type_sh_vect_math()
void node_sock_label_clear(bNodeSocket *sock)
Definition: node_util.c:81
void node_sock_label(bNodeSocket *sock, const char *name)
Definition: node_util.c:76
void node_vector_math_label(const bNodeTree *UNUSED(ntree), const bNode *node, char *label, int maxlen)
Definition: node_util.c:206
const EnumPropertyItem rna_enum_node_vec_math_items[]
Definition: rna_nodetree.c:235
#define min(a, b)
Definition: sort.c:35
const char * identifier
Definition: RNA_types.h:461
Defines a node type.
Definition: BKE_node.h:226
NodeGatherSocketLinkOperationsFunction gather_link_search_ops
Definition: BKE_node.h:335
void(* labelfunc)(const struct bNodeTree *ntree, const struct bNode *node, char *label, int maxlen)
Definition: BKE_node.h:256
void(* draw_buttons)(struct uiLayout *, struct bContext *C, struct PointerRNA *ptr)
Definition: BKE_node.h:244
NodeMultiFunctionBuildFunction build_multi_function
Definition: BKE_node.h:313
NodeDeclareFunction declare
Definition: BKE_node.h:324
#define N_(msgid)
PointerRNA * ptr
Definition: wm_files.c:3480