Advertisement Jump to content
Sign in to follow this  

Computing tangent frame in pixel shader

This topic is 2718 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

In ShaderX5 there is a nice article about generating tangent basis from within a pixel shader, without using precomputed tangents. Since I don't exactly grasp the idea why it works, I decided to take a look somewhere else. And in the popular Eric Lengyel's book I found some nice derivation which is quite close to the one from ShaderX article, but differs in one substantial aspect.
To avoid explaining or writing down the formulas, I took the liberty and scanned selected pages from those books. The scans are in the attachment. There are 3 pages from "Mathematics for 3D Game Programming and Computer Graphics" by Eric Lengyel (named lenX.jpg, X >= 1 && X <= 3) as well as 3 pages from "Normal Mapping wtihout Precomputed Tangents" in ShaderX5 by Christian Schuler (named schX.jpg, X >= 1 && X <= 3. What bothers me is the final formulation of the tangent frame. In Lengyel, in file len2.jpg there is formula 6.35 at the bottom of the page, and something similar in sch3.jpg at the top of the page.
For short, I see Lengyel defining tangent vectors like this:

PositionDelta = TexCoordDelta * Tangent

whereas Schuler defines it:

PositionDelta * Tangent = TexCoordDelta

Where does this discrepancy come from?
Moreover, I swiftly tried computing the tangent vector and visualizing it, and the Lengyel's result was quite close to the visualization of precomputed tangents, whereas Schuler looked completely broken. Note that for Schuler I literally took the code from the book. I'm quite sure that I screwed up cause pictures in Schuler's article are correct so his algorithm must work. In the meantime I struggle with this I thought I could write about these formulas' discrepancy where it all starts so maybe you guys could give me some hint. Any idea?

Share this post

Link to post
Share on other sites
I've taken a look again at the equations in Schuler's article and I dare to say the equations are not exactly correct. Schuler claims the following (scan sch2):

delta_u = |delta_P| |T| cos(alpha) -> delta_u = delta_P dot T

It is said that "the length of T corresponds to the magnitude of the u-gradient". I say that these equations assume that the length of T is 1. To verify, please, take a look at the figure 2.6.1 in scan sch2. Let's consider delta_P2 and delta_u2. We can clearly state that:

delta_u2 / |delta_P2| = cos(alpha)

If we substitute this to the Schuler's equations we get:

delta_u2 = |delta_P2| |T| cos(alpha)
delta_u2 = |delta_P2| |T| (delta_u2 / |delta_P2|) -> |T| = 1

Do I misinterpret something?

Share this post

Link to post
Share on other sites
I didn't say it didn't work. I'm just saying I don't fully understand his theory :). Would be great if somebody could verify my statement about the length of T vector

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using, you agree to our community Guidelines, Terms of Use, and Privacy Policy. is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!