Matrix4.js 19 KB

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