Shaderstudio TBN generation

Hi

Can someone explain me how the TBN matrix
generation in the Dot3 Bumpmap shader works.
As far as i understood it transforms the
Normal by the World space.

Then it use for the Tangent.x=Norm.z;
Tangent.y=0;
Tangent.z=-Norm.x;
for the Binormal.x= 0;
Binormal.y=-1;
Binormal.z= 0;
and for the Normal the World space Normal.

The Tangent is normalised before transform the normalised lightvector by this Matrix.
The transformed lightvector is biased with 1 and scaled by 0.5.

You can find Shaderstudio at http://www.shaderstudio.com

[This message has been edited by glFan (edited 08-08-2002).]

“Can someone explain me how the TBN matrix generation in the Dot3 Bumpmap shader works. As far as i understood it transforms the Normal by the World space.”

As far as I know (its what I use it for), the [TBN] matrix is used to move per-pixel normals from “tangent space” to “object space”. Think of T as the unit vector tangent that is the first derivative for a function that describes the surface the goes in the ‘X’ direction of the object. And think of B as the same thing except in the ‘Y’ direction. (not that X and Y will always be left-right and up-down respectively) And N would be the crossproduct of T and B. The tangent and binormal calculations you list seem to much quicker than calculating derivatives or other means of finding the tangent and binormal of the surface being rendered.

“The Tangent is normalised before transform the normalised lightvector by this Matrix.”

Yes, all 3 vectors of the [TBN] matrix must be of unit length in order to form an orthonormal basis (insert linear algebra definition here). Basically, all 3 vectors must be unit length and perpendicular to eachother. And in order to get proper results from this computation, the light vector must also be of unit length.

“The transformed lightvector is biased with 1 and scaled by 0.5.”

A unit length vector will always have all 3 components within the range of [-1,1]. So if you ‘bias’ (add) one to each component, all the components will now be in the [0,2] range. Then if you scale it by 0.5 (or divide by 2), each component then is in the range of [0,1]. I take it this is done because the light vector is being supplied through a color target (which is clamped to the [0,1] range)???

Anything else?

Dan

Thanks for this explanation Dan,
it has help me a lot. Do you know about disadvantages for this calculation compared with derivative?

glFan

Provided that you are working with a rather simple ‘flat’ surface, this would actually be better. Mostly because your are not having to mess around with derivatives and other expensive calculations. You explicitly know information about the surface. When you would want to use the derivative is when you are given a function that describes a curve or you don’t know that much about the surface itself. For flat surfaces, it’s fine to just use {1,0,0} {0,1,0} {0,0,1} for the [TBN] vectors because for flat surfaces, those are the ones that you would want (and they should be the ones that you would calcuate when you do find them.) It’s just really good to know information beforehand about your surfaces so you can make it run faster. (Or run your surface through some analyzer to calculate and save all the [TBN] information for you so you don’t have to recalculate it all the time, then just load it up with your surface/object/model)

Dan