Blender  V3.3
node_shader_vector_rotate.cc
Go to the documentation of this file.
1 /* SPDX-License-Identifier: GPL-2.0-or-later
2  * Copyright 2013 Blender Foundation. All rights reserved. */
3 
8 #include "node_shader_util.hh"
9 
10 #include "UI_interface.h"
11 #include "UI_resources.h"
12 
14 
16 {
17  b.is_function_node();
18  b.add_input<decl::Vector>(N_("Vector")).min(0.0f).max(1.0f).hide_value();
19  b.add_input<decl::Vector>(N_("Center"));
20  b.add_input<decl::Vector>(N_("Axis"))
21  .min(-1.0f)
22  .max(1.0f)
23  .default_value({0.0f, 0.0f, 1.0f})
24  .make_available([](bNode &node) { node.custom1 = NODE_VECTOR_ROTATE_TYPE_AXIS; });
25  b.add_input<decl::Float>(N_("Angle")).subtype(PROP_ANGLE);
26  b.add_input<decl::Vector>(N_("Rotation")).subtype(PROP_EULER).make_available([](bNode &node) {
28  });
29  b.add_output<decl::Vector>(N_("Vector"));
30 }
31 
33 {
34  uiItemR(layout, ptr, "rotation_type", UI_ITEM_R_SPLIT_EMPTY_NAME, nullptr, ICON_NONE);
35  uiItemR(layout, ptr, "invert", UI_ITEM_R_SPLIT_EMPTY_NAME, nullptr, 0);
36 }
37 
38 static const char *gpu_shader_get_name(int mode)
39 {
40  switch (mode) {
42  return "node_vector_rotate_axis_angle";
44  return "node_vector_rotate_axis_x";
46  return "node_vector_rotate_axis_y";
48  return "node_vector_rotate_axis_z";
50  return "node_vector_rotate_euler_xyz";
51  }
52 
53  return nullptr;
54 }
55 
57  bNode *node,
58  bNodeExecData *UNUSED(execdata),
59  GPUNodeStack *in,
61 {
62  const char *name = gpu_shader_get_name(node->custom1);
63 
64  if (name != nullptr) {
65  float invert = (node->custom2) ? -1.0 : 1.0;
66  return GPU_stack_link(mat, node, name, in, out, GPU_constant(&invert));
67  }
68 
69  return 0;
70 }
71 
73  const float3 &center,
74  const float3 &axis,
75  const float angle)
76 {
78  float mat[3][3];
79  axis_angle_to_mat3(mat, axis, angle);
80  mul_m3_v3(mat, result);
81  return result + center;
82 }
83 
85  const float3 &center,
86  const float3 &rotation,
87  const bool invert)
88 {
89  float mat[3][3];
91  eul_to_mat3(mat, rotation);
92  if (invert) {
93  invert_m3(mat);
94  }
95  mul_m3_v3(mat, result);
96  return result + center;
97 }
98 
100 {
101  bool invert = node.custom2;
102  const int mode = node.custom1;
103 
104  switch (mode) {
106  if (invert) {
108  "Rotate Axis",
109  [](const float3 &in, const float3 &center, const float3 &axis, float angle) {
110  return sh_node_vector_rotate_around_axis(in, center, axis, -angle);
111  }};
112  return &fn;
113  }
115  "Rotate Axis",
116  [](const float3 &in, const float3 &center, const float3 &axis, float angle) {
118  }};
119  return &fn;
120  }
122  float3 axis = float3(1.0f, 0.0f, 0.0f);
123  if (invert) {
125  "Rotate X-Axis", [=](const float3 &in, const float3 &center, float angle) {
126  return sh_node_vector_rotate_around_axis(in, center, axis, -angle);
127  }};
128  return &fn;
129  }
131  "Rotate X-Axis", [=](const float3 &in, const float3 &center, float angle) {
133  }};
134  return &fn;
135  }
137  float3 axis = float3(0.0f, 1.0f, 0.0f);
138  if (invert) {
140  "Rotate Y-Axis", [=](const float3 &in, const float3 &center, float angle) {
141  return sh_node_vector_rotate_around_axis(in, center, axis, -angle);
142  }};
143  return &fn;
144  }
146  "Rotate Y-Axis", [=](const float3 &in, const float3 &center, float angle) {
148  }};
149  return &fn;
150  }
152  float3 axis = float3(0.0f, 0.0f, 1.0f);
153  if (invert) {
155  "Rotate Z-Axis", [=](const float3 &in, const float3 &center, float angle) {
156  return sh_node_vector_rotate_around_axis(in, center, axis, -angle);
157  }};
158  return &fn;
159  }
161  "Rotate Z-Axis", [=](const float3 &in, const float3 &center, float angle) {
163  }};
164  return &fn;
165  }
167  if (invert) {
169  "Rotate Euler", [](const float3 &in, const float3 &center, const float3 &rotation) {
170  return sh_node_vector_rotate_euler(in, center, rotation, true);
171  }};
172  return &fn;
173  }
175  "Rotate Euler", [](const float3 &in, const float3 &center, const float3 &rotation) {
176  return sh_node_vector_rotate_euler(in, center, rotation, false);
177  }};
178  return &fn;
179  }
180  default:
182  return nullptr;
183  }
184 }
185 
187 {
188  const fn::MultiFunction *fn = get_multi_function(builder.node());
189  builder.set_matching_fn(fn);
190 }
191 
193 {
194  bNodeSocket *sock_rotation = nodeFindSocket(node, SOCK_IN, "Rotation");
196  ntree, sock_rotation, ELEM(node->custom1, NODE_VECTOR_ROTATE_TYPE_EULER_XYZ));
197  bNodeSocket *sock_axis = nodeFindSocket(node, SOCK_IN, "Axis");
199  bNodeSocket *sock_angle = nodeFindSocket(node, SOCK_IN, "Angle");
201  ntree, sock_angle, !ELEM(node->custom1, NODE_VECTOR_ROTATE_TYPE_EULER_XYZ));
202 }
203 
204 } // namespace blender::nodes::node_shader_vector_rotate_cc
205 
207 {
209 
210  static bNodeType ntype;
211 
218 
219  nodeRegisterType(&ntype);
220 }
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
#define SH_NODE_VECTOR_ROTATE
Definition: BKE_node.h:1179
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
#define BLI_assert_unreachable()
Definition: BLI_assert.h:93
void mul_m3_v3(const float M[3][3], float r[3])
Definition: math_matrix.c:926
bool invert_m3(float R[3][3])
Definition: math_matrix.c:1171
void eul_to_mat3(float mat[3][3], const float eul[3])
void axis_angle_to_mat3(float R[3][3], const float axis[3], float angle)
#define UNUSED(x)
#define ELEM(...)
@ NODE_VECTOR_ROTATE_TYPE_AXIS
@ NODE_VECTOR_ROTATE_TYPE_AXIS_Z
@ NODE_VECTOR_ROTATE_TYPE_AXIS_X
@ NODE_VECTOR_ROTATE_TYPE_EULER_XYZ
@ NODE_VECTOR_ROTATE_TYPE_AXIS_Y
@ SOCK_IN
NSNotificationCenter * center
GPUNodeLink * GPU_constant(const float *num)
bool GPU_stack_link(GPUMaterial *mat, struct bNode *node, const char *name, GPUNodeStack *in, GPUNodeStack *out,...)
@ PROP_ANGLE
Definition: RNA_types.h:145
@ PROP_EULER
Definition: RNA_types.h:159
@ UI_ITEM_R_SPLIT_EMPTY_NAME
void uiItemR(uiLayout *layout, struct PointerRNA *ptr, const char *propname, int flag, const char *name, int icon)
SIMD_FORCE_INLINE btScalar angle(const btVector3 &v) const
Return the angle between this and another vector.
Definition: btVector3.h:356
void set_matching_fn(const MultiFunction *fn)
OperationNode * node
bNodeTree * ntree
CCL_NAMESPACE_BEGIN ccl_device float invert(float color, float factor)
Definition: invert.h:8
static float3 sh_node_vector_rotate_around_axis(const float3 &vector, const float3 &center, const float3 &axis, const float angle)
static void sh_node_vector_rotate_declare(NodeDeclarationBuilder &b)
static void sh_node_vector_rotate_build_multi_function(NodeMultiFunctionBuilder &builder)
static void node_shader_buts_vector_rotate(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
static float3 sh_node_vector_rotate_euler(const float3 &vector, const float3 &center, const float3 &rotation, const bool invert)
static const fn::MultiFunction * get_multi_function(bNode &node)
static int gpu_shader_vector_rotate(GPUMaterial *mat, bNode *node, bNodeExecData *UNUSED(execdata), GPUNodeStack *in, GPUNodeStack *out)
static void node_shader_update_vector_rotate(bNodeTree *ntree, bNode *node)
vec_base< float, 3 > float3
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_vector_rotate()
#define min(a, b)
Definition: sort.c:35
Defines a node type.
Definition: BKE_node.h:226
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