Matrix4.js 33 KB

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