溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

unity如何實(shí)現(xiàn)貼圖矩陣運(yùn)算

發(fā)布時(shí)間:2020-07-20 14:42:44 來源:億速云 閱讀:139 作者:小豬 欄目:開發(fā)技術(shù)

這篇文章主要講解了unity如何實(shí)現(xiàn)貼圖矩陣運(yùn)算,內(nèi)容清晰明了,對(duì)此有興趣的小伙伴可以學(xué)習(xí)一下,相信大家閱讀完之后會(huì)有幫助。

我們?cè)趕hader中對(duì)貼圖處理時(shí),有時(shí)候會(huì)有一些比較復(fù)雜的運(yùn)算,比方說三角函數(shù),開方等,一般情況下,如果可以在越上層做運(yùn)算,性能會(huì)越高。C# > Vertex > fragment

因此,考慮到貼圖的旋轉(zhuǎn)用到的三角函數(shù),可以使用在C#中傳入旋轉(zhuǎn)矩陣得到,然后使用uv直接乘以矩陣就可以了。

封裝了vmatrix4x4,分享一下:

using UnityEngine;
 
namespace D11.Skin
{
 public class VMatrix
 {
  public float[,] m;
 
  public VMatrix()
  {
   m = new float[4, 4];
   m[0, 0] = 0.0f; m[0, 1] = 0.0f; m[0, 2] = 0.0f; m[0, 3] = 0.0f;
   m[1, 0] = 0.0f; m[1, 1] = 0.0f; m[1, 2] = 0.0f; m[1, 3] = 0.0f;
   m[2, 0] = 0.0f; m[2, 1] = 0.0f; m[2, 2] = 0.0f; m[2, 3] = 0.0f;
   m[3, 0] = 0.0f; m[3, 1] = 0.0f; m[3, 2] = 0.0f; m[3, 3] = 0.0f;
  }
 
  public static void MatrixSetIdentity(VMatrix matrix)
  {
   matrix.m[0,0] = 1.0f; matrix.m[0,1] = 0.0f; matrix.m[0,2] = 0.0f; matrix.m[0,3] = 0.0f;
   matrix.m[1,0] = 0.0f; matrix.m[1,1] = 1.0f; matrix.m[1,2] = 0.0f; matrix.m[1,3] = 0.0f;
   matrix.m[2,0] = 0.0f; matrix.m[2,1] = 0.0f; matrix.m[2,2] = 1.0f; matrix.m[2,3] = 0.0f;
   matrix.m[3,0] = 0.0f; matrix.m[3,1] = 0.0f; matrix.m[3,2] = 0.0f; matrix.m[3,3] = 1.0f;
  }
 
  public static void MatrixBuildTranslation(VMatrix matrix, float x, float y, float z)
  {
   MatrixSetIdentity(matrix);
   matrix.m[0,3] = x;
   matrix.m[1,3] = y;
   matrix.m[2,3] = z;
  }
 
  public static void MatrixBuildTranslation(VMatrix matrix, Vector3 vec)
  {
   MatrixSetIdentity(matrix);
   matrix.m[0, 3] = vec.x;
   matrix.m[1, 3] = vec.y;
   matrix.m[2, 3] = vec.z;
  }
 
  public static void MatrixBuildScale(VMatrix matrix, float x, float y, float z)
  {
   matrix.m[0, 0] = x; matrix.m[0, 1] = 0.0f; matrix.m[0, 2] = 0.0f; matrix.m[0, 3] = 0.0f;
   matrix.m[1, 0] = 0.0f; matrix.m[1, 1] = y; matrix.m[1, 2] = 0.0f; matrix.m[1, 3] = 0.0f;
   matrix.m[2, 0] = 0.0f; matrix.m[2, 1] = 0.0f; matrix.m[2, 2] = z; matrix.m[2, 3] = 0.0f;
   matrix.m[3, 0] = 0.0f; matrix.m[3, 1] = 0.0f; matrix.m[3, 2] = 0.0f; matrix.m[3, 3] = 1.0f;
  }
 
