Matrix4.js 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825
  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. * @author alteredq / http://alteredqualia.com/
  8. * @author mikael emtinger / http://gomo.se/
  9. */
  10. THREE.Matrix4 = function ( n11, n12, n13, n14, n21, n22, n23, n24, n31, n32, n33, n34, n41, n42, n43, n44 ) {
  11. this.set(
  12. n11 || 1, n12 || 0, n13 || 0, n14 || 0,
  13. n21 || 0, n22 || 1, n23 || 0, n24 || 0,
  14. n31 || 0, n32 || 0, n33 || 1, n34 || 0,
  15. n41 || 0, n42 || 0, n43 || 0, n44 || 1
  16. );
  17. this.flat = new Array( 16 );
  18. this.m33 = new THREE.Matrix3();
  19. };
  20. THREE.Matrix4.prototype = {
  21. constructor: THREE.Matrix4,
  22. set: function ( n11, n12, n13, n14, n21, n22, n23, n24, n31, n32, n33, n34, n41, n42, n43, n44 ) {
  23. this.n11 = n11; this.n12 = n12; this.n13 = n13; this.n14 = n14;
  24. this.n21 = n21; this.n22 = n22; this.n23 = n23; this.n24 = n24;
  25. this.n31 = n31; this.n32 = n32; this.n33 = n33; this.n34 = n34;
  26. this.n41 = n41; this.n42 = n42; this.n43 = n43; this.n44 = n44;
  27. return this;
  28. },
  29. identity: function () {
  30. this.set(
  31. 1, 0, 0, 0,
  32. 0, 1, 0, 0,
  33. 0, 0, 1, 0,
  34. 0, 0, 0, 1
  35. );
  36. return this;
  37. },
  38. copy: function ( m ) {
  39. this.set(
  40. m.n11, m.n12, m.n13, m.n14,
  41. m.n21, m.n22, m.n23, m.n24,
  42. m.n31, m.n32, m.n33, m.n34,
  43. m.n41, m.n42, m.n43, m.n44
  44. );
  45. return this;
  46. },
  47. lookAt: function ( eye, center, up ) {
  48. var x = THREE.Matrix4.__v1, y = THREE.Matrix4.__v2, z = THREE.Matrix4.__v3;
  49. z.sub( eye, center ).normalize();
  50. if ( z.length() === 0 ) {
  51. z.z = 1;
  52. }
  53. x.cross( up, z ).normalize();
  54. if ( x.length() === 0 ) {
  55. z.x += 0.0001;
  56. x.cross( up, z ).normalize();
  57. }
  58. y.cross( z, x ).normalize();
  59. this.n11 = x.x; this.n12 = y.x; this.n13 = z.x;
  60. this.n21 = x.y; this.n22 = y.y; this.n23 = z.y;
  61. this.n31 = x.z; this.n32 = y.z; this.n33 = z.z;
  62. return this;
  63. },
  64. multiplyVector3: function ( v ) {
  65. var vx = v.x, vy = v.y, vz = v.z,
  66. d = 1 / ( this.n41 * vx + this.n42 * vy + this.n43 * vz + this.n44 );
  67. v.x = ( this.n11 * vx + this.n12 * vy + this.n13 * vz + this.n14 ) * d;
  68. v.y = ( this.n21 * vx + this.n22 * vy + this.n23 * vz + this.n24 ) * d;
  69. v.z = ( this.n31 * vx + this.n32 * vy + this.n33 * vz + this.n34 ) * d;
  70. return v;
  71. },
  72. multiplyVector4: function ( v ) {
  73. var vx = v.x, vy = v.y, vz = v.z, vw = v.w;
  74. v.x = this.n11 * vx + this.n12 * vy + this.n13 * vz + this.n14 * vw;
  75. v.y = this.n21 * vx + this.n22 * vy + this.n23 * vz + this.n24 * vw;
  76. v.z = this.n31 * vx + this.n32 * vy + this.n33 * vz + this.n34 * vw;
  77. v.w = this.n41 * vx + this.n42 * vy + this.n43 * vz + this.n44 * vw;
  78. return v;
  79. },
  80. rotateAxis: function ( v ) {
  81. var vx = v.x, vy = v.y, vz = v.z;
  82. v.x = vx * this.n11 + vy * this.n12 + vz * this.n13;
  83. v.y = vx * this.n21 + vy * this.n22 + vz * this.n23;
  84. v.z = vx * this.n31 + vy * this.n32 + vz * this.n33;
  85. v.normalize();
  86. return v;
  87. },
  88. crossVector: function ( a ) {
  89. var v = new THREE.Vector4();
  90. v.x = this.n11 * a.x + this.n12 * a.y + this.n13 * a.z + this.n14 * a.w;
  91. v.y = this.n21 * a.x + this.n22 * a.y + this.n23 * a.z + this.n24 * a.w;
  92. v.z = this.n31 * a.x + this.n32 * a.y + this.n33 * a.z + this.n34 * a.w;
  93. v.w = ( a.w ) ? this.n41 * a.x + this.n42 * a.y + this.n43 * a.z + this.n44 * a.w : 1;
  94. return v;
  95. },
  96. multiply: function ( a, b ) {
  97. var a11 = a.n11, a12 = a.n12, a13 = a.n13, a14 = a.n14,
  98. a21 = a.n21, a22 = a.n22, a23 = a.n23, a24 = a.n24,
  99. a31 = a.n31, a32 = a.n32, a33 = a.n33, a34 = a.n34,
  100. a41 = a.n41, a42 = a.n42, a43 = a.n43, a44 = a.n44,
  101. b11 = b.n11, b12 = b.n12, b13 = b.n13, b14 = b.n14,
  102. b21 = b.n21, b22 = b.n22, b23 = b.n23, b24 = b.n24,
  103. b31 = b.n31, b32 = b.n32, b33 = b.n33, b34 = b.n34,
  104. b41 = b.n41, b42 = b.n42, b43 = b.n43, b44 = b.n44;
  105. this.n11 = a11 * b11 + a12 * b21 + a13 * b31 + a14 * b41;
  106. this.n12 = a11 * b12 + a12 * b22 + a13 * b32 + a14 * b42;
  107. this.n13 = a11 * b13 + a12 * b23 + a13 * b33 + a14 * b43;
  108. this.n14 = a11 * b14 + a12 * b24 + a13 * b34 + a14 * b44;
  109. this.n21 = a21 * b11 + a22 * b21 + a23 * b31 + a24 * b41;
  110. this.n22 = a21 * b12 + a22 * b22 + a23 * b32 + a24 * b42;
  111. this.n23 = a21 * b13 + a22 * b23 + a23 * b33 + a24 * b43;
  112. this.n24 = a21 * b14 + a22 * b24 + a23 * b34 + a24 * b44;
  113. this.n31 = a31 * b11 + a32 * b21 + a33 * b31 + a34 * b41;
  114. this.n32 = a31 * b12 + a32 * b22 + a33 * b32 + a34 * b42;
  115. this.n33 = a31 * b13 + a32 * b23 + a33 * b33 + a34 * b43;
  116. this.n34 = a31 * b14 + a32 * b24 + a33 * b34 + a34 * b44;
  117. this.n41 = a41 * b11 + a42 * b21 + a43 * b31 + a44 * b41;
  118. this.n42 = a41 * b12 + a42 * b22 + a43 * b32 + a44 * b42;
  119. this.n43 = a41 * b13 + a42 * b23 + a43 * b33 + a44 * b43;
  120. this.n44 = a41 * b14 + a42 * b24 + a43 * b34 + a44 * b44;
  121. return this;
  122. },
  123. multiplyToArray: function ( a, b, r ) {
  124. this.multiply( a, b );
  125. r[ 0 ] = this.n11; r[ 1 ] = this.n21; r[ 2 ] = this.n31; r[ 3 ] = this.n41;
  126. r[ 4 ] = this.n12; r[ 5 ] = this.n22; r[ 6 ] = this.n32; r[ 7 ] = this.n42;
  127. r[ 8 ] = this.n13; r[ 9 ] = this.n23; r[ 10 ] = this.n33; r[ 11 ] = this.n43;
  128. r[ 12 ] = this.n14; r[ 13 ] = this.n24; r[ 14 ] = this.n34; r[ 15 ] = this.n44;
  129. return this;
  130. },
  131. multiplySelf: function ( m ) {
  132. this.multiply( this, m );
  133. return this;
  134. },
  135. multiplyScalar: function ( s ) {
  136. this.n11 *= s; this.n12 *= s; this.n13 *= s; this.n14 *= s;
  137. this.n21 *= s; this.n22 *= s; this.n23 *= s; this.n24 *= s;
  138. this.n31 *= s; this.n32 *= s; this.n33 *= s; this.n34 *= s;
  139. this.n41 *= s; this.n42 *= s; this.n43 *= s; this.n44 *= s;
  140. return this;
  141. },
  142. determinant: function () {
  143. var n11 = this.n11, n12 = this.n12, n13 = this.n13, n14 = this.n14,
  144. n21 = this.n21, n22 = this.n22, n23 = this.n23, n24 = this.n24,
  145. n31 = this.n31, n32 = this.n32, n33 = this.n33, n34 = this.n34,
  146. n41 = this.n41, n42 = this.n42, n43 = this.n43, n44 = this.n44;
  147. //TODO: make this more efficient
  148. //( based on http://www.euclideanspace.com/maths/algebra/matrix/functions/inverse/fourD/index.htm )
  149. return (
  150. n14 * n23 * n32 * n41-
  151. n13 * n24 * n32 * n41-
  152. n14 * n22 * n33 * n41+
  153. n12 * n24 * n33 * n41+
  154. n13 * n22 * n34 * n41-
  155. n12 * n23 * n34 * n41-
  156. n14 * n23 * n31 * n42+
  157. n13 * n24 * n31 * n42+
  158. n14 * n21 * n33 * n42-
  159. n11 * n24 * n33 * n42-
  160. n13 * n21 * n34 * n42+
  161. n11 * n23 * n34 * n42+
  162. n14 * n22 * n31 * n43-
  163. n12 * n24 * n31 * n43-
  164. n14 * n21 * n32 * n43+
  165. n11 * n24 * n32 * n43+
  166. n12 * n21 * n34 * n43-
  167. n11 * n22 * n34 * n43-
  168. n13 * n22 * n31 * n44+
  169. n12 * n23 * n31 * n44+
  170. n13 * n21 * n32 * n44-
  171. n11 * n23 * n32 * n44-
  172. n12 * n21 * n33 * n44+
  173. n11 * n22 * n33 * n44
  174. );
  175. },
  176. transpose: function () {
  177. var tmp;
  178. tmp = this.n21; this.n21 = this.n12; this.n12 = tmp;
  179. tmp = this.n31; this.n31 = this.n13; this.n13 = tmp;
  180. tmp = this.n32; this.n32 = this.n23; this.n23 = tmp;
  181. tmp = this.n41; this.n41 = this.n14; this.n14 = tmp;
  182. tmp = this.n42; this.n42 = this.n24; this.n24 = tmp;
  183. tmp = this.n43; this.n43 = this.n34; this.n43 = tmp;
  184. return this;
  185. },
  186. clone: function () {
  187. var m = new THREE.Matrix4();
  188. m.n11 = this.n11; m.n12 = this.n12; m.n13 = this.n13; m.n14 = this.n14;
  189. m.n21 = this.n21; m.n22 = this.n22; m.n23 = this.n23; m.n24 = this.n24;
  190. m.n31 = this.n31; m.n32 = this.n32; m.n33 = this.n33; m.n34 = this.n34;
  191. m.n41 = this.n41; m.n42 = this.n42; m.n43 = this.n43; m.n44 = this.n44;
  192. return m;
  193. },
  194. flatten: function () {
  195. this.flat[ 0 ] = this.n11; this.flat[ 1 ] = this.n21; this.flat[ 2 ] = this.n31; this.flat[ 3 ] = this.n41;
  196. this.flat[ 4 ] = this.n12; this.flat[ 5 ] = this.n22; this.flat[ 6 ] = this.n32; this.flat[ 7 ] = this.n42;
  197. this.flat[ 8 ] = this.n13; this.flat[ 9 ] = this.n23; this.flat[ 10 ] = this.n33; this.flat[ 11 ] = this.n43;
  198. this.flat[ 12 ] = this.n14; this.flat[ 13 ] = this.n24; this.flat[ 14 ] = this.n34; this.flat[ 15 ] = this.n44;
  199. return this.flat;
  200. },
  201. flattenToArray: function ( flat ) {
  202. flat[ 0 ] = this.n11; flat[ 1 ] = this.n21; flat[ 2 ] = this.n31; flat[ 3 ] = this.n41;
  203. flat[ 4 ] = this.n12; flat[ 5 ] = this.n22; flat[ 6 ] = this.n32; flat[ 7 ] = this.n42;
  204. flat[ 8 ] = this.n13; flat[ 9 ] = this.n23; flat[ 10 ] = this.n33; flat[ 11 ] = this.n43;
  205. flat[ 12 ] = this.n14; flat[ 13 ] = this.n24; flat[ 14 ] = this.n34; flat[ 15 ] = this.n44;
  206. return flat;
  207. },
  208. flattenToArrayOffset: function( flat, offset ) {
  209. flat[ offset ] = this.n11;
  210. flat[ offset + 1 ] = this.n21;
  211. flat[ offset + 2 ] = this.n31;
  212. flat[ offset + 3 ] = this.n41;
  213. flat[ offset + 4 ] = this.n12;
  214. flat[ offset + 5 ] = this.n22;
  215. flat[ offset + 6 ] = this.n32;
  216. flat[ offset + 7 ] = this.n42;
  217. flat[ offset + 8 ] = this.n13;
  218. flat[ offset + 9 ] = this.n23;
  219. flat[ offset + 10 ] = this.n33;
  220. flat[ offset + 11 ] = this.n43;
  221. flat[ offset + 12 ] = this.n14;
  222. flat[ offset + 13 ] = this.n24;
  223. flat[ offset + 14 ] = this.n34;
  224. flat[ offset + 15 ] = this.n44;
  225. return flat;
  226. },
  227. setTranslation: function( x, y, z ) {
  228. this.set(
  229. 1, 0, 0, x,
  230. 0, 1, 0, y,
  231. 0, 0, 1, z,
  232. 0, 0, 0, 1
  233. );
  234. return this;
  235. },
  236. setScale: function ( x, y, z ) {
  237. this.set(
  238. x, 0, 0, 0,
  239. 0, y, 0, 0,
  240. 0, 0, z, 0,
  241. 0, 0, 0, 1
  242. );
  243. return this;
  244. },
  245. setRotationX: function ( theta ) {
  246. var c = Math.cos( theta ), s = Math.sin( theta );
  247. this.set(
  248. 1, 0, 0, 0,
  249. 0, c, -s, 0,
  250. 0, s, c, 0,
  251. 0, 0, 0, 1
  252. );
  253. return this;
  254. },
  255. setRotationY: function( theta ) {
  256. var c = Math.cos( theta ), s = Math.sin( theta );
  257. this.set(
  258. c, 0, s, 0,
  259. 0, 1, 0, 0,
  260. -s, 0, c, 0,
  261. 0, 0, 0, 1
  262. );
  263. return this;
  264. },
  265. setRotationZ: function( theta ) {
  266. var c = Math.cos( theta ), s = Math.sin( theta );
  267. this.set(
  268. c, -s, 0, 0,
  269. s, c, 0, 0,
  270. 0, 0, 1, 0,
  271. 0, 0, 0, 1
  272. );
  273. return this;
  274. },
  275. setRotationAxis: function( axis, angle ) {
  276. // Based on http://www.gamedev.net/reference/articles/article1199.asp
  277. var c = Math.cos( angle ),
  278. s = Math.sin( angle ),
  279. t = 1 - c,
  280. x = axis.x, y = axis.y, z = axis.z,
  281. tx = t * x, ty = t * y;
  282. this.set(
  283. tx * x + c, tx * y - s * z, tx * z + s * y, 0,
  284. tx * y + s * z, ty * y + c, ty * z - s * x, 0,
  285. tx * z - s * y, ty * z + s * x, t * z * z + c, 0,
  286. 0, 0, 0, 1
  287. );
  288. return this;
  289. },
  290. setPosition: function( v ) {
  291. this.n14 = v.x;
  292. this.n24 = v.y;
  293. this.n34 = v.z;
  294. return this;
  295. },
  296. getPosition: function() {
  297. if ( ! this.position ) {
  298. this.position = new THREE.Vector3();
  299. }
  300. this.position.set( this.n14, this.n24, this.n34 );
  301. return this.position;
  302. },
  303. getColumnX: function() {
  304. if ( ! this.columnX ) {
  305. this.columnX = new THREE.Vector3();
  306. }
  307. this.columnX.set( this.n11, this.n21, this.n31 );
  308. return this.columnX;
  309. },
  310. getColumnY: function() {
  311. if ( ! this.columnY ) {
  312. this.columnY = new THREE.Vector3();
  313. }
  314. this.columnY.set( this.n12, this.n22, this.n32 );
  315. return this.columnY;
  316. },
  317. getColumnZ: function() {
  318. if ( ! this.columnZ ) {
  319. this.columnZ = new THREE.Vector3();
  320. }
  321. this.columnZ.set( this.n13, this.n23, this.n33 );
  322. return this.columnZ;
  323. },
  324. setRotationFromEuler: function( v, order ) {
  325. var x = v.x, y = v.y, z = v.z,
  326. a = Math.cos( x ), b = Math.sin( x ),
  327. c = Math.cos( y ), d = Math.sin( y ),
  328. e = Math.cos( z ), f = Math.sin( z );
  329. switch ( order ) {
  330. case 'YXZ':
  331. var ce = c * e, cf = c * f, de = d * e, df = d * f;
  332. this.n11 = ce + df * b;
  333. this.n12 = de * b - cf;
  334. this.n13 = a * d;
  335. this.n21 = a * f;
  336. this.n22 = a * e;
  337. this.n23 = - b;
  338. this.n31 = cf * b - de;
  339. this.n32 = df + ce * b;
  340. this.n33 = a * c;
  341. break;
  342. case 'ZXY':
  343. var ce = c * e, cf = c * f, de = d * e, df = d * f;
  344. this.n11 = ce - df * b;
  345. this.n12 = - a * f;
  346. this.n13 = de + cf * b;
  347. this.n21 = cf + de * b;
  348. this.n22 = a * e;
  349. this.n23 = df - ce * b;
  350. this.n31 = - a * d;
  351. this.n32 = b;
  352. this.n33 = a * c;
  353. break;
  354. case 'ZYX':
  355. var ae = a * e, af = a * f, be = b * e, bf = b * f;
  356. this.n11 = c * e;
  357. this.n12 = be * d - af;
  358. this.n13 = ae * d + bf;
  359. this.n21 = c * f;
  360. this.n22 = bf * d + ae;
  361. this.n23 = af * d - be;
  362. this.n31 = - d;
  363. this.n32 = b * c;
  364. this.n33 = a * c;
  365. break;
  366. case 'YZX':
  367. var ac = a * c, ad = a * d, bc = b * c, bd = b * d;
  368. this.n11 = c * e;
  369. this.n12 = bd - ac * f;
  370. this.n13 = bc * f + ad;
  371. this.n21 = f;
  372. this.n22 = a * e;
  373. this.n23 = - b * e;
  374. this.n31 = - d * e;
  375. this.n32 = ad * f + bc;
  376. this.n33 = ac - bd * f;
  377. break;
  378. case 'XZY':
  379. var ac = a * c, ad = a * d, bc = b * c, bd = b * d;
  380. this.n11 = c * e;
  381. this.n12 = - f;
  382. this.n13 = d * e;
  383. this.n21 = ac * f + bd;
  384. this.n22 = a * e;
  385. this.n23 = ad * f - bc;
  386. this.n31 = bc * f - ad;
  387. this.n32 = b * e;
  388. this.n33 = bd * f + ac;
  389. break;
  390. default: // 'XYZ'
  391. var ae = a * e, af = a * f, be = b * e, bf = b * f;
  392. this.n11 = c * e;
  393. this.n12 = - c * f;
  394. this.n13 = d;
  395. this.n21 = af + be * d;
  396. this.n22 = ae - bf * d;
  397. this.n23 = - b * c;
  398. this.n31 = bf - ae * d;
  399. this.n32 = be + af * d;
  400. this.n33 = a * c;
  401. break;
  402. }
  403. return this;
  404. },
  405. setRotationFromQuaternion: function( q ) {
  406. var x = q.x, y = q.y, z = q.z, w = q.w,
  407. x2 = x + x, y2 = y + y, z2 = z + z,
  408. xx = x * x2, xy = x * y2, xz = x * z2,
  409. yy = y * y2, yz = y * z2, zz = z * z2,
  410. wx = w * x2, wy = w * y2, wz = w * z2;
  411. this.n11 = 1 - ( yy + zz );
  412. this.n12 = xy - wz;
  413. this.n13 = xz + wy;
  414. this.n21 = xy + wz;
  415. this.n22 = 1 - ( xx + zz );
  416. this.n23 = yz - wx;
  417. this.n31 = xz - wy;
  418. this.n32 = yz + wx;
  419. this.n33 = 1 - ( xx + yy );
  420. return this;
  421. },
  422. scale: function ( v ) {
  423. var x = v.x, y = v.y, z = v.z;
  424. this.n11 *= x; this.n12 *= y; this.n13 *= z;
  425. this.n21 *= x; this.n22 *= y; this.n23 *= z;
  426. this.n31 *= x; this.n32 *= y; this.n33 *= z;
  427. this.n41 *= x; this.n42 *= y; this.n43 *= z;
  428. return this;
  429. },
  430. extractPosition: function ( m ) {
  431. this.n14 = m.n14;
  432. this.n24 = m.n24;
  433. this.n34 = m.n34;
  434. },
  435. extractRotation: function ( m, s ) {
  436. var invScaleX = 1 / s.x, invScaleY = 1 / s.y, invScaleZ = 1 / s.z;
  437. this.n11 = m.n11 * invScaleX;
  438. this.n21 = m.n21 * invScaleX;
  439. this.n31 = m.n31 * invScaleX;
  440. this.n12 = m.n12 * invScaleY;
  441. this.n22 = m.n22 * invScaleY;
  442. this.n32 = m.n32 * invScaleY;
  443. this.n13 = m.n13 * invScaleZ;
  444. this.n23 = m.n23 * invScaleZ;
  445. this.n33 = m.n33 * invScaleZ;
  446. }
  447. };
  448. THREE.Matrix4.makeInvert = function ( m1, m2 ) {
  449. // based on http://www.euclideanspace.com/maths/algebra/matrix/functions/inverse/fourD/index.htm
  450. var n11 = m1.n11, n12 = m1.n12, n13 = m1.n13, n14 = m1.n14,
  451. n21 = m1.n21, n22 = m1.n22, n23 = m1.n23, n24 = m1.n24,
  452. n31 = m1.n31, n32 = m1.n32, n33 = m1.n33, n34 = m1.n34,
  453. n41 = m1.n41, n42 = m1.n42, n43 = m1.n43, n44 = m1.n44;
  454. if ( m2 === undefined ) m2 = new THREE.Matrix4();
  455. m2.n11 = n23*n34*n42 - n24*n33*n42 + n24*n32*n43 - n22*n34*n43 - n23*n32*n44 + n22*n33*n44;
  456. m2.n12 = n14*n33*n42 - n13*n34*n42 - n14*n32*n43 + n12*n34*n43 + n13*n32*n44 - n12*n33*n44;
  457. m2.n13 = n13*n24*n42 - n14*n23*n42 + n14*n22*n43 - n12*n24*n43 - n13*n22*n44 + n12*n23*n44;
  458. m2.n14 = n14*n23*n32 - n13*n24*n32 - n14*n22*n33 + n12*n24*n33 + n13*n22*n34 - n12*n23*n34;
  459. m2.n21 = n24*n33*n41 - n23*n34*n41 - n24*n31*n43 + n21*n34*n43 + n23*n31*n44 - n21*n33*n44;
  460. m2.n22 = n13*n34*n41 - n14*n33*n41 + n14*n31*n43 - n11*n34*n43 - n13*n31*n44 + n11*n33*n44;
  461. m2.n23 = n14*n23*n41 - n13*n24*n41 - n14*n21*n43 + n11*n24*n43 + n13*n21*n44 - n11*n23*n44;
  462. m2.n24 = n13*n24*n31 - n14*n23*n31 + n14*n21*n33 - n11*n24*n33 - n13*n21*n34 + n11*n23*n34;
  463. m2.n31 = n22*n34*n41 - n24*n32*n41 + n24*n31*n42 - n21*n34*n42 - n22*n31*n44 + n21*n32*n44;
  464. m2.n32 = n14*n32*n41 - n12*n34*n41 - n14*n31*n42 + n11*n34*n42 + n12*n31*n44 - n11*n32*n44;
  465. m2.n33 = n13*n24*n41 - n14*n22*n41 + n14*n21*n42 - n11*n24*n42 - n12*n21*n44 + n11*n22*n44;
  466. m2.n34 = n14*n22*n31 - n12*n24*n31 - n14*n21*n32 + n11*n24*n32 + n12*n21*n34 - n11*n22*n34;
  467. m2.n41 = n23*n32*n41 - n22*n33*n41 - n23*n31*n42 + n21*n33*n42 + n22*n31*n43 - n21*n32*n43;
  468. m2.n42 = n12*n33*n41 - n13*n32*n41 + n13*n31*n42 - n11*n33*n42 - n12*n31*n43 + n11*n32*n43;
  469. m2.n43 = n13*n22*n41 - n12*n23*n41 - n13*n21*n42 + n11*n23*n42 + n12*n21*n43 - n11*n22*n43;
  470. m2.n44 = n12*n23*n31 - n13*n22*n31 + n13*n21*n32 - n11*n23*n32 - n12*n21*n33 + n11*n22*n33;
  471. m2.multiplyScalar( 1 / m1.determinant() );
  472. return m2;
  473. };
  474. THREE.Matrix4.makeInvert3x3 = function ( m1 ) {
  475. // input: THREE.Matrix4, output: THREE.Matrix3
  476. // ( based on http://code.google.com/p/webgl-mjs/ )
  477. var m33 = m1.m33, m33m = m33.m,
  478. a11 = m1.n33 * m1.n22 - m1.n32 * m1.n23,
  479. a21 = - m1.n33 * m1.n21 + m1.n31 * m1.n23,
  480. a31 = m1.n32 * m1.n21 - m1.n31 * m1.n22,
  481. a12 = - m1.n33 * m1.n12 + m1.n32 * m1.n13,
  482. a22 = m1.n33 * m1.n11 - m1.n31 * m1.n13,
  483. a32 = - m1.n32 * m1.n11 + m1.n31 * m1.n12,
  484. a13 = m1.n23 * m1.n12 - m1.n22 * m1.n13,
  485. a23 = - m1.n23 * m1.n11 + m1.n21 * m1.n13,
  486. a33 = m1.n22 * m1.n11 - m1.n21 * m1.n12,
  487. det = m1.n11 * a11 + m1.n21 * a12 + m1.n31 * a13,
  488. idet;
  489. // no inverse
  490. if ( det == 0 ) {
  491. console.error( 'THREE.Matrix4.makeInvert3x3: Matrix not invertible.' );
  492. }
  493. idet = 1.0 / det;
  494. m33m[ 0 ] = idet * a11; m33m[ 1 ] = idet * a21; m33m[ 2 ] = idet * a31;
  495. m33m[ 3 ] = idet * a12; m33m[ 4 ] = idet * a22; m33m[ 5 ] = idet * a32;
  496. m33m[ 6 ] = idet * a13; m33m[ 7 ] = idet * a23; m33m[ 8 ] = idet * a33;
  497. return m33;
  498. }
  499. THREE.Matrix4.makeFrustum = function ( left, right, bottom, top, near, far ) {
  500. var m, x, y, a, b, c, d;
  501. m = new THREE.Matrix4();
  502. x = 2 * near / ( right - left );
  503. y = 2 * near / ( top - bottom );
  504. a = ( right + left ) / ( right - left );
  505. b = ( top + bottom ) / ( top - bottom );
  506. c = - ( far + near ) / ( far - near );
  507. d = - 2 * far * near / ( far - near );
  508. m.n11 = x; m.n12 = 0; m.n13 = a; m.n14 = 0;
  509. m.n21 = 0; m.n22 = y; m.n23 = b; m.n24 = 0;
  510. m.n31 = 0; m.n32 = 0; m.n33 = c; m.n34 = d;
  511. m.n41 = 0; m.n42 = 0; m.n43 = - 1; m.n44 = 0;
  512. return m;
  513. };
  514. THREE.Matrix4.makePerspective = function ( fov, aspect, near, far ) {
  515. var ymax, ymin, xmin, xmax;
  516. ymax = near * Math.tan( fov * Math.PI / 360 );
  517. ymin = - ymax;
  518. xmin = ymin * aspect;
  519. xmax = ymax * aspect;
  520. return THREE.Matrix4.makeFrustum( xmin, xmax, ymin, ymax, near, far );
  521. };
  522. THREE.Matrix4.makeOrtho = function ( left, right, top, bottom, near, far ) {
  523. var m, x, y, z, w, h, p;
  524. m = new THREE.Matrix4();
  525. w = right - left;
  526. h = top - bottom;
  527. p = far - near;
  528. x = ( right + left ) / w;
  529. y = ( top + bottom ) / h;
  530. z = ( far + near ) / p;
  531. m.n11 = 2 / w; m.n12 = 0; m.n13 = 0; m.n14 = -x;
  532. m.n21 = 0; m.n22 = 2 / h; m.n23 = 0; m.n24 = -y;
  533. m.n31 = 0; m.n32 = 0; m.n33 = -2 / p; m.n34 = -z;
  534. m.n41 = 0; m.n42 = 0; m.n43 = 0; m.n44 = 1;
  535. return m;
  536. };
  537. THREE.Matrix4.__v1 = new THREE.Vector3();
  538. THREE.Matrix4.__v2 = new THREE.Vector3();
  539. THREE.Matrix4.__v3 = new THREE.Vector3();