From 38c7378949b3323471c6e0c30842331db3b85af7 Mon Sep 17 00:00:00 2001 From: Johnny Matthews Date: Sun, 23 Jan 2022 12:29:37 -0600 Subject: Geometry Nodes: Relative Handle Position Mode Add a boolean option to have the Curve Handle Position input node return the position of the handle relative to each point position. Differential Revision: https://developer.blender.org/D12947 --- .../geometry/nodes/node_geo_input_curve_handles.cc | 80 +++++++++++++++++++++- 1 file changed, 78 insertions(+), 2 deletions(-) diff --git a/source/blender/nodes/geometry/nodes/node_geo_input_curve_handles.cc b/source/blender/nodes/geometry/nodes/node_geo_input_curve_handles.cc index c3c26736e88..fbf6b521fd8 100644 --- a/source/blender/nodes/geometry/nodes/node_geo_input_curve_handles.cc +++ b/source/blender/nodes/geometry/nodes/node_geo_input_curve_handles.cc @@ -14,20 +14,96 @@ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ +#include "BKE_spline.hh" + #include "node_geometry_util.hh" namespace blender::nodes::node_geo_input_curve_handles_cc { static void node_declare(NodeDeclarationBuilder &b) { + b.add_input(N_("Relative")).default_value(false).supports_field(); b.add_output(N_("Left")).field_source(); b.add_output(N_("Right")).field_source(); } +class HandlePositionFieldInput final : public GeometryFieldInput { + Field relative_; + bool left_; + + public: + HandlePositionFieldInput(Field relative, bool left) + : GeometryFieldInput(CPPType::get(), "Handle"), relative_(relative), left_(left) + { + } + + GVArray get_varray_for_context(const GeometryComponent &component, + const AttributeDomain domain, + IndexMask mask) const final + { + if (component.type() != GEO_COMPONENT_TYPE_CURVE) { + return {}; + } + + GeometryComponentFieldContext field_context{component, ATTR_DOMAIN_POINT}; + fn::FieldEvaluator evaluator(field_context, &mask); + evaluator.add(relative_); + evaluator.evaluate(); + const VArray &relative = evaluator.get_evaluated(0); + + VArray positions = component.attribute_get_for_read( + "position", ATTR_DOMAIN_POINT, {0, 0, 0}); + + StringRef side = left_ ? "handle_left" : "handle_right"; + VArray handles = component.attribute_get_for_read( + side, ATTR_DOMAIN_POINT, {0, 0, 0}); + + if (relative.is_single()) { + if (relative.get_internal_single()) { + Array output(positions.size()); + for (const int i : positions.index_range()) { + output[i] = handles[i] - positions[i]; + } + return component.attribute_try_adapt_domain( + VArray::ForContainer(std::move(output)), ATTR_DOMAIN_POINT, domain); + } + return component.attribute_try_adapt_domain(handles, ATTR_DOMAIN_POINT, domain); + } + + Array output(positions.size()); + for (const int i : positions.index_range()) { + if (relative[i]) { + output[i] = handles[i] - positions[i]; + } + else { + output[i] = handles[i]; + } + } + return component.attribute_try_adapt_domain( + VArray::ForContainer(std::move(output)), ATTR_DOMAIN_POINT, domain); + } + + uint64_t hash() const override + { + return get_default_hash_2(relative_, left_); + } + + bool is_equal_to(const fn::FieldNode &other) const override + { + if (const HandlePositionFieldInput *other_handle = + dynamic_cast(&other)) { + return relative_ == other_handle->relative_ && left_ == other_handle->left_; + } + return false; + } +}; + static void node_geo_exec(GeoNodeExecParams params) { - Field left_field = AttributeFieldInput::Create("handle_left"); - Field right_field = AttributeFieldInput::Create("handle_right"); + Field relative = params.extract_input>("Relative"); + Field left_field{std::make_shared(relative, true)}; + Field right_field{std::make_shared(relative, false)}; + params.set_output("Left", std::move(left_field)); params.set_output("Right", std::move(right_field)); } -- cgit v1.2.3