Matrix4.js 19 KB

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