Fix T62178 Eevee: Texture Box mapping not matching Cycles if object is scaled
authorClément Foucault <foucault.clem@gmail.com>
Thu, 28 Mar 2019 21:08:39 +0000 (22:08 +0100)
committerClément Foucault <foucault.clem@gmail.com>
Thu, 28 Mar 2019 21:08:54 +0000 (22:08 +0100)
The wrong transformation was used. Add a new matrix specially for this case.

This also fix the Node texture coordinate that was suffering the same issue.

source/blender/draw/intern/draw_manager.h
source/blender/draw/intern/draw_manager_data.c
source/blender/draw/intern/draw_manager_exec.c
source/blender/gpu/GPU_material.h
source/blender/gpu/GPU_shader_interface.h
source/blender/gpu/intern/gpu_codegen.c
source/blender/gpu/intern/gpu_shader_interface.c
source/blender/gpu/shaders/gpu_shader_material.glsl
source/blender/nodes/shader/nodes/node_shader_tex_image.c

index 25dbe776858209ea9ad0f6be55bc4a20989ecfde..45721951abfd8642e7174e750272016721e0b284 100644 (file)
@@ -99,10 +99,11 @@ enum {
        DRW_CALL_MODELVIEWINVERSE       = (1 << 2),
        DRW_CALL_MODELVIEWPROJECTION    = (1 << 3),
        DRW_CALL_NORMALVIEW             = (1 << 4),
-       DRW_CALL_NORMALWORLD            = (1 << 5),
-       DRW_CALL_ORCOTEXFAC             = (1 << 6),
-       DRW_CALL_EYEVEC                 = (1 << 7),
-       DRW_CALL_OBJECTINFO             = (1 << 8),
+       DRW_CALL_NORMALVIEWINVERSE      = (1 << 5),
+       DRW_CALL_NORMALWORLD            = (1 << 6),
+       DRW_CALL_ORCOTEXFAC             = (1 << 7),
+       DRW_CALL_EYEVEC                 = (1 << 8),
+       DRW_CALL_OBJECTINFO             = (1 << 9),
 };
 
 typedef struct DRWCallState {
@@ -122,6 +123,7 @@ typedef struct DRWCallState {
        float modelviewinverse[4][4];
        float modelviewprojection[4][4];
        float normalview[3][3];
+       float normalviewinverse[3][3];
        float normalworld[3][3]; /* Not view dependent */
        float orcotexfac[2][3]; /* Not view dependent */
        float objectinfo[2];
@@ -258,6 +260,7 @@ struct DRWShadingGroup {
        int modelviewinverse;
        int modelviewprojection;
        int normalview;
+       int normalviewinverse;
        int normalworld;
        int orcotexfac;
        int eye;
index 7233036effef3fe98c260b1e7c026db8b658e3cd..7f553c0926df93a44a26b2d728ae5c06f7eb5204 100644 (file)
@@ -695,6 +695,7 @@ static void drw_shgroup_init(DRWShadingGroup *shgroup, GPUShader *shader)
        shgroup->modelviewinverse = GPU_shader_get_builtin_uniform(shader, GPU_UNIFORM_MODELVIEW_INV);
        shgroup->modelviewprojection = GPU_shader_get_builtin_uniform(shader, GPU_UNIFORM_MVP);
        shgroup->normalview = GPU_shader_get_builtin_uniform(shader, GPU_UNIFORM_NORMAL);
+       shgroup->normalviewinverse = GPU_shader_get_builtin_uniform(shader, GPU_UNIFORM_NORMAL_INV);
        shgroup->normalworld = GPU_shader_get_builtin_uniform(shader, GPU_UNIFORM_WORLDNORMAL);
        shgroup->orcotexfac = GPU_shader_get_builtin_uniform(shader, GPU_UNIFORM_ORCO);
        shgroup->objectinfo = GPU_shader_get_builtin_uniform(shader, GPU_UNIFORM_OBJECT_INFO);
@@ -717,6 +718,9 @@ static void drw_shgroup_init(DRWShadingGroup *shgroup, GPUShader *shader)
        if (shgroup->normalview > -1) {
                shgroup->matflag |= DRW_CALL_NORMALVIEW;
        }
+       if (shgroup->normalviewinverse > -1) {
+               shgroup->matflag |= DRW_CALL_NORMALVIEWINVERSE;
+       }
        if (shgroup->normalworld > -1) {
                shgroup->matflag |= DRW_CALL_NORMALWORLD;
        }
index 87c56efb4938429b4baa2ac6d4ee494a90aa17a6..7dc42c4d4590fa07db1df65615e80cad36086ab5 100644 (file)
@@ -792,18 +792,20 @@ static void draw_matrices_model_prepare(DRWCallState *st)
        if (st->matflag & DRW_CALL_MODELVIEWPROJECTION) {
                mul_m4_m4m4(st->modelviewprojection, DST.view_data.matstate.mat[DRW_MAT_PERS], st->model);
        }
-       if (st->matflag & (DRW_CALL_NORMALVIEW | DRW_CALL_EYEVEC)) {
+       if (st->matflag & (DRW_CALL_NORMALVIEW | DRW_CALL_NORMALVIEWINVERSE | DRW_CALL_EYEVEC)) {
                copy_m3_m4(st->normalview, st->modelview);
                invert_m3(st->normalview);
                transpose_m3(st->normalview);
        }
+       if (st->matflag & (DRW_CALL_NORMALVIEWINVERSE | DRW_CALL_EYEVEC)) {
+               invert_m3_m3(st->normalviewinverse, st->normalview);
+       }
+       /* TODO remove eye vec (unused) */
        if (st->matflag & DRW_CALL_EYEVEC) {
                /* Used by orthographic wires */
-               float tmp[3][3];
                copy_v3_fl3(st->eyevec, 0.0f, 0.0f, 1.0f);
-               invert_m3_m3(tmp, st->normalview);
                /* set eye vector, transformed to object coords */
-               mul_m3_v3(tmp, st->eyevec);
+               mul_m3_v3(st->normalviewinverse, st->eyevec);
        }
        /* Non view dependent */
        if (st->matflag & DRW_CALL_MODELINVERSE) {
@@ -835,6 +837,7 @@ static void draw_geometry_prepare(DRWShadingGroup *shgroup, DRWCall *call)
                GPU_shader_uniform_vector(shgroup->shader, shgroup->modelviewinverse, 16, 1, (float *)state->modelviewinverse);
                GPU_shader_uniform_vector(shgroup->shader, shgroup->modelviewprojection, 16, 1, (float *)state->modelviewprojection);
                GPU_shader_uniform_vector(shgroup->shader, shgroup->normalview, 9, 1, (float *)state->normalview);
+               GPU_shader_uniform_vector(shgroup->shader, shgroup->normalviewinverse, 9, 1, (float *)state->normalviewinverse);
                GPU_shader_uniform_vector(shgroup->shader, shgroup->normalworld, 9, 1, (float *)state->normalworld);
                GPU_shader_uniform_vector(shgroup->shader, shgroup->objectinfo, 4, 1, (float *)objectinfo);
                GPU_shader_uniform_vector(shgroup->shader, shgroup->orcotexfac, 3, 2, (float *)state->orcotexfac);
index fac8270cd584c1c6b395beebf26ffbcec0a07e42..6d8319fbdd36ddc734c0d6600bc398825c6a7c21 100644 (file)
@@ -107,6 +107,7 @@ typedef enum eGPUBuiltin {
        GPU_VOLUME_TEMPERATURE =    (1 << 18),
        GPU_BARYCENTRIC_TEXCO =     (1 << 19),
        GPU_BARYCENTRIC_DIST =      (1 << 20),
+       GPU_INVERSE_NORMAL_MATRIX = (1 << 21),
 } eGPUBuiltin;
 
 typedef enum eGPUMatFlag {
index 34b607bcb5dc5db818cc8ba9de18511fc2f8ddff..ee2091c7cec1da52d799b723a0dcd8f8be480fd9 100644 (file)
@@ -45,6 +45,7 @@ typedef enum {
        GPU_UNIFORM_VIEWPROJECTION_INV,  /* mat4 ViewProjectionMatrixInverse */
 
        GPU_UNIFORM_NORMAL,      /* mat3 NormalMatrix */
+       GPU_UNIFORM_NORMAL_INV,  /* mat3 NormalMatrixInverse */
        GPU_UNIFORM_WORLDNORMAL, /* mat3 WorldNormalMatrix */
        GPU_UNIFORM_CAMERATEXCO, /* vec4 CameraTexCoFactors */
        GPU_UNIFORM_ORCO,        /* vec3 OrcoTexCoFactors[] */
index b1d94d22f3537fea5ae703ac3c75f5b78ff65cdf..355ca0832740ad7a37b8993ed7b21005dcedbbae 100644 (file)
@@ -467,6 +467,8 @@ const char *GPU_builtin_name(eGPUBuiltin builtin)
                return "unfinvviewmat";
        else if (builtin == GPU_INVERSE_OBJECT_MATRIX)
                return "unfinvobmat";
+       else if (builtin == GPU_INVERSE_NORMAL_MATRIX)
+               return "unfinvnormat";
        else if (builtin == GPU_LOC_TO_VIEW_MATRIX)
                return "unflocaltoviewmat";
        else if (builtin == GPU_INVERSE_LOC_TO_VIEW_MATRIX)
@@ -719,6 +721,8 @@ static void codegen_call_functions(DynStr *ds, ListBase *nodes, GPUOutput *final
                                        BLI_dynstr_append(ds, "objmat");
                                else if (input->builtin == GPU_INVERSE_OBJECT_MATRIX)
                                        BLI_dynstr_append(ds, "objinv");
+                               else if (input->builtin == GPU_INVERSE_NORMAL_MATRIX)
+                                       BLI_dynstr_append(ds, "norinv");
                                else if (input->builtin == GPU_VIEW_POSITION)
                                        BLI_dynstr_append(ds, "viewposition");
                                else if (input->builtin == GPU_VIEW_NORMAL)
@@ -795,6 +799,8 @@ static char *code_generate_fragment(GPUMaterial *material, ListBase *nodes, GPUO
                BLI_dynstr_append(ds, "\t#define objmat ModelMatrix\n");
        if (builtins & GPU_INVERSE_OBJECT_MATRIX)
                BLI_dynstr_append(ds, "\t#define objinv ModelMatrixInverse\n");
+       if (builtins & GPU_INVERSE_NORMAL_MATRIX)
+               BLI_dynstr_append(ds, "\t#define norinv NormalMatrixInverse\n");
        if (builtins & GPU_INVERSE_VIEW_MATRIX)
                BLI_dynstr_append(ds, "\t#define viewinv ViewMatrixInverse\n");
        if (builtins & GPU_LOC_TO_VIEW_MATRIX)
index 72f19b3fc6b9c628e1fbf1d12be93cb0e68617ed..21e2be03eb9d2fcf42dfae52a66f58dcc5bb1864 100644 (file)
@@ -61,6 +61,7 @@ static const char *BuiltinUniform_name(GPUUniformBuiltin u)
                [GPU_UNIFORM_VIEWPROJECTION_INV] = "ViewProjectionMatrixInverse",
 
                [GPU_UNIFORM_NORMAL] = "NormalMatrix",
+               [GPU_UNIFORM_NORMAL_INV] = "NormalMatrixInverse",
                [GPU_UNIFORM_WORLDNORMAL] = "WorldNormalMatrix",
                [GPU_UNIFORM_CAMERATEXCO] = "CameraTexCoFactors",
                [GPU_UNIFORM_ORCO] = "OrcoTexCoFactors",
index e0d381e6f8be65c1f49a6987eb5d7ce48c60237d..5f54b53987e87169d049a5e778aab356b6b5f3cb 100644 (file)
@@ -2,6 +2,7 @@
 uniform mat4 ModelViewMatrix;
 uniform mat4 ModelViewMatrixInverse;
 uniform mat3 NormalMatrix;
+uniform mat3 NormalMatrixInverse;
 
 #ifndef USE_ATTR
 uniform mat4 ModelMatrix;
@@ -187,6 +188,11 @@ void direction_transform_m4v3(vec3 vin, mat4 mat, out vec3 vout)
        vout = (mat * vec4(vin, 0.0)).xyz;
 }
 
+void mat3_mul(vec3 vin, mat3 mat, out vec3 vout)
+{
+       vout = mat * vin;
+}
+
 void point_transform_m4v3(vec3 vin, mat4 mat, out vec3 vout)
 {
        vout = (mat * vec4(vin, 1.0)).xyz;
@@ -1868,7 +1874,7 @@ void node_tex_coord(
         out vec3 camera, out vec3 window, out vec3 reflection)
 {
        generated = attr_orco;
-       normal = normalize((obinvmat * (viewinvmat * vec4(N, 0.0))).xyz);
+       normal = normalize(NormalMatrixInverse * N);
        uv = attr_uv;
        object = (obinvmat * (viewinvmat * vec4(I, 1.0))).xyz;
        camera = vec3(I.xy, -I.z);
@@ -2319,7 +2325,7 @@ void node_tex_image_box(vec3 texco,
        }
        else {
                /* last case, we have a mix between three */
-               weight = ((2.0 - limit) * N + (limit - 1.0)) / max(1e-8, 2.0 * limit - 1.0);
+               weight = ((2.0 - limit) * N + (limit - 1.0)) / max(1e-8, blend);
        }
 
        color = weight.x * color1 + weight.y * color2 + weight.z * color3;
index ba5d34a445f0ec538c6d5892b724e41816714fa1..3491a28fa8f9e515fecbb337250d1a0b4b0ea322 100644 (file)
@@ -123,12 +123,9 @@ static int node_shader_gpu_tex_image(GPUMaterial *mat, bNode *node, bNodeExecDat
                        GPU_stack_link(mat, node, gpu_node_name, in, out, GPU_image(ima, iuser, isdata));
                        break;
                case SHD_PROJ_BOX:
-                       GPU_link(mat, "direction_transform_m4v3", GPU_builtin(GPU_VIEW_NORMAL),
-                                                                 GPU_builtin(GPU_INVERSE_VIEW_MATRIX),
-                                                                 &norm);
-                       GPU_link(mat, "direction_transform_m4v3", norm,
-                                                                 GPU_builtin(GPU_INVERSE_OBJECT_MATRIX),
-                                                                 &norm);
+                       GPU_link(mat, "mat3_mul", GPU_builtin(GPU_VIEW_NORMAL),
+                                                 GPU_builtin(GPU_INVERSE_NORMAL_MATRIX),
+                                                 &norm);
                        GPU_link(mat, gpu_node_name, in[0].link,
                                                     norm,
                                                     GPU_image(ima, iuser, isdata),