Commit: 5fc8222babe60b218b854b4651814c84c0a23983
Author: Lukas Tönne
Date:   Thu Dec 10 09:40:50 2015 +0100
Branches: object_nodes
https://developer.blender.org/rB5fc8222babe60b218b854b4651814c84c0a23983

Basic randomization node, producing PRNG values based on either int or float 
input.

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

M       release/scripts/nodes/common_nodes.py
M       release/scripts/nodes/geometry_nodes.py
M       source/blender/blenlib/BLI_rand.h
M       source/blender/blenlib/intern/rand.c
M       source/blender/blenvm/bvm/bvm_eval.cc
M       source/blender/blenvm/bvm/bvm_eval_math.h
M       source/blender/blenvm/bvm/bvm_opcode.h
M       source/blender/blenvm/compile/bvm_nodegraph.cc

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

diff --git a/release/scripts/nodes/common_nodes.py 
b/release/scripts/nodes/common_nodes.py
index 4fa8d50..810bcac 100644
--- a/release/scripts/nodes/common_nodes.py
+++ b/release/scripts/nodes/common_nodes.py
@@ -439,6 +439,52 @@ class GetScaleNode(CommonNodeBase, ObjectNode):
         compiler.map_output(0, node.outputs[0])
 
 
+class RandomNode(CommonNodeBase, ObjectNode):
+    '''Produce random values'''
+    bl_idname = 'ObjectRandomNode'
+    bl_label = 'Random'
+
+    def _enable_input(self, index):
+        for i, s in enumerate(self.inputs):
+            s.enabled = (i == index)
+
+    seed = IntProperty(name="Seed", default=0, subtype='UNSIGNED')
+
+    _mode_items = [
+        ('INT', 'Integer', 'Use integer input', 0, 0),
+        ('FLOAT', 'Float', 'Use floating point input', 0, 1),
+        ]
+    def _mode_update(self, context):
+        if self.mode == 'INT':
+            self._enable_input(0)
+        elif self.mode == 'FLOAT':
+            self._enable_input(1)
+    mode = EnumProperty(name="Mode", description="Type of input value", 
items=_mode_items,
+                        default='INT', update=_mode_update)
+
+    def draw_buttons(self, context, layout):
+        layout.prop(self, "mode")
+        layout.prop(self, "seed")
+
+    def init(self, context):
+        self.inputs.new('NodeSocketInt', "Value")
+        self.inputs.new('NodeSocketFloat', "Value")
+        self.outputs.new('NodeSocketInt', "Random")
+        self.outputs.new('NodeSocketFloat', "Random")
+        self._enable_input(0)
+
+    def compile(self, compiler):
+        if self.mode == 'INT':
+            node = compiler.add_node("INT_TO_RANDOM")
+            compiler.map_input(0, node.inputs[1])
+        elif self.mode == 'FLOAT':
+            node = compiler.add_node("FLOAT_TO_RANDOM")
+            compiler.map_input(1, node.inputs[1])
+        node.inputs[0].set_value(self.seed)
+        compiler.map_output(0, node.outputs[0])
+        compiler.map_output(1, node.outputs[1])
+
+
 class TextureCloudsNode(CommonNodeBase, ObjectNode):
     '''Clouds texture'''
     bl_idname = 'ObjectTextureCloudsNode'
@@ -509,7 +555,7 @@ class TextureVoronoiNode(CommonNodeBase, ObjectNode):
         compiler.map_input(0, node_scale.inputs[0])
         compiler.map_input(1, node_scale.inputs[1])
         compiler.link(node_scale.outputs[0], node.inputs["position"])
-        
+
         compiler.map_input(2, node.inputs["minkowski_exponent"])
         compiler.map_input(3, node.inputs["w1"])
         compiler.map_input(4, node.inputs["w2"])
diff --git a/release/scripts/nodes/geometry_nodes.py 
b/release/scripts/nodes/geometry_nodes.py
index 13ec2c3..b998a97 100644
--- a/release/scripts/nodes/geometry_nodes.py
+++ b/release/scripts/nodes/geometry_nodes.py
@@ -268,6 +268,7 @@ def register():
             NodeItem("ObjectGetEulerNode"),
             NodeItem("ObjectGetAxisAngleNode"),
             NodeItem("ObjectGetScaleNode"),
