[Bf-blender-cvs] [375ac4f37ae] functions: new Random Vector node

Jacques Lucke noreply at git.blender.org
Wed Dec 18 13:53:32 CET 2019


Commit: 375ac4f37ae1d8402dc0038e34a2edef866fd949
Author: Jacques Lucke
Date:   Wed Dec 18 11:42:42 2019 +0100
Branches: functions
https://developer.blender.org/rB375ac4f37ae1d8402dc0038e34a2edef866fd949

new Random Vector node

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

M	release/scripts/startup/nodes/function_nodes/noise.py
M	source/blender/functions/intern/inlined_tree_multi_function_network/mappings_nodes.cc
M	source/blender/functions/intern/multi_functions/mixed.cc
M	source/blender/functions/intern/multi_functions/mixed.h

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

diff --git a/release/scripts/startup/nodes/function_nodes/noise.py b/release/scripts/startup/nodes/function_nodes/noise.py
index 46f3e4b827c..c2c9ed64c4b 100644
--- a/release/scripts/startup/nodes/function_nodes/noise.py
+++ b/release/scripts/startup/nodes/function_nodes/noise.py
@@ -4,6 +4,7 @@ from bpy.props import *
 from .. base import FunctionNode
 from .. node_builder import NodeBuilder
 
+
 class PerlinNoiseNode(bpy.types.Node, FunctionNode):
     bl_idname = "fn_PerlinNoiseNode"
     bl_label = "Perlin Noise"
@@ -20,12 +21,25 @@ class RandomFloatNode(bpy.types.Node, FunctionNode):
     bl_idname = "fn_RandomFloatNode"
     bl_label = "Random Float"
 
+    node_seed: IntProperty(
+        name="Node Seed",
+    )
+
+    def init_props(self):
+        self.node_seed = new_node_seed()
+
     def declaration(self, builder: NodeBuilder):
         builder.fixed_input("min", "Min", "Float", default=0)
         builder.fixed_input("max", "Max", "Float", default=1)
         builder.fixed_input("seed", "Seed", "Integer")
         builder.fixed_output("value", "Value", "Float")
 
+    def draw_advanced(self, layout):
+        layout.prop(self, "node_seed")
+
+    def duplicate(self, src_node):
+        self.node_seed = new_node_seed()
+
 
 class RandomFloatsNode(bpy.types.Node, FunctionNode):
     bl_idname = "fn_RandomFloatsNode"
@@ -35,6 +49,9 @@ class RandomFloatsNode(bpy.types.Node, FunctionNode):
         name="Node Seed",
     )
 
+    def init_props(self):
+        self.node_seed = new_node_seed()
+
     def declaration(self, builder: NodeBuilder):
         builder.fixed_input("amount", "Amount", "Integer", default=10)
         builder.fixed_input("min", "Min", "Float")
@@ -46,4 +63,31 @@ class RandomFloatsNode(bpy.types.Node, FunctionNode):
         layout.prop(self, "node_seed")
 
     def duplicate(self, src_node):
-        self.node_seed = random.randint(0, 10000)
+        self.node_seed = new_node_seed()
+
+
+class RandomVectorNode(bpy.types.Node, FunctionNode):
+    bl_idname = "fn_RandomVectorNode"
+    bl_label = "Random Vector"
+
+    node_seed: IntProperty(
+        name="Node Seed",
+    )
+
+    def init_props(self):
+        self.node_seed = new_node_seed()
+
+    def declaration(self, builder: NodeBuilder):
+        builder.fixed_input("amplitude", "Amplitude", "Vector", default=(1, 1, 1))
+        builder.fixed_input("seed", "Seed", "Integer")
+        builder.fixed_output("vector", "Vector", "Vector")
+
+    def draw_advanced(self, layout):
+        layout.prop(self, "node_seed")
+
+    def duplicate(self, src_node):
+        self.node_seed = new_node_seed()
+
+
+def new_node_seed():
+    return random.randint(0, 10000)
diff --git a/source/blender/functions/intern/inlined_tree_multi_function_network/mappings_nodes.cc b/source/blender/functions/intern/inlined_tree_multi_function_network/mappings_nodes.cc
index 84c70cc9da7..2ccc6515f3f 100644
--- a/source/blender/functions/intern/inlined_tree_multi_function_network/mappings_nodes.cc
+++ b/source/blender/functions/intern/inlined_tree_multi_function_network/mappings_nodes.cc
@@ -467,7 +467,8 @@ static void INSERT_map_range(VNodeMFNetworkBuilder &builder)
 
 static void INSERT_random_float(VNodeMFNetworkBuilder &builder)
 {
-  builder.set_constructed_matching_fn<MF_RandomFloat>();
+  uint node_seed = (uint)RNA_int_get(builder.rna(), "node_seed");
+  builder.set_constructed_matching_fn<MF_RandomFloat>(node_seed);
 }
 
 static void INSERT_random_floats(VNodeMFNetworkBuilder &builder)
@@ -476,6 +477,12 @@ static void INSERT_random_floats(VNodeMFNetworkBuilder &builder)
   builder.set_constructed_matching_fn<MF_RandomFloats>(node_seed);
 }
 
