mat4.js 53 KB

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