+            NodeItem("ObjectRandomNode"),
             ]),
         GeometryNodeCategory("GEO_TEXTURE", "Texture", items=[
             NodeItem("ObjectTextureCloudsNode"),
diff --git a/source/blender/blenlib/BLI_rand.h 
b/source/blender/blenlib/BLI_rand.h
index 7b84ddd..c86c9ae 100644
--- a/source/blender/blenlib/BLI_rand.h
+++ b/source/blender/blenlib/BLI_rand.h
@@ -74,6 +74,7 @@ float   BLI_frand(void) ATTR_WARN_UNUSED_RESULT;
 void    BLI_frand_unit_v3(float v[3]);
 
 /** Return a pseudo-random (hash) float from an integer value */
+int            BLI_hash_rand(unsigned int seed) ATTR_WARN_UNUSED_RESULT;
 float  BLI_hash_frand(unsigned int seed) ATTR_WARN_UNUSED_RESULT;
 
 /** Shuffle an array randomly using the given seed.
diff --git a/source/blender/blenlib/intern/rand.c 
b/source/blender/blenlib/intern/rand.c
index 66c568a..1b42ab7 100644
--- a/source/blender/blenlib/intern/rand.c
+++ b/source/blender/blenlib/intern/rand.c
@@ -245,6 +245,14 @@ void BLI_frand_unit_v3(float v[3])
        BLI_rng_get_float_unit_v3(&theBLI_rng, v);
 }
 
+int BLI_hash_rand(unsigned int seed)
+{
+       RNG rng;
+
+       BLI_rng_srandom(&rng, seed);
+       return BLI_rng_get_int(&rng);
+}
+
 float BLI_hash_frand(unsigned int seed)
 {
        RNG rng;
diff --git a/source/blender/blenvm/bvm/bvm_eval.cc 
b/source/blender/blenvm/bvm/bvm_eval.cc
index 9787110..62bfe1f 100644
--- a/source/blender/blenvm/bvm/bvm_eval.cc
+++ b/source/blender/blenvm/bvm/bvm_eval.cc
@@ -664,7 +664,24 @@ void EvalContext::eval_instructions(const EvalGlobals 
*globals, const Function *
                                eval_op_mix_rgb(stack, mode, offset_col_a, 
offset_col_b, offset_fac, offset_r);
                                break;
                        }
-                               
+                       
+                       case OP_INT_TO_RANDOM: {
+                               int seed = fn->read_int(&instr);
+                               StackIndex offset = 
fn->read_stack_index(&instr);
+                               StackIndex offset_irandom = 
fn->read_stack_index(&instr);
+                               StackIndex offset_frandom = 
fn->read_stack_index(&instr);
+                               eval_op_int_to_random(stack, (uint64_t)seed, 
offset, offset_irandom, offset_frandom);
+                               break;
+                       }
+                       case OP_FLOAT_TO_RANDOM: {
+                               int seed = fn->read_int(&instr);
+                               StackIndex offset = 
fn->read_stack_index(&instr);
+                               StackIndex offset_irandom = 
fn->read_stack_index(&instr);
+                               StackIndex offset_frandom = 
fn->read_stack_index(&instr);
+                               eval_op_float_to_random(stack, (uint64_t)seed, 
offset, offset_irandom, offset_frandom);
+                               break;
+                       }
+                       
                        case OP_TEX_PROC_VORONOI: {
                                int distance_metric = fn->read_int(&instr);
                                int color_type = fn->read_int(&instr);
diff --git a/source/blender/blenvm/bvm/bvm_eval_math.h 
b/source/blender/blenvm/bvm/bvm_eval_math.h
index bf8a1f2..fa1e640 100644
--- a/source/blender/blenvm/bvm/bvm_eval_math.h
+++ b/source/blender/blenvm/bvm/bvm_eval_math.h
@@ -34,10 +34,12 @@
 
 extern "C" {
 #include "BLI_math.h"
+#include "BLI_rand.h"
 }
 
 #include "bvm_eval_common.h"
 
+#include "bvm_util_hash.h"
 #include "bvm_util_math.h"
 
 namespace bvm {
@@ -418,6 +420,28 @@ static void eval_op_mul_matrix44_float4(float *stack, 
StackIndex offset_a, Stack
        stack_store_float4(stack, offset_r, r);
 }
 
+static void eval_op_int_to_random(float *stack, uint64_t seed, StackIndex 
offset, StackIndex offset_irandom, StackIndex offset_frandom)
+{
+       union { uint32_t u; int x; } c;
+       c.x = stack_load_int(stack, offset);
+       
+       uint32_t r = BLI_hash_rand(hash_combine(c.u, seed));
+       
+       stack_store_int(stack, offset_irandom, (int)r);
+       stack_store_float(stack, offset_frandom, (float)r / (0xFFFFFFFF));
+}
+
+static void eval_op_float_to_random(float *stack, uint64_t seed, StackIndex 
offset, StackIndex offset_irandom, StackIndex offset_frandom)
+{
+       union { uint32_t u; float x; } c;
+       c.x = stack_load_float(stack, offset);
+       
+       uint32_t r = BLI_hash_rand(hash_combine(c.u, seed));
+       
+       stack_store_int(stack, offset_irandom, (int)r);
+       stack_store_float(stack, offset_frandom, (float)r / (0xFFFFFFFF));
+}
+
 } /* namespace bvm */
 
 #endif /* __BVM_EVAL_MATH_H__ */
diff --git a/source/blender/blenvm/bvm/bvm_opcode.h 
b/source/blender/blenvm/bvm/bvm_opcode.h
index 3d4996c..d3dc35b 100644
--- a/source/blender/blenvm/bvm/bvm_opcode.h
+++ b/source/blender/blenvm/bvm/bvm_opcode.h
@@ -109,6 +109,9 @@ enum OpCode {
        
        OP_MIX_RGB,
        
+       OP_INT_TO_RANDOM,
+       OP_FLOAT_TO_RANDOM,
+       
        OP_TEX_PROC_VORONOI,
        OP_TEX_PROC_BLEND,
        OP_TEX_PROC_MAGIC,
diff --git a/source/blender/blenvm/compile/bvm_nodegraph.cc 
b/source/blender/blenvm/compile/bvm_nodegraph.cc
index 4089c39..f05bcf8 100644
--- a/source/blender/blenvm/compile/bvm_nodegraph.cc
+++ b/source/blender/blenvm/compile/bvm_nodegraph.cc
@@ -1134,6 +1134,9 @@ OpCode get_opcode_from_node_type(const string &node)
        
        NODETYPE(MIX_RGB);
        
+       NODETYPE(INT_TO_RANDOM);
+       NODETYPE(FLOAT_TO_RANDOM);
+       
        NODETYPE(TEX_PROC_VORONOI);
        NODETYPE(TEX_PROC_CLOUDS);
        
@@ -1356,6 +1359,18 @@ static void register_opcode_node_types()
        nt->add_input("color2", BVM_FLOAT4, float4(0.0f, 0.0f, 0.0f, 1.0f));
        nt->add_output("color", BVM_FLOAT4);
        
+       nt = NodeGraph::add_function_node_type("INT_TO_RANDOM");
+       nt->add_input("seed", BVM_INT, 0, INPUT_CONSTANT);
+       nt->add_input("value", BVM_INT, 0);
+       nt->add_output("irandom", BVM_INT);
+       nt->add_output("frandom", BVM_FLOAT);
+       
+       nt = NodeGraph::add_function_node_type("FLOAT_TO_RANDOM");
+       nt->add_input("seed", BVM_INT, 0, INPUT_CONSTANT);
+       nt->add_input("value", BVM_FLOAT, 0);
+       nt->add_output("irandom", BVM_INT);
+       nt->add_output("frandom", BVM_FLOAT);
+       
        nt = NodeGraph::add_function_node_type("TEX_PROC_VORONOI");
        nt->add_input("distance_metric", BVM_INT, 0, INPUT_CONSTANT);
        nt->add_input("color_type", BVM_INT, 0, INPUT_CONSTANT);

_______________________________________________
Bf-blender-cvs mailing list
[email protected]
http://lists.blender.org/mailman/listinfo/bf-blender-cvs

Reply via email to