index.umd.js 32 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198
  1. (function (global, factory) {
  2. typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports, require('@antv/g-lite')) :
  3. typeof define === 'function' && define.amd ? define(['exports', '@antv/g-lite'], factory) :
  4. (global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory((global.G = global.G || {}, global.G.CameraAPI = {}), global.window.G));
  5. }(this, (function (exports, gLite) { 'use strict';
  6. function _inheritsLoose(subClass, superClass) {
  7. subClass.prototype = Object.create(superClass.prototype);
  8. subClass.prototype.constructor = subClass;
  9. _setPrototypeOf(subClass, superClass);
  10. }
  11. function _setPrototypeOf(o, p) {
  12. _setPrototypeOf = Object.setPrototypeOf ? Object.setPrototypeOf.bind() : function _setPrototypeOf(o, p) {
  13. o.__proto__ = p;
  14. return o;
  15. };
  16. return _setPrototypeOf(o, p);
  17. }
  18. /**
  19. * Common utilities
  20. * @module glMatrix
  21. */
  22. // Configuration Constants
  23. var EPSILON = 0.000001;
  24. var ARRAY_TYPE = typeof Float32Array !== 'undefined' ? Float32Array : Array;
  25. if (!Math.hypot) Math.hypot = function () {
  26. var y = 0,
  27. i = arguments.length;
  28. while (i--) {
  29. y += arguments[i] * arguments[i];
  30. }
  31. return Math.sqrt(y);
  32. };
  33. /**
  34. * 3x3 Matrix
  35. * @module mat3
  36. */
  37. /**
  38. * Creates a new identity mat3
  39. *
  40. * @returns {mat3} a new 3x3 matrix
  41. */
  42. function create() {
  43. var out = new ARRAY_TYPE(9);
  44. if (ARRAY_TYPE != Float32Array) {
  45. out[1] = 0;
  46. out[2] = 0;
  47. out[3] = 0;
  48. out[5] = 0;
  49. out[6] = 0;
  50. out[7] = 0;
  51. }
  52. out[0] = 1;
  53. out[4] = 1;
  54. out[8] = 1;
  55. return out;
  56. }
  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$1() {
  67. var out = new ARRAY_TYPE(16);
  68. if (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 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. * Multiplies two mat4s
  142. *
  143. * @param {mat4} out the receiving matrix
  144. * @param {ReadonlyMat4} a the first operand
  145. * @param {ReadonlyMat4} b the second operand
  146. * @returns {mat4} out
  147. */
  148. function multiply(out, a, b) {
  149. var a00 = a[0],
  150. a01 = a[1],
  151. a02 = a[2],
  152. a03 = a[3];
  153. var a10 = a[4],
  154. a11 = a[5],
  155. a12 = a[6],
  156. a13 = a[7];
  157. var a20 = a[8],
  158. a21 = a[9],
  159. a22 = a[10],
  160. a23 = a[11];
  161. var a30 = a[12],
  162. a31 = a[13],
  163. a32 = a[14],
  164. a33 = a[15]; // Cache only the current line of the second matrix
  165. var b0 = b[0],
  166. b1 = b[1],
  167. b2 = b[2],
  168. b3 = b[3];
  169. out[0] = b0 * a00 + b1 * a10 + b2 * a20 + b3 * a30;
  170. out[1] = b0 * a01 + b1 * a11 + b2 * a21 + b3 * a31;
  171. out[2] = b0 * a02 + b1 * a12 + b2 * a22 + b3 * a32;
  172. out[3] = b0 * a03 + b1 * a13 + b2 * a23 + b3 * a33;
  173. b0 = b[4];
  174. b1 = b[5];
  175. b2 = b[6];
  176. b3 = b[7];
  177. out[4] = b0 * a00 + b1 * a10 + b2 * a20 + b3 * a30;
  178. out[5] = b0 * a01 + b1 * a11 + b2 * a21 + b3 * a31;
  179. out[6] = b0 * a02 + b1 * a12 + b2 * a22 + b3 * a32;
  180. out[7] = b0 * a03 + b1 * a13 + b2 * a23 + b3 * a33;
  181. b0 = b[8];
  182. b1 = b[9];
  183. b2 = b[10];
  184. b3 = b[11];
  185. out[8] = b0 * a00 + b1 * a10 + b2 * a20 + b3 * a30;
  186. out[9] = b0 * a01 + b1 * a11 + b2 * a21 + b3 * a31;
  187. out[10] = b0 * a02 + b1 * a12 + b2 * a22 + b3 * a32;
  188. out[11] = b0 * a03 + b1 * a13 + b2 * a23 + b3 * a33;
  189. b0 = b[12];
  190. b1 = b[13];
  191. b2 = b[14];
  192. b3 = b[15];
  193. out[12] = b0 * a00 + b1 * a10 + b2 * a20 + b3 * a30;
  194. out[13] = b0 * a01 + b1 * a11 + b2 * a21 + b3 * a31;
  195. out[14] = b0 * a02 + b1 * a12 + b2 * a22 + b3 * a32;
  196. out[15] = b0 * a03 + b1 * a13 + b2 * a23 + b3 * a33;
  197. return out;
  198. }
  199. /**
  200. * Translate a mat4 by the given vector
  201. *
  202. * @param {mat4} out the receiving matrix
  203. * @param {ReadonlyMat4} a the matrix to translate
  204. * @param {ReadonlyVec3} v vector to translate by
  205. * @returns {mat4} out
  206. */
  207. function translate(out, a, v) {
  208. var x = v[0],
  209. y = v[1],
  210. z = v[2];
  211. var a00, a01, a02, a03;
  212. var a10, a11, a12, a13;
  213. var a20, a21, a22, a23;
  214. if (a === out) {
  215. out[12] = a[0] * x + a[4] * y + a[8] * z + a[12];
  216. out[13] = a[1] * x + a[5] * y + a[9] * z + a[13];
  217. out[14] = a[2] * x + a[6] * y + a[10] * z + a[14];
  218. out[15] = a[3] * x + a[7] * y + a[11] * z + a[15];
  219. } else {
  220. a00 = a[0];
  221. a01 = a[1];
  222. a02 = a[2];
  223. a03 = a[3];
  224. a10 = a[4];
  225. a11 = a[5];
  226. a12 = a[6];
  227. a13 = a[7];
  228. a20 = a[8];
  229. a21 = a[9];
  230. a22 = a[10];
  231. a23 = a[11];
  232. out[0] = a00;
  233. out[1] = a01;
  234. out[2] = a02;
  235. out[3] = a03;
  236. out[4] = a10;
  237. out[5] = a11;
  238. out[6] = a12;
  239. out[7] = a13;
  240. out[8] = a20;
  241. out[9] = a21;
  242. out[10] = a22;
  243. out[11] = a23;
  244. out[12] = a00 * x + a10 * y + a20 * z + a[12];
  245. out[13] = a01 * x + a11 * y + a21 * z + a[13];
  246. out[14] = a02 * x + a12 * y + a22 * z + a[14];
  247. out[15] = a03 * x + a13 * y + a23 * z + a[15];
  248. }
  249. return out;
  250. }
  251. /**
  252. * Calculates a 4x4 matrix from the given quaternion
  253. *
  254. * @param {mat4} out mat4 receiving operation result
  255. * @param {ReadonlyQuat} q Quaternion to create matrix from
  256. *
  257. * @returns {mat4} out
  258. */
  259. function fromQuat(out, q) {
  260. var x = q[0],
  261. y = q[1],
  262. z = q[2],
  263. w = q[3];
  264. var x2 = x + x;
  265. var y2 = y + y;
  266. var z2 = z + z;
  267. var xx = x * x2;
  268. var yx = y * x2;
  269. var yy = y * y2;
  270. var zx = z * x2;
  271. var zy = z * y2;
  272. var zz = z * z2;
  273. var wx = w * x2;
  274. var wy = w * y2;
  275. var wz = w * z2;
  276. out[0] = 1 - yy - zz;
  277. out[1] = yx + wz;
  278. out[2] = zx - wy;
  279. out[3] = 0;
  280. out[4] = yx - wz;
  281. out[5] = 1 - xx - zz;
  282. out[6] = zy + wx;
  283. out[7] = 0;
  284. out[8] = zx + wy;
  285. out[9] = zy - wx;
  286. out[10] = 1 - xx - yy;
  287. out[11] = 0;
  288. out[12] = 0;
  289. out[13] = 0;
  290. out[14] = 0;
  291. out[15] = 1;
  292. return out;
  293. }
  294. /**
  295. * 3 Dimensional Vector
  296. * @module vec3
  297. */
  298. /**
  299. * Creates a new, empty vec3
  300. *
  301. * @returns {vec3} a new 3D vector
  302. */
  303. function create$2() {
  304. var out = new ARRAY_TYPE(3);
  305. if (ARRAY_TYPE != Float32Array) {
  306. out[0] = 0;
  307. out[1] = 0;
  308. out[2] = 0;
  309. }
  310. return out;
  311. }
  312. /**
  313. * Creates a new vec3 initialized with values from an existing vector
  314. *
  315. * @param {ReadonlyVec3} a vector to clone
  316. * @returns {vec3} a new 3D vector
  317. */
  318. function clone$1(a) {
  319. var out = new ARRAY_TYPE(3);
  320. out[0] = a[0];
  321. out[1] = a[1];
  322. out[2] = a[2];
  323. return out;
  324. }
  325. /**
  326. * Calculates the length of a vec3
  327. *
  328. * @param {ReadonlyVec3} a vector to calculate length of
  329. * @returns {Number} length of a
  330. */
  331. function length(a) {
  332. var x = a[0];
  333. var y = a[1];
  334. var z = a[2];
  335. return Math.hypot(x, y, z);
  336. }
  337. /**
  338. * Creates a new vec3 initialized with the given values
  339. *
  340. * @param {Number} x X component
  341. * @param {Number} y Y component
  342. * @param {Number} z Z component
  343. * @returns {vec3} a new 3D vector
  344. */
  345. function fromValues(x, y, z) {
  346. var out = new ARRAY_TYPE(3);
  347. out[0] = x;
  348. out[1] = y;
  349. out[2] = z;
  350. return out;
  351. }
  352. /**
  353. * Copy the values from one vec3 to another
  354. *
  355. * @param {vec3} out the receiving vector
  356. * @param {ReadonlyVec3} a the source vector
  357. * @returns {vec3} out
  358. */
  359. function copy$1(out, a) {
  360. out[0] = a[0];
  361. out[1] = a[1];
  362. out[2] = a[2];
  363. return out;
  364. }
  365. /**
  366. * Adds two vec3's
  367. *
  368. * @param {vec3} out the receiving vector
  369. * @param {ReadonlyVec3} a the first operand
  370. * @param {ReadonlyVec3} b the second operand
  371. * @returns {vec3} out
  372. */
  373. function add(out, a, b) {
  374. out[0] = a[0] + b[0];
  375. out[1] = a[1] + b[1];
  376. out[2] = a[2] + b[2];
  377. return out;
  378. }
  379. /**
  380. * Scales a vec3 by a scalar number
  381. *
  382. * @param {vec3} out the receiving vector
  383. * @param {ReadonlyVec3} a the vector to scale
  384. * @param {Number} b amount to scale the vector by
  385. * @returns {vec3} out
  386. */
  387. function scale(out, a, b) {
  388. out[0] = a[0] * b;
  389. out[1] = a[1] * b;
  390. out[2] = a[2] * b;
  391. return out;
  392. }
  393. /**
  394. * Calculates the euclidian distance between two vec3's
  395. *
  396. * @param {ReadonlyVec3} a the first operand
  397. * @param {ReadonlyVec3} b the second operand
  398. * @returns {Number} distance between a and b
  399. */
  400. function distance(a, b) {
  401. var x = b[0] - a[0];
  402. var y = b[1] - a[1];
  403. var z = b[2] - a[2];
  404. return Math.hypot(x, y, z);
  405. }
  406. /**
  407. * Normalize a vec3
  408. *
  409. * @param {vec3} out the receiving vector
  410. * @param {ReadonlyVec3} a vector to normalize
  411. * @returns {vec3} out
  412. */
  413. function normalize(out, a) {
  414. var x = a[0];
  415. var y = a[1];
  416. var z = a[2];
  417. var len = x * x + y * y + z * z;
  418. if (len > 0) {
  419. //TODO: evaluate use of glm_invsqrt here?
  420. len = 1 / Math.sqrt(len);
  421. }
  422. out[0] = a[0] * len;
  423. out[1] = a[1] * len;
  424. out[2] = a[2] * len;
  425. return out;
  426. }
  427. /**
  428. * Calculates the dot product of two vec3's
  429. *
  430. * @param {ReadonlyVec3} a the first operand
  431. * @param {ReadonlyVec3} b the second operand
  432. * @returns {Number} dot product of a and b
  433. */
  434. function dot(a, b) {
  435. return a[0] * b[0] + a[1] * b[1] + a[2] * b[2];
  436. }
  437. /**
  438. * Computes the cross product of two vec3's
  439. *
  440. * @param {vec3} out the receiving vector
  441. * @param {ReadonlyVec3} a the first operand
  442. * @param {ReadonlyVec3} b the second operand
  443. * @returns {vec3} out
  444. */
  445. function cross(out, a, b) {
  446. var ax = a[0],
  447. ay = a[1],
  448. az = a[2];
  449. var bx = b[0],
  450. by = b[1],
  451. bz = b[2];
  452. out[0] = ay * bz - az * by;
  453. out[1] = az * bx - ax * bz;
  454. out[2] = ax * by - ay * bx;
  455. return out;
  456. }
  457. /**
  458. * Performs a linear interpolation between two vec3's
  459. *
  460. * @param {vec3} out the receiving vector
  461. * @param {ReadonlyVec3} a the first operand
  462. * @param {ReadonlyVec3} b the second operand
  463. * @param {Number} t interpolation amount, in the range [0-1], between the two inputs
  464. * @returns {vec3} out
  465. */
  466. function lerp(out, a, b, t) {
  467. var ax = a[0];
  468. var ay = a[1];
  469. var az = a[2];
  470. out[0] = ax + t * (b[0] - ax);
  471. out[1] = ay + t * (b[1] - ay);
  472. out[2] = az + t * (b[2] - az);
  473. return out;
  474. }
  475. /**
  476. * Alias for {@link vec3.distance}
  477. * @function
  478. */
  479. var dist = distance;
  480. /**
  481. * Alias for {@link vec3.length}
  482. * @function
  483. */
  484. var len = length;
  485. /**
  486. * Perform some operation over an array of vec3s.
  487. *
  488. * @param {Array} a the array of vectors to iterate over
  489. * @param {Number} stride Number of elements between the start of each vec3. If 0 assumes tightly packed
  490. * @param {Number} offset Number of elements to skip at the beginning of the array
  491. * @param {Number} count Number of vec3s to iterate over. If 0 iterates over entire array
  492. * @param {Function} fn Function to call for each vector in the array
  493. * @param {Object} [arg] additional argument to pass to fn
  494. * @returns {Array} a
  495. * @function
  496. */
  497. var forEach = function () {
  498. var vec = create$2();
  499. return function (a, stride, offset, count, fn, arg) {
  500. var i, l;
  501. if (!stride) {
  502. stride = 3;
  503. }
  504. if (!offset) {
  505. offset = 0;
  506. }
  507. if (count) {
  508. l = Math.min(count * stride + offset, a.length);
  509. } else {
  510. l = a.length;
  511. }
  512. for (i = offset; i < l; i += stride) {
  513. vec[0] = a[i];
  514. vec[1] = a[i + 1];
  515. vec[2] = a[i + 2];
  516. fn(vec, vec, arg);
  517. a[i] = vec[0];
  518. a[i + 1] = vec[1];
  519. a[i + 2] = vec[2];
  520. }
  521. return a;
  522. };
  523. }();
  524. /**
  525. * 4 Dimensional Vector
  526. * @module vec4
  527. */
  528. /**
  529. * Creates a new, empty vec4
  530. *
  531. * @returns {vec4} a new 4D vector
  532. */
  533. function create$3() {
  534. var out = new ARRAY_TYPE(4);
  535. if (ARRAY_TYPE != Float32Array) {
  536. out[0] = 0;
  537. out[1] = 0;
  538. out[2] = 0;
  539. out[3] = 0;
  540. }
  541. return out;
  542. }
  543. /**
  544. * Normalize a vec4
  545. *
  546. * @param {vec4} out the receiving vector
  547. * @param {ReadonlyVec4} a vector to normalize
  548. * @returns {vec4} out
  549. */
  550. function normalize$1(out, a) {
  551. var x = a[0];
  552. var y = a[1];
  553. var z = a[2];
  554. var w = a[3];
  555. var len = x * x + y * y + z * z + w * w;
  556. if (len > 0) {
  557. len = 1 / Math.sqrt(len);
  558. }
  559. out[0] = x * len;
  560. out[1] = y * len;
  561. out[2] = z * len;
  562. out[3] = w * len;
  563. return out;
  564. }
  565. /**
  566. * Perform some operation over an array of vec4s.
  567. *
  568. * @param {Array} a the array of vectors to iterate over
  569. * @param {Number} stride Number of elements between the start of each vec4. If 0 assumes tightly packed
  570. * @param {Number} offset Number of elements to skip at the beginning of the array
  571. * @param {Number} count Number of vec4s to iterate over. If 0 iterates over entire array
  572. * @param {Function} fn Function to call for each vector in the array
  573. * @param {Object} [arg] additional argument to pass to fn
  574. * @returns {Array} a
  575. * @function
  576. */
  577. var forEach$1 = function () {
  578. var vec = create$3();
  579. return function (a, stride, offset, count, fn, arg) {
  580. var i, l;
  581. if (!stride) {
  582. stride = 4;
  583. }
  584. if (!offset) {
  585. offset = 0;
  586. }
  587. if (count) {
  588. l = Math.min(count * stride + offset, a.length);
  589. } else {
  590. l = a.length;
  591. }
  592. for (i = offset; i < l; i += stride) {
  593. vec[0] = a[i];
  594. vec[1] = a[i + 1];
  595. vec[2] = a[i + 2];
  596. vec[3] = a[i + 3];
  597. fn(vec, vec, arg);
  598. a[i] = vec[0];
  599. a[i + 1] = vec[1];
  600. a[i + 2] = vec[2];
  601. a[i + 3] = vec[3];
  602. }
  603. return a;
  604. };
  605. }();
  606. /**
  607. * Quaternion
  608. * @module quat
  609. */
  610. /**
  611. * Creates a new identity quat
  612. *
  613. * @returns {quat} a new quaternion
  614. */
  615. function create$4() {
  616. var out = new ARRAY_TYPE(4);
  617. if (ARRAY_TYPE != Float32Array) {
  618. out[0] = 0;
  619. out[1] = 0;
  620. out[2] = 0;
  621. }
  622. out[3] = 1;
  623. return out;
  624. }
  625. /**
  626. * Sets a quat from the given angle and rotation axis,
  627. * then returns it.
  628. *
  629. * @param {quat} out the receiving quaternion
  630. * @param {ReadonlyVec3} axis the axis around which to rotate
  631. * @param {Number} rad the angle in radians
  632. * @returns {quat} out
  633. **/
  634. function setAxisAngle(out, axis, rad) {
  635. rad = rad * 0.5;
  636. var s = Math.sin(rad);
  637. out[0] = s * axis[0];
  638. out[1] = s * axis[1];
  639. out[2] = s * axis[2];
  640. out[3] = Math.cos(rad);
  641. return out;
  642. }
  643. /**
  644. * Multiplies two quat's
  645. *
  646. * @param {quat} out the receiving quaternion
  647. * @param {ReadonlyQuat} a the first operand
  648. * @param {ReadonlyQuat} b the second operand
  649. * @returns {quat} out
  650. */
  651. function multiply$1(out, a, b) {
  652. var ax = a[0],
  653. ay = a[1],
  654. az = a[2],
  655. aw = a[3];
  656. var bx = b[0],
  657. by = b[1],
  658. bz = b[2],
  659. bw = b[3];
  660. out[0] = ax * bw + aw * bx + ay * bz - az * by;
  661. out[1] = ay * bw + aw * by + az * bx - ax * bz;
  662. out[2] = az * bw + aw * bz + ax * by - ay * bx;
  663. out[3] = aw * bw - ax * bx - ay * by - az * bz;
  664. return out;
  665. }
  666. /**
  667. * Performs a spherical linear interpolation between two quat
  668. *
  669. * @param {quat} out the receiving quaternion
  670. * @param {ReadonlyQuat} a the first operand
  671. * @param {ReadonlyQuat} b the second operand
  672. * @param {Number} t interpolation amount, in the range [0-1], between the two inputs
  673. * @returns {quat} out
  674. */
  675. function slerp(out, a, b, t) {
  676. // benchmarks:
  677. // http://jsperf.com/quaternion-slerp-implementations
  678. var ax = a[0],
  679. ay = a[1],
  680. az = a[2],
  681. aw = a[3];
  682. var bx = b[0],
  683. by = b[1],
  684. bz = b[2],
  685. bw = b[3];
  686. var omega, cosom, sinom, scale0, scale1; // calc cosine
  687. cosom = ax * bx + ay * by + az * bz + aw * bw; // adjust signs (if necessary)
  688. if (cosom < 0.0) {
  689. cosom = -cosom;
  690. bx = -bx;
  691. by = -by;
  692. bz = -bz;
  693. bw = -bw;
  694. } // calculate coefficients
  695. if (1.0 - cosom > EPSILON) {
  696. // standard case (slerp)
  697. omega = Math.acos(cosom);
  698. sinom = Math.sin(omega);
  699. scale0 = Math.sin((1.0 - t) * omega) / sinom;
  700. scale1 = Math.sin(t * omega) / sinom;
  701. } else {
  702. // "from" and "to" quaternions are very close
  703. // ... so we can do a linear interpolation
  704. scale0 = 1.0 - t;
  705. scale1 = t;
  706. } // calculate final values
  707. out[0] = scale0 * ax + scale1 * bx;
  708. out[1] = scale0 * ay + scale1 * by;
  709. out[2] = scale0 * az + scale1 * bz;
  710. out[3] = scale0 * aw + scale1 * bw;
  711. return out;
  712. }
  713. /**
  714. * Creates a quaternion from the given 3x3 rotation matrix.
  715. *
  716. * NOTE: The resultant quaternion is not normalized, so you should be sure
  717. * to renormalize the quaternion yourself where necessary.
  718. *
  719. * @param {quat} out the receiving quaternion
  720. * @param {ReadonlyMat3} m rotation matrix
  721. * @returns {quat} out
  722. * @function
  723. */
  724. function fromMat3(out, m) {
  725. // Algorithm in Ken Shoemake's article in 1987 SIGGRAPH course notes
  726. // article "Quaternion Calculus and Fast Animation".
  727. var fTrace = m[0] + m[4] + m[8];
  728. var fRoot;
  729. if (fTrace > 0.0) {
  730. // |w| > 1/2, may as well choose w > 1/2
  731. fRoot = Math.sqrt(fTrace + 1.0); // 2w
  732. out[3] = 0.5 * fRoot;
  733. fRoot = 0.5 / fRoot; // 1/(4w)
  734. out[0] = (m[5] - m[7]) * fRoot;
  735. out[1] = (m[6] - m[2]) * fRoot;
  736. out[2] = (m[1] - m[3]) * fRoot;
  737. } else {
  738. // |w| <= 1/2
  739. var i = 0;
  740. if (m[4] > m[0]) i = 1;
  741. if (m[8] > m[i * 3 + i]) i = 2;
  742. var j = (i + 1) % 3;
  743. var k = (i + 2) % 3;
  744. fRoot = Math.sqrt(m[i * 3 + i] - m[j * 3 + j] - m[k * 3 + k] + 1.0);
  745. out[i] = 0.5 * fRoot;
  746. fRoot = 0.5 / fRoot;
  747. out[3] = (m[j * 3 + k] - m[k * 3 + j]) * fRoot;
  748. out[j] = (m[j * 3 + i] + m[i * 3 + j]) * fRoot;
  749. out[k] = (m[k * 3 + i] + m[i * 3 + k]) * fRoot;
  750. }
  751. return out;
  752. }
  753. /**
  754. * Normalize a quat
  755. *
  756. * @param {quat} out the receiving quaternion
  757. * @param {ReadonlyQuat} a quaternion to normalize
  758. * @returns {quat} out
  759. * @function
  760. */
  761. var normalize$2 = normalize$1;
  762. /**
  763. * Sets a quaternion to represent the shortest rotation from one
  764. * vector to another.
  765. *
  766. * Both vectors are assumed to be unit length.
  767. *
  768. * @param {quat} out the receiving quaternion.
  769. * @param {ReadonlyVec3} a the initial vector
  770. * @param {ReadonlyVec3} b the destination vector
  771. * @returns {quat} out
  772. */
  773. var rotationTo = function () {
  774. var tmpvec3 = create$2();
  775. var xUnitVec3 = fromValues(1, 0, 0);
  776. var yUnitVec3 = fromValues(0, 1, 0);
  777. return function (out, a, b) {
  778. var dot$1 = dot(a, b);
  779. if (dot$1 < -0.999999) {
  780. cross(tmpvec3, xUnitVec3, a);
  781. if (len(tmpvec3) < 0.000001) cross(tmpvec3, yUnitVec3, a);
  782. normalize(tmpvec3, tmpvec3);
  783. setAxisAngle(out, tmpvec3, Math.PI);
  784. return out;
  785. } else if (dot$1 > 0.999999) {
  786. out[0] = 0;
  787. out[1] = 0;
  788. out[2] = 0;
  789. out[3] = 1;
  790. return out;
  791. } else {
  792. cross(tmpvec3, a, b);
  793. out[0] = tmpvec3[0];
  794. out[1] = tmpvec3[1];
  795. out[2] = tmpvec3[2];
  796. out[3] = 1 + dot$1;
  797. return normalize$2(out, out);
  798. }
  799. };
  800. }();
  801. /**
  802. * Performs a spherical linear interpolation with two control points
  803. *
  804. * @param {quat} out the receiving quaternion
  805. * @param {ReadonlyQuat} a the first operand
  806. * @param {ReadonlyQuat} b the second operand
  807. * @param {ReadonlyQuat} c the third operand
  808. * @param {ReadonlyQuat} d the fourth operand
  809. * @param {Number} t interpolation amount, in the range [0-1], between the two inputs
  810. * @returns {quat} out
  811. */
  812. var sqlerp = function () {
  813. var temp1 = create$4();
  814. var temp2 = create$4();
  815. return function (out, a, b, c, d, t) {
  816. slerp(temp1, a, d, t);
  817. slerp(temp2, b, c, t);
  818. slerp(out, temp1, temp2, 2 * t * (1 - t));
  819. return out;
  820. };
  821. }();
  822. /**
  823. * Sets the specified quaternion with values corresponding to the given
  824. * axes. Each axis is a vec3 and is expected to be unit length and
  825. * perpendicular to all other specified axes.
  826. *
  827. * @param {ReadonlyVec3} view the vector representing the viewing direction
  828. * @param {ReadonlyVec3} right the vector representing the local "right" direction
  829. * @param {ReadonlyVec3} up the vector representing the local "up" direction
  830. * @returns {quat} out
  831. */
  832. var setAxes = function () {
  833. var matr = create();
  834. return function (out, view, right, up) {
  835. matr[0] = right[0];
  836. matr[3] = right[1];
  837. matr[6] = right[2];
  838. matr[1] = up[0];
  839. matr[4] = up[1];
  840. matr[7] = up[2];
  841. matr[2] = -view[0];
  842. matr[5] = -view[1];
  843. matr[8] = -view[2];
  844. return normalize$2(out, fromMat3(out, matr));
  845. };
  846. }();
  847. var toString = {}.toString;
  848. var isType = function (value, type) { return toString.call(value) === '[object ' + type + ']'; };
  849. var isString = (function (str) {
  850. return isType(str, 'String');
  851. });
  852. /**
  853. * 判断是否数字
  854. * @return {Boolean} 是否数字
  855. */
  856. var isNumber = function (value) {
  857. return isType(value, 'Number');
  858. };
  859. /**
  860. * Provides camera action & animation.
  861. */
  862. var AdvancedCamera = /*#__PURE__*/function (_Camera) {
  863. _inheritsLoose(AdvancedCamera, _Camera);
  864. function AdvancedCamera() {
  865. var _this;
  866. for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
  867. args[_key] = arguments[_key];
  868. }
  869. _this = _Camera.call.apply(_Camera, [this].concat(args)) || this;
  870. /**
  871. * switch between multiple landmarks
  872. */
  873. _this.landmarks = [];
  874. _this.landmarkAnimationID = void 0;
  875. return _this;
  876. }
  877. var _proto = AdvancedCamera.prototype;
  878. /**
  879. * Changes the azimuth and elevation with respect to the current camera axes
  880. * @param {Number} azimuth the relative azimuth
  881. * @param {Number} elevation the relative elevation
  882. * @param {Number} roll the relative roll
  883. */
  884. _proto.rotate = function rotate(azimuth, elevation, roll) {
  885. this.relElevation = gLite.getAngle(elevation);
  886. this.relAzimuth = gLite.getAngle(azimuth);
  887. this.relRoll = gLite.getAngle(roll);
  888. this.elevation += this.relElevation;
  889. this.azimuth += this.relAzimuth;
  890. this.roll += this.relRoll;
  891. if (this.type === gLite.CameraType.EXPLORING) {
  892. var rotX = setAxisAngle(create$4(), [1, 0, 0], gLite.deg2rad((this.rotateWorld ? 1 : -1) * this.relElevation));
  893. var rotY = setAxisAngle(create$4(), [0, 1, 0], gLite.deg2rad((this.rotateWorld ? 1 : -1) * this.relAzimuth));
  894. var rotZ = setAxisAngle(create$4(), [0, 0, 1], gLite.deg2rad(this.relRoll));
  895. var rotQ = multiply$1(create$4(), rotY, rotX);
  896. rotQ = multiply$1(create$4(), rotQ, rotZ);
  897. var rotMatrix = fromQuat(create$1(), rotQ);
  898. translate(this.matrix, this.matrix, [0, 0, -this.distance]);
  899. multiply(this.matrix, this.matrix, rotMatrix);
  900. translate(this.matrix, this.matrix, [0, 0, this.distance]);
  901. } else {
  902. if (Math.abs(this.elevation) > 90) {
  903. return this;
  904. }
  905. this.computeMatrix();
  906. }
  907. this._getAxes();
  908. if (this.type === gLite.CameraType.ORBITING || this.type === gLite.CameraType.EXPLORING) {
  909. this._getPosition();
  910. } else if (this.type === gLite.CameraType.TRACKING) {
  911. this._getFocalPoint();
  912. }
  913. this._update();
  914. return this;
  915. }
  916. /**
  917. * 沿水平(right) & 垂直(up)平移相机
  918. */;
  919. _proto.pan = function pan(tx, ty) {
  920. var coords = gLite.createVec3(tx, ty, 0);
  921. var pos = clone$1(this.position);
  922. add(pos, pos, scale(create$2(), this.right, coords[0]));
  923. add(pos, pos, scale(create$2(), this.up, coords[1]));
  924. this._setPosition(pos);
  925. this.triggerUpdate();
  926. return this;
  927. }
  928. /**
  929. * 沿 n 轴移动,当距离视点远时移动速度较快,离视点越近速度越慢
  930. */;
  931. _proto.dolly = function dolly(value) {
  932. var n = this.forward;
  933. var pos = clone$1(this.position);
  934. var step = value * this.dollyingStep;
  935. var updatedDistance = this.distance + value * this.dollyingStep;
  936. // 限制视点距离范围
  937. step = Math.max(Math.min(updatedDistance, this.maxDistance), this.minDistance) - this.distance;
  938. pos[0] += step * n[0];
  939. pos[1] += step * n[1];
  940. pos[2] += step * n[2];
  941. this._setPosition(pos);
  942. if (this.type === gLite.CameraType.ORBITING || this.type === gLite.CameraType.EXPLORING) {
  943. // 重新计算视点距离
  944. this._getDistance();
  945. } else if (this.type === gLite.CameraType.TRACKING) {
  946. // 保持视距,移动视点位置
  947. add(this.focalPoint, pos, this.distanceVector);
  948. }
  949. this.triggerUpdate();
  950. return this;
  951. };
  952. _proto.cancelLandmarkAnimation = function cancelLandmarkAnimation() {
  953. if (this.landmarkAnimationID !== undefined) {
  954. this.canvas.cancelAnimationFrame(this.landmarkAnimationID);
  955. }
  956. };
  957. _proto.createLandmark = function createLandmark(name, params) {
  958. var _position$, _position$2, _focalPoint$, _focalPoint$2;
  959. if (params === void 0) {
  960. params = {};
  961. }
  962. var _params = params,
  963. _params$position = _params.position,
  964. position = _params$position === void 0 ? this.position : _params$position,
  965. _params$focalPoint = _params.focalPoint,
  966. focalPoint = _params$focalPoint === void 0 ? this.focalPoint : _params$focalPoint,
  967. roll = _params.roll,
  968. zoom = _params.zoom;
  969. var camera = new gLite.runtime.CameraContribution();
  970. camera.setType(this.type, undefined);
  971. camera.setPosition(position[0], (_position$ = position[1]) !== null && _position$ !== void 0 ? _position$ : this.position[1], (_position$2 = position[2]) !== null && _position$2 !== void 0 ? _position$2 : this.position[2]);
  972. camera.setFocalPoint(focalPoint[0], (_focalPoint$ = focalPoint[1]) !== null && _focalPoint$ !== void 0 ? _focalPoint$ : this.focalPoint[1], (_focalPoint$2 = focalPoint[2]) !== null && _focalPoint$2 !== void 0 ? _focalPoint$2 : this.focalPoint[2]);
  973. camera.setRoll(roll !== null && roll !== void 0 ? roll : this.roll);
  974. camera.setZoom(zoom !== null && zoom !== void 0 ? zoom : this.zoom);
  975. var landmark = {
  976. name: name,
  977. matrix: clone(camera.getWorldTransform()),
  978. right: clone$1(camera.right),
  979. up: clone$1(camera.up),
  980. forward: clone$1(camera.forward),
  981. position: clone$1(camera.getPosition()),
  982. focalPoint: clone$1(camera.getFocalPoint()),
  983. distanceVector: clone$1(camera.getDistanceVector()),
  984. distance: camera.getDistance(),
  985. dollyingStep: camera.getDollyingStep(),
  986. azimuth: camera.getAzimuth(),
  987. elevation: camera.getElevation(),
  988. roll: camera.getRoll(),
  989. relAzimuth: camera.relAzimuth,
  990. relElevation: camera.relElevation,
  991. relRoll: camera.relRoll,
  992. zoom: camera.getZoom()
  993. };
  994. this.landmarks.push(landmark);
  995. return landmark;
  996. };
  997. _proto.gotoLandmark = function gotoLandmark(name, options) {
  998. var _this2 = this;
  999. if (options === void 0) {
  1000. options = {};
  1001. }
  1002. var landmark = isString(name) ? this.landmarks.find(function (l) {
  1003. return l.name === name;
  1004. }) : name;
  1005. if (landmark) {
  1006. var _ref = isNumber(options) ? {
  1007. duration: options
  1008. } : options,
  1009. _ref$easing = _ref.easing,
  1010. easing = _ref$easing === void 0 ? 'linear' : _ref$easing,
  1011. _ref$duration = _ref.duration,
  1012. duration = _ref$duration === void 0 ? 100 : _ref$duration,
  1013. _ref$easingFunction = _ref.easingFunction,
  1014. easingFunction = _ref$easingFunction === void 0 ? undefined : _ref$easingFunction,
  1015. _ref$onfinish = _ref.onfinish,
  1016. onfinish = _ref$onfinish === void 0 ? undefined : _ref$onfinish;
  1017. var epsilon = 0.01;
  1018. if (duration === 0) {
  1019. this.syncFromLandmark(landmark);
  1020. if (onfinish) {
  1021. onfinish();
  1022. }
  1023. return;
  1024. }
  1025. // cancel ongoing animation
  1026. this.cancelLandmarkAnimation();
  1027. var destPosition = landmark.position;
  1028. var destFocalPoint = landmark.focalPoint;
  1029. var destZoom = landmark.zoom;
  1030. var destRoll = landmark.roll;
  1031. var easingFunc = easingFunction || gLite.runtime.EasingFunction(easing);
  1032. var timeStart;
  1033. var endAnimation = function endAnimation() {
  1034. _this2.setFocalPoint(destFocalPoint);
  1035. _this2.setPosition(destPosition);
  1036. _this2.setRoll(destRoll);
  1037. _this2.setZoom(destZoom);
  1038. _this2.computeMatrix();
  1039. _this2.triggerUpdate();
  1040. if (onfinish) {
  1041. onfinish();
  1042. }
  1043. };
  1044. var animate = function animate(timestamp) {
  1045. if (timeStart === undefined) {
  1046. timeStart = timestamp;
  1047. }
  1048. var elapsed = timestamp - timeStart;
  1049. if (elapsed > duration) {
  1050. endAnimation();
  1051. return;
  1052. }
  1053. // use the same ease function in animation system
  1054. var t = easingFunc(elapsed / duration);
  1055. var interFocalPoint = create$2();
  1056. var interPosition = create$2();
  1057. var interZoom = 1;
  1058. var interRoll = 0;
  1059. lerp(interFocalPoint, _this2.focalPoint, destFocalPoint, t);
  1060. lerp(interPosition, _this2.position, destPosition, t);
  1061. interRoll = _this2.roll * (1 - t) + destRoll * t;
  1062. interZoom = _this2.zoom * (1 - t) + destZoom * t;
  1063. _this2.setFocalPoint(interFocalPoint);
  1064. _this2.setPosition(interPosition);
  1065. _this2.setRoll(interRoll);
  1066. _this2.setZoom(interZoom);
  1067. var dist$1 = dist(interFocalPoint, destFocalPoint) + dist(interPosition, destPosition);
  1068. if (dist$1 <= epsilon && destZoom == undefined && destRoll == undefined) {
  1069. endAnimation();
  1070. return;
  1071. }
  1072. _this2.computeMatrix();
  1073. _this2.triggerUpdate();
  1074. if (elapsed < duration) {
  1075. _this2.landmarkAnimationID = _this2.canvas.requestAnimationFrame(animate);
  1076. }
  1077. };
  1078. this.canvas.requestAnimationFrame(animate);
  1079. }
  1080. };
  1081. _proto.syncFromLandmark = function syncFromLandmark(landmark) {
  1082. this.matrix = copy(this.matrix, landmark.matrix);
  1083. this.right = copy$1(this.right, landmark.right);
  1084. this.up = copy$1(this.up, landmark.up);
  1085. this.forward = copy$1(this.forward, landmark.forward);
  1086. this.position = copy$1(this.position, landmark.position);
  1087. this.focalPoint = copy$1(this.focalPoint, landmark.focalPoint);
  1088. this.distanceVector = copy$1(this.distanceVector, landmark.distanceVector);
  1089. this.azimuth = landmark.azimuth;
  1090. this.elevation = landmark.elevation;
  1091. this.roll = landmark.roll;
  1092. this.relAzimuth = landmark.relAzimuth;
  1093. this.relElevation = landmark.relElevation;
  1094. this.relRoll = landmark.relRoll;
  1095. this.dollyingStep = landmark.dollyingStep;
  1096. this.distance = landmark.distance;
  1097. this.zoom = landmark.zoom;
  1098. };
  1099. return AdvancedCamera;
  1100. }(gLite.Camera);
  1101. gLite.runtime.CameraContribution = AdvancedCamera;
  1102. exports.AdvancedCamera = AdvancedCamera;
  1103. Object.defineProperty(exports, '__esModule', { value: true });
  1104. })));