Fix T49187: inconsistent Normal Map node output for backfacing polygons.

There basically are two issues here: in smooth mode (and all non-tangent
normal map types) it doesn't invert the normal for backfacing polys;
on the other hand for flat shaded tangent type it is inverted too soon.

This fix does a brute force correction by checking the backfacing flag.

Reviewers: #cycles, brecht

Reviewed By: #cycles, brecht

Differential Revision: https://developer.blender.org/D2181
This commit is contained in:
Alexander Gavrilov 2016-08-30 12:48:59 +03:00
parent 3a0c0c1b54
commit c376878e54
2 changed files with 26 additions and 2 deletions

@ -26,6 +26,7 @@ shader node_normal_map(
output normal Normal = NormalIn) output normal Normal = NormalIn)
{ {
color mcolor = 2.0 * color(Color[0] - 0.5, Color[1] - 0.5, Color[2] - 0.5); color mcolor = 2.0 * color(Color[0] - 0.5, Color[1] - 0.5, Color[2] - 0.5);
int is_backfacing = backfacing();
if (space == "tangent") { if (space == "tangent") {
vector tangent; vector tangent;
@ -34,9 +35,15 @@ shader node_normal_map(
float is_smooth; float is_smooth;
getattribute("geom:is_smooth", is_smooth); getattribute("geom:is_smooth", is_smooth);
if (!is_smooth) if (!is_smooth) {
ninterp = normalize(transform("world", "object", Ng)); ninterp = normalize(transform("world", "object", Ng));
/* the normal is already inverted, which is too soon for the math here */
if (is_backfacing) {
ninterp = -ninterp;
}
}
// get _unnormalized_ interpolated normal and tangent // get _unnormalized_ interpolated normal and tangent
if (getattribute(attr_name, tangent) && if (getattribute(attr_name, tangent) &&
getattribute(attr_sign_name, tangent_sign) && getattribute(attr_sign_name, tangent_sign) &&
@ -73,7 +80,12 @@ shader node_normal_map(
Normal = normalize(vector(mcolor)); Normal = normalize(vector(mcolor));
} }
/* invert normal for backfacing polygons */
if (is_backfacing) {
Normal = -Normal;
}
if (Strength != 1.0) if (Strength != 1.0)
Normal = normalize(NormalIn + (Normal - NormalIn) * max(Strength, 0.0)); Normal = normalize(NormalIn + (Normal - NormalIn) * max(Strength, 0.0));
} }

@ -277,6 +277,7 @@ ccl_device void svm_node_normal_map(KernelGlobals *kg, ShaderData *sd, float *st
float3 color = stack_load_float3(stack, color_offset); float3 color = stack_load_float3(stack, color_offset);
color = 2.0f*make_float3(color.x - 0.5f, color.y - 0.5f, color.z - 0.5f); color = 2.0f*make_float3(color.x - 0.5f, color.y - 0.5f, color.z - 0.5f);
bool is_backfacing = (ccl_fetch(sd, flag) & SD_BACKFACING) != 0;
float3 N; float3 N;
if(space == NODE_NORMAL_MAP_TANGENT) { if(space == NODE_NORMAL_MAP_TANGENT) {
@ -306,6 +307,12 @@ ccl_device void svm_node_normal_map(KernelGlobals *kg, ShaderData *sd, float *st
} }
else { else {
normal = ccl_fetch(sd, Ng); normal = ccl_fetch(sd, Ng);
/* the normal is already inverted, which is too soon for the math here */
if(is_backfacing) {
normal = -normal;
}
object_inverse_normal_transform(kg, sd, &normal); object_inverse_normal_transform(kg, sd, &normal);
} }
@ -332,6 +339,11 @@ ccl_device void svm_node_normal_map(KernelGlobals *kg, ShaderData *sd, float *st
N = safe_normalize(N); N = safe_normalize(N);
} }
/* invert normal for backfacing polygons */
if(is_backfacing) {
N = -N;
}
float strength = stack_load_float(stack, strength_offset); float strength = stack_load_float(stack, strength_offset);
if(strength != 1.0f) { if(strength != 1.0f) {