[Bf-blender-cvs] [b928050af33] functions: initial Mesh Distance Falloff node

Jacques Lucke noreply at git.blender.org
Tue Sep 10 15:45:06 CEST 2019


Commit: b928050af33098fb87b56a3dbd0f9a03f5b25738
Author: Jacques Lucke
Date:   Tue Sep 10 15:44:58 2019 +0200
Branches: functions
https://developer.blender.org/rBb928050af33098fb87b56a3dbd0f9a03f5b25738

initial Mesh Distance Falloff node

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

M	release/scripts/startup/nodes/function_nodes/falloffs.py
M	source/blender/blenkernel/BKE_falloff.hpp
M	source/blender/blenkernel/intern/falloff.cpp
M	source/blender/functions/frontends/data_flow_nodes/mappings/node_inserters.cpp
M	source/blender/functions/functions/falloffs.cpp
M	source/blender/functions/functions/falloffs.hpp
M	source/blender/simulations/bparticles/forces.hpp

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

diff --git a/release/scripts/startup/nodes/function_nodes/falloffs.py b/release/scripts/startup/nodes/function_nodes/falloffs.py
index 0ff01b8d906..208fa3a8e21 100644
--- a/release/scripts/startup/nodes/function_nodes/falloffs.py
+++ b/release/scripts/startup/nodes/function_nodes/falloffs.py
@@ -21,3 +21,14 @@ class PointDistanceFalloffNode(bpy.types.Node, FunctionNode):
         builder.fixed_input("min_distance", "Min Distance", "Float")
         builder.fixed_input("max_distance", "Max Distance", "Float", default=1)
         builder.fixed_output("falloff", "Falloff", "Falloff")
+
+
+class MeshDistanceFalloffNode(bpy.types.Node, FunctionNode):
+    bl_idname = "fn_MeshDistanceFalloffNode"
+    bl_label = "Mesh Distance Falloff"
+
+    def declaration(self, builder: NodeBuilder):
+        builder.fixed_input("object", "Object", "Object")
+        builder.fixed_input("inner_distance", "Inner Distance", "Float")
+        builder.fixed_input("outer_distance", "Outer Distance", "Float", default=1.0)
+        builder.fixed_output("falloff", "Falloff", "Falloff")
diff --git a/source/blender/blenkernel/BKE_falloff.hpp b/source/blender/blenkernel/BKE_falloff.hpp
index 82c4025ef0d..626c9036a15 100644
--- a/source/blender/blenkernel/BKE_falloff.hpp
+++ b/source/blender/blenkernel/BKE_falloff.hpp
@@ -1,9 +1,17 @@
 #pragma once
 
 #include "BKE_attributes_ref.hpp"
+#include "BKE_bvhutils.h"
+
+#include "BLI_kdopbvh.h"
+#include "BLI_kdtree.h"
+
+#include "DNA_object_types.h"
 
 namespace BKE {
 
+using BLI::float4x4;
+
 class Falloff {
  public:
 #ifdef WITH_CXX_GUARDEDALLOC
@@ -34,7 +42,7 @@ class ConstantFalloff : public Falloff {
   {
   }
 
-  Falloff *clone() const
+  Falloff *clone() const override
   {
     return new ConstantFalloff(m_weight);
   }
@@ -66,4 +74,27 @@ class PointDistanceFalloff : public Falloff {
                MutableArrayRef<float> r_weights) const override;
 };
 
+class MeshDistanceFalloff : public Falloff {
+ private:
+  Object *m_object;
+  BVHTreeFromMesh m_bvhtree_data;
+  float4x4 m_local_to_world;
+  float4x4 m_world_to_local;
+  float m_inner_distance;
+  float m_outer_distance;
+
+ public:
+  MeshDistanceFalloff(Object *object, float inner_distance, float outer_distance);
+  ~MeshDistanceFalloff();
+
+  Falloff *clone() const override
+  {
+    return new MeshDistanceFalloff(m_object, m_inner_distance, m_outer_distance);
+  }
+
+  void compute(AttributesRef attributes,
+               ArrayRef<uint> indices,
+               MutableArrayRef<float> r_weights) const override;
+};
+
 }  // namespace BKE
diff --git a/source/blender/blenkernel/intern/falloff.cpp b/source/blender/blenkernel/intern/falloff.cpp
index a5eba4a473c..800bebfd2b6 100644
--- a/source/blender/blenkernel/intern/falloff.cpp
+++ b/source/blender/blenkernel/intern/falloff.cpp
@@ -1,7 +1,11 @@
 #include "BKE_falloff.hpp"
 
+#include "BLI_array.hpp"
+
 namespace BKE {
 
+using BLI::TemporaryArray;
+
 Falloff::~Falloff()
 {
 }
@@ -35,4 +39,55 @@ void PointDistanceFalloff::compute(AttributesRef attributes,
   }
 }
 
+MeshDistanceFalloff::MeshDistanceFalloff(Object *object,
+                                         float inner_distance,
+                                         float outer_distance)
+    : m_object(object), m_inner_distance(inner_distance), m_outer_distance(outer_distance)
+{
+  BLI_assert(object->type == OB_MESH);
+  m_local_to_world = m_object->obmat;
+  m_world_to_local = m_local_to_world.inverted__LocRotScale();
+
+  BKE_bvhtree_from_mesh_get(&m_bvhtree_data, (Mesh *)object->data, BVHTREE_FROM_LOOPTRI, 2);
+}
+
+MeshDistanceFalloff::~MeshDistanceFalloff()
+{
+  free_bvhtree_from_mesh(&m_bvhtree_data);
+}
+
+void MeshDistanceFalloff::compute(AttributesRef attributes,
+                                  ArrayRef<uint> indices,
+                                  MutableArrayRef<float> r_weights) const
+{
+  auto positions = attributes.get<float3>("Position");
+
+  float distance_diff = std::max(0.0001f, m_outer_distance - m_inner_distance);
+  for (uint index : indices) {
+    float3 position = positions[index];
+    float3 local_position = m_world_to_local.transform_position(position);
+
+    BVHTreeNearest nearest = {0};
+    nearest.dist_sq = 10000.0f;
+    nearest.index = -1;
+    BLI_bvhtree_find_nearest(m_bvhtree_data.tree,
+                             local_position,
+                             &nearest,
+                             m_bvhtree_data.nearest_callback,
+                             (void *)&m_bvhtree_data);
+
+    if (nearest.index == -1) {
+      r_weights[index] = 0.0f;
+      continue;
+    }
+
+    float3 nearest_position = m_local_to_world.transform_position(nearest.co);
+    float distance = float3::distance(position, nearest_position);
+
+    float weight = (distance - m_inner_distance) / distance_diff;
+    CLAMP(weight, 0.0f, 1.0f);
+    r_weights[index] = weight;
+  }
+}
+
 }  // namespace BKE
