[Bf-blender-cvs] [d2fe280a6ee] functions: improved Change Particle Velocity node

Jacques Lucke noreply at git.blender.org
Mon Sep 2 16:56:51 CEST 2019


Commit: d2fe280a6ee8f51cb5eff8420bedd217952309fd
Author: Jacques Lucke
Date:   Mon Sep 2 16:09:52 2019 +0200
Branches: functions
https://developer.blender.org/rBd2fe280a6ee8f51cb5eff8420bedd217952309fd

improved Change Particle Velocity node

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

D	release/scripts/startup/nodes/bparticle_nodes/change_direction.py
A	release/scripts/startup/nodes/bparticle_nodes/change_velocity.py
M	source/blender/simulations/bparticles/actions.cpp
M	source/blender/simulations/bparticles/actions.hpp
M	source/blender/simulations/bparticles/node_frontend.cpp

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

diff --git a/release/scripts/startup/nodes/bparticle_nodes/change_direction.py b/release/scripts/startup/nodes/bparticle_nodes/change_direction.py
deleted file mode 100644
index e716e995472..00000000000
--- a/release/scripts/startup/nodes/bparticle_nodes/change_direction.py
+++ /dev/null
@@ -1,12 +0,0 @@
-import bpy
-from bpy.props import *
-from .. base import BParticlesNode
-from .. node_builder import NodeBuilder
-
-class ChangeParticleDirectionNode(bpy.types.Node, BParticlesNode):
-    bl_idname = "bp_ChangeParticleDirectionNode"
-    bl_label = "Change Particle Direction"
-
-    def declaration(self, builder: NodeBuilder):
-        builder.fixed_input("direction", "Direction", "Vector")
-        builder.execute_output("execute", "Execute")
diff --git a/release/scripts/startup/nodes/bparticle_nodes/change_velocity.py b/release/scripts/startup/nodes/bparticle_nodes/change_velocity.py
new file mode 100644
index 00000000000..0dab6853127
--- /dev/null
+++ b/release/scripts/startup/nodes/bparticle_nodes/change_velocity.py
@@ -0,0 +1,28 @@
+import bpy
+from bpy.props import *
+from .. base import BParticlesNode
+from .. node_builder import NodeBuilder
+
+class ChangeParticleVelocityNode(bpy.types.Node, BParticlesNode):
+    bl_idname = "bp_ChangeParticleVelocityNode"
+    bl_label = "Change Particle Velocity"
+
+    mode = EnumProperty(
+        name="Mode",
+        items=[
+            ('SET', "Set", "Set a specific velocity", 'NONE', 0),
+            ('RANDOMIZE', "Randomize", "Apply some randomization to the velocity", 'NONE', 1),
+        ],
+        update= BParticlesNode.sync_tree,
+    )
+
+    def declaration(self, builder: NodeBuilder):
+        if self.mode == 'SET':
+            builder.fixed_input("velocity", "Velocity", "Vector")
+        elif self.mode == 'RANDOMIZE':
+            builder.fixed_input("randomness", "Randomness", "Float", default=0.5)
+
+        builder.execute_output("execute", "Execute")
+
+    def draw(self, layout):
+        layout.prop(self, "mode", text="")
diff --git a/source/blender/simulations/bparticles/actions.cpp b/source/blender/simulations/bparticles/actions.cpp
index d5cefb171ca..61cb76c91de 100644
--- a/source/blender/simulations/bparticles/actions.cpp
+++ b/source/blender/simulations/bparticles/actions.cpp
@@ -17,22 +17,31 @@ void ActionSequence::execute(ActionInterface &interface)
   }
 }
 
