Fix T52001: material draw mode principled BSDF artifacts at some angles.
authorBrecht Van Lommel <brechtvanlommel@gmail.com>
Sat, 8 Jul 2017 21:37:16 +0000 (23:37 +0200)
committerBrecht Van Lommel <brechtvanlommel@gmail.com>
Sat, 8 Jul 2017 21:42:20 +0000 (23:42 +0200)
The default anisotropic tangent computation could fail in some cases,
leading to NaNs and artifacts. Use a simpler formulation that doesn't
suffer from this.

source/blender/gpu/shaders/gpu_shader_material.glsl

index 67a099159c5035b1755d0729768aeb16dc128bd3..f14db57a26a092d2c2e56c871ab9775af1a101ff 100644 (file)
@@ -2636,14 +2636,11 @@ void node_bsdf_principled(vec4 base_color, float subsurface, vec3 subsurface_rad
        vec3 Tangent = T;
        if (T == vec3(0.0)) {
                // if no tangent is set, use a default tangent
-               Tangent = vec3(1.0, 0.0, 0.0);
-               if (N.x != 0.0 || N.y != 0.0) {
-                       vec3 N_xz = normalize(vec3(N.x, 0.0, N.z));
-
-                       vec3 axis = normalize(cross(vec3(0.0, 0.0, 1.0), N_xz));
-                       float angle = acos(dot(vec3(0.0, 0.0, 1.0), N_xz));
-
-                       Tangent = normalize(rotate_vector(vec3(1.0, 0.0, 0.0), axis, angle));
+               if(N.x != N.y || N.x != N.z) {
+                       Tangent = vec3(N.z-N.y, N.x-N.z, N.y-N.x);  // (1,1,1) x N
+               }
+               else {
+                       Tangent = vec3(N.z-N.y, N.x+N.z, -N.y-N.x);  // (-1,1,1) x N
                }
        }