Blender V4.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.add_input<decl::Geometry>("Instances").only_instances();
18 b.add_input<decl::Bool>("Selection").default_value(true).hide_value().field_on_all();
19 b.add_input<decl::Rotation>("Rotation").field_on_all();
20 b.add_input<decl::Vector>("Pivot Point").subtype(PROP_TRANSLATION).field_on_all();
21 b.add_input<decl::Bool>("Local Space").default_value(true).field_on_all();
22 b.add_output<decl::Geometry>("Instances").propagate_all();
23}
24
26{
27 using namespace blender::math;
28
29 const bke::InstancesFieldContext context{instances};
30 fn::FieldEvaluator evaluator{context, instances.instances_num()};
31 evaluator.set_selection(params.extract_input<Field<bool>>("Selection"));
32 evaluator.add(params.extract_input<Field<math::Quaternion>>("Rotation"));
33 evaluator.add(params.extract_input<Field<float3>>("Pivot Point"));
34 evaluator.add(params.extract_input<Field<bool>>("Local Space"));
35 evaluator.evaluate();
36
37 const IndexMask selection = evaluator.get_evaluated_selection_as_mask();
38 const VArray<math::Quaternion> rotations = evaluator.get_evaluated<math::Quaternion>(0);
39 const VArray<float3> pivots = evaluator.get_evaluated<float3>(1);
40 const VArray<bool> local_spaces = evaluator.get_evaluated<bool>(2);
41
42 MutableSpan<float4x4> transforms = instances.transforms_for_write();
43
44 selection.foreach_index(GrainSize(512), [&](const int64_t i) {
45 const float3 pivot = pivots[i];
46 const math::Quaternion rotation = rotations[i];
47 float4x4 &instance_transform = transforms[i];
48
49 float4x4 rotation_matrix;
50 float3 used_pivot;
51
52 if (local_spaces[i]) {
53 /* Find rotation axis from the matrix. This should work even if the instance is skewed. */
54 /* Create rotations around the individual axis. This could be optimized to skip some axis
55 * when the angle is zero. */
56 const EulerXYZ euler = math::to_euler(rotation);
57 const float3x3 rotation_x = from_rotation<float3x3>(
58 AxisAngle(normalize(instance_transform.x_axis()), euler.x()));
59 const float3x3 rotation_y = from_rotation<float3x3>(
60 AxisAngle(normalize(instance_transform.y_axis()), euler.y()));
61 const float3x3 rotation_z = from_rotation<float3x3>(
62 AxisAngle(normalize(instance_transform.z_axis()), euler.z()));
63
64 /* Combine the previously computed rotations into the final rotation matrix. */
65 rotation_matrix = float4x4(rotation_z * rotation_y * rotation_x);
66
67 /* Transform the passed in pivot into the local space of the instance. */
68 used_pivot = transform_point(instance_transform, pivot);
69 }
70 else {
71 used_pivot = pivot;
72 rotation_matrix = from_rotation<float4x4>(rotation);
73 }
74 /* Move the pivot to the origin so that we can rotate around it. */
75 instance_transform.location() -= used_pivot;
76 /* Perform the actual rotation. */
77 instance_transform = rotation_matrix * instance_transform;
78 /* Undo the pivot shifting done before. */
79 instance_transform.location() += used_pivot;
80 });
81}
82
84{
85 GeometrySet geometry_set = params.extract_input<GeometrySet>("Instances");
86 if (bke::Instances *instances = geometry_set.get_instances_for_write()) {
87 rotate_instances(params, *instances);
88 }
89 params.set_output("Instances", std::move(geometry_set));
90}
91
92static void node_register()
93{
94 static blender::bke::bNodeType ntype;
95
96 geo_node_type_base(&ntype, GEO_NODE_ROTATE_INSTANCES, "Rotate Instances", NODE_CLASS_GEOMETRY);
98 ntype.declare = node_declare;
100}
102
103} // namespace blender::nodes::node_geo_rotate_instances_cc
#define NODE_CLASS_GEOMETRY
Definition BKE_node.hh:418
#define NOD_REGISTER_NODE(REGISTER_FUNC)
@ PROP_TRANSLATION
Definition RNA_types.hh:164
SIMD_FORCE_INLINE btVector3 & normalize()
Normalize this vector x^2 + y^2 + z^2 = 1.
Definition btVector3.h:303
void set_selection(Field< bool > selection)
Definition FN_field.hh:385
local_group_size(16, 16) .push_constant(Type b
uiWidgetBaseParameters params[MAX_WIDGET_BASE_BATCH]
void node_register_type(bNodeType *ntype)
Definition node.cc:1708
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
void geo_node_type_base(blender::bke::bNodeType *ntype, int type, const char *name, short nclass)
__int64 int64_t
Definition stdint.h:89
Instances * get_instances_for_write()
Defines a node type.
Definition BKE_node.hh:218
NodeGeometryExecFunction geometry_node_execute
Definition BKE_node.hh:339
NodeDeclareFunction declare
Definition BKE_node.hh:347
CCL_NAMESPACE_END CCL_NAMESPACE_BEGIN ccl_device_inline float3 transform_point(ccl_private const Transform *t, const float3 a)
Definition transform.h:63