Blender V4.5
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").only_instances();
20 b.add_output<decl::Geometry>("Instances").propagate_all().align_with_previous();
21 b.add_input<decl::Bool>("Selection").default_value(true).hide_value().field_on_all();
22 b.add_input<decl::Rotation>("Rotation").field_on_all();
23 b.add_input<decl::Vector>("Pivot Point").subtype(PROP_TRANSLATION).field_on_all();
24 b.add_input<decl::Bool>("Local Space").default_value(true).field_on_all();
25}
26
28{
29 using namespace blender::math;
30
31 const bke::InstancesFieldContext context{instances};
32 fn::FieldEvaluator evaluator{context, instances.instances_num()};
33 evaluator.set_selection(params.extract_input<Field<bool>>("Selection"));
34 evaluator.add(params.extract_input<Field<math::Quaternion>>("Rotation"));
35 evaluator.add(params.extract_input<Field<float3>>("Pivot Point"));
36 evaluator.add(params.extract_input<Field<bool>>("Local Space"));
37 evaluator.evaluate();
38
39 const IndexMask selection = evaluator.get_evaluated_selection_as_mask();
40 const VArray<math::Quaternion> rotations = evaluator.get_evaluated<math::Quaternion>(0);
41 const VArray<float3> pivots = evaluator.get_evaluated<float3>(1);
42 const VArray<bool> local_spaces = evaluator.get_evaluated<bool>(2);
43
44 MutableSpan<float4x4> transforms = instances.transforms_for_write();
45
46 selection.foreach_index(GrainSize(512), [&](const int64_t i) {
47 const float3 pivot = pivots[i];
48 const math::Quaternion rotation = rotations[i];
49 float4x4 &instance_transform = transforms[i];
50
51 float4x4 rotation_matrix;
52 float3 used_pivot;
53
54 if (local_spaces[i]) {
55 /* Find rotation axis from the matrix. This should work even if the instance is skewed. */
56 /* Create rotations around the individual axis. This could be optimized to skip some axis
57 * when the angle is zero. */
58 const EulerXYZ euler = math::to_euler(rotation);
59 const float3x3 rotation_x = from_rotation<float3x3>(
60 AxisAngle(normalize(instance_transform.x_axis()), euler.x()));
61 const float3x3 rotation_y = from_rotation<float3x3>(
62 AxisAngle(normalize(instance_transform.y_axis()), euler.y()));
63 const float3x3 rotation_z = from_rotation<float3x3>(
64 AxisAngle(normalize(instance_transform.z_axis()), euler.z()));
65
66 /* Combine the previously computed rotations into the final rotation matrix. */
67 rotation_matrix = float4x4(rotation_z * rotation_y * rotation_x);
68
69 /* Transform the passed in pivot into the local space of the instance. */
70 used_pivot = transform_point(instance_transform, pivot);
71 }
72 else {
73 used_pivot = pivot;
74 rotation_matrix = from_rotation<float4x4>(rotation);
75 }
76 /* Move the pivot to the origin so that we can rotate around it. */
77 instance_transform.location() -= used_pivot;
78 /* Perform the actual rotation. */
79 instance_transform = rotation_matrix * instance_transform;
80 /* Undo the pivot shifting done before. */
81 instance_transform.location() += used_pivot;
82 });
83}
84
86{
87 GeometrySet geometry_set = params.extract_input<GeometrySet>("Instances");
88 if (bke::Instances *instances = geometry_set.get_instances_for_write()) {
89 rotate_instances(params, *instances);
90 }
91 params.set_output("Instances", std::move(geometry_set));
92}
93
94static void node_register()
95{
96 static blender::bke::bNodeType ntype;
97
98 geo_node_type_base(&ntype, "GeometryNodeRotateInstances", GEO_NODE_ROTATE_INSTANCES);
99 ntype.ui_name = "Rotate Instances";
100 ntype.ui_description = "Rotate geometry instances in local or global space";
101 ntype.enum_name_legacy = "ROTATE_INSTANCES";
104 ntype.declare = node_declare;
106}
108
109} // namespace blender::nodes::node_geo_rotate_instances_cc
#define NODE_CLASS_GEOMETRY
Definition BKE_node.hh:447
#define GEO_NODE_ROTATE_INSTANCES
#define NOD_REGISTER_NODE(REGISTER_FUNC)
@ PROP_TRANSLATION
Definition RNA_types.hh:249
long long int int64_t
int instances_num() const
Definition instances.cc:398
MutableSpan< float4x4 > transforms_for_write()
Definition instances.cc:240
void set_selection(Field< bool > selection)
Definition FN_field.hh:383
int add(GField field, GVArray *varray_ptr)
Definition field.cc:751
IndexMask get_evaluated_selection_as_mask() const
Definition field.cc:817
const GVArray & get_evaluated(const int field_index) const
Definition FN_field.hh:448
void foreach_index(Fn &&fn) const
VecBase< float, D > normalize(VecOp< float, D >) RET
uiWidgetBaseParameters params[MAX_WIDGET_BASE_BATCH]
void node_register_type(bNodeType &ntype)
Definition node.cc:2748
QuaternionBase< float > Quaternion
AxisAngleBase< float, AngleRadianBase< float > > AxisAngle
EulerXYZBase< float > EulerXYZ
MatT from_rotation(const RotationT &rotation)
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)
MatBase< float, 4, 4 > float4x4
MatBase< float, 3, 3 > float3x3
VecBase< float, 3 > float3
void geo_node_type_base(blender::bke::bNodeType *ntype, std::string idname, const std::optional< int16_t > legacy_type)
Instances * get_instances_for_write()
Defines a node type.
Definition BKE_node.hh:226
std::string ui_description
Definition BKE_node.hh:232
NodeGeometryExecFunction geometry_node_execute
Definition BKE_node.hh:347
const char * enum_name_legacy
Definition BKE_node.hh:235
NodeDeclareFunction declare
Definition BKE_node.hh:355
i
Definition text_draw.cc:230
ccl_device_inline float3 transform_point(const ccl_private Transform *t, const float3 a)
Definition transform.h:56