Blender V5.3
node_geo_rotate_instances.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#include "BLI_task.hh"
8
9#include "BKE_instances.hh"
10
11#include "node_geometry_util.hh"
12
14
16{
17 b.use_custom_socket_order();
18 b.allow_any_socket_order();
19 b.add_input<decl::Geometry>("Instances"_ustr)
20 .only_instances()
21 .description("Instances to rotate individually");
22 b.add_output<decl::Geometry>("Instances"_ustr).propagate_all_geometry().align_with_previous();
23 b.add_input<decl::Bool>("Selection"_ustr)
24 .default_value(true)
25 .hide_value()
26 .evaluated_geometry_field();
27 b.add_input<decl::Rotation>("Rotation"_ustr).evaluated_geometry_field();
28 b.add_input<decl::Vector>("Pivot Point"_ustr)
29 .subtype(PROP_TRANSLATION)
30 .evaluated_geometry_field();
31 b.add_input<decl::Bool>("Local Space"_ustr).default_value(true).evaluated_geometry_field();
32}
33
34static void rotate_instances(GeoNodeExecParams &params, bke::Instances &instances)
35{
36 using namespace blender::math;
37
38 const bke::InstancesFieldContext context{instances};
39 fn::FieldEvaluator evaluator{context, instances.instances_num()};
40 evaluator.set_selection(params.extract_input<Field<bool>>("Selection"_ustr));
41 evaluator.add(params.extract_input<Field<math::Quaternion>>("Rotation"_ustr));
42 evaluator.add(params.extract_input<Field<float3>>("Pivot Point"_ustr));
43 evaluator.add(params.extract_input<Field<bool>>("Local Space"_ustr));
44 evaluator.evaluate();
45
46 const IndexMask selection = evaluator.get_evaluated_selection_as_mask();
47 const VArray<math::Quaternion> rotations = evaluator.get_evaluated<math::Quaternion>(0);
48 const VArray<float3> pivots = evaluator.get_evaluated<float3>(1);
49 const VArray<bool> local_spaces = evaluator.get_evaluated<bool>(2);
50
51 MutableSpan<float4x4> transforms = instances.transforms_for_write();
52
53 selection.foreach_index(
54 [&](const int64_t i) {
55 const float3 pivot = pivots[i];
56 const math::Quaternion rotation = rotations[i];
57 float4x4 &instance_transform = transforms[i];
58
59 float4x4 rotation_matrix;
60 float3 used_pivot;
61
62 if (local_spaces[i]) {
63 /* Find rotation axis from the matrix. This should work even if the instance is skewed.
64 */
65 /* Create rotations around the individual axis. This could be optimized to skip some axis
66 * when the angle is zero. */
67 const EulerXYZ euler = math::to_euler(rotation);
68 const float3x3 rotation_x = from_rotation<float3x3>(
69 AxisAngle(normalize(instance_transform.x_axis()), euler.x()));
70 const float3x3 rotation_y = from_rotation<float3x3>(
71 AxisAngle(normalize(instance_transform.y_axis()), euler.y()));
72 const float3x3 rotation_z = from_rotation<float3x3>(
73 AxisAngle(normalize(instance_transform.z_axis()), euler.z()));
74
75 /* Combine the previously computed rotations into the final rotation matrix. */
76 rotation_matrix = float4x4(rotation_z * rotation_y * rotation_x);
77
78 /* Transform the passed in pivot into the local space of the instance. */
79 used_pivot = transform_point(instance_transform, pivot);
80 }
81 else {
82 used_pivot = pivot;
83 rotation_matrix = from_rotation<float4x4>(rotation);
84 }
85 /* Move the pivot to the origin so that we can rotate around it. */
86 instance_transform.location() -= used_pivot;
87 /* Perform the actual rotation. */
88 instance_transform = rotation_matrix * instance_transform;
89 /* Undo the pivot shifting done before. */
90 instance_transform.location() += used_pivot;
91 },
93}
94
96{
97 GeometrySet geometry_set = params.extract_input<GeometrySet>("Instances"_ustr);
98 if (bke::Instances *instances = geometry_set.get_instances_for_write()) {
99 rotate_instances(params, *instances);
100 }
101 params.set_output("Instances"_ustr, std::move(geometry_set));
102}
103
104static void node_register()
105{
106 static bke::bNodeType ntype;
107
108 geo_node_type_base(&ntype, "GeometryNodeRotateInstances"_ustr, GEO_NODE_ROTATE_INSTANCES);
109 ntype.ui_name = "Rotate Instances";
110 ntype.ui_description = "Rotate geometry instances in local or global space";
111 ntype.enum_name_legacy = "ROTATE_INSTANCES";
114 ntype.declare = node_declare;
116}
118
119} // namespace blender::nodes::node_geo_rotate_instances_cc
#define NODE_CLASS_GEOMETRY
Definition BKE_node.hh:492
#define GEO_NODE_ROTATE_INSTANCES
#define NOD_REGISTER_NODE(REGISTER_FUNC)
int instances_num() const
Definition instances.cc:408
MutableSpan< float4x4 > transforms_for_write()
Definition instances.cc:242
void set_selection(Field< bool > selection)
int add(GField field, GVArray *varray_ptr)
IndexMask get_evaluated_selection_as_mask() const
const GVArray & get_evaluated(const int field_index) const
void foreach_index(Fn &&fn, Mode mode=exec_mode::serial) const
T extract_input(const UString identifier)
void set_output(const UString identifier, T &&value)
VecBase< float, D > normalize(VecOp< float, D >) RET
float2x2 from_rotation(AngleRadian rotation)
float3 transform_point(float3x3 mat, float3 point)
void node_register_type(bNodeType &ntype)
Definition node.cc:2920
constexpr ParallelGrainSize grain_size(int grain_size)
QuaternionBase< float > Quaternion
Euler3Base< T > to_euler(const AxisAngleBase< T, AngleT > &axis_angle, EulerOrder order)
static void node_geo_exec(GeoNodeExecParams params)
static void rotate_instances(GeoNodeExecParams &params, bke::Instances &instances)
static void node_declare(NodeDeclarationBuilder &b)
__int64 int64_t
Definition BKE_text.h:90
MatBase< float, 4, 4 > float4x4
MatBase< float, 3, 3 > float3x3
VecBase< float, 3 > float3
@ PROP_TRANSLATION
Definition RNA_types.hh:318
void geo_node_type_base(bke::bNodeType *ntype, UString idname, const std::optional< int16_t > legacy_type)
Instances * get_instances_for_write()
Defines a node type.
Definition BKE_node.hh:265
std::string ui_description
Definition BKE_node.hh:271
NodeGeometryExecFunction geometry_node_execute
Definition BKE_node.hh:385
const char * enum_name_legacy
Definition BKE_node.hh:274
NodeDeclareFunction declare
Definition BKE_node.hh:393