Normal Node - Cycles

reviewed by Brecht, with help from Lukas.

Note: dot is reversed compared to Blender.
In Blender Normals point outside, while in Cycles they point inside.
If you use your own custom vector with the Normal Node you will see a difference.
If you feed it with object normals it should work just as good.
This commit is contained in:
Dalai Felinto
2011-12-16 18:15:07 +00:00
parent ce8f64d9f9
commit 3c8ab559a5
11 changed files with 137 additions and 3 deletions

View File

@@ -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"); xml_read_enum(&wood->type, WaveTextureNode::type_enum, node, "type");
snode = wood; snode = wood;
} }
else if(string_iequals(node.name(), "normal")) {
snode = new NormalNode();
}
else if(string_iequals(node.name(), "mapping")) { else if(string_iequals(node.name(), "mapping")) {
snode = new MappingNode(); snode = new MappingNode();
} }

View File

@@ -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_GEOMETRY: break;
case BL::ShaderNode::type_MATERIAL: break; case BL::ShaderNode::type_MATERIAL: break;
case BL::ShaderNode::type_MATERIAL_EXT: break; case BL::ShaderNode::type_MATERIAL_EXT: break;
case BL::ShaderNode::type_NORMAL: break;
case BL::ShaderNode::type_OUTPUT: break; case BL::ShaderNode::type_OUTPUT: break;
case BL::ShaderNode::type_SCRIPT: break; case BL::ShaderNode::type_SCRIPT: break;
case BL::ShaderNode::type_SQUEEZE: 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; node = vmath;
break; 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: { case BL::ShaderNode::type_MAPPING: {
BL::ShaderNodeMapping b_mapping_node(b_node); BL::ShaderNodeMapping b_mapping_node(b_node);
MappingNode *mapping = new MappingNode(); MappingNode *mapping = new MappingNode();

View File

@@ -72,6 +72,7 @@ set(SRC_SVM_HEADERS
svm/svm_musgrave.h svm/svm_musgrave.h
svm/svm_noise.h svm/svm_noise.h
svm/svm_noisetex.h svm/svm_noisetex.h
svm/svm_normal.h
svm/svm_sepcomb_rgb.h svm/svm_sepcomb_rgb.h
svm/svm_sky.h svm/svm_sky.h
svm/svm_tex_coord.h svm/svm_tex_coord.h

View File

@@ -33,6 +33,7 @@ set(SRC_OSL
node_mix.osl node_mix.osl
node_mix_closure.osl node_mix_closure.osl
node_musgrave_texture.osl node_musgrave_texture.osl
node_normal.osl
node_blend_weight_texture.osl node_blend_weight_texture.osl
node_noise_texture.osl node_noise_texture.osl
node_output_displacement.osl node_output_displacement.osl

View File

@@ -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);
}

View File

@@ -134,6 +134,7 @@ CCL_NAMESPACE_END
#include "svm_light_path.h" #include "svm_light_path.h"
#include "svm_magic.h" #include "svm_magic.h"
#include "svm_mapping.h" #include "svm_mapping.h"
#include "svm_normal.h"
#include "svm_wave.h" #include "svm_wave.h"
#include "svm_math.h" #include "svm_math.h"
#include "svm_mix.h" #include "svm_mix.h"
@@ -300,6 +301,9 @@ __device_noinline void svm_eval_nodes(KernelGlobals *kg, ShaderData *sd, ShaderT
case NODE_VECTOR_MATH: case NODE_VECTOR_MATH:
svm_node_vector_math(kg, sd, stack, node.y, node.z, node.w, &offset); svm_node_vector_math(kg, sd, stack, node.y, node.z, node.w, &offset);
break; break;
case NODE_NORMAL:
svm_node_normal(kg, sd, stack, node.y, node.z, node.w, &offset);
break;
case NODE_MAPPING: case NODE_MAPPING:
svm_node_mapping(kg, sd, stack, node.y, node.z, &offset); svm_node_mapping(kg, sd, stack, node.y, node.z, &offset);
break; break;

View File

@@ -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

View File

@@ -84,7 +84,8 @@ typedef enum NodeType {
NODE_COMBINE_RGB = 5100, NODE_COMBINE_RGB = 5100,
NODE_HSV = 5200, NODE_HSV = 5200,
NODE_CAMERA = 5300, NODE_CAMERA = 5300,
NODE_INVERT = 5400 NODE_INVERT = 5400,
NODE_NORMAL = 5500
} NodeType; } NodeType;
typedef enum NodeAttributeType { typedef enum NodeAttributeType {

View File

@@ -713,6 +713,41 @@ void MagicTextureNode::compile(OSLCompiler& compiler)
compiler.add(this, "node_magic_texture"); 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 */ /* Mapping */
MappingNode::MappingNode() MappingNode::MappingNode()

View File

@@ -343,6 +343,13 @@ public:
static ShaderEnum type_enum; static ShaderEnum type_enum;
}; };
class NormalNode : public ShaderNode {
public:
SHADER_NODE_CLASS(NormalNode)
float3 direction;
};
class VectorMathNode : public ShaderNode { class VectorMathNode : public ShaderNode {
public: public:
SHADER_NODE_CLASS(VectorMathNode) SHADER_NODE_CLASS(VectorMathNode)

View File

@@ -84,7 +84,7 @@ void register_node_type_sh_normal(bNodeTreeType *ttype)
static bNodeType ntype; static bNodeType ntype;
node_type_base(ttype, &ntype, SH_NODE_NORMAL, "Normal", NODE_CLASS_OP_VECTOR, NODE_OPTIONS); 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_socket_templates(&ntype, sh_node_normal_in, sh_node_normal_out);
node_type_init(&ntype, node_shader_init_normal); node_type_init(&ntype, node_shader_init_normal);
node_type_exec(&ntype, node_shader_exec_normal); node_type_exec(&ntype, node_shader_exec_normal);