Matrix3x3.cs 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. //**************************************************
  2. // Copyright©2018 何冠峰
  3. // Licensed under the MIT license
  4. //**************************************************
  5. namespace UnityEngine
  6. {
  7. public struct Matrix3x3
  8. {
  9. public static readonly Matrix3x3 identity = new Matrix3x3(1, 0, 0, 0, 1, 0, 0, 0, 1);
  10. public float[] Data;
  11. public Matrix3x3(float m0, float m1, float m2, float m3, float m4, float m5, float m6, float m7, float m8)
  12. {
  13. Data = new float[9];
  14. Data[0] = m0; Data[3] = m3; Data[6] = m6;
  15. Data[1] = m1; Data[4] = m4; Data[7] = m7;
  16. Data[2] = m2; Data[5] = m5; Data[8] = m8;
  17. }
  18. public void SetZero()
  19. {
  20. Data[0] = 0f; Data[3] = 0f; Data[6] = 0f;
  21. Data[1] = 0f; Data[4] = 0f; Data[7] = 0f;
  22. Data[2] = 0f; Data[5] = 0f; Data[8] = 0f;
  23. //The floats are laid out
  24. // m0 m3 m6
  25. // m1 m4 m7
  26. // m2 m5 m8
  27. //Data[0]=m00; Data[3]=m01; Data[6]=m02;
  28. //Data[1]=m10; Data[4]=m11; Data[7]=m12;
  29. //Data[2]=m20; Data[5]=m21; Data[8]=m22;
  30. //Get(0, 0) Get(0, 1) Get(0, 2)
  31. //Get(1, 0) Get(1, 1) Get(1, 2)
  32. //Get(2, 0) Get(2, 1) Get(2, 2)
  33. }
  34. public float Get(int row, int column)
  35. {
  36. return Data[row + (column * 3)];
  37. }
  38. public void Set(int row, int column, float value)
  39. {
  40. Data[row + (column * 3)] = value;
  41. }
  42. public void SetOrthoNormalBasis(Vector3 inX, Vector3 inY, Vector3 inZ)
  43. {
  44. this.Set(0, 0, inX.x); this.Set(0, 1, inY.x); this.Set(0, 2, inZ.x);
  45. this.Set(1, 0, inX.y); this.Set(1, 1, inY.y); this.Set(1, 2, inZ.y);
  46. this.Set(2, 0, inX.z); this.Set(2, 1, inY.z); this.Set(2, 2, inZ.z);
  47. }
  48. public float GetDeterminant()
  49. {
  50. float fCofactor0 = Get(0, 0) * Get(1, 1) * Get(2, 2);
  51. float fCofactor1 = Get(0, 1) * Get(1, 2) * Get(2, 0);
  52. float fCofactor2 = Get(0, 2) * Get(1, 0) * Get(2, 1);
  53. float fCofactor3 = Get(0, 2) * Get(1, 1) * Get(2, 0);
  54. float fCofactor4 = Get(0, 1) * Get(1, 0) * Get(2, 2);
  55. float fCofactor5 = Get(0, 0) * Get(1, 2) * Get(2, 1);
  56. return fCofactor0 + fCofactor1 + fCofactor2 - fCofactor3 - fCofactor4 - fCofactor5;
  57. }
  58. // Right handed
  59. public static bool LookRotationToMatrix(Vector3 viewVec, Vector3 upVec, out Matrix3x3 m)
  60. {
  61. m = Matrix3x3.identity;
  62. Vector3 z = viewVec;
  63. // compute u0
  64. float mag = z.Length();
  65. if (mag < Mathf.Epsilon)
  66. {
  67. return false;
  68. }
  69. z /= mag;
  70. Vector3 x = Vector3.Cross(upVec, z);
  71. mag = x.Length();
  72. if (mag < Mathf.Epsilon)
  73. {
  74. return false;
  75. }
  76. x /= mag;
  77. Vector3 y = Vector3.Cross(z, x);
  78. if (!Mathf.CompareApproximate(y.Length(), 1.0F))
  79. return false;
  80. m.SetOrthoNormalBasis(x, y, z);
  81. return true;
  82. }
  83. }
  84. }