Matrix4.js 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972
  1. /**
  2. * @author mrdoob / 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. * @author timknip / http://www.floorplanner.com/
  10. * @author bhouston / http://exocortex.com
  11. * @author WestLangley / http://github.com/WestLangley
  12. */
  13. THREE.Matrix4 = function ( n11, n12, n13, n14, n21, n22, n23, n24, n31, n32, n33, n34, n41, n42, n43, n44 ) {
  14. this.elements = new Float32Array( 16 );
  15. // TODO: if n11 is undefined, then just set to identity, otherwise copy all other values into matrix
  16. // we should not support semi specification of Matrix4, it is just weird.
  17. var te = this.elements;
  18. te[0] = ( n11 !== undefined ) ? n11 : 1; te[4] = n12 || 0; te[8] = n13 || 0; te[12] = n14 || 0;
  19. te[1] = n21 || 0; te[5] = ( n22 !== undefined ) ? n22 : 1; te[9] = n23 || 0; te[13] = n24 || 0;
  20. te[2] = n31 || 0; te[6] = n32 || 0; te[10] = ( n33 !== undefined ) ? n33 : 1; te[14] = n34 || 0;
  21. te[3] = n41 || 0; te[7] = n42 || 0; te[11] = n43 || 0; te[15] = ( n44 !== undefined ) ? n44 : 1;
  22. };
  23. THREE.Matrix4.prototype = {
  24. constructor: THREE.Matrix4,
  25. set: function ( n11, n12, n13, n14, n21, n22, n23, n24, n31, n32, n33, n34, n41, n42, n43, n44 ) {
  26. var te = this.elements;
  27. te[0] = n11; te[4] = n12; te[8] = n13; te[12] = n14;
  28. te[1] = n21; te[5] = n22; te[9] = n23; te[13] = n24;
  29. te[2] = n31; te[6] = n32; te[10] = n33; te[14] = n34;
  30. te[3] = n41; te[7] = n42; te[11] = n43; te[15] = n44;
  31. return this;
  32. },
  33. identity: function () {
  34. this.set(
  35. 1, 0, 0, 0,
  36. 0, 1, 0, 0,
  37. 0, 0, 1, 0,
  38. 0, 0, 0, 1
  39. );
  40. return this;
  41. },
  42. copy: function ( m ) {
  43. this.elements.set( m.elements );
  44. return this;
  45. },
  46. extractPosition: function ( m ) {
  47. console.warn( 'DEPRECATED: Matrix4\'s .extractPosition() has been renamed to .copyPosition().' );
  48. return this.copyPosition( m );
  49. },
  50. copyPosition: function ( m ) {
  51. var te = this.elements;
  52. var me = m.elements;
  53. te[12] = me[12];
  54. te[13] = me[13];
  55. te[14] = me[14];
  56. return this;
  57. },
  58. extractRotation: function () {
  59. var v1 = new THREE.Vector3();
  60. return function ( m ) {
  61. var te = this.elements;
  62. var me = m.elements;
  63. var scaleX = 1 / v1.set( me[0], me[1], me[2] ).length();
  64. var scaleY = 1 / v1.set( me[4], me[5], me[6] ).length();
  65. var scaleZ = 1 / v1.set( me[8], me[9], me[10] ).length();
  66. te[0] = me[0] * scaleX;
  67. te[1] = me[1] * scaleX;
  68. te[2] = me[2] * scaleX;
  69. te[4] = me[4] * scaleY;
  70. te[5] = me[5] * scaleY;
  71. te[6] = me[6] * scaleY;
  72. te[8] = me[8] * scaleZ;
  73. te[9] = me[9] * scaleZ;
  74. te[10] = me[10] * scaleZ;
  75. return this;
  76. };
  77. }(),
  78. makeRotationFromEuler: function ( euler ) {
  79. if ( typeof euler['order'] === undefined ) {
  80. console.error( 'ERROR: Matrix\'s .makeRotationFromEuler() now expects a Euler rotation rather than a Vector3 and order. Please update your code.' );
  81. }
  82. var te = this.elements;
  83. var x = euler.x, y = euler.y, z = euler.z;
  84. var a = Math.cos( x ), b = Math.sin( x );
  85. var c = Math.cos( y ), d = Math.sin( y );
  86. var e = Math.cos( z ), f = Math.sin( z );
  87. if ( euler.order === undefined || euler.order === 'XYZ' ) {
  88. var ae = a * e, af = a * f, be = b * e, bf = b * f;
  89. te[0] = c * e;
  90. te[4] = - c * f;
  91. te[8] = d;
  92. te[1] = af + be * d;
  93. te[5] = ae - bf * d;
  94. te[9] = - b * c;
  95. te[2] = bf - ae * d;
  96. te[6] = be + af * d;
  97. te[10] = a * c;
  98. } else if ( euler.order === 'YXZ' ) {
  99. var ce = c * e, cf = c * f, de = d * e, df = d * f;
  100. te[0] = ce + df * b;
  101. te[4] = de * b - cf;
  102. te[8] = a * d;
  103. te[1] = a * f;
  104. te[5] = a * e;
  105. te[9] = - b;
  106. te[2] = cf * b - de;
  107. te[6] = df + ce * b;
  108. te[10] = a * c;
  109. } else if ( euler.order === 'ZXY' ) {
  110. var ce = c * e, cf = c * f, de = d * e, df = d * f;
  111. te[0] = ce - df * b;
  112. te[4] = - a * f;
  113. te[8] = de + cf * b;
  114. te[1] = cf + de * b;
  115. te[5] = a * e;
  116. te[9] = df - ce * b;
  117. te[2] = - a * d;
  118. te[6] = b;
  119. te[10] = a * c;
  120. } else if ( euler.order === 'ZYX' ) {
  121. var ae = a * e, af = a * f, be = b * e, bf = b * f;
  122. te[0] = c * e;
  123. te[4] = be * d - af;
  124. te[8] = ae * d + bf;
  125. te[1] = c * f;
  126. te[5] = bf * d + ae;
  127. te[9] = af * d - be;
  128. te[2] = - d;
  129. te[6] = b * c;
  130. te[10] = a * c;
  131. } else if ( euler.order === 'YZX' ) {
  132. var ac = a * c, ad = a * d, bc = b * c, bd = b * d;
  133. te[0] = c * e;
  134. te[4] = bd - ac * f;
  135. te[8] = bc * f + ad;
  136. te[1] = f;
  137. te[5] = a * e;
  138. te[9] = - b * e;
  139. te[2] = - d * e;
  140. te[6] = ad * f + bc;
  141. te[10] = ac - bd * f;
  142. } else if ( euler.order === 'XZY' ) {
  143. var ac = a * c, ad = a * d, bc = b * c, bd = b * d;
  144. te[0] = c * e;
  145. te[4] = - f;
  146. te[8] = d * e;
  147. te[1] = ac * f + bd;
  148. te[5] = a * e;
  149. te[9] = ad * f - bc;
  150. te[2] = bc * f - ad;
  151. te[6] = b * e;
  152. te[10] = bd * f + ac;
  153. }
  154. // last column
  155. te[3] = 0;
  156. te[7] = 0;
  157. te[11] = 0;
  158. // bottom row
  159. te[12] = 0;
  160. te[13] = 0;
  161. te[14] = 0;
  162. te[15] = 1;
  163. return this;
  164. },
  165. setRotationFromQuaternion: function ( q ) {
  166. console.warn( 'DEPRECATED: Matrix4\'s .setRotationFromQuaternion() has been deprecated in favor of makeRotationFromQuaternion. Please update your code.' );
  167. return this.makeRotationFromQuaternion( q );
  168. },
  169. makeRotationFromQuaternion: function ( q ) {
  170. var te = this.elements;
  171. var x = q.x, y = q.y, z = q.z, w = q.w;
  172. var x2 = x + x, y2 = y + y, z2 = z + z;
  173. var xx = x * x2, xy = x * y2, xz = x * z2;
  174. var yy = y * y2, yz = y * z2, zz = z * z2;
  175. var wx = w * x2, wy = w * y2, wz = w * z2;
  176. te[0] = 1 - ( yy + zz );
  177. te[4] = xy - wz;
  178. te[8] = xz + wy;
  179. te[1] = xy + wz;
  180. te[5] = 1 - ( xx + zz );
  181. te[9] = yz - wx;
  182. te[2] = xz - wy;
  183. te[6] = yz + wx;
  184. te[10] = 1 - ( xx + yy );
  185. // last column
  186. te[3] = 0;
  187. te[7] = 0;
  188. te[11] = 0;
  189. // bottom row
  190. te[12] = 0;
  191. te[13] = 0;
  192. te[14] = 0;
  193. te[15] = 1;
  194. return this;
  195. },
  196. lookAt: function() {
  197. var x = new THREE.Vector3();
  198. var y = new THREE.Vector3();
  199. var z = new THREE.Vector3();
  200. return function ( eye, target, up ) {
  201. var te = this.elements;
  202. z.subVectors( eye, target ).normalize();
  203. if ( z.length() === 0 ) {
  204. z.z = 1;
  205. }
  206. x.crossVectors( up, z ).normalize();
  207. if ( x.length() === 0 ) {
  208. z.x += 0.0001;
  209. x.crossVectors( up, z ).normalize();
  210. }
  211. y.crossVectors( z, x );
  212. te[0] = x.x; te[4] = y.x; te[8] = z.x;
  213. te[1] = x.y; te[5] = y.y; te[9] = z.y;
  214. te[2] = x.z; te[6] = y.z; te[10] = z.z;
  215. return this;
  216. };
  217. }(),
  218. multiply: function ( m, n ) {
  219. if ( n !== undefined ) {
  220. console.warn( 'DEPRECATED: Matrix4\'s .multiply() now only accepts one argument. Use .multiplyMatrices( a, b ) instead.' );
  221. return this.multiplyMatrices( m, n );
  222. }
  223. return this.multiplyMatrices( this, m );
  224. },
  225. multiplyMatrices: function ( a, b ) {
  226. var ae = a.elements;
  227. var be = b.elements;
  228. var te = this.elements;
  229. var a11 = ae[0], a12 = ae[4], a13 = ae[8], a14 = ae[12];
  230. var a21 = ae[1], a22 = ae[5], a23 = ae[9], a24 = ae[13];
  231. var a31 = ae[2], a32 = ae[6], a33 = ae[10], a34 = ae[14];
  232. var a41 = ae[3], a42 = ae[7], a43 = ae[11], a44 = ae[15];
  233. var b11 = be[0], b12 = be[4], b13 = be[8], b14 = be[12];
  234. var b21 = be[1], b22 = be[5], b23 = be[9], b24 = be[13];
  235. var b31 = be[2], b32 = be[6], b33 = be[10], b34 = be[14];
  236. var b41 = be[3], b42 = be[7], b43 = be[11], b44 = be[15];
  237. te[0] = a11 * b11 + a12 * b21 + a13 * b31 + a14 * b41;
  238. te[4] = a11 * b12 + a12 * b22 + a13 * b32 + a14 * b42;
  239. te[8] = a11 * b13 + a12 * b23 + a13 * b33 + a14 * b43;
  240. te[12] = a11 * b14 + a12 * b24 + a13 * b34 + a14 * b44;
  241. te[1] = a21 * b11 + a22 * b21 + a23 * b31 + a24 * b41;
  242. te[5] = a21 * b12 + a22 * b22 + a23 * b32 + a24 * b42;
  243. te[9] = a21 * b13 + a22 * b23 + a23 * b33 + a24 * b43;
  244. te[13] = a21 * b14 + a22 * b24 + a23 * b34 + a24 * b44;
  245. te[2] = a31 * b11 + a32 * b21 + a33 * b31 + a34 * b41;
  246. te[6] = a31 * b12 + a32 * b22 + a33 * b32 + a34 * b42;
  247. te[10] = a31 * b13 + a32 * b23 + a33 * b33 + a34 * b43;
  248. te[14] = a31 * b14 + a32 * b24 + a33 * b34 + a34 * b44;
  249. te[3] = a41 * b11 + a42 * b21 + a43 * b31 + a44 * b41;
  250. te[7] = a41 * b12 + a42 * b22 + a43 * b32 + a44 * b42;
  251. te[11] = a41 * b13 + a42 * b23 + a43 * b33 + a44 * b43;
  252. te[15] = a41 * b14 + a42 * b24 + a43 * b34 + a44 * b44;
  253. return this;
  254. },
  255. multiplyToArray: function ( a, b, r ) {
  256. var te = this.elements;
  257. this.multiplyMatrices( a, b );
  258. r[ 0 ] = te[0]; r[ 1 ] = te[1]; r[ 2 ] = te[2]; r[ 3 ] = te[3];
  259. r[ 4 ] = te[4]; r[ 5 ] = te[5]; r[ 6 ] = te[6]; r[ 7 ] = te[7];
  260. r[ 8 ] = te[8]; r[ 9 ] = te[9]; r[ 10 ] = te[10]; r[ 11 ] = te[11];
  261. r[ 12 ] = te[12]; r[ 13 ] = te[13]; r[ 14 ] = te[14]; r[ 15 ] = te[15];
  262. return this;
  263. },
  264. multiplyScalar: function ( s ) {
  265. var te = this.elements;
  266. te[0] *= s; te[4] *= s; te[8] *= s; te[12] *= s;
  267. te[1] *= s; te[5] *= s; te[9] *= s; te[13] *= s;
  268. te[2] *= s; te[6] *= s; te[10] *= s; te[14] *= s;
  269. te[3] *= s; te[7] *= s; te[11] *= s; te[15] *= s;
  270. return this;
  271. },
  272. multiplyVector3: function ( vector ) {
  273. console.warn( 'DEPRECATED: Matrix4\'s .multiplyVector3() has been removed. Use vector.applyMatrix4( matrix ) or vector.applyProjection( matrix ) instead.' );
  274. return vector.applyProjection( this );
  275. },
  276. multiplyVector4: function ( vector ) {
  277. console.warn( 'DEPRECATED: Matrix4\'s .multiplyVector4() has been removed. Use vector.applyMatrix4( matrix ) instead.' );
  278. return vector.applyMatrix4( this );
  279. },
  280. multiplyVector3Array: function() {
  281. var v1 = new THREE.Vector3();
  282. return function ( a ) {
  283. for ( var i = 0, il = a.length; i < il; i += 3 ) {
  284. v1.x = a[ i ];
  285. v1.y = a[ i + 1 ];
  286. v1.z = a[ i + 2 ];
  287. v1.applyProjection( this );
  288. a[ i ] = v1.x;
  289. a[ i + 1 ] = v1.y;
  290. a[ i + 2 ] = v1.z;
  291. }
  292. return a;
  293. };
  294. }(),
  295. rotateAxis: function ( v ) {
  296. console.warn( 'DEPRECATED: Matrix4\'s .rotateAxis() has been removed. Use Vector3.transformDirection( matrix ) instead.' );
  297. v.transformDirection( this );
  298. },
  299. crossVector: function ( vector ) {
  300. console.warn( 'DEPRECATED: Matrix4\'s .crossVector() has been removed. Use vector.applyMatrix4( matrix ) instead.' );
  301. return vector.applyMatrix4( this );
  302. },
  303. determinant: function () {
  304. var te = this.elements;
  305. var n11 = te[0], n12 = te[4], n13 = te[8], n14 = te[12];
  306. var n21 = te[1], n22 = te[5], n23 = te[9], n24 = te[13];
  307. var n31 = te[2], n32 = te[6], n33 = te[10], n34 = te[14];
  308. var n41 = te[3], n42 = te[7], n43 = te[11], n44 = te[15];
  309. //TODO: make this more efficient
  310. //( based on http://www.euclideanspace.com/maths/algebra/matrix/functions/inverse/fourD/index.htm )
  311. return (
  312. n41 * (
  313. +n14 * n23 * n32
  314. -n13 * n24 * n32
  315. -n14 * n22 * n33
  316. +n12 * n24 * n33
  317. +n13 * n22 * n34
  318. -n12 * n23 * n34
  319. ) +
  320. n42 * (
  321. +n11 * n23 * n34
  322. -n11 * n24 * n33
  323. +n14 * n21 * n33
  324. -n13 * n21 * n34
  325. +n13 * n24 * n31
  326. -n14 * n23 * n31
  327. ) +
  328. n43 * (
  329. +n11 * n24 * n32
  330. -n11 * n22 * n34
  331. -n14 * n21 * n32
  332. +n12 * n21 * n34
  333. +n14 * n22 * n31
  334. -n12 * n24 * n31
  335. ) +
  336. n44 * (
  337. -n13 * n22 * n31
  338. -n11 * n23 * n32
  339. +n11 * n22 * n33
  340. +n13 * n21 * n32
  341. -n12 * n21 * n33
  342. +n12 * n23 * n31
  343. )
  344. );
  345. },
  346. transpose: function () {
  347. var te = this.elements;
  348. var tmp;
  349. tmp = te[1]; te[1] = te[4]; te[4] = tmp;
  350. tmp = te[2]; te[2] = te[8]; te[8] = tmp;
  351. tmp = te[6]; te[6] = te[9]; te[9] = tmp;
  352. tmp = te[3]; te[3] = te[12]; te[12] = tmp;
  353. tmp = te[7]; te[7] = te[13]; te[13] = tmp;
  354. tmp = te[11]; te[11] = te[14]; te[14] = tmp;
  355. return this;
  356. },
  357. flattenToArray: function ( flat ) {
  358. var te = this.elements;
  359. flat[ 0 ] = te[0]; flat[ 1 ] = te[1]; flat[ 2 ] = te[2]; flat[ 3 ] = te[3];
  360. flat[ 4 ] = te[4]; flat[ 5 ] = te[5]; flat[ 6 ] = te[6]; flat[ 7 ] = te[7];
  361. flat[ 8 ] = te[8]; flat[ 9 ] = te[9]; flat[ 10 ] = te[10]; flat[ 11 ] = te[11];
  362. flat[ 12 ] = te[12]; flat[ 13 ] = te[13]; flat[ 14 ] = te[14]; flat[ 15 ] = te[15];
  363. return flat;
  364. },
  365. flattenToArrayOffset: function( flat, offset ) {
  366. var te = this.elements;
  367. flat[ offset ] = te[0];
  368. flat[ offset + 1 ] = te[1];
  369. flat[ offset + 2 ] = te[2];
  370. flat[ offset + 3 ] = te[3];
  371. flat[ offset + 4 ] = te[4];
  372. flat[ offset + 5 ] = te[5];
  373. flat[ offset + 6 ] = te[6];
  374. flat[ offset + 7 ] = te[7];
  375. flat[ offset + 8 ] = te[8];
  376. flat[ offset + 9 ] = te[9];
  377. flat[ offset + 10 ] = te[10];
  378. flat[ offset + 11 ] = te[11];
  379. flat[ offset + 12 ] = te[12];
  380. flat[ offset + 13 ] = te[13];
  381. flat[ offset + 14 ] = te[14];
  382. flat[ offset + 15 ] = te[15];
  383. return flat;
  384. },
  385. getPosition: function() {
  386. var v1 = new THREE.Vector3();
  387. return function () {
  388. console.warn( 'DEPRECATED: Matrix4\'s .getPosition() has been removed. Use Vector3.getPositionFromMatrix( matrix ) instead.' );
  389. var te = this.elements;
  390. return v1.set( te[12], te[13], te[14] );
  391. };
  392. }(),
  393. setPosition: function ( v ) {
  394. var te = this.elements;
  395. te[12] = v.x;
  396. te[13] = v.y;
  397. te[14] = v.z;
  398. return this;
  399. },
  400. getInverse: function ( m, throwOnInvertible ) {
  401. // based on http://www.euclideanspace.com/maths/algebra/matrix/functions/inverse/fourD/index.htm
  402. var te = this.elements;
  403. var me = m.elements;
  404. var n11 = me[0], n12 = me[4], n13 = me[8], n14 = me[12];
  405. var n21 = me[1], n22 = me[5], n23 = me[9], n24 = me[13];
  406. var n31 = me[2], n32 = me[6], n33 = me[10], n34 = me[14];
  407. var n41 = me[3], n42 = me[7], n43 = me[11], n44 = me[15];
  408. te[0] = n23*n34*n42 - n24*n33*n42 + n24*n32*n43 - n22*n34*n43 - n23*n32*n44 + n22*n33*n44;
  409. te[4] = n14*n33*n42 - n13*n34*n42 - n14*n32*n43 + n12*n34*n43 + n13*n32*n44 - n12*n33*n44;
  410. te[8] = n13*n24*n42 - n14*n23*n42 + n14*n22*n43 - n12*n24*n43 - n13*n22*n44 + n12*n23*n44;
  411. te[12] = n14*n23*n32 - n13*n24*n32 - n14*n22*n33 + n12*n24*n33 + n13*n22*n34 - n12*n23*n34;
  412. te[1] = n24*n33*n41 - n23*n34*n41 - n24*n31*n43 + n21*n34*n43 + n23*n31*n44 - n21*n33*n44;
  413. te[5] = n13*n34*n41 - n14*n33*n41 + n14*n31*n43 - n11*n34*n43 - n13*n31*n44 + n11*n33*n44;
  414. te[9] = n14*n23*n41 - n13*n24*n41 - n14*n21*n43 + n11*n24*n43 + n13*n21*n44 - n11*n23*n44;
  415. te[13] = n13*n24*n31 - n14*n23*n31 + n14*n21*n33 - n11*n24*n33 - n13*n21*n34 + n11*n23*n34;
  416. te[2] = n22*n34*n41 - n24*n32*n41 + n24*n31*n42 - n21*n34*n42 - n22*n31*n44 + n21*n32*n44;
  417. te[6] = n14*n32*n41 - n12*n34*n41 - n14*n31*n42 + n11*n34*n42 + n12*n31*n44 - n11*n32*n44;
  418. te[10] = n12*n24*n41 - n14*n22*n41 + n14*n21*n42 - n11*n24*n42 - n12*n21*n44 + n11*n22*n44;
  419. te[14] = n14*n22*n31 - n12*n24*n31 - n14*n21*n32 + n11*n24*n32 + n12*n21*n34 - n11*n22*n34;
  420. te[3] = n23*n32*n41 - n22*n33*n41 - n23*n31*n42 + n21*n33*n42 + n22*n31*n43 - n21*n32*n43;
  421. te[7] = n12*n33*n41 - n13*n32*n41 + n13*n31*n42 - n11*n33*n42 - n12*n31*n43 + n11*n32*n43;
  422. te[11] = n13*n22*n41 - n12*n23*n41 - n13*n21*n42 + n11*n23*n42 + n12*n21*n43 - n11*n22*n43;
  423. te[15] = n12*n23*n31 - n13*n22*n31 + n13*n21*n32 - n11*n23*n32 - n12*n21*n33 + n11*n22*n33;
  424. var det = n11 * te[ 0 ] + n21 * te[ 4 ] + n31 * te[ 8 ] + n41 * te[ 12 ];
  425. if ( det == 0 ) {
  426. var msg = "Matrix4.getInverse(): can't invert matrix, determinant is 0";
  427. if ( throwOnInvertible || false ) {
  428. throw new Error( msg );
  429. } else {
  430. console.warn( msg );
  431. }
  432. this.identity();
  433. return this;
  434. }
  435. this.multiplyScalar( 1 / det );
  436. return this;
  437. },
  438. translate: function ( v ) {
  439. console.warn( 'DEPRECATED: Matrix4\'s .translate() has been removed.');
  440. },
  441. rotateX: function ( angle ) {
  442. console.warn( 'DEPRECATED: Matrix4\'s .rotateX() has been removed.');
  443. },
  444. rotateY: function ( angle ) {
  445. console.warn( 'DEPRECATED: Matrix4\'s .rotateY() has been removed.');
  446. },
  447. rotateZ: function ( angle ) {
  448. console.warn( 'DEPRECATED: Matrix4\'s .rotateZ() has been removed.');
  449. },
  450. rotateByAxis: function ( axis, angle ) {
  451. console.warn( 'DEPRECATED: Matrix4\'s .rotateByAxis() has been removed.');
  452. },
  453. scale: function ( v ) {
  454. var te = this.elements;
  455. var x = v.x, y = v.y, z = v.z;
  456. te[0] *= x; te[4] *= y; te[8] *= z;
  457. te[1] *= x; te[5] *= y; te[9] *= z;
  458. te[2] *= x; te[6] *= y; te[10] *= z;
  459. te[3] *= x; te[7] *= y; te[11] *= z;
  460. return this;
  461. },
  462. getMaxScaleOnAxis: function () {
  463. var te = this.elements;
  464. var scaleXSq = te[0] * te[0] + te[1] * te[1] + te[2] * te[2];
  465. var scaleYSq = te[4] * te[4] + te[5] * te[5] + te[6] * te[6];
  466. var scaleZSq = te[8] * te[8] + te[9] * te[9] + te[10] * te[10];
  467. return Math.sqrt( Math.max( scaleXSq, Math.max( scaleYSq, scaleZSq ) ) );
  468. },
  469. makeTranslation: function ( x, y, z ) {
  470. this.set(
  471. 1, 0, 0, x,
  472. 0, 1, 0, y,
  473. 0, 0, 1, z,
  474. 0, 0, 0, 1
  475. );
  476. return this;
  477. },
  478. makeRotationX: function ( theta ) {
  479. var c = Math.cos( theta ), s = Math.sin( theta );
  480. this.set(
  481. 1, 0, 0, 0,
  482. 0, c, -s, 0,
  483. 0, s, c, 0,
  484. 0, 0, 0, 1
  485. );
  486. return this;
  487. },
  488. makeRotationY: function ( theta ) {
  489. var c = Math.cos( theta ), s = Math.sin( theta );
  490. this.set(
  491. c, 0, s, 0,
  492. 0, 1, 0, 0,
  493. -s, 0, c, 0,
  494. 0, 0, 0, 1
  495. );
  496. return this;
  497. },
  498. makeRotationZ: function ( theta ) {
  499. var c = Math.cos( theta ), s = Math.sin( theta );
  500. this.set(
  501. c, -s, 0, 0,
  502. s, c, 0, 0,
  503. 0, 0, 1, 0,
  504. 0, 0, 0, 1
  505. );
  506. return this;
  507. },
  508. makeRotationAxis: function ( axis, angle ) {
  509. // Based on http://www.gamedev.net/reference/articles/article1199.asp
  510. var c = Math.cos( angle );
  511. var s = Math.sin( angle );
  512. var t = 1 - c;
  513. var x = axis.x, y = axis.y, z = axis.z;
  514. var tx = t * x, ty = t * y;
  515. this.set(
  516. tx * x + c, tx * y - s * z, tx * z + s * y, 0,
  517. tx * y + s * z, ty * y + c, ty * z - s * x, 0,
  518. tx * z - s * y, ty * z + s * x, t * z * z + c, 0,
  519. 0, 0, 0, 1
  520. );
  521. return this;
  522. },
  523. makeScale: function ( x, y, z ) {
  524. this.set(
  525. x, 0, 0, 0,
  526. 0, y, 0, 0,
  527. 0, 0, z, 0,
  528. 0, 0, 0, 1
  529. );
  530. return this;
  531. },
  532. compose: function ( position, quaternion, scale ) {
  533. this.makeRotationFromQuaternion( quaternion );
  534. this.scale( scale );
  535. this.setPosition( position );
  536. return this;
  537. },
  538. makeFrustum: function ( left, right, bottom, top, near, far ) {
  539. var te = this.elements;
  540. var x = 2 * near / ( right - left );
  541. var y = 2 * near / ( top - bottom );
  542. var a = ( right + left ) / ( right - left );
  543. var b = ( top + bottom ) / ( top - bottom );
  544. var c = - ( far + near ) / ( far - near );
  545. var d = - 2 * far * near / ( far - near );
  546. te[0] = x; te[4] = 0; te[8] = a; te[12] = 0;
  547. te[1] = 0; te[5] = y; te[9] = b; te[13] = 0;
  548. te[2] = 0; te[6] = 0; te[10] = c; te[14] = d;
  549. te[3] = 0; te[7] = 0; te[11] = - 1; te[15] = 0;
  550. return this;
  551. },
  552. makePerspective: function ( fov, aspect, near, far ) {
  553. var ymax = near * Math.tan( THREE.Math.degToRad( fov * 0.5 ) );
  554. var ymin = - ymax;
  555. var xmin = ymin * aspect;
  556. var xmax = ymax * aspect;
  557. return this.makeFrustum( xmin, xmax, ymin, ymax, near, far );
  558. },
  559. makeOrthographic: function ( left, right, top, bottom, near, far ) {
  560. var te = this.elements;
  561. var w = right - left;
  562. var h = top - bottom;
  563. var p = far - near;
  564. var x = ( right + left ) / w;
  565. var y = ( top + bottom ) / h;
  566. var z = ( far + near ) / p;
  567. te[0] = 2 / w; te[4] = 0; te[8] = 0; te[12] = -x;
  568. te[1] = 0; te[5] = 2 / h; te[9] = 0; te[13] = -y;
  569. te[2] = 0; te[6] = 0; te[10] = -2/p; te[14] = -z;
  570. te[3] = 0; te[7] = 0; te[11] = 0; te[15] = 1;
  571. return this;
  572. },
  573. fromArray: function ( array ) {
  574. this.elements.set( array );
  575. return this;
  576. },
  577. toArray: function () {
  578. var te = this.elements;
  579. return [
  580. te[ 0 ], te[ 1 ], te[ 2 ], te[ 3 ],
  581. te[ 4 ], te[ 5 ], te[ 6 ], te[ 7 ],
  582. te[ 8 ], te[ 9 ], te[ 10 ], te[ 11 ],
  583. te[ 12 ], te[ 13 ], te[ 14 ], te[ 15 ]
  584. ];
  585. },
  586. clone: function () {
  587. var te = this.elements;
  588. return new THREE.Matrix4(
  589. te[0], te[4], te[8], te[12],
  590. te[1], te[5], te[9], te[13],
  591. te[2], te[6], te[10], te[14],
  592. te[3], te[7], te[11], te[15]
  593. );
  594. }
  595. };
  596. THREE.extend( THREE.Matrix4.prototype, {
  597. decompose: function () {
  598. var x = new THREE.Vector3();
  599. var y = new THREE.Vector3();
  600. var z = new THREE.Vector3();
  601. var matrix = new THREE.Matrix4();
  602. return function ( position, quaternion, scale ) {
  603. var te = this.elements;
  604. // grab the axis vectors
  605. x.set( te[0], te[1], te[2] );
  606. y.set( te[4], te[5], te[6] );
  607. z.set( te[8], te[9], te[10] );
  608. position = ( position instanceof THREE.Vector3 ) ? position : new THREE.Vector3();
  609. quaternion = ( quaternion instanceof THREE.Quaternion ) ? quaternion : new THREE.Quaternion();
  610. scale = ( scale instanceof THREE.Vector3 ) ? scale : new THREE.Vector3();
  611. scale.x = x.length();
  612. scale.y = y.length();
  613. scale.z = z.length();
  614. position.x = te[12];
  615. position.y = te[13];
  616. position.z = te[14];
  617. // scale the rotation part
  618. matrix.copy( this );
  619. matrix.elements[0] /= scale.x;
  620. matrix.elements[1] /= scale.x;
  621. matrix.elements[2] /= scale.x;
  622. matrix.elements[4] /= scale.y;
  623. matrix.elements[5] /= scale.y;
  624. matrix.elements[6] /= scale.y;
  625. matrix.elements[8] /= scale.z;
  626. matrix.elements[9] /= scale.z;
  627. matrix.elements[10] /= scale.z;
  628. quaternion.setFromRotationMatrix( matrix );
  629. return [ position, quaternion, scale ];
  630. };
  631. }()
  632. } );