Cycles: add Vector Displacement node and extend Displacement node.
authorBrecht Van Lommel <brechtvanlommel@gmail.com>
Sat, 20 Jan 2018 23:40:42 +0000 (00:40 +0100)
committerBrecht Van Lommel <brechtvanlommel@gmail.com>
Sat, 3 Feb 2018 11:20:26 +0000 (12:20 +0100)
This adds midlevel and object/world space for displacement, and a
vector displacement node with tangent/object/world space, midlevel
and scale.

Note that tangent space vector displacement still is not exactly
compatible with maps created by other software, this will require
changes to the tangent computation.

Differential Revision: https://developer.blender.org/D1734

24 files changed:
intern/cycles/blender/addon/version_update.py
intern/cycles/blender/blender_mesh.cpp
intern/cycles/blender/blender_shader.cpp
intern/cycles/kernel/shaders/CMakeLists.txt
intern/cycles/kernel/shaders/node_displacement.osl
intern/cycles/kernel/shaders/node_vector_displacement.osl [new file with mode: 0644]
intern/cycles/kernel/svm/svm.h
intern/cycles/kernel/svm/svm_displace.h
intern/cycles/kernel/svm/svm_types.h
intern/cycles/render/nodes.cpp
intern/cycles/render/nodes.h
release/scripts/startup/nodeitems_builtins.py
source/blender/blenkernel/BKE_node.h
source/blender/blenkernel/intern/node.c
source/blender/editors/space_node/drawnode.c
source/blender/gpu/shaders/gpu_shader_material.glsl
source/blender/makesdna/DNA_node_types.h
source/blender/makesrna/intern/rna_nodetree.c
source/blender/nodes/CMakeLists.txt
source/blender/nodes/NOD_shader.h
source/blender/nodes/NOD_static_types.h
source/blender/nodes/shader/nodes/node_shader_displacement.c
source/blender/nodes/shader/nodes/node_shader_normal_map.c
source/blender/nodes/shader/nodes/node_shader_vector_displacement.c [new file with mode: 0644]

index 73d84e24794341b7007de9047136191a9b29fe58..400d6dac4540ee78697c9aee9c1e613e95bc77b5 100644 (file)
@@ -119,6 +119,7 @@ def displacement_node_insert(material, nodetree, traversed):
         node.location[0] = 0.5 * (from_node.location[0] + to_node.location[0]);
         node.location[1] = 0.5 * (from_node.location[1] + to_node.location[1]);
         node.inputs['Scale'].default_value = 0.1
+        node.inputs['Midlevel'].default_value = 0.0
 
         nodetree.links.new(from_socket, node.inputs['Height'])
         nodetree.links.new(node.outputs['Displacement'], to_socket)
@@ -129,6 +130,11 @@ def displacement_nodes_insert():
         if check_is_new_shading_material(material):
             displacement_node_insert(material, material.node_tree, traversed)
 
