Created
January 7, 2014 22:54
-
-
Save borisbat/8308442 to your computer and use it in GitHub Desktop.
Tangent space
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 ); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment