diff options
author | Jacques Lucke <jacques@blender.org> | 2021-01-12 14:55:14 +0300 |
---|---|---|
committer | Jacques Lucke <jacques@blender.org> | 2021-01-12 14:55:14 +0300 |
commit | 3b77bd48f9c63aebd5c8bc4a126d496f503c75a7 (patch) | |
tree | 0b45925a0b6ef0aa963732ec4dacf6fce16803bc /source/blender/nodes | |
parent | 3a254b93fd8597c47fc4fe55fa0417d1f9fa85fc (diff) |
Geometry Nodes: new Align Rotation to Vector node
This adds a new Align Rotation to Vector node based on the mockup
in T83669.
Reviewers: HooglyBoogly, simonthommes
Differential Revision: https://developer.blender.org/D10081
Diffstat (limited to 'source/blender/nodes')
-rw-r--r-- | source/blender/nodes/CMakeLists.txt | 1 | ||||
-rw-r--r-- | source/blender/nodes/NOD_geometry.h | 1 | ||||
-rw-r--r-- | source/blender/nodes/NOD_static_types.h | 1 | ||||
-rw-r--r-- | source/blender/nodes/geometry/nodes/node_geo_align_rotation_to_vector.cc | 149 |
4 files changed, 152 insertions, 0 deletions
diff --git a/source/blender/nodes/CMakeLists.txt b/source/blender/nodes/CMakeLists.txt index de4d23d8577..64667faa735 100644 --- a/source/blender/nodes/CMakeLists.txt +++ b/source/blender/nodes/CMakeLists.txt @@ -140,6 +140,7 @@ set(SRC function/nodes/node_fn_switch.cc function/node_function_util.cc + geometry/nodes/node_geo_align_rotation_to_vector.cc geometry/nodes/node_geo_attribute_color_ramp.cc geometry/nodes/node_geo_attribute_compare.cc geometry/nodes/node_geo_attribute_fill.cc diff --git a/source/blender/nodes/NOD_geometry.h b/source/blender/nodes/NOD_geometry.h index 93123c9a684..4cd9bce1b9d 100644 --- a/source/blender/nodes/NOD_geometry.h +++ b/source/blender/nodes/NOD_geometry.h @@ -44,6 +44,7 @@ void register_node_type_geo_attribute_compare(void); void register_node_type_geo_attribute_mix(void); void register_node_type_geo_attribute_color_ramp(void); void register_node_type_geo_rotate_points(void); +void register_node_type_geo_align_rotation_to_vector(void); #ifdef __cplusplus } diff --git a/source/blender/nodes/NOD_static_types.h b/source/blender/nodes/NOD_static_types.h index 55b06cb31a5..8eeb179ed0d 100644 --- a/source/blender/nodes/NOD_static_types.h +++ b/source/blender/nodes/NOD_static_types.h @@ -286,6 +286,7 @@ DefNode(GeometryNode, GEO_NODE_POINT_SEPARATE, 0, "POINT_SEPARATE", PointSeparat DefNode(GeometryNode, GEO_NODE_ATTRIBUTE_COMPARE, def_geo_attribute_attribute_compare, "ATTRIBUTE_COMPARE", AttributeCompare, "Attribute Compare", "") DefNode(GeometryNode, GEO_NODE_ROTATE_POINTS, def_geo_rotate_points, "ROTATE_POINTS", RotatePoints, "Rotate Points", "") DefNode(GeometryNode, GEO_NODE_ATTRIBUTE_VECTOR_MATH, def_geo_attribute_vector_math, "ATTRIBUTE_VECTOR_MATH", AttributeVectorMath, "Attribute Vector Math", "") +DefNode(GeometryNode, GEO_NODE_ALIGN_ROTATION_TO_VECTOR, def_geo_align_rotation_to_vector, "ALIGN_ROTATION_TO_VECTOR", AlignRotationToVector, "Align Rotation to Vector", "") /* undefine macros */ #undef DefNode diff --git a/source/blender/nodes/geometry/nodes/node_geo_align_rotation_to_vector.cc b/source/blender/nodes/geometry/nodes/node_geo_align_rotation_to_vector.cc new file mode 100644 index 00000000000..c38f54ce7f7 --- /dev/null +++ b/source/blender/nodes/geometry/nodes/node_geo_align_rotation_to_vector.cc @@ -0,0 +1,149 @@ +/* + * 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 "node_geometry_util.hh" + +#include "BLI_math_rotation.h" + +static bNodeSocketTemplate geo_node_align_rotation_to_vector_in[] = { + {SOCK_GEOMETRY, N_("Geometry")}, + {SOCK_STRING, N_("Factor")}, + {SOCK_FLOAT, N_("Factor"), 1.0, 0.0, 0.0, 0.0, 0.0, 1.0, PROP_FACTOR}, + {SOCK_STRING, N_("Vector")}, + {SOCK_VECTOR, N_("Vector"), 0.0, 0.0, 1.0, 0.0, -FLT_MAX, FLT_MAX, PROP_ANGLE}, + {-1, ""}, +}; + +static bNodeSocketTemplate geo_node_align_rotation_to_vector_out[] = { + {SOCK_GEOMETRY, N_("Geometry")}, + {-1, ""}, +}; + +namespace blender::nodes { + +static void align_rotations_on_component(GeometryComponent &component, + const GeoNodeExecParams ¶ms) +{ + const bNode &node = params.node(); + const NodeGeometryAlignRotationToVector &storage = *(const NodeGeometryAlignRotationToVector *) + node.storage; + + WriteAttributePtr rotation_attribute = component.attribute_try_ensure_for_write( + "rotation", ATTR_DOMAIN_POINT, CD_PROP_FLOAT3); + if (!rotation_attribute) { + return; + } + MutableSpan<float3> rotations = rotation_attribute->get_span().typed<float3>(); + + FloatReadAttribute factors = params.get_input_attribute<float>( + "Factor", component, ATTR_DOMAIN_POINT, 1.0f); + Float3ReadAttribute vectors = params.get_input_attribute<float3>( + "Vector", component, ATTR_DOMAIN_POINT, {0, 0, 1}); + + float3 main_axis{0, 0, 0}; + main_axis[storage.axis] = 1; + + const int domain_size = component.attribute_domain_size(ATTR_DOMAIN_POINT); + for (const int i : IndexRange(domain_size)) { + const float3 vector = vectors[i]; + if (is_zero_v3(vector)) { + continue; + } + + float old_rotation[3][3]; + eul_to_mat3(old_rotation, rotations[i]); + float3 old_axis; + mul_v3_m3v3(old_axis, old_rotation, main_axis); + + const float3 new_axis = vector.normalized(); + const float3 rotation_axis = float3::cross_high_precision(old_axis, new_axis); + const float full_angle = angle_normalized_v3v3(old_axis, new_axis); + const float angle = factors[i] * full_angle; + + float rotation[3][3]; + axis_angle_to_mat3(rotation, rotation_axis, angle); + + float new_rotation_matrix[3][3]; + mul_m3_m3m3(new_rotation_matrix, rotation, old_rotation); + + float3 new_rotation; + mat3_to_eul(new_rotation, new_rotation_matrix); + + rotations[i] = new_rotation; + } + + rotation_attribute->apply_span(); +} + +static void geo_node_align_rotation_to_vector_exec(GeoNodeExecParams params) +{ + GeometrySet geometry_set = params.extract_input<GeometrySet>("Geometry"); + + if (geometry_set.has<MeshComponent>()) { + align_rotations_on_component(geometry_set.get_component_for_write<MeshComponent>(), params); + } + if (geometry_set.has<PointCloudComponent>()) { + align_rotations_on_component(geometry_set.get_component_for_write<PointCloudComponent>(), + params); + } + + params.set_output("Geometry", geometry_set); +} + +static void geo_node_align_rotation_to_vector_init(bNodeTree *UNUSED(ntree), bNode *node) +{ + NodeGeometryAlignRotationToVector *node_storage = (NodeGeometryAlignRotationToVector *) + MEM_callocN(sizeof(NodeGeometryAlignRotationToVector), __func__); + + node_storage->axis = GEO_NODE_ALIGN_ROTATION_TO_VECTOR_AXIS_X; + node_storage->input_type_factor = GEO_NODE_ATTRIBUTE_INPUT_FLOAT; + node_storage->input_type_vector = GEO_NODE_ATTRIBUTE_INPUT_VECTOR; + + node->storage = node_storage; +} + +static void geo_node_align_rotation_to_vector_update(bNodeTree *UNUSED(ntree), bNode *node) +{ + NodeGeometryAlignRotationToVector *node_storage = (NodeGeometryAlignRotationToVector *) + node->storage; + update_attribute_input_socket_availabilities( + *node, "Factor", (GeometryNodeAttributeInputMode)node_storage->input_type_factor); + update_attribute_input_socket_availabilities( + *node, "Vector", (GeometryNodeAttributeInputMode)node_storage->input_type_vector); +} + +} // namespace blender::nodes + +void register_node_type_geo_align_rotation_to_vector() +{ + static bNodeType ntype; + + geo_node_type_base(&ntype, + GEO_NODE_ALIGN_ROTATION_TO_VECTOR, + "Align Rotation to Vector", + NODE_CLASS_GEOMETRY, + 0); + node_type_socket_templates( + &ntype, geo_node_align_rotation_to_vector_in, geo_node_align_rotation_to_vector_out); + node_type_init(&ntype, blender::nodes::geo_node_align_rotation_to_vector_init); + node_type_update(&ntype, blender::nodes::geo_node_align_rotation_to_vector_update); + node_type_storage(&ntype, + "NodeGeometryAlignRotationToVector", + node_free_standard_storage, + node_copy_standard_storage); + ntype.geometry_node_execute = blender::nodes::geo_node_align_rotation_to_vector_exec; + nodeRegisterType(&ntype); +} |