[Bf-blender-cvs] [c9958be7497] functions: initial mesh collision event

Jacques Lucke noreply at git.blender.org
Fri Jun 21 13:29:41 CEST 2019


Commit: c9958be7497c8cad0a4cc056832ca45476b51f87
Author: Jacques Lucke
Date:   Fri Jun 21 12:53:51 2019 +0200
Branches: functions
https://developer.blender.org/rBc9958be7497c8cad0a4cc056832ca45476b51f87

initial mesh collision event

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

M	release/scripts/startup/bl_ui/properties_data_modifier.py
M	source/blender/blenkernel/BKE_bvhutils.h
M	source/blender/blenlib/BLI_math.hpp
M	source/blender/makesdna/DNA_modifier_types.h
M	source/blender/makesrna/intern/rna_modifier.c
M	source/blender/modifiers/intern/MOD_nodeparticles.c
M	source/blender/simulations/CMakeLists.txt
M	source/blender/simulations/bparticles/c_wrapper.cpp
M	source/blender/simulations/bparticles/events.cpp
M	source/blender/simulations/bparticles/events.hpp

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

diff --git a/release/scripts/startup/bl_ui/properties_data_modifier.py b/release/scripts/startup/bl_ui/properties_data_modifier.py
index c54577ec638..76982706257 100644
--- a/release/scripts/startup/bl_ui/properties_data_modifier.py
+++ b/release/scripts/startup/bl_ui/properties_data_modifier.py
@@ -1667,6 +1667,7 @@ class DATA_PT_modifiers(ModifierButtonsPanel, Panel):
         layout.prop(md, "control1")
         layout.prop(md, "control2")
         layout.prop(md, "emitter_object")
+        layout.prop(md, "collision_object")
 
 
 class DATA_PT_gpencil_modifiers(ModifierButtonsPanel, Panel):
diff --git a/source/blender/blenkernel/BKE_bvhutils.h b/source/blender/blenkernel/BKE_bvhutils.h
index c88a64097bb..65fd16aca12 100644
--- a/source/blender/blenkernel/BKE_bvhutils.h
+++ b/source/blender/blenkernel/BKE_bvhutils.h
@@ -26,6 +26,10 @@
 #include "BLI_bitmap.h"
 #include "BLI_kdopbvh.h"
 
+#ifdef __cplusplus
+extern "C" {
+#endif
+
 /**
  * This header encapsulates necessary code to build a BVH
  */
@@ -235,4 +239,8 @@ bool bvhcache_has_tree(const BVHCache *cache, const BVHTree *tree);
 void bvhcache_insert(BVHCache **cache_p, BVHTree *tree, int type);
 void bvhcache_free(BVHCache **cache_p);
 
+#ifdef __cplusplus
+}
+#endif
+
 #endif
diff --git a/source/blender/blenlib/BLI_math.hpp b/source/blender/blenlib/BLI_math.hpp
index a0da94136f7..3917eeb9f55 100644
--- a/source/blender/blenlib/BLI_math.hpp
+++ b/source/blender/blenlib/BLI_math.hpp
@@ -1,5 +1,6 @@
 #pragma once
 
