mat4.ts 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269
  1. /**
  2. * Copyright (c) 2017-2020 mol* contributors, licensed under MIT, See LICENSE file for more info.
  3. *
  4. * @author David Sehnal <david.sehnal@gmail.com>
  5. * @author Alexander Rose <alexander.rose@weirdbyte.de>
  6. */
  7. /*
  8. * This code has been modified from https://github.com/toji/gl-matrix/,
  9. * copyright (c) 2015, Brandon Jones, Colin MacKenzie IV.
  10. *
  11. * Permission is hereby granted, free of charge, to any person obtaining a copy
  12. * of this software and associated documentation files (the "Software"), to deal
  13. * in the Software without restriction, including without limitation the rights
  14. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  15. * copies of the Software, and to permit persons to whom the Software is
  16. * furnished to do so, subject to the following conditions:
  17. */
  18. import { EPSILON, equalEps } from './common';
  19. import { Vec3 } from './vec3';
  20. import { Quat } from './quat';
  21. import { degToRad } from '../../misc';
  22. import { NumberArray } from '../../../mol-util/type-helpers';
  23. import { Mat3 } from './mat3';
  24. import { Euler } from './euler';
  25. interface Mat4 extends Array<number> { [d: number]: number, '@type': 'mat4', length: 16 }
  26. interface ReadonlyMat4 extends Array<number> { readonly [d: number]: number, '@type': 'mat4', length: 16 }
  27. function Mat4() {
  28. return Mat4.zero();
  29. }
  30. /**
  31. * Stores a 4x4 matrix in a column major (j * 4 + i indexing) format.
  32. */
  33. namespace Mat4 {
  34. export function zero(): Mat4 {
  35. // force double backing array by 0.1.
  36. const ret = [0.1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0];
  37. ret[0] = 0.0;
  38. return ret as any;
  39. }
  40. export function identity(): Mat4 {
  41. const out = zero();
  42. out[0] = 1;
  43. out[1] = 0;
  44. out[2] = 0;
  45. out[3] = 0;
  46. out[4] = 0;
  47. out[5] = 1;
  48. out[6] = 0;
  49. out[7] = 0;
  50. out[8] = 0;
  51. out[9] = 0;
  52. out[10] = 1;
  53. out[11] = 0;
  54. out[12] = 0;
  55. out[13] = 0;
  56. out[14] = 0;
  57. out[15] = 1;
  58. return out;
  59. }
  60. export function setIdentity(mat: Mat4): Mat4 {
  61. mat[0] = 1;
  62. mat[1] = 0;
  63. mat[2] = 0;
  64. mat[3] = 0;
  65. mat[4] = 0;
  66. mat[5] = 1;
  67. mat[6] = 0;
  68. mat[7] = 0;
  69. mat[8] = 0;
  70. mat[9] = 0;
  71. mat[10] = 1;
  72. mat[11] = 0;
  73. mat[12] = 0;
  74. mat[13] = 0;
  75. mat[14] = 0;
  76. mat[15] = 1;
  77. return mat;
  78. }
  79. export function setZero(mat: Mat4): Mat4 {
  80. for (let i = 0; i < 16; i++) mat[i] = 0;
  81. return mat;
  82. }
  83. export function ofRows(rows: number[][]): Mat4 {
  84. const out = zero();
  85. for (let i = 0; i < 4; i++) {
  86. const r = rows[i];
  87. for (let j = 0; j < 4; j++) {
  88. out[4 * j + i] = r[j];
  89. }
  90. }
  91. return out;
  92. }
  93. const _id = identity();
  94. export function isIdentity(m: Mat4, eps?: number) {
  95. return areEqual(m, _id, typeof eps === 'undefined' ? EPSILON : eps);
  96. }
  97. export function hasNaN(m: Mat4) {
  98. for (let i = 0; i < 16; i++) if (isNaN(m[i])) return true;
  99. return false;
  100. }
  101. export function areEqual(a: Mat4, b: Mat4, eps: number) {
  102. for (let i = 0; i < 16; i++) {
  103. if (Math.abs(a[i] - b[i]) > eps) return false;
  104. }
  105. return true;
  106. }
  107. export function setValue(a: Mat4, i: number, j: number, value: number) {
  108. a[4 * j + i] = value;
  109. }
  110. export function getValue(a: Mat4, i: number, j: number) {
  111. return a[4 * j + i];
  112. }
  113. export function toArray<T extends NumberArray>(a: Mat4, out: T, offset: number) {
  114. out[offset + 0] = a[0];
  115. out[offset + 1] = a[1];
  116. out[offset + 2] = a[2];
  117. out[offset + 3] = a[3];
  118. out[offset + 4] = a[4];
  119. out[offset + 5] = a[5];
  120. out[offset + 6] = a[6];
  121. out[offset + 7] = a[7];
  122. out[offset + 8] = a[8];
  123. out[offset + 9] = a[9];
  124. out[offset + 10] = a[10];
  125. out[offset + 11] = a[11];
  126. out[offset + 12] = a[12];
  127. out[offset + 13] = a[13];
  128. out[offset + 14] = a[14];
  129. out[offset + 15] = a[15];
  130. return out;
  131. }
  132. export function fromArray(a: Mat4, array: NumberArray, offset: number) {
  133. a[0] = array[offset + 0];
  134. a[1] = array[offset + 1];
  135. a[2] = array[offset + 2];
  136. a[3] = array[offset + 3];
  137. a[4] = array[offset + 4];
  138. a[5] = array[offset + 5];
  139. a[6] = array[offset + 6];
  140. a[7] = array[offset + 7];
  141. a[8] = array[offset + 8];
  142. a[9] = array[offset + 9];
  143. a[10] = array[offset + 10];
  144. a[11] = array[offset + 11];
  145. a[12] = array[offset + 12];
  146. a[13] = array[offset + 13];
  147. a[14] = array[offset + 14];
  148. a[15] = array[offset + 15];
  149. return a;
  150. }
  151. export function fromBasis(a: Mat4, x: Vec3, y: Vec3, z: Vec3) {
  152. setZero(a);
  153. setValue(a, 0, 0, x[0]);
  154. setValue(a, 1, 0, x[1]);
  155. setValue(a, 2, 0, x[2]);
  156. setValue(a, 0, 1, y[0]);
  157. setValue(a, 1, 1, y[1]);
  158. setValue(a, 2, 1, y[2]);
  159. setValue(a, 0, 2, z[0]);
  160. setValue(a, 1, 2, z[1]);
  161. setValue(a, 2, 2, z[2]);
  162. setValue(a, 3, 3, 1);
  163. return a;
  164. }
  165. export function copy(out: Mat4, a: Mat4) {
  166. out[0] = a[0];
  167. out[1] = a[1];
  168. out[2] = a[2];
  169. out[3] = a[3];
  170. out[4] = a[4];
  171. out[5] = a[5];
  172. out[6] = a[6];
  173. out[7] = a[7];
  174. out[8] = a[8];
  175. out[9] = a[9];
  176. out[10] = a[10];
  177. out[11] = a[11];
  178. out[12] = a[12];
  179. out[13] = a[13];
  180. out[14] = a[14];
  181. out[15] = a[15];
  182. return out;
  183. }
  184. export function clone(a: Mat4) {
  185. return copy(zero(), a);
  186. }
  187. /**
  188. * Returns the translation vector component of a transformation matrix.
  189. */
  190. export function getTranslation(out: Vec3, mat: Mat4) {
  191. out[0] = mat[12];
  192. out[1] = mat[13];
  193. out[2] = mat[14];
  194. return out;
  195. }
  196. /**
  197. * Returns the scaling factor component of a transformation matrix.
  198. */
  199. export function getScaling(out: Vec3, mat: Mat4) {
  200. const m11 = mat[0];
  201. const m12 = mat[1];
  202. const m13 = mat[2];
  203. const m21 = mat[4];
  204. const m22 = mat[5];
  205. const m23 = mat[6];
  206. const m31 = mat[8];
  207. const m32 = mat[9];
  208. const m33 = mat[10];
  209. out[0] = Math.sqrt(m11 * m11 + m12 * m12 + m13 * m13);
  210. out[1] = Math.sqrt(m21 * m21 + m22 * m22 + m23 * m23);
  211. out[2] = Math.sqrt(m31 * m31 + m32 * m32 + m33 * m33);
  212. return out;
  213. }
  214. /**
  215. * Returns a quaternion representing the rotational component of a transformation matrix.
  216. */
  217. export function getRotation(out: Quat, mat: Mat4) {
  218. // Algorithm taken from http://www.euclideanspace.com/maths/geometry/rotations/conversions/matrixToQuaternion/index.htm
  219. const trace = mat[0] + mat[5] + mat[10];
  220. let S = 0;
  221. if (trace > 0) {
  222. S = Math.sqrt(trace + 1.0) * 2;
  223. out[3] = 0.25 * S;
  224. out[0] = (mat[6] - mat[9]) / S;
  225. out[1] = (mat[8] - mat[2]) / S;
  226. out[2] = (mat[1] - mat[4]) / S;
  227. } else if ((mat[0] > mat[5]) && (mat[0] > mat[10])) {
  228. S = Math.sqrt(1.0 + mat[0] - mat[5] - mat[10]) * 2;
  229. out[3] = (mat[6] - mat[9]) / S;
  230. out[0] = 0.25 * S;
  231. out[1] = (mat[1] + mat[4]) / S;
  232. out[2] = (mat[8] + mat[2]) / S;
  233. } else if (mat[5] > mat[10]) {
  234. S = Math.sqrt(1.0 + mat[5] - mat[0] - mat[10]) * 2;
  235. out[3] = (mat[8] - mat[2]) / S;
  236. out[0] = (mat[1] + mat[4]) / S;
  237. out[1] = 0.25 * S;
  238. out[2] = (mat[6] + mat[9]) / S;
  239. } else {
  240. S = Math.sqrt(1.0 + mat[10] - mat[0] - mat[5]) * 2;
  241. out[3] = (mat[1] - mat[4]) / S;
  242. out[0] = (mat[8] + mat[2]) / S;
  243. out[1] = (mat[6] + mat[9]) / S;
  244. out[2] = 0.25 * S;
  245. }
  246. return out;
  247. }
  248. export function extractRotation(out: Mat4, mat: Mat4) {
  249. const scaleX = 1 / Math.sqrt(mat[0] * mat[0] + mat[1] * mat[1] + mat[2] * mat[2]);
  250. const scaleY = 1 / Math.sqrt(mat[4] * mat[4] + mat[5] * mat[5] + mat[6] * mat[6]);
  251. const scaleZ = 1 / Math.sqrt(mat[8] * mat[8] + mat[9] * mat[9] + mat[10] * mat[10]);
  252. out[0] = mat[0] * scaleX;
  253. out[1] = mat[1] * scaleX;
  254. out[2] = mat[2] * scaleX;
  255. out[3] = 0;
  256. out[4] = mat[4] * scaleY;
  257. out[5] = mat[5] * scaleY;
  258. out[6] = mat[6] * scaleY;
  259. out[7] = 0;
  260. out[8] = mat[8] * scaleZ;
  261. out[9] = mat[9] * scaleZ;
  262. out[10] = mat[10] * scaleZ;
  263. out[11] = 0;
  264. out[12] = 0;
  265. out[13] = 0;
  266. out[14] = 0;
  267. out[15] = 1;
  268. return out;
  269. }
  270. export function transpose(out: Mat4, a: Mat4) {
  271. // If we are transposing ourselves we can skip a few steps but have to cache some values
  272. if (out === a) {
  273. const a01 = a[1], a02 = a[2], a03 = a[3];
  274. const a12 = a[6], a13 = a[7];
  275. const a23 = a[11];
  276. out[1] = a[4];
  277. out[2] = a[8];
  278. out[3] = a[12];
  279. out[4] = a01;
  280. out[6] = a[9];
  281. out[7] = a[13];
  282. out[8] = a02;
  283. out[9] = a12;
  284. out[11] = a[14];
  285. out[12] = a03;
  286. out[13] = a13;
  287. out[14] = a23;
  288. } else {
  289. out[0] = a[0];
  290. out[1] = a[4];
  291. out[2] = a[8];
  292. out[3] = a[12];
  293. out[4] = a[1];
  294. out[5] = a[5];
  295. out[6] = a[9];
  296. out[7] = a[13];
  297. out[8] = a[2];
  298. out[9] = a[6];
  299. out[10] = a[10];
  300. out[11] = a[14];
  301. out[12] = a[3];
  302. out[13] = a[7];
  303. out[14] = a[11];
  304. out[15] = a[15];
  305. }
  306. return out;
  307. }
  308. export function tryInvert(out: Mat4, a: Mat4) {
  309. const a00 = a[0], a01 = a[1], a02 = a[2], a03 = a[3],
  310. a10 = a[4], a11 = a[5], a12 = a[6], a13 = a[7],
  311. a20 = a[8], a21 = a[9], a22 = a[10], a23 = a[11],
  312. a30 = a[12], a31 = a[13], a32 = a[14], a33 = a[15],
  313. b00 = a00 * a11 - a01 * a10,
  314. b01 = a00 * a12 - a02 * a10,
  315. b02 = a00 * a13 - a03 * a10,
  316. b03 = a01 * a12 - a02 * a11,
  317. b04 = a01 * a13 - a03 * a11,
  318. b05 = a02 * a13 - a03 * a12,
  319. b06 = a20 * a31 - a21 * a30,
  320. b07 = a20 * a32 - a22 * a30,
  321. b08 = a20 * a33 - a23 * a30,
  322. b09 = a21 * a32 - a22 * a31,
  323. b10 = a21 * a33 - a23 * a31,
  324. b11 = a22 * a33 - a23 * a32;
  325. // Calculate the determinant
  326. let det = b00 * b11 - b01 * b10 + b02 * b09 + b03 * b08 - b04 * b07 + b05 * b06;
  327. if (!det) {
  328. return false;
  329. }
  330. det = 1.0 / det;
  331. out[0] = (a11 * b11 - a12 * b10 + a13 * b09) * det;
  332. out[1] = (a02 * b10 - a01 * b11 - a03 * b09) * det;
  333. out[2] = (a31 * b05 - a32 * b04 + a33 * b03) * det;
  334. out[3] = (a22 * b04 - a21 * b05 - a23 * b03) * det;
  335. out[4] = (a12 * b08 - a10 * b11 - a13 * b07) * det;
  336. out[5] = (a00 * b11 - a02 * b08 + a03 * b07) * det;
  337. out[6] = (a32 * b02 - a30 * b05 - a33 * b01) * det;
  338. out[7] = (a20 * b05 - a22 * b02 + a23 * b01) * det;
  339. out[8] = (a10 * b10 - a11 * b08 + a13 * b06) * det;
  340. out[9] = (a01 * b08 - a00 * b10 - a03 * b06) * det;
  341. out[10] = (a30 * b04 - a31 * b02 + a33 * b00) * det;
  342. out[11] = (a21 * b02 - a20 * b04 - a23 * b00) * det;
  343. out[12] = (a11 * b07 - a10 * b09 - a12 * b06) * det;
  344. out[13] = (a00 * b09 - a01 * b07 + a02 * b06) * det;
  345. out[14] = (a31 * b01 - a30 * b03 - a32 * b00) * det;
  346. out[15] = (a20 * b03 - a21 * b01 + a22 * b00) * det;
  347. return true;
  348. }
  349. export function invert(out: Mat4, a: Mat4) {
  350. if (!tryInvert(out, a)) {
  351. console.warn('non-invertible matrix.', a);
  352. }
  353. return out;
  354. }
  355. export function mul(out: Mat4, a: Mat4, b: Mat4) {
  356. const a00 = a[0], a01 = a[1], a02 = a[2], a03 = a[3],
  357. a10 = a[4], a11 = a[5], a12 = a[6], a13 = a[7],
  358. a20 = a[8], a21 = a[9], a22 = a[10], a23 = a[11],
  359. a30 = a[12], a31 = a[13], a32 = a[14], a33 = a[15];
  360. // Cache only the current line of the second matrix
  361. let b0 = b[0], b1 = b[1], b2 = b[2], b3 = b[3];
  362. out[0] = b0 * a00 + b1 * a10 + b2 * a20 + b3 * a30;
  363. out[1] = b0 * a01 + b1 * a11 + b2 * a21 + b3 * a31;
  364. out[2] = b0 * a02 + b1 * a12 + b2 * a22 + b3 * a32;
  365. out[3] = b0 * a03 + b1 * a13 + b2 * a23 + b3 * a33;
  366. b0 = b[4]; b1 = b[5]; b2 = b[6]; b3 = b[7];
  367. out[4] = b0 * a00 + b1 * a10 + b2 * a20 + b3 * a30;
  368. out[5] = b0 * a01 + b1 * a11 + b2 * a21 + b3 * a31;
  369. out[6] = b0 * a02 + b1 * a12 + b2 * a22 + b3 * a32;
  370. out[7] = b0 * a03 + b1 * a13 + b2 * a23 + b3 * a33;
  371. b0 = b[8]; b1 = b[9]; b2 = b[10]; b3 = b[11];
  372. out[8] = b0 * a00 + b1 * a10 + b2 * a20 + b3 * a30;
  373. out[9] = b0 * a01 + b1 * a11 + b2 * a21 + b3 * a31;
  374. out[10] = b0 * a02 + b1 * a12 + b2 * a22 + b3 * a32;
  375. out[11] = b0 * a03 + b1 * a13 + b2 * a23 + b3 * a33;
  376. b0 = b[12]; b1 = b[13]; b2 = b[14]; b3 = b[15];
  377. out[12] = b0 * a00 + b1 * a10 + b2 * a20 + b3 * a30;
  378. out[13] = b0 * a01 + b1 * a11 + b2 * a21 + b3 * a31;
  379. out[14] = b0 * a02 + b1 * a12 + b2 * a22 + b3 * a32;
  380. out[15] = b0 * a03 + b1 * a13 + b2 * a23 + b3 * a33;
  381. return out;
  382. }
  383. /**
  384. * Like `mul` but with offsets into arrays
  385. */
  386. export function mulOffset(out: NumberArray, a: NumberArray, b: NumberArray, oOut: number, oA: number, oB: number) {
  387. const a00 = a[0 + oA], a01 = a[1 + oA], a02 = a[2 + oA], a03 = a[3 + oA],
  388. a10 = a[4 + oA], a11 = a[5 + oA], a12 = a[6 + oA], a13 = a[7 + oA],
  389. a20 = a[8 + oA], a21 = a[9 + oA], a22 = a[10 + oA], a23 = a[11 + oA],
  390. a30 = a[12 + oA], a31 = a[13 + oA], a32 = a[14 + oA], a33 = a[15 + oA];
  391. // Cache only the current line of the second matrix
  392. let b0 = b[0 + oB], b1 = b[1 + oB], b2 = b[2 + oB], b3 = b[3 + oB];
  393. out[0 + oOut] = b0 * a00 + b1 * a10 + b2 * a20 + b3 * a30;
  394. out[1 + oOut] = b0 * a01 + b1 * a11 + b2 * a21 + b3 * a31;
  395. out[2 + oOut] = b0 * a02 + b1 * a12 + b2 * a22 + b3 * a32;
  396. out[3 + oOut] = b0 * a03 + b1 * a13 + b2 * a23 + b3 * a33;
  397. b0 = b[4 + oB]; b1 = b[5 + oB]; b2 = b[6 + oB]; b3 = b[7 + oB];
  398. out[4 + oOut] = b0 * a00 + b1 * a10 + b2 * a20 + b3 * a30;
  399. out[5 + oOut] = b0 * a01 + b1 * a11 + b2 * a21 + b3 * a31;
  400. out[6 + oOut] = b0 * a02 + b1 * a12 + b2 * a22 + b3 * a32;
  401. out[7 + oOut] = b0 * a03 + b1 * a13 + b2 * a23 + b3 * a33;
  402. b0 = b[8 + oB]; b1 = b[9 + oB]; b2 = b[10 + oB]; b3 = b[11 + oB];
  403. out[8 + oOut] = b0 * a00 + b1 * a10 + b2 * a20 + b3 * a30;
  404. out[9 + oOut] = b0 * a01 + b1 * a11 + b2 * a21 + b3 * a31;
  405. out[10 + oOut] = b0 * a02 + b1 * a12 + b2 * a22 + b3 * a32;
  406. out[11 + oOut] = b0 * a03 + b1 * a13 + b2 * a23 + b3 * a33;
  407. b0 = b[12 + oB]; b1 = b[13 + oB]; b2 = b[14 + oB]; b3 = b[15 + oB];
  408. out[12 + oOut] = b0 * a00 + b1 * a10 + b2 * a20 + b3 * a30;
  409. out[13 + oOut] = b0 * a01 + b1 * a11 + b2 * a21 + b3 * a31;
  410. out[14 + oOut] = b0 * a02 + b1 * a12 + b2 * a22 + b3 * a32;
  411. out[15 + oOut] = b0 * a03 + b1 * a13 + b2 * a23 + b3 * a33;
  412. return out;
  413. }
  414. export function mul3(out: Mat4, a: Mat4, b: Mat4, c: Mat4) {
  415. return mul(out, mul(out, a, b), c);
  416. }
  417. /** Translate a Mat4 by the given Vec3 */
  418. export function translate(out: Mat4, a: Mat4, v: Vec3) {
  419. const x = v[0], y = v[1], z = v[2];
  420. let a00: number, a01: number, a02: number, a03: number,
  421. a10: number, a11: number, a12: number, a13: number,
  422. a20: number, a21: number, a22: number, a23: number;
  423. if (a === out) {
  424. out[12] = a[0] * x + a[4] * y + a[8] * z + a[12];
  425. out[13] = a[1] * x + a[5] * y + a[9] * z + a[13];
  426. out[14] = a[2] * x + a[6] * y + a[10] * z + a[14];
  427. out[15] = a[3] * x + a[7] * y + a[11] * z + a[15];
  428. } else {
  429. a00 = a[0]; a01 = a[1]; a02 = a[2]; a03 = a[3];
  430. a10 = a[4]; a11 = a[5]; a12 = a[6]; a13 = a[7];
  431. a20 = a[8]; a21 = a[9]; a22 = a[10]; a23 = a[11];
  432. out[0] = a00; out[1] = a01; out[2] = a02; out[3] = a03;
  433. out[4] = a10; out[5] = a11; out[6] = a12; out[7] = a13;
  434. out[8] = a20; out[9] = a21; out[10] = a22; out[11] = a23;
  435. out[12] = a00 * x + a10 * y + a20 * z + a[12];
  436. out[13] = a01 * x + a11 * y + a21 * z + a[13];
  437. out[14] = a02 * x + a12 * y + a22 * z + a[14];
  438. out[15] = a03 * x + a13 * y + a23 * z + a[15];
  439. }
  440. return out;
  441. }
  442. export function fromTranslation(out: Mat4, v: Vec3) {
  443. out[0] = 1;
  444. out[1] = 0;
  445. out[2] = 0;
  446. out[3] = 0;
  447. out[4] = 0;
  448. out[5] = 1;
  449. out[6] = 0;
  450. out[7] = 0;
  451. out[8] = 0;
  452. out[9] = 0;
  453. out[10] = 1;
  454. out[11] = 0;
  455. out[12] = v[0];
  456. out[13] = v[1];
  457. out[14] = v[2];
  458. out[15] = 1;
  459. return out;
  460. }
  461. export function setTranslation(out: Mat4, v: Vec3) {
  462. out[12] = v[0];
  463. out[13] = v[1];
  464. out[14] = v[2];
  465. return out;
  466. }
  467. /**
  468. * Sets the specified quaternion with values corresponding to the given
  469. * axes. Each axis is a vec3 and is expected to be unit length and
  470. * perpendicular to all other specified axes.
  471. */
  472. export function setAxes(out: Mat4, view: Vec3, right: Vec3, up: Vec3) {
  473. out[0] = right[0];
  474. out[4] = right[1];
  475. out[8] = right[2];
  476. out[1] = up[0];
  477. out[5] = up[1];
  478. out[9] = up[2];
  479. out[2] = view[0];
  480. out[6] = view[1];
  481. out[10] = view[2];
  482. return out;
  483. }
  484. export function rotate(out: Mat4, a: Mat4, rad: number, axis: Vec3) {
  485. let x = axis[0], y = axis[1], z = axis[2];
  486. let len = Math.sqrt(x * x + y * y + z * z);
  487. if (Math.abs(len) < EPSILON) {
  488. return identity();
  489. }
  490. len = 1 / len;
  491. x *= len;
  492. y *= len;
  493. z *= len;
  494. const s = Math.sin(rad);
  495. const c = Math.cos(rad);
  496. const t = 1 - c;
  497. const a00 = a[0], a01 = a[1], a02 = a[2], a03 = a[3];
  498. const a10 = a[4], a11 = a[5], a12 = a[6], a13 = a[7];
  499. const a20 = a[8], a21 = a[9], a22 = a[10], a23 = a[11];
  500. // Construct the elements of the rotation matrix
  501. const b00 = x * x * t + c, b01 = y * x * t + z * s, b02 = z * x * t - y * s;
  502. const b10 = x * y * t - z * s, b11 = y * y * t + c, b12 = z * y * t + x * s;
  503. const b20 = x * z * t + y * s, b21 = y * z * t - x * s, b22 = z * z * t + c;
  504. // Perform rotation-specific matrix multiplication
  505. out[0] = a00 * b00 + a10 * b01 + a20 * b02;
  506. out[1] = a01 * b00 + a11 * b01 + a21 * b02;
  507. out[2] = a02 * b00 + a12 * b01 + a22 * b02;
  508. out[3] = a03 * b00 + a13 * b01 + a23 * b02;
  509. out[4] = a00 * b10 + a10 * b11 + a20 * b12;
  510. out[5] = a01 * b10 + a11 * b11 + a21 * b12;
  511. out[6] = a02 * b10 + a12 * b11 + a22 * b12;
  512. out[7] = a03 * b10 + a13 * b11 + a23 * b12;
  513. out[8] = a00 * b20 + a10 * b21 + a20 * b22;
  514. out[9] = a01 * b20 + a11 * b21 + a21 * b22;
  515. out[10] = a02 * b20 + a12 * b21 + a22 * b22;
  516. out[11] = a03 * b20 + a13 * b21 + a23 * b22;
  517. if (a !== out) { // If the source and destination differ, copy the unchanged last row
  518. out[12] = a[12];
  519. out[13] = a[13];
  520. out[14] = a[14];
  521. out[15] = a[15];
  522. }
  523. return out;
  524. }
  525. export function fromRotation(out: Mat4, rad: number, axis: Vec3) {
  526. let x = axis[0], y = axis[1], z = axis[2];
  527. let len = Math.sqrt(x * x + y * y + z * z);
  528. if (Math.abs(len) < EPSILON) { return setIdentity(out); }
  529. len = 1 / len;
  530. x *= len;
  531. y *= len;
  532. z *= len;
  533. const s = Math.sin(rad);
  534. const c = Math.cos(rad);
  535. const t = 1 - c;
  536. // Perform rotation-specific matrix multiplication
  537. out[0] = x * x * t + c;
  538. out[1] = y * x * t + z * s;
  539. out[2] = z * x * t - y * s;
  540. out[3] = 0;
  541. out[4] = x * y * t - z * s;
  542. out[5] = y * y * t + c;
  543. out[6] = z * y * t + x * s;
  544. out[7] = 0;
  545. out[8] = x * z * t + y * s;
  546. out[9] = y * z * t - x * s;
  547. out[10] = z * z * t + c;
  548. out[11] = 0;
  549. out[12] = 0;
  550. out[13] = 0;
  551. out[14] = 0;
  552. out[15] = 1;
  553. return out;
  554. }
  555. export function scale(out: Mat4, a: Mat4, v: Vec3) {
  556. const x = v[0], y = v[1], z = v[2];
  557. out[0] = a[0] * x;
  558. out[1] = a[1] * x;
  559. out[2] = a[2] * x;
  560. out[3] = a[3] * x;
  561. out[4] = a[4] * y;
  562. out[5] = a[5] * y;
  563. out[6] = a[6] * y;
  564. out[7] = a[7] * y;
  565. out[8] = a[8] * z;
  566. out[9] = a[9] * z;
  567. out[10] = a[10] * z;
  568. out[11] = a[11] * z;
  569. out[12] = a[12];
  570. out[13] = a[13];
  571. out[14] = a[14];
  572. out[15] = a[15];
  573. return out;
  574. }
  575. export function scaleUniformly(out: Mat4, a: Mat4, scale: number) {
  576. out[0] = a[0] * scale;
  577. out[1] = a[1] * scale;
  578. out[2] = a[2] * scale;
  579. out[3] = a[3] * scale;
  580. out[4] = a[4] * scale;
  581. out[5] = a[5] * scale;
  582. out[6] = a[6] * scale;
  583. out[7] = a[7] * scale;
  584. out[8] = a[8] * scale;
  585. out[9] = a[9] * scale;
  586. out[10] = a[10] * scale;
  587. out[11] = a[11] * scale;
  588. out[12] = a[12];
  589. out[13] = a[13];
  590. out[14] = a[14];
  591. out[15] = a[15];
  592. return out;
  593. }
  594. export function fromScaling(out: Mat4, v: Vec3) {
  595. out[0] = v[0];
  596. out[1] = 0;
  597. out[2] = 0;
  598. out[3] = 0;
  599. out[4] = 0;
  600. out[5] = v[1];
  601. out[6] = 0;
  602. out[7] = 0;
  603. out[8] = 0;
  604. out[9] = 0;
  605. out[10] = v[2];
  606. out[11] = 0;
  607. out[12] = 0;
  608. out[13] = 0;
  609. out[14] = 0;
  610. out[15] = 1;
  611. return out;
  612. }
  613. export function fromUniformScaling(out: Mat4, scale: number) {
  614. out[0] = scale;
  615. out[1] = 0;
  616. out[2] = 0;
  617. out[3] = 0;
  618. out[4] = 0;
  619. out[5] = scale;
  620. out[6] = 0;
  621. out[7] = 0;
  622. out[8] = 0;
  623. out[9] = 0;
  624. out[10] = scale;
  625. out[11] = 0;
  626. out[12] = 0;
  627. out[13] = 0;
  628. out[14] = 0;
  629. out[15] = 1;
  630. return out;
  631. }
  632. /**
  633. * Copies the mat3 into upper-left 3x3 values.
  634. */
  635. export function fromMat3(out: Mat4, a: Mat3) {
  636. out[0] = a[0];
  637. out[1] = a[1];
  638. out[2] = a[2];
  639. out[4] = a[3];
  640. out[5] = a[4];
  641. out[6] = a[5];
  642. out[8] = a[6];
  643. out[9] = a[7];
  644. out[10] = a[8];
  645. return out;
  646. }
  647. export function compose(out: Mat4, position: Vec3, quaternion: Quat, scale: Vec3) {
  648. const [x, y, z, w] = quaternion;
  649. const x2 = x + x, y2 = y + y, z2 = z + z;
  650. const xx = x * x2, xy = x * y2, xz = x * z2;
  651. const yy = y * y2, yz = y * z2, zz = z * z2;
  652. const wx = w * x2, wy = w * y2, wz = w * z2;
  653. const [sx, sy, sz] = scale;
  654. out[0] = (1 - (yy + zz)) * sx;
  655. out[1] = (xy + wz) * sx;
  656. out[2] = (xz - wy) * sx;
  657. out[3] = 0;
  658. out[4] = (xy - wz) * sy;
  659. out[5] = (1 - (xx + zz)) * sy;
  660. out[6] = (yz + wx) * sy;
  661. out[7] = 0;
  662. out[8] = (xz + wy) * sz;
  663. out[9] = (yz - wx) * sz;
  664. out[10] = (1 - (xx + yy)) * sz;
  665. out[11] = 0;
  666. out[12] = position[0];
  667. out[13] = position[1];
  668. out[14] = position[2];
  669. out[15] = 1;
  670. return out;
  671. }
  672. const _v3 = [0, 0, 0] as unknown as Vec3;
  673. const _m4 = zero();
  674. export function decompose(m: Mat4, position: Vec3, quaternion: Quat, scale: Vec3) {
  675. let sx = Vec3.magnitude(Vec3.set(_v3, m[0], m[1], m[2]));
  676. const sy = Vec3.magnitude(Vec3.set(_v3, m[4], m[5], m[6]));
  677. const sz = Vec3.magnitude(Vec3.set(_v3, m[8], m[9], m[10]));
  678. // if determine is negative, we need to invert one scale
  679. const det = determinant(m);
  680. if (det < 0) sx = -sx;
  681. position[0] = m[12];
  682. position[1] = m[13];
  683. position[2] = m[14];
  684. // scale the rotation part
  685. copy(_m4, m);
  686. const invSX = 1 / sx;
  687. const invSY = 1 / sy;
  688. const invSZ = 1 / sz;
  689. _m4[0] *= invSX;
  690. _m4[1] *= invSX;
  691. _m4[2] *= invSX;
  692. _m4[4] *= invSY;
  693. _m4[5] *= invSY;
  694. _m4[6] *= invSY;
  695. _m4[8] *= invSZ;
  696. _m4[9] *= invSZ;
  697. _m4[10] *= invSZ;
  698. getRotation(quaternion, _m4);
  699. scale[0] = sx;
  700. scale[1] = sy;
  701. scale[2] = sz;
  702. return m;
  703. }
  704. export function makeTable(m: Mat4) {
  705. let ret = '';
  706. for (let i = 0; i < 4; i++) {
  707. for (let j = 0; j < 4; j++) {
  708. ret += m[4 * j + i].toString();
  709. if (j < 3) ret += ' ';
  710. }
  711. if (i < 3) ret += '\n';
  712. }
  713. return ret;
  714. }
  715. export function determinant(a: Mat4) {
  716. const a00 = a[0], a01 = a[1], a02 = a[2], a03 = a[3],
  717. a10 = a[4], a11 = a[5], a12 = a[6], a13 = a[7],
  718. a20 = a[8], a21 = a[9], a22 = a[10], a23 = a[11],
  719. a30 = a[12], a31 = a[13], a32 = a[14], a33 = a[15],
  720. b00 = a00 * a11 - a01 * a10,
  721. b01 = a00 * a12 - a02 * a10,
  722. b02 = a00 * a13 - a03 * a10,
  723. b03 = a01 * a12 - a02 * a11,
  724. b04 = a01 * a13 - a03 * a11,
  725. b05 = a02 * a13 - a03 * a12,
  726. b06 = a20 * a31 - a21 * a30,
  727. b07 = a20 * a32 - a22 * a30,
  728. b08 = a20 * a33 - a23 * a30,
  729. b09 = a21 * a32 - a22 * a31,
  730. b10 = a21 * a33 - a23 * a31,
  731. b11 = a22 * a33 - a23 * a32;
  732. // Calculate the determinant
  733. return b00 * b11 - b01 * b10 + b02 * b09 + b03 * b08 - b04 * b07 + b05 * b06;
  734. }
  735. /**
  736. * Check if the matrix has the form
  737. * [ Rotation Translation ]
  738. * [ 0 1 ]
  739. *
  740. * Allows for improper rotations
  741. */
  742. export function isRotationAndTranslation(a: Mat4, eps?: number) {
  743. return _isRotationAndTranslation(a, typeof eps !== 'undefined' ? eps : EPSILON);
  744. }
  745. function _isRotationAndTranslation(a: Mat4, eps: number) {
  746. const a00 = a[0], a01 = a[1], a02 = a[2], a03 = a[3],
  747. a10 = a[4], a11 = a[5], a12 = a[6], a13 = a[7],
  748. a20 = a[8], a21 = a[9], a22 = a[10], a23 = a[11],
  749. a33 = a[15];
  750. if (!equalEps(a33, 1, eps) || !equalEps(a03, 0, eps) || !equalEps(a13, 0, eps) || !equalEps(a23, 0, eps)) {
  751. return false;
  752. }
  753. // use `abs` to allow for improper rotations
  754. const det3x3 = Math.abs(a00 * (a11 * a22 - a12 * a21) - a01 * (a10 * a22 - a12 * a20) + a02 * (a10 * a21 - a11 * a20));
  755. if (!equalEps(det3x3, 1, eps)) {
  756. return false;
  757. }
  758. return true;
  759. }
  760. /**
  761. * Check if the matrix has only translation and uniform scaling
  762. * [ S 0 0 X ]
  763. * [ 0 S 0 Y ]
  764. * [ 0 0 S Z ]
  765. * [ 0 0 0 1 ]
  766. */
  767. export function isTranslationAndUniformScaling(a: Mat4, eps?: number) {
  768. return _isTranslationAndUniformScaling(a, typeof eps !== 'undefined' ? eps : EPSILON);
  769. }
  770. function _isTranslationAndUniformScaling(a: Mat4, eps: number) {
  771. const a00 = a[0];
  772. return (
  773. // 0 base scaling
  774. equalEps(a[1], 0, eps) &&
  775. equalEps(a[2], 0, eps) &&
  776. equalEps(a[3], 0, eps) &&
  777. equalEps(a[4], 0, eps) &&
  778. equalEps(a[5], a00, eps) &&
  779. equalEps(a[6], 0, eps) &&
  780. equalEps(a[7], 0, eps) &&
  781. equalEps(a[8], 0, eps) &&
  782. equalEps(a[9], 0, eps) &&
  783. equalEps(a[10], a00, eps) &&
  784. equalEps(a[11], 0, eps) &&
  785. // 12, 13, 14 translation can be anything
  786. equalEps(a[15], 1, eps)
  787. );
  788. }
  789. export function fromQuat(out: Mat4, q: Quat) {
  790. const x = q[0], y = q[1], z = q[2], w = q[3];
  791. const x2 = x + x;
  792. const y2 = y + y;
  793. const z2 = z + z;
  794. const xx = x * x2;
  795. const yx = y * x2;
  796. const yy = y * y2;
  797. const zx = z * x2;
  798. const zy = z * y2;
  799. const zz = z * z2;
  800. const wx = w * x2;
  801. const wy = w * y2;
  802. const wz = w * z2;
  803. out[0] = 1 - yy - zz;
  804. out[1] = yx + wz;
  805. out[2] = zx - wy;
  806. out[3] = 0;
  807. out[4] = yx - wz;
  808. out[5] = 1 - xx - zz;
  809. out[6] = zy + wx;
  810. out[7] = 0;
  811. out[8] = zx + wy;
  812. out[9] = zy - wx;
  813. out[10] = 1 - xx - yy;
  814. out[11] = 0;
  815. out[12] = 0;
  816. out[13] = 0;
  817. out[14] = 0;
  818. out[15] = 1;
  819. return out;
  820. }
  821. export function fromEuler(out: Mat4, euler: Euler, order: Euler.Order) {
  822. const x = euler[0], y = euler[1], z = euler[2];
  823. const a = Math.cos(x), b = Math.sin(x);
  824. const c = Math.cos(y), d = Math.sin(y);
  825. const e = Math.cos(z), f = Math.sin(z);
  826. if (order === 'XYZ') {
  827. const ae = a * e, af = a * f, be = b * e, bf = b * f;
  828. out[0] = c * e;
  829. out[4] = - c * f;
  830. out[8] = d;
  831. out[1] = af + be * d;
  832. out[5] = ae - bf * d;
  833. out[9] = - b * c;
  834. out[2] = bf - ae * d;
  835. out[6] = be + af * d;
  836. out[10] = a * c;
  837. } else if (order === 'YXZ') {
  838. const ce = c * e, cf = c * f, de = d * e, df = d * f;
  839. out[0] = ce + df * b;
  840. out[4] = de * b - cf;
  841. out[8] = a * d;
  842. out[1] = a * f;
  843. out[5] = a * e;
  844. out[9] = - b;
  845. out[2] = cf * b - de;
  846. out[6] = df + ce * b;
  847. out[10] = a * c;
  848. } else if (order === 'ZXY') {
  849. const ce = c * e, cf = c * f, de = d * e, df = d * f;
  850. out[0] = ce - df * b;
  851. out[4] = - a * f;
  852. out[8] = de + cf * b;
  853. out[1] = cf + de * b;
  854. out[5] = a * e;
  855. out[9] = df - ce * b;
  856. out[2] = - a * d;
  857. out[6] = b;
  858. out[10] = a * c;
  859. } else if (order === 'ZYX') {
  860. const ae = a * e, af = a * f, be = b * e, bf = b * f;
  861. out[0] = c * e;
  862. out[4] = be * d - af;
  863. out[8] = ae * d + bf;
  864. out[1] = c * f;
  865. out[5] = bf * d + ae;
  866. out[9] = af * d - be;
  867. out[2] = - d;
  868. out[6] = b * c;
  869. out[10] = a * c;
  870. } else if (order === 'YZX') {
  871. const ac = a * c, ad = a * d, bc = b * c, bd = b * d;
  872. out[0] = c * e;
  873. out[4] = bd - ac * f;
  874. out[8] = bc * f + ad;
  875. out[1] = f;
  876. out[5] = a * e;
  877. out[9] = - b * e;
  878. out[2] = - d * e;
  879. out[6] = ad * f + bc;
  880. out[10] = ac - bd * f;
  881. } else if (order === 'XZY') {
  882. const ac = a * c, ad = a * d, bc = b * c, bd = b * d;
  883. out[0] = c * e;
  884. out[4] = - f;
  885. out[8] = d * e;
  886. out[1] = ac * f + bd;
  887. out[5] = a * e;
  888. out[9] = ad * f - bc;
  889. out[2] = bc * f - ad;
  890. out[6] = b * e;
  891. out[10] = bd * f + ac;
  892. }
  893. // bottom row
  894. out[3] = 0;
  895. out[7] = 0;
  896. out[11] = 0;
  897. // last column
  898. out[12] = 0;
  899. out[13] = 0;
  900. out[14] = 0;
  901. out[15] = 1;
  902. return out;
  903. }
  904. /**
  905. * Generates a perspective projection (frustum) matrix with the given bounds
  906. */
  907. export function perspective(out: Mat4, left: number, right: number, top: number, bottom: number, near: number, far: number) {
  908. const x = 2 * near / (right - left);
  909. const y = 2 * near / (top - bottom);
  910. const a = (right + left) / (right - left);
  911. const b = (top + bottom) / (top - bottom);
  912. const c = -(far + near) / (far - near);
  913. const d = -2 * far * near / (far - near);
  914. out[0] = x;
  915. out[1] = 0;
  916. out[2] = 0;
  917. out[3] = 0;
  918. out[4] = 0;
  919. out[5] = y;
  920. out[6] = 0;
  921. out[7] = 0;
  922. out[8] = a;
  923. out[9] = b;
  924. out[10] = c;
  925. out[11] = -1;
  926. out[12] = 0;
  927. out[13] = 0;
  928. out[14] = d;
  929. out[15] = 0;
  930. return out;
  931. }
  932. /**
  933. * Generates a orthogonal projection matrix with the given bounds
  934. */
  935. export function ortho(out: Mat4, left: number, right: number, top: number, bottom: number, near: number, far: number) {
  936. const w = 1.0 / (right - left);
  937. const h = 1.0 / (top - bottom);
  938. const p = 1.0 / (far - near);
  939. const x = (right + left) * w;
  940. const y = (top + bottom) * h;
  941. const z = (far + near) * p;
  942. out[0] = 2 * w;
  943. out[1] = 0;
  944. out[2] = 0;
  945. out[3] = 0;
  946. out[4] = 0;
  947. out[5] = 2 * h;
  948. out[6] = 0;
  949. out[7] = 0;
  950. out[8] = 0;
  951. out[9] = 0;
  952. out[10] = -2 * p;
  953. out[11] = 0;
  954. out[12] = -x;
  955. out[13] = -y;
  956. out[14] = -z;
  957. out[15] = 1;
  958. return out;
  959. }
  960. /**
  961. * Generates a look-at matrix with the given eye position, focal point, and up axis
  962. */
  963. export function lookAt(out: Mat4, eye: Vec3, center: Vec3, up: Vec3) {
  964. let x0, x1, x2, y0, y1, y2, z0, z1, z2, len;
  965. const eyex = eye[0];
  966. const eyey = eye[1];
  967. const eyez = eye[2];
  968. const upx = up[0];
  969. const upy = up[1];
  970. const upz = up[2];
  971. const centerx = center[0];
  972. const centery = center[1];
  973. const centerz = center[2];
  974. if (Math.abs(eyex - centerx) < EPSILON &&
  975. Math.abs(eyey - centery) < EPSILON &&
  976. Math.abs(eyez - centerz) < EPSILON
  977. ) {
  978. return setIdentity(out);
  979. }
  980. z0 = eyex - centerx;
  981. z1 = eyey - centery;
  982. z2 = eyez - centerz;
  983. len = 1 / Math.sqrt(z0 * z0 + z1 * z1 + z2 * z2);
  984. z0 *= len;
  985. z1 *= len;
  986. z2 *= len;
  987. x0 = upy * z2 - upz * z1;
  988. x1 = upz * z0 - upx * z2;
  989. x2 = upx * z1 - upy * z0;
  990. len = Math.sqrt(x0 * x0 + x1 * x1 + x2 * x2);
  991. if (!len) {
  992. x0 = 0;
  993. x1 = 0;
  994. x2 = 0;
  995. } else {
  996. len = 1 / len;
  997. x0 *= len;
  998. x1 *= len;
  999. x2 *= len;
  1000. }
  1001. y0 = z1 * x2 - z2 * x1;
  1002. y1 = z2 * x0 - z0 * x2;
  1003. y2 = z0 * x1 - z1 * x0;
  1004. len = Math.sqrt(y0 * y0 + y1 * y1 + y2 * y2);
  1005. if (!len) {
  1006. y0 = 0;
  1007. y1 = 0;
  1008. y2 = 0;
  1009. } else {
  1010. len = 1 / len;
  1011. y0 *= len;
  1012. y1 *= len;
  1013. y2 *= len;
  1014. }
  1015. out[0] = x0;
  1016. out[1] = y0;
  1017. out[2] = z0;
  1018. out[3] = 0;
  1019. out[4] = x1;
  1020. out[5] = y1;
  1021. out[6] = z1;
  1022. out[7] = 0;
  1023. out[8] = x2;
  1024. out[9] = y2;
  1025. out[10] = z2;
  1026. out[11] = 0;
  1027. out[12] = -(x0 * eyex + x1 * eyey + x2 * eyez);
  1028. out[13] = -(y0 * eyex + y1 * eyey + y2 * eyez);
  1029. out[14] = -(z0 * eyex + z1 * eyey + z2 * eyez);
  1030. out[15] = 1;
  1031. return out;
  1032. }
  1033. /**
  1034. * Generates a matrix that makes something look at something else.
  1035. */
  1036. export function targetTo(out: Mat4, eye: Vec3, target: Vec3, up: Vec3) {
  1037. const eyex = eye[0],
  1038. eyey = eye[1],
  1039. eyez = eye[2],
  1040. upx = up[0],
  1041. upy = up[1],
  1042. upz = up[2];
  1043. let z0 = eyex - target[0],
  1044. z1 = eyey - target[1],
  1045. z2 = eyez - target[2];
  1046. let len = z0 * z0 + z1 * z1 + z2 * z2;
  1047. if (len > 0) {
  1048. len = 1 / Math.sqrt(len);
  1049. z0 *= len;
  1050. z1 *= len;
  1051. z2 *= len;
  1052. }
  1053. let x0 = upy * z2 - upz * z1,
  1054. x1 = upz * z0 - upx * z2,
  1055. x2 = upx * z1 - upy * z0;
  1056. len = x0 * x0 + x1 * x1 + x2 * x2;
  1057. if (len > 0) {
  1058. len = 1 / Math.sqrt(len);
  1059. x0 *= len;
  1060. x1 *= len;
  1061. x2 *= len;
  1062. }
  1063. out[0] = x0;
  1064. out[1] = x1;
  1065. out[2] = x2;
  1066. out[3] = 0;
  1067. out[4] = z1 * x2 - z2 * x1;
  1068. out[5] = z2 * x0 - z0 * x2;
  1069. out[6] = z0 * x1 - z1 * x0;
  1070. out[7] = 0;
  1071. out[8] = z0;
  1072. out[9] = z1;
  1073. out[10] = z2;
  1074. out[11] = 0;
  1075. out[12] = eyex;
  1076. out[13] = eyey;
  1077. out[14] = eyez;
  1078. out[15] = 1;
  1079. return out;
  1080. }
  1081. /**
  1082. * Perm is 0-indexed permutation
  1083. */
  1084. export function fromPermutation(out: Mat4, perm: number[]) {
  1085. setZero(out);
  1086. for (let i = 0; i < 4; i++) {
  1087. const p = perm[i];
  1088. setValue(out, i, p, 1);
  1089. }
  1090. return out;
  1091. }
  1092. export function getMaxScaleOnAxis(m: Mat4) {
  1093. const scaleXSq = m[0] * m[0] + m[1] * m[1] + m[2] * m[2];
  1094. const scaleYSq = m[4] * m[4] + m[5] * m[5] + m[6] * m[6];
  1095. const scaleZSq = m[8] * m[8] + m[9] * m[9] + m[10] * m[10];
  1096. return Math.sqrt(Math.max(scaleXSq, scaleYSq, scaleZSq));
  1097. }
  1098. const xAxis = [1, 0, 0] as unknown as Vec3;
  1099. const yAxis = [0, 1, 0] as unknown as Vec3;
  1100. const zAxis = [0, 0, 1] as unknown as Vec3;
  1101. /** Rotation matrix for 90deg around x-axis */
  1102. export const rotX90: ReadonlyMat4 = fromRotation(zero(), degToRad(90), xAxis);
  1103. /** Rotation matrix for 180deg around x-axis */
  1104. export const rotX180: ReadonlyMat4 = fromRotation(zero(), degToRad(180), xAxis);
  1105. /** Rotation matrix for 90deg around y-axis */
  1106. export const rotY90: ReadonlyMat4 = fromRotation(zero(), degToRad(90), yAxis);
  1107. /** Rotation matrix for 180deg around y-axis */
  1108. export const rotY180: ReadonlyMat4 = fromRotation(zero(), degToRad(180), yAxis);
  1109. /** Rotation matrix for 270deg around y-axis */
  1110. export const rotY270: ReadonlyMat4 = fromRotation(zero(), degToRad(270), yAxis);
  1111. /** Rotation matrix for 90deg around z-axis */
  1112. export const rotZ90: ReadonlyMat4 = fromRotation(zero(), degToRad(90), zAxis);
  1113. /** Rotation matrix for 180deg around z-axis */
  1114. export const rotZ180: ReadonlyMat4 = fromRotation(zero(), degToRad(180), zAxis);
  1115. /** Rotation matrix for 90deg around first x-axis and then y-axis */
  1116. export const rotXY90: ReadonlyMat4 = mul(zero(), rotX90, rotY90);
  1117. /** Rotation matrix for 90deg around first z-axis and then y-axis */
  1118. export const rotZY90: ReadonlyMat4 = mul(zero(), rotZ90, rotY90);
  1119. /** Rotation matrix for 90deg around first z-axis and then y-axis and then z-axis */
  1120. export const rotZYZ90: ReadonlyMat4 = mul(zero(), rotZY90, rotZ90);
  1121. /** Rotation matrix for 90deg around first z-axis and then 180deg around x-axis */
  1122. export const rotZ90X180: ReadonlyMat4 = mul(zero(), rotZ90, rotX180);
  1123. /** Rotation matrix for 90deg around first y-axis and then 180deg around z-axis */
  1124. export const rotY90Z180: ReadonlyMat4 = mul(zero(), rotY90, rotZ180);
  1125. /** Identity matrix */
  1126. export const id: ReadonlyMat4 = identity();
  1127. }
  1128. export { Mat4 };