  public static void MatrixBuildScale(VMatrix matrix, Vector3 scale)
  {
   MatrixBuildScale(matrix, scale.x, scale.y, scale.z);
  }
 
  public static void MatrixBuildRotate(VMatrix matrix, float angleDegrees)
  {
   float radians = angleDegrees * (Mathf.PI / 180.0f);
 
   float fSin = Mathf.Sin(radians);
   float fCos = Mathf.Cos(radians);
   matrix.m[0, 0] = fCos; matrix.m[0, 1] = -fSin; matrix.m[0, 2] = 0.0f; matrix.m[0, 3] = 0.0f;
   matrix.m[1, 0] = fSin; matrix.m[1, 1] = fCos; matrix.m[1, 2] = 0.0f; matrix.m[1, 3] = 0.0f;
   matrix.m[2, 0] = 0.0f; matrix.m[2, 1] = 0.0f; matrix.m[2, 2] = 1.0f; matrix.m[2, 3] = 0.0f;
   matrix.m[3, 0] = 0.0f; matrix.m[3, 1] = 0.0f; matrix.m[3, 2] = 0.0f; matrix.m[3, 3] = 1.0f;
  }
 
  public static VMatrix MatrixMultiply(VMatrix src1, VMatrix src2)
  {
   VMatrix dst = new VMatrix();
   dst.m[0,0] = src1.m[0,0] * src2.m[0,0] + src1.m[0,1] * src2.m[1,0] + src1.m[0,2] * src2.m[2,0] + src1.m[0,3] * src2.m[3,0];
   dst.m[0,1] = src1.m[0,0] * src2.m[0,1] + src1.m[0,1] * src2.m[1,1] + src1.m[0,2] * src2.m[2,1] + src1.m[0,3] * src2.m[3,1];
   dst.m[0,2] = src1.m[0,0] * src2.m[0,2] + src1.m[0,1] * src2.m[1,2] + src1.m[0,2] * src2.m[2,2] + src1.m[0,3] * src2.m[3,2];
   dst.m[0,3] = src1.m[0,0] * src2.m[0,3] + src1.m[0,1] * src2.m[1,3] + src1.m[0,2] * src2.m[2,3] + src1.m[0,3] * src2.m[3,3];
 
   dst.m[1,0] = src1.m[1,0] * src2.m[0,0] + src1.m[1,1] * src2.m[1,0] + src1.m[1,2] * src2.m[2,0] + src1.m[1,3] * src2.m[3,0];
   dst.m[1,1] = src1.m[1,0] * src2.m[0,1] + src1.m[1,1] * src2.m[1,1] + src1.m[1,2] * src2.m[2,1] + src1.m[1,3] * src2.m[3,1];
   dst.m[1,2] = src1.m[1,0] * src2.m[0,2] + src1.m[1,1] * src2.m[1,2] + src1.m[1,2] * src2.m[2,2] + src1.m[1,3] * src2.m[3,2];
   dst.m[1,3] = src1.m[1,0] * src2.m[0,3] + src1.m[1,1] * src2.m[1,3] + src1.m[1,2] * src2.m[2,3] + src1.m[1,3] * src2.m[3,3];
 
   dst.m[2,0] = src1.m[2,0] * src2.m[0,0] + src1.m[2,1] * src2.m[1,0] + src1.m[2,2] * src2.m[2,0] + src1.m[2,3] * src2.m[3,0];
   dst.m[2,1] = src1.m[2,0] * src2.m[0,1] + src1.m[2,1] * src2.m[1,1] + src1.m[2,2] * src2.m[2,1] + src1.m[2,3] * src2.m[3,1];
   dst.m[2,2] = src1.m[2,0] * src2.m[0,2] + src1.m[2,1] * src2.m[1,2] + src1.m[2,2] * src2.m[2,2] + src1.m[2,3] * src2.m[3,2];
   dst.m[2,3] = src1.m[2,0] * src2.m[0,3] + src1.m[2,1] * src2.m[1,3] + src1.m[2,2] * src2.m[2,3] + src1.m[2,3] * src2.m[3,3];
 
   dst.m[3,0] = src1.m[3,0] * src2.m[0,0] + src1.m[3,1] * src2.m[1,0] + src1.m[3,2] * src2.m[2,0] + src1.m[3,3] * src2.m[3,0];
   dst.m[3,1] = src1.m[3,0] * src2.m[0,1] + src1.m[3,1] * src2.m[1,1] + src1.m[3,2] * src2.m[2,1] + src1.m[3,3] * src2.m[3,1];
   dst.m[3,2] = src1.m[3,0] * src2.m[0,2] + src1.m[3,1] * src2.m[1,2] + src1.m[3,2] * src2.m[2,2] + src1.m[3,3] * src2.m[3,2];
   dst.m[3,3] = src1.m[3,0] * src2.m[0,3] + src1.m[3,1] * src2.m[1,3] + src1.m[3,2] * src2.m[2,3] + src1.m[3,3] * src2.m[3,3];
   return dst;
  }
 