-void ChangeDirectionAction::execute(ActionInterface &interface)
+static float random_number()
+{
+  static uint number = 0;
+  number++;
+  return BLI_hash_int_01(number) * 2.0f - 1.0f;
+}
+
+static float3 random_direction()
+{
+  return float3(random_number(), random_number(), random_number());
+}
+
+static void update_position_and_velocity_offsets(ActionInterface &interface)
 {
   ParticleSet particles = interface.particles();
+  AttributeArrays attribute_offsets = interface.attribute_offsets();
   auto velocities = particles.attributes().get<float3>("Velocity");
-  auto position_offsets = interface.attribute_offsets().try_get<float3>("Position");
-  auto velocity_offsets = interface.attribute_offsets().try_get<float3>("Velocity");
-
-  auto inputs = m_compute_inputs->compute(interface);
+  auto position_offsets = attribute_offsets.try_get<float3>("Position");
+  auto velocity_offsets = attribute_offsets.try_get<float3>("Velocity");
 
   for (uint pindex : particles.pindices()) {
-    float3 direction = inputs->get<float3>("Direction", 0, pindex);
-
-    velocities[pindex] = direction;
+    float3 velocity = velocities[pindex];
 
     if (position_offsets.has_value()) {
-      position_offsets.value()[pindex] = direction * interface.remaining_time_in_step(pindex);
+      position_offsets.value()[pindex] = velocity * interface.remaining_time_in_step(pindex);
     }
     if (velocity_offsets.has_value()) {
       velocity_offsets.value()[pindex] = float3(0);
@@ -40,33 +49,55 @@ void ChangeDirectionAction::execute(ActionInterface &interface)
   }
 }
 
-void ChangeColorAction::execute(ActionInterface &interface)
+void SetVelocityAction::execute(ActionInterface &interface)
 {
   ParticleSet particles = interface.particles();
-  auto colors = particles.attributes().get<rgba_f>("Color");
+  auto velocities = particles.attributes().get<float3>("Velocity");
 
   auto inputs = m_compute_inputs->compute(interface);
+
   for (uint pindex : particles.pindices()) {
-    rgba_f color = inputs->get<rgba_f>("Color", 0, pindex);
-    colors[pindex] = color;
+    float3 velocity = inputs->get<float3>("Velocity", 0, pindex);
+    velocities[pindex] = velocity;
   }
+
+  update_position_and_velocity_offsets(interface);
 }
 
-void KillAction::execute(ActionInterface &interface)
+void RandomizeVelocityAction::execute(ActionInterface &interface)
 {
-  interface.kill(interface.particles().pindices());
+  ParticleSet particles = interface.particles();
+  auto velocities = particles.attributes().get<float3>("Velocity");
+
+  auto inputs = m_compute_inputs->compute(interface);
+
+  for (uint pindex : particles.pindices()) {
+    float randomness = inputs->get<float>("Randomness", 0, pindex);
+    float3 old_velocity = velocities[pindex];
+    float old_speed = old_velocity.length();
+
+    float3 velocity_offset = random_direction().normalized() * old_speed * randomness;
+    velocities[pindex] += velocity_offset;
+  }
+
+  update_position_and_velocity_offsets(interface);
 }
 
-static float random_number()
+void ChangeColorAction::execute(ActionInterface &interface)
 {
-  static uint number = 0;
-  number++;
-  return BLI_hash_int_01(number) * 2.0f - 1.0f;
+  ParticleSet particles = interface.particles();
+  auto colors = particles.attributes().get<rgba_f>("Color");
+
+  auto inputs = m_compute_inputs->compute(interface);
+  for (uint pindex : particles.pindices()) {
+    rgba_f color = inputs->get<rgba_f>("Color", 0, pindex);
+    colors[pindex] = color;
+  }
 }
 
-static float3 random_direction()
+void KillAction::execute(ActionInterface &interface)
 {
-  return float3(random_number(), random_number(), random_number());
+  interface.kill(interface.particles().pindices());
 }
 
 void ExplodeAction::execute(ActionInterface &interface)
diff --git a/source/blender/simulations/bparticles/actions.hpp b/source/blender/simulations/bparticles/actions.hpp
index 0062689aec2..9e6a304257c 100644
--- a/source/blender/simulations/bparticles/actions.hpp
+++ b/source/blender/simulations/bparticles/actions.hpp
@@ -25,12 +25,25 @@ class KillAction : public Action {
   void execute(ActionInterface &interface) override;
 };
 
-class ChangeDirectionAction : public Action {
+class SetVelocityAction : public Action {
  private:
   std::unique_ptr<ParticleFunction> m_compute_inputs;
 
  public:
-  ChangeDirectionAction(std::unique_ptr<ParticleFunction> compute_inputs)
+  SetVelocityAction(std::unique_ptr<ParticleFunction> compute_inputs)
+      : m_compute_inputs(std::move(compute_inputs))
+  {
+  }
+
+  void execute(ActionInterface &interface) override;
+};
+
+class RandomizeVelocityAction : public Action {
+ private:
+  std::unique_ptr<ParticleFunction> m_compute_inputs;
+
+ public:
+  RandomizeVelocityAction(std::unique_ptr<ParticleFunction> compute_inputs)
       : m_compute_inputs(std::move(compute_inputs))
   {
   }
diff --git a/source/blender/simulations/bparticles/node_frontend.cpp b/source/blender/simulations/bparticles/node_frontend.cpp
index c95ca097c19..dcdd1bf315b 100644
--- a/source/blender/simulations/bparticles/node_frontend.cpp
+++ b/source/blender/simulations/bparticles/node_frontend.cpp
@@ -88,8 +88,8 @@ static std::unique_ptr<Action> ACTION_kill(VTreeDataGraph &UNUSED(vtree_data_gra
   return std::unique_ptr<Action>(new KillAction());
 }
 
-static std::unique_ptr<Action> ACTION_change_direction(VTreeDataGraph &vtree_data_graph,
-                                                       VirtualSocket *execute_vsocket)
+static std::unique_ptr<Action> ACTION_change_velocity(VTreeDataGraph &vtree_data_graph,
+                                                      VirtualSocket *execute_vsocket)
 {
   VirtualNode *vnode = execute_vsocket->vnode();
 
@@ -99,7 +99,17 @@ static std::unique_ptr<Action> ACTION_change_direction(VTreeDataGraph &vtree_dat
   }
   std::unique_ptr<ParticleFunction> compute_inputs_fn = fn_or_error.extract_value();
 
-  Action *action = new ChangeDirectionAction(std::move(compute_inputs_fn));
+  PointerRNA rna = vnode->rna();
+  int mode = RNA_enum_get(&rna, "mode");
+
+  Action *action = nullptr;
+  if (mode == 0) {
+    action = new SetVelocityAction(std::move(compute_inputs_fn));
+  }
+  else if (mode == 1) {
+    action = new RandomizeVelocityAction(std::move(compute_inputs_fn));
+  }
+
   return std::unique_ptr<Action>(action);
 }
 
@@ -161,7 +171,7 @@ BLI_LAZY_INIT_STATIC(StringMap<ActionParserCallback>, get_action_parsers)
 {
   StringMap<ActionParserCallback> map;
   map.add_new("bp_KillParticleNode", ACTION_kill);
-  map.add_new("bp_ChangeParticleDirectionNode", ACTION_change_direction);
+  map.add_new("bp_ChangeParticleVelocityNode", ACTION_change_velocity);
   map.add_new("bp_ExplodeParticleNode", ACTION_explode);
   map.add_new("bp_ParticleConditionNode", ACTION_condition);
   map.add_new("bp_ChangeParticleColorNode", ACTION_change_color);



More information about the Bf-blender-cvs mailing list