Cycles / Fresnel Node:

* Add a "Normal" Input to the Fresnel node.
* Fix for the Fresnel GLSL code (normalize the Incoming vector).

Patch #37384 by Philipp Oeser (lichtwerk) , thanks!
This commit is contained in:
Thomas Dinges 2013-11-09 13:14:00 +00:00
parent 396fb315c5
commit cc7b2a0b04
4 changed files with 18 additions and 6 deletions

@ -18,13 +18,17 @@ CCL_NAMESPACE_BEGIN
/* Fresnel Node */ /* Fresnel Node */
__device void svm_node_fresnel(ShaderData *sd, float *stack, uint ior_offset, uint ior_value, uint out_offset) __device void svm_node_fresnel(ShaderData *sd, float *stack, uint ior_offset, uint ior_value, uint node)
{ {
uint normal_offset, out_offset;
decode_node_uchar4(node, &normal_offset, &out_offset, NULL, NULL);
float eta = (stack_valid(ior_offset))? stack_load_float(stack, ior_offset): __uint_as_float(ior_value); float eta = (stack_valid(ior_offset))? stack_load_float(stack, ior_offset): __uint_as_float(ior_value);
float3 normal_in = stack_valid(normal_offset)? stack_load_float3(stack, normal_offset): sd->N;
eta = fmaxf(eta, 1.0f + 1e-5f); eta = fmaxf(eta, 1.0f + 1e-5f);
eta = (sd->flag & SD_BACKFACING)? 1.0f/eta: eta; eta = (sd->flag & SD_BACKFACING)? 1.0f/eta: eta;
float f = fresnel_dielectric_cos(dot(sd->I, sd->N), eta); float f = fresnel_dielectric_cos(dot(sd->I, normal_in), eta);
stack_store_float(stack, out_offset, f); stack_store_float(stack, out_offset, f);
} }

@ -3166,12 +3166,17 @@ FresnelNode::FresnelNode()
void FresnelNode::compile(SVMCompiler& compiler) void FresnelNode::compile(SVMCompiler& compiler)
{ {
ShaderInput *normal_in = input("Normal");
ShaderInput *ior_in = input("IOR"); ShaderInput *ior_in = input("IOR");
ShaderOutput *fac_out = output("Fac"); ShaderOutput *fac_out = output("Fac");
compiler.stack_assign(ior_in); compiler.stack_assign(ior_in);
compiler.stack_assign(fac_out); compiler.stack_assign(fac_out);
compiler.add_node(NODE_FRESNEL, ior_in->stack_offset, __float_as_int(ior_in->value.x), fac_out->stack_offset);
if(normal_in->link)
compiler.stack_assign(normal_in);
compiler.add_node(NODE_FRESNEL, ior_in->stack_offset, __float_as_int(ior_in->value.x), compiler.encode_uchar4(normal_in->stack_offset, fac_out->stack_offset));
} }
void FresnelNode::compile(OSLCompiler& compiler) void FresnelNode::compile(OSLCompiler& compiler)

@ -2131,7 +2131,7 @@ void node_add_shader(vec4 shader1, vec4 shader2, out vec4 shader)
void node_fresnel(float ior, vec3 N, vec3 I, out float result) void node_fresnel(float ior, vec3 N, vec3 I, out float result)
{ {
float eta = max(ior, 0.00001); float eta = max(ior, 0.00001);
result = fresnel_dielectric(I, N, (gl_FrontFacing)? eta: 1.0/eta); result = fresnel_dielectric(normalize(I), N, (gl_FrontFacing)? eta: 1.0/eta);
} }
/* gamma */ /* gamma */

@ -30,6 +30,7 @@
/* **************** Fresnel ******************** */ /* **************** Fresnel ******************** */
static bNodeSocketTemplate sh_node_fresnel_in[] = { static bNodeSocketTemplate sh_node_fresnel_in[] = {
{ SOCK_FLOAT, 1, N_("IOR"), 1.45f, 0.0f, 0.0f, 0.0f, 1.0f, 1000.0f}, { SOCK_FLOAT, 1, N_("IOR"), 1.45f, 0.0f, 0.0f, 0.0f, 1.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, "" } { -1, 0, "" }
}; };
@ -40,8 +41,10 @@ static bNodeSocketTemplate sh_node_fresnel_out[] = {
static int node_shader_gpu_fresnel(GPUMaterial *mat, bNode *UNUSED(node), bNodeExecData *UNUSED(execdata), GPUNodeStack *in, GPUNodeStack *out) static int node_shader_gpu_fresnel(GPUMaterial *mat, bNode *UNUSED(node), bNodeExecData *UNUSED(execdata), GPUNodeStack *in, GPUNodeStack *out)
{ {
/* todo: is incoming vector normalized? */ if (!in[1].link)
return GPU_stack_link(mat, "node_fresnel", in, out, GPU_builtin(GPU_VIEW_NORMAL), GPU_builtin(GPU_VIEW_POSITION)); in[1].link = GPU_builtin(GPU_VIEW_NORMAL);
return GPU_stack_link(mat, "node_fresnel", in, out, GPU_builtin(GPU_VIEW_POSITION));
} }
/* node type definition */ /* node type definition */