diff --git a/intern/cycles/app/cycles_xml.cpp b/intern/cycles/app/cycles_xml.cpp index 530a4ad14d8..a0282988179 100644 --- a/intern/cycles/app/cycles_xml.cpp +++ b/intern/cycles/app/cycles_xml.cpp @@ -368,6 +368,9 @@ static void xml_read_shader_graph(const XMLReadState& state, Shader *shader, pug xml_read_enum(&wood->type, WaveTextureNode::type_enum, node, "type"); snode = wood; } + else if(string_iequals(node.name(), "normal")) { + snode = new NormalNode(); + } else if(string_iequals(node.name(), "mapping")) { snode = new MappingNode(); } diff --git a/intern/cycles/blender/blender_shader.cpp b/intern/cycles/blender/blender_shader.cpp index b0dd6988457..4191fef4dcd 100644 --- a/intern/cycles/blender/blender_shader.cpp +++ b/intern/cycles/blender/blender_shader.cpp @@ -132,7 +132,6 @@ static ShaderNode *add_node(BL::BlendData b_data, ShaderGraph *graph, BL::Node * case BL::ShaderNode::type_GEOMETRY: break; case BL::ShaderNode::type_MATERIAL: break; case BL::ShaderNode::type_MATERIAL_EXT: break; - case BL::ShaderNode::type_NORMAL: break; case BL::ShaderNode::type_OUTPUT: break; case BL::ShaderNode::type_SCRIPT: break; case BL::ShaderNode::type_SQUEEZE: break; @@ -198,6 +197,17 @@ static ShaderNode *add_node(BL::BlendData b_data, ShaderGraph *graph, BL::Node * node = vmath; break; } + case BL::ShaderNode::type_NORMAL: { + BL::Node::outputs_iterator out_it; + b_node.outputs.begin(out_it); + BL::NodeSocketVectorNone vec_sock(*out_it); + + NormalNode *norm = new NormalNode(); + norm->direction = get_float3(vec_sock.default_value()); + + node = norm; + break; + } case BL::ShaderNode::type_MAPPING: { BL::ShaderNodeMapping b_mapping_node(b_node); MappingNode *mapping = new MappingNode(); diff --git a/intern/cycles/kernel/CMakeLists.txt b/intern/cycles/kernel/CMakeLists.txt index a0803f37cb9..65338eeea6e 100644 --- a/intern/cycles/kernel/CMakeLists.txt +++ b/intern/cycles/kernel/CMakeLists.txt @@ -72,6 +72,7 @@ set(SRC_SVM_HEADERS svm/svm_musgrave.h svm/svm_noise.h svm/svm_noisetex.h + svm/svm_normal.h svm/svm_sepcomb_rgb.h svm/svm_sky.h svm/svm_tex_coord.h diff --git a/intern/cycles/kernel/osl/nodes/CMakeLists.txt b/intern/cycles/kernel/osl/nodes/CMakeLists.txt index 433fc2155fe..5c1fd8b75d8 100644 --- a/intern/cycles/kernel/osl/nodes/CMakeLists.txt +++ b/intern/cycles/kernel/osl/nodes/CMakeLists.txt @@ -33,6 +33,7 @@ set(SRC_OSL node_mix.osl node_mix_closure.osl node_musgrave_texture.osl + node_normal.osl node_blend_weight_texture.osl node_noise_texture.osl node_output_displacement.osl diff --git a/intern/cycles/kernel/osl/nodes/node_normal.osl b/intern/cycles/kernel/osl/nodes/node_normal.osl new file mode 100644 index 00000000000..038a33c9898 --- /dev/null +++ b/intern/cycles/kernel/osl/nodes/node_normal.osl @@ -0,0 +1,31 @@ +/* + * Copyright 2011, Blender Foundation. + * + * 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. + */ + +#include "stdosl.h" + +shader node_normal( + normal Direction = normal(0.0, 0.0, 0.0), + normal NormalIn = normal(0.0, 0.0, 0.0), + output normal NormalOut = normal(0.0, 0.0, 0.0), + output float Dot = 1.0 +{ + Direction = normalize(Direction); + NormalOut = Direction; + Dot = dot(Direction, NormalIn); +} + diff --git a/intern/cycles/kernel/svm/svm.h b/intern/cycles/kernel/svm/svm.h index 5fde44e2769..2beff7ff7e6 100644 --- a/intern/cycles/kernel/svm/svm.h +++ b/intern/cycles/kernel/svm/svm.h @@ -134,6 +134,7 @@ CCL_NAMESPACE_END #include "svm_light_path.h" #include "svm_magic.h" #include "svm_mapping.h" +#include "svm_normal.h" #include "svm_wave.h" #include "svm_math.h" #include "svm_mix.h" @@ -300,6 +301,9 @@ __device_noinline void svm_eval_nodes(KernelGlobals *kg, ShaderData *sd, ShaderT case NODE_VECTOR_MATH: svm_node_vector_math(kg, sd, stack, node.y, node.z, node.w, &offset); break; + case NODE_NORMAL: + svm_node_normal(kg, sd, stack, node.y, node.z, node.w, &offset); + break; case NODE_MAPPING: svm_node_mapping(kg, sd, stack, node.y, node.z, &offset); break; diff --git a/intern/cycles/kernel/svm/svm_normal.h b/intern/cycles/kernel/svm/svm_normal.h new file mode 100644 index 00000000000..0b3f63b6d7e --- /dev/null +++ b/intern/cycles/kernel/svm/svm_normal.h @@ -0,0 +1,41 @@ +/* + * Copyright 2011, Blender Foundation. + * + * 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. + */ + +CCL_NAMESPACE_BEGIN + +__device void svm_node_normal(KernelGlobals *kg, ShaderData *sd, float *stack, uint in_normal_offset, uint out_normal_offset, uint out_dot_offset, int *offset) +{ + /* read extra data */ + uint4 node1 = read_node(kg, offset); + float3 normal = stack_load_float3(stack, in_normal_offset); + + float3 direction; + direction.x = node1.x; + direction.y = node1.y; + direction.z = node1.z; + direction = normalize(direction); + + if (stack_valid(out_normal_offset)) + stack_store_float3(stack, out_normal_offset, direction); + + if (stack_valid(out_dot_offset)) + stack_store_float(stack, out_dot_offset, dot(direction, normalize(normal))); +} + +CCL_NAMESPACE_END + diff --git a/intern/cycles/kernel/svm/svm_types.h b/intern/cycles/kernel/svm/svm_types.h index efec5c7650d..e5e3175761b 100644 --- a/intern/cycles/kernel/svm/svm_types.h +++ b/intern/cycles/kernel/svm/svm_types.h @@ -84,7 +84,8 @@ typedef enum NodeType { NODE_COMBINE_RGB = 5100, NODE_HSV = 5200, NODE_CAMERA = 5300, - NODE_INVERT = 5400 + NODE_INVERT = 5400, + NODE_NORMAL = 5500 } NodeType; typedef enum NodeAttributeType { diff --git a/intern/cycles/render/nodes.cpp b/intern/cycles/render/nodes.cpp index 45f0ab41775..aec6f1f3e26 100644 --- a/intern/cycles/render/nodes.cpp +++ b/intern/cycles/render/nodes.cpp @@ -713,6 +713,41 @@ void MagicTextureNode::compile(OSLCompiler& compiler) compiler.add(this, "node_magic_texture"); } +/* Normal */ + +NormalNode::NormalNode() +: ShaderNode("normal") +{ + direction = make_float3(0.0f, 0.0f, 1.0f); + + add_input("Normal", SHADER_SOCKET_NORMAL); + add_output("Normal", SHADER_SOCKET_NORMAL); + add_output("Dot", SHADER_SOCKET_FLOAT); +} + +void NormalNode::compile(SVMCompiler& compiler) +{ + ShaderInput *normal_in = input("Normal"); + ShaderOutput *normal_out = output("Normal"); + ShaderOutput *dot_out = output("Dot"); + + compiler.stack_assign(normal_in); + compiler.stack_assign(normal_out); + compiler.stack_assign(dot_out); + + compiler.add_node(NODE_NORMAL, normal_in->stack_offset, normal_out->stack_offset, dot_out->stack_offset); + compiler.add_node( + __float_as_int(direction.x), + __float_as_int(direction.y), + __float_as_int(direction.z)); +} + +void NormalNode::compile(OSLCompiler& compiler) +{ + compiler.parameter_vector("Direction", direction); + compiler.add(this, "node_normal"); +} + /* Mapping */ MappingNode::MappingNode() diff --git a/intern/cycles/render/nodes.h b/intern/cycles/render/nodes.h index e08651cf1eb..9d947de1af5 100644 --- a/intern/cycles/render/nodes.h +++ b/intern/cycles/render/nodes.h @@ -343,6 +343,13 @@ public: static ShaderEnum type_enum; }; +class NormalNode : public ShaderNode { +public: + SHADER_NODE_CLASS(NormalNode) + + float3 direction; +}; + class VectorMathNode : public ShaderNode { public: SHADER_NODE_CLASS(VectorMathNode) diff --git a/source/blender/nodes/shader/nodes/node_shader_normal.c b/source/blender/nodes/shader/nodes/node_shader_normal.c index f699268e939..8515c99ba81 100644 --- a/source/blender/nodes/shader/nodes/node_shader_normal.c +++ b/source/blender/nodes/shader/nodes/node_shader_normal.c @@ -84,7 +84,7 @@ void register_node_type_sh_normal(bNodeTreeType *ttype) static bNodeType ntype; node_type_base(ttype, &ntype, SH_NODE_NORMAL, "Normal", NODE_CLASS_OP_VECTOR, NODE_OPTIONS); - node_type_compatibility(&ntype, NODE_OLD_SHADING); + node_type_compatibility(&ntype, NODE_OLD_SHADING|NODE_NEW_SHADING); node_type_socket_templates(&ntype, sh_node_normal_in, sh_node_normal_out); node_type_init(&ntype, node_shader_init_normal); node_type_exec(&ntype, node_shader_exec_normal);