Blender  V3.3
node_geo_rotate_instances.cc
Go to the documentation of this file.
1 /* SPDX-License-Identifier: GPL-2.0-or-later */
2 
3 #include "BLI_task.hh"
4 
5 #include "node_geometry_util.hh"
6 
8 
10 {
11  b.add_input<decl::Geometry>(N_("Instances")).only_instances();
12  b.add_input<decl::Bool>(N_("Selection")).default_value(true).hide_value().supports_field();
13  b.add_input<decl::Vector>(N_("Rotation")).subtype(PROP_EULER).supports_field();
14  b.add_input<decl::Vector>(N_("Pivot Point")).subtype(PROP_TRANSLATION).supports_field();
15  b.add_input<decl::Bool>(N_("Local Space")).default_value(true).supports_field();
16  b.add_output<decl::Geometry>(N_("Instances"));
17 }
18 
19 static void rotate_instances(GeoNodeExecParams &params, InstancesComponent &instances_component)
20 {
21  GeometryComponentFieldContext field_context{instances_component, ATTR_DOMAIN_INSTANCE};
22  const int domain_num = instances_component.instances_num();
23 
24  fn::FieldEvaluator evaluator{field_context, domain_num};
25  evaluator.set_selection(params.extract_input<Field<bool>>("Selection"));
26  evaluator.add(params.extract_input<Field<float3>>("Rotation"));
27  evaluator.add(params.extract_input<Field<float3>>("Pivot Point"));
28  evaluator.add(params.extract_input<Field<bool>>("Local Space"));
29  evaluator.evaluate();
30 
31  const IndexMask selection = evaluator.get_evaluated_selection_as_mask();
32  const VArray<float3> rotations = evaluator.get_evaluated<float3>(0);
33  const VArray<float3> pivots = evaluator.get_evaluated<float3>(1);
34  const VArray<bool> local_spaces = evaluator.get_evaluated<bool>(2);
35 
36  MutableSpan<float4x4> instance_transforms = instances_component.instance_transforms();
37 
38  threading::parallel_for(selection.index_range(), 512, [&](IndexRange range) {
39  for (const int i_selection : range) {
40  const int i = selection[i_selection];
41  const float3 pivot = pivots[i];
42  const float3 euler = rotations[i];
43  float4x4 &instance_transform = instance_transforms[i];
44 
45  float4x4 rotation_matrix;
46  float3 used_pivot;
47 
48  if (local_spaces[i]) {
49  /* Find rotation axis from the matrix. This should work even if the instance is skewed. */
50  const float3 rotation_axis_x = instance_transform.values[0];
51  const float3 rotation_axis_y = instance_transform.values[1];
52  const float3 rotation_axis_z = instance_transform.values[2];
53 
54  /* Create rotations around the individual axis. This could be optimized to skip some axis
55  * when the angle is zero. */
56  float rotation_x[3][3], rotation_y[3][3], rotation_z[3][3];
57  axis_angle_to_mat3(rotation_x, rotation_axis_x, euler.x);
58  axis_angle_to_mat3(rotation_y, rotation_axis_y, euler.y);
59  axis_angle_to_mat3(rotation_z, rotation_axis_z, euler.z);
60 
61  /* Combine the previously computed rotations into the final rotation matrix. */
62  float rotation[3][3];
63  mul_m3_series(rotation, rotation_z, rotation_y, rotation_x);
64  copy_m4_m3(rotation_matrix.values, rotation);
65 
66  /* Transform the passed in pivot into the local space of the instance. */
67  used_pivot = instance_transform * pivot;
68  }
69  else {
70  used_pivot = pivot;
71  eul_to_mat4(rotation_matrix.values, euler);
72  }
73  /* Move the pivot to the origin so that we can rotate around it. */
74  sub_v3_v3(instance_transform.values[3], used_pivot);
75  /* Perform the actual rotation. */
76  mul_m4_m4_pre(instance_transform.values, rotation_matrix.values);
77  /* Undo the pivot shifting done before. */
78  add_v3_v3(instance_transform.values[3], used_pivot);
79  }
80  });
81 }
82 
84 {
85  GeometrySet geometry_set = params.extract_input<GeometrySet>("Instances");
86  if (geometry_set.has_instances()) {
88  rotate_instances(params, instances);
89  }
90  params.set_output("Instances", std::move(geometry_set));
91 }
92 
93 } // namespace blender::nodes::node_geo_rotate_instances_cc
94 
96 {
98 
99  static bNodeType ntype;
100 
104  nodeRegisterType(&ntype);
105 }
@ ATTR_DOMAIN_INSTANCE
Definition: BKE_attribute.h:32
#define NODE_CLASS_GEOMETRY
Definition: BKE_node.h:359
#define GEO_NODE_ROTATE_INSTANCES
Definition: BKE_node.h:1464
void nodeRegisterType(struct bNodeType *ntype)
Definition: node.cc:1357
@ PROP_EULER
Definition: RNA_types.h:159
@ PROP_TRANSLATION
Definition: RNA_types.h:154
blender::MutableSpan< blender::float4x4 > instance_transforms()
IndexRange index_range() const
uiWidgetBaseParameters params[MAX_WIDGET_BASE_BATCH]
static int domain_num(const CurvesGeometry &curves, const eAttrDomain domain)
static void rotate_instances(GeoNodeExecParams &params, InstancesComponent &instances_component)
static void node_geo_exec(GeoNodeExecParams params)
static void node_declare(NodeDeclarationBuilder &b)
void parallel_for(IndexRange range, int64_t grain_size, const Function &function)
Definition: BLI_task.hh:51
static const pxr::TfToken b("b", pxr::TfToken::Immortal)
MutableSpan< float3 > rotations
void register_node_type_geo_rotate_instances()
void geo_node_type_base(bNodeType *ntype, int type, const char *name, short nclass)
GeometryComponent & get_component_for_write(GeometryComponentType component_type)
bool has_instances() const
Defines a node type.
Definition: BKE_node.h:226
NodeGeometryExecFunction geometry_node_execute
Definition: BKE_node.h:316
NodeDeclareFunction declare
Definition: BKE_node.h:324
#define N_(msgid)