• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
cozzie

Where to calculate TBN matrix?

14 posts in this topic

Hi,

In my normal mapping implementation I currently calculate the TBN matrix in my vertex shader and pass it to the pixel shader.

There I take the normal map normal and using the TBN matrix transform it to world space, for lighting calculations.

All works fine.

 

Now I was wondering, in some implementations I've seen, people send the normal/binormal/tangent from the VS to the PS and build the TBN matrix in the pixel shader. Or somewhere in between, do the world transformation of those 3 vectors in the VS, send to PS and normalize there + build TBN matrix.

 

I've tried out both approaches but with no visible difference.

Can someone tell me why it would be beneficial to create the TBN matrix in a pixel shader instead of the vertex shader?

(the final normal is still for the pixel, because I transform the normal from the normal map in the pixel shader)

 

Note: when I calculate the TBN matrix in the VS, I also normalize the end resulting normal (normalize(mul(normalMapNormal, TBN)).

0

Share this post


Link to post
Share on other sites

Thanks.

That makes sense.

 

So basically it doesn't matter if I send 3 float3's from the VS to the PS or 1 float3x3.

 

And how about multiplying the VS input normal/tangent/bitangent with the world matrix?

I'd say I do that in the VS because then it doesn't need to be executed per pixel (and the result is the same I suppose?)

 

Same for normalizing the result of normal/tanget/bitangent multiplied with the world matrix.

Would you do those 2 actions in the VS?

0

Share this post


Link to post
Share on other sites

So basically it doesn't matter if I send 3 float3's from the VS to the PS or 1 float3x3.

 

Correct.

 

 

 


And how about multiplying the VS input normal/tangent/bitangent with the world matrix?
I'd say I do that in the VS because then it doesn't need to be executed per pixel (and the result is the same I suppose?)

 

Yes, exactly. Do it in the VS for this reason.

 

 

 


Same for normalizing the result of normal/tanget/bitangent multiplied with the world matrix.
Would you do those 2 actions in the VS?

 

Interpolated unit vectors don't retain their unit length (consider what would happen if they pointed 180 degrees from each other and were weighted 0.5 each). So you'll need to re-normalize in the pixel shader if you want correct results (you'll probably want to keep normalizing in the vertex shader too).

Edited by phil_t
0

Share this post


Link to post
Share on other sites

Thanks, I've got it in place.

Here it is (just the relevant code snippets):

struct VS_OUTPUT
{
	float4 	Pos			: POSITION0;
	float3 	NormalWorld		: TEXCOORD1;
	float2 	TexCoord		: TEXCOORD2;
	float3 	wPos			: TEXCOORD3;
	float3 	ViewDir		: TEXCOORD4;
	float3	BiNormalWorld	: TEXCOORD5;
	float3	TangentWorld	: TEXCOORD6;
};


VS_OUTPUT VS_function(VS_INPUT input)
{
	VS_OUTPUT Out = (VS_OUTPUT)0;

	float4 worldPosition = mul(input.Pos, World);
	Out.Pos = mul(worldPosition, ViewProj);

	Out.TexCoord = input.TexCoord;
	Out.wPos = worldPosition.xyz;
	Out.ViewDir = normalize(CameraPos - Out.wPos.xyz);
  
	// Worldspace to Tangent space for normalMapping
	Out.TangentWorld	= normalize(mul(input.Tangent, (float3x3)World));
	Out.BiNormalWorld	= normalize(mul(input.Binormal, (float3x3)World));
	Out.NormalWorld	= normalize(mul(input.Normal, (float3x3)World));
			
	return Out;
}

// part of the pixel shader

float4 PS_function(VS_OUTPUT input): COLOR0
{
	float4 textureColor = tex2D(textureSampler, input.TexCoord);

	float3x3 worldToTangent = float3x3(	normalize(input.TangentWorld),
						 	normalize(input.BiNormalWorld),
							normalize(input.NormalWorld));
	
	float3 normalMap = normalize(2.0 * (tex2D(normalMapSampler, input.TexCoord).xyz) - 1.0);
	normalMap = normalize(mul(normalMap, worldToTangent));


0

Share this post


Link to post
Share on other sites
8 normalize calls and each one involves a square root. In the frank d. Luna's implementation (which i use), it is only 1 normalize call in pixel shader.
0

Share this post


Link to post
Share on other sites

Although it's not directly related to the TBN matrix, I want to point out that you can't compute the View vector in the vertex shader and interpolate that like you're doing. It doesn't interpolate linearly in world space. To get correct view vectors, you need to - unfortunately - send world space position in an interpolator, and then compute the view vector in the pixel shader.

0

Share this post


Link to post
Share on other sites
Thanks both.
@Newtechnology: how would you do that with just one normalize in the PS, you skip normalizing the vectors when they come into the PS? (normal, bitangent and tangent)

@OsmanB: but do I need the view vector per pixel instead of per vertex?
0

Share this post


Link to post
Share on other sites

Thanks both.
@Newtechnology: how would you do that with just one normalize in the PS, you skip normalizing the vectors when they come into the PS? (normal, bitangent and tangent)

Yeah, you 'construct' the TBN matrix using non-normalized N, B and T variables. Then when you use this matrix to transform your normal-map value into a normal, it won't be normalized either -- so you normalize this final value only.
It's probably very slightly less accurate, but is much cheaper!
1

Share this post


Link to post
Share on other sites

Thanks both.
@Newtechnology: how would you do that with just one normalize in the PS, you skip normalizing the vectors when they come into the PS? (normal, bitangent and tangent)

@OsmanB: but do I need the view vector per pixel instead of per vertex?

//---------------------------------------------------------------------------------------
// Transforms a normal map sample to world space.
//---------------------------------------------------------------------------------------
float3 NormalSampleToWorldSpace(float3 normalMapSample, float3 unitNormalW, float3 tangentW)
{
// Uncompress each component from [0,1] to [-1,1].
float3 normalT = 2.0f*normalMapSample - 1.0f;
 
// Build orthonormal basis.
float3 N = unitNormalW;
float3 T = normalize(tangentW - dot(tangentW, N)*N);
float3 B = cross(N, T);
 
float3x3 TBN = float3x3(T, B, N);
 
// Transform from tangent space to world space.
float3 bumpedNormalW = mul(normalT, TBN);
 
return bumpedNormalW;
}
 
//In pixel shader
 
float3 normalMapSample = gNormalMap.Sample(samLinear, pin.Tex).rgb;
float3 bumpedNormalW = NormalSampleToWorldSpace(normalMapSample, pin.NormalW, pin.TangentW);

this approach calculates bitangent in pixel shader.

Edited by newtechnology
1

Share this post


Link to post
Share on other sites

Thanks, in my case it would come down to this:

VS_OUTPUT VS_function(VS_INPUT input)
{
	VS_OUTPUT Out = (VS_OUTPUT)0;

// more stuff

	#ifdef USENORMALMAP
	// Worldspace to Tangent space for normalMapping
	Out.TangentWorld	= mul(input.Tangent, (float3x3)World);
	Out.BiNormalWorld	= mul(input.Binormal, (float3x3)World);
	Out.NormalWorld	= mul(input.Normal, (float3x3)World);
	#else
	Out.Normal = normalize(mul(input.Normal, (float3x3)World));
	#endif

}

// and the PS

float4 PS_function(VS_OUTPUT input): COLOR0
{
// more stuff

	#ifdef USENORMALMAP
	float3x3 worldToTangent = float3x3(input.TangentWorld,
						     input.BiNormalWorld,
						     input.NormalWorld);
	
	float3 finalNormal = normalize(2.0 * (tex2D(normalMapSampler, input.TexCoord).xyz) - 1.0);
	finalNormal = normalize(mul(finalNormal, worldToTangent));

	#else
	float3 finalNormal = normalize(input.Normal);
	#endif


I do indeed see no visual difference. Save me quite some normalizations now.

Maybe I can do the same when I don't use normal mapping, only normalize the normal in te PS, not in the VS also?

 

@OsmanB: thanks, I think I can pass the viewdir like this:

// in the VS

	Out.ViewDir = CameraPos - Out.wPos.xyz;

// then in the ps

	float3 ViewDir = normalize(input.ViewDir);

The result is so much nicer, and correct now probably smile.png

 

@Newtechnology: thanks, I tried it out sending just 2 out of 3 and calculating the bitangent in the PS, but I don't see the avantages yet (maybe that's about taste), I might do it when I'm gonna use DXT5 compressed normal maps where I leave 1 channel out and calculate it through the other 2. I've read that this gives higher quality results, didn't see that yet.

Edited by cozzie
0

Share this post


Link to post
Share on other sites

 

@Newtechnology: thanks, I tried it out sending just 2 out of 3 and calculating the bitangent in the PS, but I don't see the avantages yet (maybe that's about taste), I might do it when I'm gonna use DXT5 compressed normal maps where I leave 1 channel out and calculate it through the other 2. I've read that this gives higher quality results, didn't see that yet.

 

you save memory by storing only tangent.

0

Share this post


Link to post
Share on other sites

Ok, but on the other hand it costs performance to calculate it every run of the pixelshader smile.png

Probably an example of possible 'pre-optimization', untill I would do some profiling

Edited by cozzie
0

Share this post


Link to post
Share on other sites

Ok, but on the other hand it costs performance to calculate it every run of the pixelshader smile.png

Probably an example of possible 'pre-optimization', untill I would do some profiling

 

that calculation won't slow down any modern GPU. Although if you have 100 models each with 10,000 vertices, then also it [bitangent] would only take 0.99 MB of space.

Edited by newtechnology
0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0