+static void INSERT_random_vector(VNodeMFNetworkBuilder &builder)
+{
+  uint node_seed = (uint)RNA_int_get(builder.rna(), "node_seed");
+  builder.set_constructed_matching_fn<MF_RandomVector>(node_seed);
+}
+
 static void INSERT_value(VNodeMFNetworkBuilder &builder)
 {
   const XOutputSocket &xsocket = builder.xnode().output(0);
@@ -541,6 +548,7 @@ void add_inlined_tree_node_mapping_info(VTreeMultiFunctionMappings &mappings)
   mappings.xnode_inserters.add_new("fn_FloatClampNode", INSERT_clamp_float);
   mappings.xnode_inserters.add_new("fn_RandomFloatNode", INSERT_random_float);
   mappings.xnode_inserters.add_new("fn_RandomFloatsNode", INSERT_random_floats);
+  mappings.xnode_inserters.add_new("fn_RandomVectorNode", INSERT_random_vector);
   mappings.xnode_inserters.add_new("fn_ValueNode", INSERT_value);
   mappings.xnode_inserters.add_new("fn_EmitterTimeInfoNode", INSERT_emitter_time_info);
   mappings.xnode_inserters.add_new("fn_SampleObjectSurfaceNode", INSERT_sample_object_surface);
diff --git a/source/blender/functions/intern/multi_functions/mixed.cc b/source/blender/functions/intern/multi_functions/mixed.cc
index 64d59a2cd82..a0c11f5f5c1 100644
--- a/source/blender/functions/intern/multi_functions/mixed.cc
+++ b/source/blender/functions/intern/multi_functions/mixed.cc
@@ -542,7 +542,7 @@ void MF_Clamp::call(MFMask mask, MFParams params, MFContext UNUSED(context)) con
   }
 }
 
-MF_RandomFloat::MF_RandomFloat()
+MF_RandomFloat::MF_RandomFloat(uint seed) : m_seed(seed * 53723457)
 {
   MFSignatureBuilder signature = this->get_builder("Random Float");
   signature.single_input<float>("Min");
@@ -559,7 +559,7 @@ void MF_RandomFloat::call(MFMask mask, MFParams params, MFContext UNUSED(context
   MutableArrayRef<float> r_values = params.uninitialized_single_output<float>(3, "Value");
 
   for (uint i : mask.indices()) {
-    float value = BLI_hash_int_01(seeds[i]);
+    float value = BLI_hash_int_01(seeds[i] ^ m_seed);
     r_values[i] = value * (max_values[i] - min_values[i]) + min_values[i];
   }
 }
@@ -599,6 +599,31 @@ void MF_RandomFloats::call(MFMask mask, MFParams params, MFContext UNUSED(contex
   BLI_rng_free(rng);
 }
 
+MF_RandomVector::MF_RandomVector(uint seed) : m_seed(seed * 56242361)
+{
+  MFSignatureBuilder signature = this->get_builder("Random Vector");
+  signature.single_input<float3>("Amplitude");
+  signature.single_input<int>("Seed");
+  signature.single_output<float3>("Vector");
+}
+
+void MF_RandomVector::call(MFMask mask, MFParams params, MFContext UNUSED(context)) const
+{
+  VirtualListRef<float3> amplitudes = params.readonly_single_input<float3>(0, "Amplitude");
+  VirtualListRef<int> seeds = params.readonly_single_input<int>(1, "Seed");
+  MutableArrayRef<float3> r_vectors = params.uninitialized_single_output<float3>(2, "Vector");
+
+  for (uint i : mask.indices()) {
+    uint seed = seeds[i] ^ m_seed;
+    float x = BLI_hash_int_01(seed * 4521341) - 0.5f;
+    float y = BLI_hash_int_01(seed * 4623413) - 0.5f;
+    float z = BLI_hash_int_01(seed * 7826313) - 0.5f;
+    float3 amplitude = amplitudes[i];
+    float3 vector = float3(x, y, z) * amplitude;
+    r_vectors[i] = vector;
+  }
+}
+
 MF_FindNonClosePoints::MF_FindNonClosePoints()
 {
   MFSignatureBuilder signature = this->get_builder("Remove Close Points");
diff --git a/source/blender/functions/intern/multi_functions/mixed.h b/source/blender/functions/intern/multi_functions/mixed.h
index 927a8957a0d..19ed8912990 100644
--- a/source/blender/functions/intern/multi_functions/mixed.h
+++ b/source/blender/functions/intern/multi_functions/mixed.h
@@ -61,8 +61,11 @@ class MF_TextLength final : public MultiFunction {
 };
 
 class MF_RandomFloat final : public MultiFunction {
+ private:
+  uint m_seed;
+
  public:
-  MF_RandomFloat();
+  MF_RandomFloat(uint seed);
   void call(MFMask mask, MFParams parms, MFContext context) const override;
 };
 
@@ -75,6 +78,15 @@ class MF_RandomFloats final : public MultiFunction {
   void call(MFMask mask, MFParams parms, MFContext context) const override;
 };
 
+class MF_RandomVector final : public MultiFunction {
+ private:
+  uint m_seed;
+
+ public:
+  MF_RandomVector(uint seed);
+  void call(MFMask mask, MFParams parms, MFContext context) const override;
+};
+
 class MF_ContextVertexPosition final : public MultiFunction {
  public:
   MF_ContextVertexPosition();



More information about the Bf-blender-cvs mailing list