Matrix4.js 19 KB

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