Matrix4.js 33 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339
  1. import { WebGLCoordinateSystem, WebGPUCoordinateSystem } from '../constants.js';
  2. import { Vector3 } from './Vector3.js';
  3. /**
  4. * Represents a 4x4 matrix.
  5. *
  6. * The most common use of a 4x4 matrix in 3D computer graphics is as a transformation matrix.
  7. * For an introduction to transformation matrices as used in WebGL, check out [this tutorial](https://www.opengl-tutorial.org/beginners-tutorials/tutorial-3-matrices)
  8. *
  9. * This allows a 3D vector representing a point in 3D space to undergo
  10. * transformations such as translation, rotation, shear, scale, reflection,
  11. * orthogonal or perspective projection and so on, by being multiplied by the
  12. * matrix. This is known as `applying` the matrix to the vector.
  13. *
  14. * A Note on Row-Major and Column-Major Ordering:
  15. *
  16. * The constructor and {@link Matrix3#set} method take arguments in
  17. * [row-major](https://en.wikipedia.org/wiki/Row-_and_column-major_order#Column-major_order)
  18. * order, while internally they are stored in the {@link Matrix3#elements} array in column-major order.
  19. * This means that calling:
  20. * ```js
  21. * const m = new THREE.Matrix4();
  22. * m.set( 11, 12, 13, 14,
  23. * 21, 22, 23, 24,
  24. * 31, 32, 33, 34,
  25. * 41, 42, 43, 44 );
  26. * ```
  27. * will result in the elements array containing:
  28. * ```js
  29. * m.elements = [ 11, 21, 31, 41,
  30. * 12, 22, 32, 42,
  31. * 13, 23, 33, 43,
  32. * 14, 24, 34, 44 ];
  33. * ```
  34. * and internally all calculations are performed using column-major ordering.
  35. * However, as the actual ordering makes no difference mathematically and
  36. * most people are used to thinking about matrices in row-major order, the
  37. * three.js documentation shows matrices in row-major order. Just bear in
  38. * mind that if you are reading the source code, you'll have to take the
  39. * transpose of any matrices outlined here to make sense of the calculations.
  40. */
  41. class Matrix4 {
  42. static {
  43. /**
  44. * This flag can be used for type testing.
  45. *
  46. * @type {boolean}
  47. * @readonly
  48. * @default true
  49. */
  50. Matrix4.prototype.isMatrix4 = true;
  51. }
  52. /**
  53. * Constructs a new 4x4 matrix. The arguments are supposed to be
  54. * in row-major order. If no arguments are provided, the constructor
  55. * initializes the matrix as an identity matrix.
  56. *
  57. * @param {number} [n11] - 1-1 matrix element.
  58. * @param {number} [n12] - 1-2 matrix element.
  59. * @param {number} [n13] - 1-3 matrix element.
  60. * @param {number} [n14] - 1-4 matrix element.
  61. * @param {number} [n21] - 2-1 matrix element.
  62. * @param {number} [n22] - 2-2 matrix element.
  63. * @param {number} [n23] - 2-3 matrix element.
  64. * @param {number} [n24] - 2-4 matrix element.
  65. * @param {number} [n31] - 3-1 matrix element.
  66. * @param {number} [n32] - 3-2 matrix element.
  67. * @param {number} [n33] - 3-3 matrix element.
  68. * @param {number} [n34] - 3-4 matrix element.
  69. * @param {number} [n41] - 4-1 matrix element.
  70. * @param {number} [n42] - 4-2 matrix element.
  71. * @param {number} [n43] - 4-3 matrix element.
  72. * @param {number} [n44] - 4-4 matrix element.
  73. */
  74. constructor( n11, n12, n13, n14, n21, n22, n23, n24, n31, n32, n33, n34, n41, n42, n43, n44 ) {
  75. /**
  76. * A column-major list of matrix values.
  77. *
  78. * @type {Array<number>}
  79. */
  80. this.elements = [
  81. 1, 0, 0, 0,
  82. 0, 1, 0, 0,
  83. 0, 0, 1, 0,
  84. 0, 0, 0, 1
  85. ];
  86. if ( n11 !== undefined ) {
  87. this.set( n11, n12, n13, n14, n21, n22, n23, n24, n31, n32, n33, n34, n41, n42, n43, n44 );
  88. }
  89. }
  90. /**
  91. * Sets the elements of the matrix.The arguments are supposed to be
  92. * in row-major order.
  93. *
  94. * @param {number} [n11] - 1-1 matrix element.
  95. * @param {number} [n12] - 1-2 matrix element.
  96. * @param {number} [n13] - 1-3 matrix element.
  97. * @param {number} [n14] - 1-4 matrix element.
  98. * @param {number} [n21] - 2-1 matrix element.
  99. * @param {number} [n22] - 2-2 matrix element.
  100. * @param {number} [n23] - 2-3 matrix element.
  101. * @param {number} [n24] - 2-4 matrix element.
  102. * @param {number} [n31] - 3-1 matrix element.
  103. * @param {number} [n32] - 3-2 matrix element.
  104. * @param {number} [n33] - 3-3 matrix element.
  105. * @param {number} [n34] - 3-4 matrix element.
  106. * @param {number} [n41] - 4-1 matrix element.
  107. * @param {number} [n42] - 4-2 matrix element.
  108. * @param {number} [n43] - 4-3 matrix element.
  109. * @param {number} [n44] - 4-4 matrix element.
  110. * @return {Matrix4} A reference to this matrix.
  111. */
  112. set( n11, n12, n13, n14, n21, n22, n23, n24, n31, n32, n33, n34, n41, n42, n43, n44 ) {
  113. const te = this.elements;
  114. te[ 0 ] = n11; te[ 4 ] = n12; te[ 8 ] = n13; te[ 12 ] = n14;
  115. te[ 1 ] = n21; te[ 5 ] = n22; te[ 9 ] = n23; te[ 13 ] = n24;
  116. te[ 2 ] = n31; te[ 6 ] = n32; te[ 10 ] = n33; te[ 14 ] = n34;
  117. te[ 3 ] = n41; te[ 7 ] = n42; te[ 11 ] = n43; te[ 15 ] = n44;
  118. return this;
  119. }
  120. /**
  121. * Sets this matrix to the 4x4 identity matrix.
  122. *
  123. * @return {Matrix4} A reference to this matrix.
  124. */
  125. identity() {
  126. this.set(
  127. 1, 0, 0, 0,
  128. 0, 1, 0, 0,
  129. 0, 0, 1, 0,
  130. 0, 0, 0, 1
  131. );
  132. return this;
  133. }
  134. /**
  135. * Returns a matrix with copied values from this instance.
  136. *
  137. * @return {Matrix4} A clone of this instance.
  138. */
  139. clone() {
  140. return new Matrix4().fromArray( this.elements );
  141. }
  142. /**
  143. * Copies the values of the given matrix to this instance.
  144. *
  145. * @param {Matrix4} m - The matrix to copy.
  146. * @return {Matrix4} A reference to this matrix.
  147. */
  148. copy( m ) {
  149. const te = this.elements;
  150. const me = m.elements;
  151. te[ 0 ] = me[ 0 ]; te[ 1 ] = me[ 1 ]; te[ 2 ] = me[ 2 ]; te[ 3 ] = me[ 3 ];
  152. te[ 4 ] = me[ 4 ]; te[ 5 ] = me[ 5 ]; te[ 6 ] = me[ 6 ]; te[ 7 ] = me[ 7 ];
  153. te[ 8 ] = me[ 8 ]; te[ 9 ] = me[ 9 ]; te[ 10 ] = me[ 10 ]; te[ 11 ] = me[ 11 ];
  154. te[ 12 ] = me[ 12 ]; te[ 13 ] = me[ 13 ]; te[ 14 ] = me[ 14 ]; te[ 15 ] = me[ 15 ];
  155. return this;
  156. }
  157. /**
  158. * Copies the translation component of the given matrix
  159. * into this matrix's translation component.
  160. *
  161. * @param {Matrix4} m - The matrix to copy the translation component.
  162. * @return {Matrix4} A reference to this matrix.
  163. */
  164. copyPosition( m ) {
  165. const te = this.elements, me = m.elements;
  166. te[ 12 ] = me[ 12 ];
  167. te[ 13 ] = me[ 13 ];
  168. te[ 14 ] = me[ 14 ];
  169. return this;
  170. }
  171. /**
  172. * Set the upper 3x3 elements of this matrix to the values of given 3x3 matrix.
  173. *
  174. * @param {Matrix3} m - The 3x3 matrix.
  175. * @return {Matrix4} A reference to this matrix.
  176. */
  177. setFromMatrix3( m ) {
  178. const me = m.elements;
  179. this.set(
  180. me[ 0 ], me[ 3 ], me[ 6 ], 0,
  181. me[ 1 ], me[ 4 ], me[ 7 ], 0,
  182. me[ 2 ], me[ 5 ], me[ 8 ], 0,
  183. 0, 0, 0, 1
  184. );
  185. return this;
  186. }
  187. /**
  188. * Extracts the basis of this matrix into the three axis vectors provided.
  189. *
  190. * @param {Vector3} xAxis - The basis's x axis.
  191. * @param {Vector3} yAxis - The basis's y axis.
  192. * @param {Vector3} zAxis - The basis's z axis.
  193. * @return {Matrix4} A reference to this matrix.
  194. */
  195. extractBasis( xAxis, yAxis, zAxis ) {
  196. if ( this.determinantAffine() === 0 ) {
  197. xAxis.set( 1, 0, 0 );
  198. yAxis.set( 0, 1, 0 );
  199. zAxis.set( 0, 0, 1 );
  200. return this;
  201. }
  202. xAxis.setFromMatrixColumn( this, 0 );
  203. yAxis.setFromMatrixColumn( this, 1 );
  204. zAxis.setFromMatrixColumn( this, 2 );
  205. return this;
  206. }
  207. /**
  208. * Sets the given basis vectors to this matrix.
  209. *
  210. * @param {Vector3} xAxis - The basis's x axis.
  211. * @param {Vector3} yAxis - The basis's y axis.
  212. * @param {Vector3} zAxis - The basis's z axis.
  213. * @return {Matrix4} A reference to this matrix.
  214. */
  215. makeBasis( xAxis, yAxis, zAxis ) {
  216. this.set(
  217. xAxis.x, yAxis.x, zAxis.x, 0,
  218. xAxis.y, yAxis.y, zAxis.y, 0,
  219. xAxis.z, yAxis.z, zAxis.z, 0,
  220. 0, 0, 0, 1
  221. );
  222. return this;
  223. }
  224. /**
  225. * Extracts the rotation component of the given matrix
  226. * into this matrix's rotation component.
  227. *
  228. * Note: This method does not support reflection matrices.
  229. *
  230. * @param {Matrix4} m - The matrix.
  231. * @return {Matrix4} A reference to this matrix.
  232. */
  233. extractRotation( m ) {
  234. if ( m.determinantAffine() === 0 ) {
  235. return this.identity();
  236. }
  237. const te = this.elements;
  238. const me = m.elements;
  239. const scaleX = 1 / _v1.setFromMatrixColumn( m, 0 ).length();
  240. const scaleY = 1 / _v1.setFromMatrixColumn( m, 1 ).length();
  241. const scaleZ = 1 / _v1.setFromMatrixColumn( m, 2 ).length();
  242. te[ 0 ] = me[ 0 ] * scaleX;
  243. te[ 1 ] = me[ 1 ] * scaleX;
  244. te[ 2 ] = me[ 2 ] * scaleX;
  245. te[ 3 ] = 0;
  246. te[ 4 ] = me[ 4 ] * scaleY;
  247. te[ 5 ] = me[ 5 ] * scaleY;
  248. te[ 6 ] = me[ 6 ] * scaleY;
  249. te[ 7 ] = 0;
  250. te[ 8 ] = me[ 8 ] * scaleZ;
  251. te[ 9 ] = me[ 9 ] * scaleZ;
  252. te[ 10 ] = me[ 10 ] * scaleZ;
  253. te[ 11 ] = 0;
  254. te[ 12 ] = 0;
  255. te[ 13 ] = 0;
  256. te[ 14 ] = 0;
  257. te[ 15 ] = 1;
  258. return this;
  259. }
  260. /**
  261. * Sets the rotation component (the upper left 3x3 matrix) of this matrix to
  262. * the rotation specified by the given Euler angles. The rest of
  263. * the matrix is set to the identity. Depending on the {@link Euler#order},
  264. * there are six possible outcomes. See [this page](https://en.wikipedia.org/wiki/Euler_angles#Rotation_matrix)
  265. * for a complete list.
  266. *
  267. * @param {Euler} euler - The Euler angles.
  268. * @return {Matrix4} A reference to this matrix.
  269. */
  270. makeRotationFromEuler( euler ) {
  271. const te = this.elements;
  272. const x = euler.x, y = euler.y, z = euler.z;
  273. const a = Math.cos( x ), b = Math.sin( x );
  274. const c = Math.cos( y ), d = Math.sin( y );
  275. const e = Math.cos( z ), f = Math.sin( z );
  276. if ( euler.order === 'XYZ' ) {
  277. const ae = a * e, af = a * f, be = b * e, bf = b * f;
  278. te[ 0 ] = c * e;
  279. te[ 4 ] = - c * f;
  280. te[ 8 ] = d;
  281. te[ 1 ] = af + be * d;
  282. te[ 5 ] = ae - bf * d;
  283. te[ 9 ] = - b * c;
  284. te[ 2 ] = bf - ae * d;
  285. te[ 6 ] = be + af * d;
  286. te[ 10 ] = a * c;
  287. } else if ( euler.order === 'YXZ' ) {
  288. const ce = c * e, cf = c * f, de = d * e, df = d * f;
  289. te[ 0 ] = ce + df * b;
  290. te[ 4 ] = de * b - cf;
  291. te[ 8 ] = a * d;
  292. te[ 1 ] = a * f;
  293. te[ 5 ] = a * e;
  294. te[ 9 ] = - b;
  295. te[ 2 ] = cf * b - de;
  296. te[ 6 ] = df + ce * b;
  297. te[ 10 ] = a * c;
  298. } else if ( euler.order === 'ZXY' ) {
  299. const ce = c * e, cf = c * f, de = d * e, df = d * f;
  300. te[ 0 ] = ce - df * b;
  301. te[ 4 ] = - a * f;
  302. te[ 8 ] = de + cf * b;
  303. te[ 1 ] = cf + de * b;
  304. te[ 5 ] = a * e;
  305. te[ 9 ] = df - ce * b;
  306. te[ 2 ] = - a * d;
  307. te[ 6 ] = b;
  308. te[ 10 ] = a * c;
  309. } else if ( euler.order === 'ZYX' ) {
  310. const ae = a * e, af = a * f, be = b * e, bf = b * f;
  311. te[ 0 ] = c * e;
  312. te[ 4 ] = be * d - af;
  313. te[ 8 ] = ae * d + bf;
  314. te[ 1 ] = c * f;
  315. te[ 5 ] = bf * d + ae;
  316. te[ 9 ] = af * d - be;
  317. te[ 2 ] = - d;
  318. te[ 6 ] = b * c;
  319. te[ 10 ] = a * c;
  320. } else if ( euler.order === 'YZX' ) {
  321. const ac = a * c, ad = a * d, bc = b * c, bd = b * d;
  322. te[ 0 ] = c * e;
  323. te[ 4 ] = bd - ac * f;
  324. te[ 8 ] = bc * f + ad;
  325. te[ 1 ] = f;
  326. te[ 5 ] = a * e;
  327. te[ 9 ] = - b * e;
  328. te[ 2 ] = - d * e;
  329. te[ 6 ] = ad * f + bc;
  330. te[ 10 ] = ac - bd * f;
  331. } else if ( euler.order === 'XZY' ) {
  332. const ac = a * c, ad = a * d, bc = b * c, bd = b * d;
  333. te[ 0 ] = c * e;
  334. te[ 4 ] = - f;
  335. te[ 8 ] = d * e;
  336. te[ 1 ] = ac * f + bd;
  337. te[ 5 ] = a * e;
  338. te[ 9 ] = ad * f - bc;
  339. te[ 2 ] = bc * f - ad;
  340. te[ 6 ] = b * e;
  341. te[ 10 ] = bd * f + ac;
  342. }
  343. // bottom row
  344. te[ 3 ] = 0;
  345. te[ 7 ] = 0;
  346. te[ 11 ] = 0;
  347. // last column
  348. te[ 12 ] = 0;
  349. te[ 13 ] = 0;
  350. te[ 14 ] = 0;
  351. te[ 15 ] = 1;
  352. return this;
  353. }
  354. /**
  355. * Sets the rotation component of this matrix to the rotation specified by
  356. * the given Quaternion as outlined [here](https://en.wikipedia.org/wiki/Rotation_matrix#Quaternion)
  357. * The rest of the matrix is set to the identity.
  358. *
  359. * @param {Quaternion} q - The Quaternion.
  360. * @return {Matrix4} A reference to this matrix.
  361. */
  362. makeRotationFromQuaternion( q ) {
  363. return this.compose( _zero, q, _one );
  364. }
  365. /**
  366. * Sets the rotation component of the transformation matrix, looking from `eye` towards
  367. * `target`, and oriented by the up-direction.
  368. *
  369. * @param {Vector3} eye - The eye vector.
  370. * @param {Vector3} target - The target vector.
  371. * @param {Vector3} up - The up vector.
  372. * @return {Matrix4} A reference to this matrix.
  373. */
  374. lookAt( eye, target, up ) {
  375. const te = this.elements;
  376. _z.subVectors( eye, target );
  377. if ( _z.lengthSq() === 0 ) {
  378. // eye and target are in the same position
  379. _z.z = 1;
  380. }
  381. _z.normalize();
  382. _x.crossVectors( up, _z );
  383. if ( _x.lengthSq() === 0 ) {
  384. // up and z are parallel
  385. if ( Math.abs( up.z ) === 1 ) {
  386. _z.x += 0.0001;
  387. } else {
  388. _z.z += 0.0001;
  389. }
  390. _z.normalize();
  391. _x.crossVectors( up, _z );
  392. }
  393. _x.normalize();
  394. _y.crossVectors( _z, _x );
  395. te[ 0 ] = _x.x; te[ 4 ] = _y.x; te[ 8 ] = _z.x;
  396. te[ 1 ] = _x.y; te[ 5 ] = _y.y; te[ 9 ] = _z.y;
  397. te[ 2 ] = _x.z; te[ 6 ] = _y.z; te[ 10 ] = _z.z;
  398. return this;
  399. }
  400. /**
  401. * Post-multiplies this matrix by the given 4x4 matrix.
  402. *
  403. * @param {Matrix4} m - The matrix to multiply with.
  404. * @return {Matrix4} A reference to this matrix.
  405. */
  406. multiply( m ) {
  407. return this.multiplyMatrices( this, m );
  408. }
  409. /**
  410. * Pre-multiplies this matrix by the given 4x4 matrix.
  411. *
  412. * @param {Matrix4} m - The matrix to multiply with.
  413. * @return {Matrix4} A reference to this matrix.
  414. */
  415. premultiply( m ) {
  416. return this.multiplyMatrices( m, this );
  417. }
  418. /**
  419. * Multiples the given 4x4 matrices and stores the result
  420. * in this matrix.
  421. *
  422. * @param {Matrix4} a - The first matrix.
  423. * @param {Matrix4} b - The second matrix.
  424. * @return {Matrix4} A reference to this matrix.
  425. */
  426. multiplyMatrices( a, b ) {
  427. const ae = a.elements;
  428. const be = b.elements;
  429. const te = this.elements;
  430. const a11 = ae[ 0 ], a12 = ae[ 4 ], a13 = ae[ 8 ], a14 = ae[ 12 ];
  431. const a21 = ae[ 1 ], a22 = ae[ 5 ], a23 = ae[ 9 ], a24 = ae[ 13 ];
  432. const a31 = ae[ 2 ], a32 = ae[ 6 ], a33 = ae[ 10 ], a34 = ae[ 14 ];
  433. const a41 = ae[ 3 ], a42 = ae[ 7 ], a43 = ae[ 11 ], a44 = ae[ 15 ];
  434. const b11 = be[ 0 ], b12 = be[ 4 ], b13 = be[ 8 ], b14 = be[ 12 ];
  435. const b21 = be[ 1 ], b22 = be[ 5 ], b23 = be[ 9 ], b24 = be[ 13 ];
  436. const b31 = be[ 2 ], b32 = be[ 6 ], b33 = be[ 10 ], b34 = be[ 14 ];
  437. const b41 = be[ 3 ], b42 = be[ 7 ], b43 = be[ 11 ], b44 = be[ 15 ];
  438. te[ 0 ] = a11 * b11 + a12 * b21 + a13 * b31 + a14 * b41;
  439. te[ 4 ] = a11 * b12 + a12 * b22 + a13 * b32 + a14 * b42;
  440. te[ 8 ] = a11 * b13 + a12 * b23 + a13 * b33 + a14 * b43;
  441. te[ 12 ] = a11 * b14 + a12 * b24 + a13 * b34 + a14 * b44;
  442. te[ 1 ] = a21 * b11 + a22 * b21 + a23 * b31 + a24 * b41;
  443. te[ 5 ] = a21 * b12 + a22 * b22 + a23 * b32 + a24 * b42;
  444. te[ 9 ] = a21 * b13 + a22 * b23 + a23 * b33 + a24 * b43;
  445. te[ 13 ] = a21 * b14 + a22 * b24 + a23 * b34 + a24 * b44;
  446. te[ 2 ] = a31 * b11 + a32 * b21 + a33 * b31 + a34 * b41;
  447. te[ 6 ] = a31 * b12 + a32 * b22 + a33 * b32 + a34 * b42;
  448. te[ 10 ] = a31 * b13 + a32 * b23 + a33 * b33 + a34 * b43;
  449. te[ 14 ] = a31 * b14 + a32 * b24 + a33 * b34 + a34 * b44;
  450. te[ 3 ] = a41 * b11 + a42 * b21 + a43 * b31 + a44 * b41;
  451. te[ 7 ] = a41 * b12 + a42 * b22 + a43 * b32 + a44 * b42;
  452. te[ 11 ] = a41 * b13 + a42 * b23 + a43 * b33 + a44 * b43;
  453. te[ 15 ] = a41 * b14 + a42 * b24 + a43 * b34 + a44 * b44;
  454. return this;
  455. }
  456. /**
  457. * Multiplies every component of the matrix by the given scalar.
  458. *
  459. * @param {number} s - The scalar.
  460. * @return {Matrix4} A reference to this matrix.
  461. */
  462. multiplyScalar( s ) {
  463. const te = this.elements;
  464. te[ 0 ] *= s; te[ 4 ] *= s; te[ 8 ] *= s; te[ 12 ] *= s;
  465. te[ 1 ] *= s; te[ 5 ] *= s; te[ 9 ] *= s; te[ 13 ] *= s;
  466. te[ 2 ] *= s; te[ 6 ] *= s; te[ 10 ] *= s; te[ 14 ] *= s;
  467. te[ 3 ] *= s; te[ 7 ] *= s; te[ 11 ] *= s; te[ 15 ] *= s;
  468. return this;
  469. }
  470. /**
  471. * Computes and returns the determinant of this matrix.
  472. *
  473. * Based on the method outlined [here](http://www.euclideanspace.com/maths/algebra/matrix/functions/inverse/fourD/index.html).
  474. *
  475. * @return {number} The determinant.
  476. */
  477. determinant() {
  478. const te = this.elements;
  479. const n11 = te[ 0 ], n12 = te[ 4 ], n13 = te[ 8 ], n14 = te[ 12 ];
  480. const n21 = te[ 1 ], n22 = te[ 5 ], n23 = te[ 9 ], n24 = te[ 13 ];
  481. const n31 = te[ 2 ], n32 = te[ 6 ], n33 = te[ 10 ], n34 = te[ 14 ];
  482. const n41 = te[ 3 ], n42 = te[ 7 ], n43 = te[ 11 ], n44 = te[ 15 ];
  483. const t11 = n23 * n34 - n24 * n33;
  484. const t12 = n22 * n34 - n24 * n32;
  485. const t13 = n22 * n33 - n23 * n32;
  486. const t21 = n21 * n34 - n24 * n31;
  487. const t22 = n21 * n33 - n23 * n31;
  488. const t23 = n21 * n32 - n22 * n31;
  489. return n11 * ( n42 * t11 - n43 * t12 + n44 * t13 ) -
  490. n12 * ( n41 * t11 - n43 * t21 + n44 * t22 ) +
  491. n13 * ( n41 * t12 - n42 * t21 + n44 * t23 ) -
  492. n14 * ( n41 * t13 - n42 * t22 + n43 * t23 );
  493. }
  494. /**
  495. * Computes and returns the determinant of the 4x4 matrix, but assumes the
  496. * matrix is affine, saving some computations.
  497. *
  498. * For affine matrices (like an object's world matrix), this value equals the
  499. * full 4x4 {@link Matrix4#determinant} but is cheaper to compute.
  500. *
  501. * Assumes the bottom row is [0, 0, 0, 1].
  502. *
  503. * @return {number} The determinant of the matrix.
  504. */
  505. determinantAffine() {
  506. const te = this.elements;
  507. const n11 = te[ 0 ], n12 = te[ 4 ], n13 = te[ 8 ];
  508. const n21 = te[ 1 ], n22 = te[ 5 ], n23 = te[ 9 ];
  509. const n31 = te[ 2 ], n32 = te[ 6 ], n33 = te[ 10 ];
  510. return n11 * ( n22 * n33 - n23 * n32 ) -
  511. n12 * ( n21 * n33 - n23 * n31 ) +
  512. n13 * ( n21 * n32 - n22 * n31 );
  513. }
  514. /**
  515. * Transposes this matrix in place.
  516. *
  517. * @return {Matrix4} A reference to this matrix.
  518. */
  519. transpose() {
  520. const te = this.elements;
  521. let tmp;
  522. tmp = te[ 1 ]; te[ 1 ] = te[ 4 ]; te[ 4 ] = tmp;
  523. tmp = te[ 2 ]; te[ 2 ] = te[ 8 ]; te[ 8 ] = tmp;
  524. tmp = te[ 6 ]; te[ 6 ] = te[ 9 ]; te[ 9 ] = tmp;
  525. tmp = te[ 3 ]; te[ 3 ] = te[ 12 ]; te[ 12 ] = tmp;
  526. tmp = te[ 7 ]; te[ 7 ] = te[ 13 ]; te[ 13 ] = tmp;
  527. tmp = te[ 11 ]; te[ 11 ] = te[ 14 ]; te[ 14 ] = tmp;
  528. return this;
  529. }
  530. /**
  531. * Sets the position component for this matrix from the given vector,
  532. * without affecting the rest of the matrix.
  533. *
  534. * @param {number|Vector3} x - The x component of the vector or alternatively the vector object.
  535. * @param {number} y - The y component of the vector.
  536. * @param {number} z - The z component of the vector.
  537. * @return {Matrix4} A reference to this matrix.
  538. */
  539. setPosition( x, y, z ) {
  540. const te = this.elements;
  541. if ( x.isVector3 ) {
  542. te[ 12 ] = x.x;
  543. te[ 13 ] = x.y;
  544. te[ 14 ] = x.z;
  545. } else {
  546. te[ 12 ] = x;
  547. te[ 13 ] = y;
  548. te[ 14 ] = z;
  549. }
  550. return this;
  551. }
  552. /**
  553. * Inverts this matrix, using the [analytic method](https://en.wikipedia.org/wiki/Invertible_matrix#Analytic_solution).
  554. * You can not invert with a determinant of zero. If you attempt this, the method produces
  555. * a zero matrix instead.
  556. *
  557. * @return {Matrix4} A reference to this matrix.
  558. */
  559. invert() {
  560. // based on https://github.com/toji/gl-matrix
  561. const te = this.elements,
  562. n11 = te[ 0 ], n21 = te[ 1 ], n31 = te[ 2 ], n41 = te[ 3 ],
  563. n12 = te[ 4 ], n22 = te[ 5 ], n32 = te[ 6 ], n42 = te[ 7 ],
  564. n13 = te[ 8 ], n23 = te[ 9 ], n33 = te[ 10 ], n43 = te[ 11 ],
  565. n14 = te[ 12 ], n24 = te[ 13 ], n34 = te[ 14 ], n44 = te[ 15 ],
  566. t1 = n11 * n22 - n21 * n12,
  567. t2 = n11 * n32 - n31 * n12,
  568. t3 = n11 * n42 - n41 * n12,
  569. t4 = n21 * n32 - n31 * n22,
  570. t5 = n21 * n42 - n41 * n22,
  571. t6 = n31 * n42 - n41 * n32,
  572. t7 = n13 * n24 - n23 * n14,
  573. t8 = n13 * n34 - n33 * n14,
  574. t9 = n13 * n44 - n43 * n14,
  575. t10 = n23 * n34 - n33 * n24,
  576. t11 = n23 * n44 - n43 * n24,
  577. t12 = n33 * n44 - n43 * n34;
  578. const det = t1 * t12 - t2 * t11 + t3 * t10 + t4 * t9 - t5 * t8 + t6 * t7;
  579. if ( det === 0 ) return this.set( 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 );
  580. const detInv = 1 / det;
  581. te[ 0 ] = ( n22 * t12 - n32 * t11 + n42 * t10 ) * detInv;
  582. te[ 1 ] = ( n31 * t11 - n21 * t12 - n41 * t10 ) * detInv;
  583. te[ 2 ] = ( n24 * t6 - n34 * t5 + n44 * t4 ) * detInv;
  584. te[ 3 ] = ( n33 * t5 - n23 * t6 - n43 * t4 ) * detInv;
  585. te[ 4 ] = ( n32 * t9 - n12 * t12 - n42 * t8 ) * detInv;
  586. te[ 5 ] = ( n11 * t12 - n31 * t9 + n41 * t8 ) * detInv;
  587. te[ 6 ] = ( n34 * t3 - n14 * t6 - n44 * t2 ) * detInv;
  588. te[ 7 ] = ( n13 * t6 - n33 * t3 + n43 * t2 ) * detInv;
  589. te[ 8 ] = ( n12 * t11 - n22 * t9 + n42 * t7 ) * detInv;
  590. te[ 9 ] = ( n21 * t9 - n11 * t11 - n41 * t7 ) * detInv;
  591. te[ 10 ] = ( n14 * t5 - n24 * t3 + n44 * t1 ) * detInv;
  592. te[ 11 ] = ( n23 * t3 - n13 * t5 - n43 * t1 ) * detInv;
  593. te[ 12 ] = ( n22 * t8 - n12 * t10 - n32 * t7 ) * detInv;
  594. te[ 13 ] = ( n11 * t10 - n21 * t8 + n31 * t7 ) * detInv;
  595. te[ 14 ] = ( n24 * t2 - n14 * t4 - n34 * t1 ) * detInv;
  596. te[ 15 ] = ( n13 * t4 - n23 * t2 + n33 * t1 ) * detInv;
  597. return this;
  598. }
  599. /**
  600. * Multiplies the columns of this matrix by the given vector.
  601. *
  602. * @param {Vector3} v - The scale vector.
  603. * @return {Matrix4} A reference to this matrix.
  604. */
  605. scale( v ) {
  606. const te = this.elements;
  607. const x = v.x, y = v.y, z = v.z;
  608. te[ 0 ] *= x; te[ 4 ] *= y; te[ 8 ] *= z;
  609. te[ 1 ] *= x; te[ 5 ] *= y; te[ 9 ] *= z;
  610. te[ 2 ] *= x; te[ 6 ] *= y; te[ 10 ] *= z;
  611. te[ 3 ] *= x; te[ 7 ] *= y; te[ 11 ] *= z;
  612. return this;
  613. }
  614. /**
  615. * Gets the maximum scale value of the three axes.
  616. *
  617. * @return {number} The maximum scale.
  618. */
  619. getMaxScaleOnAxis() {
  620. const te = this.elements;
  621. const scaleXSq = te[ 0 ] * te[ 0 ] + te[ 1 ] * te[ 1 ] + te[ 2 ] * te[ 2 ];
  622. const scaleYSq = te[ 4 ] * te[ 4 ] + te[ 5 ] * te[ 5 ] + te[ 6 ] * te[ 6 ];
  623. const scaleZSq = te[ 8 ] * te[ 8 ] + te[ 9 ] * te[ 9 ] + te[ 10 ] * te[ 10 ];
  624. return Math.sqrt( Math.max( scaleXSq, scaleYSq, scaleZSq ) );
  625. }
  626. /**
  627. * Sets this matrix as a translation transform from the given vector.
  628. *
  629. * @param {number|Vector3} x - The amount to translate in the X axis or alternatively a translation vector.
  630. * @param {number} y - The amount to translate in the Y axis.
  631. * @param {number} z - The amount to translate in the z axis.
  632. * @return {Matrix4} A reference to this matrix.
  633. */
  634. makeTranslation( x, y, z ) {
  635. if ( x.isVector3 ) {
  636. this.set(
  637. 1, 0, 0, x.x,
  638. 0, 1, 0, x.y,
  639. 0, 0, 1, x.z,
  640. 0, 0, 0, 1
  641. );
  642. } else {
  643. this.set(
  644. 1, 0, 0, x,
  645. 0, 1, 0, y,
  646. 0, 0, 1, z,
  647. 0, 0, 0, 1
  648. );
  649. }
  650. return this;
  651. }
  652. /**
  653. * Sets this matrix as a rotational transformation around the X axis by
  654. * the given angle.
  655. *
  656. * @param {number} theta - The rotation in radians.
  657. * @return {Matrix4} A reference to this matrix.
  658. */
  659. makeRotationX( theta ) {
  660. const c = Math.cos( theta ), s = Math.sin( theta );
  661. this.set(
  662. 1, 0, 0, 0,
  663. 0, c, - s, 0,
  664. 0, s, c, 0,
  665. 0, 0, 0, 1
  666. );
  667. return this;
  668. }
  669. /**
  670. * Sets this matrix as a rotational transformation around the Y axis by
  671. * the given angle.
  672. *
  673. * @param {number} theta - The rotation in radians.
  674. * @return {Matrix4} A reference to this matrix.
  675. */
  676. makeRotationY( theta ) {
  677. const c = Math.cos( theta ), s = Math.sin( theta );
  678. this.set(
  679. c, 0, s, 0,
  680. 0, 1, 0, 0,
  681. - s, 0, c, 0,
  682. 0, 0, 0, 1
  683. );
  684. return this;
  685. }
  686. /**
  687. * Sets this matrix as a rotational transformation around the Z axis by
  688. * the given angle.
  689. *
  690. * @param {number} theta - The rotation in radians.
  691. * @return {Matrix4} A reference to this matrix.
  692. */
  693. makeRotationZ( theta ) {
  694. const c = Math.cos( theta ), s = Math.sin( theta );
  695. this.set(
  696. c, - s, 0, 0,
  697. s, c, 0, 0,
  698. 0, 0, 1, 0,
  699. 0, 0, 0, 1
  700. );
  701. return this;
  702. }
  703. /**
  704. * Sets this matrix as a rotational transformation around the given axis by
  705. * the given angle.
  706. *
  707. * This is a somewhat controversial but mathematically sound alternative to
  708. * rotating via Quaternions. See the discussion [here](https://www.gamedev.net/articles/programming/math-and-physics/do-we-really-need-quaternions-r1199).
  709. *
  710. * @param {Vector3} axis - The normalized rotation axis.
  711. * @param {number} angle - The rotation in radians.
  712. * @return {Matrix4} A reference to this matrix.
  713. */
  714. makeRotationAxis( axis, angle ) {
  715. // Based on http://www.gamedev.net/reference/articles/article1199.asp
  716. const c = Math.cos( angle );
  717. const s = Math.sin( angle );
  718. const t = 1 - c;
  719. const x = axis.x, y = axis.y, z = axis.z;
  720. const tx = t * x, ty = t * y;
  721. this.set(
  722. tx * x + c, tx * y - s * z, tx * z + s * y, 0,
  723. tx * y + s * z, ty * y + c, ty * z - s * x, 0,
  724. tx * z - s * y, ty * z + s * x, t * z * z + c, 0,
  725. 0, 0, 0, 1
  726. );
  727. return this;
  728. }
  729. /**
  730. * Sets this matrix as a scale transformation.
  731. *
  732. * @param {number} x - The amount to scale in the X axis.
  733. * @param {number} y - The amount to scale in the Y axis.
  734. * @param {number} z - The amount to scale in the Z axis.
  735. * @return {Matrix4} A reference to this matrix.
  736. */
  737. makeScale( x, y, z ) {
  738. this.set(
  739. x, 0, 0, 0,
  740. 0, y, 0, 0,
  741. 0, 0, z, 0,
  742. 0, 0, 0, 1
  743. );
  744. return this;
  745. }
  746. /**
  747. * Sets this matrix as a shear transformation.
  748. *
  749. * @param {number} xy - The amount to shear X by Y.
  750. * @param {number} xz - The amount to shear X by Z.
  751. * @param {number} yx - The amount to shear Y by X.
  752. * @param {number} yz - The amount to shear Y by Z.
  753. * @param {number} zx - The amount to shear Z by X.
  754. * @param {number} zy - The amount to shear Z by Y.
  755. * @return {Matrix4} A reference to this matrix.
  756. */
  757. makeShear( xy, xz, yx, yz, zx, zy ) {
  758. this.set(
  759. 1, yx, zx, 0,
  760. xy, 1, zy, 0,
  761. xz, yz, 1, 0,
  762. 0, 0, 0, 1
  763. );
  764. return this;
  765. }
  766. /**
  767. * Sets this matrix to the transformation composed of the given position,
  768. * rotation (Quaternion) and scale.
  769. *
  770. * @param {Vector3} position - The position vector.
  771. * @param {Quaternion} quaternion - The rotation as a Quaternion.
  772. * @param {Vector3} scale - The scale vector.
  773. * @return {Matrix4} A reference to this matrix.
  774. */
  775. compose( position, quaternion, scale ) {
  776. const te = this.elements;
  777. const x = quaternion._x, y = quaternion._y, z = quaternion._z, w = quaternion._w;
  778. const x2 = x + x, y2 = y + y, z2 = z + z;
  779. const xx = x * x2, xy = x * y2, xz = x * z2;
  780. const yy = y * y2, yz = y * z2, zz = z * z2;
  781. const wx = w * x2, wy = w * y2, wz = w * z2;
  782. const sx = scale.x, sy = scale.y, sz = scale.z;
  783. te[ 0 ] = ( 1 - ( yy + zz ) ) * sx;
  784. te[ 1 ] = ( xy + wz ) * sx;
  785. te[ 2 ] = ( xz - wy ) * sx;
  786. te[ 3 ] = 0;
  787. te[ 4 ] = ( xy - wz ) * sy;
  788. te[ 5 ] = ( 1 - ( xx + zz ) ) * sy;
  789. te[ 6 ] = ( yz + wx ) * sy;
  790. te[ 7 ] = 0;
  791. te[ 8 ] = ( xz + wy ) * sz;
  792. te[ 9 ] = ( yz - wx ) * sz;
  793. te[ 10 ] = ( 1 - ( xx + yy ) ) * sz;
  794. te[ 11 ] = 0;
  795. te[ 12 ] = position.x;
  796. te[ 13 ] = position.y;
  797. te[ 14 ] = position.z;
  798. te[ 15 ] = 1;
  799. return this;
  800. }
  801. /**
  802. * Decomposes this matrix into its position, rotation and scale components
  803. * and provides the result in the given objects.
  804. *
  805. * Note: Not all matrices are decomposable in this way. For example, if an
  806. * object has a non-uniformly scaled parent, then the object's world matrix
  807. * may not be decomposable, and this method may not be appropriate.
  808. *
  809. * @param {Vector3} position - The position vector.
  810. * @param {Quaternion} quaternion - The rotation as a Quaternion.
  811. * @param {Vector3} scale - The scale vector.
  812. * @return {Matrix4} A reference to this matrix.
  813. */
  814. decompose( position, quaternion, scale ) {
  815. const te = this.elements;
  816. position.x = te[ 12 ];
  817. position.y = te[ 13 ];
  818. position.z = te[ 14 ];
  819. const det = this.determinantAffine();
  820. if ( det === 0 ) {
  821. scale.set( 1, 1, 1 );
  822. quaternion.identity();
  823. return this;
  824. }
  825. let sx = _v1.set( te[ 0 ], te[ 1 ], te[ 2 ] ).length();
  826. const sy = _v1.set( te[ 4 ], te[ 5 ], te[ 6 ] ).length();
  827. const sz = _v1.set( te[ 8 ], te[ 9 ], te[ 10 ] ).length();
  828. // if determinant is negative, we need to invert one scale
  829. if ( det < 0 ) sx = - sx;
  830. // scale the rotation part
  831. _m1.copy( this );
  832. const invSX = 1 / sx;
  833. const invSY = 1 / sy;
  834. const invSZ = 1 / sz;
  835. _m1.elements[ 0 ] *= invSX;
  836. _m1.elements[ 1 ] *= invSX;
  837. _m1.elements[ 2 ] *= invSX;
  838. _m1.elements[ 4 ] *= invSY;
  839. _m1.elements[ 5 ] *= invSY;
  840. _m1.elements[ 6 ] *= invSY;
  841. _m1.elements[ 8 ] *= invSZ;
  842. _m1.elements[ 9 ] *= invSZ;
  843. _m1.elements[ 10 ] *= invSZ;
  844. quaternion.setFromRotationMatrix( _m1 );
  845. scale.x = sx;
  846. scale.y = sy;
  847. scale.z = sz;
  848. return this;
  849. }
  850. /**
  851. * Creates a perspective projection matrix. This is used internally by
  852. * {@link PerspectiveCamera#updateProjectionMatrix}.
  853. * @param {number} left - Left boundary of the viewing frustum at the near plane.
  854. * @param {number} right - Right boundary of the viewing frustum at the near plane.
  855. * @param {number} top - Top boundary of the viewing frustum at the near plane.
  856. * @param {number} bottom - Bottom boundary of the viewing frustum at the near plane.
  857. * @param {number} near - The distance from the camera to the near plane.
  858. * @param {number} far - The distance from the camera to the far plane.
  859. * @param {(WebGLCoordinateSystem|WebGPUCoordinateSystem)} [coordinateSystem=WebGLCoordinateSystem] - The coordinate system.
  860. * @param {boolean} [reversedDepth=false] - Whether to use a reversed depth.
  861. * @return {Matrix4} A reference to this matrix.
  862. */
  863. makePerspective( left, right, top, bottom, near, far, coordinateSystem = WebGLCoordinateSystem, reversedDepth = false ) {
  864. const te = this.elements;
  865. const x = 2 * near / ( right - left );
  866. const y = 2 * near / ( top - bottom );
  867. const a = ( right + left ) / ( right - left );
  868. const b = ( top + bottom ) / ( top - bottom );
  869. let c, d;
  870. if ( reversedDepth ) {
  871. c = near / ( far - near );
  872. d = ( far * near ) / ( far - near );
  873. } else {
  874. if ( coordinateSystem === WebGLCoordinateSystem ) {
  875. c = - ( far + near ) / ( far - near );
  876. d = ( - 2 * far * near ) / ( far - near );
  877. } else if ( coordinateSystem === WebGPUCoordinateSystem ) {
  878. c = - far / ( far - near );
  879. d = ( - far * near ) / ( far - near );
  880. } else {
  881. throw new Error( 'THREE.Matrix4.makePerspective(): Invalid coordinate system: ' + coordinateSystem );
  882. }
  883. }
  884. te[ 0 ] = x; te[ 4 ] = 0; te[ 8 ] = a; te[ 12 ] = 0;
  885. te[ 1 ] = 0; te[ 5 ] = y; te[ 9 ] = b; te[ 13 ] = 0;
  886. te[ 2 ] = 0; te[ 6 ] = 0; te[ 10 ] = c; te[ 14 ] = d;
  887. te[ 3 ] = 0; te[ 7 ] = 0; te[ 11 ] = - 1; te[ 15 ] = 0;
  888. return this;
  889. }
  890. /**
  891. * Creates a orthographic projection matrix. This is used internally by
  892. * {@link OrthographicCamera#updateProjectionMatrix}.
  893. * @param {number} left - Left boundary of the viewing frustum at the near plane.
  894. * @param {number} right - Right boundary of the viewing frustum at the near plane.
  895. * @param {number} top - Top boundary of the viewing frustum at the near plane.
  896. * @param {number} bottom - Bottom boundary of the viewing frustum at the near plane.
  897. * @param {number} near - The distance from the camera to the near plane.
  898. * @param {number} far - The distance from the camera to the far plane.
  899. * @param {(WebGLCoordinateSystem|WebGPUCoordinateSystem)} [coordinateSystem=WebGLCoordinateSystem] - The coordinate system.
  900. * @param {boolean} [reversedDepth=false] - Whether to use a reversed depth.
  901. * @return {Matrix4} A reference to this matrix.
  902. */
  903. makeOrthographic( left, right, top, bottom, near, far, coordinateSystem = WebGLCoordinateSystem, reversedDepth = false ) {
  904. const te = this.elements;
  905. const x = 2 / ( right - left );
  906. const y = 2 / ( top - bottom );
  907. const a = - ( right + left ) / ( right - left );
  908. const b = - ( top + bottom ) / ( top - bottom );
  909. let c, d;
  910. if ( reversedDepth ) {
  911. c = 1 / ( far - near );
  912. d = far / ( far - near );
  913. } else {
  914. if ( coordinateSystem === WebGLCoordinateSystem ) {
  915. c = - 2 / ( far - near );
  916. d = - ( far + near ) / ( far - near );
  917. } else if ( coordinateSystem === WebGPUCoordinateSystem ) {
  918. c = - 1 / ( far - near );
  919. d = - near / ( far - near );
  920. } else {
  921. throw new Error( 'THREE.Matrix4.makeOrthographic(): Invalid coordinate system: ' + coordinateSystem );
  922. }
  923. }
  924. te[ 0 ] = x; te[ 4 ] = 0; te[ 8 ] = 0; te[ 12 ] = a;
  925. te[ 1 ] = 0; te[ 5 ] = y; te[ 9 ] = 0; te[ 13 ] = b;
  926. te[ 2 ] = 0; te[ 6 ] = 0; te[ 10 ] = c; te[ 14 ] = d;
  927. te[ 3 ] = 0; te[ 7 ] = 0; te[ 11 ] = 0; te[ 15 ] = 1;
  928. return this;
  929. }
  930. /**
  931. * Returns `true` if this matrix is equal with the given one.
  932. *
  933. * @param {Matrix4} matrix - The matrix to test for equality.
  934. * @return {boolean} Whether this matrix is equal with the given one.
  935. */
  936. equals( matrix ) {
  937. const te = this.elements;
  938. const me = matrix.elements;
  939. for ( let i = 0; i < 16; i ++ ) {
  940. if ( te[ i ] !== me[ i ] ) return false;
  941. }
  942. return true;
  943. }
  944. /**
  945. * Sets the elements of the matrix from the given array.
  946. *
  947. * @param {Array<number>} array - The matrix elements in column-major order.
  948. * @param {number} [offset=0] - Index of the first element in the array.
  949. * @return {Matrix4} A reference to this matrix.
  950. */
  951. fromArray( array, offset = 0 ) {
  952. for ( let i = 0; i < 16; i ++ ) {
  953. this.elements[ i ] = array[ i + offset ];
  954. }
  955. return this;
  956. }
  957. /**
  958. * Writes the elements of this matrix to the given array. If no array is provided,
  959. * the method returns a new instance.
  960. *
  961. * @param {Array<number>} [array=[]] - The target array holding the matrix elements in column-major order.
  962. * @param {number} [offset=0] - Index of the first element in the array.
  963. * @return {Array<number>} The matrix elements in column-major order.
  964. */
  965. toArray( array = [], offset = 0 ) {
  966. const te = this.elements;
  967. array[ offset ] = te[ 0 ];
  968. array[ offset + 1 ] = te[ 1 ];
  969. array[ offset + 2 ] = te[ 2 ];
  970. array[ offset + 3 ] = te[ 3 ];
  971. array[ offset + 4 ] = te[ 4 ];
  972. array[ offset + 5 ] = te[ 5 ];
  973. array[ offset + 6 ] = te[ 6 ];
  974. array[ offset + 7 ] = te[ 7 ];
  975. array[ offset + 8 ] = te[ 8 ];
  976. array[ offset + 9 ] = te[ 9 ];
  977. array[ offset + 10 ] = te[ 10 ];
  978. array[ offset + 11 ] = te[ 11 ];
  979. array[ offset + 12 ] = te[ 12 ];
  980. array[ offset + 13 ] = te[ 13 ];
  981. array[ offset + 14 ] = te[ 14 ];
  982. array[ offset + 15 ] = te[ 15 ];
  983. return array;
  984. }
  985. }
  986. const _v1 = /*@__PURE__*/ new Vector3();
  987. const _m1 = /*@__PURE__*/ new Matrix4();
  988. const _zero = /*@__PURE__*/ new Vector3( 0, 0, 0 );
  989. const _one = /*@__PURE__*/ new Vector3( 1, 1, 1 );
  990. const _x = /*@__PURE__*/ new Vector3();
  991. const _y = /*@__PURE__*/ new Vector3();
  992. const _z = /*@__PURE__*/ new Vector3();
  993. export { Matrix4 };
粤ICP备19079148号