mat4.ts 33 KB

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