Wednesday, October 9, 2019

textures - Is it possible to calculate the TBN matrix in the fragment shader?


Just from model position, model normal and texture uv?



Answer



Yes. There an article about this by Chris­t­ian Schüler:
http://www.thetenthplanet.de/archives/1180
It's a followup to a book article (ShaderX 5) which did exactly what you need. I've used it myself.


Here is the part that you need: (p : world-space position, N : world-space normal)


mat3 cotangent_frame( vec3 N, vec3 p, vec2 uv )
{

// get edge vectors of the pixel triangle
vec3 dp1 = dFdx( p );
vec3 dp2 = dFdy( p );
vec2 duv1 = dFdx( uv );
vec2 duv2 = dFdy( uv );

// solve the linear system
vec3 dp2perp = cross( dp2, N );
vec3 dp1perp = cross( N, dp1 );
vec3 T = dp2perp * duv1.x + dp1perp * duv2.x;

vec3 B = dp2perp * duv1.y + dp1perp * duv2.y;

// construct a scale-invariant frame
float invmax = inversesqrt( max( dot(T,T), dot(B,B) ) );
return mat3( T * invmax, B * invmax, N );
}

No comments:

Post a Comment

Simple past, Present perfect Past perfect

Can you tell me which form of the following sentences is the correct one please? Imagine two friends discussing the gym... I was in a good s...