1
$\begingroup$

enter image description here

I have been trying to apply normal maps to a rotating sphere with light shining directing from above the sphere (0,1,0). but lighting does not look correct under rotation as one side of the sphere appears darker and the top side of sphere does not seem lit as expected.

I have tried using a fake normal of (0,0,1) and multiplying that by my TBN matrix to ensure the T,B,N are correct and that all seems to work as expected so I have run of ideas of what the issue might be.

 if(has_normal_map){
        simd_float3 colour = flatMap.sample(textureSampler, in.tex).rgb;
        simd_float3 tangent = normalize(in.tangent - dot(in.tangent, in.normal) * in.normal);
        simd_float3 bitangent = normalize(cross(in.normal, tangent));
        simd_float3x3 TBN = simd_float3x3(normalize(tangent),bitangent,normalize(in.normal));
        float3 normal = (normalMap.sample(textureSampler, in.tex)).rgb;
        normal = (normal * 2.0 - 1.0);
        normal = normalize(normal);
        //normal = simd_float3(0,0,1);
        normal = normalize(TBN * normal);
        float attenuation = saturate(dot(light_vector,normal));
        return float4(attenuation*colour,1);
        
    }
$\endgroup$
4
  • $\begingroup$ The matrix multiplication order might be wrong, but I can't really tell if you create the TBN matrix column-wise or row-wise. Try normal*TBN $\endgroup$
    – Mathis
    Commented Mar 25, 2023 at 21:56
  • 1
    $\begingroup$ simd-float3x3 constructor makes the matrix column wise so that multiplication is okay $\endgroup$
    – Sina Dasht
    Commented Mar 26, 2023 at 9:03
  • 1
    $\begingroup$ Ok! Some other questions: are you sure that your tangent vector is calculated correctly? You are removing the contribution of the normal vector from in.tangent? Maybe try to calculate it naively as "normalize(cross(vec3(0,1,0),normal))", just to check. Are your normals in the [0-1] range or maybe stored as [-1,1]? Otherwise, sorry that I couldn't help and good luck! $\endgroup$
    – Mathis
    Commented Mar 26, 2023 at 10:18
  • $\begingroup$ Usually when computing the bitangent we pass in a sigma value indicating the handedness of the TBN coordinate frame. bitangent = normalize(cross(norm,tang))*sigma Where sigma is computed at the same time as the tangent and bitangent. And is always either +1 or -1. I don't see this being applied anywhere in the posted code sample. $\endgroup$
    – pmw1234
    Commented Mar 29, 2023 at 12:15

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.