Welcome to mirror list, hosted at ThFree Co, Russian Federation.

git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorHans Goudey <h.goudey@me.com>2021-09-21 21:11:39 +0300
committerHans Goudey <h.goudey@me.com>2021-09-21 21:11:39 +0300
commit4d51af68adb273a9e63cba196a71a404b07be95d (patch)
tree4fff8175e3eebd7f0326bed9e9b8828fff31ae31
parent29b13fa3cae61bff3707d6a3d844188f21dda06c (diff)
Geometry Nodes: Curve tangent node
This node outputs the direction vector, or tangent of a curve at every control point. For poly splines this is simply the evaluated tangents, so it all works very simply. For Bezier splines it uses the tangent at the evaluated point corresponding to each control point, and NURBS are interpereted as poly splines built from their control points. Internally the node is called "Input Tangent" to simplify using it for mesh tangents as well in the future like the "Normal" node. Differential Revision: https://developer.blender.org/D12581
-rw-r--r--release/scripts/startup/nodeitems_builtins.py1
-rw-r--r--source/blender/blenkernel/BKE_node.h1
-rw-r--r--source/blender/blenkernel/intern/node.cc1
-rw-r--r--source/blender/nodes/CMakeLists.txt1
-rw-r--r--source/blender/nodes/NOD_geometry.h1
-rw-r--r--source/blender/nodes/NOD_static_types.h1
-rw-r--r--source/blender/nodes/geometry/nodes/node_geo_input_tangent.cc174
7 files changed, 180 insertions, 0 deletions
diff --git a/release/scripts/startup/nodeitems_builtins.py b/release/scripts/startup/nodeitems_builtins.py
index b8bb4e551d2..ea9305caff3 100644
--- a/release/scripts/startup/nodeitems_builtins.py
+++ b/release/scripts/startup/nodeitems_builtins.py
@@ -527,6 +527,7 @@ geometry_node_categories = [
NodeItem("GeometryNodeCurveFill"),
NodeItem("GeometryNodeCurveTrim"),
NodeItem("GeometryNodeCurveLength"),
+ NodeItem("GeometryNodeInputTangent", poll=geometry_nodes_fields_poll),
NodeItem("GeometryNodeCurveSample", poll=geometry_nodes_fields_poll),
]),
GeometryNodeCategory("GEO_PRIMITIVES_CURVE", "Curve Primitives", items=[
diff --git a/source/blender/blenkernel/BKE_node.h b/source/blender/blenkernel/BKE_node.h
index e4e9a6eff3a..28ac27c09c6 100644
--- a/source/blender/blenkernel/BKE_node.h
+++ b/source/blender/blenkernel/BKE_node.h
@@ -1496,6 +1496,7 @@ int ntreeTexExecTree(struct bNodeTree *ntree,
#define GEO_NODE_REALIZE_INSTANCES 1083
#define GEO_NODE_ATTRIBUTE_STATISTIC 1084
#define GEO_NODE_CURVE_SAMPLE 1085
+#define GEO_NODE_INPUT_TANGENT 1086
/** \} */
diff --git a/source/blender/blenkernel/intern/node.cc b/source/blender/blenkernel/intern/node.cc
index e2891f00119..cfcd50dc482 100644
--- a/source/blender/blenkernel/intern/node.cc
+++ b/source/blender/blenkernel/intern/node.cc
@@ -5208,6 +5208,7 @@ static void registerGeometryNodes()
register_node_type_geo_input_material();
register_node_type_geo_input_normal();
register_node_type_geo_input_position();
+ register_node_type_geo_input_tangent();
register_node_type_geo_is_viewport();
register_node_type_geo_join_geometry();
register_node_type_geo_material_assign();
diff --git a/source/blender/nodes/CMakeLists.txt b/source/blender/nodes/CMakeLists.txt
index 842c76935d1..c60e68e08f1 100644
--- a/source/blender/nodes/CMakeLists.txt
+++ b/source/blender/nodes/CMakeLists.txt
@@ -195,6 +195,7 @@ set(SRC
geometry/nodes/node_geo_input_material.cc
geometry/nodes/node_geo_input_normal.cc
geometry/nodes/node_geo_input_position.cc
+ geometry/nodes/node_geo_input_tangent.cc
geometry/nodes/node_geo_input_index.cc
geometry/nodes/node_geo_is_viewport.cc
geometry/nodes/node_geo_join_geometry.cc
diff --git a/source/blender/nodes/NOD_geometry.h b/source/blender/nodes/NOD_geometry.h
index 63330b7df62..013ec638b82 100644
--- a/source/blender/nodes/NOD_geometry.h
+++ b/source/blender/nodes/NOD_geometry.h
@@ -81,6 +81,7 @@ void register_node_type_geo_input_index(void);
void register_node_type_geo_input_material(void);
void register_node_type_geo_input_normal(void);
void register_node_type_geo_input_position(void);
+void register_node_type_geo_input_tangent(void);
void register_node_type_geo_is_viewport(void);
void register_node_type_geo_join_geometry(void);
void register_node_type_geo_material_assign(void);
diff --git a/source/blender/nodes/NOD_static_types.h b/source/blender/nodes/NOD_static_types.h
index 918c82dec1c..832e73e1ff1 100644
--- a/source/blender/nodes/NOD_static_types.h
+++ b/source/blender/nodes/NOD_static_types.h
@@ -332,6 +332,7 @@ DefNode(GeometryNode, GEO_NODE_INPUT_INDEX, 0, "INDEX", InputIndex, "Index", "")
DefNode(GeometryNode, GEO_NODE_INPUT_MATERIAL, def_geo_input_material, "INPUT_MATERIAL", InputMaterial, "Material", "")
DefNode(GeometryNode, GEO_NODE_INPUT_NORMAL, 0, "INPUT_NORMAL", InputNormal, "Normal", "")
DefNode(GeometryNode, GEO_NODE_INPUT_POSITION, 0, "POSITION", InputPosition, "Position", "")
+DefNode(GeometryNode, GEO_NODE_INPUT_TANGENT, 0, "INPUT_TANGENT", InputTangent, "Curve Tangent", "")
DefNode(GeometryNode, GEO_NODE_IS_VIEWPORT, 0, "IS_VIEWPORT", IsViewport, "Is Viewport", "")
DefNode(GeometryNode, GEO_NODE_JOIN_GEOMETRY, 0, "JOIN_GEOMETRY", JoinGeometry, "Join Geometry", "")
DefNode(GeometryNode, GEO_NODE_MATERIAL_ASSIGN, 0, "MATERIAL_ASSIGN", MaterialAssign, "Material Assign", "")
diff --git a/source/blender/nodes/geometry/nodes/node_geo_input_tangent.cc b/source/blender/nodes/geometry/nodes/node_geo_input_tangent.cc
new file mode 100644
index 00000000000..68788709f1e
--- /dev/null
+++ b/source/blender/nodes/geometry/nodes/node_geo_input_tangent.cc
@@ -0,0 +1,174 @@
+/*
+ * 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 "BKE_spline.hh"
+
+#include "node_geometry_util.hh"
+
+namespace blender::nodes {
+
+static void geo_node_input_tangent_declare(NodeDeclarationBuilder &b)
+{
+ b.add_output<decl::Vector>("Tangent");
+}
+
+static void calculate_bezier_tangents(const BezierSpline &spline, MutableSpan<float3> tangents)
+{
+ Span<int> offsets = spline.control_point_offsets();
+ Span<float3> evaluated_tangents = spline.evaluated_tangents();
+ for (const int i : IndexRange(spline.size())) {
+ tangents[i] = evaluated_tangents[offsets[i]];
+ }
+}
+
+static void calculate_poly_tangents(const PolySpline &spline, MutableSpan<float3> tangents)
+{
+ tangents.copy_from(spline.evaluated_tangents());
+}
+
+/**
+ * Because NURBS control points are not necessarily on the path, the tangent at the control points
+ * is not well defined, so create a temporary poly spline to find the tangents. This requires extra
+ * copying currently, but may be more efficient in the future if attributes have some form of CoW.
+ */
+static void calculate_nurbs_tangents(const NURBSpline &spline, MutableSpan<float3> tangents)
+{
+ PolySpline poly_spline;
+ poly_spline.resize(spline.size());
+ poly_spline.positions().copy_from(spline.positions());
+ tangents.copy_from(poly_spline.evaluated_tangents());
+}
+
+static Array<float3> curve_tangent_point_domain(const CurveEval &curve)
+{
+ Span<SplinePtr> splines = curve.splines();
+ Array<int> offsets = curve.control_point_offsets();
+ const int total_size = offsets.last();
+ Array<float3> tangents(total_size);
+
+ threading::parallel_for(splines.index_range(), 128, [&](IndexRange range) {
+ for (const int i : range) {
+ const Spline &spline = *splines[i];
+ MutableSpan spline_tangents{tangents.as_mutable_span().slice(offsets[i], spline.size())};
+ switch (splines[i]->type()) {
+ case Spline::Type::Bezier: {
+ calculate_bezier_tangents(static_cast<const BezierSpline &>(spline), spline_tangents);
+ break;
+ }
+ case Spline::Type::Poly: {
+ calculate_poly_tangents(static_cast<const PolySpline &>(spline), spline_tangents);
+ break;
+ }
+ case Spline::Type::NURBS: {
+ calculate_nurbs_tangents(static_cast<const NURBSpline &>(spline), spline_tangents);
+ break;
+ }
+ }
+ }
+ });
+ return tangents;
+}
+
+static const GVArray *construct_curve_tangent_gvarray(const CurveComponent &component,
+ const AttributeDomain domain,
+ ResourceScope &scope)
+{
+ const CurveEval *curve = component.get_for_read();
+ if (curve == nullptr) {
+ return nullptr;
+ }
+
+ if (domain == ATTR_DOMAIN_POINT) {
+ const Span<SplinePtr> splines = curve->splines();
+
+ /* Use a reference to evaluated tangents if possible to avoid an allocation and a copy.
+ * This is only possible when there is only one poly spline. */
+ if (splines.size() == 1 && splines.first()->type() == Spline::Type::Poly) {
+ const PolySpline &spline = static_cast<PolySpline &>(*splines.first());
+ return &scope.construct<fn::GVArray_For_Span<float3>>(spline.evaluated_tangents());
+ }
+
+ Array<float3> tangents = curve_tangent_point_domain(*curve);
+ return &scope.construct<fn::GVArray_For_ArrayContainer<Array<float3>>>(std::move(tangents));
+ }
+
+ if (domain == ATTR_DOMAIN_CURVE) {
+ Array<float3> point_tangents = curve_tangent_point_domain(*curve);
+ GVArrayPtr gvarray = std::make_unique<fn::GVArray_For_ArrayContainer<Array<float3>>>(
+ std::move(point_tangents));
+ GVArrayPtr spline_tangents = component.attribute_try_adapt_domain(
+ std::move(gvarray), ATTR_DOMAIN_POINT, ATTR_DOMAIN_CURVE);
+ return scope.add_value(std::move(spline_tangents)).get();
+ }
+
+ return nullptr;
+}
+
+class TangentFieldInput final : public fn::FieldInput {
+ public:
+ TangentFieldInput() : fn::FieldInput(CPPType::get<float3>(), "Tangent")
+ {
+ }
+
+ const GVArray *get_varray_for_context(const fn::FieldContext &context,
+ IndexMask UNUSED(mask),
+ ResourceScope &scope) const final
+ {
+ if (const GeometryComponentFieldContext *geometry_context =
+ dynamic_cast<const GeometryComponentFieldContext *>(&context)) {
+
+ const GeometryComponent &component = geometry_context->geometry_component();
+ const AttributeDomain domain = geometry_context->domain();
+
+ if (component.type() == GEO_COMPONENT_TYPE_CURVE) {
+ const CurveComponent &curve_component = static_cast<const CurveComponent &>(component);
+ return construct_curve_tangent_gvarray(curve_component, domain, scope);
+ }
+ }
+ return nullptr;
+ }
+
+ uint64_t hash() const override
+ {
+ /* Some random constant hash. */
+ return 91827364589;
+ }
+
+ bool is_equal_to(const fn::FieldNode &other) const override
+ {
+ return dynamic_cast<const TangentFieldInput *>(&other) != nullptr;
+ }
+};
+
+static void geo_node_input_tangent_exec(GeoNodeExecParams params)
+{
+ Field<float3> tangent_field{std::make_shared<TangentFieldInput>()};
+ params.set_output("Tangent", std::move(tangent_field));
+}
+
+} // namespace blender::nodes
+
+void register_node_type_geo_input_tangent()
+{
+ static bNodeType ntype;
+
+ geo_node_type_base(&ntype, GEO_NODE_INPUT_TANGENT, "Curve Tangent", NODE_CLASS_INPUT, 0);
+ ntype.geometry_node_execute = blender::nodes::geo_node_input_tangent_exec;
+ ntype.declare = blender::nodes::geo_node_input_tangent_declare;
+ nodeRegisterType(&ntype);
+}