[Bf-blender-cvs] [6cf3878765f] geometry-nodes-curve-support: Geometry Nodes Curves: Add Z-Up normal calculation mode

Hans Goudey noreply at git.blender.org
Mon Apr 12 01:54:12 CEST 2021


Commit: 6cf3878765f7eee7bc248c008648c4158c5a2918
Author: Hans Goudey
Date:   Sun Apr 11 17:37:21 2021 -0500
Branches: geometry-nodes-curve-support
https://developer.blender.org/rB6cf3878765f7eee7bc248c008648c4158c5a2918

Geometry Nodes Curves: Add Z-Up normal calculation mode

===================================================================

M	source/blender/blenkernel/BKE_derived_curve.hh
M	source/blender/blenkernel/intern/derived_curve.cc
M	source/blender/nodes/geometry/nodes/node_geo_curve_to_mesh.cc

===================================================================

diff --git a/source/blender/blenkernel/BKE_derived_curve.hh b/source/blender/blenkernel/BKE_derived_curve.hh
index 46935a1228f..8512cfc022f 100644
--- a/source/blender/blenkernel/BKE_derived_curve.hh
+++ b/source/blender/blenkernel/BKE_derived_curve.hh
@@ -76,6 +76,13 @@ class Spline {
   Type type;
   bool is_cyclic = false;
 
+  enum NormalCalculationMode {
+    ZUp,
+    Minimum,
+    Tangent,
+  };
+  NormalCalculationMode normal_mode;
+
  protected:
   mutable bool base_cache_dirty_ = true;
   mutable std::mutex base_cache_mutex_;
@@ -189,6 +196,8 @@ struct DCurve {
 
   // bool is_2d;
 
+  // DCurve *copy();
+
   ~DCurve()
   {
     for (Spline *spline : splines) {
diff --git a/source/blender/blenkernel/intern/derived_curve.cc b/source/blender/blenkernel/intern/derived_curve.cc
index d63cf929837..9364d68c929 100644
--- a/source/blender/blenkernel/intern/derived_curve.cc
+++ b/source/blender/blenkernel/intern/derived_curve.cc
@@ -53,6 +53,20 @@ static BezierPoint::HandleType handle_type_from_dna_bezt(const eBezTriple_Handle
   return BezierPoint::Auto;
 }
 
+static Spline::NormalCalculationMode normal_mode_from_dna_curve(const int twist_mode)
+{
+  switch (twist_mode) {
+    case CU_TWIST_Z_UP:
+      return Spline::NormalCalculationMode::ZUp;
+    case CU_TWIST_MINIMUM:
+      return Spline::NormalCalculationMode::Minimum;
+    case CU_TWIST_TANGENT:
+      return Spline::NormalCalculationMode::Tangent;
+  }
+  BLI_assert_unreachable();
+  return Spline::NormalCalculationMode::Minimum;
+}
+
 DCurve *dcurve_from_dna_curve(const Curve &dna_curve)
 {
   DCurve *curve = new DCurve();
@@ -88,6 +102,12 @@ DCurve *dcurve_from_dna_curve(const Curve &dna_curve)
     }
   }
 
+  const Spline::NormalCalculationMode normal_mode = normal_mode_from_dna_curve(
+      dna_curve.twist_mode);
+  for (Spline *spline : curve->splines) {
+    spline->normal_mode = normal_mode;
+  }
+
   return curve;
 }
 
@@ -325,9 +345,9 @@ static void make_normals_cyclic(Span<float3> tangents, MutableSpan<float3> norma
 
 /* This algorithm is a copy from animation nodes bezier normal calculation.
  * TODO: Explore different methods. */
-static void evaluate_normals(Span<float3> tangents,
-                             const bool is_cyclic,
-                             MutableSpan<float3> normals)
+static void calculate_normals_minimum_twist(Span<float3> tangents,
+                                            const bool is_cyclic,
+                                            MutableSpan<float3> normals)
 {
   if (normals.size() == 1) {
     normals.first() = float3(1.0f, 0.0f, 0.0f);
@@ -347,6 +367,13 @@ static void evaluate_normals(Span<float3> tangents,
   }
 }
 
+static void calculate_normals_z_up(Span<float3> tangents, MutableSpan<float3> normals)
+{
+  for (const int i : normals.index_range()) {
+    normals[i] = float3::cross(tangents[i], float3(0.0f, 0.0f, 1.0f)).normalized();
+  }
+}
+
 Span<float3> Spline::evaluated_normals() const
 {
   if (!this->normal_cache_dirty_) {
@@ -362,7 +389,17 @@ Span<float3> Spline::evaluated_normals() const
   this->evaluated_normals_cache_.resize(total);
 
   Span<float3> tangents = this->evaluated_tangents();
-  evaluate_normals(tangents, is_cyclic, this->evaluated_normals_cache_);
+  switch (this->normal_mode) {
+    case NormalCalculationMode::Minimum:
+      calculate_normals_minimum_twist(tangents, is_cyclic, this->evaluated_normals_cache_);
+      break;
+    case NormalCalculationMode::ZUp:
+      calculate_normals_z_up(tangents, this->evaluated_normals_cache_);
+      break;
+    case NormalCalculationMode::Tangent:
+      // calculate_normals_tangent(tangents, this->evaluated_normals_cache_);
+      break;
+  }
 
   this->normal_cache_dirty_ = false;
   return evaluated_normals_cache_;
diff --git a/source/blender/nodes/geometry/nodes/node_geo_curve_to_mesh.cc b/source/blender/nodes/geometry/nodes/node_geo_curve_to_mesh.cc
index 90efaaaa48d..d8a1ac0e9dc 100644
--- a/source/blender/nodes/geometry/nodes/node_geo_curve_to_mesh.cc
+++ b/source/blender/nodes/geometry/nodes/node_geo_curve_to_mesh.cc
@@ -184,7 +184,7 @@ static void spline_extrude_to_mesh_data(const Spline &spline,
         positions[i_ring], tangents[i_ring], normals[i_ring]);
 
     const float radius = spline.get_evaluated_point_radius(i_ring);
-    point_matrix.apply_scale(radius);
+    point_matrix.apply_scale(2.0f * radius);
 
     for (const int i_profile : IndexRange(profile_vert_len)) {
       MVert &vert = verts[vert_offset++];



More information about the Bf-blender-cvs mailing list