+#include "BLI_math_vector.h"
 #include "BLI_math_matrix.h"
 
 namespace BLI {
@@ -17,11 +18,33 @@ struct float3 {
   {
   }
 
+  operator const float *() const
+  {
+    return (const float *)this;
+  }
+
   operator float *()
   {
     return (float *)this;
   }
 
+  float normalize_and_get_length()
+  {
+    return normalize_v3(*this);
+  }
+
+  float3 normalized() const
+  {
+    float3 result;
+    normalize_v3_v3(result, *this);
+    return result;
+  }
+
+  float length() const
+  {
+    return len_v3(*this);
+  }
+
   friend float3 operator+(float3 a, float3 b)
   {
     return {a.x + b.x, a.y + b.y, a.z + b.z};
diff --git a/source/blender/makesdna/DNA_modifier_types.h b/source/blender/makesdna/DNA_modifier_types.h
index cbd33856d21..c63c30c7423 100644
--- a/source/blender/makesdna/DNA_modifier_types.h
+++ b/source/blender/makesdna/DNA_modifier_types.h
@@ -1975,6 +1975,7 @@ typedef struct NodeParticlesModifierData {
   ModifierData modifier;
   float control1, control2;
   struct Object *emitter_object;
+  struct Object *collision_object;
 } NodeParticlesModifierData;
 
 #endif /* __DNA_MODIFIER_TYPES_H__ */
diff --git a/source/blender/makesrna/intern/rna_modifier.c b/source/blender/makesrna/intern/rna_modifier.c
index fc0a601e5b2..f92b77ded63 100644
--- a/source/blender/makesrna/intern/rna_modifier.c
+++ b/source/blender/makesrna/intern/rna_modifier.c
@@ -5993,6 +5993,11 @@ static void rna_def_modifier_node_particles(BlenderRNA *brna)
   RNA_def_property_flag(prop, PROP_EDITABLE);
   RNA_def_property_ui_text(prop, "Emitter Object", "Object to emit from");
   RNA_def_property_update(prop, 0, "rna_Modifier_dependency_update");
+
+  prop = RNA_def_property(srna, "collision_object", PROP_POINTER, PROP_NONE);
+  RNA_def_property_flag(prop, PROP_EDITABLE);
+  RNA_def_property_ui_text(prop, "Collision Object", "Object to collide with");
+  RNA_def_property_update(prop, 0, "rna_Modifier_dependency_update");
 }
 
 void RNA_def_modifier(BlenderRNA *brna)
diff --git a/source/blender/modifiers/intern/MOD_nodeparticles.c b/source/blender/modifiers/intern/MOD_nodeparticles.c
index 20f96dd9706..306a9983f6c 100644
--- a/source/blender/modifiers/intern/MOD_nodeparticles.c
+++ b/source/blender/modifiers/intern/MOD_nodeparticles.c
@@ -189,7 +189,11 @@ static void updateDepsgraph(ModifierData *md, const ModifierUpdateDepsgraphConte
   NodeParticlesModifierData *npmd = (NodeParticlesModifierData *)md;
   if (npmd->emitter_object) {
     DEG_add_object_relation(
-        ctx->node, npmd->emitter_object, DEG_OB_COMP_GEOMETRY, "Node Particles Modifier");
+        ctx->node, npmd->emitter_object, DEG_OB_COMP_GEOMETRY, "Modifier Emitter");
+  }
+  if (npmd->collision_object) {
+    DEG_add_object_relation(
+        ctx->node, npmd->collision_object, DEG_OB_COMP_GEOMETRY, "Modifer Collision");
   }
 }
 
@@ -197,12 +201,14 @@ static void foreachObjectLink(ModifierData *md, Object *ob, ObjectWalkFunc walk,
 {
   NodeParticlesModifierData *npmd = (NodeParticlesModifierData *)md;
   walk(userData, ob, &npmd->emitter_object, IDWALK_CB_NOP);
+  walk(userData, ob, &npmd->collision_object, IDWALK_CB_NOP);
 }
 
 static void foreachIDLink(ModifierData *md, Object *ob, IDWalkFunc walk, void *userData)
 {
   NodeParticlesModifierData *npmd = (NodeParticlesModifierData *)md;
   walk(userData, ob, (ID **)&npmd->emitter_object, IDWALK_CB_NOP);
+  walk(userData, ob, (ID **)&npmd->collision_object, IDWALK_CB_NOP);
 }
 
 ModifierTypeInfo modifierType_NodeParticles = {
diff --git a/source/blender/simulations/CMakeLists.txt b/source/blender/simulations/CMakeLists.txt
index b617ff09214..eca93184e7b 100644
--- a/source/blender/simulations/CMakeLists.txt
+++ b/source/blender/simulations/CMakeLists.txt
@@ -36,6 +36,7 @@ set(SRC
 
 set(LIB
   bf_blenlib
+  bf_blenkernel
 )
 
 blender_add_lib(bf_simulations "${SRC}" "${INC}" "${INC_SYS}" "${LIB}")
diff --git a/source/blender/simulations/bparticles/c_wrapper.cpp b/source/blender/simulations/bparticles/c_wrapper.cpp
index 7556a77b29e..b347c8cae5a 100644
--- a/source/blender/simulations/bparticles/c_wrapper.cpp
+++ b/source/blender/simulations/bparticles/c_wrapper.cpp
@@ -11,6 +11,7 @@
 #include "BLI_listbase.h"
 
 #include "BKE_curve.h"
+#include "BKE_bvhutils.h"
 
 #include "DNA_modifier_types.h"
 #include "DNA_object_types.h"
@@ -129,13 +130,22 @@ void BParticles_simulate_modifier(NodeParticlesModifierData *npmd,
     description.m_emitters.append(
         EMITTER_mesh_surface((Mesh *)npmd->emitter_object->data, npmd->control1).release());
   }
+  BVHTreeFromMesh treedata = {0};
+  if (npmd->collision_object) {
+    BKE_bvhtree_from_mesh_get(
+        &treedata, (Mesh *)npmd->collision_object->data, BVHTREE_FROM_LOOPTRI, 4);
+    description.m_influences.m_events.append(EVENT_mesh_collection(&treedata).release());
+    description.m_influences.m_actions.append(ACTION_kill().release());
+  }
   description.m_influences.m_forces.append(FORCE_directional({0, 0, -2}).release());
-  description.m_influences.m_events.append(EVENT_age_reached(6.0f).release());
-  description.m_influences.m_actions.append(ACTION_kill().release());
   description.m_influences.m_events.append(EVENT_age_reached(3.0f).release());
   description.m_influences.m_actions.append(ACTION_move({0, 1, 0}).release());
   simulate_step(state, description);
 
+  if (npmd->collision_object) {
+    free_bvhtree_from_mesh(&treedata);
+  }
+
   std::cout << "Active Blocks: " << state.m_container->active_blocks().size() << "\n";
   std::cout << " Particle Amount: " << BParticles_state_particle_count(state_c) << "\n";
 }
diff --git a/source/blender/simulations/bparticles/events.cpp b/source/blender/simulations/bparticles/events.cpp
index 576be833394..1f9b950c606 100644
--- a/source/blender/simulations/bparticles/events.cpp
+++ b/source/blender/simulations/bparticles/events.cpp
@@ -1,5 +1,9 @@
 #include "events.hpp"
 
+#include "BLI_kdopbvh.h"
+
+#include "BKE_bvhutils.h"
+
 namespace BParticles {
 
 class AgeReachedEvent : public Event {
@@ -35,10 +39,63 @@ class AgeReachedEvent : public Event {
   }
 };
 
+class MeshCollisionEvent : public Event {
+ private:
+  BVHTreeFromMesh *m_treedata;
+
+ public:
+  MeshCollisionEvent(BVHTreeFromMesh *treedata) : m_treedata(treedata)
+  {
+  }
+
+  void filter(AttributeArrays attributes,
+              ArrayRef<uint> particle_indices,
+              IdealOffsets &ideal_offsets,
+              ArrayRef<float> UNUSED(durations),
+              float UNUSED(end_time),
+              SmallVector<uint> &r_filtered_indices,
+              SmallVector<float> &r_time_factors) override
+  {
+    auto positions = attributes.get_float3("Position");
+    auto position_offsets = ideal_offsets.position_offsets;
+
+    for (uint i = 0; i < particle_indices.size(); i++) {
+      uint pindex = particle_indices[i];
+
+      float3 start_position = positions[pindex];
+      float3 direction = position_offsets[i];
+      float length = direction.normalize_and_get_length();
+
+      BVHTreeRayHit hit;
+      hit.dist = length;
+      hit.index = -1;
+      BLI_bvhtree_ray_cast(m_treedata->tree,
+                           start_position,
+                           direction,
+                           0.0f,
+                           &hit,
+                           m_treedata->raycast_callback,
+                           (void *)m_treedata);
+
+      if (hit.index != -1) {
+        float time_factor = hit.dist / direction.length();
+        r_filtered_indices.append(i);
+        r_time_factors.append(time_factor);
+      }
+    }
+  }
+};
+
 std::unique_ptr<Event> EVENT_age_reached(float age)
 {
   Event *event = new AgeReachedEvent(age);
   return std::unique_ptr<Event>(event);
 }
 
+std::unique_ptr<Event> EVENT_mesh_collection(BVHTreeFromMesh *treedata)
+{
+  Event *event = new MeshCollisionEvent(treedata);
+  return std::unique_ptr<Event>(event);
+}
+
 }  // namespace BParticles
diff --git a/source/blender/simulations/bparticles/events.hpp b/source/blender/simulations/bparticles/events.hpp
index 89a5e576aad..3115e4e6821 100644
--- a/source/blender/simulations/bparticles/events.hpp
+++ b/source/blender/simulations/bparticles/events.hpp
@@ -2,6 +2,11 @@
 
 #include "core.hpp"
 
+struct BVHTreeFromMesh;
+
 namespace BParticles {
+
 std::unique_ptr<Event> EVENT_age_reached(float age);
-}
+std::unique_ptr<Event> EVENT_mesh_collection(struct BVHTreeFromMesh *treedata);
+
+}  // namespace BParticles



More information about the Bf-blender-cvs mailing list