从法线方向cgunity3d获取切线



我在cg中编写了一个着色器,我在那里置换了顶点。因为我置换了顶点,所以我重新计算法线,使它们远离表面,并将该信息提供给碎片函数。在着色器中,我也实现了一个法线贴图,现在我想知道我不应该重新计算切线吗?有公式可以计算正切吗?我知道它和法线成90度角,我能用外积求出来吗?

我想将右切线传递给VOUT.tangentWorld。这是我的顶点函数

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;
        }

它不就是向量v2 - newVertv1 - newVert吗?因为沿着曲面的点?我怎么知道是哪一个呢?

我使用了如下代码:

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

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

相关内容

  • 没有找到相关文章

最新更新