[Bf-blender-cvs] [2746f423bdf] functions: initial turbulence node

Jacques Lucke noreply at git.blender.org
Fri Jul 5 17:39:05 CEST 2019


Commit: 2746f423bdfe13d53856b1776853bfe7fb33edde
Author: Jacques Lucke
Date:   Fri Jul 5 17:38:29 2019 +0200
Branches: functions
https://developer.blender.org/rB2746f423bdfe13d53856b1776853bfe7fb33edde

initial turbulence node

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

A	release/scripts/startup/nodes/bparticle_nodes/turbulence_force.py
M	source/blender/simulations/bparticles/c_wrapper.cpp
M	source/blender/simulations/bparticles/forces.cpp
M	source/blender/simulations/bparticles/forces.hpp

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

diff --git a/release/scripts/startup/nodes/bparticle_nodes/turbulence_force.py b/release/scripts/startup/nodes/bparticle_nodes/turbulence_force.py
new file mode 100644
index 00000000000..0c16147550b
--- /dev/null
+++ b/release/scripts/startup/nodes/bparticle_nodes/turbulence_force.py
@@ -0,0 +1,12 @@
+import bpy
+from bpy.props import *
+from .. base import BParticlesNode
+from .. socket_builder import SocketBuilder
+
+class TurbulenceForceNode(bpy.types.Node, BParticlesNode):
+    bl_idname = "bp_TurbulenceForceNode"
+    bl_label = "Turbulence Force"
+
+    def declaration(self, builder : SocketBuilder):
+        builder.fixed_input("strength", "Strength", "Vector")
+        builder.particle_modifier_output("force", "Force")
diff --git a/source/blender/simulations/bparticles/c_wrapper.cpp b/source/blender/simulations/bparticles/c_wrapper.cpp
index 4996e426f35..782af8ed106 100644
--- a/source/blender/simulations/bparticles/c_wrapper.cpp
+++ b/source/blender/simulations/bparticles/c_wrapper.cpp
@@ -467,6 +467,30 @@ static void INSERT_FORCE_gravity(bNode *force_node,
   }
 }
 
+static void INSERT_FORCE_turbulence(bNode *force_node,
+                                    IndexedNodeTree &indexed_tree,
+                                    FN::DataFlowNodes::GeneratedGraph &data_graph,
+                                    ModifierStepDescription &step_description)
+{
+  BLI_assert(STREQ(force_node->idname, "bp_TurbulenceForceNode"));
+  bSocketList node_inputs(force_node->inputs);
+  bSocketList node_outputs(force_node->outputs);
+
+  for (SocketWithNode linked : indexed_tree.linked(node_outputs.get(0))) {
+    if (!is_particle_type_node(linked.node)) {
+      continue;
+    }
+
+    SharedFunction fn = create_function(
+        indexed_tree, data_graph, {node_inputs.get(0)}, force_node->name);
+
+    Force *force = FORCE_turbulence(fn);
+
+    bNode *type_node = linked.node;
+    step_description.m_types.lookup_ref(type_node->name)->m_integrator->m_forces.append(force);
+  }
+}
+
 static ModifierStepDescription *step_description_from_node_tree(bNodeTree *btree)
 {
   SCOPED_TIMER(__func__);
@@ -481,6 +505,7 @@ static ModifierStepDescription *step_description_from_node_tree(bNodeTree *btree
 
   SmallMap<std::string, ModifierInserter> modifier_inserters;
   event_inserters.add_new("bp_GravityForceNode", INSERT_FORCE_gravity);
+  event_inserters.add_new("bp_TurbulenceForceNode", INSERT_FORCE_turbulence);
 
   ModifierStepDescription *step_description = new ModifierStepDescription();
 
diff --git a/source/blender/simulations/bparticles/forces.cpp b/source/blender/simulations/bparticles/forces.cpp
index 4b3215720eb..013fb3c8134 100644
--- a/source/blender/simulations/bparticles/forces.cpp
+++ b/source/blender/simulations/bparticles/forces.cpp
@@ -37,23 +37,34 @@ class GravityForce : public Force {
   };
 };
 
-class TurbulenceForce : public BParticles::Force {
+class TurbulenceForce : public Force {
  private:
-  float m_strength;
+  SharedFunction m_compute_strength_fn;
+  TupleCallBody *m_compute_strength_body;
 
  public:
-  TurbulenceForce(float strength) : m_strength(strength)
+  TurbulenceForce(SharedFunction &compute_strength_fn) : m_compute_strength_fn(compute_strength_fn)
   {
+    m_compute_strength_body = m_compute_strength_fn->body<TupleCallBody>();
   }
 
   void add_force(ParticlesBlock &block, ArrayRef<float3> r_force) override
   {
     auto positions = block.attributes().get_float3("Position");
 
+    FN_TUPLE_CALL_ALLOC_TUPLES(m_compute_strength_body, fn_in, fn_out);
+    FN::ExecutionStack stack;
+    FN::ExecutionContext execution_context(stack);
+    m_compute_strength_body->call(fn_in, fn_out, execution_context);
+
+    float3 strength = fn_out.get<float3>(0);
+
     for (uint pindex = 0; pindex < block.active_amount(); pindex++) {
       float3 pos = positions[pindex];
-      float value = BLI_hnoise(0.5f, pos.x, pos.y, pos.z);
-      r_force[pindex].z += value * m_strength;
+      float x = (BLI_gNoise(0.5f, pos.x, pos.y, pos.z + 1000.0f, false, 1) - 0.5f) * strength.x;
+      float y = (BLI_gNoise(0.5f, pos.x, pos.y + 1000.0f, pos.z, false, 1) - 0.5f) * strength.y;
+      float z = (BLI_gNoise(0.5f, pos.x + 1000.0f, pos.y, pos.z, false, 1) - 0.5f) * strength.z;
+      r_force[pindex] += {x, y, z};
     }
   }
 };
@@ -63,9 +74,9 @@ Force *FORCE_gravity(SharedFunction &compute_acceleration_fn)
   return new GravityForce(compute_acceleration_fn);
 }
 
-Force *FORCE_turbulence(float strength)
+Force *FORCE_turbulence(SharedFunction &compute_strength_fn)
 {
-  return new TurbulenceForce(strength);
+  return new TurbulenceForce(compute_strength_fn);
 }
 
 }  // namespace BParticles
diff --git a/source/blender/simulations/bparticles/forces.hpp b/source/blender/simulations/bparticles/forces.hpp
index 08680b7da18..4ee25b7dad9 100644
--- a/source/blender/simulations/bparticles/forces.hpp
+++ b/source/blender/simulations/bparticles/forces.hpp
@@ -12,6 +12,6 @@ class Force {
 };
 
 Force *FORCE_gravity(SharedFunction &compute_acceleration_fn);
-Force *FORCE_turbulence(float strength);
+Force *FORCE_turbulence(SharedFunction &compute_strength_fn);
 
 }  // namespace BParticles



More information about the Bf-blender-cvs mailing list