[Bf-blender-cvs] [7aa22a24a7f] functions: initial forwarding listeners + particle trail node

Jacques Lucke noreply at git.blender.org
Thu Jul 18 18:19:01 CEST 2019


Commit: 7aa22a24a7fea3766cab4e59f46bbca8064ded8d
Author: Jacques Lucke
Date:   Thu Jul 18 12:08:36 2019 +0200
Branches: functions
https://developer.blender.org/rB7aa22a24a7fea3766cab4e59f46bbca8064ded8d

initial forwarding listeners + particle trail node

Still looking for a better name instead of Forwarding Listeners

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

A	release/scripts/startup/nodes/bparticle_nodes/trails.py
M	source/blender/simulations/bparticles/core.cpp
M	source/blender/simulations/bparticles/core.hpp
M	source/blender/simulations/bparticles/forces.cpp
M	source/blender/simulations/bparticles/forces.hpp
M	source/blender/simulations/bparticles/inserters.cpp
M	source/blender/simulations/bparticles/inserters.hpp
M	source/blender/simulations/bparticles/node_frontend.cpp
M	source/blender/simulations/bparticles/simulate.cpp
M	source/blender/simulations/bparticles/step_description.hpp

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

diff --git a/release/scripts/startup/nodes/bparticle_nodes/trails.py b/release/scripts/startup/nodes/bparticle_nodes/trails.py
new file mode 100644
index 00000000000..125d9a046c0
--- /dev/null
+++ b/release/scripts/startup/nodes/bparticle_nodes/trails.py
@@ -0,0 +1,16 @@
+import bpy
+from bpy.props import *
+from .. base import BParticlesNode
+from .. socket_builder import SocketBuilder
+
+class ParticleTrailsNode(bpy.types.Node, BParticlesNode):
+    bl_idname = "bp_ParticleTrailsNode"
+    bl_label = "Particle Trails"
+
+    particle_type_name: StringProperty()
+
+    def declaration(self, builder : SocketBuilder):
+        builder.particle_modifier_output("effect", "Effect")
+
+    def draw(self, layout):
+        layout.prop(self, "particle_type_name", text="")
diff --git a/source/blender/simulations/bparticles/core.cpp b/source/blender/simulations/bparticles/core.cpp
index e04e0b1bf3b..de3626a44bd 100644
--- a/source/blender/simulations/bparticles/core.cpp
+++ b/source/blender/simulations/bparticles/core.cpp
@@ -14,10 +14,24 @@ Event::~Event()
 {
 }
 
+ForwardingListener::~ForwardingListener()
+{
+}
+
 ParticleType::~ParticleType()
 {
 }
 
+ArrayRef<ForwardingListener *> ParticleType::forwarding_listeners()
+{
+  return {};
+}
+
+ArrayRef<Event *> ParticleType::events()
+{
+  return {};
+}
+
 StepDescription::~StepDescription()
 {
 }
@@ -357,4 +371,22 @@ IntegratorInterface::IntegratorInterface(ParticlesBlock &block,
 {
 }
 
+/* ForwardingListenerInterface
+ ****************************************************/
+
+ForwardingListenerInterface::ForwardingListenerInterface(ParticleSet &particles,
+                                                         ParticleAllocator &particle_allocator,
+                                                         AttributeArrays offsets,
+                                                         float step_end_time,
+                                                         ArrayRef<float> durations,
+                                                         ArrayRef<float> time_factors)
+    : m_particles(particles),
+      m_particle_allocator(particle_allocator),
+      m_offsets(offsets),
+      m_step_end_time(step_end_time),
+      m_durations(durations),
+      m_time_factors(time_factors)
+{
+}
+
 }  // namespace BParticles
diff --git a/source/blender/simulations/bparticles/core.hpp b/source/blender/simulations/bparticles/core.hpp
index 70aefdc2ebb..0b12545b1b7 100644
--- a/source/blender/simulations/bparticles/core.hpp
+++ b/source/blender/simulations/bparticles/core.hpp
@@ -25,6 +25,7 @@ class EventFilterInterface;
 class EventExecuteInterface;
 class EmitterInterface;
 class IntegratorInterface;
