mat4.ts 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978
  1. /**
  2. * Copyright (c) 2017-2018 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. interface Mat4 extends Array<number> { [d: number]: number, '@type': 'mat4', length: 16 }
  23. interface ReadonlyMat4 extends Array<number> { readonly [d: number]: number, '@type': 'mat4', length: 16 }
  24. function Mat4() {
  25. return Mat4.zero();
  26. }
  27. /**
  28. * Stores a 4x4 matrix in a column major (j * 4 + i indexing) format.
  29. */
  30. namespace Mat4 {
  31. export function zero(): Mat4 {
  32. // force double backing array by 0.1.
  33. const ret = [0.1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0];
  34. ret[0] = 0.0;
  35. return ret as any;
  36. }
  37. export function identity(): Mat4 {
  38. const out = zero();
  39. out[0] = 1;
  40. out[1] = 0;
  41. out[2] = 0;
  42. out[3] = 0;
  43. out[4] = 0;
  44. out[5] = 1;
  45. out[6] = 0;
  46. out[7] = 0;
  47. out[8] = 0;
  48. out[9] = 0;
  49. out[10] = 1;
  50. out[11] = 0;
  51. out[12] = 0;
  52. out[13] = 0;
  53. out[14] = 0;
  54. out[15] = 1;
  55. return out;
  56. }
  57. export function setIdentity(mat: Mat4): Mat4 {
  58. mat[0] = 1;
  59. mat[1] = 0;
  60. mat[2] = 0;
  61. mat[3] = 0;
  62. mat[4] = 0;
  63. mat[5] = 1;
  64. mat[6] = 0;
  65. mat[7] = 0;
  66. mat[8] = 0;
  67. mat[9] = 0;
  68. mat[10] = 1;
  69. mat[11] = 0;
  70. mat[12] = 0;
  71. mat[13] = 0;
  72. mat[14] = 0;
  73. mat[15] = 1;
  74. return mat;
  75. }
  76. export function setZero(mat: Mat4): Mat4 {
  77. for (let i = 0; i < 16; i++) mat[i] = 0;
  78. return mat;
  79. }
  80. export function ofRows(rows: number[][]): Mat4 {
  81. const out = zero();
  82. for (let i = 0; i < 4; i++) {
  83. const r = rows[i];
  84. for (let j = 0; j < 4; j++) {
  85. out[4 * j + i] = r[j];
  86. }
  87. }
  88. return out;
  89. }
  90. const _id = identity();
  91. export function isIdentity(m: Mat4, eps?: number) {
  92. return areEqual(m, _id, typeof eps === 'undefined' ? EPSILON.Value : eps);
  93. }
  94. export function hasNaN(m: Mat4) {
  95. for (let i = 0; i < 16; i++) if (isNaN(m[i])) return true
  96. return false
  97. }
  98. export function areEqual(a: Mat4, b: Mat4, eps: number) {
  99. for (let i = 0; i < 16; i++) {
  100. if (Math.abs(a[i] - b[i]) > eps) return false;
  101. }
  102. return true;
  103. }
  104. export function setValue(a: Mat4, i: number, j: number, value: number) {
  105. a[4 * j + i] = value;
  106. }
  107. export function toArray(a: Mat4, out: Helpers.NumberArray, offset: number) {
  108. out[offset + 0] = a[0];
  109. out[offset + 1] = a[1];
  110. out[offset + 2] = a[2];
  111. out[offset + 3] = a[3];
  112. out[offset + 4] = a[4];
  113. out[offset + 5] = a[5];
  114. out[offset + 6] = a[6];
  115. out[offset + 7] = a[7];
  116. out[offset + 8] = a[8];
  117. out[offset + 9] = a[9];
  118. out[offset + 10] = a[10];
  119. out[offset + 11] = a[11];
  120. out[offset + 12] = a[12];
  121. out[offset + 13] = a[13];
  122. out[offset + 14] = a[14];
  123. out[offset + 15] = a[15];
  124. }
  125. export function fromArray(a: Mat4, array: Helpers.NumberArray, offset: number) {
  126. a[0] = array[offset + 0]
  127. a[1] = array[offset + 1]
  128. a[2] = array[offset + 2]
  129. a[3] = array[offset + 3]
  130. a[4] = array[offset + 4]
  131. a[5] = array[offset + 5]
  132. a[6] = array[offset + 6]
  133. a[7] = array[offset + 7]
  134. a[8] = array[offset + 8]
  135. a[9] = array[offset + 9]
  136. a[10] = array[offset + 10]
  137. a[11] = array[offset + 11]
  138. a[12] = array[offset + 12]
  139. a[13] = array[offset + 13]
  140. a[14] = array[offset + 14]
  141. a[15] = array[offset + 15]
  142. return a
  143. }
  144. export function copy(out: Mat4, a: Mat4) {
  145. out[0] = a[0];
  146. out[1] = a[1];
  147. out[2] = a[2];
  148. out[3] = a[3];
  149. out[4] = a[4];
  150. out[5] = a[5];
  151. out[6] = a[6];
  152. out[7] = a[7];
  153. out[8] = a[8];
  154. out[9] = a[9];
  155. out[10] = a[10];
  156. out[11] = a[11];
  157. out[12] = a[12];
  158. out[13] = a[13];
  159. out[14] = a[14];
  160. out[15] = a[15];
  161. return out;
  162. }
  163. export function clone(a: Mat4) {
  164. return Mat4.copy(Mat4.zero(), a);
  165. }
  166. /**
  167. * Returns the translation vector component of a transformation matrix.
  168. */
  169. export function getTranslation(out: Vec3, mat: Mat4) {
  170. out[0] = mat[12];
  171. out[1] = mat[13];
  172. out[2] = mat[14];
  173. return out;
  174. }
  175. /**
  176. * Returns the scaling factor component of a transformation matrix.
  177. */
  178. export function getScaling(out: Vec3, mat: Mat4) {
  179. let m11 = mat[0];
  180. let m12 = mat[1];
  181. let m13 = mat[2];
  182. let m21 = mat[4];
  183. let m22 = mat[5];
  184. let m23 = mat[6];
  185. let m31 = mat[8];
  186. let m32 = mat[9];
  187. let m33 = mat[10];
  188. out[0] = Math.sqrt(m11 * m11 + m12 * m12 + m13 * m13);
  189. out[1] = Math.sqrt(m21 * m21 + m22 * m22 + m23 * m23);
  190. out[2] = Math.sqrt(m31 * m31 + m32 * m32 + m33 * m33);
  191. return out;
  192. }
  193. /**
  194. * Returns a quaternion representing the rotational component of a transformation matrix.
  195. */
  196. export function getRotation(out: Quat, mat: Mat4) {
  197. // Algorithm taken from http://www.euclideanspace.com/maths/geometry/rotations/conversions/matrixToQuaternion/index.htm
  198. let trace = mat[0] + mat[5] + mat[10];
  199. let S = 0;
  200. if (trace > 0) {
  201. S = Math.sqrt(trace + 1.0) * 2;
  202. out[3] = 0.25 * S;
  203. out[0] = (mat[6] - mat[9]) / S;
  204. out[1] = (mat[8] - mat[2]) / S;
  205. out[2] = (mat[1] - mat[4]) / S;
  206. } else if ((mat[0] > mat[5]) && (mat[0] > mat[10])) {
  207. S = Math.sqrt(1.0 + mat[0] - mat[5] - mat[10]) * 2;
  208. out[3] = (mat[6] - mat[9]) / S;
  209. out[0] = 0.25 * S;
  210. out[1] = (mat[1] + mat[4]) / S;
  211. out[2] = (mat[8] + mat[2]) / S;
  212. } else if (mat[5] > mat[10]) {
  213. S = Math.sqrt(1.0 + mat[5] - mat[0] - mat[10]) * 2;
  214. out[3] = (mat[8] - mat[2]) / S;
  215. out[0] = (mat[1] + mat[4]) / S;
  216. out[1] = 0.25 * S;
  217. out[2] = (mat[6] + mat[9]) / S;
  218. } else {
  219. S = Math.sqrt(1.0 + mat[10] - mat[0] - mat[5]) * 2;
  220. out[3] = (mat[1] - mat[4]) / S;
  221. out[0] = (mat[8] + mat[2]) / S;
  222. out[1] = (mat[6] + mat[9]) / S;
  223. out[2] = 0.25 * S;
  224. }
  225. return out;
  226. }
  227. export function transpose(out: Mat4, a: Mat4) {
  228. // If we are transposing ourselves we can skip a few steps but have to cache some values
  229. if (out === a) {
  230. const a01 = a[1], a02 = a[2], a03 = a[3];
  231. const a12 = a[6], a13 = a[7];
  232. const a23 = a[11];
  233. out[1] = a[4];
  234. out[2] = a[8];
  235. out[3] = a[12];
  236. out[4] = a01;
  237. out[6] = a[9];
  238. out[7] = a[13];
  239. out[8] = a02;
  240. out[9] = a12;
  241. out[11] = a[14];
  242. out[12] = a03;
  243. out[13] = a13;
  244. out[14] = a23;
  245. } else {
  246. out[0] = a[0];
  247. out[1] = a[4];
  248. out[2] = a[8];
  249. out[3] = a[12];
  250. out[4] = a[1];
  251. out[5] = a[5];
  252. out[6] = a[9];
  253. out[7] = a[13];
  254. out[8] = a[2];
  255. out[9] = a[6];
  256. out[10] = a[10];
  257. out[11] = a[14];
  258. out[12] = a[3];
  259. out[13] = a[7];
  260. out[14] = a[11];
  261. out[15] = a[15];
  262. }
  263. return out;
  264. }
  265. export function invert(out: Mat4, a: Mat4) {
  266. const a00 = a[0], a01 = a[1], a02 = a[2], a03 = a[3],
  267. a10 = a[4], a11 = a[5], a12 = a[6], a13 = a[7],
  268. a20 = a[8], a21 = a[9], a22 = a[10], a23 = a[11],
  269. a30 = a[12], a31 = a[13], a32 = a[14], a33 = a[15],
  270. b00 = a00 * a11 - a01 * a10,
  271. b01 = a00 * a12 - a02 * a10,
  272. b02 = a00 * a13 - a03 * a10,
  273. b03 = a01 * a12 - a02 * a11,
  274. b04 = a01 * a13 - a03 * a11,
  275. b05 = a02 * a13 - a03 * a12,
  276. b06 = a20 * a31 - a21 * a30,
  277. b07 = a20 * a32 - a22 * a30,
  278. b08 = a20 * a33 - a23 * a30,
  279. b09 = a21 * a32 - a22 * a31,
  280. b10 = a21 * a33 - a23 * a31,
  281. b11 = a22 * a33 - a23 * a32;
  282. // Calculate the determinant
  283. let det = b00 * b11 - b01 * b10 + b02 * b09 + b03 * b08 - b04 * b07 + b05 * b06;
  284. if (!det) {
  285. console.warn('non-invertible matrix.', a);
  286. return out;
  287. }
  288. det = 1.0 / det;
  289. out[0] = (a11 * b11 - a12 * b10 + a13 * b09) * det;
  290. out[1] = (a02 * b10 - a01 * b11 - a03 * b09) * det;
  291. out[2] = (a31 * b05 - a32 * b04 + a33 * b03) * det;
  292. out[3] = (a22 * b04 - a21 * b05 - a23 * b03) * det;
  293. out[4] = (a12 * b08 - a10 * b11 - a13 * b07) * det;
  294. out[5] = (a00 * b11 - a02 * b08 + a03 * b07) * det;
  295. out[6] = (a32 * b02 - a30 * b05 - a33 * b01) * det;
  296. out[7] = (a20 * b05 - a22 * b02 + a23 * b01) * det;
  297. out[8] = (a10 * b10 - a11 * b08 + a13 * b06) * det;
  298. out[9] = (a01 * b08 - a00 * b10 - a03 * b06) * det;
  299. out[10] = (a30 * b04 - a31 * b02 + a33 * b00) * det;
  300. out[11] = (a21 * b02 - a20 * b04 - a23 * b00) * det;
  301. out[12] = (a11 * b07 - a10 * b09 - a12 * b06) * det;
  302. out[13] = (a00 * b09 - a01 * b07 + a02 * b06) * det;
  303. out[14] = (a31 * b01 - a30 * b03 - a32 * b00) * det;
  304. out[15] = (a20 * b03 - a21 * b01 + a22 * b00) * det;
  305. return out;
  306. }
  307. export function mul(out: Mat4, a: Mat4, b: Mat4) {
  308. const a00 = a[0], a01 = a[1], a02 = a[2], a03 = a[3],
  309. a10 = a[4], a11 = a[5], a12 = a[6], a13 = a[7],
  310. a20 = a[8], a21 = a[9], a22 = a[10], a23 = a[11],
  311. a30 = a[12], a31 = a[13], a32 = a[14], a33 = a[15];
  312. // Cache only the current line of the second matrix
  313. let b0 = b[0], b1 = b[1], b2 = b[2], b3 = b[3];
  314. out[0] = b0 * a00 + b1 * a10 + b2 * a20 + b3 * a30;
  315. out[1] = b0 * a01 + b1 * a11 + b2 * a21 + b3 * a31;
  316. out[2] = b0 * a02 + b1 * a12 + b2 * a22 + b3 * a32;
  317. out[3] = b0 * a03 + b1 * a13 + b2 * a23 + b3 * a33;
  318. b0 = b[4]; b1 = b[5]; b2 = b[6]; b3 = b[7];
  319. out[4] = b0 * a00 + b1 * a10 + b2 * a20 + b3 * a30;
  320. out[5] = b0 * a01 + b1 * a11 + b2 * a21 + b3 * a31;
  321. out[6] = b0 * a02 + b1 * a12 + b2 * a22 + b3 * a32;
  322. out[7] = b0 * a03 + b1 * a13 + b2 * a23 + b3 * a33;
  323. b0 = b[8]; b1 = b[9]; b2 = b[10]; b3 = b[11];
  324. out[8] = b0 * a00 + b1 * a10 + b2 * a20 + b3 * a30;
  325. out[9] = b0 * a01 + b1 * a11 + b2 * a21 + b3 * a31;
  326. out[10] = b0 * a02 + b1 * a12 + b2 * a22 + b3 * a32;
  327. out[11] = b0 * a03 + b1 * a13 + b2 * a23 + b3 * a33;
  328. b0 = b[12]; b1 = b[13]; b2 = b[14]; b3 = b[15];
  329. out[12] = b0 * a00 + b1 * a10 + b2 * a20 + b3 * a30;
  330. out[13] = b0 * a01 + b1 * a11 + b2 * a21 + b3 * a31;
  331. out[14] = b0 * a02 + b1 * a12 + b2 * a22 + b3 * a32;
  332. out[15] = b0 * a03 + b1 * a13 + b2 * a23 + b3 * a33;
  333. return out;
  334. }
  335. export function mul3(out: Mat4, a: Mat4, b: Mat4, c: Mat4) {
  336. return mul(out, mul(out, a, b), c);
  337. }
  338. /** Translate a Mat4 by the given Vec3 */
  339. export function translate(out: Mat4, a: Mat4, v: Vec3) {
  340. const x = v[0], y = v[1], z = v[2];
  341. let a00: number, a01: number, a02: number, a03: number,
  342. a10: number, a11: number, a12: number, a13: number,
  343. a20: number, a21: number, a22: number, a23: number;
  344. if (a === out) {
  345. out[12] = a[0] * x + a[4] * y + a[8] * z + a[12];
  346. out[13] = a[1] * x + a[5] * y + a[9] * z + a[13];
  347. out[14] = a[2] * x + a[6] * y + a[10] * z + a[14];
  348. out[15] = a[3] * x + a[7] * y + a[11] * z + a[15];
  349. } else {
  350. a00 = a[0]; a01 = a[1]; a02 = a[2]; a03 = a[3];
  351. a10 = a[4]; a11 = a[5]; a12 = a[6]; a13 = a[7];
  352. a20 = a[8]; a21 = a[9]; a22 = a[10]; a23 = a[11];
  353. out[0] = a00; out[1] = a01; out[2] = a02; out[3] = a03;
  354. out[4] = a10; out[5] = a11; out[6] = a12; out[7] = a13;
  355. out[8] = a20; out[9] = a21; out[10] = a22; out[11] = a23;
  356. out[12] = a00 * x + a10 * y + a20 * z + a[12];
  357. out[13] = a01 * x + a11 * y + a21 * z + a[13];
  358. out[14] = a02 * x + a12 * y + a22 * z + a[14];
  359. out[15] = a03 * x + a13 * y + a23 * z + a[15];
  360. }
  361. return out;
  362. }
  363. export function fromTranslation(out: Mat4, v: Vec3) {
  364. out[0] = 1;
  365. out[1] = 0;
  366. out[2] = 0;
  367. out[3] = 0;
  368. out[4] = 0;
  369. out[5] = 1;
  370. out[6] = 0;
  371. out[7] = 0;
  372. out[8] = 0;
  373. out[9] = 0;
  374. out[10] = 1;
  375. out[11] = 0;
  376. out[12] = v[0];
  377. out[13] = v[1];
  378. out[14] = v[2];
  379. out[15] = 1;
  380. return out;
  381. }
  382. export function setTranslation(out: Mat4, v: Vec3) {
  383. out[12] = v[0];
  384. out[13] = v[1];
  385. out[14] = v[2];
  386. return out;
  387. }
  388. /**
  389. * Sets the specified quaternion with values corresponding to the given
  390. * axes. Each axis is a vec3 and is expected to be unit length and
  391. * perpendicular to all other specified axes.
  392. */
  393. export function setAxes(out: Mat4, view: Vec3, right: Vec3, up: Vec3) {
  394. out[0] = right[0];
  395. out[4] = right[1];
  396. out[8] = right[2];
  397. out[1] = up[0];
  398. out[5] = up[1];
  399. out[9] = up[2];
  400. out[2] = view[0];
  401. out[6] = view[1];
  402. out[10] = view[2];
  403. return out
  404. }
  405. export function rotate(out: Mat4, a: Mat4, rad: number, axis: Vec3) {
  406. let x = axis[0], y = axis[1], z = axis[2],
  407. len = Math.sqrt(x * x + y * y + z * z),
  408. s, c, t,
  409. a00, a01, a02, a03,
  410. a10, a11, a12, a13,
  411. a20, a21, a22, a23,
  412. b00, b01, b02,
  413. b10, b11, b12,
  414. b20, b21, b22;
  415. if (Math.abs(len) < EPSILON.Value) {
  416. return Mat4.identity();
  417. }
  418. len = 1 / len;
  419. x *= len;
  420. y *= len;
  421. z *= len;
  422. s = Math.sin(rad);
  423. c = Math.cos(rad);
  424. t = 1 - c;
  425. a00 = a[0]; a01 = a[1]; a02 = a[2]; a03 = a[3];
  426. a10 = a[4]; a11 = a[5]; a12 = a[6]; a13 = a[7];
  427. a20 = a[8]; a21 = a[9]; a22 = a[10]; a23 = a[11];
  428. // Construct the elements of the rotation matrix
  429. b00 = x * x * t + c; b01 = y * x * t + z * s; b02 = z * x * t - y * s;
  430. b10 = x * y * t - z * s; b11 = y * y * t + c; b12 = z * y * t + x * s;
  431. b20 = x * z * t + y * s; b21 = y * z * t - x * s; b22 = z * z * t + c;
  432. // Perform rotation-specific matrix multiplication
  433. out[0] = a00 * b00 + a10 * b01 + a20 * b02;
  434. out[1] = a01 * b00 + a11 * b01 + a21 * b02;
  435. out[2] = a02 * b00 + a12 * b01 + a22 * b02;
  436. out[3] = a03 * b00 + a13 * b01 + a23 * b02;
  437. out[4] = a00 * b10 + a10 * b11 + a20 * b12;
  438. out[5] = a01 * b10 + a11 * b11 + a21 * b12;
  439. out[6] = a02 * b10 + a12 * b11 + a22 * b12;
  440. out[7] = a03 * b10 + a13 * b11 + a23 * b12;
  441. out[8] = a00 * b20 + a10 * b21 + a20 * b22;
  442. out[9] = a01 * b20 + a11 * b21 + a21 * b22;
  443. out[10] = a02 * b20 + a12 * b21 + a22 * b22;
  444. out[11] = a03 * b20 + a13 * b21 + a23 * b22;
  445. if (a !== out) { // If the source and destination differ, copy the unchanged last row
  446. out[12] = a[12];
  447. out[13] = a[13];
  448. out[14] = a[14];
  449. out[15] = a[15];
  450. }
  451. return out;
  452. }
  453. export function fromRotation(out: Mat4, rad: number, axis: Vec3) {
  454. let x = axis[0], y = axis[1], z = axis[2],
  455. len = Math.sqrt(x * x + y * y + z * z),
  456. s, c, t;
  457. if (Math.abs(len) < EPSILON.Value) { return setIdentity(out); }
  458. len = 1 / len;
  459. x *= len;
  460. y *= len;
  461. z *= len;
  462. s = Math.sin(rad);
  463. c = Math.cos(rad);
  464. t = 1 - c;
  465. // Perform rotation-specific matrix multiplication
  466. out[0] = x * x * t + c;
  467. out[1] = y * x * t + z * s;
  468. out[2] = z * x * t - y * s;
  469. out[3] = 0;
  470. out[4] = x * y * t - z * s;
  471. out[5] = y * y * t + c;
  472. out[6] = z * y * t + x * s;
  473. out[7] = 0;
  474. out[8] = x * z * t + y * s;
  475. out[9] = y * z * t - x * s;
  476. out[10] = z * z * t + c;
  477. out[11] = 0;
  478. out[12] = 0;
  479. out[13] = 0;
  480. out[14] = 0;
  481. out[15] = 1;
  482. return out;
  483. }
  484. export function scale(out: Mat4, a: Mat4, v: Vec3) {
  485. const x = v[0], y = v[1], z = v[2];
  486. out[0] = a[0] * x;
  487. out[1] = a[1] * x;
  488. out[2] = a[2] * x;
  489. out[3] = a[3] * x;
  490. out[4] = a[4] * y;
  491. out[5] = a[5] * y;
  492. out[6] = a[6] * y;
  493. out[7] = a[7] * y;
  494. out[8] = a[8] * z;
  495. out[9] = a[9] * z;
  496. out[10] = a[10] * z;
  497. out[11] = a[11] * z;
  498. out[12] = a[12];
  499. out[13] = a[13];
  500. out[14] = a[14];
  501. out[15] = a[15];
  502. return out;
  503. }
  504. export function scaleUniformly(out: Mat4, a: Mat4, scale: number) {
  505. out[0] = a[0] * scale;
  506. out[1] = a[1] * scale;
  507. out[2] = a[2] * scale;
  508. out[3] = a[3] * scale;
  509. out[4] = a[4] * scale;
  510. out[5] = a[5] * scale;
  511. out[6] = a[6] * scale;
  512. out[7] = a[7] * scale;
  513. out[8] = a[8] * scale;
  514. out[9] = a[9] * scale;
  515. out[10] = a[10] * scale;
  516. out[11] = a[11] * scale;
  517. out[12] = a[12];
  518. out[13] = a[13];
  519. out[14] = a[14];
  520. out[15] = a[15];
  521. return out;
  522. }
  523. export function fromScaling(out: Mat4, v: Vec3) {
  524. out[0] = v[0];
  525. out[1] = 0;
  526. out[2] = 0;
  527. out[3] = 0;
  528. out[4] = 0;
  529. out[5] = v[1];
  530. out[6] = 0;
  531. out[7] = 0;
  532. out[8] = 0;
  533. out[9] = 0;
  534. out[10] = v[2];
  535. out[11] = 0;
  536. out[12] = 0;
  537. out[13] = 0;
  538. out[14] = 0;
  539. out[15] = 1;
  540. return out;
  541. }
  542. export function fromUniformScaling(out: Mat4, scale: number) {
  543. out[0] = scale;
  544. out[1] = 0;
  545. out[2] = 0;
  546. out[3] = 0;
  547. out[4] = 0;
  548. out[5] = scale;
  549. out[6] = 0;
  550. out[7] = 0;
  551. out[8] = 0;
  552. out[9] = 0;
  553. out[10] = scale;
  554. out[11] = 0;
  555. out[12] = 0;
  556. out[13] = 0;
  557. out[14] = 0;
  558. out[15] = 1;
  559. return out;
  560. }
  561. export function makeTable(m: Mat4) {
  562. let ret = '';
  563. for (let i = 0; i < 4; i++) {
  564. for (let j = 0; j < 4; j++) {
  565. ret += m[4 * j + i].toString();
  566. if (j < 3) ret += ' ';
  567. }
  568. if (i < 3) ret += '\n';
  569. }
  570. return ret;
  571. }
  572. export function determinant(a: Mat4) {
  573. const a00 = a[0], a01 = a[1], a02 = a[2], a03 = a[3],
  574. a10 = a[4], a11 = a[5], a12 = a[6], a13 = a[7],
  575. a20 = a[8], a21 = a[9], a22 = a[10], a23 = a[11],
  576. a30 = a[12], a31 = a[13], a32 = a[14], a33 = a[15],
  577. b00 = a00 * a11 - a01 * a10,
  578. b01 = a00 * a12 - a02 * a10,
  579. b02 = a00 * a13 - a03 * a10,
  580. b03 = a01 * a12 - a02 * a11,
  581. b04 = a01 * a13 - a03 * a11,
  582. b05 = a02 * a13 - a03 * a12,
  583. b06 = a20 * a31 - a21 * a30,
  584. b07 = a20 * a32 - a22 * a30,
  585. b08 = a20 * a33 - a23 * a30,
  586. b09 = a21 * a32 - a22 * a31,
  587. b10 = a21 * a33 - a23 * a31,
  588. b11 = a22 * a33 - a23 * a32;
  589. // Calculate the determinant
  590. return b00 * b11 - b01 * b10 + b02 * b09 + b03 * b08 - b04 * b07 + b05 * b06;
  591. }
  592. /**
  593. * Check if the matrix has the form
  594. * [ Rotation Translation ]
  595. * [ 0 1 ]
  596. */
  597. export function isRotationAndTranslation(a: Mat4, eps?: number) {
  598. return _isRotationAndTranslation(a, typeof eps !== 'undefined' ? eps : EPSILON.Value)
  599. }
  600. function _isRotationAndTranslation(a: Mat4, eps: number) {
  601. const a00 = a[0], a01 = a[1], a02 = a[2], a03 = a[3],
  602. a10 = a[4], a11 = a[5], a12 = a[6], a13 = a[7],
  603. a20 = a[8], a21 = a[9], a22 = a[10], a23 = a[11],
  604. /* a30 = a[12], a31 = a[13], a32 = a[14],*/ a33 = a[15];
  605. if (!equalEps(a33, 1, eps) || !equalEps(a03, 0, eps) || !equalEps(a13, 0, eps) || !equalEps(a23, 0, eps)) {
  606. return false;
  607. }
  608. const det3x3 = a00 * (a11 * a22 - a12 * a21) - a01 * (a10 * a22 - a12 * a20) + a02 * (a10 * a21 - a11 * a20);
  609. if (!equalEps(det3x3, 1, eps)) {
  610. return false;
  611. }
  612. return true;
  613. }
  614. export function fromQuat(out: Mat4, q: Quat) {
  615. const x = q[0], y = q[1], z = q[2], w = q[3];
  616. const x2 = x + x;
  617. const y2 = y + y;
  618. const z2 = z + z;
  619. const xx = x * x2;
  620. const yx = y * x2;
  621. const yy = y * y2;
  622. const zx = z * x2;
  623. const zy = z * y2;
  624. const zz = z * z2;
  625. const wx = w * x2;
  626. const wy = w * y2;
  627. const wz = w * z2;
  628. out[0] = 1 - yy - zz;
  629. out[1] = yx + wz;
  630. out[2] = zx - wy;
  631. out[3] = 0;
  632. out[4] = yx - wz;
  633. out[5] = 1 - xx - zz;
  634. out[6] = zy + wx;
  635. out[7] = 0;
  636. out[8] = zx + wy;
  637. out[9] = zy - wx;
  638. out[10] = 1 - xx - yy;
  639. out[11] = 0;
  640. out[12] = 0;
  641. out[13] = 0;
  642. out[14] = 0;
  643. out[15] = 1;
  644. return out;
  645. }
  646. /**
  647. * Generates a frustum matrix with the given bounds
  648. */
  649. export function frustum(out: Mat4, left: number, right: number, bottom: number, top: number, near: number, far: number) {
  650. const rl = 1 / (right - left);
  651. const tb = 1 / (top - bottom);
  652. const nf = 1 / (near - far);
  653. out[0] = (near * 2) * rl;
  654. out[1] = 0;
  655. out[2] = 0;
  656. out[3] = 0;
  657. out[4] = 0;
  658. out[5] = (near * 2) * tb;
  659. out[6] = 0;
  660. out[7] = 0;
  661. out[8] = (right + left) * rl;
  662. out[9] = (top + bottom) * tb;
  663. out[10] = (far + near) * nf;
  664. out[11] = -1;
  665. out[12] = 0;
  666. out[13] = 0;
  667. out[14] = (far * near * 2) * nf;
  668. out[15] = 0;
  669. return out;
  670. }
  671. /**
  672. * Generates a perspective projection matrix with the given bounds
  673. */
  674. export function perspective(out: Mat4, fovy: number, aspect: number, near: number, far: number) {
  675. const f = 1.0 / Math.tan(fovy / 2);
  676. const nf = 1 / (near - far);
  677. out[0] = f / aspect;
  678. out[1] = 0;
  679. out[2] = 0;
  680. out[3] = 0;
  681. out[4] = 0;
  682. out[5] = f;
  683. out[6] = 0;
  684. out[7] = 0;
  685. out[8] = 0;
  686. out[9] = 0;
  687. out[10] = (far + near) * nf;
  688. out[11] = -1;
  689. out[12] = 0;
  690. out[13] = 0;
  691. out[14] = (2 * far * near) * nf;
  692. out[15] = 0;
  693. return out;
  694. }
  695. /**
  696. * Generates a orthogonal projection matrix with the given bounds
  697. */
  698. export function ortho(out: Mat4, left: number, right: number, bottom: number, top: number, near: number, far: number) {
  699. const lr = 1 / (left - right);
  700. const bt = 1 / (bottom - top);
  701. const nf = 1 / (near - far);
  702. out[0] = -2 * lr;
  703. out[1] = 0;
  704. out[2] = 0;
  705. out[3] = 0;
  706. out[4] = 0;
  707. out[5] = -2 * bt;
  708. out[6] = 0;
  709. out[7] = 0;
  710. out[8] = 0;
  711. out[9] = 0;
  712. out[10] = 2 * nf;
  713. out[11] = 0;
  714. out[12] = (left + right) * lr;
  715. out[13] = (top + bottom) * bt;
  716. out[14] = (far + near) * nf;
  717. out[15] = 1;
  718. return out;
  719. }
  720. /**
  721. * Generates a look-at matrix with the given eye position, focal point, and up axis
  722. */
  723. export function lookAt(out: Mat4, eye: Vec3, center: Vec3, up: Vec3) {
  724. let x0, x1, x2, y0, y1, y2, z0, z1, z2, len;
  725. const eyex = eye[0];
  726. const eyey = eye[1];
  727. const eyez = eye[2];
  728. const upx = up[0];
  729. const upy = up[1];
  730. const upz = up[2];
  731. const centerx = center[0];
  732. const centery = center[1];
  733. const centerz = center[2];
  734. if (Math.abs(eyex - centerx) < EPSILON.Value &&
  735. Math.abs(eyey - centery) < EPSILON.Value &&
  736. Math.abs(eyez - centerz) < EPSILON.Value
  737. ) {
  738. return setIdentity(out);
  739. }
  740. z0 = eyex - centerx;
  741. z1 = eyey - centery;
  742. z2 = eyez - centerz;
  743. len = 1 / Math.sqrt(z0 * z0 + z1 * z1 + z2 * z2);
  744. z0 *= len;
  745. z1 *= len;
  746. z2 *= len;
  747. x0 = upy * z2 - upz * z1;
  748. x1 = upz * z0 - upx * z2;
  749. x2 = upx * z1 - upy * z0;
  750. len = Math.sqrt(x0 * x0 + x1 * x1 + x2 * x2);
  751. if (!len) {
  752. x0 = 0;
  753. x1 = 0;
  754. x2 = 0;
  755. } else {
  756. len = 1 / len;
  757. x0 *= len;
  758. x1 *= len;
  759. x2 *= len;
  760. }
  761. y0 = z1 * x2 - z2 * x1;
  762. y1 = z2 * x0 - z0 * x2;
  763. y2 = z0 * x1 - z1 * x0;
  764. len = Math.sqrt(y0 * y0 + y1 * y1 + y2 * y2);
  765. if (!len) {
  766. y0 = 0;
  767. y1 = 0;
  768. y2 = 0;
  769. } else {
  770. len = 1 / len;
  771. y0 *= len;
  772. y1 *= len;
  773. y2 *= len;
  774. }
  775. out[0] = x0;
  776. out[1] = y0;
  777. out[2] = z0;
  778. out[3] = 0;
  779. out[4] = x1;
  780. out[5] = y1;
  781. out[6] = z1;
  782. out[7] = 0;
  783. out[8] = x2;
  784. out[9] = y2;
  785. out[10] = z2;
  786. out[11] = 0;
  787. out[12] = -(x0 * eyex + x1 * eyey + x2 * eyez);
  788. out[13] = -(y0 * eyex + y1 * eyey + y2 * eyez);
  789. out[14] = -(z0 * eyex + z1 * eyey + z2 * eyez);
  790. out[15] = 1;
  791. return out;
  792. }
  793. /**
  794. * Generates a matrix that makes something look at something else.
  795. */
  796. export function targetTo(out: Mat4, eye: Vec3, target: Vec3, up: Vec3) {
  797. const eyex = eye[0],
  798. eyey = eye[1],
  799. eyez = eye[2],
  800. upx = up[0],
  801. upy = up[1],
  802. upz = up[2];
  803. let z0 = eyex - target[0],
  804. z1 = eyey - target[1],
  805. z2 = eyez - target[2];
  806. let len = z0*z0 + z1*z1 + z2*z2;
  807. if (len > 0) {
  808. len = 1 / Math.sqrt(len);
  809. z0 *= len;
  810. z1 *= len;
  811. z2 *= len;
  812. }
  813. let x0 = upy * z2 - upz * z1,
  814. x1 = upz * z0 - upx * z2,
  815. x2 = upx * z1 - upy * z0;
  816. len = x0*x0 + x1*x1 + x2*x2;
  817. if (len > 0) {
  818. len = 1 / Math.sqrt(len);
  819. x0 *= len;
  820. x1 *= len;
  821. x2 *= len;
  822. }
  823. out[0] = x0;
  824. out[1] = x1;
  825. out[2] = x2;
  826. out[3] = 0;
  827. out[4] = z1 * x2 - z2 * x1;
  828. out[5] = z2 * x0 - z0 * x2;
  829. out[6] = z0 * x1 - z1 * x0;
  830. out[7] = 0;
  831. out[8] = z0;
  832. out[9] = z1;
  833. out[10] = z2;
  834. out[11] = 0;
  835. out[12] = eyex;
  836. out[13] = eyey;
  837. out[14] = eyez;
  838. out[15] = 1;
  839. return out;
  840. }
  841. /**
  842. * Perm is 0-indexed permutation
  843. */
  844. export function fromPermutation(out: Mat4, perm: number[]) {
  845. setZero(out);
  846. for (let i = 0; i < 4; i++) {
  847. const p = perm[i];
  848. setValue(out, i, p, 1);
  849. }
  850. return out;
  851. }
  852. export function getMaxScaleOnAxis(m: Mat4) {
  853. const scaleXSq = m[0] * m[0] + m[1] * m[1] + m[2] * m[2]
  854. const scaleYSq = m[4] * m[4] + m[5] * m[5] + m[6] * m[6]
  855. const scaleZSq = m[8] * m[8] + m[9] * m[9] + m[10] * m[10]
  856. return Math.sqrt(Math.max(scaleXSq, scaleYSq, scaleZSq))
  857. }
  858. /** Rotation matrix for 90deg around x-axis */
  859. export const rotX90: ReadonlyMat4 = Mat4.fromRotation(Mat4.identity(), degToRad(90), Vec3.create(1, 0, 0))
  860. /** Rotation matrix for 180deg around x-axis */
  861. export const rotX180: ReadonlyMat4 = Mat4.fromRotation(Mat4.identity(), degToRad(180), Vec3.create(1, 0, 0))
  862. /** Rotation matrix for 90deg around y-axis */
  863. export const rotY90: ReadonlyMat4 = Mat4.fromRotation(Mat4.identity(), degToRad(90), Vec3.create(0, 1, 0))
  864. /** Rotation matrix for 180deg around y-axis */
  865. export const rotY180: ReadonlyMat4 = Mat4.fromRotation(Mat4.identity(), degToRad(180), Vec3.create(0, 1, 0))
  866. /** Rotation matrix for 90deg around z-axis */
  867. export const rotZ90: ReadonlyMat4 = Mat4.fromRotation(Mat4.identity(), degToRad(90), Vec3.create(0, 0, 1))
  868. /** Rotation matrix for 180deg around z-axis */
  869. export const rotZ180: ReadonlyMat4 = Mat4.fromRotation(Mat4.identity(), degToRad(180), Vec3.create(0, 0, 1))
  870. /** Rotation matrix for 90deg around first x-axis and then y-axis */
  871. export const rotXY90: ReadonlyMat4 = Mat4.mul(Mat4.identity(), rotX90, rotY90)
  872. /** Rotation matrix for 90deg around first z-axis and then y-axis */
  873. export const rotZY90: ReadonlyMat4 = Mat4.mul(Mat4.identity(), rotZ90, rotY90)
  874. /** Rotation matrix for 90deg around first z-axis and then y-axis and then z-axis */
  875. export const rotZYZ90: ReadonlyMat4 = Mat4.mul(Mat4.identity(), rotZY90, rotZ90)
  876. /** Rotation matrix for 90deg around first z-axis and then 180deg around x-axis */
  877. export const rotZ90X180: ReadonlyMat4 = Mat4.mul(Mat4.identity(), rotZ90, rotX180)
  878. /** Rotation matrix for 90deg around first y-axis and then 180deg around z-axis */
  879. export const rotY90Z180: ReadonlyMat4 = Mat4.mul(Mat4.identity(), rotY90, rotZ180)
  880. }
  881. export default Mat4