Blender V5.3
node_fn_combine_transform.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2023 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
5#include "BLI_math_matrix.hh"
7
10
11#include "node_function_util.hh"
12#include "node_shader_util.hh"
13
15
17{
18 b.is_function_node();
19 b.add_input<decl::Vector>("Translation"_ustr).subtype(PROP_TRANSLATION);
20 b.add_input<decl::Rotation>("Rotation"_ustr);
21 b.add_input<decl::Vector>("Scale"_ustr).default_value(float3(1)).subtype(PROP_XYZ);
22 b.add_output<decl::Matrix>("Transform"_ustr);
23}
24
25class CombineTransformFunction : public mf::MultiFunction {
26 public:
28 {
29 static const mf::Signature signature = []() {
30 mf::Signature signature;
31 mf::SignatureBuilder builder{"Combine Transform", signature};
32 builder.single_input<float3>("Translation");
33 builder.single_input<math::Quaternion>("Rotation");
34 builder.single_input<float3>("Scale");
35 builder.single_output<float4x4>("Transform");
36 return signature;
37 }();
39 }
40
41 void call(const IndexMask &mask, mf::Params params, mf::Context /*context*/) const override
42 {
43 const VArray translation = params.readonly_single_input<float3>(0, "Translation");
44 const VArray rotation = params.readonly_single_input<math::Quaternion>(1, "Rotation");
45 const VArray scale = params.readonly_single_input<float3>(2, "Scale");
46 MutableSpan transforms = params.uninitialized_single_output<float4x4>(3, "Transform");
47
48 const std::optional<float3> translation_single = translation.get_if_single();
49 const std::optional<math::Quaternion> rotation_single = rotation.get_if_single();
50 const std::optional<float3> scale_single = scale.get_if_single();
51
52 const bool no_translation = translation_single && math::is_zero(*translation_single);
53 const bool no_rotation = rotation_single && math::angle_of(*rotation_single).radian() < 1e-7f;
54 const bool no_scale = scale_single && math::is_equal(*scale_single, float3(1), 1e-7f);
55
56 if (no_rotation && no_scale) {
57 mask.foreach_index(
58 [&](const int64_t i) { transforms[i] = math::from_location<float4x4>(translation[i]); });
59 }
60 else if (no_translation && no_scale) {
61 mask.foreach_index(
62 [&](const int64_t i) { transforms[i] = math::from_rotation<float4x4>(rotation[i]); });
63 }
64 else if (no_translation && no_rotation) {
65 mask.foreach_index(
66 [&](const int64_t i) { transforms[i] = math::from_scale<float4x4>(scale[i]); });
67 }
68 else {
69 mask.foreach_index([&](const int64_t i) {
70 transforms[i] = math::from_loc_rot_scale<float4x4>(translation[i], rotation[i], scale[i]);
71 });
72 }
73 }
74};
75
81
83{
84 using namespace value_elem;
85 MatrixElem matrix_elem;
86 matrix_elem.translation = params.get_input_elem<VectorElem>("Translation"_ustr);
87 matrix_elem.rotation = params.get_input_elem<RotationElem>("Rotation"_ustr);
88 matrix_elem.scale = params.get_input_elem<VectorElem>("Scale"_ustr);
89 params.set_output_elem("Transform"_ustr, matrix_elem);
90}
91
93{
94 using namespace value_elem;
95 const MatrixElem matrix_elem = params.get_output_elem<MatrixElem>("Transform"_ustr);
96 params.set_input_elem("Translation"_ustr, matrix_elem.translation);
97 params.set_input_elem("Rotation"_ustr, matrix_elem.rotation);
98 params.set_input_elem("Scale"_ustr, matrix_elem.scale);
99}
100
102{
103 const float4x4 transform = params.get_output<float4x4>("Transform"_ustr);
104 float3 translation;
105 math::Quaternion rotation;
107 math::to_loc_rot_scale_safe<true>(transform, translation, rotation, scale);
108 params.set_input("Translation"_ustr, translation);
109 params.set_input("Rotation"_ustr, rotation);
110 params.set_input("Scale"_ustr, scale);
111}
112
114 bNode *node,
115 bNodeExecData * /*execdata*/,
118{
119 return GPU_stack_link(mat, node, "combine_transform", in, out);
120}
121
122static void node_register()
123{
124 static bke::bNodeType ntype;
125 fn_cmp_node_type_base(&ntype, "FunctionNodeCombineTransform"_ustr, FN_NODE_COMBINE_TRANSFORM);
126 ntype.ui_name = "Combine Transform";
127 ntype.ui_description =
128 "Combine a translation vector, a rotation, and a scale vector into a transformation matrix";
129 ntype.enum_name_legacy = "COMBINE_TRANSFORM";
131 ntype.declare = node_declare;
138}
140
141} // namespace blender::nodes::node_fn_combine_transform_cc
#define NODE_CLASS_CONVERTER
Definition BKE_node.hh:484
#define FN_NODE_COMBINE_TRANSFORM
#define NOD_REGISTER_NODE(REGISTER_FUNC)
std::optional< T > get_if_single() const
void set_signature(const Signature *signature)
void set_matching_fn(const mf::MultiFunction *fn)
void set_input(const UString identifier, T value)
void call(const IndexMask &mask, mf::Params params, mf::Context) const override
T get_input_elem(const UString identifier) const
void set_output_elem(const UString identifier, T elem)
void set_input_elem(const UString identifier, T elem)
float3x3 scale(float3x3 mat, float2 scale)
void node_register_type(bNodeType &ntype)
Definition node.cc:2920
QuaternionBase< float > Quaternion
bool is_zero(const T &a)
AngleRadianBase< T > angle_of(const QuaternionBase< T > &q)
MatT from_scale(const VecBase< typename MatT::base_type, ScaleDim > &scale)
bool is_equal(const MatBase< T, NumCol, NumRow > &a, const MatBase< T, NumCol, NumRow > &b, const T epsilon=T(0))
void to_loc_rot_scale_safe(const MatBase< T, 4, 4 > &mat, VecBase< T, 3 > &r_location, RotationT &r_rotation, VecBase< T, 3 > &r_scale)
MatT from_location(const typename MatT::loc_type &location)
MatT from_rotation(const RotationT &rotation)
MatT from_loc_rot_scale(const typename MatT::loc_type &location, const RotationT &rotation, const VecBase< typename MatT::base_type, ScaleDim > &scale)
static void node_eval_elem(value_elem::ElemEvalParams &params)
static int node_gpu_material(GPUMaterial *mat, bNode *node, bNodeExecData *, GPUNodeStack *in, GPUNodeStack *out)
static void node_eval_inverse(inverse_eval::InverseEvalParams &params)
static void node_eval_inverse_elem(value_elem::InverseElemEvalParams &params)
static void node_declare(NodeDeclarationBuilder &b)
static void node_build_multi_function(NodeMultiFunctionBuilder &builder)
bool GPU_stack_link(GPUMaterial *mat, const bNode *node, const char *name, GPUNodeStack *in, GPUNodeStack *out,...)
__int64 int64_t
Definition BKE_text.h:90
void fn_cmp_node_type_base(bke::bNodeType *ntype, UString idname, const std::optional< int16_t > legacy_type)
MatBase< float, 4, 4 > float4x4
VecBase< float, 3 > float3
@ PROP_TRANSLATION
Definition RNA_types.hh:318
Defines a node type.
Definition BKE_node.hh:265
NodeInverseElemEvalFunction eval_inverse_elem
Definition BKE_node.hh:434
NodeInverseEvalFunction eval_inverse
Definition BKE_node.hh:441
std::string ui_description
Definition BKE_node.hh:271
NodeGPUExecFunction gpu_fn
Definition BKE_node.hh:373
NodeElemEvalFunction eval_elem
Definition BKE_node.hh:428
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