【问题标题】:Get the tangent from a normal direction cg unity3d从法线方向cg unity3d获取切线
【发布时间】:2019-09-30 17:31:23
【问题描述】:

我正在 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 因为沿曲面的点吗?我怎么知道它是两者中的哪一个?

【问题讨论】:

  • 我认为会像 slumtrimpet 所说的那样涉及交叉产品。

标签: unity3d cg


【解决方案1】:

我用过类似下面的东西:

 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

【讨论】:

  • 不只是向量v2 - newVertv1 - newVert 因为沿曲面的点吗?我怎么知道它是两者中的哪一个?
猜你喜欢
  • 1970-01-01
  • 2013-04-17
  • 1970-01-01
  • 1970-01-01
  • 2022-01-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多