From 78445ebd5fd99444741abee1406aa8d318b5a269 Mon Sep 17 00:00:00 2001 From: Erik Abrahamsson Date: Wed, 13 Oct 2021 09:42:10 -0500 Subject: Geometry Nodes: Rotate Instances Node Adds a node that can rotate each of a geometry's instances in global (to the modifier object) or local space (of each point) by a specified angle around a pivot point. In the future, separating the local-global choice for the pivot and the rotation might be useful. However, for now the node is kept simple. Differential Revision: https://developer.blender.org/D12682 --- source/blender/blenkernel/BKE_node.h | 1 + source/blender/blenkernel/intern/node.cc | 1 + source/blender/nodes/CMakeLists.txt | 1 + source/blender/nodes/NOD_geometry.h | 1 + source/blender/nodes/NOD_static_types.h | 1 + .../geometry/nodes/node_geo_rotate_instances.cc | 99 ++++++++++++++++++++++ 6 files changed, 104 insertions(+) create mode 100644 source/blender/nodes/geometry/nodes/node_geo_rotate_instances.cc (limited to 'source/blender') diff --git a/source/blender/blenkernel/BKE_node.h b/source/blender/blenkernel/BKE_node.h index 4c1c49555b8..b83d8d0e5e6 100644 --- a/source/blender/blenkernel/BKE_node.h +++ b/source/blender/blenkernel/BKE_node.h @@ -1534,6 +1534,7 @@ int ntreeTexExecTree(struct bNodeTree *ntree, #define GEO_NODE_SET_MATERIAL_INDEX 1119 #define GEO_NODE_TRANSLATE_INSTANCES 1120 #define GEO_NODE_SCALE_INSTANCES 1121 +#define GEO_NODE_ROTATE_INSTANCES 1122 /** \} */ diff --git a/source/blender/blenkernel/intern/node.cc b/source/blender/blenkernel/intern/node.cc index 283e28ed54f..16435ea6c58 100644 --- a/source/blender/blenkernel/intern/node.cc +++ b/source/blender/blenkernel/intern/node.cc @@ -5809,6 +5809,7 @@ static void registerGeometryNodes() register_node_type_geo_proximity(); register_node_type_geo_raycast(); register_node_type_geo_realize_instances(); + register_node_type_geo_rotate_instances(); register_node_type_geo_sample_texture(); register_node_type_geo_scale_instances(); register_node_type_geo_separate_components(); diff --git a/source/blender/nodes/CMakeLists.txt b/source/blender/nodes/CMakeLists.txt index e543db92b9f..19740a973a2 100644 --- a/source/blender/nodes/CMakeLists.txt +++ b/source/blender/nodes/CMakeLists.txt @@ -253,6 +253,7 @@ set(SRC geometry/nodes/node_geo_points_to_volume.cc geometry/nodes/node_geo_proximity.cc geometry/nodes/node_geo_realize_instances.cc + geometry/nodes/node_geo_rotate_instances.cc geometry/nodes/node_geo_scale_instances.cc geometry/nodes/node_geo_separate_components.cc geometry/nodes/node_geo_separate_geometry.cc diff --git a/source/blender/nodes/NOD_geometry.h b/source/blender/nodes/NOD_geometry.h index 3b0816300f5..f9b3c5ee09a 100644 --- a/source/blender/nodes/NOD_geometry.h +++ b/source/blender/nodes/NOD_geometry.h @@ -129,6 +129,7 @@ void register_node_type_geo_points_to_volume(void); void register_node_type_geo_proximity(void); void register_node_type_geo_raycast(void); void register_node_type_geo_realize_instances(void); +void register_node_type_geo_rotate_instances(void); void register_node_type_geo_sample_texture(void); void register_node_type_geo_scale_instances(void); void register_node_type_geo_select_by_handle_type(void); diff --git a/source/blender/nodes/NOD_static_types.h b/source/blender/nodes/NOD_static_types.h index 010327a1ac1..813e97be044 100644 --- a/source/blender/nodes/NOD_static_types.h +++ b/source/blender/nodes/NOD_static_types.h @@ -381,6 +381,7 @@ DefNode(GeometryNode, GEO_NODE_POINTS_TO_VERTICES, 0, "POINTS_TO_VERTICES", Poin DefNode(GeometryNode, GEO_NODE_POINTS_TO_VOLUME, def_geo_points_to_volume, "POINTS_TO_VOLUME", PointsToVolume, "Points to Volume", "") DefNode(GeometryNode, GEO_NODE_PROXIMITY, def_geo_proximity, "PROXIMITY", Proximity, "Geometry Proximity", "") DefNode(GeometryNode, GEO_NODE_REALIZE_INSTANCES, 0, "REALIZE_INSTANCES", RealizeInstances, "Realize Instances", "") +DefNode(GeometryNode, GEO_NODE_ROTATE_INSTANCES, 0, "ROTATE_INSTANCES", RotateInstances, "Rotate Instances", "") DefNode(GeometryNode, GEO_NODE_SCALE_INSTANCES, 0, "SCALE_INSTANCES", ScaleInstances, "Scale Instances", "") DefNode(GeometryNode, GEO_NODE_SEPARATE_COMPONENTS, 0, "SEPARATE_COMPONENTS", SeparateComponents, "Separate Components", "") DefNode(GeometryNode, GEO_NODE_SEPARATE_GEOMETRY, def_geo_separate_geometry, "SEPARATE_GEOMETRY", SeparateGeometry, "Separate Geometry", "") diff --git a/source/blender/nodes/geometry/nodes/node_geo_rotate_instances.cc b/source/blender/nodes/geometry/nodes/node_geo_rotate_instances.cc new file mode 100644 index 00000000000..09d92d7fa1e --- /dev/null +++ b/source/blender/nodes/geometry/nodes/node_geo_rotate_instances.cc @@ -0,0 +1,99 @@ +/* + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ + +#include "BLI_task.hh" + +#include "node_geometry_util.hh" + +namespace blender::nodes { + +static void geo_node_rotate_instances_declare(NodeDeclarationBuilder &b) +{ + b.add_input("Geometry"); + b.add_input("Selection").default_value(true).hide_value().supports_field(); + b.add_input("Rotation").subtype(PROP_EULER).supports_field(); + b.add_input("Pivot Point").subtype(PROP_TRANSLATION).supports_field(); + b.add_input("Local Space").default_value(true).supports_field(); + b.add_output("Geometry"); +}; + +static void rotate_instances(GeoNodeExecParams ¶ms, InstancesComponent &instances_component) +{ + GeometryComponentFieldContext field_context{instances_component, ATTR_DOMAIN_POINT}; + const int domain_size = instances_component.instances_amount(); + + fn::FieldEvaluator selection_evaluator{field_context, domain_size}; + selection_evaluator.add(params.extract_input>("Selection")); + selection_evaluator.evaluate(); + const IndexMask selection = selection_evaluator.get_evaluated_as_mask(0); + + fn::FieldEvaluator transforms_evaluator{field_context, &selection}; + transforms_evaluator.add(params.extract_input>("Rotation")); + transforms_evaluator.add(params.extract_input>("Pivot Point")); + transforms_evaluator.add(params.extract_input>("Local Space")); + transforms_evaluator.evaluate(); + const VArray &rotations = transforms_evaluator.get_evaluated(0); + const VArray &pivots = transforms_evaluator.get_evaluated(1); + const VArray &local_spaces = transforms_evaluator.get_evaluated(2); + + MutableSpan instance_transforms = instances_component.instance_transforms(); + + threading::parallel_for(selection.index_range(), 512, [&](IndexRange range) { + for (const int i_selection : range) { + const int i = selection[i_selection]; + const float3 pivot = pivots[i]; + float4x4 &instance_transform = instance_transforms[i]; + const float4x4 rotation_matrix = float4x4::from_loc_eul_scale( + {0, 0, 0}, rotations[i], {1, 1, 1}); + + if (local_spaces[i]) { + instance_transform *= float4x4::from_location(pivot); + instance_transform *= rotation_matrix; + instance_transform *= float4x4::from_location(-pivot); + } + else { + const float4x4 orgiginal_transform = instance_transform; + instance_transform = float4x4::from_location(pivot); + instance_transform *= rotation_matrix; + instance_transform *= float4x4::from_location(-pivot); + instance_transform *= orgiginal_transform; + } + } + }); +} + +static void geo_node_rotate_instances_exec(GeoNodeExecParams params) +{ + GeometrySet geometry_set = params.extract_input("Geometry"); + if (geometry_set.has_instances()) { + InstancesComponent &instances = geometry_set.get_component_for_write(); + rotate_instances(params, instances); + } + params.set_output("Geometry", std::move(geometry_set)); +} + +} // namespace blender::nodes + +void register_node_type_geo_rotate_instances() +{ + static bNodeType ntype; + + geo_node_type_base( + &ntype, GEO_NODE_ROTATE_INSTANCES, "Rotate Instances", NODE_CLASS_GEOMETRY, 0); + ntype.geometry_node_execute = blender::nodes::geo_node_rotate_instances_exec; + ntype.declare = blender::nodes::geo_node_rotate_instances_declare; + nodeRegisterType(&ntype); +} -- cgit v1.2.3