0

I am writing a shader in cg where I displace the vertexes. Because I displace the vertexes, I recalculate the normals so they point away from the surface and give that information to the fragment function. In the shader I also implemented a normal map, now am I wondering shouldn't I also recalculate the tangents? And is there a formula to calculate the tangent? I read that it is on a 90 degrees angle of the normal, could I use the cross product for that?

I want to pass on the right tangent to VOUT.tangentWorld. This is my vertex function:

VertexOutput vert (VertexInput i)
        {
            VertexOutput VOUT;

            // put the vert in world space
            float4 newVert = mul(_Object2World,i.vertex);

            // create fake vertexes
            float4 v1 = newVert + float4(0.05,0.0,0.0,0.0) ; // X
            float4 v2 = newVert + float4(0.0,0.0,0.05,0.0) ; // Z

            // assign the displacement map to uv coords 
            float4 disp = tex2Dlod(_Displacement, float4(newVert.x + (_Time.x * _Speed), newVert.z + (_Time.x * _Speed),0.0,0.0));
            float4 disp2 = tex2Dlod(_Displacement, float4(v1.x + (_Time.x * _Speed), newVert.z + (_Time.x * _Speed),0.0,0.0));
            float4 disp3 = tex2Dlod(_Displacement, float4(newVert.x + (_Time.x * _Speed), v2.z + (_Time.x * _Speed),0.0,0.0));

            // offset the main vert
            newVert.y += _Scale * disp.y; 

            // offset fake vertexes 
            v1 += _Scale * disp2.y; 
            v2 += _Scale * disp3.y; 

            // calculate the new normal direction
            float3 newNor = cross(v2 - newVert, v1 - newVert);

            // return world position of the vert for frag calculations
            VOUT.posWorld = newVert;

            // set the vert back in object space
            float4 vertObjectSpace = mul(newVert,_World2Object);

            // apply unity mvp matrix to the vert
            VOUT.pos = mul(UNITY_MATRIX_MVP,vertObjectSpace);

            //return the tex coords for frag calculations
            VOUT.tex = i.texcoord;

            // return normal, tangents, and binormal information for frag calculations
            VOUT.normalWorld = normalize( mul(float4(newNor,0.0),_World2Object).xyz);
            VOUT.tangentWorld = normalize( mul(_Object2World,i.tangent).xyz);
            VOUT.binormalWorld = normalize( cross(VOUT.normalWorld, VOUT.tangentWorld) * i.tangent.w);

            return VOUT;
        }

Isn't it just the vector v2 - newVert or v1 - newVert because the point along the surface? And how do I know which one of the two it is?

anonymous-dev
  • 2,897
  • 9
  • 48
  • 112

1 Answers1

1

I've used something like the below:

 Vector3 tangent = Vector3.Cross( normal, Vector3.forward );
 if( tangent.magnitude == 0 ) {
   tangent = Vector3.Cross( normal, Vector3.up );
 }

Source: http://answers.unity3d.com/questions/133680/how-do-you-find-the-tangent-from-a-given-normal.html

slumtrimpet
  • 3,159
  • 2
  • 31
  • 44
  • Isn't it just the vector `v2 - newVert` or `v1 - newVert` because the point along the surface? And how do I know which one of the two it is? – anonymous-dev Aug 07 '15 at 01:49