MegaMatrix.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. using UnityEngine;
  2. public class MegaMatrix
  3. {
  4. static public void Set(ref Matrix4x4 mat, float[] vals)
  5. {
  6. if ( vals.Length >= 16 )
  7. {
  8. for ( int i = 0; i < 16; i++ )
  9. mat[i] = vals[i];
  10. }
  11. }
  12. static public void Translate(ref Matrix4x4 mat, Vector3 p)
  13. {
  14. Translate(ref mat, p.x, p.y, p.z);
  15. }
  16. static public void Scale(ref Matrix4x4 mat, Vector3 s, bool trans)
  17. {
  18. Matrix4x4 tm = Matrix4x4.identity;
  19. tm[0, 0] = s.x;
  20. tm[1, 1] = s.y;
  21. tm[2, 2] = s.z;
  22. mat = tm * mat;
  23. if ( trans )
  24. {
  25. mat[0, 3] *= s.x;
  26. mat[1, 3] *= s.y;
  27. mat[2, 3] *= s.z;
  28. }
  29. }
  30. static public Vector3 GetTrans(ref Matrix4x4 mat)
  31. {
  32. Vector3 p = new Vector3();
  33. p.x = mat[0, 3];
  34. p.y = mat[1, 3];
  35. p.z = mat[2, 3];
  36. return p;
  37. }
  38. static public void SetTrans(ref Matrix4x4 mat, Vector3 p)
  39. {
  40. mat[0, 3] = p.x;
  41. mat[1, 3] = p.y;
  42. mat[2, 3] = p.z;
  43. }
  44. static public void NoTrans(ref Matrix4x4 mat)
  45. {
  46. mat[0, 3] = 0.0f;
  47. mat[1, 3] = 0.0f;
  48. mat[2, 3] = 0.0f;
  49. }
  50. static public void Translate(ref Matrix4x4 mat, float x, float y, float z)
  51. {
  52. Matrix4x4 tm = Matrix4x4.identity;
  53. tm[0, 3] = x;
  54. tm[1, 3] = y;
  55. tm[2, 3] = z;
  56. mat = tm * mat;
  57. }
  58. static public void RotateX(ref Matrix4x4 mat, float ang)
  59. {
  60. Matrix4x4 tm = Matrix4x4.identity;
  61. float c = Mathf.Cos(ang);
  62. float s = Mathf.Sin(ang);
  63. tm[1, 1] = c;
  64. tm[1, 2] = s;
  65. tm[2, 1] = -s;
  66. tm[2, 2] = c;
  67. mat = tm * mat;
  68. }
  69. static public void RotateY(ref Matrix4x4 mat, float ang)
  70. {
  71. Matrix4x4 tm = Matrix4x4.identity;
  72. float c = Mathf.Cos(ang);
  73. float s = Mathf.Sin(ang);
  74. tm[0, 0] = c;
  75. tm[0, 2] = -s;
  76. tm[2, 0] = s;
  77. tm[2, 2] = c;
  78. mat = tm * mat;
  79. }
  80. static public void RotateZ(ref Matrix4x4 mat, float ang)
  81. {
  82. Matrix4x4 tm = Matrix4x4.identity;
  83. float c = Mathf.Cos(ang);
  84. float s = Mathf.Sin(ang);
  85. tm[0, 0] = c;
  86. tm[0, 1] = s;
  87. tm[1, 0] = -s;
  88. tm[1, 1] = c;
  89. mat = tm * mat;
  90. }
  91. static public void Rotate(ref Matrix4x4 mat, Vector3 rot)
  92. {
  93. RotateX(ref mat, rot.x);
  94. RotateY(ref mat, rot.y);
  95. RotateZ(ref mat, rot.z);
  96. }
  97. static public void LookAt(ref Matrix4x4 mat, Vector3 source_pos, Vector3 target_pos)
  98. {
  99. Vector3 source_target_unit_vector = target_pos - source_pos;
  100. Vector3 ydir = Vector3.Normalize(target_pos - source_pos);
  101. Vector3 zdir = Vector3.Normalize(Vector3.Cross(Vector3.up, ydir));
  102. mat = Matrix4x4.identity;
  103. mat.SetColumn(1, Vector3.Normalize(Vector3.Cross(ydir, zdir)));
  104. mat.SetColumn(2, Vector3.Normalize(source_target_unit_vector));
  105. mat.SetColumn(0, zdir);
  106. }
  107. static public void LookAt(ref Matrix4x4 mat, Vector3 source_pos, Vector3 target_pos, Vector3 up)
  108. {
  109. Vector3 source_target_unit_vector = target_pos - source_pos;
  110. Vector3 ydir = Vector3.Normalize(target_pos - source_pos);
  111. Vector3 zdir = Vector3.Normalize(Vector3.Cross(up, ydir));
  112. mat = Matrix4x4.identity;
  113. mat.SetColumn(1, Vector3.Normalize(Vector3.Cross(ydir, zdir)));
  114. mat.SetColumn(2, Vector3.Normalize(source_target_unit_vector));
  115. mat.SetColumn(0, zdir);
  116. }
  117. static public void Set(ref Matrix4x4 mat, Vector3 right, Vector3 up, Vector3 fwd)
  118. {
  119. mat = Matrix4x4.identity;
  120. #if false
  121. mat.SetColumn(0, right);
  122. mat.SetColumn(1, up);
  123. mat.SetColumn(2, fwd);
  124. #else
  125. mat.SetRow(0, right);
  126. mat.SetRow(1, up);
  127. mat.SetRow(2, fwd);
  128. #endif
  129. }
  130. static public void SetTR(ref Matrix4x4 mat, Vector3 p, Quaternion q)
  131. {
  132. float xx = q.x * q.x;
  133. float yy = q.y * q.y;
  134. float zz = q.z * q.z;
  135. float xy = q.x * q.y;
  136. float xz = q.x * q.z;
  137. float yz = q.y * q.z;
  138. float wx = q.w * q.x;
  139. float wy = q.w * q.y;
  140. float wz = q.w * q.z;
  141. mat.m00 = 1.0f - 2.0f * (yy + zz);
  142. mat.m01 = 2.0f * (xy - wz);
  143. mat.m02 = 2.0f * (xz + wy);
  144. mat.m10 = 2.0f * (xy + wz);
  145. mat.m11 = 1.0f - 2.0f * (xx + zz);
  146. mat.m12 = 2.0f * (yz - wx);
  147. mat.m20 = 2.0f * (xz - wy);
  148. mat.m21 = 2.0f * (yz + wx);
  149. mat.m22 = 1.0f - 2.0f * (xx + yy);
  150. //mat.m30 = mat.m31 = mat.m32 = 0.0f;
  151. mat.m30 = mat.m31 = mat.m32 = 0.0f;
  152. mat.m33 = 1.0f;
  153. mat.m03 = p.x;
  154. mat.m13 = p.y;
  155. mat.m23 = p.z;
  156. }
  157. }