  public Vector4 MatrixGetCol(int nCol)
  {
   System.Diagnostics.Debug.Assert((nCol >= 0) && (nCol <= 3));
 
   Vector4 vec;
   vec.x = m[0,nCol];
   vec.y = m[1,nCol];
   vec.z = m[2,nCol];
   vec.w = m[3,nCol];
   return vec;
  }
 
  public Vector4 MatrixGetRow(int nRow)
  {
   System.Diagnostics.Debug.Assert((nRow >= 0) && (nRow <= 3));
   Vector4 vec;
   vec.x = m[nRow, 0];
   vec.y = m[nRow, 1];
   vec.z = m[nRow, 2];
   vec.w = m[nRow, 3];
   return vec;
  }
 
  public static VMatrix GetSRTMatrix(Vector2 scale, float rotation, Vector2 center, Vector2 translation)
  {
   VMatrix mat = new VMatrix();
   VMatrix temp = new VMatrix();
 
   MatrixBuildScale(mat, scale.x, scale.y, 1.0f);
   MatrixBuildTranslation(temp, -center);
   mat = MatrixMultiply(temp, mat);
   MatrixBuildRotate(temp, rotation);
   mat = MatrixMultiply(temp, mat);
   MatrixBuildTranslation(temp, center.x + translation.x, center.y - translation.y, 0.0f);
   mat = MatrixMultiply(temp, mat);
   return mat;
  }
 }
}

調(diào)用方式:

VMatrix matrix = VMatrix.GetSRTMatrix(scale, -m_cur_rotate, center, translation + translationExtra);
m_CRTTexture.material.SetVector("_SRT0", matrix.MatrixGetRow(0));
m_CRTTexture.material.SetVector("_SRT1", matrix.MatrixGetRow(1));

shader使用:

Properties
{
 _SRT0("PatternSRT0", Vector) = (1, 1, 1, 1)
 _SRT1("PatternSRT1", Vector) = (1, 1, 1, 1)
}
 
Pass
{
 float4 _SRT0;
 float4 _SRT1;
 
 float4 get_pattern_color(float2 uv)
 {
 float2 uv2;
 uv2.x = dot(uv, _SRT0.xy) + _SRT0.w;
 uv2.y = dot(uv, _SRT1.xy) + _SRT1.w;
 return tex2D(_PatternTexture, uv2);
 }
}

感興趣的可以自己試一試

看完上述內(nèi)容,是不是對(duì)unity如何實(shí)現(xiàn)貼圖矩陣運(yùn)算有進(jìn)一步的了解,如果還想學(xué)習(xí)更多內(nèi)容,歡迎關(guān)注億速云行業(yè)資訊頻道。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請(qǐng)聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI