Cycles bugfix: [32431] Cycles Math Node : Clamp does not work
authorDalai Felinto <dfelinto@gmail.com>
Wed, 29 Aug 2012 17:30:14 +0000 (17:30 +0000)
committerDalai Felinto <dfelinto@gmail.com>
Wed, 29 Aug 2012 17:30:14 +0000 (17:30 +0000)
the OSL solution is slightly different than the svm, but I think it's fine.

thanks Lukas Toenne for helping with a fix on the original patch

intern/cycles/blender/blender_shader.cpp
intern/cycles/kernel/osl/nodes/node_math.osl
intern/cycles/kernel/svm/svm_math.h
intern/cycles/kernel/svm/svm_types.h
intern/cycles/render/nodes.cpp
intern/cycles/render/nodes.h

index 2c15a60dab61fa2685f5356d63b9c6f3ace3639c..37ab1ddac26bc14bd33e8e06f3ff7919d76da698 100644 (file)
@@ -244,6 +244,7 @@ static ShaderNode *add_node(BL::BlendData b_data, BL::Scene b_scene, ShaderGraph
                        BL::ShaderNodeMath b_math_node(b_node);
                        MathNode *math = new MathNode();
                        math->type = MathNode::type_enum[b_math_node.operation()];
+                       math->use_clamp = b_math_node.use_clamp();
                        node = math;
                        break;
                }
index be9bb71d5113ed9dd2c76a912dd751ee0c746968..b62403600ffacdd8b681b30a5a0a2ea7826be8ce 100644 (file)
@@ -40,6 +40,7 @@ float safe_log(float a, float b)
 
 shader node_math(
        string type = "Add",
+       int Clamp = false,
        float Value1 = 0.0,
        float Value2 = 0.0,
        output float Value = 0.0)
@@ -80,5 +81,8 @@ shader node_math(
                Value = Value1 < Value2;
        if(type == "Greater Than")
                Value = Value1 > Value2;
+
+       if(Clamp)
+               Value = clamp(Value1, 0.0, 1.0);
 }
 
index bc2f774097e86df9a68027f30a2a22099d38a446..e39c7a4ba6cc613a558fc1a269eff860ba2c9f52 100644 (file)
@@ -108,6 +108,8 @@ __device float svm_math(NodeMath type, float Fac1, float Fac2)
                Fac = Fac1 < Fac2;
        else if(type == NODE_MATH_GREATER_THAN)
                Fac = Fac1 > Fac2;
+       else if(type == NODE_MATH_CLAMP)
+               Fac = clamp(Fac1, 0.0f, 1.0f);
        else
                Fac = 0.0f;
        
index cbff0c099ea37de296c15210a4b6f25917e26943..df06b883cae4cb98d7cad1bb5682f3f8bc5047b0 100644 (file)
@@ -183,7 +183,8 @@ typedef enum NodeMath {
        NODE_MATH_MAXIMUM,
        NODE_MATH_ROUND,
        NODE_MATH_LESS_THAN,
-       NODE_MATH_GREATER_THAN
+       NODE_MATH_GREATER_THAN,
+       NODE_MATH_CLAMP /* used for the clamp UI option */
 } NodeMath;
 
 typedef enum NodeVectorMath {
index 250570e1d65ce2c4c4b3d5f3466f0b4051c6fb12..07310cb4461d8b592fa6fac8d1c715f960bb71ce 100644 (file)
@@ -2391,6 +2391,8 @@ MathNode::MathNode()
 {
        type = ustring("Add");
 
+       use_clamp = false;
+
        add_input("Value1", SHADER_SOCKET_FLOAT);
        add_input("Value2", SHADER_SOCKET_FLOAT);
        add_output("Value",  SHADER_SOCKET_FLOAT);
@@ -2435,11 +2437,17 @@ void MathNode::compile(SVMCompiler& compiler)
 
        compiler.add_node(NODE_MATH, type_enum[type], value1_in->stack_offset, value2_in->stack_offset);
        compiler.add_node(NODE_MATH, value_out->stack_offset);
+
+       if(use_clamp) {
+               compiler.add_node(NODE_MATH, NODE_MATH_CLAMP, value_out->stack_offset);
+               compiler.add_node(NODE_MATH, value_out->stack_offset);
+       }
 }
 
 void MathNode::compile(OSLCompiler& compiler)
 {
        compiler.parameter("type", type);
+       compiler.parameter("Clamp", use_clamp);
        compiler.add(this, "node_math");
 }
 
index efd814e4ae6c687098f56e37d7a492cc6fa0938a..2e0acc32e517b81518b43bf91206da4fbc65e870 100644 (file)
@@ -385,6 +385,8 @@ class MathNode : public ShaderNode {
 public:
        SHADER_NODE_CLASS(MathNode)
 
+       bool use_clamp;
+
        ustring type;
        static ShaderEnum type_enum;
 };