glsl – TBN matrix multiplication by regular map texture pattern offers incorrect outcomes

[ad_1]

Vertex shader:

#model 450

structure(push_constant) uniform MVPConst{
    mat4 m;
    mat4 n;
} mvp;

structure(binding = 0) uniform UBO{
    mat4  vM;
    mat4  cM;
    mat4  pM;
    vec4  lP;
    float time;
    uvec2 res;
    float varA;
} ubo;

structure(location = 0) in vec3 vert_pos;
structure(location = 1) in vec2 vert_uvco;
structure(location = 2) in vec3 vert_norm;
structure(location = 3) in vec4 vert_tangent;
structure(location = 4) in vec3 vert_bitangent;

structure(location = 0) out vec2 frag_uvco;
structure(location = 1) out vec3 frag_wpos;
structure(location = 2) out vec3 frag_light_dir;
structure(location = 3) out mat3 frag_TBN;

void essential(){
    vec4     wP = mvp.m * vec4(vert_pos,1.0f);
    mat4    mat = ubo.pM * ubo.vM;
    gl_Position = mat * wP;

    frag_uvco = vert_uvco;
    frag_wpos = wP.xyz;

    frag_light_dir = ubo.lP.xyz - wP.xyz;

    vec3 T = normalize(vec3(mvp.n * vert_tangent));
    vec3 N = normalize(vec3(mvp.n * vec4(vert_norm,1.)));
    vec3 B = normalize(cross(N,T) * vert_tangent.w);

    frag_TBN = mat3(T,B,N);
}

Fragment shader:

#model 450

#outline PI 3.14159265359

structure(binding = 0) uniform UBO{
    mat4  vM;
    mat4  cM;
    mat4  pM;
    vec4  lP;
    float time;
    uvec2 res;
    float varA;
} ubo;

structure(binding = 1) uniform sampler2DArray tex_samplr;

structure(location = 0) in vec2 frag_uvco;
structure(location = 1) in vec3 frag_wpos;
structure(location = 2) in vec3 frag_light_dir;
structure(location = 3) in mat3 frag_TBN;

structure(location = 0) out vec4 out_frag;

uvec2 res  = ubo.res;
float time = ubo.time;

vec3 specular(){
    vec3  diffuse_light = vec3(1.);
    vec3 specular_light = vec3(.0);
    vec3      tex_color = texture(tex_samplr,vec3(frag_uvco * 10,0)).rgb;
    vec3       tex_norm = texture(tex_samplr,vec3(frag_uvco * 10,1)).rgb;
               tex_norm = tex_norm * 2.0 - 1.0;
               tex_norm = normalize(tex_norm * frag_TBN);

    vec3 surf_norm;

    if(ubo.varA > 1.)
    surf_norm = normalize(tex_norm);
    else
    surf_norm = normalize(frag_TBN[2]);

    return surf_norm;
}

void essential(){
    out_frag.rgb = specular();
}

Vertex Normal

Normal Map * TBN

My up vector is Z (Blue)

Methods to repair it?

[ad_2]

Categories:

Leave a Reply

Your email address will not be published. Required fields are marked *