+def displacement_node_space(node):
+    if node.bl_idname == 'ShaderNodeDisplacement':
+        if node.space != 'WORLD':
+            node.space = 'OBJECT'
+
 
 def mapping_node_order_flip(node):
     """
@@ -365,3 +371,5 @@ def do_versions(self):
             cmat = mat.cycles
             if not cmat.is_property_set("displacement_method"):
                 cmat.displacement_method = 'BUMP'
+
+        foreach_cycles_node(displacement_node_space)
index cda9fb59e498ae9746537f628f718513810d512d..6f1c49774f2b1e5a08107ca3ea09dfc90afadd2c 100644 (file)
@@ -97,8 +97,11 @@ struct MikkUserData {
 
                if(layer_name == NULL) {
                        Attribute *attr_orco = attributes.find(ATTR_STD_GENERATED);
-                       orco = attr_orco->data_float3();
-                       mesh_texture_space(*(BL::Mesh*)&b_mesh, orco_loc, orco_size);
+
+                       if(attr_orco) {
+                               orco = attr_orco->data_float3();
+                               mesh_texture_space(*(BL::Mesh*)&b_mesh, orco_loc, orco_size);
+                       }
                }
                else {
                        Attribute *attr_uv = attributes.find(ustring(layer_name));
index 45633b2920947cb8c8d56c0e64f13fbf9671b618..e3d7e75556661ef02534e3ea9b7c1a699cc0e0ad 100644 (file)
@@ -873,7 +873,17 @@ static ShaderNode *add_node(Scene *scene,
                node = bevel;
        }
        else if(b_node.is_a(&RNA_ShaderNodeDisplacement)) {
-               node = new DisplacementNode();
+               BL::ShaderNodeDisplacement b_disp_node(b_node);
+               DisplacementNode *disp = new DisplacementNode();
+               disp->space = (NodeNormalMapSpace)b_disp_node.space();
+               node = disp;
+       }
+       else if(b_node.is_a(&RNA_ShaderNodeVectorDisplacement)) {
+               BL::ShaderNodeVectorDisplacement b_disp_node(b_node);
+               VectorDisplacementNode *disp = new VectorDisplacementNode();
+               disp->space = (NodeNormalMapSpace)b_disp_node.space();
+               disp->attribute = "";
+               node = disp;
        }
 
        if(node) {
index fb983a445795bec851e0d825704ce5af4c21d5ce..5e8e98773d96d0cce603e1395f143103f339706a 100644 (file)
@@ -24,6 +24,7 @@ set(SRC_OSL
        node_convert_from_vector.osl
        node_diffuse_bsdf.osl
        node_displacement.osl
+       node_vector_displacement.osl
        node_emission.osl
        node_environment_texture.osl
        node_fresnel.osl
index fb81533c778292404ac9d1ad9bda0c85467f212d..89f35841527357fa35b51a181abecc25f7aac1fe 100644 (file)
 #include "stdosl.h"
 
 shader node_displacement(
+       string space = "object",
        float Height = 0.0,
+       float Midlevel = 0.5,
        float Scale = 1.0,
        normal Normal = N,
        output vector Displacement = vector(0.0, 0.0, 0.0))
 {
-       Displacement = normalize(transform("object", Normal));
-       Displacement *= Height * Scale;
-       Displacement = transform("object", "world", Displacement);
+       Displacement = Normal;
+       if(space == "object") {
+               Displacement = transform("object", Displacement);
+       }
+
+       Displacement = normalize(Displacement) * (Height - Midlevel) * Scale;
+
+       if(space == "object") {
+               Displacement = transform("object", "world", Displacement);
+       }
 }
 
diff --git a/intern/cycles/kernel/shaders/node_vector_displacement.osl b/intern/cycles/kernel/shaders/node_vector_displacement.osl
new file mode 100644 (file)
index 0000000..b19bc22
--- /dev/null
@@ -0,0 +1,60 @@
+/*
+ * Copyright 2011-2013 Blender Foundation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "stdosl.h"
+
+shader node_vector_displacement(
+       color Vector = color(0.0, 0.0, 0.0),
+       float Midlevel = 0.0,
+       float Scale = 1.0,
+       string space = "tangent",
+       string attr_name = "geom:tangent",
+       string attr_sign_name = "geom:tangent_sign",
+       output vector Displacement = vector(0.0, 0.0, 0.0))
+{
+       vector offset = (Vector - vector(Midlevel)) * Scale;
+
+       if(space == "tangent") {
+               /* Tangent space. */
+               vector N_object = normalize(transform("world", "object", N));
+
+               vector T_object;
+               if(getattribute(attr_name, T_object)) {
+                       T_object = normalize(T_object);
+               }
+               else {
+                       T_object = normalize(dPdu);
+               }
+
+               vector B_object = normalize(cross(N_object, T_object));
+               float tangent_sign;
+               if(getattribute(attr_sign_name, tangent_sign)) {
+                       B_object *= tangent_sign;
+               }
+
+               Displacement = T_object*offset[0] + N_object*offset[1] + B_object*offset[2];
+       }
+       else {
+               /* Object or world space. */
+               Displacement = offset;
+       }
+
+       if(space != "world") {
+               /* Tangent or object space. */
+               Displacement = transform("object", "world", Displacement);
+       }
+}
+
index cbc603d4645539df035a88cc9dc71f9be30e39cf..a8f99d23b7db6dbdd6e43a7b5c6785b4f26df703 100644 (file)
@@ -270,6 +270,9 @@ ccl_device_noinline void svm_eval_nodes(KernelGlobals *kg, ShaderData *sd, ccl_a
                        case NODE_DISPLACEMENT:
                                svm_node_displacement(kg, sd, stack, node);
                                break;
+                       case NODE_VECTOR_DISPLACEMENT:
+                               svm_node_vector_displacement(kg, sd, stack, node, &offset);
+                               break;
 #  endif  /* NODES_FEATURE(NODE_FEATURE_BUMP) */
 #  ifdef __TEXTURES__
                        case NODE_TEX_IMAGE:
index 3066a3646844ffd84be96135822295468cda6683..b9fd5bbf84d46fabe1eaef6cfb7e3810d22ecfb2 100644 (file)
@@ -89,17 +89,72 @@ ccl_device void svm_node_set_displacement(KernelGlobals *kg, ShaderData *sd, flo
 
 ccl_device void svm_node_displacement(KernelGlobals *kg, ShaderData *sd, float *stack, uint4 node)
 {
-       uint height_offset, scale_offset, normal_offset, displacement_offset;
-       decode_node_uchar4(node.y, &height_offset, &scale_offset, &normal_offset, &displacement_offset);
+       uint height_offset, midlevel_offset, scale_offset, normal_offset;
+       decode_node_uchar4(node.y, &height_offset, &midlevel_offset, &scale_offset, &normal_offset);
 
        float height = stack_load_float(stack, height_offset);
+       float midlevel = stack_load_float(stack, midlevel_offset);
        float scale = stack_load_float(stack, scale_offset);
        float3 normal = stack_valid(normal_offset)? stack_load_float3(stack, normal_offset): sd->N;
+       uint space = node.w;
 
        float3 dP = normal;
-       object_inverse_normal_transform(kg, sd, &dP);
-       dP *= height * scale;
-       object_dir_transform(kg, sd, &dP);
+
+       if(space == NODE_NORMAL_MAP_OBJECT) {
+               /* Object space. */
+               object_inverse_normal_transform(kg, sd, &dP);
+               dP *= (height - midlevel) * scale;
+               object_dir_transform(kg, sd, &dP);
+       }
+       else {
+               /* World space. */
+               dP *= (height - midlevel) * scale;
+       }
+
+       stack_store_float3(stack, node.z, dP);
+}
+
+ccl_device void svm_node_vector_displacement(KernelGlobals *kg, ShaderData *sd, float *stack, uint4 node, int *offset)
+{
+       uint4 data_node = read_node(kg, offset);
+       uint space = data_node.x;
+
+       uint vector_offset, midlevel_offset,scale_offset, displacement_offset;
+       decode_node_uchar4(node.y, &vector_offset, &midlevel_offset, &scale_offset, &displacement_offset);
+
+       float3 vector = stack_load_float3(stack, vector_offset);
+       float midlevel = stack_load_float(stack, midlevel_offset);
+       float scale = stack_load_float(stack, scale_offset);
+       float3 dP = (vector - make_float3(midlevel, midlevel, midlevel)) * scale;
+
+       if(space == NODE_NORMAL_MAP_TANGENT) {
+               /* Tangent space. */
+               float3 normal = sd->N;
+               object_inverse_normal_transform(kg, sd, &normal);
+
+               const AttributeDescriptor attr = find_attribute(kg, sd, node.z);
+               float3 tangent;
+               if(attr.offset != ATTR_STD_NOT_FOUND) {
+                       tangent = primitive_attribute_float3(kg, sd, attr, NULL, NULL);
+               }
+               else {
+                       tangent = normalize(sd->dPdu);
+               }
+
+               float3 bitangent = normalize(cross(normal, tangent));;
+               const AttributeDescriptor attr_sign = find_attribute(kg, sd, node.w);
+               if(attr_sign.offset != ATTR_STD_NOT_FOUND) {
+                       float sign = primitive_attribute_float(kg, sd, attr_sign, NULL, NULL);
+                       bitangent *= sign;
+               }
+
+               dP = tangent*dP.x + normal*dP.y + bitangent*dP.z;
+       }
+
+       if(space != NODE_NORMAL_MAP_WORLD) {
+               /* Tangent or object space. */
+               object_dir_transform(kg, sd, &dP);
+       }
 
        stack_store_float3(stack, displacement_offset, dP);
 }
index 00c7752da8c24523f0a101bb2b3e6ff0ae8a2ab7..6ff04a65462dba448a5aeae5d3af95d8bccbeebc 100644 (file)
@@ -134,6 +134,7 @@ typedef enum ShaderNodeType {
        NODE_LEAVE_BUMP_EVAL,
        NODE_BEVEL,
        NODE_DISPLACEMENT,
+       NODE_VECTOR_DISPLACEMENT,
 } ShaderNodeType;
 
 typedef enum NodeAttributeType {
index 14c0dbab9f3f0f1f354da40ad9af806206a1ac5a..acfe07bf112eb7799f8240065dae55599abc955e 100644 (file)
@@ -5650,7 +5650,14 @@ NODE_DEFINE(DisplacementNode)
 {
        NodeType* type = NodeType::add("displacement", create, NodeType::SHADER);
 
+       static NodeEnum space_enum;
+       space_enum.insert("object", NODE_NORMAL_MAP_OBJECT);
+       space_enum.insert("world", NODE_NORMAL_MAP_WORLD);
+
+       SOCKET_ENUM(space, "Space", space_enum, NODE_NORMAL_MAP_TANGENT);
+
        SOCKET_IN_FLOAT(height, "Height", 0.0f);
+       SOCKET_IN_FLOAT(midlevel, "Midlevel", 0.5f);
        SOCKET_IN_FLOAT(scale, "Scale", 1.0f);
        SOCKET_IN_NORMAL(normal, "Normal", make_float3(0.0f, 0.0f, 0.0f), SocketType::LINK_NORMAL);
 
@@ -5667,20 +5674,116 @@ DisplacementNode::DisplacementNode()
 void DisplacementNode::compile(SVMCompiler& compiler)
 {
        ShaderInput *height_in = input("Height");
+       ShaderInput *midlevel_in = input("Midlevel");
        ShaderInput *scale_in = input("Scale");
        ShaderInput *normal_in = input("Normal");
        ShaderOutput *displacement_out = output("Displacement");
 
        compiler.add_node(NODE_DISPLACEMENT,
                compiler.encode_uchar4(compiler.stack_assign(height_in),
+                                      compiler.stack_assign(midlevel_in),
                                       compiler.stack_assign(scale_in),
-                                      compiler.stack_assign_if_linked(normal_in),
-                                      compiler.stack_assign(displacement_out)));
+                                      compiler.stack_assign_if_linked(normal_in)),
+           compiler.stack_assign(displacement_out),
+               space);
 }
 
 void DisplacementNode::compile(OSLCompiler& compiler)
 {
+       compiler.parameter(this, "space");
        compiler.add(this, "node_displacement");
 }
 
+/* Vector Displacement */
+
+NODE_DEFINE(VectorDisplacementNode)
+{
+       NodeType* type = NodeType::add("vector_displacement", create, NodeType::SHADER);
+
+       static NodeEnum space_enum;
+       space_enum.insert("tangent", NODE_NORMAL_MAP_TANGENT);
+       space_enum.insert("object", NODE_NORMAL_MAP_OBJECT);
+       space_enum.insert("world", NODE_NORMAL_MAP_WORLD);
+
+       SOCKET_ENUM(space, "Space", space_enum, NODE_NORMAL_MAP_TANGENT);
+       SOCKET_STRING(attribute, "Attribute", ustring(""));
+
+       SOCKET_IN_COLOR(vector, "Vector", make_float3(0.0f, 0.0f, 0.0f));
+       SOCKET_IN_FLOAT(midlevel, "Midlevel", 0.0f);
+       SOCKET_IN_FLOAT(scale, "Scale", 1.0f);
+
+       SOCKET_OUT_VECTOR(displacement, "Displacement");
+
+       return type;
+}
+
+VectorDisplacementNode::VectorDisplacementNode()
+: ShaderNode(node_type)
+{
+}
+
+void VectorDisplacementNode::attributes(Shader *shader, AttributeRequestSet *attributes)
+{
+       if(shader->has_surface && space == NODE_NORMAL_MAP_TANGENT) {
+               if(attribute == ustring("")) {
+                       attributes->add(ATTR_STD_UV_TANGENT);
+                       attributes->add(ATTR_STD_UV_TANGENT_SIGN);
+               }
+               else {
+                       attributes->add(ustring((string(attribute.c_str()) + ".tangent").c_str()));
+                       attributes->add(ustring((string(attribute.c_str()) + ".tangent_sign").c_str()));
+               }
+
+               attributes->add(ATTR_STD_VERTEX_NORMAL);
+       }
+
+       ShaderNode::attributes(shader, attributes);
+}
+
+void VectorDisplacementNode::compile(SVMCompiler& compiler)
+{
+       ShaderInput *vector_in = input("Vector");
+       ShaderInput *midlevel_in = input("Midlevel");
+       ShaderInput *scale_in = input("Scale");
+       ShaderOutput *displacement_out = output("Displacement");
+       int attr = 0, attr_sign = 0;
+
+       if(space == NODE_NORMAL_MAP_TANGENT) {
+               if(attribute == ustring("")) {
+                       attr = compiler.attribute(ATTR_STD_UV_TANGENT);
+                       attr_sign = compiler.attribute(ATTR_STD_UV_TANGENT_SIGN);
+               }
+               else {
+                       attr = compiler.attribute(ustring((string(attribute.c_str()) + ".tangent").c_str()));
+                       attr_sign = compiler.attribute(ustring((string(attribute.c_str()) + ".tangent_sign").c_str()));
+               }
+       }
+
+       compiler.add_node(NODE_VECTOR_DISPLACEMENT,
+               compiler.encode_uchar4(compiler.stack_assign(vector_in),
+                                      compiler.stack_assign(midlevel_in),
+                                      compiler.stack_assign(scale_in),
+                                      compiler.stack_assign(displacement_out)),
+               attr, attr_sign);
+
+       compiler.add_node(space);
+}
+
+void VectorDisplacementNode::compile(OSLCompiler& compiler)
+{
+       if(space == NODE_NORMAL_MAP_TANGENT) {
+               if(attribute == ustring("")) {
+                       compiler.parameter("attr_name", ustring("geom:tangent"));
+                       compiler.parameter("attr_sign_name", ustring("geom:tangent_sign"));
+               }
+               else {
+                       compiler.parameter("attr_name", ustring((string(attribute.c_str()) + ".tangent").c_str()));
+                       compiler.parameter("attr_sign_name", ustring((string(attribute.c_str()) + ".tangent_sign").c_str()));
+               }
+       }
+
+       compiler.parameter(this, "space");
+       compiler.add(this, "node_vector_displacement");
+}
+
 CCL_NAMESPACE_END
index 578451cbcfa324413d0dd4261cea351b5f4da1c4..a00b48ca5bcf4afe14c2f8bd92cd90894005da63 100644 (file)
@@ -1034,11 +1034,28 @@ public:
                return NODE_FEATURE_BUMP;
        }
 
+       NodeNormalMapSpace space;
        float height;
+       float midlevel;
        float scale;
        float3 normal;
 };
 
+class VectorDisplacementNode : public ShaderNode {
+public:
+       SHADER_NODE_CLASS(VectorDisplacementNode)
+       void attributes(Shader *shader, AttributeRequestSet *attributes);
+       virtual int get_feature() {
+               return NODE_FEATURE_BUMP;
+       }
+
+       NodeNormalMapSpace space;
+       ustring attribute;
+       float3 vector;
+       float midlevel;
+       float scale;
+};
+
 CCL_NAMESPACE_END
 
 #endif /* __NODES_H__ */
index fd458c71b7f5f62df488cf82f1c62cb3d5442d17..c433850cac49f88b6c71e61829925831c4ab355c 100644 (file)
@@ -272,6 +272,7 @@ shader_node_categories = [
         NodeItem("ShaderNodeMapping"),
         NodeItem("ShaderNodeBump"),
         NodeItem("ShaderNodeDisplacement"),
+        NodeItem("ShaderNodeVectorDisplacement"),
         NodeItem("ShaderNodeNormalMap"),
         NodeItem("ShaderNodeNormal"),
         NodeItem("ShaderNodeVectorCurve"),
index 854a3e64acc8f09f9065b5fcf5e158e70cb90834..8a736c9952d21732411f0c422c63367ea2df30eb 100644 (file)
@@ -791,6 +791,7 @@ struct ShadeResult;
 #define SH_NODE_BSDF_PRINCIPLED         193
 #define SH_NODE_BEVEL                   197
 #define SH_NODE_DISPLACEMENT            198
+#define SH_NODE_VECTOR_DISPLACEMENT     199
 
 /* custom defines options for Material node */
 #define SH_NODE_MAT_DIFF   1
index 932b8b0c30ade9f9e308d58e4525bed82b0c354a..74766bec9a8ed48acf444b241eca37c0ae3d38f4 100644 (file)
@@ -3590,6 +3590,7 @@ static void registerShaderNodes(void)
        register_node_type_sh_attribute();
        register_node_type_sh_bevel();
        register_node_type_sh_displacement();
+       register_node_type_sh_vector_displacement();
        register_node_type_sh_geometry();
        register_node_type_sh_light_path();
        register_node_type_sh_light_falloff();
index bcf06f117aa1820d6df4a86243cf561c597ee751..eccf3c057d457c1c94bb92e171deec13ca40536b 100644 (file)
@@ -1036,7 +1036,7 @@ static void node_shader_buts_normal_map(uiLayout *layout, bContext *C, PointerRN
 {
        uiItemR(layout, ptr, "space", 0, "", 0);
 
-       if (RNA_enum_get(ptr, "space") == SHD_NORMAL_MAP_TANGENT) {
+       if (RNA_enum_get(ptr, "space") == SHD_SPACE_TANGENT) {
                PointerRNA obptr = CTX_data_pointer_get(C, "active_object");
 
                if (obptr.data && RNA_enum_get(&obptr, "type") == OB_MESH) {
@@ -1048,6 +1048,11 @@ static void node_shader_buts_normal_map(uiLayout *layout, bContext *C, PointerRN
        }
 }
 
+static void node_shader_buts_displacement(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
+{
+       uiItemR(layout, ptr, "space", 0, "", 0);
+}
+
 static void node_shader_buts_tangent(uiLayout *layout, bContext *C, PointerRNA *ptr)
 {
        uiLayout *split, *row;
@@ -1239,6 +1244,10 @@ static void node_shader_set_butfunc(bNodeType *ntype)
                case SH_NODE_NORMAL_MAP:
                        ntype->draw_buttons = node_shader_buts_normal_map;
                        break;
+               case SH_NODE_DISPLACEMENT:
+               case SH_NODE_VECTOR_DISPLACEMENT:
+                       ntype->draw_buttons = node_shader_buts_displacement;
+                       break;
                case SH_NODE_TANGENT:
                        ntype->draw_buttons = node_shader_buts_tangent;
                        break;
index a77e7eb793726d3e5c664fa54b9b32d598d7dc75..d589c8765e68dc31917a10e3aedc8f416b59f341 100644 (file)
@@ -3827,9 +3827,38 @@ void node_bevel(float radius, vec3 N, out vec3 result)
        result = N;
 }
 
-void node_displacement(float height, float dist, vec3 N, out vec3 result)
+void node_displacement_object(float height, float midlevel, float scale, vec3 N, mat4 obmat, out vec3 result)
 {
-       result = height * dist * N;
+       N = (vec4(N, 0.0) * obmat).xyz;
+       result = (height - midlevel) * scale * normalize(N);
+       result = (obmat * vec4(result, 0.0)).xyz;
+}
+
+void node_displacement_world(float height, float midlevel, float scale, vec3 N, out vec3 result)
+{
+       result = (height - midlevel) * scale * normalize(N);
+}
+
+void node_vector_displacement_tangent(vec4 vector, float midlevel, float scale, vec4 tangent, vec3 normal, mat4 obmat, mat4 viewmat, out vec3 result)
+{
+       vec3 N_object = normalize(((vec4(normal, 0.0) * viewmat) * obmat).xyz);
+       vec3 T_object = normalize(((vec4(tangent.xyz, 0.0) * viewmat) * obmat).xyz);
+       vec3 B_object = tangent.w * normalize(cross(N_object, T_object));
+
+       vec3 offset = (vector.xyz - vec3(midlevel)) * scale;
+       result = offset.x * T_object + offset.y * N_object + offset.z * B_object;
+       result = (obmat * vec4(result, 0.0)).xyz;
+}
+
+void node_vector_displacement_object(vec4 vector, float midlevel, float scale, mat4 obmat, out vec3 result)
+{
+       result = (vector.xyz - vec3(midlevel)) * scale;
+       result = (obmat * vec4(result, 0.0)).xyz;
+}
+
+void node_vector_displacement_world(vec4 vector, float midlevel, float scale, out vec3 result)
+{
+       result = (vector.xyz - vec3(midlevel)) * scale;
 }
 
 /* output */
index e6bc315b728ce8f703ef425d24e45160d0c7f721..4dbf3a354ce715682a82d2ca694d953bbcfc24e0 100644 (file)
@@ -1029,12 +1029,12 @@ typedef struct NodeSunBeams {
 #define SHD_TANGENT_AXIS_Y                     1
 #define SHD_TANGENT_AXIS_Z                     2
 
-/* normal map space */
-#define SHD_NORMAL_MAP_TANGENT                 0
-#define SHD_NORMAL_MAP_OBJECT                  1
-#define SHD_NORMAL_MAP_WORLD                   2
-#define SHD_NORMAL_MAP_BLENDER_OBJECT  3
-#define SHD_NORMAL_MAP_BLENDER_WORLD   4
+/* normal map, displacement space */
+#define SHD_SPACE_TANGENT                      0
+#define SHD_SPACE_OBJECT                       1
+#define SHD_SPACE_WORLD                                2
+#define SHD_SPACE_BLENDER_OBJECT       3
+#define SHD_SPACE_BLENDER_WORLD                4
 
 /* math node clamp */
 #define SHD_MATH_CLAMP         1
index 33c6cc687635bdb69f3b5d8c4aa738610a4e6552..4225aa38d572ecef78bed22d7bbfca1ebd632c83 100644 (file)
@@ -4328,11 +4328,11 @@ static void def_sh_uvalongstroke(StructRNA *srna)
 static void def_sh_normal_map(StructRNA *srna)
 {
        static const EnumPropertyItem prop_space_items[] = {
-               {SHD_NORMAL_MAP_TANGENT, "TANGENT", 0, "Tangent Space", "Tangent space normal mapping"},
-               {SHD_NORMAL_MAP_OBJECT, "OBJECT", 0, "Object Space", "Object space normal mapping"},
-               {SHD_NORMAL_MAP_WORLD, "WORLD", 0, "World Space", "World space normal mapping"},
-               {SHD_NORMAL_MAP_BLENDER_OBJECT, "BLENDER_OBJECT", 0, "Blender Object Space", "Object space normal mapping, compatible with Blender render baking"},
-               {SHD_NORMAL_MAP_BLENDER_WORLD, "BLENDER_WORLD", 0, "Blender World Space", "World space normal mapping, compatible with Blender render baking"},
+               {SHD_SPACE_TANGENT, "TANGENT", 0, "Tangent Space", "Tangent space normal mapping"},
+               {SHD_SPACE_OBJECT, "OBJECT", 0, "Object Space", "Object space normal mapping"},
+               {SHD_SPACE_WORLD, "WORLD", 0, "World Space", "World space normal mapping"},
+               {SHD_SPACE_BLENDER_OBJECT, "BLENDER_OBJECT", 0, "Blender Object Space", "Object space normal mapping, compatible with Blender render baking"},
+               {SHD_SPACE_BLENDER_WORLD, "BLENDER_WORLD", 0, "Blender World Space", "World space normal mapping, compatible with Blender render baking"},
                {0, NULL, 0, NULL, NULL}
        };
 
@@ -4352,6 +4352,45 @@ static void def_sh_normal_map(StructRNA *srna)
        RNA_def_struct_sdna_from(srna, "bNode", NULL);
 }
 
+static void def_sh_displacement(StructRNA *srna)
+{
+       static const EnumPropertyItem prop_space_items[] = {
+               {SHD_SPACE_OBJECT, "OBJECT", 0, "Object Space", "Displacement is in object space, affected by object scale"},
+               {SHD_SPACE_WORLD, "WORLD", 0, "World Space", "Displacement is in world space, not affected by object scale"},
+               {0, NULL, 0, NULL, NULL}
+       };
+
+       PropertyRNA *prop;
+
+       prop = RNA_def_property(srna, "space", PROP_ENUM, PROP_NONE);
+       RNA_def_property_enum_sdna(prop, NULL, "custom1");
+       RNA_def_property_enum_items(prop, prop_space_items);
+       RNA_def_property_ui_text(prop, "Space", "Space of the input height");
+       RNA_def_property_update(prop, 0, "rna_Node_update");
+
+       RNA_def_struct_sdna_from(srna, "bNode", NULL);
+}
+
+static void def_sh_vector_displacement(StructRNA *srna)
+{
+       static const EnumPropertyItem prop_space_items[] = {
+               {SHD_SPACE_TANGENT, "TANGENT", 0, "Tangent Space", "Tagent space vector displacement mapping"},
+               {SHD_SPACE_OBJECT, "OBJECT", 0, "Object Space", "Object space vector displacement mapping"},
+               {SHD_SPACE_WORLD, "WORLD", 0, "World Space", "World space vector displacement mapping"},
+               {0, NULL, 0, NULL, NULL}
+       };
+
+       PropertyRNA *prop;
+
+       prop = RNA_def_property(srna, "space", PROP_ENUM, PROP_NONE);
+       RNA_def_property_enum_sdna(prop, NULL, "custom1");
+       RNA_def_property_enum_items(prop, prop_space_items);
+       RNA_def_property_ui_text(prop, "Space", "Space of the input height");
+       RNA_def_property_update(prop, 0, "rna_Node_update");
+
+       RNA_def_struct_sdna_from(srna, "bNode", NULL);
+}
+
 static void def_sh_tangent(StructRNA *srna)
 {
        static const EnumPropertyItem prop_direction_type_items[] = {
index f8cbd14a346a5ab85a98c68202b952b6c06e6073..1acd1c4ea466598da95daece8c674edbdf145242 100644 (file)
@@ -194,6 +194,7 @@ set(SRC
        shader/nodes/node_shader_tangent.c
        shader/nodes/node_shader_bevel.c
        shader/nodes/node_shader_displacement.c
+       shader/nodes/node_shader_vector_displacement.c
        shader/nodes/node_shader_tex_brick.c
        shader/nodes/node_shader_tex_checker.c
        shader/nodes/node_shader_tex_coord.c
index 9590b0c79448b812f433d1a2389054ce24da305a..6ed71e0282300607550dc17fbd949511d0af6721 100644 (file)
@@ -80,6 +80,7 @@ void register_node_type_sh_tex_pointdensity(void);
 void register_node_type_sh_attribute(void);
 void register_node_type_sh_bevel(void);
 void register_node_type_sh_displacement(void);
+void register_node_type_sh_vector_displacement(void);
 void register_node_type_sh_geometry(void);
 void register_node_type_sh_light_path(void);
 void register_node_type_sh_light_falloff(void);
index 56a34a92b934b38954e60ec2825a049c805a3a0f..2db23c2122decf5c2ef55bbe0cf03a67af53253a 100644 (file)
@@ -127,7 +127,8 @@ DefNode( ShaderNode,     SH_NODE_UVALONGSTROKE,      def_sh_uvalongstroke,   "UV
 DefNode( ShaderNode,     SH_NODE_SEPXYZ,             0,                      "SEPXYZ",             SeparateXYZ,      "Separate XYZ",      ""       )
 DefNode( ShaderNode,     SH_NODE_COMBXYZ,            0,                      "COMBXYZ",            CombineXYZ,       "Combine XYZ",       ""       )
 DefNode( ShaderNode,     SH_NODE_BEVEL,              def_sh_bevel,           "BEVEL",              Bevel,            "Bevel",             ""       )
-DefNode( ShaderNode,     SH_NODE_DISPLACEMENT,       0,                      "DISPLACEMENT",       Displacement,     "Displacement",      ""       )
+DefNode( ShaderNode,     SH_NODE_DISPLACEMENT,       def_sh_displacement,    "DISPLACEMENT",       Displacement,     "Displacement",      ""       )
+DefNode( ShaderNode,     SH_NODE_VECTOR_DISPLACEMENT,def_sh_vector_displacement,"VECTOR_DISPLACEMENT",VectorDisplacement,"Vector Displacement",""  )
 
 DefNode( CompositorNode, CMP_NODE_VIEWER,         def_cmp_viewer,         "VIEWER",         Viewer,           "Viewer",            ""              )
 DefNode( CompositorNode, CMP_NODE_RGB,            0,                      "RGB",            RGB,              "RGB",               ""              )
index bcd11b758f8f923d24afc4629be25eb7eff4b1ed..e976eaf143acb58b0adbe0df9dc9590217f45e2c 100644 (file)
@@ -31,6 +31,7 @@
 
 static bNodeSocketTemplate sh_node_displacement_in[] = {
        {       SOCK_FLOAT, 0, N_("Height"), 0.00f, 0.0f, 0.0f, 0.0f, 0.0f, 1000.0f},
+       {       SOCK_FLOAT, 0, N_("Midlevel"), 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1000.0f},
        {       SOCK_FLOAT, 0, N_("Scale"), 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1000.0f},
        {       SOCK_VECTOR, 1, N_("Normal"), 0.0f, 0.0f, 0.0f, 1.0f, -1.0f, 1.0f, PROP_NONE, SOCK_HIDE_VALUE},
        {       -1, 0, ""       }
@@ -41,13 +42,30 @@ static bNodeSocketTemplate sh_node_displacement_out[] = {
        {       -1, 0, ""       }
 };
 
-static int gpu_shader_displacement(GPUMaterial *mat, bNode *UNUSED(node), bNodeExecData *UNUSED(execdata), GPUNodeStack *in, GPUNodeStack *out)
+static void node_shader_init_displacement(bNodeTree *UNUSED(ntree), bNode *node)
 {
-       if (!in[2].link) {
-               GPU_link(mat, "direction_transform_m4v3", GPU_builtin(GPU_VIEW_NORMAL), GPU_builtin(GPU_INVERSE_VIEW_MATRIX), &in[2].link);
+       node->custom1 = SHD_SPACE_OBJECT; /* space */
+
+       /* Set default value here for backwards compatibility. */
+       for (bNodeSocket *sock = node->inputs.first; sock; sock = sock->next) {
+               if (STREQ(sock->name, "Midlevel")) {
+                       ((bNodeSocketValueFloat *)sock->default_value)->value = 0.5f;
+               }
        }
+}
 
-       return GPU_stack_link(mat, "node_displacement", in, out);
+static int gpu_shader_displacement(GPUMaterial *mat, bNode *node, bNodeExecData *UNUSED(execdata), GPUNodeStack *in, GPUNodeStack *out)
+{
+       if (!in[3].link) {
+               GPU_link(mat, "direction_transform_m4v3", GPU_builtin(GPU_VIEW_NORMAL), GPU_builtin(GPU_INVERSE_VIEW_MATRIX), &in[3].link);
+       }
+
+       if(node->custom1 == SHD_SPACE_OBJECT) {
+               return GPU_stack_link(mat, "node_displacement_object", in, out, GPU_builtin(GPU_OBJECT_MATRIX));
+       }
+       else {
+               return GPU_stack_link(mat, "node_displacement_world", in, out, GPU_builtin(GPU_OBJECT_MATRIX));
+       }
 }
 
 /* node type definition */
@@ -59,6 +77,7 @@ void register_node_type_sh_displacement(void)
        node_type_compatibility(&ntype, NODE_NEW_SHADING);
        node_type_socket_templates(&ntype, sh_node_displacement_in, sh_node_displacement_out);
        node_type_storage(&ntype, "", NULL, NULL);
+       node_type_init(&ntype, node_shader_init_displacement);
        node_type_gpu(&ntype, gpu_shader_displacement);
 
        nodeRegisterType(&ntype);
index 36d7522e3e612946c3e5fc0fb6e866a781b4f45a..7584b5eba4dd206fc1baedfc68379725dc06d3af 100644 (file)
@@ -68,7 +68,7 @@ static void node_shader_exec_normal_map(
                float *N = shi->nmapnorm;
                int uv_index = 0;
                switch (nm->space) {
-                       case SHD_NORMAL_MAP_TANGENT:
+                       case SHD_SPACE_TANGENT:
                                if (nm->uv_map[0]) {
                                        /* find uv map by name */
                                        for (int i = 0; i < shi->totuv; i++) {
@@ -96,8 +96,8 @@ static void node_shader_exec_normal_map(
                                }
                                break;
 
-                       case SHD_NORMAL_MAP_OBJECT:
-                       case SHD_NORMAL_MAP_BLENDER_OBJECT:
+                       case SHD_SPACE_OBJECT:
+                       case SHD_SPACE_BLENDER_OBJECT:
                                if (shi->use_world_space_shading) {
                                        mul_mat3_m4_v3((float (*)[4])RE_object_instance_get_matrix(shi->obi, RE_OBJECT_INSTANCE_MATRIX_OB), vecIn);
                                        mul_mat3_m4_v3((float (*)[4])RE_render_current_get_matrix(RE_VIEWINV_MATRIX), N);
@@ -107,8 +107,8 @@ static void node_shader_exec_normal_map(
                                interp_v3_v3v3(out[0]->vec, N, vecIn, strength);
                                break;
 
-                       case SHD_NORMAL_MAP_WORLD:
-                       case SHD_NORMAL_MAP_BLENDER_WORLD:
+                       case SHD_SPACE_WORLD:
+                       case SHD_SPACE_BLENDER_WORLD:
                                if (shi->use_world_space_shading)
                                        mul_mat3_m4_v3((float (*)[4])RE_render_current_get_matrix(RE_VIEWINV_MATRIX), N);
                                else
@@ -150,10 +150,10 @@ static int gpu_shader_normal_map(GPUMaterial *mat, bNode *node, bNodeExecData *U
                /* ******* CYCLES or BLENDER INTERNAL with world space shading flag ******* */
 
                const char *color_to_normal_fnc_name = "color_to_normal_new_shading";
-               if (nm->space == SHD_NORMAL_MAP_BLENDER_OBJECT || nm->space == SHD_NORMAL_MAP_BLENDER_WORLD || !GPU_material_use_new_shading_nodes(mat))
+               if (nm->space == SHD_SPACE_BLENDER_OBJECT || nm->space == SHD_SPACE_BLENDER_WORLD || !GPU_material_use_new_shading_nodes(mat))
                        color_to_normal_fnc_name = "color_to_blender_normal_new_shading";
                switch (nm->space) {
-                       case SHD_NORMAL_MAP_TANGENT:
+                       case SHD_SPACE_TANGENT:
                                GPU_link(mat, "color_to_normal_new_shading", realnorm, &realnorm);
                                GPU_link(mat, "node_normal_map", GPU_attribute(CD_TANGENT, nm->uv_map), negnorm, realnorm, &realnorm);
                                GPU_link(mat, "vec_math_mix", strength, realnorm, GPU_builtin(GPU_VIEW_NORMAL), &out[0].link);
@@ -161,14 +161,14 @@ static int gpu_shader_normal_map(GPUMaterial *mat, bNode *node, bNodeExecData *U
                                GPU_link(mat, "direction_transform_m4v3", out[0].link, GPU_builtin(GPU_INVERSE_VIEW_MATRIX), &out[0].link);
                                GPU_link(mat, "vect_normalize", out[0].link, &out[0].link);
                                return true;
-                       case SHD_NORMAL_MAP_OBJECT:
-                       case SHD_NORMAL_MAP_BLENDER_OBJECT:
+                       case SHD_SPACE_OBJECT:
+                       case SHD_SPACE_BLENDER_OBJECT:
                                GPU_link(mat, "direction_transform_m4v3", negnorm, GPU_builtin(GPU_INVERSE_VIEW_MATRIX), &negnorm);
                                GPU_link(mat, color_to_normal_fnc_name, realnorm, &realnorm);
                                GPU_link(mat, "direction_transform_m4v3", realnorm, GPU_builtin(GPU_OBJECT_MATRIX), &realnorm);
                                break;
-                       case SHD_NORMAL_MAP_WORLD:
-                       case SHD_NORMAL_MAP_BLENDER_WORLD:
+                       case SHD_SPACE_WORLD:
+                       case SHD_SPACE_BLENDER_WORLD:
                                GPU_link(mat, "direction_transform_m4v3", negnorm, GPU_builtin(GPU_INVERSE_VIEW_MATRIX), &negnorm);
                                GPU_link(mat, color_to_normal_fnc_name, realnorm, &realnorm);
                                break;
@@ -184,15 +184,15 @@ static int gpu_shader_normal_map(GPUMaterial *mat, bNode *node, bNodeExecData *U
                GPU_link(mat, "vec_math_negate", negnorm, &negnorm);
 
                switch (nm->space) {
-                       case SHD_NORMAL_MAP_TANGENT:
+                       case SHD_SPACE_TANGENT:
                                GPU_link(mat, "node_normal_map", GPU_attribute(CD_TANGENT, nm->uv_map), negnorm, realnorm, &realnorm);
                                break;
-                       case SHD_NORMAL_MAP_OBJECT:
-                       case SHD_NORMAL_MAP_BLENDER_OBJECT:
+                       case SHD_SPACE_OBJECT:
+                       case SHD_SPACE_BLENDER_OBJECT:
                                GPU_link(mat, "direction_transform_m4v3", realnorm, GPU_builtin(GPU_LOC_TO_VIEW_MATRIX),  &realnorm);
                                break;
-                       case SHD_NORMAL_MAP_WORLD:
-                       case SHD_NORMAL_MAP_BLENDER_WORLD:
+                       case SHD_SPACE_WORLD:
+                       case SHD_SPACE_BLENDER_WORLD:
                                GPU_link(mat, "direction_transform_m4v3", realnorm, GPU_builtin(GPU_VIEW_MATRIX),  &realnorm);
                                break;
                }
diff --git a/source/blender/nodes/shader/nodes/node_shader_vector_displacement.c b/source/blender/nodes/shader/nodes/node_shader_vector_displacement.c
new file mode 100644 (file)
index 0000000..c40377c
--- /dev/null
@@ -0,0 +1,82 @@
+/*
+ * ***** BEGIN GPL LICENSE BLOCK *****
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation; either version 2
+ * of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ *
+ * The Original Code is Copyright (C) 2005 Blender Foundation.
+ * All rights reserved.
+ *
+ * The Original Code is: all of this file.
+ *
+ * Contributor(s): none yet.
+ *
+ * ***** END GPL LICENSE BLOCK *****
+ */
+
+#include "../node_shader_util.h"
+
+/* **************** OUTPUT ******************** */
+
+static bNodeSocketTemplate sh_node_vector_displacement_in[] = {
+       {       SOCK_RGBA, 0, N_("Vector"), 0.00f, 0.0f, 0.0f, 0.0f, 0.0f, 1000.0f, PROP_NONE, SOCK_HIDE_VALUE},
+       {       SOCK_FLOAT, 0, N_("Midlevel"), 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1000.0f},
+       {       SOCK_FLOAT, 0, N_("Scale"), 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1000.0f},
+       {       -1, 0, ""       }
+};
+
+static bNodeSocketTemplate sh_node_vector_displacement_out[] = {
+       {       SOCK_VECTOR, 0, N_("Displacement"), 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f},
+       {       -1, 0, ""       }
+};
+
+static void node_shader_init_vector_displacement(bNodeTree *UNUSED(ntree), bNode *node)
+{
+       node->custom1 = SHD_SPACE_TANGENT; /* space */
+}
+
+static int gpu_shader_vector_displacement(GPUMaterial *mat, bNode *node, bNodeExecData *UNUSED(execdata), GPUNodeStack *in, GPUNodeStack *out)
+{
+       if(node->custom1 == SHD_SPACE_TANGENT) {
+               return GPU_stack_link(mat,
+                                     "node_vector_displacement_tangent",
+                                     in,
+                                     out,
+                                     GPU_attribute(CD_TANGENT, ""),
+                                     GPU_builtin(GPU_VIEW_NORMAL),
+                                     GPU_builtin(GPU_OBJECT_MATRIX),
+                                     GPU_builtin(GPU_VIEW_MATRIX));
+       }
+       else if(node->custom1 == SHD_SPACE_OBJECT) {
+               return GPU_stack_link(mat, "node_vector_displacement_object", in, out, GPU_builtin(GPU_OBJECT_MATRIX));
+       }
+       else {
+               return GPU_stack_link(mat, "node_vector_displacement_world", in, out);
+       }
+}
+
+/* node type definition */
+void register_node_type_sh_vector_displacement(void)
+{
+       static bNodeType ntype;
+
+       sh_node_type_base(&ntype, SH_NODE_VECTOR_DISPLACEMENT, "Vector Displacement", NODE_CLASS_OP_VECTOR, 0);
+       node_type_compatibility(&ntype, NODE_NEW_SHADING);
+       node_type_socket_templates(&ntype, sh_node_vector_displacement_in, sh_node_vector_displacement_out);
+       node_type_storage(&ntype, "", NULL, NULL);
+       node_type_init(&ntype, node_shader_init_vector_displacement);
+       node_type_gpu(&ntype, gpu_shader_vector_displacement);
+
+       nodeRegisterType(&ntype);
+}