mat3.js 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853
  1. "use strict";
  2. function _typeof(obj) { "@babel/helpers - typeof"; if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }
  3. Object.defineProperty(exports, "__esModule", {
  4. value: true
  5. });
  6. exports.create = create;
  7. exports.fromMat4 = fromMat4;
  8. exports.clone = clone;
  9. exports.copy = copy;
  10. exports.fromValues = fromValues;
  11. exports.set = set;
  12. exports.identity = identity;
  13. exports.transpose = transpose;
  14. exports.invert = invert;
  15. exports.adjoint = adjoint;
  16. exports.determinant = determinant;
  17. exports.multiply = multiply;
  18. exports.translate = translate;
  19. exports.rotate = rotate;
  20. exports.scale = scale;
  21. exports.fromTranslation = fromTranslation;
  22. exports.fromRotation = fromRotation;
  23. exports.fromScaling = fromScaling;
  24. exports.fromMat2d = fromMat2d;
  25. exports.fromQuat = fromQuat;
  26. exports.normalFromMat4 = normalFromMat4;
  27. exports.projection = projection;
  28. exports.str = str;
  29. exports.frob = frob;
  30. exports.add = add;
  31. exports.subtract = subtract;
  32. exports.multiplyScalar = multiplyScalar;
  33. exports.multiplyScalarAndAdd = multiplyScalarAndAdd;
  34. exports.exactEquals = exactEquals;
  35. exports.equals = equals;
  36. exports.sub = exports.mul = void 0;
  37. var glMatrix = _interopRequireWildcard(require("./common.js"));
  38. function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
  39. function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || _typeof(obj) !== "object" && typeof obj !== "function") { return { "default": obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj["default"] = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
  40. /**
  41. * 3x3 Matrix
  42. * @module mat3
  43. */
  44. /**
  45. * Creates a new identity mat3
  46. *
  47. * @returns {mat3} a new 3x3 matrix
  48. */
  49. function create() {
  50. var out = new glMatrix.ARRAY_TYPE(9);
  51. if (glMatrix.ARRAY_TYPE != Float32Array) {
  52. out[1] = 0;
  53. out[2] = 0;
  54. out[3] = 0;
  55. out[5] = 0;
  56. out[6] = 0;
  57. out[7] = 0;
  58. }
  59. out[0] = 1;
  60. out[4] = 1;
  61. out[8] = 1;
  62. return out;
  63. }
  64. /**
  65. * Copies the upper-left 3x3 values into the given mat3.
  66. *
  67. * @param {mat3} out the receiving 3x3 matrix
  68. * @param {ReadonlyMat4} a the source 4x4 matrix
  69. * @returns {mat3} out
  70. */
  71. function fromMat4(out, a) {
  72. out[0] = a[0];
  73. out[1] = a[1];
  74. out[2] = a[2];
  75. out[3] = a[4];
  76. out[4] = a[5];
  77. out[5] = a[6];
  78. out[6] = a[8];
  79. out[7] = a[9];
  80. out[8] = a[10];
  81. return out;
  82. }
  83. /**
  84. * Creates a new mat3 initialized with values from an existing matrix
  85. *
  86. * @param {ReadonlyMat3} a matrix to clone
  87. * @returns {mat3} a new 3x3 matrix
  88. */
  89. function clone(a) {
  90. var out = new glMatrix.ARRAY_TYPE(9);
  91. out[0] = a[0];
  92. out[1] = a[1];
  93. out[2] = a[2];
  94. out[3] = a[3];
  95. out[4] = a[4];
  96. out[5] = a[5];
  97. out[6] = a[6];
  98. out[7] = a[7];
  99. out[8] = a[8];
  100. return out;
  101. }
  102. /**
  103. * Copy the values from one mat3 to another
  104. *
  105. * @param {mat3} out the receiving matrix
  106. * @param {ReadonlyMat3} a the source matrix
  107. * @returns {mat3} out
  108. */
  109. function copy(out, a) {
  110. out[0] = a[0];
  111. out[1] = a[1];
  112. out[2] = a[2];
  113. out[3] = a[3];
  114. out[4] = a[4];
  115. out[5] = a[5];
  116. out[6] = a[6];
  117. out[7] = a[7];
  118. out[8] = a[8];
  119. return out;
  120. }
  121. /**
  122. * Create a new mat3 with the given values
  123. *
  124. * @param {Number} m00 Component in column 0, row 0 position (index 0)
  125. * @param {Number} m01 Component in column 0, row 1 position (index 1)
  126. * @param {Number} m02 Component in column 0, row 2 position (index 2)
  127. * @param {Number} m10 Component in column 1, row 0 position (index 3)
  128. * @param {Number} m11 Component in column 1, row 1 position (index 4)
  129. * @param {Number} m12 Component in column 1, row 2 position (index 5)
  130. * @param {Number} m20 Component in column 2, row 0 position (index 6)
  131. * @param {Number} m21 Component in column 2, row 1 position (index 7)
  132. * @param {Number} m22 Component in column 2, row 2 position (index 8)
  133. * @returns {mat3} A new mat3
  134. */
  135. function fromValues(m00, m01, m02, m10, m11, m12, m20, m21, m22) {
  136. var out = new glMatrix.ARRAY_TYPE(9);
  137. out[0] = m00;
  138. out[1] = m01;
  139. out[2] = m02;
  140. out[3] = m10;
  141. out[4] = m11;
  142. out[5] = m12;
  143. out[6] = m20;
  144. out[7] = m21;
  145. out[8] = m22;
  146. return out;
  147. }
  148. /**
  149. * Set the components of a mat3 to the given values
  150. *
  151. * @param {mat3} out the receiving matrix
  152. * @param {Number} m00 Component in column 0, row 0 position (index 0)
  153. * @param {Number} m01 Component in column 0, row 1 position (index 1)
  154. * @param {Number} m02 Component in column 0, row 2 position (index 2)
  155. * @param {Number} m10 Component in column 1, row 0 position (index 3)
  156. * @param {Number} m11 Component in column 1, row 1 position (index 4)
  157. * @param {Number} m12 Component in column 1, row 2 position (index 5)
  158. * @param {Number} m20 Component in column 2, row 0 position (index 6)
  159. * @param {Number} m21 Component in column 2, row 1 position (index 7)
  160. * @param {Number} m22 Component in column 2, row 2 position (index 8)
  161. * @returns {mat3} out
  162. */
  163. function set(out, m00, m01, m02, m10, m11, m12, m20, m21, m22) {
  164. out[0] = m00;
  165. out[1] = m01;
  166. out[2] = m02;
  167. out[3] = m10;
  168. out[4] = m11;
  169. out[5] = m12;
  170. out[6] = m20;
  171. out[7] = m21;
  172. out[8] = m22;
  173. return out;
  174. }
  175. /**
  176. * Set a mat3 to the identity matrix
  177. *
  178. * @param {mat3} out the receiving matrix
  179. * @returns {mat3} out
  180. */
  181. function identity(out) {
  182. out[0] = 1;
  183. out[1] = 0;
  184. out[2] = 0;
  185. out[3] = 0;
  186. out[4] = 1;
  187. out[5] = 0;
  188. out[6] = 0;
  189. out[7] = 0;
  190. out[8] = 1;
  191. return out;
  192. }
  193. /**
  194. * Transpose the values of a mat3
  195. *
  196. * @param {mat3} out the receiving matrix
  197. * @param {ReadonlyMat3} a the source matrix
  198. * @returns {mat3} out
  199. */
  200. function transpose(out, a) {
  201. // If we are transposing ourselves we can skip a few steps but have to cache some values
  202. if (out === a) {
  203. var a01 = a[1],
  204. a02 = a[2],
  205. a12 = a[5];
  206. out[1] = a[3];
  207. out[2] = a[6];
  208. out[3] = a01;
  209. out[5] = a[7];
  210. out[6] = a02;
  211. out[7] = a12;
  212. } else {
  213. out[0] = a[0];
  214. out[1] = a[3];
  215. out[2] = a[6];
  216. out[3] = a[1];
  217. out[4] = a[4];
  218. out[5] = a[7];
  219. out[6] = a[2];
  220. out[7] = a[5];
  221. out[8] = a[8];
  222. }
  223. return out;
  224. }
  225. /**
  226. * Inverts a mat3
  227. *
  228. * @param {mat3} out the receiving matrix
  229. * @param {ReadonlyMat3} a the source matrix
  230. * @returns {mat3} out
  231. */
  232. function invert(out, a) {
  233. var a00 = a[0],
  234. a01 = a[1],
  235. a02 = a[2];
  236. var a10 = a[3],
  237. a11 = a[4],
  238. a12 = a[5];
  239. var a20 = a[6],
  240. a21 = a[7],
  241. a22 = a[8];
  242. var b01 = a22 * a11 - a12 * a21;
  243. var b11 = -a22 * a10 + a12 * a20;
  244. var b21 = a21 * a10 - a11 * a20; // Calculate the determinant
  245. var det = a00 * b01 + a01 * b11 + a02 * b21;
  246. if (!det) {
  247. return null;
  248. }
  249. det = 1.0 / det;
  250. out[0] = b01 * det;
  251. out[1] = (-a22 * a01 + a02 * a21) * det;
  252. out[2] = (a12 * a01 - a02 * a11) * det;
  253. out[3] = b11 * det;
  254. out[4] = (a22 * a00 - a02 * a20) * det;
  255. out[5] = (-a12 * a00 + a02 * a10) * det;
  256. out[6] = b21 * det;
  257. out[7] = (-a21 * a00 + a01 * a20) * det;
  258. out[8] = (a11 * a00 - a01 * a10) * det;
  259. return out;
  260. }
  261. /**
  262. * Calculates the adjugate of a mat3
  263. *
  264. * @param {mat3} out the receiving matrix
  265. * @param {ReadonlyMat3} a the source matrix
  266. * @returns {mat3} out
  267. */
  268. function adjoint(out, a) {
  269. var a00 = a[0],
  270. a01 = a[1],
  271. a02 = a[2];
  272. var a10 = a[3],
  273. a11 = a[4],
  274. a12 = a[5];
  275. var a20 = a[6],
  276. a21 = a[7],
  277. a22 = a[8];
  278. out[0] = a11 * a22 - a12 * a21;
  279. out[1] = a02 * a21 - a01 * a22;
  280. out[2] = a01 * a12 - a02 * a11;
  281. out[3] = a12 * a20 - a10 * a22;
  282. out[4] = a00 * a22 - a02 * a20;
  283. out[5] = a02 * a10 - a00 * a12;
  284. out[6] = a10 * a21 - a11 * a20;
  285. out[7] = a01 * a20 - a00 * a21;
  286. out[8] = a00 * a11 - a01 * a10;
  287. return out;
  288. }
  289. /**
  290. * Calculates the determinant of a mat3
  291. *
  292. * @param {ReadonlyMat3} a the source matrix
  293. * @returns {Number} determinant of a
  294. */
  295. function determinant(a) {
  296. var a00 = a[0],
  297. a01 = a[1],
  298. a02 = a[2];
  299. var a10 = a[3],
  300. a11 = a[4],
  301. a12 = a[5];
  302. var a20 = a[6],
  303. a21 = a[7],
  304. a22 = a[8];
  305. return a00 * (a22 * a11 - a12 * a21) + a01 * (-a22 * a10 + a12 * a20) + a02 * (a21 * a10 - a11 * a20);
  306. }
  307. /**
  308. * Multiplies two mat3's
  309. *
  310. * @param {mat3} out the receiving matrix
  311. * @param {ReadonlyMat3} a the first operand
  312. * @param {ReadonlyMat3} b the second operand
  313. * @returns {mat3} out
  314. */
  315. function multiply(out, a, b) {
  316. var a00 = a[0],
  317. a01 = a[1],
  318. a02 = a[2];
  319. var a10 = a[3],
  320. a11 = a[4],
  321. a12 = a[5];
  322. var a20 = a[6],
  323. a21 = a[7],
  324. a22 = a[8];
  325. var b00 = b[0],
  326. b01 = b[1],
  327. b02 = b[2];
  328. var b10 = b[3],
  329. b11 = b[4],
  330. b12 = b[5];
  331. var b20 = b[6],
  332. b21 = b[7],
  333. b22 = b[8];
  334. out[0] = b00 * a00 + b01 * a10 + b02 * a20;
  335. out[1] = b00 * a01 + b01 * a11 + b02 * a21;
  336. out[2] = b00 * a02 + b01 * a12 + b02 * a22;
  337. out[3] = b10 * a00 + b11 * a10 + b12 * a20;
  338. out[4] = b10 * a01 + b11 * a11 + b12 * a21;
  339. out[5] = b10 * a02 + b11 * a12 + b12 * a22;
  340. out[6] = b20 * a00 + b21 * a10 + b22 * a20;
  341. out[7] = b20 * a01 + b21 * a11 + b22 * a21;
  342. out[8] = b20 * a02 + b21 * a12 + b22 * a22;
  343. return out;
  344. }
  345. /**
  346. * Translate a mat3 by the given vector
  347. *
  348. * @param {mat3} out the receiving matrix
  349. * @param {ReadonlyMat3} a the matrix to translate
  350. * @param {ReadonlyVec2} v vector to translate by
  351. * @returns {mat3} out
  352. */
  353. function translate(out, a, v) {
  354. var a00 = a[0],
  355. a01 = a[1],
  356. a02 = a[2],
  357. a10 = a[3],
  358. a11 = a[4],
  359. a12 = a[5],
  360. a20 = a[6],
  361. a21 = a[7],
  362. a22 = a[8],
  363. x = v[0],
  364. y = v[1];
  365. out[0] = a00;
  366. out[1] = a01;
  367. out[2] = a02;
  368. out[3] = a10;
  369. out[4] = a11;
  370. out[5] = a12;
  371. out[6] = x * a00 + y * a10 + a20;
  372. out[7] = x * a01 + y * a11 + a21;
  373. out[8] = x * a02 + y * a12 + a22;
  374. return out;
  375. }
  376. /**
  377. * Rotates a mat3 by the given angle
  378. *
  379. * @param {mat3} out the receiving matrix
  380. * @param {ReadonlyMat3} a the matrix to rotate
  381. * @param {Number} rad the angle to rotate the matrix by
  382. * @returns {mat3} out
  383. */
  384. function rotate(out, a, rad) {
  385. var a00 = a[0],
  386. a01 = a[1],
  387. a02 = a[2],
  388. a10 = a[3],
  389. a11 = a[4],
  390. a12 = a[5],
  391. a20 = a[6],
  392. a21 = a[7],
  393. a22 = a[8],
  394. s = Math.sin(rad),
  395. c = Math.cos(rad);
  396. out[0] = c * a00 + s * a10;
  397. out[1] = c * a01 + s * a11;
  398. out[2] = c * a02 + s * a12;
  399. out[3] = c * a10 - s * a00;
  400. out[4] = c * a11 - s * a01;
  401. out[5] = c * a12 - s * a02;
  402. out[6] = a20;
  403. out[7] = a21;
  404. out[8] = a22;
  405. return out;
  406. }
  407. /**
  408. * Scales the mat3 by the dimensions in the given vec2
  409. *
  410. * @param {mat3} out the receiving matrix
  411. * @param {ReadonlyMat3} a the matrix to rotate
  412. * @param {ReadonlyVec2} v the vec2 to scale the matrix by
  413. * @returns {mat3} out
  414. **/
  415. function scale(out, a, v) {
  416. var x = v[0],
  417. y = v[1];
  418. out[0] = x * a[0];
  419. out[1] = x * a[1];
  420. out[2] = x * a[2];
  421. out[3] = y * a[3];
  422. out[4] = y * a[4];
  423. out[5] = y * a[5];
  424. out[6] = a[6];
  425. out[7] = a[7];
  426. out[8] = a[8];
  427. return out;
  428. }
  429. /**
  430. * Creates a matrix from a vector translation
  431. * This is equivalent to (but much faster than):
  432. *
  433. * mat3.identity(dest);
  434. * mat3.translate(dest, dest, vec);
  435. *
  436. * @param {mat3} out mat3 receiving operation result
  437. * @param {ReadonlyVec2} v Translation vector
  438. * @returns {mat3} out
  439. */
  440. function fromTranslation(out, v) {
  441. out[0] = 1;
  442. out[1] = 0;
  443. out[2] = 0;
  444. out[3] = 0;
  445. out[4] = 1;
  446. out[5] = 0;
  447. out[6] = v[0];
  448. out[7] = v[1];
  449. out[8] = 1;
  450. return out;
  451. }
  452. /**
  453. * Creates a matrix from a given angle
  454. * This is equivalent to (but much faster than):
  455. *
  456. * mat3.identity(dest);
  457. * mat3.rotate(dest, dest, rad);
  458. *
  459. * @param {mat3} out mat3 receiving operation result
  460. * @param {Number} rad the angle to rotate the matrix by
  461. * @returns {mat3} out
  462. */
  463. function fromRotation(out, rad) {
  464. var s = Math.sin(rad),
  465. c = Math.cos(rad);
  466. out[0] = c;
  467. out[1] = s;
  468. out[2] = 0;
  469. out[3] = -s;
  470. out[4] = c;
  471. out[5] = 0;
  472. out[6] = 0;
  473. out[7] = 0;
  474. out[8] = 1;
  475. return out;
  476. }
  477. /**
  478. * Creates a matrix from a vector scaling
  479. * This is equivalent to (but much faster than):
  480. *
  481. * mat3.identity(dest);
  482. * mat3.scale(dest, dest, vec);
  483. *
  484. * @param {mat3} out mat3 receiving operation result
  485. * @param {ReadonlyVec2} v Scaling vector
  486. * @returns {mat3} out
  487. */
  488. function fromScaling(out, v) {
  489. out[0] = v[0];
  490. out[1] = 0;
  491. out[2] = 0;
  492. out[3] = 0;
  493. out[4] = v[1];
  494. out[5] = 0;
  495. out[6] = 0;
  496. out[7] = 0;
  497. out[8] = 1;
  498. return out;
  499. }
  500. /**
  501. * Copies the values from a mat2d into a mat3
  502. *
  503. * @param {mat3} out the receiving matrix
  504. * @param {ReadonlyMat2d} a the matrix to copy
  505. * @returns {mat3} out
  506. **/
  507. function fromMat2d(out, a) {
  508. out[0] = a[0];
  509. out[1] = a[1];
  510. out[2] = 0;
  511. out[3] = a[2];
  512. out[4] = a[3];
  513. out[5] = 0;
  514. out[6] = a[4];
  515. out[7] = a[5];
  516. out[8] = 1;
  517. return out;
  518. }
  519. /**
  520. * Calculates a 3x3 matrix from the given quaternion
  521. *
  522. * @param {mat3} out mat3 receiving operation result
  523. * @param {ReadonlyQuat} q Quaternion to create matrix from
  524. *
  525. * @returns {mat3} out
  526. */
  527. function fromQuat(out, q) {
  528. var x = q[0],
  529. y = q[1],
  530. z = q[2],
  531. w = q[3];
  532. var x2 = x + x;
  533. var y2 = y + y;
  534. var z2 = z + z;
  535. var xx = x * x2;
  536. var yx = y * x2;
  537. var yy = y * y2;
  538. var zx = z * x2;
  539. var zy = z * y2;
  540. var zz = z * z2;
  541. var wx = w * x2;
  542. var wy = w * y2;
  543. var wz = w * z2;
  544. out[0] = 1 - yy - zz;
  545. out[3] = yx - wz;
  546. out[6] = zx + wy;
  547. out[1] = yx + wz;
  548. out[4] = 1 - xx - zz;
  549. out[7] = zy - wx;
  550. out[2] = zx - wy;
  551. out[5] = zy + wx;
  552. out[8] = 1 - xx - yy;
  553. return out;
  554. }
  555. /**
  556. * Calculates a 3x3 normal matrix (transpose inverse) from the 4x4 matrix
  557. *
  558. * @param {mat3} out mat3 receiving operation result
  559. * @param {ReadonlyMat4} a Mat4 to derive the normal matrix from
  560. *
  561. * @returns {mat3} out
  562. */
  563. function normalFromMat4(out, a) {
  564. var a00 = a[0],
  565. a01 = a[1],
  566. a02 = a[2],
  567. a03 = a[3];
  568. var a10 = a[4],
  569. a11 = a[5],
  570. a12 = a[6],
  571. a13 = a[7];
  572. var a20 = a[8],
  573. a21 = a[9],
  574. a22 = a[10],
  575. a23 = a[11];
  576. var a30 = a[12],
  577. a31 = a[13],
  578. a32 = a[14],
  579. a33 = a[15];
  580. var b00 = a00 * a11 - a01 * a10;
  581. var b01 = a00 * a12 - a02 * a10;
  582. var b02 = a00 * a13 - a03 * a10;
  583. var b03 = a01 * a12 - a02 * a11;
  584. var b04 = a01 * a13 - a03 * a11;
  585. var b05 = a02 * a13 - a03 * a12;
  586. var b06 = a20 * a31 - a21 * a30;
  587. var b07 = a20 * a32 - a22 * a30;
  588. var b08 = a20 * a33 - a23 * a30;
  589. var b09 = a21 * a32 - a22 * a31;
  590. var b10 = a21 * a33 - a23 * a31;
  591. var b11 = a22 * a33 - a23 * a32; // Calculate the determinant
  592. var det = b00 * b11 - b01 * b10 + b02 * b09 + b03 * b08 - b04 * b07 + b05 * b06;
  593. if (!det) {
  594. return null;
  595. }
  596. det = 1.0 / det;
  597. out[0] = (a11 * b11 - a12 * b10 + a13 * b09) * det;
  598. out[1] = (a12 * b08 - a10 * b11 - a13 * b07) * det;
  599. out[2] = (a10 * b10 - a11 * b08 + a13 * b06) * det;
  600. out[3] = (a02 * b10 - a01 * b11 - a03 * b09) * det;
  601. out[4] = (a00 * b11 - a02 * b08 + a03 * b07) * det;
  602. out[5] = (a01 * b08 - a00 * b10 - a03 * b06) * det;
  603. out[6] = (a31 * b05 - a32 * b04 + a33 * b03) * det;
  604. out[7] = (a32 * b02 - a30 * b05 - a33 * b01) * det;
  605. out[8] = (a30 * b04 - a31 * b02 + a33 * b00) * det;
  606. return out;
  607. }
  608. /**
  609. * Generates a 2D projection matrix with the given bounds
  610. *
  611. * @param {mat3} out mat3 frustum matrix will be written into
  612. * @param {number} width Width of your gl context
  613. * @param {number} height Height of gl context
  614. * @returns {mat3} out
  615. */
  616. function projection(out, width, height) {
  617. out[0] = 2 / width;
  618. out[1] = 0;
  619. out[2] = 0;
  620. out[3] = 0;
  621. out[4] = -2 / height;
  622. out[5] = 0;
  623. out[6] = -1;
  624. out[7] = 1;
  625. out[8] = 1;
  626. return out;
  627. }
  628. /**
  629. * Returns a string representation of a mat3
  630. *
  631. * @param {ReadonlyMat3} a matrix to represent as a string
  632. * @returns {String} string representation of the matrix
  633. */
  634. function str(a) {
  635. return "mat3(" + a[0] + ", " + a[1] + ", " + a[2] + ", " + a[3] + ", " + a[4] + ", " + a[5] + ", " + a[6] + ", " + a[7] + ", " + a[8] + ")";
  636. }
  637. /**
  638. * Returns Frobenius norm of a mat3
  639. *
  640. * @param {ReadonlyMat3} a the matrix to calculate Frobenius norm of
  641. * @returns {Number} Frobenius norm
  642. */
  643. function frob(a) {
  644. return Math.hypot(a[0], a[1], a[2], a[3], a[4], a[5], a[6], a[7], a[8]);
  645. }
  646. /**
  647. * Adds two mat3's
  648. *
  649. * @param {mat3} out the receiving matrix
  650. * @param {ReadonlyMat3} a the first operand
  651. * @param {ReadonlyMat3} b the second operand
  652. * @returns {mat3} out
  653. */
  654. function add(out, a, b) {
  655. out[0] = a[0] + b[0];
  656. out[1] = a[1] + b[1];
  657. out[2] = a[2] + b[2];
  658. out[3] = a[3] + b[3];
  659. out[4] = a[4] + b[4];
  660. out[5] = a[5] + b[5];
  661. out[6] = a[6] + b[6];
  662. out[7] = a[7] + b[7];
  663. out[8] = a[8] + b[8];
  664. return out;
  665. }
  666. /**
  667. * Subtracts matrix b from matrix a
  668. *
  669. * @param {mat3} out the receiving matrix
  670. * @param {ReadonlyMat3} a the first operand
  671. * @param {ReadonlyMat3} b the second operand
  672. * @returns {mat3} out
  673. */
  674. function subtract(out, a, b) {
  675. out[0] = a[0] - b[0];
  676. out[1] = a[1] - b[1];
  677. out[2] = a[2] - b[2];
  678. out[3] = a[3] - b[3];
  679. out[4] = a[4] - b[4];
  680. out[5] = a[5] - b[5];
  681. out[6] = a[6] - b[6];
  682. out[7] = a[7] - b[7];
  683. out[8] = a[8] - b[8];
  684. return out;
  685. }
  686. /**
  687. * Multiply each element of the matrix by a scalar.
  688. *
  689. * @param {mat3} out the receiving matrix
  690. * @param {ReadonlyMat3} a the matrix to scale
  691. * @param {Number} b amount to scale the matrix's elements by
  692. * @returns {mat3} out
  693. */
  694. function multiplyScalar(out, a, b) {
  695. out[0] = a[0] * b;
  696. out[1] = a[1] * b;
  697. out[2] = a[2] * b;
  698. out[3] = a[3] * b;
  699. out[4] = a[4] * b;
  700. out[5] = a[5] * b;
  701. out[6] = a[6] * b;
  702. out[7] = a[7] * b;
  703. out[8] = a[8] * b;
  704. return out;
  705. }
  706. /**
  707. * Adds two mat3's after multiplying each element of the second operand by a scalar value.
  708. *
  709. * @param {mat3} out the receiving vector
  710. * @param {ReadonlyMat3} a the first operand
  711. * @param {ReadonlyMat3} b the second operand
  712. * @param {Number} scale the amount to scale b's elements by before adding
  713. * @returns {mat3} out
  714. */
  715. function multiplyScalarAndAdd(out, a, b, scale) {
  716. out[0] = a[0] + b[0] * scale;
  717. out[1] = a[1] + b[1] * scale;
  718. out[2] = a[2] + b[2] * scale;
  719. out[3] = a[3] + b[3] * scale;
  720. out[4] = a[4] + b[4] * scale;
  721. out[5] = a[5] + b[5] * scale;
  722. out[6] = a[6] + b[6] * scale;
  723. out[7] = a[7] + b[7] * scale;
  724. out[8] = a[8] + b[8] * scale;
  725. return out;
  726. }
  727. /**
  728. * Returns whether or not the matrices have exactly the same elements in the same position (when compared with ===)
  729. *
  730. * @param {ReadonlyMat3} a The first matrix.
  731. * @param {ReadonlyMat3} b The second matrix.
  732. * @returns {Boolean} True if the matrices are equal, false otherwise.
  733. */
  734. function exactEquals(a, b) {
  735. 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];
  736. }
  737. /**
  738. * Returns whether or not the matrices have approximately the same elements in the same position.
  739. *
  740. * @param {ReadonlyMat3} a The first matrix.
  741. * @param {ReadonlyMat3} b The second matrix.
  742. * @returns {Boolean} True if the matrices are equal, false otherwise.
  743. */
  744. function equals(a, b) {
  745. var a0 = a[0],
  746. a1 = a[1],
  747. a2 = a[2],
  748. a3 = a[3],
  749. a4 = a[4],
  750. a5 = a[5],
  751. a6 = a[6],
  752. a7 = a[7],
  753. a8 = a[8];
  754. var b0 = b[0],
  755. b1 = b[1],
  756. b2 = b[2],
  757. b3 = b[3],
  758. b4 = b[4],
  759. b5 = b[5],
  760. b6 = b[6],
  761. b7 = b[7],
  762. b8 = b[8];
  763. 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));
  764. }
  765. /**
  766. * Alias for {@link mat3.multiply}
  767. * @function
  768. */
  769. var mul = multiply;
  770. /**
  771. * Alias for {@link mat3.subtract}
  772. * @function
  773. */
  774. exports.mul = mul;
  775. var sub = subtract;
  776. exports.sub = sub;