Matrix4.js 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970
  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( 'THREEMatrix4: .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 ( euler instanceof THREE.Euler === false ) {
  80. console.error( 'THREE.Matrix: .makeRotationFromEuler() now expects a Euler rotation rather than a Vector3 and order.' );
  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 === '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( 'THREE.Matrix4: .setRotationFromQuaternion() has been renamed to .makeRotationFromQuaternion().' );
  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( 'THREE.Matrix4: .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( 'THREE.Matrix4: .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( 'THREE.Matrix4: .multiplyVector4() has been removed. Use vector.applyMatrix4( matrix ) instead.' );
  278. return vector.applyMatrix4( this );
  279. },
  280. multiplyVector3Array: function ( a ) {
  281. console.warn( 'THREE.Matrix4: .multiplyVector3Array() has been renamed. Use matrix.applyToVector3Array( array ) instead.' );
  282. return this.applyToVector3Array( a );
  283. },
  284. applyToVector3Array: function () {
  285. var v1 = new THREE.Vector3();
  286. return function ( array, offset, length ) {
  287. if ( offset === undefined ) offset = 0;
  288. if ( length === undefined ) length = array.length;
  289. for ( var i = 0, j = offset, il; i < length; i += 3, j += 3 ) {
  290. v1.x = array[ j ];
  291. v1.y = array[ j + 1 ];
  292. v1.z = array[ j + 2 ];
  293. v1.applyMatrix4( this );
  294. array[ j ] = v1.x;
  295. array[ j + 1 ] = v1.y;
  296. array[ j + 2 ] = v1.z;
  297. }
  298. return array;
  299. };
  300. }(),
  301. rotateAxis: function ( v ) {
  302. console.warn( 'THREE.Matrix4: .rotateAxis() has been removed. Use Vector3.transformDirection( matrix ) instead.' );
  303. v.transformDirection( this );
  304. },
  305. crossVector: function ( vector ) {
  306. console.warn( 'THREE.Matrix4: .crossVector() has been removed. Use vector.applyMatrix4( matrix ) instead.' );
  307. return vector.applyMatrix4( this );
  308. },
  309. determinant: function () {
  310. var te = this.elements;
  311. var n11 = te[ 0 ], n12 = te[ 4 ], n13 = te[ 8 ], n14 = te[ 12 ];
  312. var n21 = te[ 1 ], n22 = te[ 5 ], n23 = te[ 9 ], n24 = te[ 13 ];
  313. var n31 = te[ 2 ], n32 = te[ 6 ], n33 = te[ 10 ], n34 = te[ 14 ];
  314. var n41 = te[ 3 ], n42 = te[ 7 ], n43 = te[ 11 ], n44 = te[ 15 ];
  315. //TODO: make this more efficient
  316. //( based on http://www.euclideanspace.com/maths/algebra/matrix/functions/inverse/fourD/index.htm )
  317. return (
  318. n41 * (
  319. + n14 * n23 * n32
  320. - n13 * n24 * n32
  321. - n14 * n22 * n33
  322. + n12 * n24 * n33
  323. + n13 * n22 * n34
  324. - n12 * n23 * n34
  325. ) +
  326. n42 * (
  327. + n11 * n23 * n34
  328. - n11 * n24 * n33
  329. + n14 * n21 * n33
  330. - n13 * n21 * n34
  331. + n13 * n24 * n31
  332. - n14 * n23 * n31
  333. ) +
  334. n43 * (
  335. + n11 * n24 * n32
  336. - n11 * n22 * n34
  337. - n14 * n21 * n32
  338. + n12 * n21 * n34
  339. + n14 * n22 * n31
  340. - n12 * n24 * n31
  341. ) +
  342. n44 * (
  343. - n13 * n22 * n31
  344. - n11 * n23 * n32
  345. + n11 * n22 * n33
  346. + n13 * n21 * n32
  347. - n12 * n21 * n33
  348. + n12 * n23 * n31
  349. )
  350. );
  351. },
  352. transpose: function () {
  353. var te = this.elements;
  354. var tmp;
  355. tmp = te[ 1 ]; te[ 1 ] = te[ 4 ]; te[ 4 ] = tmp;
  356. tmp = te[ 2 ]; te[ 2 ] = te[ 8 ]; te[ 8 ] = tmp;
  357. tmp = te[ 6 ]; te[ 6 ] = te[ 9 ]; te[ 9 ] = tmp;
  358. tmp = te[ 3 ]; te[ 3 ] = te[ 12 ]; te[ 12 ] = tmp;
  359. tmp = te[ 7 ]; te[ 7 ] = te[ 13 ]; te[ 13 ] = tmp;
  360. tmp = te[ 11 ]; te[ 11 ] = te[ 14 ]; te[ 14 ] = tmp;
  361. return this;
  362. },
  363. flattenToArrayOffset: function ( array, offset ) {
  364. var te = this.elements;
  365. array[ offset ] = te[ 0 ];
  366. array[ offset + 1 ] = te[ 1 ];
  367. array[ offset + 2 ] = te[ 2 ];
  368. array[ offset + 3 ] = te[ 3 ];
  369. array[ offset + 4 ] = te[ 4 ];
  370. array[ offset + 5 ] = te[ 5 ];
  371. array[ offset + 6 ] = te[ 6 ];
  372. array[ offset + 7 ] = te[ 7 ];
  373. array[ offset + 8 ] = te[ 8 ];
  374. array[ offset + 9 ] = te[ 9 ];
  375. array[ offset + 10 ] = te[ 10 ];
  376. array[ offset + 11 ] = te[ 11 ];
  377. array[ offset + 12 ] = te[ 12 ];
  378. array[ offset + 13 ] = te[ 13 ];
  379. array[ offset + 14 ] = te[ 14 ];
  380. array[ offset + 15 ] = te[ 15 ];
  381. return array;
  382. },
  383. getPosition: function () {
  384. var v1 = new THREE.Vector3();
  385. return function () {
  386. console.warn( 'THREE.Matrix4: .getPosition() has been removed. Use Vector3.setFromMatrixPosition( matrix ) instead.' );
  387. var te = this.elements;
  388. return v1.set( te[ 12 ], te[ 13 ], te[ 14 ] );
  389. };
  390. }(),
  391. setPosition: function ( v ) {
  392. var te = this.elements;
  393. te[ 12 ] = v.x;
  394. te[ 13 ] = v.y;
  395. te[ 14 ] = v.z;
  396. return this;
  397. },
  398. getInverse: function ( m, throwOnInvertible ) {
  399. // based on http://www.euclideanspace.com/maths/algebra/matrix/functions/inverse/fourD/index.htm
  400. var te = this.elements;
  401. var me = m.elements;
  402. var n11 = me[ 0 ], n12 = me[ 4 ], n13 = me[ 8 ], n14 = me[ 12 ];
  403. var n21 = me[ 1 ], n22 = me[ 5 ], n23 = me[ 9 ], n24 = me[ 13 ];
  404. var n31 = me[ 2 ], n32 = me[ 6 ], n33 = me[ 10 ], n34 = me[ 14 ];
  405. var n41 = me[ 3 ], n42 = me[ 7 ], n43 = me[ 11 ], n44 = me[ 15 ];
  406. te[ 0 ] = n23 * n34 * n42 - n24 * n33 * n42 + n24 * n32 * n43 - n22 * n34 * n43 - n23 * n32 * n44 + n22 * n33 * n44;
  407. te[ 4 ] = n14 * n33 * n42 - n13 * n34 * n42 - n14 * n32 * n43 + n12 * n34 * n43 + n13 * n32 * n44 - n12 * n33 * n44;
  408. te[ 8 ] = n13 * n24 * n42 - n14 * n23 * n42 + n14 * n22 * n43 - n12 * n24 * n43 - n13 * n22 * n44 + n12 * n23 * n44;
  409. te[ 12 ] = n14 * n23 * n32 - n13 * n24 * n32 - n14 * n22 * n33 + n12 * n24 * n33 + n13 * n22 * n34 - n12 * n23 * n34;
  410. te[ 1 ] = n24 * n33 * n41 - n23 * n34 * n41 - n24 * n31 * n43 + n21 * n34 * n43 + n23 * n31 * n44 - n21 * n33 * n44;
  411. te[ 5 ] = n13 * n34 * n41 - n14 * n33 * n41 + n14 * n31 * n43 - n11 * n34 * n43 - n13 * n31 * n44 + n11 * n33 * n44;
  412. te[ 9 ] = n14 * n23 * n41 - n13 * n24 * n41 - n14 * n21 * n43 + n11 * n24 * n43 + n13 * n21 * n44 - n11 * n23 * n44;
  413. te[ 13 ] = n13 * n24 * n31 - n14 * n23 * n31 + n14 * n21 * n33 - n11 * n24 * n33 - n13 * n21 * n34 + n11 * n23 * n34;
  414. te[ 2 ] = n22 * n34 * n41 - n24 * n32 * n41 + n24 * n31 * n42 - n21 * n34 * n42 - n22 * n31 * n44 + n21 * n32 * n44;
  415. te[ 6 ] = n14 * n32 * n41 - n12 * n34 * n41 - n14 * n31 * n42 + n11 * n34 * n42 + n12 * n31 * n44 - n11 * n32 * n44;
  416. te[ 10 ] = n12 * n24 * n41 - n14 * n22 * n41 + n14 * n21 * n42 - n11 * n24 * n42 - n12 * n21 * n44 + n11 * n22 * n44;
  417. te[ 14 ] = n14 * n22 * n31 - n12 * n24 * n31 - n14 * n21 * n32 + n11 * n24 * n32 + n12 * n21 * n34 - n11 * n22 * n34;
  418. te[ 3 ] = n23 * n32 * n41 - n22 * n33 * n41 - n23 * n31 * n42 + n21 * n33 * n42 + n22 * n31 * n43 - n21 * n32 * n43;
  419. te[ 7 ] = n12 * n33 * n41 - n13 * n32 * n41 + n13 * n31 * n42 - n11 * n33 * n42 - n12 * n31 * n43 + n11 * n32 * n43;
  420. te[ 11 ] = n13 * n22 * n41 - n12 * n23 * n41 - n13 * n21 * n42 + n11 * n23 * n42 + n12 * n21 * n43 - n11 * n22 * n43;
  421. te[ 15 ] = n12 * n23 * n31 - n13 * n22 * n31 + n13 * n21 * n32 - n11 * n23 * n32 - n12 * n21 * n33 + n11 * n22 * n33;
  422. var det = n11 * te[ 0 ] + n21 * te[ 4 ] + n31 * te[ 8 ] + n41 * te[ 12 ];
  423. if ( det == 0 ) {
  424. var msg = "Matrix4.getInverse(): can't invert matrix, determinant is 0";
  425. if ( throwOnInvertible || false ) {
  426. throw new Error( msg );
  427. } else {
  428. console.warn( msg );
  429. }
  430. this.identity();
  431. return this;
  432. }
  433. this.multiplyScalar( 1 / det );
  434. return this;
  435. },
  436. translate: function ( v ) {
  437. console.warn( 'THREE.Matrix4: .translate() has been removed.' );
  438. },
  439. rotateX: function ( angle ) {
  440. console.warn( 'THREE.Matrix4: .rotateX() has been removed.' );
  441. },
  442. rotateY: function ( angle ) {
  443. console.warn( 'THREE.Matrix4: .rotateY() has been removed.' );
  444. },
  445. rotateZ: function ( angle ) {
  446. console.warn( 'THREE.Matrix4: .rotateZ() has been removed.' );
  447. },
  448. rotateByAxis: function ( axis, angle ) {
  449. console.warn( 'THREE.Matrix4: .rotateByAxis() has been removed.' );
  450. },
  451. scale: function ( v ) {
  452. var te = this.elements;
  453. var x = v.x, y = v.y, z = v.z;
  454. te[ 0 ] *= x; te[ 4 ] *= y; te[ 8 ] *= z;
  455. te[ 1 ] *= x; te[ 5 ] *= y; te[ 9 ] *= z;
  456. te[ 2 ] *= x; te[ 6 ] *= y; te[ 10 ] *= z;
  457. te[ 3 ] *= x; te[ 7 ] *= y; te[ 11 ] *= z;
  458. return this;
  459. },
  460. getMaxScaleOnAxis: function () {
  461. var te = this.elements;
  462. var scaleXSq = te[ 0 ] * te[ 0 ] + te[ 1 ] * te[ 1 ] + te[ 2 ] * te[ 2 ];
  463. var scaleYSq = te[ 4 ] * te[ 4 ] + te[ 5 ] * te[ 5 ] + te[ 6 ] * te[ 6 ];
  464. var scaleZSq = te[ 8 ] * te[ 8 ] + te[ 9 ] * te[ 9 ] + te[ 10 ] * te[ 10 ];
  465. return Math.sqrt( Math.max( scaleXSq, Math.max( scaleYSq, scaleZSq ) ) );
  466. },
  467. makeTranslation: function ( x, y, z ) {
  468. this.set(
  469. 1, 0, 0, x,
  470. 0, 1, 0, y,
  471. 0, 0, 1, z,
  472. 0, 0, 0, 1
  473. );
  474. return this;
  475. },
  476. makeRotationX: function ( theta ) {
  477. var c = Math.cos( theta ), s = Math.sin( theta );
  478. this.set(
  479. 1, 0, 0, 0,
  480. 0, c, - s, 0,
  481. 0, s, c, 0,
  482. 0, 0, 0, 1
  483. );
  484. return this;
  485. },
  486. makeRotationY: function ( theta ) {
  487. var c = Math.cos( theta ), s = Math.sin( theta );
  488. this.set(
  489. c, 0, s, 0,
  490. 0, 1, 0, 0,
  491. - s, 0, c, 0,
  492. 0, 0, 0, 1
  493. );
  494. return this;
  495. },
  496. makeRotationZ: function ( theta ) {
  497. var c = Math.cos( theta ), s = Math.sin( theta );
  498. this.set(
  499. c, - s, 0, 0,
  500. s, c, 0, 0,
  501. 0, 0, 1, 0,
  502. 0, 0, 0, 1
  503. );
  504. return this;
  505. },
  506. makeRotationAxis: function ( axis, angle ) {
  507. // Based on http://www.gamedev.net/reference/articles/article1199.asp
  508. var c = Math.cos( angle );
  509. var s = Math.sin( angle );
  510. var t = 1 - c;
  511. var x = axis.x, y = axis.y, z = axis.z;
  512. var tx = t * x, ty = t * y;
  513. this.set(
  514. tx * x + c, tx * y - s * z, tx * z + s * y, 0,
  515. tx * y + s * z, ty * y + c, ty * z - s * x, 0,
  516. tx * z - s * y, ty * z + s * x, t * z * z + c, 0,
  517. 0, 0, 0, 1
  518. );
  519. return this;
  520. },
  521. makeScale: function ( x, y, z ) {
  522. this.set(
  523. x, 0, 0, 0,
  524. 0, y, 0, 0,
  525. 0, 0, z, 0,
  526. 0, 0, 0, 1
  527. );
  528. return this;
  529. },
  530. compose: function ( position, quaternion, scale ) {
  531. this.makeRotationFromQuaternion( quaternion );
  532. this.scale( scale );
  533. this.setPosition( position );
  534. return this;
  535. },
  536. decompose: function () {
  537. var vector = new THREE.Vector3();
  538. var matrix = new THREE.Matrix4();
  539. return function ( position, quaternion, scale ) {
  540. var te = this.elements;
  541. var sx = vector.set( te[ 0 ], te[ 1 ], te[ 2 ] ).length();
  542. var sy = vector.set( te[ 4 ], te[ 5 ], te[ 6 ] ).length();
  543. var sz = vector.set( te[ 8 ], te[ 9 ], te[ 10 ] ).length();
  544. // if determine is negative, we need to invert one scale
  545. var det = this.determinant();
  546. if ( det < 0 ) {
  547. sx = - sx;
  548. }
  549. position.x = te[ 12 ];
  550. position.y = te[ 13 ];
  551. position.z = te[ 14 ];
  552. // scale the rotation part
  553. matrix.elements.set( this.elements ); // at this point matrix is incomplete so we can't use .copy()
  554. var invSX = 1 / sx;
  555. var invSY = 1 / sy;
  556. var invSZ = 1 / sz;
  557. matrix.elements[ 0 ] *= invSX;
  558. matrix.elements[ 1 ] *= invSX;
  559. matrix.elements[ 2 ] *= invSX;
  560. matrix.elements[ 4 ] *= invSY;
  561. matrix.elements[ 5 ] *= invSY;
  562. matrix.elements[ 6 ] *= invSY;
  563. matrix.elements[ 8 ] *= invSZ;
  564. matrix.elements[ 9 ] *= invSZ;
  565. matrix.elements[ 10 ] *= invSZ;
  566. quaternion.setFromRotationMatrix( matrix );
  567. scale.x = sx;
  568. scale.y = sy;
  569. scale.z = sz;
  570. return this;
  571. };
  572. }(),
  573. makeFrustum: function ( left, right, bottom, top, near, far ) {
  574. var te = this.elements;
  575. var x = 2 * near / ( right - left );
  576. var y = 2 * near / ( top - bottom );
  577. var a = ( right + left ) / ( right - left );
  578. var b = ( top + bottom ) / ( top - bottom );
  579. var c = - ( far + near ) / ( far - near );
  580. var d = - 2 * far * near / ( far - near );
  581. te[ 0 ] = x; te[ 4 ] = 0; te[ 8 ] = a; te[ 12 ] = 0;
  582. te[ 1 ] = 0; te[ 5 ] = y; te[ 9 ] = b; te[ 13 ] = 0;
  583. te[ 2 ] = 0; te[ 6 ] = 0; te[ 10 ] = c; te[ 14 ] = d;
  584. te[ 3 ] = 0; te[ 7 ] = 0; te[ 11 ] = - 1; te[ 15 ] = 0;
  585. return this;
  586. },
  587. makePerspective: function ( fov, aspect, near, far ) {
  588. var ymax = near * Math.tan( THREE.Math.degToRad( fov * 0.5 ) );
  589. var ymin = - ymax;
  590. var xmin = ymin * aspect;
  591. var xmax = ymax * aspect;
  592. return this.makeFrustum( xmin, xmax, ymin, ymax, near, far );
  593. },
  594. makeOrthographic: function ( left, right, top, bottom, near, far ) {
  595. var te = this.elements;
  596. var w = right - left;
  597. var h = top - bottom;
  598. var p = far - near;
  599. var x = ( right + left ) / w;
  600. var y = ( top + bottom ) / h;
  601. var z = ( far + near ) / p;
  602. te[ 0 ] = 2 / w; te[ 4 ] = 0; te[ 8 ] = 0; te[ 12 ] = - x;
  603. te[ 1 ] = 0; te[ 5 ] = 2 / h; te[ 9 ] = 0; te[ 13 ] = - y;
  604. te[ 2 ] = 0; te[ 6 ] = 0; te[ 10 ] = - 2 / p; te[ 14 ] = - z;
  605. te[ 3 ] = 0; te[ 7 ] = 0; te[ 11 ] = 0; te[ 15 ] = 1;
  606. return this;
  607. },
  608. fromArray: function ( array ) {
  609. this.elements.set( array );
  610. return this;
  611. },
  612. toArray: function () {
  613. var te = this.elements;
  614. return [
  615. te[ 0 ], te[ 1 ], te[ 2 ], te[ 3 ],
  616. te[ 4 ], te[ 5 ], te[ 6 ], te[ 7 ],
  617. te[ 8 ], te[ 9 ], te[ 10 ], te[ 11 ],
  618. te[ 12 ], te[ 13 ], te[ 14 ], te[ 15 ]
  619. ];
  620. },
  621. clone: function () {
  622. var te = this.elements;
  623. return new THREE.Matrix4(
  624. te[ 0 ], te[ 4 ], te[ 8 ], te[ 12 ],
  625. te[ 1 ], te[ 5 ], te[ 9 ], te[ 13 ],
  626. te[ 2 ], te[ 6 ], te[ 10 ], te[ 14 ],
  627. te[ 3 ], te[ 7 ], te[ 11 ], te[ 15 ]
  628. );
  629. }
  630. };