Blender V4.5
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
20#include "UI_interface.hh"
21#include "UI_resources.hh"
22
24
26{
27 b.is_function_node();
28 b.add_input<decl::Vector>("Vector").min(0.0f).max(1.0f).hide_value();
29 b.add_input<decl::Vector>("Center").description("Point to rotate around");
30 b.add_input<decl::Vector>("Axis")
31 .min(-1.0f)
32 .max(1.0f)
33 .default_value({0.0f, 0.0f, 1.0f})
34 .make_available([](bNode &node) { node.custom1 = NODE_VECTOR_ROTATE_TYPE_AXIS; })
35 .description("Axis to rotate around");
36 b.add_input<decl::Float>("Angle")
37 .subtype(PROP_ANGLE)
38 .description("Angle to rotate the input vector by");
39 b.add_input<decl::Vector>("Rotation")
40 .subtype(PROP_EULER)
42 .description(
43 "The amount of rotation on each axis, around the X, Y, then Z axes in that order");
44 b.add_output<decl::Vector>("Vector");
45}
46
48{
49 layout->prop(ptr, "rotation_type", UI_ITEM_R_SPLIT_EMPTY_NAME, std::nullopt, ICON_NONE);
50 layout->prop(ptr, "invert", UI_ITEM_R_SPLIT_EMPTY_NAME, std::nullopt, ICON_NONE);
51}
52
53static const char *gpu_shader_get_name(int mode)
54{
55 switch (mode) {
57 return "node_vector_rotate_axis_angle";
59 return "node_vector_rotate_axis_x";
61 return "node_vector_rotate_axis_y";
63 return "node_vector_rotate_axis_z";
65 return "node_vector_rotate_euler_xyz";
66 }
67
68 return nullptr;
69}
70
72 bNode *node,
73 bNodeExecData * /*execdata*/,
76{
77 const char *name = gpu_shader_get_name(node->custom1);
78
79 if (name != nullptr) {
80 float invert = (node->custom2) ? -1.0 : 1.0;
81 return GPU_stack_link(mat, node, name, in, out, GPU_constant(&invert));
82 }
83
84 return 0;
85}
86
88 const float3 &center,
89 const float3 &axis,
90 const float angle)
91{
92 float3 result = vector - center;
93 float mat[3][3];
94 axis_angle_to_mat3(mat, axis, angle);
95 mul_m3_v3(mat, result);
96 return result + center;
97}
98
100 const float3 &center,
101 const float3 &rotation,
102 const bool invert)
103{
104 float mat[3][3];
105 float3 result = vector - center;
106 eul_to_mat3(mat, rotation);
107 if (invert) {
108 invert_m3(mat);
109 }
110 mul_m3_v3(mat, result);
111 return result + center;
112}
113
114static const mf::MultiFunction *get_multi_function(const bNode &node)
115{
116 bool invert = node.custom2;
117 const int mode = node.custom1;
118
119 switch (mode) {
121 if (invert) {
122 static auto fn = mf::build::SI4_SO<float3, float3, float3, float, float3>(
123 "Rotate Axis",
124 [](const float3 &in, const float3 &center, const float3 &axis, float angle) {
125 return sh_node_vector_rotate_around_axis(in, center, axis, -angle);
126 });
127 return &fn;
128 }
129 static auto fn = mf::build::SI4_SO<float3, float3, float3, float, float3>(
130 "Rotate Axis",
131 [](const float3 &in, const float3 &center, const float3 &axis, float angle) {
132 return sh_node_vector_rotate_around_axis(in, center, axis, angle);
133 });
134 return &fn;
135 }
137 float3 axis = float3(1.0f, 0.0f, 0.0f);
138 if (invert) {
139 static auto fn = mf::build::SI3_SO<float3, float3, float, float3>(
140 "Rotate X-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 }
145 static auto fn = mf::build::SI3_SO<float3, float3, float, float3>(
146 "Rotate X-Axis", [=](const float3 &in, const float3 &center, float angle) {
147 return sh_node_vector_rotate_around_axis(in, center, axis, angle);
148 });
149 return &fn;
150 }
152 float3 axis = float3(0.0f, 1.0f, 0.0f);
153 if (invert) {
154 static auto fn = mf::build::SI3_SO<float3, float3, float, float3>(
155 "Rotate Y-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 }
160 static auto fn = mf::build::SI3_SO<float3, float3, float, float3>(
161 "Rotate Y-Axis", [=](const float3 &in, const float3 &center, float angle) {
162 return sh_node_vector_rotate_around_axis(in, center, axis, angle);
163 });
164 return &fn;
165 }
167 float3 axis = float3(0.0f, 0.0f, 1.0f);
168 if (invert) {
169 static auto fn = mf::build::SI3_SO<float3, float3, float, float3>(
170 "Rotate Z-Axis", [=](const float3 &in, const float3 &center, float angle) {
171 return sh_node_vector_rotate_around_axis(in, center, axis, -angle);
172 });
173 return &fn;
174 }
175 static auto fn = mf::build::SI3_SO<float3, float3, float, float3>(
176 "Rotate Z-Axis", [=](const float3 &in, const float3 &center, float angle) {
177 return sh_node_vector_rotate_around_axis(in, center, axis, angle);
178 });
179 return &fn;
180 }
182 if (invert) {
183 static auto fn = mf::build::SI3_SO<float3, float3, float3, float3>(
184 "Rotate Euler", [](const float3 &in, const float3 &center, const float3 &rotation) {
185 return sh_node_vector_rotate_euler(in, center, rotation, true);
186 });
187 return &fn;
188 }
189 static auto fn = mf::build::SI3_SO<float3, float3, float3, float3>(
190 "Rotate Euler", [](const float3 &in, const float3 &center, const float3 &rotation) {
191 return sh_node_vector_rotate_euler(in, center, rotation, false);
192 });
193 return &fn;
194 }
195 default:
197 return nullptr;
198 }
199}
200
202{
203 const mf::MultiFunction *fn = get_multi_function(builder.node());
204 builder.set_matching_fn(fn);
205}
206
208{
209 bNodeSocket *sock_rotation = bke::node_find_socket(*node, SOCK_IN, "Rotation");
211 *ntree, *sock_rotation, ELEM(node->custom1, NODE_VECTOR_ROTATE_TYPE_EULER_XYZ));
212 bNodeSocket *sock_axis = bke::node_find_socket(*node, SOCK_IN, "Axis");
214 *ntree, *sock_axis, ELEM(node->custom1, NODE_VECTOR_ROTATE_TYPE_AXIS));
215 bNodeSocket *sock_angle = bke::node_find_socket(*node, SOCK_IN, "Angle");
217 *ntree, *sock_angle, !ELEM(node->custom1, NODE_VECTOR_ROTATE_TYPE_EULER_XYZ));
218}
219
221#ifdef WITH_MATERIALX
222{
223 int mode = node_->custom1;
224 bool invert = node_->custom2;
225
226 NodeItem vector = get_input_value("Vector", NodeItem::Type::Vector3);
227 NodeItem center = get_input_value("Center", NodeItem::Type::Vector3) *
228 val(MaterialX::Vector3(1.0f, 1.0f, -1.0f));
229 vector = vector - center;
230
232 NodeItem rotation = get_input_value("Rotation", NodeItem::Type::Vector3) *
233 val(MaterialX::Vector3(1.0f, 1.0f, -1.0f) * 180.0f / M_PI);
234
235 return vector.rotate(invert ? -rotation : rotation, invert) + center;
236 }
237
238 NodeItem angle = get_input_value("Angle", NodeItem::Type::Float) * val(float(180.0f / M_PI));
239 NodeItem axis = empty();
240 switch (mode) {
242 axis = get_input_value("Axis", NodeItem::Type::Vector3) *
243 val(MaterialX::Vector3(1.0f, 1.0f, -1.0f));
244 break;
246 axis = val(MaterialX::Vector3(1.0f, 0.0f, 0.0f));
247 break;
249 axis = val(MaterialX::Vector3(0.0f, 1.0f, 0.0f));
250 break;
252 axis = val(MaterialX::Vector3(0.0f, 0.0f, -1.0f));
253 break;
254 default:
256 }
257
258 return vector.rotate(invert ? -angle : angle, axis) + center;
259}
260#endif
262
263} // namespace blender::nodes::node_shader_vector_rotate_cc
264
266{
268
269 static blender::bke::bNodeType ntype;
270
271 common_node_type_base(&ntype, "ShaderNodeVectorRotate", SH_NODE_VECTOR_ROTATE);
272 ntype.ui_name = "Vector Rotate";
273 ntype.ui_description = "Rotate a vector around a pivot point (center)";
274 ntype.enum_name_legacy = "VECTOR_ROTATE";
276 ntype.declare = file_ns::sh_node_vector_rotate_declare;
277 ntype.draw_buttons = file_ns::node_shader_buts_vector_rotate;
278 ntype.gpu_fn = file_ns::gpu_shader_vector_rotate;
279 ntype.updatefunc = file_ns::node_shader_update_vector_rotate;
280 ntype.build_multi_function = file_ns::sh_node_vector_rotate_build_multi_function;
281 ntype.materialx_fn = file_ns::node_shader_materialx;
282
284}
#define NODE_CLASS_OP_VECTOR
Definition BKE_node.hh:436
#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:240
@ PROP_EULER
Definition RNA_types.hh:254
@ 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:2864
void node_register_type(bNodeType &ntype)
Definition node.cc:2748
void node_set_socket_availability(bNodeTree &ntree, bNodeSocket &sock, bool is_available)
Definition node.cc:5011
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()
#define min(a, b)
Definition sort.cc:36
int16_t custom1
int16_t custom2
Defines a node type.
Definition BKE_node.hh:226
NodeMaterialXFunction materialx_fn
Definition BKE_node.hh:332
std::string ui_description
Definition BKE_node.hh:232
NodeGPUExecFunction gpu_fn
Definition BKE_node.hh:330
NodeMultiFunctionBuildFunction build_multi_function
Definition BKE_node.hh:344
const char * enum_name_legacy
Definition BKE_node.hh:235
void(* draw_buttons)(uiLayout *, bContext *C, PointerRNA *ptr)
Definition BKE_node.hh:247
NodeDeclareFunction declare
Definition BKE_node.hh:355
void(* updatefunc)(bNodeTree *ntree, bNode *node)
Definition BKE_node.hh:269
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:4226