Cycles: bump node changes to add a Distance input that controls the overall displacement
authorBrecht Van Lommel <brechtvanlommel@pandora.be>
Fri, 10 May 2013 16:57:17 +0000 (16:57 +0000)
committerBrecht Van Lommel <brechtvanlommel@pandora.be>
Fri, 10 May 2013 16:57:17 +0000 (16:57 +0000)
distance, and an Invert option to invert the bump effect.

intern/cycles/blender/blender_shader.cpp
intern/cycles/kernel/shaders/node_bump.osl
intern/cycles/kernel/svm/svm_displace.h
intern/cycles/render/nodes.cpp
intern/cycles/render/nodes.h
source/blender/editors/space_node/drawnode.c
source/blender/gpu/shaders/gpu_shader_material.glsl
source/blender/makesrna/intern/rna_nodetree.c
source/blender/nodes/NOD_static_types.h
source/blender/nodes/shader/nodes/node_shader_bump.c

index 7deb826a1e1dd3c4cb3dbee378d70e3447d99295..94415514e7b4055a59cdd94d201f1ae79f8747e5 100644 (file)
@@ -394,7 +394,10 @@ static ShaderNode *add_node(Scene *scene, BL::BlendData b_data, BL::Scene b_scen
                node = new HairInfoNode();
        }
        else if (b_node.is_a(&RNA_ShaderNodeBump)) {
-               node = new BumpNode();
+               BL::ShaderNodeBump b_bump_node(b_node);
+               BumpNode *bump = new BumpNode();
+               bump->invert = b_bump_node.invert();
+               node = bump;
        }
        else if (b_node.is_a(&RNA_ShaderNodeScript)) {
 #ifdef WITH_OSL
index a7dd805149f113f1897295d9217a5a9c331064a2..6d0ec51a9da3ce64bb7513b9b4c509096a1f93ad 100644 (file)
  * Morten S. Mikkelsen, 2010 */
 
 surface node_bump(
+       int invert = 0,
        normal NormalIn = N,
-       float Strength = 0.0,
+       float Strength = 0.1,
+       float Distance = 1.0,
        float SampleCenter = 0.0,
        float SampleX = 0.0,
        float SampleY = 0.0,
@@ -42,10 +44,14 @@ surface node_bump(
 
        float absdet = fabs(det);
 
-       float strength = clamp(Strength, 0.0, 1.0);
+       float strength = max(Strength, 0.0);
+       float dist = Distance;
+
+       if (invert)
+               dist *= -1.0;
        
        /* compute and output perturbed normal */
-       NormalOut = normalize(absdet * NormalIn - sign(det) * surfgrad);
+       NormalOut = normalize(absdet * NormalIn - dist * sign(det) * surfgrad);
        NormalOut = normalize(strength*NormalOut + (1.0 - strength)*NormalIn);
 }
 
index 5d0300c5855b398d6498aaf7d38d627a44907849..ee3c0c6fd2175ba7c88fd4851ca6b29a92855dc5 100644 (file)
@@ -24,7 +24,10 @@ __device void svm_node_set_bump(KernelGlobals *kg, ShaderData *sd, float *stack,
 {
 #ifdef __RAY_DIFFERENTIALS__
        /* get normal input */
-       float3 normal_in = stack_valid(node.y)? stack_load_float3(stack, node.y): sd->N;
+       uint normal_offset, distance_offset, invert;
+       decode_node_uchar4(node.y, &normal_offset, &distance_offset, &invert, NULL);
+
+       float3 normal_in = stack_valid(normal_offset)? stack_load_float3(stack, normal_offset): sd->N;
 
        /* get surface tangents from normal */
        float3 Rx = cross(sd->dP.dy, normal_in);
@@ -45,10 +48,15 @@ __device void svm_node_set_bump(KernelGlobals *kg, ShaderData *sd, float *stack,
        float absdet = fabsf(det);
 
        float strength = stack_load_float(stack, strength_offset);
-       strength = clamp(strength, 0.0f, 1.0f);
+       float distance = stack_load_float(stack, distance_offset);
+
+       if(invert)
+               distance *= -1.0f;
+
+       strength = max(strength, 0.0f);
 
        /* compute and output perturbed normal */
-       float3 normal_out = normalize(absdet*normal_in - signf(det)*surfgrad);
+       float3 normal_out = normalize(absdet*normal_in - distance*signf(det)*surfgrad);
        normal_out = normalize(strength*normal_out + (1.0f - strength)*normal_in);
        stack_store_float3(stack, node.w, normal_out);
 #endif
index 406b5876a4bc025f99463c997876d89004dee687..d7ac379739df259a8b69a3b2df89da0928648e6c 100644 (file)
@@ -3047,6 +3047,8 @@ void VectorMathNode::compile(OSLCompiler& compiler)
 BumpNode::BumpNode()
 : ShaderNode("bump")
 {
+       invert = false;
+
        /* this input is used by the user, but after graph transform it is no longer
         * used and moved to sampler center/x/y instead */
        add_input("Height", SHADER_SOCKET_FLOAT);
@@ -3055,7 +3057,8 @@ BumpNode::BumpNode()
        add_input("SampleX", SHADER_SOCKET_FLOAT);
        add_input("SampleY", SHADER_SOCKET_FLOAT);
        add_input("Normal", SHADER_SOCKET_NORMAL, ShaderInput::NORMAL);
-       add_input("Strength", SHADER_SOCKET_FLOAT, 0.1f);
+       add_input("Strength", SHADER_SOCKET_FLOAT, 1.0f);
+       add_input("Distance", SHADER_SOCKET_FLOAT, 0.1f);
 
        add_output("Normal", SHADER_SOCKET_NORMAL);
 }
@@ -3066,13 +3069,15 @@ void BumpNode::compile(SVMCompiler& compiler)
        ShaderInput *dx_in = input("SampleX");
        ShaderInput *dy_in = input("SampleY");
        ShaderInput *normal_in = input("Normal");
-       ShaderInput *intensity_in = input("Strength");
+       ShaderInput *strength_in = input("Strength");
+       ShaderInput *distance_in = input("Distance");
        ShaderOutput *normal_out = output("Normal");
 
        compiler.stack_assign(center_in);
        compiler.stack_assign(dx_in);
        compiler.stack_assign(dy_in);
-       compiler.stack_assign(intensity_in);
+       compiler.stack_assign(strength_in);
+       compiler.stack_assign(distance_in);
        compiler.stack_assign(normal_out);
 
        if(normal_in->link)
@@ -3080,14 +3085,15 @@ void BumpNode::compile(SVMCompiler& compiler)
        
        /* pack all parameters in the node */
        compiler.add_node(NODE_SET_BUMP,
-               normal_in->stack_offset,
+               compiler.encode_uchar4(normal_in->stack_offset, distance_in->stack_offset, invert),
                compiler.encode_uchar4(center_in->stack_offset, dx_in->stack_offset,
-                       dy_in->stack_offset, intensity_in->stack_offset),
+                       dy_in->stack_offset, strength_in->stack_offset),
                normal_out->stack_offset);
 }
 
 void BumpNode::compile(OSLCompiler& compiler)
 {
+       compiler.parameter("invert", invert);
        compiler.add(this, "node_bump");
 }
 
index 85829e00782ea9af446ce53e797a1ddb318be828..41fa87a3859b4c9618868a00ee43f9d95b2bebf9 100644 (file)
@@ -470,6 +470,7 @@ public:
 class BumpNode : public ShaderNode {
 public:
        SHADER_NODE_CLASS(BumpNode)
+       bool invert;
 };
 
 class RGBCurvesNode : public ShaderNode {
index 084ec53ff7e1a3f0bb0b121a70b1973d6094056b..9ce28cc3314c73e3d7f32282af0642a9633cb062 100644 (file)
@@ -839,6 +839,11 @@ static void node_shader_buts_tex_coord(uiLayout *layout, bContext *UNUSED(C), Po
        uiItemR(layout, ptr, "from_dupli", 0, NULL, 0);
 }
 
+static void node_shader_buts_bump(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
+{
+       uiItemR(layout, ptr, "invert", 0, NULL, 0);
+}
+
 static void node_shader_buts_normal_map(uiLayout *layout, bContext *C, PointerRNA *ptr)
 {
        uiItemR(layout, ptr, "space", 0, "", 0);
@@ -990,6 +995,9 @@ static void node_shader_set_butfunc(bNodeType *ntype)
                case SH_NODE_TEX_COORD:
                        ntype->uifunc = node_shader_buts_tex_coord;
                        break;
+               case SH_NODE_BUMP:
+                       ntype->uifunc = node_shader_buts_bump;
+                       break;
                case SH_NODE_NORMAL_MAP:
                        ntype->uifunc = node_shader_buts_normal_map;
                        break;
index d9f5d99f950c1143a477e205676925b6c6f06b33..fd519a86fc9f7f34251973e3c270374efc9eda21 100644 (file)
@@ -2260,7 +2260,7 @@ void node_object_info(out vec3 location, out float object_index, out float mater
        random = 0.0;
 }
 
-void node_bump(float strength, float height, vec3 N, out vec3 result)
+void node_bump(float strength, float dist, float height, vec3 N, out vec3 result)
 {
        result = N;
 }
index d2d3a4b0488903f9f04c2d32c533c1702f7430e6..d05692f9abc04c9bcceb58f8b016263c75573274 100644 (file)
@@ -3416,6 +3416,16 @@ static void def_glossy(StructRNA *srna)
        RNA_def_property_update(prop, NC_NODE | NA_EDITED, "rna_Node_update");
 }
 
+static void def_sh_bump(StructRNA *srna)
+{
+       PropertyRNA *prop;
+       
+       prop = RNA_def_property(srna, "invert", PROP_BOOLEAN, PROP_NONE);
+       RNA_def_property_boolean_sdna(prop, NULL, "custom1", 1);
+       RNA_def_property_ui_text(prop, "Invert", "Invert the bump mapping direction to push into the surface instead of out");
+       RNA_def_property_update(prop, NC_NODE | NA_EDITED, "rna_Node_update");
+}
+
 static void def_sh_normal_map(StructRNA *srna)
 {
        static EnumPropertyItem prop_space_items[] = {
index 4a45f98f7c93570aaf1a1f69e8e4420dfe8ad8e1..f9868966892eb1c65640814fca298fe5b5e9b11a 100644 (file)
@@ -94,7 +94,7 @@ DefNode( ShaderNode,     SH_NODE_LIGHT_FALLOFF,      0,                      "LI
 DefNode( ShaderNode,     SH_NODE_OBJECT_INFO,        0,                      "OBJECT_INFO",        ObjectInfo,       "Object Info",       ""       )
 DefNode( ShaderNode,     SH_NODE_PARTICLE_INFO,      0,                      "PARTICLE_INFO",      ParticleInfo,     "Particle Info",     ""       )
 DefNode( ShaderNode,     SH_NODE_HAIR_INFO,          0,                      "HAIR_INFO",          HairInfo,         "Hair Info",         ""       )
-DefNode( ShaderNode,     SH_NODE_BUMP,               0,                      "BUMP",               Bump,             "Bump",              ""       )
+DefNode( ShaderNode,     SH_NODE_BUMP,               def_sh_bump,            "BUMP",               Bump,             "Bump",              ""       )
 DefNode( ShaderNode,     SH_NODE_NORMAL_MAP,         def_sh_normal_map,      "NORMAL_MAP",         NormalMap,        "Normal Map",        ""       )
 DefNode( ShaderNode,     SH_NODE_TANGENT,            def_sh_tangent,         "TANGENT",            Tangent,          "Tangent",           ""       )
 DefNode( ShaderNode,     SH_NODE_SCRIPT,             def_sh_script,          "SCRIPT",             Script,           "Script",            ""       )
index b5b093ac7fa8f79d372355141fcfb93006cc9ca5..2b58bd3395bda888827e1b1a5a53247ccdfd3327 100644 (file)
@@ -36,7 +36,8 @@
 
 /* **************** BUMP ******************** */ 
 static bNodeSocketTemplate sh_node_bump_in[] = {
-       { SOCK_FLOAT, 1, N_("Strength"),        0.1f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f},
+       { SOCK_FLOAT, 1, N_("Strength"),        1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f},
+       { SOCK_FLOAT, 1, N_("Distance"),        0.1f, 0.0f, 0.0f, 0.0f, 0.0f, 1000.0f},
        { SOCK_FLOAT, 1, N_("Height"),          1.0f, 1.0f, 1.0f, 1.0f, -1000.0f, 1000.0f, PROP_NONE, SOCK_HIDE_VALUE},
        { SOCK_VECTOR, 1, N_("Normal"), 0.0f, 0.0f, 0.0f, 1.0f, -1.0f, 1.0f, PROP_NONE, SOCK_HIDE_VALUE},
        { -1, 0, "" }