Matrix4.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  1. /**
  2. * @author mr.doob / http://mrdoob.com/
  3. * @author supereggbert / http://www.paulbrunt.co.uk/
  4. * @author philogb / http://blog.thejit.org/
  5. * @author jordi_ros / http://plattsoft.com
  6. * @author D1plo1d / http://github.com/D1plo1d
  7. */
  8. THREE.Matrix4 = function () {
  9. this._x = new THREE.Vector3();
  10. this._y = new THREE.Vector3();
  11. this._z = new THREE.Vector3();
  12. };
  13. THREE.Matrix4.prototype = {
  14. n11: 1, n12: 0, n13: 0, n14: 0,
  15. n21: 0, n22: 1, n23: 0, n24: 0,
  16. n31: 0, n32: 0, n33: 1, n34: 0,
  17. n41: 0, n42: 0, n43: 0, n44: 1,
  18. identity: function () {
  19. this.n11 = 1; this.n12 = 0; this.n13 = 0; this.n14 = 0;
  20. this.n21 = 0; this.n22 = 1; this.n23 = 0; this.n24 = 0;
  21. this.n31 = 0; this.n32 = 0; this.n33 = 1; this.n34 = 0;
  22. this.n41 = 0; this.n42 = 0; this.n43 = 0; this.n44 = 1;
  23. },
  24. lookAt: function ( eye, center, up ) {
  25. var x = this._x, y = this._y, z = this._z;
  26. z.sub( eye, center );
  27. z.normalize();
  28. x.cross( up, z );
  29. x.normalize();
  30. y.cross( z, x );
  31. y.normalize();
  32. this.n11 = x.x; this.n12 = x.y; this.n13 = x.z; this.n14 = - x.dot( eye );
  33. this.n21 = y.x; this.n22 = y.y; this.n23 = y.z; this.n24 = - y.dot( eye );
  34. this.n31 = z.x; this.n32 = z.y; this.n33 = z.z; this.n34 = - z.dot( eye );
  35. },
  36. transform: function ( v ) {
  37. var vx = v.x, vy = v.y, vz = v.z, vw = v.w ? v.w : 1.0;
  38. v.x = this.n11 * vx + this.n12 * vy + this.n13 * vz + this.n14 * vw;
  39. v.y = this.n21 * vx + this.n22 * vy + this.n23 * vz + this.n24 * vw;
  40. v.z = this.n31 * vx + this.n32 * vy + this.n33 * vz + this.n34 * vw;
  41. vw = this.n41 * vx + this.n42 * vy + this.n43 * vz + this.n44 * vw;
  42. if( v.w ) {
  43. v.w = vw;
  44. } else {
  45. v.x = v.x / vw;
  46. v.y = v.y / vw;
  47. v.z = v.z / vw;
  48. }
  49. },
  50. crossVector: function ( a ) {
  51. var v = new THREE.Vector4();
  52. v.x = this.n11 * a.x + this.n12 * a.y + this.n13 * a.z + this.n14 * a.w;
  53. v.y = this.n21 * a.x + this.n22 * a.y + this.n23 * a.z + this.n24 * a.w;
  54. v.z = this.n31 * a.x + this.n32 * a.y + this.n33 * a.z + this.n34 * a.w;
  55. v.w = ( a.w ) ? this.n41 * a.x + this.n42 * a.y + this.n43 * a.z + this.n44 * a.w : 1;
  56. return v;
  57. },
  58. multiply: function ( a, b ) {
  59. this.n11 = a.n11 * b.n11 + a.n12 * b.n21 + a.n13 * b.n31 + a.n14 * b.n41;
  60. this.n12 = a.n11 * b.n12 + a.n12 * b.n22 + a.n13 * b.n32 + a.n14 * b.n42;
  61. this.n13 = a.n11 * b.n13 + a.n12 * b.n23 + a.n13 * b.n33 + a.n14 * b.n43;
  62. this.n14 = a.n11 * b.n14 + a.n12 * b.n24 + a.n13 * b.n34 + a.n14 * b.n44;
  63. this.n21 = a.n21 * b.n11 + a.n22 * b.n21 + a.n23 * b.n31 + a.n24 * b.n41;
  64. this.n22 = a.n21 * b.n12 + a.n22 * b.n22 + a.n23 * b.n32 + a.n24 * b.n42;
  65. this.n23 = a.n21 * b.n13 + a.n22 * b.n23 + a.n23 * b.n33 + a.n24 * b.n43;
  66. this.n24 = a.n21 * b.n14 + a.n22 * b.n24 + a.n23 * b.n34 + a.n24 * b.n44;
  67. this.n31 = a.n31 * b.n11 + a.n32 * b.n21 + a.n33 * b.n31 + a.n34 * b.n41;
  68. this.n32 = a.n31 * b.n12 + a.n32 * b.n22 + a.n33 * b.n32 + a.n34 * b.n42;
  69. this.n33 = a.n31 * b.n13 + a.n32 * b.n23 + a.n33 * b.n33 + a.n34 * b.n43;
  70. this.n34 = a.n31 * b.n14 + a.n32 * b.n24 + a.n33 * b.n34 + a.n34 * b.n44;
  71. this.n41 = a.n41 * b.n11 + a.n42 * b.n21 + a.n43 * b.n31 + a.n44 * b.n41;
  72. this.n42 = a.n41 * b.n12 + a.n42 * b.n22 + a.n43 * b.n32 + a.n44 * b.n42;
  73. this.n43 = a.n41 * b.n13 + a.n42 * b.n23 + a.n43 * b.n33 + a.n44 * b.n43;
  74. this.n44 = a.n41 * b.n14 + a.n42 * b.n24 + a.n43 * b.n34 + a.n44 * b.n44;
  75. },
  76. multiplySelf: function ( m ) {
  77. var n11 = this.n11, n12 = this.n12, n13 = this.n13, n14 = this.n14,
  78. n21 = this.n21, n22 = this.n22, n23 = this.n23, n24 = this.n24,
  79. n31 = this.n31, n32 = this.n32, n33 = this.n33, n34 = this.n34,
  80. n41 = this.n41, n42 = this.n42, n43 = this.n43, n44 = this.n44;
  81. this.n11 = n11 * m.n11 + n12 * m.n21 + n13 * m.n31 + n14 * m.n41;
  82. this.n12 = n11 * m.n12 + n12 * m.n22 + n13 * m.n32 + n14 * m.n42;
  83. this.n13 = n11 * m.n13 + n12 * m.n23 + n13 * m.n33 + n14 * m.n43;
  84. this.n14 = n11 * m.n14 + n12 * m.n24 + n13 * m.n34 + n14 * m.n44;
  85. this.n21 = n21 * m.n11 + n22 * m.n21 + n23 * m.n31 + n24 * m.n41;
  86. this.n22 = n21 * m.n12 + n22 * m.n22 + n23 * m.n32 + n24 * m.n42;
  87. this.n23 = n21 * m.n13 + n22 * m.n23 + n23 * m.n33 + n24 * m.n43;
  88. this.n24 = n21 * m.n14 + n22 * m.n24 + n23 * m.n34 + n24 * m.n44;
  89. this.n31 = n31 * m.n11 + n32 * m.n21 + n33 * m.n31 + n34 * m.n41;
  90. this.n32 = n31 * m.n12 + n32 * m.n22 + n33 * m.n32 + n34 * m.n42;
  91. this.n33 = n31 * m.n13 + n32 * m.n23 + n33 * m.n33 + n34 * m.n43;
  92. this.n34 = n31 * m.n14 + n32 * m.n24 + n33 * m.n34 + n34 * m.n44;
  93. this.n41 = n41 * m.n11 + n42 * m.n21 + n43 * m.n31 + n44 * m.n41;
  94. this.n42 = n41 * m.n12 + n42 * m.n22 + n43 * m.n32 + n44 * m.n42;
  95. this.n43 = n41 * m.n13 + n42 * m.n23 + n43 * m.n33 + n44 * m.n43;
  96. this.n44 = n41 * m.n14 + n42 * m.n24 + n43 * m.n34 + n44 * m.n44;
  97. },
  98. multiplyScalar: function ( s ) {
  99. this.n11 *= s; this.n12 *= s; this.n13 *= s; this.n14 *= s;
  100. this.n21 *= s; this.n22 *= s; this.n23 *= s; this.n24 *= s;
  101. this.n31 *= s; this.n32 *= s; this.n33 *= s; this.n34 *= s;
  102. this.n41 *= s; this.n42 *= s; this.n43 *= s; this.n44 *= s;
  103. },
  104. determinant: function () {
  105. //TODO: make this more efficient
  106. //( based on http://www.euclideanspace.com/maths/algebra/matrix/functions/inverse/fourD/index.htm )
  107. return (
  108. this.n14 * this.n23 * this.n32 * this.n41-
  109. this.n13 * this.n24 * this.n32 * this.n41-
  110. this.n14 * this.n22 * this.n33 * this.n41+
  111. this.n12 * this.n24 * this.n33 * this.n41+
  112. this.n13 * this.n22 * this.n34 * this.n41-
  113. this.n12 * this.n23 * this.n34 * this.n41-
  114. this.n14 * this.n23 * this.n31 * this.n42+
  115. this.n13 * this.n24 * this.n31 * this.n42+
  116. this.n14 * this.n21 * this.n33 * this.n42-
  117. this.n11 * this.n24 * this.n33 * this.n42-
  118. this.n13 * this.n21 * this.n34 * this.n42+
  119. this.n11 * this.n23 * this.n34 * this.n42+
  120. this.n14 * this.n22 * this.n31 * this.n43-
  121. this.n12 * this.n24 * this.n31 * this.n43-
  122. this.n14 * this.n21 * this.n32 * this.n43+
  123. this.n11 * this.n24 * this.n32 * this.n43+
  124. this.n12 * this.n21 * this.n34 * this.n43-
  125. this.n11 * this.n22 * this.n34 * this.n43-
  126. this.n13 * this.n22 * this.n31 * this.n44+
  127. this.n12 * this.n23 * this.n31 * this.n44+
  128. this.n13 * this.n21 * this.n32 * this.n44-
  129. this.n11 * this.n23 * this.n32 * this.n44-
  130. this.n12 * this.n21 * this.n33 * this.n44+
  131. this.n11 * this.n22 * this.n33 * this.n44 );
  132. },
  133. clone: function () {
  134. var m = new THREE.Matrix4();
  135. m.n11 = this.n11; m.n12 = this.n12; m.n13 = this.n13; m.n14 = this.n14;
  136. m.n21 = this.n21; m.n22 = this.n22; m.n23 = this.n23; m.n24 = this.n24;
  137. m.n31 = this.n31; m.n32 = this.n32; m.n33 = this.n33; m.n34 = this.n34;
  138. m.n41 = this.n41; m.n42 = this.n42; m.n43 = this.n43; m.n44 = this.n44;
  139. return m;
  140. },
  141. toString: function() {
  142. return "| " + this.n11 + " " + this.n12 + " " + this.n13 + " " + this.n14 + " |\n" +
  143. "| " + this.n21 + " " + this.n22 + " " + this.n23 + " " + this.n24 + " |\n" +
  144. "| " + this.n31 + " " + this.n32 + " " + this.n33 + " " + this.n34 + " |\n" +
  145. "| " + this.n41 + " " + this.n42 + " " + this.n43 + " " + this.n44 + " |";
  146. }
  147. };
  148. THREE.Matrix4.translationMatrix = function ( x, y, z ) {
  149. var m = new THREE.Matrix4();
  150. m.n14 = x;
  151. m.n24 = y;
  152. m.n34 = z;
  153. return m;
  154. };
  155. THREE.Matrix4.scaleMatrix = function ( x, y, z ) {
  156. var m = new THREE.Matrix4();
  157. m.n11 = x;
  158. m.n22 = y;
  159. m.n33 = z;
  160. return m;
  161. };
  162. THREE.Matrix4.rotationXMatrix = function ( theta ) {
  163. var rot = new THREE.Matrix4();
  164. rot.n22 = rot.n33 = Math.cos( theta );
  165. rot.n32 = Math.sin( theta );
  166. rot.n23 = - rot.n32;
  167. return rot;
  168. };
  169. THREE.Matrix4.rotationYMatrix = function ( theta ) {
  170. var rot = new THREE.Matrix4();
  171. rot.n11 = rot.n33 = Math.cos( theta );
  172. rot.n13 = Math.sin( theta );
  173. rot.n31 = - rot.n13;
  174. return rot;
  175. };
  176. THREE.Matrix4.rotationZMatrix = function ( theta ) {
  177. var rot = new THREE.Matrix4();
  178. rot.n11 = rot.n22 = Math.cos( theta );
  179. rot.n21 = Math.sin( theta );
  180. rot.n12 = - rot.n21;
  181. return rot;
  182. };
  183. THREE.Matrix4.makeInvert = function ( m1 ) {
  184. //TODO: make this more efficient
  185. //( based on http://www.euclideanspace.com/maths/algebra/matrix/functions/inverse/fourD/index.htm )
  186. var m2 = new THREE.Matrix4();
  187. m2.n11 = m1.n23*m1.n34*m1.n42 - m1.n24*m1.n33*m1.n42 + m1.n24*m1.n32*m1.n43 - m1.n22*m1.n34*m1.n43 - m1.n23*m1.n32*m1.n44 + m1.n22*m1.n33*m1.n44;
  188. m2.n12 = m1.n14*m1.n33*m1.n42 - m1.n13*m1.n34*m1.n42 - m1.n14*m1.n32*m1.n43 + m1.n12*m1.n34*m1.n43 + m1.n13*m1.n32*m1.n44 - m1.n12*m1.n33*m1.n44;
  189. m2.n13 = m1.n13*m1.n24*m1.n42 - m1.n14*m1.n23*m1.n42 + m1.n14*m1.n22*m1.n43 - m1.n12*m1.n24*m1.n43 - m1.n13*m1.n22*m1.n44 + m1.n12*m1.n23*m1.n44;
  190. m2.n14 = m1.n14*m1.n23*m1.n32 - m1.n13*m1.n24*m1.n32 - m1.n14*m1.n22*m1.n33 + m1.n12*m1.n24*m1.n33 + m1.n13*m1.n22*m1.n34 - m1.n12*m1.n23*m1.n34;
  191. m2.n21 = m1.n24*m1.n33*m1.n41 - m1.n23*m1.n34*m1.n41 - m1.n24*m1.n31*m1.n43 + m1.n21*m1.n34*m1.n43 + m1.n23*m1.n31*m1.n44 - m1.n21*m1.n33*m1.n44;
  192. m2.n22 = m1.n13*m1.n34*m1.n41 - m1.n14*m1.n33*m1.n41 + m1.n14*m1.n31*m1.n43 - m1.n11*m1.n34*m1.n43 - m1.n13*m1.n31*m1.n44 + m1.n11*m1.n33*m1.n44;
  193. m2.n23 = m1.n14*m1.n23*m1.n41 - m1.n13*m1.n24*m1.n41 - m1.n14*m1.n21*m1.n43 + m1.n11*m1.n24*m1.n43 + m1.n13*m1.n21*m1.n44 - m1.n11*m1.n23*m1.n44;
  194. m2.n24 = m1.n13*m1.n24*m1.n31 - m1.n14*m1.n23*m1.n31 + m1.n14*m1.n21*m1.n33 - m1.n11*m1.n24*m1.n33 - m1.n13*m1.n21*m1.n34 + m1.n11*m1.n23*m1.n34;
  195. m2.n31 = m1.n22*m1.n34*m1.n41 - m1.n24*m1.n32*m1.n41 + m1.n24*m1.n31*m1.n42 - m1.n21*m1.n34*m1.n42 - m1.n22*m1.n31*m1.n44 + m1.n21*m1.n32*m1.n44;
  196. m2.n32 = m1.n14*m1.n32*m1.n41 - m1.n12*m1.n34*m1.n41 - m1.n14*m1.n31*m1.n42 + m1.n11*m1.n34*m1.n42 + m1.n12*m1.n31*m1.n44 - m1.n11*m1.n32*m1.n44;
  197. m2.n33 = m1.n13*m1.n24*m1.n41 - m1.n14*m1.n22*m1.n41 + m1.n14*m1.n21*m1.n42 - m1.n11*m1.n24*m1.n42 - m1.n12*m1.n21*m1.n44 + m1.n11*m1.n22*m1.n44;
  198. m2.n34 = m1.n14*m1.n22*m1.n31 - m1.n12*m1.n24*m1.n31 - m1.n14*m1.n21*m1.n32 + m1.n11*m1.n24*m1.n32 + m1.n12*m1.n21*m1.n34 - m1.n11*m1.n22*m1.n34;
  199. m2.n41 = m1.n23*m1.n32*m1.n41 - m1.n22*m1.n33*m1.n41 - m1.n23*m1.n31*m1.n42 + m1.n21*m1.n33*m1.n42 + m1.n22*m1.n31*m1.n43 - m1.n21*m1.n32*m1.n43;
  200. m2.n42 = m1.n12*m1.n33*m1.n41 - m1.n13*m1.n32*m1.n41 + m1.n13*m1.n31*m1.n42 - m1.n11*m1.n33*m1.n42 - m1.n12*m1.n31*m1.n43 + m1.n11*m1.n32*m1.n43;
  201. m2.n43 = m1.n13*m1.n22*m1.n41 - m1.n12*m1.n23*m1.n41 - m1.n13*m1.n21*m1.n42 + m1.n11*m1.n23*m1.n42 + m1.n12*m1.n21*m1.n43 - m1.n11*m1.n22*m1.n43;
  202. m2.n44 = m1.n12*m1.n23*m1.n31 - m1.n13*m1.n22*m1.n31 + m1.n13*m1.n21*m1.n32 - m1.n11*m1.n23*m1.n32 - m1.n12*m1.n21*m1.n33 + m1.n11*m1.n22*m1.n33;
  203. m2.scale( 1 / m1.determinant() );
  204. return m2;
  205. };
  206. THREE.Matrix4.makeFrustum = function( left, right, bottom, top, near, far ) {
  207. var m, x, y, a, b, c, d;
  208. m = new THREE.Matrix4();
  209. x = 2 * near / ( right - left );
  210. y = 2 * near / ( top - bottom );
  211. a = ( right + left ) / ( right - left );
  212. b = ( top + bottom ) / ( top - bottom );
  213. c = - ( far + near ) / ( far - near );
  214. d = - 2 * far * near / ( far - near );
  215. m.n11 = x; m.n12 = 0; m.n13 = a; m.n14 = 0;
  216. m.n21 = 0; m.n22 = y; m.n23 = b; m.n24 = 0;
  217. m.n31 = 0; m.n32 = 0; m.n33 = c; m.n34 = d;
  218. m.n41 = 0; m.n42 = 0; m.n43 = - 1; m.n44 = 0;
  219. return m;
  220. };
  221. THREE.Matrix4.makePerspective = function( fov, aspect, near, far ) {
  222. var ymax, ymin, xmin, xmax;
  223. ymax = near * Math.tan( fov * Math.PI / 360 );
  224. ymin = - ymax;
  225. xmin = ymin * aspect;
  226. xmax = ymax * aspect;
  227. return THREE.Matrix4.makeFrustum( xmin, xmax, ymin, ymax, near, far );
  228. };
  229. THREE.Matrix4.makeOrtho = function( left, right, top, bottom, near, far ) {
  230. var m, x, y, z, w, h, p;
  231. m = new THREE.Matrix4();
  232. w = right - left;
  233. h = bottom - top;
  234. p = far - near;
  235. x = ( right + left ) / w;
  236. y = ( bottom + top ) / h;
  237. z = ( far + near ) / p;
  238. m.n11 = 2 / w; m.n12 = 0; m.n13 = 0; m.n14 = -x;
  239. m.n21 = 0; m.n22 = 2 / h; m.n23 = 0; m.n24 = -y;
  240. m.n31 = 0; m.n32 = 0; m.n33 = -2 / p; m.n34 = -z;
  241. m.n41 = 0; m.n42 = 0; m.n43 = 0; m.n44 = 1;
  242. return m;
  243. };