Eevee: Fix Tangent vectors using NormalMatrix and make them world space
authorClément Foucault <foucault.clem@gmail.com>
Wed, 8 May 2019 21:21:09 +0000 (23:21 +0200)
committerClément Foucault <foucault.clem@gmail.com>
Wed, 8 May 2019 21:28:06 +0000 (23:28 +0200)
Making them world space by default remove a lot of legacy conversion from
viewspace.

source/blender/gpu/intern/gpu_codegen.c
source/blender/gpu/shaders/gpu_shader_material.glsl
source/blender/nodes/shader/nodes/node_shader_bsdf_principled.c
source/blender/nodes/shader/nodes/node_shader_normal_map.c
source/blender/nodes/shader/nodes/node_shader_tangent.c
source/blender/nodes/shader/nodes/node_shader_vector_displacement.c

index 310c57f..2c42cc6 100644 (file)
@@ -1118,7 +1118,7 @@ static char *code_generate_vertex(ListBase *nodes, const char *vert_code, bool u
       if (input->source == GPU_SOURCE_ATTR && input->attr_first) {
         if (input->attr_type == CD_TANGENT) { /* silly exception */
           BLI_dynstr_appendf(ds,
-                             "\tvar%d%s.xyz = NormalMatrix * att%d.xyz;\n",
+                             "\tvar%d%s.xyz = transpose(mat3(ModelMatrixInverse)) * att%d.xyz;\n",
                              input->attr_id,
                              use_geom ? "g" : "",
                              input->attr_id);
index f03e30b..59eb463 100644 (file)
@@ -2013,18 +2013,13 @@ void tangent_orco_z(vec3 orco_in, out vec3 orco_out)
   orco_out = orco_in.yxz * vec3(-0.5, 0.5, 0.0) + vec3(0.25, -0.25, 0.0);
 }
 
-void node_tangentmap(vec4 attr_tangent, mat4 toworld, out vec3 tangent)
+void node_tangentmap(vec4 attr_tangent, out vec3 tangent)
 {
-  tangent = normalize((toworld * vec4(attr_tangent.xyz, 0.0)).xyz);
+  tangent = normalize(attr_tangent.xyz);
 }
 
-void node_tangent(vec3 N, vec3 orco, mat4 objmat, mat4 toworld, out vec3 T)
+void node_tangent(vec3 N, vec3 orco, mat4 objmat, out vec3 T)
 {
-#ifndef VOLUMETRICS
-  N = normalize(gl_FrontFacing ? worldNormal : -worldNormal);
-#else
-  N = (toworld * vec4(N, 0.0)).xyz;
-#endif
   T = (objmat * vec4(orco, 0.0)).xyz;
   T = cross(N, normalize(cross(T, N)));
 }
@@ -2068,7 +2063,7 @@ void node_geometry(vec3 I,
   true_normal = normal;
 #  endif
   tangent_orco_z(orco, orco);
-  node_tangent(N, orco, objmat, toworld, tangent);
+  node_tangent(N, orco, objmat, tangent);
 
   parametric = vec3(barycentric, 0.0);
   backfacing = (gl_FrontFacing) ? 0.0 : 1.0;
@@ -3334,6 +3329,7 @@ void node_vector_displacement_tangent(vec4 vector,
                                       mat4 viewmat,
                                       out vec3 result)
 {
+  /* TODO(fclem) this is broken. revisit latter. */
   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));
index 37c35c0..72852d2 100644 (file)
@@ -114,10 +114,9 @@ static int node_shader_gpu_bsdf_principled(GPUMaterial *mat,
     GPU_link(mat, "tangent_orco_z", orco, &in[19].link);
     GPU_link(mat,
              "node_tangent",
-             GPU_builtin(GPU_VIEW_NORMAL),
+             GPU_builtin(GPU_WORLD_NORMAL),
              in[19].link,
              GPU_builtin(GPU_OBJECT_MATRIX),
-             GPU_builtin(GPU_INVERSE_VIEW_MATRIX),
              &in[19].link);
   }
 
index d769a21..4976d03 100644 (file)
@@ -84,52 +84,33 @@ static int gpu_shader_normal_map(GPUMaterial *mat,
     realnorm = GPU_constant(in[1].vec);
   }
 
-  negnorm = GPU_builtin(GPU_VIEW_NORMAL);
+  negnorm = GPU_builtin(GPU_WORLD_NORMAL);
   GPU_link(mat, "math_max", strength, GPU_constant(d), &strength);
 
   const char *color_to_normal_fnc_name = "color_to_normal_new_shading";
   if (nm->space == SHD_SPACE_BLENDER_OBJECT || nm->space == SHD_SPACE_BLENDER_WORLD) {
     color_to_normal_fnc_name = "color_to_blender_normal_new_shading";
   }
+
+  GPU_link(mat, color_to_normal_fnc_name, realnorm, &realnorm);
   switch (nm->space) {
     case SHD_SPACE_TANGENT:
-      GPU_link(mat, "color_to_normal_new_shading", realnorm, &realnorm);
       GPU_link(mat,
                "node_normal_map",
                GPU_builtin(GPU_OBJECT_INFO),
                GPU_attribute(CD_TANGENT, nm->uv_map),
-               negnorm,
+               GPU_builtin(GPU_WORLD_NORMAL),
                realnorm,
                &realnorm);
-      GPU_link(
-          mat, "vec_math_mix", strength, realnorm, GPU_builtin(GPU_VIEW_NORMAL), &out[0].link);
-      /* for uniform scale this is sufficient to match Cycles */
-      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;
+      break;
     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_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);
+      /* Nothing to do. */
       break;
   }
 
index 5512754..6795f48 100644 (file)
@@ -42,13 +42,7 @@ static int node_shader_gpu_tangent(GPUMaterial *mat,
   NodeShaderTangent *attr = node->storage;
 
   if (attr->direction_type == SHD_TANGENT_UVMAP) {
-    return GPU_stack_link(mat,
-                          node,
-                          "node_tangentmap",
-                          in,
-                          out,
-                          GPU_attribute(CD_TANGENT, ""),
-                          GPU_builtin(GPU_INVERSE_VIEW_MATRIX));
+    return GPU_stack_link(mat, node, "node_tangentmap", in, out, GPU_attribute(CD_TANGENT, ""));
   }
   else {
     GPUNodeLink *orco = GPU_attribute(CD_ORCO, "");
@@ -68,10 +62,9 @@ static int node_shader_gpu_tangent(GPUMaterial *mat,
                           "node_tangent",
                           in,
                           out,
-                          GPU_builtin(GPU_VIEW_NORMAL),
+                          GPU_builtin(GPU_WORLD_NORMAL),
                           orco,
-                          GPU_builtin(GPU_OBJECT_MATRIX),
-                          GPU_builtin(GPU_INVERSE_VIEW_MATRIX));
+                          GPU_builtin(GPU_OBJECT_MATRIX));
   }
 }
 
index cbc0120..ac8b49c 100644 (file)
@@ -61,7 +61,7 @@ static int gpu_shader_vector_displacement(GPUMaterial *mat,
                           in,
                           out,
                           GPU_attribute(CD_TANGENT, ""),
-                          GPU_builtin(GPU_VIEW_NORMAL),
+                          GPU_builtin(GPU_WORLD_NORMAL),
                           GPU_builtin(GPU_OBJECT_MATRIX),
                           GPU_builtin(GPU_VIEW_MATRIX));
   }