Source: node_modules/gl-matrix/esm/mat4.js

  1. import * as glMatrix from "./common.js";
  2. /**
  3. * 4x4 Matrix<br>Format: column-major, when typed out it looks like row-major<br>The matrices are being post multiplied.
  4. * @module mat4
  5. */
  6. /**
  7. * Creates a new identity mat4
  8. *
  9. * @returns {mat4} a new 4x4 matrix
  10. */
  11. export function create() {
  12. var out = new glMatrix.ARRAY_TYPE(16);
  13. if (glMatrix.ARRAY_TYPE != Float32Array) {
  14. out[1] = 0;
  15. out[2] = 0;
  16. out[3] = 0;
  17. out[4] = 0;
  18. out[6] = 0;
  19. out[7] = 0;
  20. out[8] = 0;
  21. out[9] = 0;
  22. out[11] = 0;
  23. out[12] = 0;
  24. out[13] = 0;
  25. out[14] = 0;
  26. }
  27. out[0] = 1;
  28. out[5] = 1;
  29. out[10] = 1;
  30. out[15] = 1;
  31. return out;
  32. }
  33. /**
  34. * Creates a new mat4 initialized with values from an existing matrix
  35. *
  36. * @param {ReadonlyMat4} a matrix to clone
  37. * @returns {mat4} a new 4x4 matrix
  38. */
  39. export function clone(a) {
  40. var out = new glMatrix.ARRAY_TYPE(16);
  41. out[0] = a[0];
  42. out[1] = a[1];
  43. out[2] = a[2];
  44. out[3] = a[3];
  45. out[4] = a[4];
  46. out[5] = a[5];
  47. out[6] = a[6];
  48. out[7] = a[7];
  49. out[8] = a[8];
  50. out[9] = a[9];
  51. out[10] = a[10];
  52. out[11] = a[11];
  53. out[12] = a[12];
  54. out[13] = a[13];
  55. out[14] = a[14];
  56. out[15] = a[15];
  57. return out;
  58. }
  59. /**
  60. * Copy the values from one mat4 to another
  61. *
  62. * @param {mat4} out the receiving matrix
  63. * @param {ReadonlyMat4} a the source matrix
  64. * @returns {mat4} out
  65. */
  66. export function copy(out, a) {
  67. out[0] = a[0];
  68. out[1] = a[1];
  69. out[2] = a[2];
  70. out[3] = a[3];
  71. out[4] = a[4];
  72. out[5] = a[5];
  73. out[6] = a[6];
  74. out[7] = a[7];
  75. out[8] = a[8];
  76. out[9] = a[9];
  77. out[10] = a[10];
  78. out[11] = a[11];
  79. out[12] = a[12];
  80. out[13] = a[13];
  81. out[14] = a[14];
  82. out[15] = a[15];
  83. return out;
  84. }
  85. /**
  86. * Create a new mat4 with the given values
  87. *
  88. * @param {Number} m00 Component in column 0, row 0 position (index 0)
  89. * @param {Number} m01 Component in column 0, row 1 position (index 1)
  90. * @param {Number} m02 Component in column 0, row 2 position (index 2)
  91. * @param {Number} m03 Component in column 0, row 3 position (index 3)
  92. * @param {Number} m10 Component in column 1, row 0 position (index 4)
  93. * @param {Number} m11 Component in column 1, row 1 position (index 5)
  94. * @param {Number} m12 Component in column 1, row 2 position (index 6)
  95. * @param {Number} m13 Component in column 1, row 3 position (index 7)
  96. * @param {Number} m20 Component in column 2, row 0 position (index 8)
  97. * @param {Number} m21 Component in column 2, row 1 position (index 9)
  98. * @param {Number} m22 Component in column 2, row 2 position (index 10)
  99. * @param {Number} m23 Component in column 2, row 3 position (index 11)
  100. * @param {Number} m30 Component in column 3, row 0 position (index 12)
  101. * @param {Number} m31 Component in column 3, row 1 position (index 13)
  102. * @param {Number} m32 Component in column 3, row 2 position (index 14)
  103. * @param {Number} m33 Component in column 3, row 3 position (index 15)
  104. * @returns {mat4} A new mat4
  105. */
  106. export function fromValues(m00, m01, m02, m03, m10, m11, m12, m13, m20, m21, m22, m23, m30, m31, m32, m33) {
  107. var out = new glMatrix.ARRAY_TYPE(16);
  108. out[0] = m00;
  109. out[1] = m01;
  110. out[2] = m02;
  111. out[3] = m03;
  112. out[4] = m10;
  113. out[5] = m11;
  114. out[6] = m12;
  115. out[7] = m13;
  116. out[8] = m20;
  117. out[9] = m21;
  118. out[10] = m22;
  119. out[11] = m23;
  120. out[12] = m30;
  121. out[13] = m31;
  122. out[14] = m32;
  123. out[15] = m33;
  124. return out;
  125. }
  126. /**
  127. * Set the components of a mat4 to the given values
  128. *
  129. * @param {mat4} out the receiving matrix
  130. * @param {Number} m00 Component in column 0, row 0 position (index 0)
  131. * @param {Number} m01 Component in column 0, row 1 position (index 1)
  132. * @param {Number} m02 Component in column 0, row 2 position (index 2)
  133. * @param {Number} m03 Component in column 0, row 3 position (index 3)
  134. * @param {Number} m10 Component in column 1, row 0 position (index 4)
  135. * @param {Number} m11 Component in column 1, row 1 position (index 5)
  136. * @param {Number} m12 Component in column 1, row 2 position (index 6)
  137. * @param {Number} m13 Component in column 1, row 3 position (index 7)
  138. * @param {Number} m20 Component in column 2, row 0 position (index 8)
  139. * @param {Number} m21 Component in column 2, row 1 position (index 9)
  140. * @param {Number} m22 Component in column 2, row 2 position (index 10)
  141. * @param {Number} m23 Component in column 2, row 3 position (index 11)
  142. * @param {Number} m30 Component in column 3, row 0 position (index 12)
  143. * @param {Number} m31 Component in column 3, row 1 position (index 13)
  144. * @param {Number} m32 Component in column 3, row 2 position (index 14)
  145. * @param {Number} m33 Component in column 3, row 3 position (index 15)
  146. * @returns {mat4} out
  147. */
  148. export function set(out, m00, m01, m02, m03, m10, m11, m12, m13, m20, m21, m22, m23, m30, m31, m32, m33) {
  149. out[0] = m00;
  150. out[1] = m01;
  151. out[2] = m02;
  152. out[3] = m03;
  153. out[4] = m10;
  154. out[5] = m11;
  155. out[6] = m12;
  156. out[7] = m13;
  157. out[8] = m20;
  158. out[9] = m21;
  159. out[10] = m22;
  160. out[11] = m23;
  161. out[12] = m30;
  162. out[13] = m31;
  163. out[14] = m32;
  164. out[15] = m33;
  165. return out;
  166. }
  167. /**
  168. * Set a mat4 to the identity matrix
  169. *
  170. * @param {mat4} out the receiving matrix
  171. * @returns {mat4} out
  172. */
  173. export function identity(out) {
  174. out[0] = 1;
  175. out[1] = 0;
  176. out[2] = 0;
  177. out[3] = 0;
  178. out[4] = 0;
  179. out[5] = 1;
  180. out[6] = 0;
  181. out[7] = 0;
  182. out[8] = 0;
  183. out[9] = 0;
  184. out[10] = 1;
  185. out[11] = 0;
  186. out[12] = 0;
  187. out[13] = 0;
  188. out[14] = 0;
  189. out[15] = 1;
  190. return out;
  191. }
  192. /**
  193. * Transpose the values of a mat4
  194. *
  195. * @param {mat4} out the receiving matrix
  196. * @param {ReadonlyMat4} a the source matrix
  197. * @returns {mat4} out
  198. */
  199. export function transpose(out, a) {
  200. // If we are transposing ourselves we can skip a few steps but have to cache some values
  201. if (out === a) {
  202. var a01 = a[1],
  203. a02 = a[2],
  204. a03 = a[3];
  205. var a12 = a[6],
  206. a13 = a[7];
  207. var a23 = a[11];
  208. out[1] = a[4];
  209. out[2] = a[8];
  210. out[3] = a[12];
  211. out[4] = a01;
  212. out[6] = a[9];
  213. out[7] = a[13];
  214. out[8] = a02;
  215. out[9] = a12;
  216. out[11] = a[14];
  217. out[12] = a03;
  218. out[13] = a13;
  219. out[14] = a23;
  220. } else {
  221. out[0] = a[0];
  222. out[1] = a[4];
  223. out[2] = a[8];
  224. out[3] = a[12];
  225. out[4] = a[1];
  226. out[5] = a[5];
  227. out[6] = a[9];
  228. out[7] = a[13];
  229. out[8] = a[2];
  230. out[9] = a[6];
  231. out[10] = a[10];
  232. out[11] = a[14];
  233. out[12] = a[3];
  234. out[13] = a[7];
  235. out[14] = a[11];
  236. out[15] = a[15];
  237. }
  238. return out;
  239. }
  240. /**
  241. * Inverts a mat4
  242. *
  243. * @param {mat4} out the receiving matrix
  244. * @param {ReadonlyMat4} a the source matrix
  245. * @returns {mat4} out
  246. */
  247. export function invert(out, a) {
  248. var a00 = a[0],
  249. a01 = a[1],
  250. a02 = a[2],
  251. a03 = a[3];
  252. var a10 = a[4],
  253. a11 = a[5],
  254. a12 = a[6],
  255. a13 = a[7];
  256. var a20 = a[8],
  257. a21 = a[9],
  258. a22 = a[10],
  259. a23 = a[11];
  260. var a30 = a[12],
  261. a31 = a[13],
  262. a32 = a[14],
  263. a33 = a[15];
  264. var b00 = a00 * a11 - a01 * a10;
  265. var b01 = a00 * a12 - a02 * a10;
  266. var b02 = a00 * a13 - a03 * a10;
  267. var b03 = a01 * a12 - a02 * a11;
  268. var b04 = a01 * a13 - a03 * a11;
  269. var b05 = a02 * a13 - a03 * a12;
  270. var b06 = a20 * a31 - a21 * a30;
  271. var b07 = a20 * a32 - a22 * a30;
  272. var b08 = a20 * a33 - a23 * a30;
  273. var b09 = a21 * a32 - a22 * a31;
  274. var b10 = a21 * a33 - a23 * a31;
  275. var b11 = a22 * a33 - a23 * a32; // Calculate the determinant
  276. var det = b00 * b11 - b01 * b10 + b02 * b09 + b03 * b08 - b04 * b07 + b05 * b06;
  277. if (!det) {
  278. return null;
  279. }
  280. det = 1.0 / det;
  281. out[0] = (a11 * b11 - a12 * b10 + a13 * b09) * det;
  282. out[1] = (a02 * b10 - a01 * b11 - a03 * b09) * det;
  283. out[2] = (a31 * b05 - a32 * b04 + a33 * b03) * det;
  284. out[3] = (a22 * b04 - a21 * b05 - a23 * b03) * det;
  285. out[4] = (a12 * b08 - a10 * b11 - a13 * b07) * det;
  286. out[5] = (a00 * b11 - a02 * b08 + a03 * b07) * det;
  287. out[6] = (a32 * b02 - a30 * b05 - a33 * b01) * det;
  288. out[7] = (a20 * b05 - a22 * b02 + a23 * b01) * det;
  289. out[8] = (a10 * b10 - a11 * b08 + a13 * b06) * det;
  290. out[9] = (a01 * b08 - a00 * b10 - a03 * b06) * det;
  291. out[10] = (a30 * b04 - a31 * b02 + a33 * b00) * det;
  292. out[11] = (a21 * b02 - a20 * b04 - a23 * b00) * det;
  293. out[12] = (a11 * b07 - a10 * b09 - a12 * b06) * det;
  294. out[13] = (a00 * b09 - a01 * b07 + a02 * b06) * det;
  295. out[14] = (a31 * b01 - a30 * b03 - a32 * b00) * det;
  296. out[15] = (a20 * b03 - a21 * b01 + a22 * b00) * det;
  297. return out;
  298. }
  299. /**
  300. * Calculates the adjugate of a mat4
  301. *
  302. * @param {mat4} out the receiving matrix
  303. * @param {ReadonlyMat4} a the source matrix
  304. * @returns {mat4} out
  305. */
  306. export function adjoint(out, a) {
  307. var a00 = a[0],
  308. a01 = a[1],
  309. a02 = a[2],
  310. a03 = a[3];
  311. var a10 = a[4],
  312. a11 = a[5],
  313. a12 = a[6],
  314. a13 = a[7];
  315. var a20 = a[8],
  316. a21 = a[9],
  317. a22 = a[10],
  318. a23 = a[11];
  319. var a30 = a[12],
  320. a31 = a[13],
  321. a32 = a[14],
  322. a33 = a[15];
  323. out[0] = a11 * (a22 * a33 - a23 * a32) - a21 * (a12 * a33 - a13 * a32) + a31 * (a12 * a23 - a13 * a22);
  324. out[1] = -(a01 * (a22 * a33 - a23 * a32) - a21 * (a02 * a33 - a03 * a32) + a31 * (a02 * a23 - a03 * a22));
  325. out[2] = a01 * (a12 * a33 - a13 * a32) - a11 * (a02 * a33 - a03 * a32) + a31 * (a02 * a13 - a03 * a12);
  326. out[3] = -(a01 * (a12 * a23 - a13 * a22) - a11 * (a02 * a23 - a03 * a22) + a21 * (a02 * a13 - a03 * a12));
  327. out[4] = -(a10 * (a22 * a33 - a23 * a32) - a20 * (a12 * a33 - a13 * a32) + a30 * (a12 * a23 - a13 * a22));
  328. out[5] = a00 * (a22 * a33 - a23 * a32) - a20 * (a02 * a33 - a03 * a32) + a30 * (a02 * a23 - a03 * a22);
  329. out[6] = -(a00 * (a12 * a33 - a13 * a32) - a10 * (a02 * a33 - a03 * a32) + a30 * (a02 * a13 - a03 * a12));
  330. out[7] = a00 * (a12 * a23 - a13 * a22) - a10 * (a02 * a23 - a03 * a22) + a20 * (a02 * a13 - a03 * a12);
  331. out[8] = a10 * (a21 * a33 - a23 * a31) - a20 * (a11 * a33 - a13 * a31) + a30 * (a11 * a23 - a13 * a21);
  332. out[9] = -(a00 * (a21 * a33 - a23 * a31) - a20 * (a01 * a33 - a03 * a31) + a30 * (a01 * a23 - a03 * a21));
  333. out[10] = a00 * (a11 * a33 - a13 * a31) - a10 * (a01 * a33 - a03 * a31) + a30 * (a01 * a13 - a03 * a11);
  334. out[11] = -(a00 * (a11 * a23 - a13 * a21) - a10 * (a01 * a23 - a03 * a21) + a20 * (a01 * a13 - a03 * a11));
  335. out[12] = -(a10 * (a21 * a32 - a22 * a31) - a20 * (a11 * a32 - a12 * a31) + a30 * (a11 * a22 - a12 * a21));
  336. out[13] = a00 * (a21 * a32 - a22 * a31) - a20 * (a01 * a32 - a02 * a31) + a30 * (a01 * a22 - a02 * a21);
  337. out[14] = -(a00 * (a11 * a32 - a12 * a31) - a10 * (a01 * a32 - a02 * a31) + a30 * (a01 * a12 - a02 * a11));
  338. out[15] = a00 * (a11 * a22 - a12 * a21) - a10 * (a01 * a22 - a02 * a21) + a20 * (a01 * a12 - a02 * a11);
  339. return out;
  340. }
  341. /**
  342. * Calculates the determinant of a mat4
  343. *
  344. * @param {ReadonlyMat4} a the source matrix
  345. * @returns {Number} determinant of a
  346. */
  347. export function determinant(a) {
  348. var a00 = a[0],
  349. a01 = a[1],
  350. a02 = a[2],
  351. a03 = a[3];
  352. var a10 = a[4],
  353. a11 = a[5],
  354. a12 = a[6],
  355. a13 = a[7];
  356. var a20 = a[8],
  357. a21 = a[9],
  358. a22 = a[10],
  359. a23 = a[11];
  360. var a30 = a[12],
  361. a31 = a[13],
  362. a32 = a[14],
  363. a33 = a[15];
  364. var b00 = a00 * a11 - a01 * a10;
  365. var b01 = a00 * a12 - a02 * a10;
  366. var b02 = a00 * a13 - a03 * a10;
  367. var b03 = a01 * a12 - a02 * a11;
  368. var b04 = a01 * a13 - a03 * a11;
  369. var b05 = a02 * a13 - a03 * a12;
  370. var b06 = a20 * a31 - a21 * a30;
  371. var b07 = a20 * a32 - a22 * a30;
  372. var b08 = a20 * a33 - a23 * a30;
  373. var b09 = a21 * a32 - a22 * a31;
  374. var b10 = a21 * a33 - a23 * a31;
  375. var b11 = a22 * a33 - a23 * a32; // Calculate the determinant
  376. return b00 * b11 - b01 * b10 + b02 * b09 + b03 * b08 - b04 * b07 + b05 * b06;
  377. }
  378. /**
  379. * Multiplies two mat4s
  380. *
  381. * @param {mat4} out the receiving matrix
  382. * @param {ReadonlyMat4} a the first operand
  383. * @param {ReadonlyMat4} b the second operand
  384. * @returns {mat4} out
  385. */
  386. export function multiply(out, a, b) {
  387. var a00 = a[0],
  388. a01 = a[1],
  389. a02 = a[2],
  390. a03 = a[3];
  391. var a10 = a[4],
  392. a11 = a[5],
  393. a12 = a[6],
  394. a13 = a[7];
  395. var a20 = a[8],
  396. a21 = a[9],
  397. a22 = a[10],
  398. a23 = a[11];
  399. var a30 = a[12],
  400. a31 = a[13],
  401. a32 = a[14],
  402. a33 = a[15]; // Cache only the current line of the second matrix
  403. var b0 = b[0],
  404. b1 = b[1],
  405. b2 = b[2],
  406. b3 = b[3];
  407. out[0] = b0 * a00 + b1 * a10 + b2 * a20 + b3 * a30;
  408. out[1] = b0 * a01 + b1 * a11 + b2 * a21 + b3 * a31;
  409. out[2] = b0 * a02 + b1 * a12 + b2 * a22 + b3 * a32;
  410. out[3] = b0 * a03 + b1 * a13 + b2 * a23 + b3 * a33;
  411. b0 = b[4];
  412. b1 = b[5];
  413. b2 = b[6];
  414. b3 = b[7];
  415. out[4] = b0 * a00 + b1 * a10 + b2 * a20 + b3 * a30;
  416. out[5] = b0 * a01 + b1 * a11 + b2 * a21 + b3 * a31;
  417. out[6] = b0 * a02 + b1 * a12 + b2 * a22 + b3 * a32;
  418. out[7] = b0 * a03 + b1 * a13 + b2 * a23 + b3 * a33;
  419. b0 = b[8];
  420. b1 = b[9];
  421. b2 = b[10];
  422. b3 = b[11];
  423. out[8] = b0 * a00 + b1 * a10 + b2 * a20 + b3 * a30;
  424. out[9] = b0 * a01 + b1 * a11 + b2 * a21 + b3 * a31;
  425. out[10] = b0 * a02 + b1 * a12 + b2 * a22 + b3 * a32;
  426. out[11] = b0 * a03 + b1 * a13 + b2 * a23 + b3 * a33;
  427. b0 = b[12];
  428. b1 = b[13];
  429. b2 = b[14];
  430. b3 = b[15];
  431. out[12] = b0 * a00 + b1 * a10 + b2 * a20 + b3 * a30;
  432. out[13] = b0 * a01 + b1 * a11 + b2 * a21 + b3 * a31;
  433. out[14] = b0 * a02 + b1 * a12 + b2 * a22 + b3 * a32;
  434. out[15] = b0 * a03 + b1 * a13 + b2 * a23 + b3 * a33;
  435. return out;
  436. }
  437. /**
  438. * Translate a mat4 by the given vector
  439. *
  440. * @param {mat4} out the receiving matrix
  441. * @param {ReadonlyMat4} a the matrix to translate
  442. * @param {ReadonlyVec3} v vector to translate by
  443. * @returns {mat4} out
  444. */
  445. export function translate(out, a, v) {
  446. var x = v[0],
  447. y = v[1],
  448. z = v[2];
  449. var a00, a01, a02, a03;
  450. var a10, a11, a12, a13;
  451. var a20, a21, a22, a23;
  452. if (a === out) {
  453. out[12] = a[0] * x + a[4] * y + a[8] * z + a[12];
  454. out[13] = a[1] * x + a[5] * y + a[9] * z + a[13];
  455. out[14] = a[2] * x + a[6] * y + a[10] * z + a[14];
  456. out[15] = a[3] * x + a[7] * y + a[11] * z + a[15];
  457. } else {
  458. a00 = a[0];
  459. a01 = a[1];
  460. a02 = a[2];
  461. a03 = a[3];
  462. a10 = a[4];
  463. a11 = a[5];
  464. a12 = a[6];
  465. a13 = a[7];
  466. a20 = a[8];
  467. a21 = a[9];
  468. a22 = a[10];
  469. a23 = a[11];
  470. out[0] = a00;
  471. out[1] = a01;
  472. out[2] = a02;
  473. out[3] = a03;
  474. out[4] = a10;
  475. out[5] = a11;
  476. out[6] = a12;
  477. out[7] = a13;
  478. out[8] = a20;
  479. out[9] = a21;
  480. out[10] = a22;
  481. out[11] = a23;
  482. out[12] = a00 * x + a10 * y + a20 * z + a[12];
  483. out[13] = a01 * x + a11 * y + a21 * z + a[13];
  484. out[14] = a02 * x + a12 * y + a22 * z + a[14];
  485. out[15] = a03 * x + a13 * y + a23 * z + a[15];
  486. }
  487. return out;
  488. }
  489. /**
  490. * Scales the mat4 by the dimensions in the given vec3 not using vectorization
  491. *
  492. * @param {mat4} out the receiving matrix
  493. * @param {ReadonlyMat4} a the matrix to scale
  494. * @param {ReadonlyVec3} v the vec3 to scale the matrix by
  495. * @returns {mat4} out
  496. **/
  497. export function scale(out, a, v) {
  498. var x = v[0],
  499. y = v[1],
  500. z = v[2];
  501. out[0] = a[0] * x;
  502. out[1] = a[1] * x;
  503. out[2] = a[2] * x;
  504. out[3] = a[3] * x;
  505. out[4] = a[4] * y;
  506. out[5] = a[5] * y;
  507. out[6] = a[6] * y;
  508. out[7] = a[7] * y;
  509. out[8] = a[8] * z;
  510. out[9] = a[9] * z;
  511. out[10] = a[10] * z;
  512. out[11] = a[11] * z;
  513. out[12] = a[12];
  514. out[13] = a[13];
  515. out[14] = a[14];
  516. out[15] = a[15];
  517. return out;
  518. }
  519. /**
  520. * Rotates a mat4 by the given angle around the given axis
  521. *
  522. * @param {mat4} out the receiving matrix
  523. * @param {ReadonlyMat4} a the matrix to rotate
  524. * @param {Number} rad the angle to rotate the matrix by
  525. * @param {ReadonlyVec3} axis the axis to rotate around
  526. * @returns {mat4} out
  527. */
  528. export function rotate(out, a, rad, axis) {
  529. var x = axis[0],
  530. y = axis[1],
  531. z = axis[2];
  532. var len = Math.hypot(x, y, z);
  533. var s, c, t;
  534. var a00, a01, a02, a03;
  535. var a10, a11, a12, a13;
  536. var a20, a21, a22, a23;
  537. var b00, b01, b02;
  538. var b10, b11, b12;
  539. var b20, b21, b22;
  540. if (len < glMatrix.EPSILON) {
  541. return null;
  542. }
  543. len = 1 / len;
  544. x *= len;
  545. y *= len;
  546. z *= len;
  547. s = Math.sin(rad);
  548. c = Math.cos(rad);
  549. t = 1 - c;
  550. a00 = a[0];
  551. a01 = a[1];
  552. a02 = a[2];
  553. a03 = a[3];
  554. a10 = a[4];
  555. a11 = a[5];
  556. a12 = a[6];
  557. a13 = a[7];
  558. a20 = a[8];
  559. a21 = a[9];
  560. a22 = a[10];
  561. a23 = a[11]; // Construct the elements of the rotation matrix
  562. b00 = x * x * t + c;
  563. b01 = y * x * t + z * s;
  564. b02 = z * x * t - y * s;
  565. b10 = x * y * t - z * s;
  566. b11 = y * y * t + c;
  567. b12 = z * y * t + x * s;
  568. b20 = x * z * t + y * s;
  569. b21 = y * z * t - x * s;
  570. b22 = z * z * t + c; // Perform rotation-specific matrix multiplication
  571. out[0] = a00 * b00 + a10 * b01 + a20 * b02;
  572. out[1] = a01 * b00 + a11 * b01 + a21 * b02;
  573. out[2] = a02 * b00 + a12 * b01 + a22 * b02;
  574. out[3] = a03 * b00 + a13 * b01 + a23 * b02;
  575. out[4] = a00 * b10 + a10 * b11 + a20 * b12;
  576. out[5] = a01 * b10 + a11 * b11 + a21 * b12;
  577. out[6] = a02 * b10 + a12 * b11 + a22 * b12;
  578. out[7] = a03 * b10 + a13 * b11 + a23 * b12;
  579. out[8] = a00 * b20 + a10 * b21 + a20 * b22;
  580. out[9] = a01 * b20 + a11 * b21 + a21 * b22;
  581. out[10] = a02 * b20 + a12 * b21 + a22 * b22;
  582. out[11] = a03 * b20 + a13 * b21 + a23 * b22;
  583. if (a !== out) {
  584. // If the source and destination differ, copy the unchanged last row
  585. out[12] = a[12];
  586. out[13] = a[13];
  587. out[14] = a[14];
  588. out[15] = a[15];
  589. }
  590. return out;
  591. }
  592. /**
  593. * Rotates a matrix by the given angle around the X axis
  594. *
  595. * @param {mat4} out the receiving matrix
  596. * @param {ReadonlyMat4} a the matrix to rotate
  597. * @param {Number} rad the angle to rotate the matrix by
  598. * @returns {mat4} out
  599. */
  600. export function rotateX(out, a, rad) {
  601. var s = Math.sin(rad);
  602. var c = Math.cos(rad);
  603. var a10 = a[4];
  604. var a11 = a[5];
  605. var a12 = a[6];
  606. var a13 = a[7];
  607. var a20 = a[8];
  608. var a21 = a[9];
  609. var a22 = a[10];
  610. var a23 = a[11];
  611. if (a !== out) {
  612. // If the source and destination differ, copy the unchanged rows
  613. out[0] = a[0];
  614. out[1] = a[1];
  615. out[2] = a[2];
  616. out[3] = a[3];
  617. out[12] = a[12];
  618. out[13] = a[13];
  619. out[14] = a[14];
  620. out[15] = a[15];
  621. } // Perform axis-specific matrix multiplication
  622. out[4] = a10 * c + a20 * s;
  623. out[5] = a11 * c + a21 * s;
  624. out[6] = a12 * c + a22 * s;
  625. out[7] = a13 * c + a23 * s;
  626. out[8] = a20 * c - a10 * s;
  627. out[9] = a21 * c - a11 * s;
  628. out[10] = a22 * c - a12 * s;
  629. out[11] = a23 * c - a13 * s;
  630. return out;
  631. }
  632. /**
  633. * Rotates a matrix by the given angle around the Y axis
  634. *
  635. * @param {mat4} out the receiving matrix
  636. * @param {ReadonlyMat4} a the matrix to rotate
  637. * @param {Number} rad the angle to rotate the matrix by
  638. * @returns {mat4} out
  639. */
  640. export function rotateY(out, a, rad) {
  641. var s = Math.sin(rad);
  642. var c = Math.cos(rad);
  643. var a00 = a[0];
  644. var a01 = a[1];
  645. var a02 = a[2];
  646. var a03 = a[3];
  647. var a20 = a[8];
  648. var a21 = a[9];
  649. var a22 = a[10];
  650. var a23 = a[11];
  651. if (a !== out) {
  652. // If the source and destination differ, copy the unchanged rows
  653. out[4] = a[4];
  654. out[5] = a[5];
  655. out[6] = a[6];
  656. out[7] = a[7];
  657. out[12] = a[12];
  658. out[13] = a[13];
  659. out[14] = a[14];
  660. out[15] = a[15];
  661. } // Perform axis-specific matrix multiplication
  662. out[0] = a00 * c - a20 * s;
  663. out[1] = a01 * c - a21 * s;
  664. out[2] = a02 * c - a22 * s;
  665. out[3] = a03 * c - a23 * s;
  666. out[8] = a00 * s + a20 * c;
  667. out[9] = a01 * s + a21 * c;
  668. out[10] = a02 * s + a22 * c;
  669. out[11] = a03 * s + a23 * c;
  670. return out;
  671. }
  672. /**
  673. * Rotates a matrix by the given angle around the Z axis
  674. *
  675. * @param {mat4} out the receiving matrix
  676. * @param {ReadonlyMat4} a the matrix to rotate
  677. * @param {Number} rad the angle to rotate the matrix by
  678. * @returns {mat4} out
  679. */
  680. export function rotateZ(out, a, rad) {
  681. var s = Math.sin(rad);
  682. var c = Math.cos(rad);
  683. var a00 = a[0];
  684. var a01 = a[1];
  685. var a02 = a[2];
  686. var a03 = a[3];
  687. var a10 = a[4];
  688. var a11 = a[5];
  689. var a12 = a[6];
  690. var a13 = a[7];
  691. if (a !== out) {
  692. // If the source and destination differ, copy the unchanged last row
  693. out[8] = a[8];
  694. out[9] = a[9];
  695. out[10] = a[10];
  696. out[11] = a[11];
  697. out[12] = a[12];
  698. out[13] = a[13];
  699. out[14] = a[14];
  700. out[15] = a[15];
  701. } // Perform axis-specific matrix multiplication
  702. out[0] = a00 * c + a10 * s;
  703. out[1] = a01 * c + a11 * s;
  704. out[2] = a02 * c + a12 * s;
  705. out[3] = a03 * c + a13 * s;
  706. out[4] = a10 * c - a00 * s;
  707. out[5] = a11 * c - a01 * s;
  708. out[6] = a12 * c - a02 * s;
  709. out[7] = a13 * c - a03 * s;
  710. return out;
  711. }
  712. /**
  713. * Creates a matrix from a vector translation
  714. * This is equivalent to (but much faster than):
  715. *
  716. * mat4.identity(dest);
  717. * mat4.translate(dest, dest, vec);
  718. *
  719. * @param {mat4} out mat4 receiving operation result
  720. * @param {ReadonlyVec3} v Translation vector
  721. * @returns {mat4} out
  722. */
  723. export function fromTranslation(out, v) {
  724. out[0] = 1;
  725. out[1] = 0;
  726. out[2] = 0;
  727. out[3] = 0;
  728. out[4] = 0;
  729. out[5] = 1;
  730. out[6] = 0;
  731. out[7] = 0;
  732. out[8] = 0;
  733. out[9] = 0;
  734. out[10] = 1;
  735. out[11] = 0;
  736. out[12] = v[0];
  737. out[13] = v[1];
  738. out[14] = v[2];
  739. out[15] = 1;
  740. return out;
  741. }
  742. /**
  743. * Creates a matrix from a vector scaling
  744. * This is equivalent to (but much faster than):
  745. *
  746. * mat4.identity(dest);
  747. * mat4.scale(dest, dest, vec);
  748. *
  749. * @param {mat4} out mat4 receiving operation result
  750. * @param {ReadonlyVec3} v Scaling vector
  751. * @returns {mat4} out
  752. */
  753. export function fromScaling(out, v) {
  754. out[0] = v[0];
  755. out[1] = 0;
  756. out[2] = 0;
  757. out[3] = 0;
  758. out[4] = 0;
  759. out[5] = v[1];
  760. out[6] = 0;
  761. out[7] = 0;
  762. out[8] = 0;
  763. out[9] = 0;
  764. out[10] = v[2];
  765. out[11] = 0;
  766. out[12] = 0;
  767. out[13] = 0;
  768. out[14] = 0;
  769. out[15] = 1;
  770. return out;
  771. }
  772. /**
  773. * Creates a matrix from a given angle around a given axis
  774. * This is equivalent to (but much faster than):
  775. *
  776. * mat4.identity(dest);
  777. * mat4.rotate(dest, dest, rad, axis);
  778. *
  779. * @param {mat4} out mat4 receiving operation result
  780. * @param {Number} rad the angle to rotate the matrix by
  781. * @param {ReadonlyVec3} axis the axis to rotate around
  782. * @returns {mat4} out
  783. */
  784. export function fromRotation(out, rad, axis) {
  785. var x = axis[0],
  786. y = axis[1],
  787. z = axis[2];
  788. var len = Math.hypot(x, y, z);
  789. var s, c, t;
  790. if (len < glMatrix.EPSILON) {
  791. return null;
  792. }
  793. len = 1 / len;
  794. x *= len;
  795. y *= len;
  796. z *= len;
  797. s = Math.sin(rad);
  798. c = Math.cos(rad);
  799. t = 1 - c; // Perform rotation-specific matrix multiplication
  800. out[0] = x * x * t + c;
  801. out[1] = y * x * t + z * s;
  802. out[2] = z * x * t - y * s;
  803. out[3] = 0;
  804. out[4] = x * y * t - z * s;
  805. out[5] = y * y * t + c;
  806. out[6] = z * y * t + x * s;
  807. out[7] = 0;
  808. out[8] = x * z * t + y * s;
  809. out[9] = y * z * t - x * s;
  810. out[10] = z * z * t + c;
  811. out[11] = 0;
  812. out[12] = 0;
  813. out[13] = 0;
  814. out[14] = 0;
  815. out[15] = 1;
  816. return out;
  817. }
  818. /**
  819. * Creates a matrix from the given angle around the X axis
  820. * This is equivalent to (but much faster than):
  821. *
  822. * mat4.identity(dest);
  823. * mat4.rotateX(dest, dest, rad);
  824. *
  825. * @param {mat4} out mat4 receiving operation result
  826. * @param {Number} rad the angle to rotate the matrix by
  827. * @returns {mat4} out
  828. */
  829. export function fromXRotation(out, rad) {
  830. var s = Math.sin(rad);
  831. var c = Math.cos(rad); // Perform axis-specific matrix multiplication
  832. out[0] = 1;
  833. out[1] = 0;
  834. out[2] = 0;
  835. out[3] = 0;
  836. out[4] = 0;
  837. out[5] = c;
  838. out[6] = s;
  839. out[7] = 0;
  840. out[8] = 0;
  841. out[9] = -s;
  842. out[10] = c;
  843. out[11] = 0;
  844. out[12] = 0;
  845. out[13] = 0;
  846. out[14] = 0;
  847. out[15] = 1;
  848. return out;
  849. }
  850. /**
  851. * Creates a matrix from the given angle around the Y axis
  852. * This is equivalent to (but much faster than):
  853. *
  854. * mat4.identity(dest);
  855. * mat4.rotateY(dest, dest, rad);
  856. *
  857. * @param {mat4} out mat4 receiving operation result
  858. * @param {Number} rad the angle to rotate the matrix by
  859. * @returns {mat4} out
  860. */
  861. export function fromYRotation(out, rad) {
  862. var s = Math.sin(rad);
  863. var c = Math.cos(rad); // Perform axis-specific matrix multiplication
  864. out[0] = c;
  865. out[1] = 0;
  866. out[2] = -s;
  867. out[3] = 0;
  868. out[4] = 0;
  869. out[5] = 1;
  870. out[6] = 0;
  871. out[7] = 0;
  872. out[8] = s;
  873. out[9] = 0;
  874. out[10] = c;
  875. out[11] = 0;
  876. out[12] = 0;
  877. out[13] = 0;
  878. out[14] = 0;
  879. out[15] = 1;
  880. return out;
  881. }
  882. /**
  883. * Creates a matrix from the given angle around the Z axis
  884. * This is equivalent to (but much faster than):
  885. *
  886. * mat4.identity(dest);
  887. * mat4.rotateZ(dest, dest, rad);
  888. *
  889. * @param {mat4} out mat4 receiving operation result
  890. * @param {Number} rad the angle to rotate the matrix by
  891. * @returns {mat4} out
  892. */
  893. export function fromZRotation(out, rad) {
  894. var s = Math.sin(rad);
  895. var c = Math.cos(rad); // Perform axis-specific matrix multiplication
  896. out[0] = c;
  897. out[1] = s;
  898. out[2] = 0;
  899. out[3] = 0;
  900. out[4] = -s;
  901. out[5] = c;
  902. out[6] = 0;
  903. out[7] = 0;
  904. out[8] = 0;
  905. out[9] = 0;
  906. out[10] = 1;
  907. out[11] = 0;
  908. out[12] = 0;
  909. out[13] = 0;
  910. out[14] = 0;
  911. out[15] = 1;
  912. return out;
  913. }
  914. /**
  915. * Creates a matrix from a quaternion rotation and vector translation
  916. * This is equivalent to (but much faster than):
  917. *
  918. * mat4.identity(dest);
  919. * mat4.translate(dest, vec);
  920. * let quatMat = mat4.create();
  921. * quat4.toMat4(quat, quatMat);
  922. * mat4.multiply(dest, quatMat);
  923. *
  924. * @param {mat4} out mat4 receiving operation result
  925. * @param {quat4} q Rotation quaternion
  926. * @param {ReadonlyVec3} v Translation vector
  927. * @returns {mat4} out
  928. */
  929. export function fromRotationTranslation(out, q, v) {
  930. // Quaternion math
  931. var x = q[0],
  932. y = q[1],
  933. z = q[2],
  934. w = q[3];
  935. var x2 = x + x;
  936. var y2 = y + y;
  937. var z2 = z + z;
  938. var xx = x * x2;
  939. var xy = x * y2;
  940. var xz = x * z2;
  941. var yy = y * y2;
  942. var yz = y * z2;
  943. var zz = z * z2;
  944. var wx = w * x2;
  945. var wy = w * y2;
  946. var wz = w * z2;
  947. out[0] = 1 - (yy + zz);
  948. out[1] = xy + wz;
  949. out[2] = xz - wy;
  950. out[3] = 0;
  951. out[4] = xy - wz;
  952. out[5] = 1 - (xx + zz);
  953. out[6] = yz + wx;
  954. out[7] = 0;
  955. out[8] = xz + wy;
  956. out[9] = yz - wx;
  957. out[10] = 1 - (xx + yy);
  958. out[11] = 0;
  959. out[12] = v[0];
  960. out[13] = v[1];
  961. out[14] = v[2];
  962. out[15] = 1;
  963. return out;
  964. }
  965. /**
  966. * Creates a new mat4 from a dual quat.
  967. *
  968. * @param {mat4} out Matrix
  969. * @param {ReadonlyQuat2} a Dual Quaternion
  970. * @returns {mat4} mat4 receiving operation result
  971. */
  972. export function fromQuat2(out, a) {
  973. var translation = new glMatrix.ARRAY_TYPE(3);
  974. var bx = -a[0],
  975. by = -a[1],
  976. bz = -a[2],
  977. bw = a[3],
  978. ax = a[4],
  979. ay = a[5],
  980. az = a[6],
  981. aw = a[7];
  982. var magnitude = bx * bx + by * by + bz * bz + bw * bw; //Only scale if it makes sense
  983. if (magnitude > 0) {
  984. translation[0] = (ax * bw + aw * bx + ay * bz - az * by) * 2 / magnitude;
  985. translation[1] = (ay * bw + aw * by + az * bx - ax * bz) * 2 / magnitude;
  986. translation[2] = (az * bw + aw * bz + ax * by - ay * bx) * 2 / magnitude;
  987. } else {
  988. translation[0] = (ax * bw + aw * bx + ay * bz - az * by) * 2;
  989. translation[1] = (ay * bw + aw * by + az * bx - ax * bz) * 2;
  990. translation[2] = (az * bw + aw * bz + ax * by - ay * bx) * 2;
  991. }
  992. fromRotationTranslation(out, a, translation);
  993. return out;
  994. }
  995. /**
  996. * Returns the translation vector component of a transformation
  997. * matrix. If a matrix is built with fromRotationTranslation,
  998. * the returned vector will be the same as the translation vector
  999. * originally supplied.
  1000. * @param {vec3} out Vector to receive translation component
  1001. * @param {ReadonlyMat4} mat Matrix to be decomposed (input)
  1002. * @return {vec3} out
  1003. */
  1004. export function getTranslation(out, mat) {
  1005. out[0] = mat[12];
  1006. out[1] = mat[13];
  1007. out[2] = mat[14];
  1008. return out;
  1009. }
  1010. /**
  1011. * Returns the scaling factor component of a transformation
  1012. * matrix. If a matrix is built with fromRotationTranslationScale
  1013. * with a normalized Quaternion paramter, the returned vector will be
  1014. * the same as the scaling vector
  1015. * originally supplied.
  1016. * @param {vec3} out Vector to receive scaling factor component
  1017. * @param {ReadonlyMat4} mat Matrix to be decomposed (input)
  1018. * @return {vec3} out
  1019. */
  1020. export function getScaling(out, mat) {
  1021. var m11 = mat[0];
  1022. var m12 = mat[1];
  1023. var m13 = mat[2];
  1024. var m21 = mat[4];
  1025. var m22 = mat[5];
  1026. var m23 = mat[6];
  1027. var m31 = mat[8];
  1028. var m32 = mat[9];
  1029. var m33 = mat[10];
  1030. out[0] = Math.hypot(m11, m12, m13);
  1031. out[1] = Math.hypot(m21, m22, m23);
  1032. out[2] = Math.hypot(m31, m32, m33);
  1033. return out;
  1034. }
  1035. /**
  1036. * Returns a quaternion representing the rotational component
  1037. * of a transformation matrix. If a matrix is built with
  1038. * fromRotationTranslation, the returned quaternion will be the
  1039. * same as the quaternion originally supplied.
  1040. * @param {quat} out Quaternion to receive the rotation component
  1041. * @param {ReadonlyMat4} mat Matrix to be decomposed (input)
  1042. * @return {quat} out
  1043. */
  1044. export function getRotation(out, mat) {
  1045. var scaling = new glMatrix.ARRAY_TYPE(3);
  1046. getScaling(scaling, mat);
  1047. var is1 = 1 / scaling[0];
  1048. var is2 = 1 / scaling[1];
  1049. var is3 = 1 / scaling[2];
  1050. var sm11 = mat[0] * is1;
  1051. var sm12 = mat[1] * is2;
  1052. var sm13 = mat[2] * is3;
  1053. var sm21 = mat[4] * is1;
  1054. var sm22 = mat[5] * is2;
  1055. var sm23 = mat[6] * is3;
  1056. var sm31 = mat[8] * is1;
  1057. var sm32 = mat[9] * is2;
  1058. var sm33 = mat[10] * is3;
  1059. var trace = sm11 + sm22 + sm33;
  1060. var S = 0;
  1061. if (trace > 0) {
  1062. S = Math.sqrt(trace + 1.0) * 2;
  1063. out[3] = 0.25 * S;
  1064. out[0] = (sm23 - sm32) / S;
  1065. out[1] = (sm31 - sm13) / S;
  1066. out[2] = (sm12 - sm21) / S;
  1067. } else if (sm11 > sm22 && sm11 > sm33) {
  1068. S = Math.sqrt(1.0 + sm11 - sm22 - sm33) * 2;
  1069. out[3] = (sm23 - sm32) / S;
  1070. out[0] = 0.25 * S;
  1071. out[1] = (sm12 + sm21) / S;
  1072. out[2] = (sm31 + sm13) / S;
  1073. } else if (sm22 > sm33) {
  1074. S = Math.sqrt(1.0 + sm22 - sm11 - sm33) * 2;
  1075. out[3] = (sm31 - sm13) / S;
  1076. out[0] = (sm12 + sm21) / S;
  1077. out[1] = 0.25 * S;
  1078. out[2] = (sm23 + sm32) / S;
  1079. } else {
  1080. S = Math.sqrt(1.0 + sm33 - sm11 - sm22) * 2;
  1081. out[3] = (sm12 - sm21) / S;
  1082. out[0] = (sm31 + sm13) / S;
  1083. out[1] = (sm23 + sm32) / S;
  1084. out[2] = 0.25 * S;
  1085. }
  1086. return out;
  1087. }
  1088. /**
  1089. * Creates a matrix from a quaternion rotation, vector translation and vector scale
  1090. * This is equivalent to (but much faster than):
  1091. *
  1092. * mat4.identity(dest);
  1093. * mat4.translate(dest, vec);
  1094. * let quatMat = mat4.create();
  1095. * quat4.toMat4(quat, quatMat);
  1096. * mat4.multiply(dest, quatMat);
  1097. * mat4.scale(dest, scale)
  1098. *
  1099. * @param {mat4} out mat4 receiving operation result
  1100. * @param {quat4} q Rotation quaternion
  1101. * @param {ReadonlyVec3} v Translation vector
  1102. * @param {ReadonlyVec3} s Scaling vector
  1103. * @returns {mat4} out
  1104. */
  1105. export function fromRotationTranslationScale(out, q, v, s) {
  1106. // Quaternion math
  1107. var x = q[0],
  1108. y = q[1],
  1109. z = q[2],
  1110. w = q[3];
  1111. var x2 = x + x;
  1112. var y2 = y + y;
  1113. var z2 = z + z;
  1114. var xx = x * x2;
  1115. var xy = x * y2;
  1116. var xz = x * z2;
  1117. var yy = y * y2;
  1118. var yz = y * z2;
  1119. var zz = z * z2;
  1120. var wx = w * x2;
  1121. var wy = w * y2;
  1122. var wz = w * z2;
  1123. var sx = s[0];
  1124. var sy = s[1];
  1125. var sz = s[2];
  1126. out[0] = (1 - (yy + zz)) * sx;
  1127. out[1] = (xy + wz) * sx;
  1128. out[2] = (xz - wy) * sx;
  1129. out[3] = 0;
  1130. out[4] = (xy - wz) * sy;
  1131. out[5] = (1 - (xx + zz)) * sy;
  1132. out[6] = (yz + wx) * sy;
  1133. out[7] = 0;
  1134. out[8] = (xz + wy) * sz;
  1135. out[9] = (yz - wx) * sz;
  1136. out[10] = (1 - (xx + yy)) * sz;
  1137. out[11] = 0;
  1138. out[12] = v[0];
  1139. out[13] = v[1];
  1140. out[14] = v[2];
  1141. out[15] = 1;
  1142. return out;
  1143. }
  1144. /**
  1145. * Creates a matrix from a quaternion rotation, vector translation and vector scale, rotating and scaling around the given origin
  1146. * This is equivalent to (but much faster than):
  1147. *
  1148. * mat4.identity(dest);
  1149. * mat4.translate(dest, vec);
  1150. * mat4.translate(dest, origin);
  1151. * let quatMat = mat4.create();
  1152. * quat4.toMat4(quat, quatMat);
  1153. * mat4.multiply(dest, quatMat);
  1154. * mat4.scale(dest, scale)
  1155. * mat4.translate(dest, negativeOrigin);
  1156. *
  1157. * @param {mat4} out mat4 receiving operation result
  1158. * @param {quat4} q Rotation quaternion
  1159. * @param {ReadonlyVec3} v Translation vector
  1160. * @param {ReadonlyVec3} s Scaling vector
  1161. * @param {ReadonlyVec3} o The origin vector around which to scale and rotate
  1162. * @returns {mat4} out
  1163. */
  1164. export function fromRotationTranslationScaleOrigin(out, q, v, s, o) {
  1165. // Quaternion math
  1166. var x = q[0],
  1167. y = q[1],
  1168. z = q[2],
  1169. w = q[3];
  1170. var x2 = x + x;
  1171. var y2 = y + y;
  1172. var z2 = z + z;
  1173. var xx = x * x2;
  1174. var xy = x * y2;
  1175. var xz = x * z2;
  1176. var yy = y * y2;
  1177. var yz = y * z2;
  1178. var zz = z * z2;
  1179. var wx = w * x2;
  1180. var wy = w * y2;
  1181. var wz = w * z2;
  1182. var sx = s[0];
  1183. var sy = s[1];
  1184. var sz = s[2];
  1185. var ox = o[0];
  1186. var oy = o[1];
  1187. var oz = o[2];
  1188. var out0 = (1 - (yy + zz)) * sx;
  1189. var out1 = (xy + wz) * sx;
  1190. var out2 = (xz - wy) * sx;
  1191. var out4 = (xy - wz) * sy;
  1192. var out5 = (1 - (xx + zz)) * sy;
  1193. var out6 = (yz + wx) * sy;
  1194. var out8 = (xz + wy) * sz;
  1195. var out9 = (yz - wx) * sz;
  1196. var out10 = (1 - (xx + yy)) * sz;
  1197. out[0] = out0;
  1198. out[1] = out1;
  1199. out[2] = out2;
  1200. out[3] = 0;
  1201. out[4] = out4;
  1202. out[5] = out5;
  1203. out[6] = out6;
  1204. out[7] = 0;
  1205. out[8] = out8;
  1206. out[9] = out9;
  1207. out[10] = out10;
  1208. out[11] = 0;
  1209. out[12] = v[0] + ox - (out0 * ox + out4 * oy + out8 * oz);
  1210. out[13] = v[1] + oy - (out1 * ox + out5 * oy + out9 * oz);
  1211. out[14] = v[2] + oz - (out2 * ox + out6 * oy + out10 * oz);
  1212. out[15] = 1;
  1213. return out;
  1214. }
  1215. /**
  1216. * Calculates a 4x4 matrix from the given quaternion
  1217. *
  1218. * @param {mat4} out mat4 receiving operation result
  1219. * @param {ReadonlyQuat} q Quaternion to create matrix from
  1220. *
  1221. * @returns {mat4} out
  1222. */
  1223. export function fromQuat(out, q) {
  1224. var x = q[0],
  1225. y = q[1],
  1226. z = q[2],
  1227. w = q[3];
  1228. var x2 = x + x;
  1229. var y2 = y + y;
  1230. var z2 = z + z;
  1231. var xx = x * x2;
  1232. var yx = y * x2;
  1233. var yy = y * y2;
  1234. var zx = z * x2;
  1235. var zy = z * y2;
  1236. var zz = z * z2;
  1237. var wx = w * x2;
  1238. var wy = w * y2;
  1239. var wz = w * z2;
  1240. out[0] = 1 - yy - zz;
  1241. out[1] = yx + wz;
  1242. out[2] = zx - wy;
  1243. out[3] = 0;
  1244. out[4] = yx - wz;
  1245. out[5] = 1 - xx - zz;
  1246. out[6] = zy + wx;
  1247. out[7] = 0;
  1248. out[8] = zx + wy;
  1249. out[9] = zy - wx;
  1250. out[10] = 1 - xx - yy;
  1251. out[11] = 0;
  1252. out[12] = 0;
  1253. out[13] = 0;
  1254. out[14] = 0;
  1255. out[15] = 1;
  1256. return out;
  1257. }
  1258. /**
  1259. * Generates a frustum matrix with the given bounds
  1260. *
  1261. * @param {mat4} out mat4 frustum matrix will be written into
  1262. * @param {Number} left Left bound of the frustum
  1263. * @param {Number} right Right bound of the frustum
  1264. * @param {Number} bottom Bottom bound of the frustum
  1265. * @param {Number} top Top bound of the frustum
  1266. * @param {Number} near Near bound of the frustum
  1267. * @param {Number} far Far bound of the frustum
  1268. * @returns {mat4} out
  1269. */
  1270. export function frustum(out, left, right, bottom, top, near, far) {
  1271. var rl = 1 / (right - left);
  1272. var tb = 1 / (top - bottom);
  1273. var nf = 1 / (near - far);
  1274. out[0] = near * 2 * rl;
  1275. out[1] = 0;
  1276. out[2] = 0;
  1277. out[3] = 0;
  1278. out[4] = 0;
  1279. out[5] = near * 2 * tb;
  1280. out[6] = 0;
  1281. out[7] = 0;
  1282. out[8] = (right + left) * rl;
  1283. out[9] = (top + bottom) * tb;
  1284. out[10] = (far + near) * nf;
  1285. out[11] = -1;
  1286. out[12] = 0;
  1287. out[13] = 0;
  1288. out[14] = far * near * 2 * nf;
  1289. out[15] = 0;
  1290. return out;
  1291. }
  1292. /**
  1293. * Generates a perspective projection matrix with the given bounds.
  1294. * The near/far clip planes correspond to a normalized device coordinate Z range of [-1, 1],
  1295. * which matches WebGL/OpenGL's clip volume.
  1296. * Passing null/undefined/no value for far will generate infinite projection matrix.
  1297. *
  1298. * @param {mat4} out mat4 frustum matrix will be written into
  1299. * @param {number} fovy Vertical field of view in radians
  1300. * @param {number} aspect Aspect ratio. typically viewport width/height
  1301. * @param {number} near Near bound of the frustum
  1302. * @param {number} far Far bound of the frustum, can be null or Infinity
  1303. * @returns {mat4} out
  1304. */
  1305. export function perspectiveNO(out, fovy, aspect, near, far) {
  1306. var f = 1.0 / Math.tan(fovy / 2),
  1307. nf;
  1308. out[0] = f / aspect;
  1309. out[1] = 0;
  1310. out[2] = 0;
  1311. out[3] = 0;
  1312. out[4] = 0;
  1313. out[5] = f;
  1314. out[6] = 0;
  1315. out[7] = 0;
  1316. out[8] = 0;
  1317. out[9] = 0;
  1318. out[11] = -1;
  1319. out[12] = 0;
  1320. out[13] = 0;
  1321. out[15] = 0;
  1322. if (far != null && far !== Infinity) {
  1323. nf = 1 / (near - far);
  1324. out[10] = (far + near) * nf;
  1325. out[14] = 2 * far * near * nf;
  1326. } else {
  1327. out[10] = -1;
  1328. out[14] = -2 * near;
  1329. }
  1330. return out;
  1331. }
  1332. /**
  1333. * Alias for {@link mat4.perspectiveNO}
  1334. * @function
  1335. */
  1336. export var perspective = perspectiveNO;
  1337. /**
  1338. * Generates a perspective projection matrix suitable for WebGPU with the given bounds.
  1339. * The near/far clip planes correspond to a normalized device coordinate Z range of [0, 1],
  1340. * which matches WebGPU/Vulkan/DirectX/Metal's clip volume.
  1341. * Passing null/undefined/no value for far will generate infinite projection matrix.
  1342. *
  1343. * @param {mat4} out mat4 frustum matrix will be written into
  1344. * @param {number} fovy Vertical field of view in radians
  1345. * @param {number} aspect Aspect ratio. typically viewport width/height
  1346. * @param {number} near Near bound of the frustum
  1347. * @param {number} far Far bound of the frustum, can be null or Infinity
  1348. * @returns {mat4} out
  1349. */
  1350. export function perspectiveZO(out, fovy, aspect, near, far) {
  1351. var f = 1.0 / Math.tan(fovy / 2),
  1352. nf;
  1353. out[0] = f / aspect;
  1354. out[1] = 0;
  1355. out[2] = 0;
  1356. out[3] = 0;
  1357. out[4] = 0;
  1358. out[5] = f;
  1359. out[6] = 0;
  1360. out[7] = 0;
  1361. out[8] = 0;
  1362. out[9] = 0;
  1363. out[11] = -1;
  1364. out[12] = 0;
  1365. out[13] = 0;
  1366. out[15] = 0;
  1367. if (far != null && far !== Infinity) {
  1368. nf = 1 / (near - far);
  1369. out[10] = far * nf;
  1370. out[14] = far * near * nf;
  1371. } else {
  1372. out[10] = -1;
  1373. out[14] = -near;
  1374. }
  1375. return out;
  1376. }
  1377. /**
  1378. * Generates a perspective projection matrix with the given field of view.
  1379. * This is primarily useful for generating projection matrices to be used
  1380. * with the still experiemental WebVR API.
  1381. *
  1382. * @param {mat4} out mat4 frustum matrix will be written into
  1383. * @param {Object} fov Object containing the following values: upDegrees, downDegrees, leftDegrees, rightDegrees
  1384. * @param {number} near Near bound of the frustum
  1385. * @param {number} far Far bound of the frustum
  1386. * @returns {mat4} out
  1387. */
  1388. export function perspectiveFromFieldOfView(out, fov, near, far) {
  1389. var upTan = Math.tan(fov.upDegrees * Math.PI / 180.0);
  1390. var downTan = Math.tan(fov.downDegrees * Math.PI / 180.0);
  1391. var leftTan = Math.tan(fov.leftDegrees * Math.PI / 180.0);
  1392. var rightTan = Math.tan(fov.rightDegrees * Math.PI / 180.0);
  1393. var xScale = 2.0 / (leftTan + rightTan);
  1394. var yScale = 2.0 / (upTan + downTan);
  1395. out[0] = xScale;
  1396. out[1] = 0.0;
  1397. out[2] = 0.0;
  1398. out[3] = 0.0;
  1399. out[4] = 0.0;
  1400. out[5] = yScale;
  1401. out[6] = 0.0;
  1402. out[7] = 0.0;
  1403. out[8] = -((leftTan - rightTan) * xScale * 0.5);
  1404. out[9] = (upTan - downTan) * yScale * 0.5;
  1405. out[10] = far / (near - far);
  1406. out[11] = -1.0;
  1407. out[12] = 0.0;
  1408. out[13] = 0.0;
  1409. out[14] = far * near / (near - far);
  1410. out[15] = 0.0;
  1411. return out;
  1412. }
  1413. /**
  1414. * Generates a orthogonal projection matrix with the given bounds.
  1415. * The near/far clip planes correspond to a normalized device coordinate Z range of [-1, 1],
  1416. * which matches WebGL/OpenGL's clip volume.
  1417. *
  1418. * @param {mat4} out mat4 frustum matrix will be written into
  1419. * @param {number} left Left bound of the frustum
  1420. * @param {number} right Right bound of the frustum
  1421. * @param {number} bottom Bottom bound of the frustum
  1422. * @param {number} top Top bound of the frustum
  1423. * @param {number} near Near bound of the frustum
  1424. * @param {number} far Far bound of the frustum
  1425. * @returns {mat4} out
  1426. */
  1427. export function orthoNO(out, left, right, bottom, top, near, far) {
  1428. var lr = 1 / (left - right);
  1429. var bt = 1 / (bottom - top);
  1430. var nf = 1 / (near - far);
  1431. out[0] = -2 * lr;
  1432. out[1] = 0;
  1433. out[2] = 0;
  1434. out[3] = 0;
  1435. out[4] = 0;
  1436. out[5] = -2 * bt;
  1437. out[6] = 0;
  1438. out[7] = 0;
  1439. out[8] = 0;
  1440. out[9] = 0;
  1441. out[10] = 2 * nf;
  1442. out[11] = 0;
  1443. out[12] = (left + right) * lr;
  1444. out[13] = (top + bottom) * bt;
  1445. out[14] = (far + near) * nf;
  1446. out[15] = 1;
  1447. return out;
  1448. }
  1449. /**
  1450. * Alias for {@link mat4.orthoNO}
  1451. * @function
  1452. */
  1453. export var ortho = orthoNO;
  1454. /**
  1455. * Generates a orthogonal projection matrix with the given bounds.
  1456. * The near/far clip planes correspond to a normalized device coordinate Z range of [0, 1],
  1457. * which matches WebGPU/Vulkan/DirectX/Metal's clip volume.
  1458. *
  1459. * @param {mat4} out mat4 frustum matrix will be written into
  1460. * @param {number} left Left bound of the frustum
  1461. * @param {number} right Right bound of the frustum
  1462. * @param {number} bottom Bottom bound of the frustum
  1463. * @param {number} top Top bound of the frustum
  1464. * @param {number} near Near bound of the frustum
  1465. * @param {number} far Far bound of the frustum
  1466. * @returns {mat4} out
  1467. */
  1468. export function orthoZO(out, left, right, bottom, top, near, far) {
  1469. var lr = 1 / (left - right);
  1470. var bt = 1 / (bottom - top);
  1471. var nf = 1 / (near - far);
  1472. out[0] = -2 * lr;
  1473. out[1] = 0;
  1474. out[2] = 0;
  1475. out[3] = 0;
  1476. out[4] = 0;
  1477. out[5] = -2 * bt;
  1478. out[6] = 0;
  1479. out[7] = 0;
  1480. out[8] = 0;
  1481. out[9] = 0;
  1482. out[10] = nf;
  1483. out[11] = 0;
  1484. out[12] = (left + right) * lr;
  1485. out[13] = (top + bottom) * bt;
  1486. out[14] = near * nf;
  1487. out[15] = 1;
  1488. return out;
  1489. }
  1490. /**
  1491. * Generates a look-at matrix with the given eye position, focal point, and up axis.
  1492. * If you want a matrix that actually makes an object look at another object, you should use targetTo instead.
  1493. *
  1494. * @param {mat4} out mat4 frustum matrix will be written into
  1495. * @param {ReadonlyVec3} eye Position of the viewer
  1496. * @param {ReadonlyVec3} center Point the viewer is looking at
  1497. * @param {ReadonlyVec3} up vec3 pointing up
  1498. * @returns {mat4} out
  1499. */
  1500. export function lookAt(out, eye, center, up) {
  1501. var x0, x1, x2, y0, y1, y2, z0, z1, z2, len;
  1502. var eyex = eye[0];
  1503. var eyey = eye[1];
  1504. var eyez = eye[2];
  1505. var upx = up[0];
  1506. var upy = up[1];
  1507. var upz = up[2];
  1508. var centerx = center[0];
  1509. var centery = center[1];
  1510. var centerz = center[2];
  1511. if (Math.abs(eyex - centerx) < glMatrix.EPSILON && Math.abs(eyey - centery) < glMatrix.EPSILON && Math.abs(eyez - centerz) < glMatrix.EPSILON) {
  1512. return identity(out);
  1513. }
  1514. z0 = eyex - centerx;
  1515. z1 = eyey - centery;
  1516. z2 = eyez - centerz;
  1517. len = 1 / Math.hypot(z0, z1, z2);
  1518. z0 *= len;
  1519. z1 *= len;
  1520. z2 *= len;
  1521. x0 = upy * z2 - upz * z1;
  1522. x1 = upz * z0 - upx * z2;
  1523. x2 = upx * z1 - upy * z0;
  1524. len = Math.hypot(x0, x1, x2);
  1525. if (!len) {
  1526. x0 = 0;
  1527. x1 = 0;
  1528. x2 = 0;
  1529. } else {
  1530. len = 1 / len;
  1531. x0 *= len;
  1532. x1 *= len;
  1533. x2 *= len;
  1534. }
  1535. y0 = z1 * x2 - z2 * x1;
  1536. y1 = z2 * x0 - z0 * x2;
  1537. y2 = z0 * x1 - z1 * x0;
  1538. len = Math.hypot(y0, y1, y2);
  1539. if (!len) {
  1540. y0 = 0;
  1541. y1 = 0;
  1542. y2 = 0;
  1543. } else {
  1544. len = 1 / len;
  1545. y0 *= len;
  1546. y1 *= len;
  1547. y2 *= len;
  1548. }
  1549. out[0] = x0;
  1550. out[1] = y0;
  1551. out[2] = z0;
  1552. out[3] = 0;
  1553. out[4] = x1;
  1554. out[5] = y1;
  1555. out[6] = z1;
  1556. out[7] = 0;
  1557. out[8] = x2;
  1558. out[9] = y2;
  1559. out[10] = z2;
  1560. out[11] = 0;
  1561. out[12] = -(x0 * eyex + x1 * eyey + x2 * eyez);
  1562. out[13] = -(y0 * eyex + y1 * eyey + y2 * eyez);
  1563. out[14] = -(z0 * eyex + z1 * eyey + z2 * eyez);
  1564. out[15] = 1;
  1565. return out;
  1566. }
  1567. /**
  1568. * Generates a matrix that makes something look at something else.
  1569. *
  1570. * @param {mat4} out mat4 frustum matrix will be written into
  1571. * @param {ReadonlyVec3} eye Position of the viewer
  1572. * @param {ReadonlyVec3} center Point the viewer is looking at
  1573. * @param {ReadonlyVec3} up vec3 pointing up
  1574. * @returns {mat4} out
  1575. */
  1576. export function targetTo(out, eye, target, up) {
  1577. var eyex = eye[0],
  1578. eyey = eye[1],
  1579. eyez = eye[2],
  1580. upx = up[0],
  1581. upy = up[1],
  1582. upz = up[2];
  1583. var z0 = eyex - target[0],
  1584. z1 = eyey - target[1],
  1585. z2 = eyez - target[2];
  1586. var len = z0 * z0 + z1 * z1 + z2 * z2;
  1587. if (len > 0) {
  1588. len = 1 / Math.sqrt(len);
  1589. z0 *= len;
  1590. z1 *= len;
  1591. z2 *= len;
  1592. }
  1593. var x0 = upy * z2 - upz * z1,
  1594. x1 = upz * z0 - upx * z2,
  1595. x2 = upx * z1 - upy * z0;
  1596. len = x0 * x0 + x1 * x1 + x2 * x2;
  1597. if (len > 0) {
  1598. len = 1 / Math.sqrt(len);
  1599. x0 *= len;
  1600. x1 *= len;
  1601. x2 *= len;
  1602. }
  1603. out[0] = x0;
  1604. out[1] = x1;
  1605. out[2] = x2;
  1606. out[3] = 0;
  1607. out[4] = z1 * x2 - z2 * x1;
  1608. out[5] = z2 * x0 - z0 * x2;
  1609. out[6] = z0 * x1 - z1 * x0;
  1610. out[7] = 0;
  1611. out[8] = z0;
  1612. out[9] = z1;
  1613. out[10] = z2;
  1614. out[11] = 0;
  1615. out[12] = eyex;
  1616. out[13] = eyey;
  1617. out[14] = eyez;
  1618. out[15] = 1;
  1619. return out;
  1620. }
  1621. /**
  1622. * Returns a string representation of a mat4
  1623. *
  1624. * @param {ReadonlyMat4} a matrix to represent as a string
  1625. * @returns {String} string representation of the matrix
  1626. */
  1627. export function str(a) {
  1628. return "mat4(" + a[0] + ", " + a[1] + ", " + a[2] + ", " + a[3] + ", " + a[4] + ", " + a[5] + ", " + a[6] + ", " + a[7] + ", " + a[8] + ", " + a[9] + ", " + a[10] + ", " + a[11] + ", " + a[12] + ", " + a[13] + ", " + a[14] + ", " + a[15] + ")";
  1629. }
  1630. /**
  1631. * Returns Frobenius norm of a mat4
  1632. *
  1633. * @param {ReadonlyMat4} a the matrix to calculate Frobenius norm of
  1634. * @returns {Number} Frobenius norm
  1635. */
  1636. export function frob(a) {
  1637. return Math.hypot(a[0], a[1], a[2], a[3], a[4], a[5], a[6], a[7], a[8], a[9], a[10], a[11], a[12], a[13], a[14], a[15]);
  1638. }
  1639. /**
  1640. * Adds two mat4's
  1641. *
  1642. * @param {mat4} out the receiving matrix
  1643. * @param {ReadonlyMat4} a the first operand
  1644. * @param {ReadonlyMat4} b the second operand
  1645. * @returns {mat4} out
  1646. */
  1647. export function add(out, a, b) {
  1648. out[0] = a[0] + b[0];
  1649. out[1] = a[1] + b[1];
  1650. out[2] = a[2] + b[2];
  1651. out[3] = a[3] + b[3];
  1652. out[4] = a[4] + b[4];
  1653. out[5] = a[5] + b[5];
  1654. out[6] = a[6] + b[6];
  1655. out[7] = a[7] + b[7];
  1656. out[8] = a[8] + b[8];
  1657. out[9] = a[9] + b[9];
  1658. out[10] = a[10] + b[10];
  1659. out[11] = a[11] + b[11];
  1660. out[12] = a[12] + b[12];
  1661. out[13] = a[13] + b[13];
  1662. out[14] = a[14] + b[14];
  1663. out[15] = a[15] + b[15];
  1664. return out;
  1665. }
  1666. /**
  1667. * Subtracts matrix b from matrix a
  1668. *
  1669. * @param {mat4} out the receiving matrix
  1670. * @param {ReadonlyMat4} a the first operand
  1671. * @param {ReadonlyMat4} b the second operand
  1672. * @returns {mat4} out
  1673. */
  1674. export function subtract(out, a, b) {
  1675. out[0] = a[0] - b[0];
  1676. out[1] = a[1] - b[1];
  1677. out[2] = a[2] - b[2];
  1678. out[3] = a[3] - b[3];
  1679. out[4] = a[4] - b[4];
  1680. out[5] = a[5] - b[5];
  1681. out[6] = a[6] - b[6];
  1682. out[7] = a[7] - b[7];
  1683. out[8] = a[8] - b[8];
  1684. out[9] = a[9] - b[9];
  1685. out[10] = a[10] - b[10];
  1686. out[11] = a[11] - b[11];
  1687. out[12] = a[12] - b[12];
  1688. out[13] = a[13] - b[13];
  1689. out[14] = a[14] - b[14];
  1690. out[15] = a[15] - b[15];
  1691. return out;
  1692. }
  1693. /**
  1694. * Multiply each element of the matrix by a scalar.
  1695. *
  1696. * @param {mat4} out the receiving matrix
  1697. * @param {ReadonlyMat4} a the matrix to scale
  1698. * @param {Number} b amount to scale the matrix's elements by
  1699. * @returns {mat4} out
  1700. */
  1701. export function multiplyScalar(out, a, b) {
  1702. out[0] = a[0] * b;
  1703. out[1] = a[1] * b;
  1704. out[2] = a[2] * b;
  1705. out[3] = a[3] * b;
  1706. out[4] = a[4] * b;
  1707. out[5] = a[5] * b;
  1708. out[6] = a[6] * b;
  1709. out[7] = a[7] * b;
  1710. out[8] = a[8] * b;
  1711. out[9] = a[9] * b;
  1712. out[10] = a[10] * b;
  1713. out[11] = a[11] * b;
  1714. out[12] = a[12] * b;
  1715. out[13] = a[13] * b;
  1716. out[14] = a[14] * b;
  1717. out[15] = a[15] * b;
  1718. return out;
  1719. }
  1720. /**
  1721. * Adds two mat4's after multiplying each element of the second operand by a scalar value.
  1722. *
  1723. * @param {mat4} out the receiving vector
  1724. * @param {ReadonlyMat4} a the first operand
  1725. * @param {ReadonlyMat4} b the second operand
  1726. * @param {Number} scale the amount to scale b's elements by before adding
  1727. * @returns {mat4} out
  1728. */
  1729. export function multiplyScalarAndAdd(out, a, b, scale) {
  1730. out[0] = a[0] + b[0] * scale;
  1731. out[1] = a[1] + b[1] * scale;
  1732. out[2] = a[2] + b[2] * scale;
  1733. out[3] = a[3] + b[3] * scale;
  1734. out[4] = a[4] + b[4] * scale;
  1735. out[5] = a[5] + b[5] * scale;
  1736. out[6] = a[6] + b[6] * scale;
  1737. out[7] = a[7] + b[7] * scale;
  1738. out[8] = a[8] + b[8] * scale;
  1739. out[9] = a[9] + b[9] * scale;
  1740. out[10] = a[10] + b[10] * scale;
  1741. out[11] = a[11] + b[11] * scale;
  1742. out[12] = a[12] + b[12] * scale;
  1743. out[13] = a[13] + b[13] * scale;
  1744. out[14] = a[14] + b[14] * scale;
  1745. out[15] = a[15] + b[15] * scale;
  1746. return out;
  1747. }
  1748. /**
  1749. * Returns whether or not the matrices have exactly the same elements in the same position (when compared with ===)
  1750. *
  1751. * @param {ReadonlyMat4} a The first matrix.
  1752. * @param {ReadonlyMat4} b The second matrix.
  1753. * @returns {Boolean} True if the matrices are equal, false otherwise.
  1754. */
  1755. export function exactEquals(a, b) {
  1756. return a[0] === b[0] && a[1] === b[1] && a[2] === b[2] && a[3] === b[3] && a[4] === b[4] && a[5] === b[5] && a[6] === b[6] && a[7] === b[7] && a[8] === b[8] && a[9] === b[9] && a[10] === b[10] && a[11] === b[11] && a[12] === b[12] && a[13] === b[13] && a[14] === b[14] && a[15] === b[15];
  1757. }
  1758. /**
  1759. * Returns whether or not the matrices have approximately the same elements in the same position.
  1760. *
  1761. * @param {ReadonlyMat4} a The first matrix.
  1762. * @param {ReadonlyMat4} b The second matrix.
  1763. * @returns {Boolean} True if the matrices are equal, false otherwise.
  1764. */
  1765. export function equals(a, b) {
  1766. var a0 = a[0],
  1767. a1 = a[1],
  1768. a2 = a[2],
  1769. a3 = a[3];
  1770. var a4 = a[4],
  1771. a5 = a[5],
  1772. a6 = a[6],
  1773. a7 = a[7];
  1774. var a8 = a[8],
  1775. a9 = a[9],
  1776. a10 = a[10],
  1777. a11 = a[11];
  1778. var a12 = a[12],
  1779. a13 = a[13],
  1780. a14 = a[14],
  1781. a15 = a[15];
  1782. var b0 = b[0],
  1783. b1 = b[1],
  1784. b2 = b[2],
  1785. b3 = b[3];
  1786. var b4 = b[4],
  1787. b5 = b[5],
  1788. b6 = b[6],
  1789. b7 = b[7];
  1790. var b8 = b[8],
  1791. b9 = b[9],
  1792. b10 = b[10],
  1793. b11 = b[11];
  1794. var b12 = b[12],
  1795. b13 = b[13],
  1796. b14 = b[14],
  1797. b15 = b[15];
  1798. return Math.abs(a0 - b0) <= glMatrix.EPSILON * Math.max(1.0, Math.abs(a0), Math.abs(b0)) && Math.abs(a1 - b1) <= glMatrix.EPSILON * Math.max(1.0, Math.abs(a1), Math.abs(b1)) && Math.abs(a2 - b2) <= glMatrix.EPSILON * Math.max(1.0, Math.abs(a2), Math.abs(b2)) && Math.abs(a3 - b3) <= glMatrix.EPSILON * Math.max(1.0, Math.abs(a3), Math.abs(b3)) && Math.abs(a4 - b4) <= glMatrix.EPSILON * Math.max(1.0, Math.abs(a4), Math.abs(b4)) && Math.abs(a5 - b5) <= glMatrix.EPSILON * Math.max(1.0, Math.abs(a5), Math.abs(b5)) && Math.abs(a6 - b6) <= glMatrix.EPSILON * Math.max(1.0, Math.abs(a6), Math.abs(b6)) && Math.abs(a7 - b7) <= glMatrix.EPSILON * Math.max(1.0, Math.abs(a7), Math.abs(b7)) && Math.abs(a8 - b8) <= glMatrix.EPSILON * Math.max(1.0, Math.abs(a8), Math.abs(b8)) && Math.abs(a9 - b9) <= glMatrix.EPSILON * Math.max(1.0, Math.abs(a9), Math.abs(b9)) && Math.abs(a10 - b10) <= glMatrix.EPSILON * Math.max(1.0, Math.abs(a10), Math.abs(b10)) && Math.abs(a11 - b11) <= glMatrix.EPSILON * Math.max(1.0, Math.abs(a11), Math.abs(b11)) && Math.abs(a12 - b12) <= glMatrix.EPSILON * Math.max(1.0, Math.abs(a12), Math.abs(b12)) && Math.abs(a13 - b13) <= glMatrix.EPSILON * Math.max(1.0, Math.abs(a13), Math.abs(b13)) && Math.abs(a14 - b14) <= glMatrix.EPSILON * Math.max(1.0, Math.abs(a14), Math.abs(b14)) && Math.abs(a15 - b15) <= glMatrix.EPSILON * Math.max(1.0, Math.abs(a15), Math.abs(b15));
  1799. }
  1800. /**
  1801. * Alias for {@link mat4.multiply}
  1802. * @function
  1803. */
  1804. export var mul = multiply;
  1805. /**
  1806. * Alias for {@link mat4.subtract}
  1807. * @function
  1808. */
  1809. export var sub = subtract;