Matrix4.js 19 KB

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