diff --git a/source/blender/functions/frontends/data_flow_nodes/mappings/node_inserters.cpp b/source/blender/functions/frontends/data_flow_nodes/mappings/node_inserters.cpp
index c76d18fdfb1..20be4a5811c 100644
--- a/source/blender/functions/frontends/data_flow_nodes/mappings/node_inserters.cpp
+++ b/source/blender/functions/frontends/data_flow_nodes/mappings/node_inserters.cpp
@@ -406,6 +406,7 @@ void REGISTER_node_inserters(std::unique_ptr<NodeInserters> &inserters)
   REGISTER_FUNCTION("fn_TextLengthNode", string_length);
   REGISTER_FUNCTION("fn_PointDistanceFalloffNode", point_distance_falloff);
   REGISTER_FUNCTION("fn_ConstantFalloffNode", constant_falloff);
+  REGISTER_FUNCTION("fn_MeshDistanceFalloffNode", mesh_distance_falloff);
 
   REGISTER_INSERTER("fn_CallNode", INSERT_call);
   REGISTER_INSERTER("fn_ClampNode", INSERT_clamp);
diff --git a/source/blender/functions/functions/falloffs.cpp b/source/blender/functions/functions/falloffs.cpp
index 12dcb321c11..279286e8603 100644
--- a/source/blender/functions/functions/falloffs.cpp
+++ b/source/blender/functions/functions/falloffs.cpp
@@ -55,5 +55,35 @@ BLI_LAZY_INIT(SharedFunction, GET_FN_constant_falloff)
   return fn;
 }
 
+class MeshDistanceFalloff : public TupleCallBody {
+  void call(Tuple &fn_in, Tuple &fn_out, ExecutionContext &UNUSED(ctx)) const override
+  {
+    Object *object = fn_in.relocate_out<ObjectW>(0).ptr();
+    if (object == nullptr || object->type != OB_MESH) {
+      FalloffW fallback = new BKE::ConstantFalloff(1.0f);
+      fn_out.move_in(0, fallback);
+      return;
+    }
+
+    float inner_distance = fn_in.get<float>(1);
+    float outer_distance = fn_in.get<float>(2);
+    FalloffW falloff = new BKE::MeshDistanceFalloff(object, inner_distance, outer_distance);
+    fn_out.move_in(0, falloff);
+  }
+};
+
+BLI_LAZY_INIT(SharedFunction, GET_FN_mesh_distance_falloff)
+{
+  FunctionBuilder builder;
+  builder.add_input("Object", TYPE_object);
+  builder.add_input("Inner Distance", TYPE_float);
+  builder.add_input("Outer Distance", TYPE_float);
+  builder.add_output("Falloff", TYPE_falloff);
+
+  auto fn = builder.build("Mesh Distance Falloff");
+  fn->add_body<MeshDistanceFalloff>();
+  return fn;
+}
+
 }  // namespace Functions
 }  // namespace FN
diff --git a/source/blender/functions/functions/falloffs.hpp b/source/blender/functions/functions/falloffs.hpp
index eac9e872d2c..2c92180b46c 100644
--- a/source/blender/functions/functions/falloffs.hpp
+++ b/source/blender/functions/functions/falloffs.hpp
@@ -7,6 +7,7 @@ namespace Functions {
 
 SharedFunction &GET_FN_point_distance_falloff();
 SharedFunction &GET_FN_constant_falloff();
+SharedFunction &GET_FN_mesh_distance_falloff();
 
 }  // namespace Functions
 }  // namespace FN
diff --git a/source/blender/simulations/bparticles/forces.hpp b/source/blender/simulations/bparticles/forces.hpp
index a1581f6b9ed..7fd1f077c61 100644
--- a/source/blender/simulations/bparticles/forces.hpp
+++ b/source/blender/simulations/bparticles/forces.hpp
@@ -82,6 +82,11 @@ class MeshForce : public Force {
     BKE_bvhtree_from_mesh_get(&m_bvhtree_data, (Mesh *)object->data, BVHTREE_FROM_LOOPTRI, 2);
   }
 
+  ~MeshForce()
+  {
+    free_bvhtree_from_mesh(&m_bvhtree_data);
+  }
+
   void add_force(ForceInterface &interface) override;
 };



More information about the Bf-blender-cvs mailing list