Blender V5.0
node_shader_vector_rotate.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2013 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
9#include "BLI_math_matrix.h"
10#include "BLI_math_rotation.h"
12
13#include "FN_multi_function.hh"
15
16#include "NOD_multi_function.hh"
17
18#include "node_shader_util.hh"
19
21#include "UI_resources.hh"
22
24
26{
27 b.is_function_node();
28 b.use_custom_socket_order();
29 b.allow_any_socket_order();
30 b.add_default_layout();
31 b.add_input<decl::Vector>("Vector").min(0.0f).max(1.0f).hide_value();
32 b.add_output<decl::Vector>("Vector").align_with_previous();
33 b.add_input<decl::Vector>("Center").description("Point to rotate around");
34 b.add_input<decl::Vector>("Axis")
35 .min(-1.0f)
36 .max(1.0f)
37 .default_value({0.0f, 0.0f, 1.0f})
38 .make_available([](bNode &node) { node.custom1 = NODE_VECTOR_ROTATE_TYPE_AXIS; })
39 .description("Axis to rotate around");
40 b.add_input<decl::Float>("Angle")
41 .subtype(PROP_ANGLE)
42 .description("Angle to rotate the input vector by");
43 b.add_input<decl::Vector>("Rotation")
44 .subtype(PROP_EULER)
46 .description(
47 "The amount of rotation on each axis, around the X, Y, then Z axes in that order");
48}
49
51{
52 layout->prop(ptr, "rotation_type", UI_ITEM_R_SPLIT_EMPTY_NAME, std::nullopt, ICON_NONE);
53 layout->prop(ptr, "invert", UI_ITEM_R_SPLIT_EMPTY_NAME, std::nullopt, ICON_NONE);
54}
55
56static const char *gpu_shader_get_name(int mode)
57{
58 switch (mode) {
60 return "node_vector_rotate_axis_angle";
62 return "node_vector_rotate_axis_x";
64 return "node_vector_rotate_axis_y";
66 return "node_vector_rotate_axis_z";
68 return "node_vector_rotate_euler_xyz";
69 }
70
71 return nullptr;
72}
73
75 bNode *node,
76 bNodeExecData * /*execdata*/,
79{
80 const char *name = gpu_shader_get_name(node->custom1);
81
82 if (name != nullptr) {
83 float invert = (node->custom2) ? -1.0 : 1.0;
84 return GPU_stack_link(mat, node, name, in, out, GPU_constant(&invert));
85 }
86
87 return 0;
88}
89
91 const float3 &center,
92 const float3 &axis,
93 const float angle)
94{
95 float3 result = vector - center;
96 float mat[3][3];
97 axis_angle_to_mat3(mat, axis, angle);
98 mul_m3_v3(mat, result);
99 return result + center;
100}
101
103 const float3 &center,
104 const float3 &rotation,
105 const bool invert)
106{
107 float mat[3][3];
108 float3 result = vector - center;
109 eul_to_mat3(mat, rotation);
110 if (invert) {
111 invert_m3(mat);
112 }
113 mul_m3_v3(mat, result);
114 return result + center;
115}
116
117static const mf::MultiFunction *get_multi_function(const bNode &node)
118{
119 bool invert = node.custom2;
120 const int mode = node.custom1;
121
122 switch (mode) {
124 if (invert) {
125 static auto fn = mf::build::SI4_SO<float3, float3, float3, float, float3>(
126 "Rotate Axis",
127 [](const float3 &in, const float3 &center, const float3 &axis, float angle) {
128 return sh_node_vector_rotate_around_axis(in, center, axis, -angle);
129 });
130 return &fn;
131 }
132 static auto fn = mf::build::SI4_SO<float3, float3, float3, float, float3>(
133 "Rotate Axis",
134 [](const float3 &in, const float3 &center, const float3 &axis, float angle) {
135 return sh_node_vector_rotate_around_axis(in, center, axis, angle);
136 });
137 return &fn;
138 }
140 float3 axis = float3(1.0f, 0.0f, 0.0f);
141 if (invert) {
142 static auto fn = mf::build::SI3_SO<float3, float3, float, float3>(
143 "Rotate X-Axis", [=](const float3 &in, const float3 &center, float angle) {
144 return sh_node_vector_rotate_around_axis(in, center, axis, -angle);
145 });
146 return &fn;
147 }
148 static auto fn = mf::build::SI3_SO<float3, float3, float, float3>(
149 "Rotate X-Axis", [=](const float3 &in, const float3 &center, float angle) {
150 return sh_node_vector_rotate_around_axis(in, center, axis, angle);
151 });
152 return &fn;
153 }
155 float3 axis = float3(0.0f, 1.0f, 0.0f);
156 if (invert) {
157 static auto fn = mf::build::SI3_SO<float3, float3, float, float3>(
158 "Rotate Y-Axis", [=](const float3 &in, const float3 &center, float angle) {
159 return sh_node_vector_rotate_around_axis(in, center, axis, -angle);
160 });
161 return &fn;
162 }
163 static auto fn = mf::build::SI3_SO<float3, float3, float, float3>(
164 "Rotate Y-Axis", [=](const float3 &in, const float3 &center, float angle) {
165 return sh_node_vector_rotate_around_axis(in, center, axis, angle);
166 });
167 return &fn;
168 }
170 float3 axis = float3(0.0f, 0.0f, 1.0f);
171 if (invert) {
172 static auto fn = mf::build::SI3_SO<float3, float3, float, float3>(
173 "Rotate Z-Axis", [=](const float3 &in, const float3 &center, float angle) {
174 return sh_node_vector_rotate_around_axis(in, center, axis, -angle);
175 });
176 return &fn;
177 }
178 static auto fn = mf::build::SI3_SO<float3, float3, float, float3>(
179 "Rotate Z-Axis", [=](const float3 &in, const float3 &center, float angle) {
180 return sh_node_vector_rotate_around_axis(in, center, axis, angle);
181 });
182 return &fn;
183 }
185 if (invert) {
186 static auto fn = mf::build::SI3_SO<float3, float3, float3, float3>(
187 "Rotate Euler", [](const float3 &in, const float3 &center, const float3 &rotation) {
188 return sh_node_vector_rotate_euler(in, center, rotation, true);
189 });
190 return &fn;
191 }
192 static auto fn = mf::build::SI3_SO<float3, float3, float3, float3>(
193 "Rotate Euler", [](const float3 &in, const float3 &center, const float3 &rotation) {
194 return sh_node_vector_rotate_euler(in, center, rotation, false);
195 });
196 return &fn;
197 }
198 default:
200 return nullptr;
201 }
202}
203
205{
206 const mf::MultiFunction *fn = get_multi_function(builder.node());
207 builder.set_matching_fn(fn);
208}
209
211{
212 bNodeSocket *sock_rotation = bke::node_find_socket(*node, SOCK_IN, "Rotation");
214 *ntree, *sock_rotation, ELEM(node->custom1, NODE_VECTOR_ROTATE_TYPE_EULER_XYZ));
215 bNodeSocket *sock_axis = bke::node_find_socket(*node, SOCK_IN, "Axis");
217 *ntree, *sock_axis, ELEM(node->custom1, NODE_VECTOR_ROTATE_TYPE_AXIS));
218 bNodeSocket *sock_angle = bke::node_find_socket(*node, SOCK_IN, "Angle");
220 *ntree, *sock_angle, !ELEM(node->custom1, NODE_VECTOR_ROTATE_TYPE_EULER_XYZ));
221}
222
224#ifdef WITH_MATERIALX
225{
226 int mode = node_->custom1;
227 bool invert = node_->custom2;
228
229 NodeItem vector = get_input_value("Vector", NodeItem::Type::Vector3);
230 NodeItem center = get_input_value("Center", NodeItem::Type::Vector3) *
231 val(MaterialX::Vector3(1.0f, 1.0f, -1.0f));
232 vector = vector - center;
233
235 NodeItem rotation = get_input_value("Rotation", NodeItem::Type::Vector3) *
236 val(MaterialX::Vector3(1.0f, 1.0f, -1.0f) * 180.0f / M_PI);
237
238 return vector.rotate(invert ? -rotation : rotation, invert) + center;
239 }
240
241 NodeItem angle = get_input_value("Angle", NodeItem::Type::Float) * val(float(180.0f / M_PI));
242 NodeItem axis = empty();
243 switch (mode) {
245 axis = get_input_value("Axis", NodeItem::Type::Vector3) *
246 val(MaterialX::Vector3(1.0f, 1.0f, -1.0f));
247 break;
249 axis = val(MaterialX::Vector3(1.0f, 0.0f, 0.0f));
250 break;
252 axis = val(MaterialX::Vector3(0.0f, 1.0f, 0.0f));
253 break;
255 axis = val(MaterialX::Vector3(0.0f, 0.0f, -1.0f));
256 break;
257 default:
259 }
260
261 return vector.rotate(invert ? -angle : angle, axis) + center;
262}
263#endif
265
266} // namespace blender::nodes::node_shader_vector_rotate_cc
267
269{
271
272 static blender::bke::bNodeType ntype;
273
274 common_node_type_base(&ntype, "ShaderNodeVectorRotate", SH_NODE_VECTOR_ROTATE);
275 ntype.ui_name = "Vector Rotate";
276 ntype.ui_description = "Rotate a vector around a pivot point (center)";
277 ntype.enum_name_legacy = "VECTOR_ROTATE";
279 ntype.declare = file_ns::sh_node_vector_rotate_declare;
280 ntype.draw_buttons = file_ns::node_shader_buts_vector_rotate;
281 ntype.gpu_fn = file_ns::gpu_shader_vector_rotate;
282 ntype.updatefunc = file_ns::node_shader_update_vector_rotate;
283 ntype.build_multi_function = file_ns::sh_node_vector_rotate_build_multi_function;
284 ntype.materialx_fn = file_ns::node_shader_materialx;
285
287}
#define NODE_CLASS_OP_VECTOR
Definition BKE_node.hh:450
#define SH_NODE_VECTOR_ROTATE
#define BLI_assert_unreachable()
Definition BLI_assert.h:93
#define M_PI
void mul_m3_v3(const float M[3][3], float r[3])
bool invert_m3(float mat[3][3])
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 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
bool GPU_stack_link(GPUMaterial *mat, const bNode *node, const char *name, GPUNodeStack *in, GPUNodeStack *out,...)
GPUNodeLink * GPU_constant(const float *num)
static double angle(const Eigen::Vector3d &v1, const Eigen::Vector3d &v2)
Definition IK_Math.h:117
@ PROP_ANGLE
Definition RNA_types.hh:252
@ PROP_EULER
Definition RNA_types.hh:266
@ UI_ITEM_R_SPLIT_EMPTY_NAME
void set_matching_fn(const mf::MultiFunction *fn)
void make_available(bNode &node) const
#define in
#define out
CCL_NAMESPACE_BEGIN ccl_device float invert(const float color, const float factor)
Definition invert.h:11
bNodeSocket * node_find_socket(bNode &node, eNodeSocketInOut in_out, StringRef identifier)
Definition node.cc:2532
void node_register_type(bNodeType &ntype)
Definition node.cc:2416
void node_set_socket_availability(bNodeTree &ntree, bNodeSocket &sock, bool is_available)
Definition node.cc:4739
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 node_shader_buts_vector_rotate(uiLayout *layout, bContext *, PointerRNA *ptr)
static const mf::MultiFunction * get_multi_function(const bNode &node)
static void sh_node_vector_rotate_build_multi_function(NodeMultiFunctionBuilder &builder)
static float3 sh_node_vector_rotate_euler(const float3 &vector, const float3 &center, const float3 &rotation, const bool invert)
static int gpu_shader_vector_rotate(GPUMaterial *mat, bNode *node, bNodeExecData *, GPUNodeStack *in, GPUNodeStack *out)
static void node_shader_update_vector_rotate(bNodeTree *ntree, bNode *node)
VecBase< float, 3 > float3
#define NODE_SHADER_MATERIALX_BEGIN
#define NODE_SHADER_MATERIALX_END
void common_node_type_base(blender::bke::bNodeType *ntype, std::string idname, const std::optional< int16_t > legacy_type)
void register_node_type_sh_vector_rotate()
const char * name
#define min(a, b)
Definition sort.cc:36
int16_t custom1
int16_t custom2
Defines a node type.
Definition BKE_node.hh:238
NodeMaterialXFunction materialx_fn
Definition BKE_node.hh:344
std::string ui_description
Definition BKE_node.hh:244
NodeGPUExecFunction gpu_fn
Definition BKE_node.hh:342
NodeMultiFunctionBuildFunction build_multi_function
Definition BKE_node.hh:351
const char * enum_name_legacy
Definition BKE_node.hh:247
void(* draw_buttons)(uiLayout *, bContext *C, PointerRNA *ptr)
Definition BKE_node.hh:259
NodeDeclareFunction declare
Definition BKE_node.hh:362
void(* updatefunc)(bNodeTree *ntree, bNode *node)
Definition BKE_node.hh:281
void prop(PointerRNA *ptr, PropertyRNA *prop, int index, int value, eUI_Item_Flag flag, std::optional< blender::StringRef > name_opt, int icon, std::optional< blender::StringRef > placeholder=std::nullopt)
PointerRNA * ptr
Definition wm_files.cc:4238