Matrix4.js 20 KB

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