Blender V5.3
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
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
23namespace blender {
24
26
28{
29 b.is_function_node();
30 b.use_custom_socket_order();
31 b.allow_any_socket_order();
32 b.add_default_layout();
33 b.add_input<decl::Vector>("Vector"_ustr).min(0.0f).max(1.0f).hide_value();
34 b.add_output<decl::Vector>("Vector"_ustr).align_with_previous();
35 b.add_input<decl::Vector>("Center"_ustr).description("Point to rotate around");
36 b.add_input<decl::Vector>("Axis"_ustr)
37 .min(-1.0f)
38 .max(1.0f)
39 .default_value({0.0f, 0.0f, 1.0f})
40 .make_available([](bNode &node) { node.custom1 = NODE_VECTOR_ROTATE_TYPE_AXIS; })
41 .description("Axis to rotate around");
42 b.add_input<decl::Float>("Angle"_ustr)
43 .subtype(PROP_ANGLE)
44 .description("Angle to rotate the input vector by");
45 b.add_input<decl::Vector>("Rotation"_ustr)
46 .subtype(PROP_EULER)
48 .description(
49 "The amount of rotation on each axis, around the X, Y, then Z axes in that order");
50}
51
53{
54 layout.prop(ptr, "rotation_type", ui::ITEM_R_SPLIT_EMPTY_NAME, std::nullopt, ICON_NONE);
55 layout.prop(ptr, "invert", ui::ITEM_R_SPLIT_EMPTY_NAME, std::nullopt, ICON_NONE);
56}
57
58static const char *gpu_shader_get_name(int mode)
59{
60 switch (mode) {
62 return "node_vector_rotate_axis_angle";
64 return "node_vector_rotate_axis_x";
66 return "node_vector_rotate_axis_y";
68 return "node_vector_rotate_axis_z";
70 return "node_vector_rotate_euler_xyz";
71 }
72
73 return nullptr;
74}
75
77 bNode *node,
78 bNodeExecData * /*execdata*/,
81{
82 const char *name = gpu_shader_get_name(node->custom1);
83
84 if (name != nullptr) {
85 float invert = (node->custom2) ? -1.0 : 1.0;
86 return GPU_stack_link(mat, node, name, in, out, GPU_constant(&invert));
87 }
88
89 return 0;
90}
91
93 const float3 &center,
94 const float3 &axis,
95 const float angle)
96{
97 float3 result = vector - center;
98 float mat[3][3];
99 axis_angle_to_mat3(mat, axis, angle);
100 mul_m3_v3(mat, result);
101 return result + center;
102}
103
105 const float3 &center,
106 const float3 &rotation,
107 const bool invert)
108{
109 float mat[3][3];
110 float3 result = vector - center;
111 eul_to_mat3(mat, rotation);
112 if (invert) {
113 invert_m3(mat);
114 }
115 mul_m3_v3(mat, result);
116 return result + center;
117}
118
119static const mf::MultiFunction *get_multi_function(const bNode &node)
120{
121 bool invert = node.custom2;
122 const int mode = node.custom1;
123
124 switch (mode) {
126 if (invert) {
127 static auto fn = mf::build::SI4_SO<float3, float3, float3, float, float3>(
128 "Rotate Axis",
129 [](const float3 &in, const float3 &center, const float3 &axis, float angle) {
130 return sh_node_vector_rotate_around_axis(in, center, axis, -angle);
131 });
132 return &fn;
133 }
134 static auto fn = mf::build::SI4_SO<float3, float3, float3, float, float3>(
135 "Rotate Axis",
136 [](const float3 &in, const float3 &center, const float3 &axis, float angle) {
137 return sh_node_vector_rotate_around_axis(in, center, axis, angle);
138 });
139 return &fn;
140 }
142 float3 axis = float3(1.0f, 0.0f, 0.0f);
143 if (invert) {
144 static auto fn = mf::build::SI3_SO<float3, float3, float, float3>(
145 "Rotate X-Axis", [=](const float3 &in, const float3 &center, float angle) {
146 return sh_node_vector_rotate_around_axis(in, center, axis, -angle);
147 });
148 return &fn;
149 }
150 static auto fn = mf::build::SI3_SO<float3, float3, float, float3>(
151 "Rotate X-Axis", [=](const float3 &in, const float3 &center, float angle) {
152 return sh_node_vector_rotate_around_axis(in, center, axis, angle);
153 });
154 return &fn;
155 }
157 float3 axis = float3(0.0f, 1.0f, 0.0f);
158 if (invert) {
159 static auto fn = mf::build::SI3_SO<float3, float3, float, float3>(
160 "Rotate Y-Axis", [=](const float3 &in, const float3 &center, float angle) {
161 return sh_node_vector_rotate_around_axis(in, center, axis, -angle);
162 });
163 return &fn;
164 }
165 static auto fn = mf::build::SI3_SO<float3, float3, float, float3>(
166 "Rotate Y-Axis", [=](const float3 &in, const float3 &center, float angle) {
167 return sh_node_vector_rotate_around_axis(in, center, axis, angle);
168 });
169 return &fn;
170 }
172 float3 axis = float3(0.0f, 0.0f, 1.0f);
173 if (invert) {
174 static auto fn = mf::build::SI3_SO<float3, float3, float, float3>(
175 "Rotate Z-Axis", [=](const float3 &in, const float3 &center, float angle) {
176 return sh_node_vector_rotate_around_axis(in, center, axis, -angle);
177 });
178 return &fn;
179 }
180 static auto fn = mf::build::SI3_SO<float3, float3, float, float3>(
181 "Rotate Z-Axis", [=](const float3 &in, const float3 &center, float angle) {
182 return sh_node_vector_rotate_around_axis(in, center, axis, angle);
183 });
184 return &fn;
185 }
187 if (invert) {
188 static auto fn = mf::build::SI3_SO<float3, float3, float3, float3>(
189 "Rotate Euler", [](const float3 &in, const float3 &center, const float3 &rotation) {
190 return sh_node_vector_rotate_euler(in, center, rotation, true);
191 });
192 return &fn;
193 }
194 static auto fn = mf::build::SI3_SO<float3, float3, float3, float3>(
195 "Rotate Euler", [](const float3 &in, const float3 &center, const float3 &rotation) {
196 return sh_node_vector_rotate_euler(in, center, rotation, false);
197 });
198 return &fn;
199 }
200 default:
202 return nullptr;
203 }
204}
205
207{
208 const mf::MultiFunction *fn = get_multi_function(builder.node());
209 builder.set_matching_fn(fn);
210}
211
213{
214 bNodeSocket *sock_rotation = bke::node_find_socket(*node, SOCK_IN, "Rotation"_ustr);
216 *ntree, *sock_rotation, ELEM(node->custom1, NODE_VECTOR_ROTATE_TYPE_EULER_XYZ));
217 bNodeSocket *sock_axis = bke::node_find_socket(*node, SOCK_IN, "Axis"_ustr);
219 *ntree, *sock_axis, ELEM(node->custom1, NODE_VECTOR_ROTATE_TYPE_AXIS));
220 bNodeSocket *sock_angle = bke::node_find_socket(*node, SOCK_IN, "Angle"_ustr);
222 *ntree, *sock_angle, !ELEM(node->custom1, NODE_VECTOR_ROTATE_TYPE_EULER_XYZ));
223}
224
226#ifdef WITH_MATERIALX
227{
228 int mode = node_->custom1;
229 bool invert = node_->custom2;
230
231 NodeItem vector = get_input_value("Vector", NodeItem::Type::Vector3);
232 NodeItem center = get_input_value("Center", NodeItem::Type::Vector3) *
233 val(MaterialX::Vector3(1.0f, 1.0f, -1.0f));
234 vector = vector - center;
235
237 NodeItem rotation = get_input_value("Rotation", NodeItem::Type::Vector3) *
238 val(MaterialX::Vector3(1.0f, 1.0f, -1.0f) * 180.0f / M_PI);
239
240 return vector.rotate(invert ? -rotation : rotation, invert) + center;
241 }
242
243 NodeItem angle = get_input_value("Angle", NodeItem::Type::Float) * val(float(180.0f / M_PI));
244 NodeItem axis = empty();
245 switch (mode) {
247 axis = get_input_value("Axis", NodeItem::Type::Vector3) *
248 val(MaterialX::Vector3(1.0f, 1.0f, -1.0f));
249 break;
251 axis = val(MaterialX::Vector3(1.0f, 0.0f, 0.0f));
252 break;
254 axis = val(MaterialX::Vector3(0.0f, 1.0f, 0.0f));
255 break;
257 axis = val(MaterialX::Vector3(0.0f, 0.0f, -1.0f));
258 break;
259 default:
261 }
262
263 return vector.rotate(invert ? -angle : angle, axis) + center;
264}
265#endif
267
268} // namespace nodes::node_shader_vector_rotate_cc
269
271{
272 namespace file_ns = nodes::node_shader_vector_rotate_cc;
273
274 static bke::bNodeType ntype;
275
276 common_node_type_base(&ntype, "ShaderNodeVectorRotate"_ustr, SH_NODE_VECTOR_ROTATE);
277 ntype.ui_name = "Vector Rotate";
278 ntype.ui_description = "Rotate a vector around a pivot point (center)";
279 ntype.enum_name_legacy = "VECTOR_ROTATE";
281 ntype.declare = file_ns::sh_node_vector_rotate_declare;
282 ntype.draw_buttons = file_ns::node_shader_buts_vector_rotate;
283 ntype.gpu_fn = file_ns::gpu_shader_vector_rotate;
284 ntype.updatefunc = file_ns::node_shader_update_vector_rotate;
285 ntype.build_multi_function = file_ns::sh_node_vector_rotate_build_multi_function;
286 ntype.materialx_fn = file_ns::node_shader_materialx;
287
289}
290
291} // namespace blender
#define NODE_CLASS_OP_VECTOR
Definition BKE_node.hh:481
#define SH_NODE_VECTOR_ROTATE
#define M_PI
#define BLI_assert_unreachable()
#define ELEM(...)
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 mf::MultiFunction *fn)
void make_available(bNode &node) const
float2x2 invert(float2x2 mat)
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
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(ui::Layout &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)
BMesh Iterators.
bool GPU_stack_link(GPUMaterial *mat, const bNode *node, const char *name, GPUNodeStack *in, GPUNodeStack *out,...)
void mul_m3_v3(const float M[3][3], float r[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)
GPUNodeLink * GPU_constant(const float *num)
@ NODE_VECTOR_ROTATE_TYPE_AXIS_Y
@ NODE_VECTOR_ROTATE_TYPE_EULER_XYZ
@ NODE_VECTOR_ROTATE_TYPE_AXIS_Z
@ NODE_VECTOR_ROTATE_TYPE_AXIS
@ NODE_VECTOR_ROTATE_TYPE_AXIS_X
bool invert_m3(float mat[3][3])
VecBase< float, 3 > float3
void register_node_type_sh_vector_rotate()
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
Defines a node type.
Definition BKE_node.hh:265
NodeMaterialXFunction materialx_fn
Definition BKE_node.hh:375
void(*) draw_buttons(ui::Layout &, bContext *C, PointerRNA *ptr)
Definition BKE_node.hh:290
std::string ui_description
Definition BKE_node.hh:271
void(*) updatefunc(bNodeTree *ntree, bNode *node)
Definition BKE_node.hh:312
NodeGPUExecFunction gpu_fn
Definition BKE_node.hh:373
NodeMultiFunctionBuildFunction build_multi_function
Definition BKE_node.hh:382
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)