+class ForwardingListenerInterface;
 
 /* Main API for the particle simulation. These classes have to be subclassed to define how the
  * particles should behave.
@@ -118,6 +119,13 @@ class Integrator {
   virtual void integrate(IntegratorInterface &interface) = 0;
 };
 
+class ForwardingListener {
+ public:
+  virtual ~ForwardingListener();
+
+  virtual void listen(ForwardingListenerInterface &interface) = 0;
+};
+
 /**
  * Describes how one type of particle behaves and which attributes it has.
  */
@@ -130,10 +138,12 @@ class ParticleType {
    */
   virtual Integrator &integrator() = 0;
 
+  virtual ArrayRef<ForwardingListener *> forwarding_listeners();
+
   /**
    * Return the events that particles of this type can trigger.
    */
-  virtual ArrayRef<Event *> events() = 0;
+  virtual ArrayRef<Event *> events();
 
   /**
    * Allows to define which attributes should exist for the type.
@@ -574,6 +584,32 @@ class IntegratorInterface {
   AttributeArrays offset_targets();
 };
 
+class ForwardingListenerInterface {
+ private:
+  ParticleSet m_particles;
+  ParticleAllocator &m_particle_allocator;
+  AttributeArrays m_offsets;
+  float m_step_end_time;
+  ArrayRef<float> m_durations;
+  ArrayRef<float> m_time_factors;
+
+ public:
+  ForwardingListenerInterface(ParticleSet &particles,
+                              ParticleAllocator &particle_allocator,
+                              AttributeArrays offsets,
+                              float step_end_time,
+                              ArrayRef<float> durations,
+                              ArrayRef<float> time_factors);
+
+  ParticleSet &particles();
+  ParticleAllocator &particle_allocator();
+  AttributeArrays &offsets();
+  ArrayRef<float> time_factors();
+  float step_end_time();
+  ArrayRef<float> durations();
+  TimeSpan time_span(uint pindex);
+};
+
 /* Event inline functions
  ********************************************/
 
@@ -862,4 +898,43 @@ inline AttributeArrays IntegratorInterface::offset_targets()
   return m_offsets;
 }
 
+/* ForwardingListenerInterface inline functions
+ **********************************************/
+
+inline ParticleSet &ForwardingListenerInterface::particles()
+{
+  return m_particles;
+}
+
+inline ParticleAllocator &ForwardingListenerInterface::particle_allocator()
+{
+  return m_particle_allocator;
+}
+
+inline AttributeArrays &ForwardingListenerInterface::offsets()
+{
+  return m_offsets;
+}
+
+inline ArrayRef<float> ForwardingListenerInterface::time_factors()
+{
+  return m_time_factors;
+}
+
+inline float ForwardingListenerInterface::step_end_time()
+{
+  return m_step_end_time;
+}
+
+inline ArrayRef<float> ForwardingListenerInterface::durations()
+{
+  return m_durations;
+}
+
+inline TimeSpan ForwardingListenerInterface::time_span(uint pindex)
+{
+  float duration = m_durations[pindex];
+  return TimeSpan(m_step_end_time - duration, duration);
+}
+
 }  // namespace BParticles
diff --git a/source/blender/simulations/bparticles/forces.cpp b/source/blender/simulations/bparticles/forces.cpp
index 28d00e9355d..7e39e512cd8 100644
--- a/source/blender/simulations/bparticles/forces.cpp
+++ b/source/blender/simulations/bparticles/forces.cpp
@@ -44,4 +44,22 @@ void TurbulenceForce::add_force(ParticlesBlock &block, ArrayRef<float3> r_force)
   }
 }
 
+void TrailListener::listen(ForwardingListenerInterface &interface)
+{
+  ParticleSet particles = interface.particles();
+  auto positions = particles.attributes().get_float3("Position");
+
+  SmallVector<float3> new_positions;
+  SmallVector<float> new_birth_times;
+  for (uint pindex : particles.pindices()) {
+    new_positions.append(positions[pindex]);
+    new_birth_times.append(interface.time_span(pindex).start());
+  }
+
+  auto new_particles = interface.particle_allocator().request(m_particle_type_name,
+                                                              new_positions.size());
+  new_particles.set_float3("Position", new_positions);
+  new_particles.set_float("Birth Time", new_birth_times);
+}
+
 }  // namespace BParticles
