Sign in to follow this  
qxsl2000

is the TBN matrix of the vertex shader really rotation matrix in nature?

Recommended Posts

Hello guys.I've formerly seen someguy's shader snippet, that the composed TBN matrix was used to transform the attribute a_pos.but obviously it's not related to bump-maping,or anything else. could this be rotation matrix made from input parameters of the shader? those vectors(normal,binormal,tangent) are nothing more than three bases, aren't they?

I mean the TBN matrix of the shader is not used to be the TBN matrix applied on bump-maping ever (as you know, to transform a vector to vector in tangent space,or to transform vector in tangent back using inverse of TBN matrix), and there 's no light calculation involved in,but it directly transform position attribute instead. Really a little bit confusion in my mind.

Hope your guy help me get through it. i really appreciate it.
sorry for my bad english,thank you!

Here's the vertex shader, as the following:



#ifdef GL_ES
#else
#define highp
#define mediump
#define lowp
#endif
uniform mat4 u_mvp;

attribute vec3 a_pos;
attribute vec3 a_normal;
attribute vec2 a_uv0;
attribute vec2 a_uv1;

varying vec2 v_texcoord0;
varying vec2 v_texcoordMap;

uniform highp float u_time;
uniform mediump float u_1DivLevelWidth;
uniform mediump float u_1DivLevelHeight;

void main()
{
float mapPos = a_normal.x * 0.1 + a_normal.y * 0.6;
vec2 v1 = vec2(cos(mapPos + u_time * 0.6), cos(mapPos + u_time * 0.9));
mapPos = (a_normal.x + a_normal.y + a_pos.x + a_pos.y) * 3.0;
vec2 v2 = vec2(cos(mapPos + u_time * 3.2 + (a_uv1.y * 0.1)), cos(mapPos + u_time * 2.9 + (a_uv1.y * 0.1))) * 0.02 * a_uv1.y;


vec3 normal = normalize(vec3(v1 * (a_uv1.x * 0.2), 5.0));
//vec3 normal = vec3(0.0, 0.0, 1.0);
vec3 binormal = normalize(cross(normal, vec3(1.0, 0.0, 0.0)));
vec3 tangent = normalize(cross(binormal, normal));


// to transform the position using the above TBN matrix
// what's the TBN matrix meaning for
vec4 pos = vec4((a_pos * mat3(tangent, binormal, normal)) + a_normal, 1.0) + vec4(v2 * (a_uv1.y * 0.2), 0.0, 0.0);

gl_Position = u_mvp * pos;
v_texcoord0 = a_uv0;
v_texcoordMap = vec2(pos.x * u_1DivLevelWidth, pos.y * u_1DivLevelHeight);
}

Share this post


Link to post
Share on other sites

Yes, 3 orthonormal unit vectors always define an orientation so you can use it for rotation. No matter how you name it, how common the usecase is, between what spaces you wanna rotate or whatever.

 

I often wonder about code like this:

a_pos * mat3(tangent, binormal, normal)

 

What may happen here is that you copy from 3 registers to 3 other registers just to get a matrix.

It's pretty likely this increases shader register usage and occupancy goes down for nothing.

I often have luck getting a faster shader by doing the rotation with dot products of the basis vectors and not using a matrix.

 

EDIT: it's more than just 3 registers, but i don't know if a GPU uses 3 or 4 for single vec3 :)

Edited by JoeJ

Share this post


Link to post
Share on other sites

I often wonder about code like this:
a_pos * mat3(tangent, binormal, normal)

What may happen here is that you copy from 3 registers to 3 other registers just to get a matrix.
It's pretty likely this increases shader register usage and occupancy goes down for nothing.
I often have luck getting a faster shader by doing the rotation with dot products of the basis vectors and not using a matrix.

EDIT: it's more than just 3 registers, but i don't know if a GPU uses 3 or 4 for single vec3 :)

Thank your reply.
yes.i think i got it. good opinion about optimization stuff.i 'll keep it in my mind!

Share this post


Link to post
Share on other sites

Yes. As long as the TBN matrix is composed of three unit-length, orthogonal vectors, then it's a regular rotation matrix. If the three normals aren't unit length and orthogonal, then besides translation, there will be scaling and shearing going on.

Thank you.
That's clear answer. just another question out of my mind. how's the handedness (right or left hand) of the matrix.if i prefer one over another, how to do the conversation between them?

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