Matrix4.js 19 KB

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