diff --git a/source/blender/simulations/bparticles/forces.hpp b/source/blender/simulations/bparticles/forces.hpp
index 074bef42e71..f9c3b21ed5c 100644
--- a/source/blender/simulations/bparticles/forces.hpp
+++ b/source/blender/simulations/bparticles/forces.hpp
@@ -40,4 +40,17 @@ class TurbulenceForce : public Force {
   void add_force(ParticlesBlock &block, ArrayRef<float3> r_force) override;
 };
 
+class TrailListener : public ForwardingListener {
+ private:
+  std::string m_particle_type_name;
+
+ public:
+  TrailListener(StringRef particle_type_name)
+      : m_particle_type_name(particle_type_name.to_std_string())
+  {
+  }
+
+  void listen(ForwardingListenerInterface &interface) override;
+};
+
 }  // namespace BParticles
diff --git a/source/blender/simulations/bparticles/inserters.cpp b/source/blender/simulations/bparticles/inserters.cpp
index 3b30e5c2283..7ca19b297f7 100644
--- a/source/blender/simulations/bparticles/inserters.cpp
+++ b/source/blender/simulations/bparticles/inserters.cpp
@@ -397,6 +397,21 @@ static std::unique_ptr<Emitter> BUILD_EMITTER_initial_grid(BuildContext &ctx,
                              body->get_output<float>(fn_out, 4, "Size")));
 }
 
+static std::unique_ptr<ForwardingListener> BUILD_FORWARDING_LISTENER_trails(BuildContext &ctx,
+                                                                            bNode *bnode)
+{
+  PointerRNA rna = ctx.indexed_tree.get_rna(bnode);
+  char name[65];
+  RNA_string_get(&rna, "particle_type_name", name);
+
+  if (ctx.step_description.m_types.contains(name)) {
+    return std::unique_ptr<ForwardingListener>(new TrailListener(name));
+  }
+  else {
+    return {};
+  }
+}
+
 BLI_LAZY_INIT(StringMap<ForceFromNodeCallback>, get_force_builders)
 {
   StringMap<ForceFromNodeCallback> map;
@@ -424,4 +439,11 @@ BLI_LAZY_INIT(StringMap<EmitterFromNodeCallback>, get_emitter_builders)
   return map;
 }
 
+BLI_LAZY_INIT(StringMap<ForwardingListenerFromNodeCallback>, get_forwarding_listener_builders)
+{
+  StringMap<ForwardingListenerFromNodeCallback> map;
+  map.add_new("bp_ParticleTrailsNode", BUILD_FORWARDING_LISTENER_trails);
+  return map;
+}
+
 }  // namespace BParticles
diff --git a/source/blender/simulations/bparticles/inserters.hpp b/source/blender/simulations/bparticles/inserters.hpp
index 4e300513f3e..845a5a961e7 100644
--- a/source/blender/simulations/bparticles/inserters.hpp
+++ b/source/blender/simulations/bparticles/inserters.hpp
@@ -40,4 +40,9 @@ using EmitterFromNodeCallback = std::function<std::unique_ptr<Emitter>(
 
 StringMap<EmitterFromNodeCallback> &get_emitter_builders();
 
+using ForwardingListenerFromNodeCallback =
+    std::function<std::unique_ptr<ForwardingListener>(BuildContext &ctx, bNode *bnode)>;
+
+StringMap<ForwardingListenerFromNodeCallback> &get_forwarding_listener_builders();
+
 }  // namespace BParticles
diff --git a/source/blender/simulations/bparticles/node_frontend.cpp b/source/blender/simulations/bparticles/node_frontend.cpp
index 70aff0ce7a8..185c61cb535 100644
--- a/source/blender/simulations/bparticles/node_frontend.cpp
+++ b/source/blender/simulations/bparticles/node_frontend.cpp
@@ -60,6 +60,21 @@ std::unique_ptr<StepDescription> step_description_from_node_tree(IndexedNodeTree
     }
   }
 
+  for (auto item : get_forwarding_listener_builders().items()) {
+    for (bNode *bnode : indexed_tree.nodes_with_idname(item.key)) {
+      bNodeSocket *listener_output = bSocketList(bnode->outputs).get(0);
+      for (SocketWithNode linke

@@ Diff output truncated at 10240 characters. @@



More information about the Bf-blender-cvs mailing list