Source: libs/gl-matrix2.js

  1. /**
  2. * Auto-generated set of math modules.
  3. * based on glMatrix 2.1.0
  4. * pay attention to parameters order, quat.rotationTo() and quat.setAxes()
  5. */
  6. b4w.module["__vec3"] = function(exports, require) {
  7. var GLMAT_EPSILON = 0.0000001;
  8. var GLMAT_ARRAY_TYPE = (typeof Float32Array !== 'undefined') ? Float32Array : Array;
  9. var GLMAT_RANDOM = Math.random;
  10. /* Copyright (c) 2015, Brandon Jones, Colin MacKenzie IV.
  11. Permission is hereby granted, free of charge, to any person obtaining a copy
  12. of this software and associated documentation files (the "Software"), to deal
  13. in the Software without restriction, including without limitation the rights
  14. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  15. copies of the Software, and to permit persons to whom the Software is
  16. furnished to do so, subject to the following conditions:
  17. The above copyright notice and this permission notice shall be included in
  18. all copies or substantial portions of the Software.
  19. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  20. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  21. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  22. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  23. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  24. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  25. THE SOFTWARE. */
  26. /**
  27. * @module 3 Dimensional Vector
  28. * @name vec3
  29. */
  30. var vec3 = exports;
  31. /**
  32. * Creates a new, empty vec3
  33. *
  34. * @returns {Vec3} a new 3D vector
  35. * @method module:vec3.create
  36. */
  37. vec3.create = function() {
  38. var out = new GLMAT_ARRAY_TYPE(3);
  39. out[0] = 0;
  40. out[1] = 0;
  41. out[2] = 0;
  42. return out;
  43. };
  44. /**
  45. * Creates a new vec3 initialized with values from an existing vector
  46. *
  47. * @param {Vec3} a vector to clone
  48. * @returns {Vec3} a new 3D vector
  49. * @method module:vec3.clone
  50. */
  51. vec3.clone = function(a) {
  52. var out = new GLMAT_ARRAY_TYPE(3);
  53. out[0] = a[0];
  54. out[1] = a[1];
  55. out[2] = a[2];
  56. return out;
  57. };
  58. /**
  59. * Creates a new vec3 initialized with the given values
  60. *
  61. * @param {Number} x X component
  62. * @param {Number} y Y component
  63. * @param {Number} z Z component
  64. * @returns {Vec3} a new 3D vector
  65. * @method module:vec3.fromValues
  66. */
  67. vec3.fromValues = function(x, y, z) {
  68. var out = new GLMAT_ARRAY_TYPE(3);
  69. out[0] = x;
  70. out[1] = y;
  71. out[2] = z;
  72. return out;
  73. };
  74. /**
  75. * Copy the values from one vec3 to another
  76. *
  77. * @param {Vec3} a the source vector
  78. * @returns {Vec3} out
  79. * @param {Vec3} out the receiving vector
  80. * @method module:vec3.copy
  81. */
  82. vec3.copy = function(a, out) {
  83. out[0] = a[0];
  84. out[1] = a[1];
  85. out[2] = a[2];
  86. return out;
  87. };
  88. /**
  89. * Set the components of a vec3 to the given values
  90. *
  91. * @param {Number} x X component
  92. * @param {Number} y Y component
  93. * @param {Number} z Z component
  94. * @returns {Vec3} out
  95. * @param {Vec3} out the receiving vector
  96. * @method module:vec3.set
  97. */
  98. vec3.set = function(x, y, z, out) {
  99. out[0] = x;
  100. out[1] = y;
  101. out[2] = z;
  102. return out;
  103. };
  104. /**
  105. * Adds two vec3's
  106. *
  107. * @param {Vec3} a the first operand
  108. * @param {Vec3} b the second operand
  109. * @returns {Vec3} out
  110. * @param {Vec3} out the receiving vector
  111. * @method module:vec3.add
  112. */
  113. vec3.add = function(a, b, out) {
  114. out[0] = a[0] + b[0];
  115. out[1] = a[1] + b[1];
  116. out[2] = a[2] + b[2];
  117. return out;
  118. };
  119. /**
  120. * Subtracts vector b from vector a
  121. *
  122. * @param {Vec3} a the first operand
  123. * @param {Vec3} b the second operand
  124. * @returns {Vec3} out
  125. * @param {Vec3} out the receiving vector
  126. * @method module:vec3.subtract
  127. */
  128. vec3.subtract = function(a, b, out) {
  129. out[0] = a[0] - b[0];
  130. out[1] = a[1] - b[1];
  131. out[2] = a[2] - b[2];
  132. return out;
  133. };
  134. /**
  135. * Alias for {@link vec3.subtract}
  136. * @function
  137. * @method module:vec3.sub
  138. */
  139. vec3.sub = vec3.subtract;
  140. /**
  141. * Multiplies two vec3's
  142. *
  143. * @param {Vec3} a the first operand
  144. * @param {Vec3} b the second operand
  145. * @returns {Vec3} out
  146. * @param {Vec3} out the receiving vector
  147. * @method module:vec3.multiply
  148. */
  149. vec3.multiply = function(a, b, out) {
  150. out[0] = a[0] * b[0];
  151. out[1] = a[1] * b[1];
  152. out[2] = a[2] * b[2];
  153. return out;
  154. };
  155. /**
  156. * Alias for {@link vec3.multiply}
  157. * @function
  158. * @method module:vec3.mul
  159. */
  160. vec3.mul = vec3.multiply;
  161. /**
  162. * Divides two vec3's
  163. *
  164. * @param {Vec3} a the first operand
  165. * @param {Vec3} b the second operand
  166. * @returns {Vec3} out
  167. * @param {Vec3} out the receiving vector
  168. * @method module:vec3.divide
  169. */
  170. vec3.divide = function(a, b, out) {
  171. out[0] = a[0] / b[0];
  172. out[1] = a[1] / b[1];
  173. out[2] = a[2] / b[2];
  174. return out;
  175. };
  176. /**
  177. * Alias for {@link vec3.divide}
  178. * @function
  179. * @method module:vec3.div
  180. */
  181. vec3.div = vec3.divide;
  182. /**
  183. * Returns the minimum of two vec3's
  184. *
  185. * @param {Vec3} a the first operand
  186. * @param {Vec3} b the second operand
  187. * @returns {Vec3} out
  188. * @param {Vec3} out the receiving vector
  189. * @method module:vec3.min
  190. */
  191. vec3.min = function(a, b, out) {
  192. out[0] = Math.min(a[0], b[0]);
  193. out[1] = Math.min(a[1], b[1]);
  194. out[2] = Math.min(a[2], b[2]);
  195. return out;
  196. };
  197. /**
  198. * Returns the maximum of two vec3's
  199. *
  200. * @param {Vec3} a the first operand
  201. * @param {Vec3} b the second operand
  202. * @returns {Vec3} out
  203. * @param {Vec3} out the receiving vector
  204. * @method module:vec3.max
  205. */
  206. vec3.max = function(a, b, out) {
  207. out[0] = Math.max(a[0], b[0]);
  208. out[1] = Math.max(a[1], b[1]);
  209. out[2] = Math.max(a[2], b[2]);
  210. return out;
  211. };
  212. /**
  213. * Scales a vec3 by a scalar number
  214. *
  215. * @param {Vec3} a the vector to scale
  216. * @param {Number} b amount to scale the vector by
  217. * @returns {Vec3} out
  218. * @param {Vec3} out the receiving vector
  219. * @method module:vec3.scale
  220. */
  221. vec3.scale = function(a, b, out) {
  222. out[0] = a[0] * b;
  223. out[1] = a[1] * b;
  224. out[2] = a[2] * b;
  225. return out;
  226. };
  227. /**
  228. * Adds two vec3's after scaling the second operand by a scalar value
  229. *
  230. * @param {Vec3} a the first operand
  231. * @param {Vec3} b the second operand
  232. * @param {Number} scale the amount to scale b by before adding
  233. * @returns {Vec3} out
  234. * @param {Vec3} out the receiving vector
  235. * @method module:vec3.scaleAndAdd
  236. */
  237. vec3.scaleAndAdd = function(a, b, scale, out) {
  238. out[0] = a[0] + (b[0] * scale);
  239. out[1] = a[1] + (b[1] * scale);
  240. out[2] = a[2] + (b[2] * scale);
  241. return out;
  242. };
  243. /**
  244. * Calculates the euclidian distance between two vec3's
  245. *
  246. * @param {Vec3} a the first operand
  247. * @param {Vec3} b the second operand
  248. * @returns {Number} distance between a and b
  249. * @method module:vec3.distance
  250. */
  251. vec3.distance = function(a, b) {
  252. var x = b[0] - a[0],
  253. y = b[1] - a[1],
  254. z = b[2] - a[2];
  255. return Math.sqrt(x*x + y*y + z*z);
  256. };
  257. /**
  258. * Alias for {@link vec3.distance}
  259. * @function
  260. * @method module:vec3.dist
  261. */
  262. vec3.dist = vec3.distance;
  263. /**
  264. * Calculates the squared euclidian distance between two vec3's
  265. *
  266. * @param {Vec3} a the first operand
  267. * @param {Vec3} b the second operand
  268. * @returns {Number} squared distance between a and b
  269. * @method module:vec3.squaredDistance
  270. */
  271. vec3.squaredDistance = function(a, b) {
  272. var x = b[0] - a[0],
  273. y = b[1] - a[1],
  274. z = b[2] - a[2];
  275. return x*x + y*y + z*z;
  276. };
  277. /**
  278. * Alias for {@link vec3.squaredDistance}
  279. * @function
  280. * @method module:vec3.sqrDist
  281. */
  282. vec3.sqrDist = vec3.squaredDistance;
  283. /**
  284. * Calculates the length of a vec3
  285. *
  286. * @param {Vec3} a vector to calculate length of
  287. * @returns {Number} length of a
  288. * @method module:vec3.length
  289. */
  290. vec3.length = function (a) {
  291. var x = a[0],
  292. y = a[1],
  293. z = a[2];
  294. return Math.sqrt(x*x + y*y + z*z);
  295. };
  296. /**
  297. * Alias for {@link vec3.length}
  298. * @function
  299. * @method module:vec3.len
  300. */
  301. vec3.len = vec3.length;
  302. /**
  303. * Calculates the squared length of a vec3
  304. *
  305. * @param {Vec3} a vector to calculate squared length of
  306. * @returns {Number} squared length of a
  307. * @method module:vec3.squaredLength
  308. */
  309. vec3.squaredLength = function (a) {
  310. var x = a[0],
  311. y = a[1],
  312. z = a[2];
  313. return x*x + y*y + z*z;
  314. };
  315. /**
  316. * Alias for {@link vec3.squaredLength}
  317. * @function
  318. * @method module:vec3.sqrLen
  319. */
  320. vec3.sqrLen = vec3.squaredLength;
  321. /**
  322. * Negates the components of a vec3
  323. *
  324. * @param {Vec3} a vector to negate
  325. * @returns {Vec3} out
  326. * @param {Vec3} out the receiving vector
  327. * @method module:vec3.negate
  328. */
  329. vec3.negate = function(a, out) {
  330. out[0] = -a[0];
  331. out[1] = -a[1];
  332. out[2] = -a[2];
  333. return out;
  334. };
  335. /**
  336. * Returns the inverse of the components of a vec3
  337. *
  338. * @param {Vec3} a vector to invert
  339. * @returns {Vec3} out
  340. * @param {Vec3} out the receiving vector
  341. * @method module:vec3.inverse
  342. */
  343. vec3.inverse = function(a, out) {
  344. out[0] = 1.0 / a[0];
  345. out[1] = 1.0 / a[1];
  346. out[2] = 1.0 / a[2];
  347. return out;
  348. };
  349. /**
  350. * Normalize a vec3
  351. *
  352. * @param {Vec3} a vector to normalize
  353. * @returns {Vec3} out
  354. * @param {Vec3} out the receiving vector
  355. * @method module:vec3.normalize
  356. */
  357. vec3.normalize = function(a, out) {
  358. var x = a[0],
  359. y = a[1],
  360. z = a[2];
  361. var len = x*x + y*y + z*z;
  362. if (len > 0) {
  363. //TODO: evaluate use of glm_invsqrt here?
  364. len = 1 / Math.sqrt(len);
  365. out[0] = a[0] * len;
  366. out[1] = a[1] * len;
  367. out[2] = a[2] * len;
  368. }
  369. return out;
  370. };
  371. /**
  372. * Calculates the dot product of two vec3's
  373. *
  374. * @param {Vec3} a the first operand
  375. * @param {Vec3} b the second operand
  376. * @returns {Number} dot product of a and b
  377. * @method module:vec3.dot
  378. */
  379. vec3.dot = function (a, b) {
  380. return a[0] * b[0] + a[1] * b[1] + a[2] * b[2];
  381. };
  382. /**
  383. * Computes the cross product of two vec3's
  384. *
  385. * @param {Vec3} a the first operand
  386. * @param {Vec3} b the second operand
  387. * @returns {Vec3} out
  388. * @param {Vec3} out the receiving vector
  389. * @method module:vec3.cross
  390. */
  391. vec3.cross = function(a, b, out) {
  392. var ax = a[0], ay = a[1], az = a[2],
  393. bx = b[0], by = b[1], bz = b[2];
  394. out[0] = ay * bz - az * by;
  395. out[1] = az * bx - ax * bz;
  396. out[2] = ax * by - ay * bx;
  397. return out;
  398. };
  399. /**
  400. * Performs a linear interpolation between two vec3's
  401. *
  402. * @param {Vec3} a the first operand
  403. * @param {Vec3} b the second operand
  404. * @param {Number} t interpolation amount between the two inputs
  405. * @returns {Vec3} out
  406. * @param {Vec3} out the receiving vector
  407. * @method module:vec3.lerp
  408. */
  409. vec3.lerp = function (a, b, t, out) {
  410. var ax = a[0],
  411. ay = a[1],
  412. az = a[2];
  413. out[0] = ax + t * (b[0] - ax);
  414. out[1] = ay + t * (b[1] - ay);
  415. out[2] = az + t * (b[2] - az);
  416. return out;
  417. };
  418. /**
  419. * Performs a hermite interpolation with two control points
  420. *
  421. * @param {Vec3} a the first operand
  422. * @param {Vec3} b the second operand
  423. * @param {Vec3} c the third operand
  424. * @param {Vec3} d the fourth operand
  425. * @param {Number} t interpolation amount between the two inputs
  426. * @returns {Vec3} out
  427. * @param {Vec3} out the receiving vector
  428. * @method module:vec3.hermite
  429. */
  430. vec3.hermite = function (a, b, c, d, t, out) {
  431. var factorTimes2 = t * t,
  432. factor1 = factorTimes2 * (2 * t - 3) + 1,
  433. factor2 = factorTimes2 * (t - 2) + t,
  434. factor3 = factorTimes2 * (t - 1),
  435. factor4 = factorTimes2 * (3 - 2 * t);
  436. out[0] = a[0] * factor1 + b[0] * factor2 + c[0] * factor3 + d[0] * factor4;
  437. out[1] = a[1] * factor1 + b[1] * factor2 + c[1] * factor3 + d[1] * factor4;
  438. out[2] = a[2] * factor1 + b[2] * factor2 + c[2] * factor3 + d[2] * factor4;
  439. return out;
  440. };
  441. /**
  442. * Performs a bezier interpolation with two control points
  443. *
  444. * @param {Vec3} a the first operand
  445. * @param {Vec3} b the second operand
  446. * @param {Vec3} c the third operand
  447. * @param {Vec3} d the fourth operand
  448. * @param {Number} t interpolation amount between the two inputs
  449. * @returns {Vec3} out
  450. * @param {Vec3} out the receiving vector
  451. * @method module:vec3.bezier
  452. */
  453. vec3.bezier = function (a, b, c, d, t, out) {
  454. var inverseFactor = 1 - t,
  455. inverseFactorTimesTwo = inverseFactor * inverseFactor,
  456. factorTimes2 = t * t,
  457. factor1 = inverseFactorTimesTwo * inverseFactor,
  458. factor2 = 3 * t * inverseFactorTimesTwo,
  459. factor3 = 3 * factorTimes2 * inverseFactor,
  460. factor4 = factorTimes2 * t;
  461. out[0] = a[0] * factor1 + b[0] * factor2 + c[0] * factor3 + d[0] * factor4;
  462. out[1] = a[1] * factor1 + b[1] * factor2 + c[1] * factor3 + d[1] * factor4;
  463. out[2] = a[2] * factor1 + b[2] * factor2 + c[2] * factor3 + d[2] * factor4;
  464. return out;
  465. };
  466. /**
  467. * Generates a random vector with the given scale
  468. *
  469. * @param {Number} [scale] Length of the resulting vector. If ommitted, a unit vector will be returned
  470. * @returns {Vec3} out
  471. * @param {Vec3} out the receiving vector
  472. * @method module:vec3.random
  473. */
  474. vec3.random = function (scale, out) {
  475. scale = scale || 1.0;
  476. var r = GLMAT_RANDOM() * 2.0 * Math.PI;
  477. var z = (GLMAT_RANDOM() * 2.0) - 1.0;
  478. var zScale = Math.sqrt(1.0-z*z) * scale;
  479. out[0] = Math.cos(r) * zScale;
  480. out[1] = Math.sin(r) * zScale;
  481. out[2] = z * scale;
  482. return out;
  483. };
  484. /**
  485. * Transforms the vec3 with a mat4.
  486. * 4th vector component is implicitly '1'
  487. *
  488. * @param {Vec3} a the vector to transform
  489. * @param {Mat4} m matrix to transform with
  490. * @returns {Vec3} out
  491. * @param {Vec3} out the receiving vector
  492. * @method module:vec3.transformMat4
  493. */
  494. vec3.transformMat4 = function(a, m, out) {
  495. var x = a[0], y = a[1], z = a[2],
  496. w = m[3] * x + m[7] * y + m[11] * z + m[15];
  497. w = w || 1.0;
  498. out[0] = (m[0] * x + m[4] * y + m[8] * z + m[12]) / w;
  499. out[1] = (m[1] * x + m[5] * y + m[9] * z + m[13]) / w;
  500. out[2] = (m[2] * x + m[6] * y + m[10] * z + m[14]) / w;
  501. return out;
  502. };
  503. /**
  504. * Transforms the vec3 with a mat3.
  505. *
  506. * @param {Vec3} a the vector to transform
  507. * @param {Mat4} m the 3x3 matrix to transform with
  508. * @returns {Vec3} out
  509. * @param {Vec3} out the receiving vector
  510. * @method module:vec3.transformMat3
  511. */
  512. vec3.transformMat3 = function(a, m, out) {
  513. var x = a[0], y = a[1], z = a[2];
  514. out[0] = x * m[0] + y * m[3] + z * m[6];
  515. out[1] = x * m[1] + y * m[4] + z * m[7];
  516. out[2] = x * m[2] + y * m[5] + z * m[8];
  517. return out;
  518. };
  519. /**
  520. * Transforms the vec3 with a quat
  521. *
  522. * @param {Vec3} a the vector to transform
  523. * @param {Quat} q quaternion to transform with
  524. * @returns {Vec3} out
  525. * @param {Vec3} out the receiving vector
  526. * @method module:vec3.transformQuat
  527. */
  528. vec3.transformQuat = function(a, q, out) {
  529. // benchmarks: http://jsperf.com/quaternion-transform-vec3-implementations
  530. var x = a[0], y = a[1], z = a[2],
  531. qx = q[0], qy = q[1], qz = q[2], qw = q[3],
  532. // calculate quat * vec
  533. ix = qw * x + qy * z - qz * y,
  534. iy = qw * y + qz * x - qx * z,
  535. iz = qw * z + qx * y - qy * x,
  536. iw = -qx * x - qy * y - qz * z;
  537. // calculate result * inverse quat
  538. out[0] = ix * qw + iw * -qx + iy * -qz - iz * -qy;
  539. out[1] = iy * qw + iw * -qy + iz * -qx - ix * -qz;
  540. out[2] = iz * qw + iw * -qz + ix * -qy - iy * -qx;
  541. return out;
  542. };
  543. /**
  544. * Rotate a 3D vector around the x-axis
  545. * @param {Vec3} a The vec3 point to rotate
  546. * @param {Vec3} b The origin of the rotation
  547. * @param {Number} c The angle of rotation
  548. * @returns {Vec3} out
  549. * @param {Vec3} out The receiving vec3
  550. * @method module:vec3.rotateX
  551. */
  552. vec3.rotateX = function(a, b, c, out){
  553. var p = [], r=[];
  554. //Translate point to the origin
  555. p[0] = a[0] - b[0];
  556. p[1] = a[1] - b[1];
  557. p[2] = a[2] - b[2];
  558. //perform rotation
  559. r[0] = p[0];
  560. r[1] = p[1]*Math.cos(c) - p[2]*Math.sin(c);
  561. r[2] = p[1]*Math.sin(c) + p[2]*Math.cos(c);
  562. //translate to correct position
  563. out[0] = r[0] + b[0];
  564. out[1] = r[1] + b[1];
  565. out[2] = r[2] + b[2];
  566. return out;
  567. };
  568. /**
  569. * Rotate a 3D vector around the y-axis
  570. * @param {Vec3} a The vec3 point to rotate
  571. * @param {Vec3} b The origin of the rotation
  572. * @param {Number} c The angle of rotation
  573. * @returns {Vec3} out
  574. * @param {Vec3} out The receiving vec3
  575. * @method module:vec3.rotateY
  576. */
  577. vec3.rotateY = function(a, b, c, out){
  578. var p = [], r=[];
  579. //Translate point to the origin
  580. p[0] = a[0] - b[0];
  581. p[1] = a[1] - b[1];
  582. p[2] = a[2] - b[2];
  583. //perform rotation
  584. r[0] = p[2]*Math.sin(c) + p[0]*Math.cos(c);
  585. r[1] = p[1];
  586. r[2] = p[2]*Math.cos(c) - p[0]*Math.sin(c);
  587. //translate to correct position
  588. out[0] = r[0] + b[0];
  589. out[1] = r[1] + b[1];
  590. out[2] = r[2] + b[2];
  591. return out;
  592. };
  593. /**
  594. * Rotate a 3D vector around the z-axis
  595. * @param {Vec3} a The vec3 point to rotate
  596. * @param {Vec3} b The origin of the rotation
  597. * @param {Number} c The angle of rotation
  598. * @returns {Vec3} out
  599. * @param {Vec3} out The receiving vec3
  600. * @method module:vec3.rotateZ
  601. */
  602. vec3.rotateZ = function(a, b, c, out){
  603. var p = [], r=[];
  604. //Translate point to the origin
  605. p[0] = a[0] - b[0];
  606. p[1] = a[1] - b[1];
  607. p[2] = a[2] - b[2];
  608. //perform rotation
  609. r[0] = p[0]*Math.cos(c) - p[1]*Math.sin(c);
  610. r[1] = p[0]*Math.sin(c) + p[1]*Math.cos(c);
  611. r[2] = p[2];
  612. //translate to correct position
  613. out[0] = r[0] + b[0];
  614. out[1] = r[1] + b[1];
  615. out[2] = r[2] + b[2];
  616. return out;
  617. };
  618. /**
  619. * Perform some operation over an array of vec3s.
  620. *
  621. * @param {Array} a the array of vectors to iterate over
  622. * @param {Number} stride Number of elements between the start of each vec3. If 0 assumes tightly packed
  623. * @param {Number} offset Number of elements to skip at the beginning of the array
  624. * @param {Number} count Number of vec3s to iterate over. If 0 iterates over entire array
  625. * @param {Function} fn Function to call for each vector in the array
  626. * @param {Object} [arg] additional argument to pass to fn
  627. * @returns {Array} a
  628. * @function
  629. * @method module:vec3.forEach
  630. */
  631. vec3.forEach = (function() {
  632. var vec = vec3.create();
  633. return function(a, stride, offset, count, fn, arg) {
  634. var i, l;
  635. if(!stride) {
  636. stride = 3;
  637. }
  638. if(!offset) {
  639. offset = 0;
  640. }
  641. if(count) {
  642. l = Math.min((count * stride) + offset, a.length);
  643. } else {
  644. l = a.length;
  645. }
  646. for(i = offset; i < l; i += stride) {
  647. vec[0] = a[i]; vec[1] = a[i+1]; vec[2] = a[i+2];
  648. fn(vec, arg, vec);
  649. a[i] = vec[0]; a[i+1] = vec[1]; a[i+2] = vec[2];
  650. }
  651. return a;
  652. };
  653. })();
  654. /**
  655. * Get the angle between two 3D vectors
  656. * @param {Vec3} a The first operand
  657. * @param {Vec3} b The second operand
  658. * @returns {Number} The angle in radians
  659. * @method module:vec3.angle
  660. */
  661. vec3.angle = function(a, b) {
  662. var tempA = vec3.fromValues(a[0], a[1], a[2]);
  663. var tempB = vec3.fromValues(b[0], b[1], b[2]);
  664. vec3.normalize(tempA, tempA);
  665. vec3.normalize(tempB, tempB);
  666. var cosine = vec3.dot(tempA, tempB);
  667. if(cosine > 1.0){
  668. return 0;
  669. } else {
  670. return Math.acos(cosine);
  671. }
  672. };
  673. /**
  674. * Returns a string representation of a vector
  675. *
  676. * @param {Vec3} vec vector to represent as a string
  677. * @returns {String} string representation of the vector
  678. * @method module:vec3.str
  679. */
  680. vec3.str = function (a) {
  681. return 'vec3(' + a[0] + ', ' + a[1] + ', ' + a[2] + ')';
  682. };
  683. }
  684. b4w.module["vec3"] = b4w.module["__vec3"];
  685. b4w.module["__vec4"] = function(exports, require) {
  686. var GLMAT_EPSILON = 0.0000001;
  687. var GLMAT_ARRAY_TYPE = (typeof Float32Array !== 'undefined') ? Float32Array : Array;
  688. var GLMAT_RANDOM = Math.random;
  689. /* Copyright (c) 2015, Brandon Jones, Colin MacKenzie IV.
  690. Permission is hereby granted, free of charge, to any person obtaining a copy
  691. of this software and associated documentation files (the "Software"), to deal
  692. in the Software without restriction, including without limitation the rights
  693. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  694. copies of the Software, and to permit persons to whom the Software is
  695. furnished to do so, subject to the following conditions:
  696. The above copyright notice and this permission notice shall be included in
  697. all copies or substantial portions of the Software.
  698. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  699. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  700. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  701. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  702. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  703. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  704. THE SOFTWARE. */
  705. /**
  706. * @module 4 Dimensional Vector
  707. * @name vec4
  708. */
  709. var vec4 = exports;
  710. /**
  711. * Creates a new, empty vec4
  712. *
  713. * @returns {Vec4} a new 4D vector
  714. * @method module:vec4.create
  715. */
  716. vec4.create = function() {
  717. var out = new GLMAT_ARRAY_TYPE(4);
  718. out[0] = 0;
  719. out[1] = 0;
  720. out[2] = 0;
  721. out[3] = 0;
  722. return out;
  723. };
  724. /**
  725. * Creates a new vec4 initialized with values from an existing vector
  726. *
  727. * @param {Vec4} a vector to clone
  728. * @returns {Vec4} a new 4D vector
  729. * @method module:vec4.clone
  730. */
  731. vec4.clone = function(a) {
  732. var out = new GLMAT_ARRAY_TYPE(4);
  733. out[0] = a[0];
  734. out[1] = a[1];
  735. out[2] = a[2];
  736. out[3] = a[3];
  737. return out;
  738. };
  739. /**
  740. * Creates a new vec4 initialized with the given values
  741. *
  742. * @param {Number} x X component
  743. * @param {Number} y Y component
  744. * @param {Number} z Z component
  745. * @param {Number} w W component
  746. * @returns {Vec4} a new 4D vector
  747. * @method module:vec4.fromValues
  748. */
  749. vec4.fromValues = function(x, y, z, w) {
  750. var out = new GLMAT_ARRAY_TYPE(4);
  751. out[0] = x;
  752. out[1] = y;
  753. out[2] = z;
  754. out[3] = w;
  755. return out;
  756. };
  757. /**
  758. * Copy the values from one vec4 to another
  759. *
  760. * @param {Vec4} a the source vector
  761. * @returns {Vec4} out
  762. * @param {Vec4} out the receiving vector
  763. * @method module:vec4.copy
  764. */
  765. vec4.copy = function(a, out) {
  766. out[0] = a[0];
  767. out[1] = a[1];
  768. out[2] = a[2];
  769. out[3] = a[3];
  770. return out;
  771. };
  772. /**
  773. * Set the components of a vec4 to the given values
  774. *
  775. * @param {Number} x X component
  776. * @param {Number} y Y component
  777. * @param {Number} z Z component
  778. * @param {Number} w W component
  779. * @returns {Vec4} out
  780. * @param {Vec4} out the receiving vector
  781. * @method module:vec4.set
  782. */
  783. vec4.set = function(x, y, z, w, out) {
  784. out[0] = x;
  785. out[1] = y;
  786. out[2] = z;
  787. out[3] = w;
  788. return out;
  789. };
  790. /**
  791. * Adds two vec4's
  792. *
  793. * @param {Vec4} a the first operand
  794. * @param {Vec4} b the second operand
  795. * @returns {Vec4} out
  796. * @param {Vec4} out the receiving vector
  797. * @method module:vec4.add
  798. */
  799. vec4.add = function(a, b, out) {
  800. out[0] = a[0] + b[0];
  801. out[1] = a[1] + b[1];
  802. out[2] = a[2] + b[2];
  803. out[3] = a[3] + b[3];
  804. return out;
  805. };
  806. /**
  807. * Subtracts vector b from vector a
  808. *
  809. * @param {Vec4} a the first operand
  810. * @param {Vec4} b the second operand
  811. * @returns {Vec4} out
  812. * @param {Vec4} out the receiving vector
  813. * @method module:vec4.subtract
  814. */
  815. vec4.subtract = function(a, b, out) {
  816. out[0] = a[0] - b[0];
  817. out[1] = a[1] - b[1];
  818. out[2] = a[2] - b[2];
  819. out[3] = a[3] - b[3];
  820. return out;
  821. };
  822. /**
  823. * Alias for {@link vec4.subtract}
  824. * @function
  825. * @method module:vec4.sub
  826. */
  827. vec4.sub = vec4.subtract;
  828. /**
  829. * Multiplies two vec4's
  830. *
  831. * @param {Vec4} a the first operand
  832. * @param {Vec4} b the second operand
  833. * @returns {Vec4} out
  834. * @param {Vec4} out the receiving vector
  835. * @method module:vec4.multiply
  836. */
  837. vec4.multiply = function(a, b, out) {
  838. out[0] = a[0] * b[0];
  839. out[1] = a[1] * b[1];
  840. out[2] = a[2] * b[2];
  841. out[3] = a[3] * b[3];
  842. return out;
  843. };
  844. /**
  845. * Alias for {@link vec4.multiply}
  846. * @function
  847. * @method module:vec4.mul
  848. */
  849. vec4.mul = vec4.multiply;
  850. /**
  851. * Divides two vec4's
  852. *
  853. * @param {Vec4} a the first operand
  854. * @param {Vec4} b the second operand
  855. * @returns {Vec4} out
  856. * @param {Vec4} out the receiving vector
  857. * @method module:vec4.divide
  858. */
  859. vec4.divide = function(a, b, out) {
  860. out[0] = a[0] / b[0];
  861. out[1] = a[1] / b[1];
  862. out[2] = a[2] / b[2];
  863. out[3] = a[3] / b[3];
  864. return out;
  865. };
  866. /**
  867. * Alias for {@link vec4.divide}
  868. * @function
  869. * @method module:vec4.div
  870. */
  871. vec4.div = vec4.divide;
  872. /**
  873. * Returns the minimum of two vec4's
  874. *
  875. * @param {Vec4} a the first operand
  876. * @param {Vec4} b the second operand
  877. * @returns {Vec4} out
  878. * @param {Vec4} out the receiving vector
  879. * @method module:vec4.min
  880. */
  881. vec4.min = function(a, b, out) {
  882. out[0] = Math.min(a[0], b[0]);
  883. out[1] = Math.min(a[1], b[1]);
  884. out[2] = Math.min(a[2], b[2]);
  885. out[3] = Math.min(a[3], b[3]);
  886. return out;
  887. };
  888. /**
  889. * Returns the maximum of two vec4's
  890. *
  891. * @param {Vec4} a the first operand
  892. * @param {Vec4} b the second operand
  893. * @returns {Vec4} out
  894. * @param {Vec4} out the receiving vector
  895. * @method module:vec4.max
  896. */
  897. vec4.max = function(a, b, out) {
  898. out[0] = Math.max(a[0], b[0]);
  899. out[1] = Math.max(a[1], b[1]);
  900. out[2] = Math.max(a[2], b[2]);
  901. out[3] = Math.max(a[3], b[3]);
  902. return out;
  903. };
  904. /**
  905. * Scales a vec4 by a scalar number
  906. *
  907. * @param {Vec4} a the vector to scale
  908. * @param {Number} b amount to scale the vector by
  909. * @returns {Vec4} out
  910. * @param {Vec4} out the receiving vector
  911. * @method module:vec4.scale
  912. */
  913. vec4.scale = function(a, b, out) {
  914. out[0] = a[0] * b;
  915. out[1] = a[1] * b;
  916. out[2] = a[2] * b;
  917. out[3] = a[3] * b;
  918. return out;
  919. };
  920. /**
  921. * Adds two vec4's after scaling the second operand by a scalar value
  922. *
  923. * @param {Vec4} a the first operand
  924. * @param {Vec4} b the second operand
  925. * @param {Number} scale the amount to scale b by before adding
  926. * @returns {Vec4} out
  927. * @param {Vec4} out the receiving vector
  928. * @method module:vec4.scaleAndAdd
  929. */
  930. vec4.scaleAndAdd = function(a, b, scale, out) {
  931. out[0] = a[0] + (b[0] * scale);
  932. out[1] = a[1] + (b[1] * scale);
  933. out[2] = a[2] + (b[2] * scale);
  934. out[3] = a[3] + (b[3] * scale);
  935. return out;
  936. };
  937. /**
  938. * Calculates the euclidian distance between two vec4's
  939. *
  940. * @param {Vec4} a the first operand
  941. * @param {Vec4} b the second operand
  942. * @returns {Number} distance between a and b
  943. * @method module:vec4.distance
  944. */
  945. vec4.distance = function(a, b) {
  946. var x = b[0] - a[0],
  947. y = b[1] - a[1],
  948. z = b[2] - a[2],
  949. w = b[3] - a[3];
  950. return Math.sqrt(x*x + y*y + z*z + w*w);
  951. };
  952. /**
  953. * Alias for {@link vec4.distance}
  954. * @function
  955. * @method module:vec4.dist
  956. */
  957. vec4.dist = vec4.distance;
  958. /**
  959. * Calculates the squared euclidian distance between two vec4's
  960. *
  961. * @param {Vec4} a the first operand
  962. * @param {Vec4} b the second operand
  963. * @returns {Number} squared distance between a and b
  964. * @method module:vec4.squaredDistance
  965. */
  966. vec4.squaredDistance = function(a, b) {
  967. var x = b[0] - a[0],
  968. y = b[1] - a[1],
  969. z = b[2] - a[2],
  970. w = b[3] - a[3];
  971. return x*x + y*y + z*z + w*w;
  972. };
  973. /**
  974. * Alias for {@link vec4.squaredDistance}
  975. * @function
  976. * @method module:vec4.sqrDist
  977. */
  978. vec4.sqrDist = vec4.squaredDistance;
  979. /**
  980. * Calculates the length of a vec4
  981. *
  982. * @param {Vec4} a vector to calculate length of
  983. * @returns {Number} length of a
  984. * @method module:vec4.length
  985. */
  986. vec4.length = function (a) {
  987. var x = a[0],
  988. y = a[1],
  989. z = a[2],
  990. w = a[3];
  991. return Math.sqrt(x*x + y*y + z*z + w*w);
  992. };
  993. /**
  994. * Alias for {@link vec4.length}
  995. * @function
  996. * @method module:vec4.len
  997. */
  998. vec4.len = vec4.length;
  999. /**
  1000. * Calculates the squared length of a vec4
  1001. *
  1002. * @param {Vec4} a vector to calculate squared length of
  1003. * @returns {Number} squared length of a
  1004. * @method module:vec4.squaredLength
  1005. */
  1006. vec4.squaredLength = function (a) {
  1007. var x = a[0],
  1008. y = a[1],
  1009. z = a[2],
  1010. w = a[3];
  1011. return x*x + y*y + z*z + w*w;
  1012. };
  1013. /**
  1014. * Alias for {@link vec4.squaredLength}
  1015. * @function
  1016. * @method module:vec4.sqrLen
  1017. */
  1018. vec4.sqrLen = vec4.squaredLength;
  1019. /**
  1020. * Negates the components of a vec4
  1021. *
  1022. * @param {Vec4} a vector to negate
  1023. * @returns {Vec4} out
  1024. * @param {Vec4} out the receiving vector
  1025. * @method module:vec4.negate
  1026. */
  1027. vec4.negate = function(a, out) {
  1028. out[0] = -a[0];
  1029. out[1] = -a[1];
  1030. out[2] = -a[2];
  1031. out[3] = -a[3];
  1032. return out;
  1033. };
  1034. /**
  1035. * Returns the inverse of the components of a vec4
  1036. *
  1037. * @param {Vec4} a vector to invert
  1038. * @returns {Vec4} out
  1039. * @param {Vec4} out the receiving vector
  1040. * @method module:vec4.inverse
  1041. */
  1042. vec4.inverse = function(a, out) {
  1043. out[0] = 1.0 / a[0];
  1044. out[1] = 1.0 / a[1];
  1045. out[2] = 1.0 / a[2];
  1046. out[3] = 1.0 / a[3];
  1047. return out;
  1048. };
  1049. /**
  1050. * Normalize a vec4
  1051. *
  1052. * @param {Vec4} a vector to normalize
  1053. * @returns {Vec4} out
  1054. * @param {Vec4} out the receiving vector
  1055. * @method module:vec4.normalize
  1056. */
  1057. vec4.normalize = function(a, out) {
  1058. var x = a[0],
  1059. y = a[1],
  1060. z = a[2],
  1061. w = a[3];
  1062. var len = x*x + y*y + z*z + w*w;
  1063. if (len > 0) {
  1064. len = 1 / Math.sqrt(len);
  1065. out[0] = x * len;
  1066. out[1] = y * len;
  1067. out[2] = z * len;
  1068. out[3] = w * len;
  1069. }
  1070. return out;
  1071. };
  1072. /**
  1073. * Calculates the dot product of two vec4's
  1074. *
  1075. * @param {Vec4} a the first operand
  1076. * @param {Vec4} b the second operand
  1077. * @returns {Number} dot product of a and b
  1078. * @method module:vec4.dot
  1079. */
  1080. vec4.dot = function (a, b) {
  1081. return a[0] * b[0] + a[1] * b[1] + a[2] * b[2] + a[3] * b[3];
  1082. };
  1083. /**
  1084. * Performs a linear interpolation between two vec4's
  1085. *
  1086. * @param {Vec4} a the first operand
  1087. * @param {Vec4} b the second operand
  1088. * @param {Number} t interpolation amount between the two inputs
  1089. * @returns {Vec4} out
  1090. * @param {Vec4} out the receiving vector
  1091. * @method module:vec4.lerp
  1092. */
  1093. vec4.lerp = function (a, b, t, out) {
  1094. var ax = a[0],
  1095. ay = a[1],
  1096. az = a[2],
  1097. aw = a[3];
  1098. out[0] = ax + t * (b[0] - ax);
  1099. out[1] = ay + t * (b[1] - ay);
  1100. out[2] = az + t * (b[2] - az);
  1101. out[3] = aw + t * (b[3] - aw);
  1102. return out;
  1103. };
  1104. /**
  1105. * Generates a random vector with the given scale
  1106. *
  1107. * @param {Number} [scale] Length of the resulting vector. If ommitted, a unit vector will be returned
  1108. * @returns {Vec4} out
  1109. * @param {Vec4} out the receiving vector
  1110. * @method module:vec4.random
  1111. */
  1112. vec4.random = function (scale, out) {
  1113. scale = scale || 1.0;
  1114. //TODO: This is a pretty awful way of doing this. Find something better.
  1115. out[0] = GLMAT_RANDOM();
  1116. out[1] = GLMAT_RANDOM();
  1117. out[2] = GLMAT_RANDOM();
  1118. out[3] = GLMAT_RANDOM();
  1119. vec4.normalize(out, out);
  1120. vec4.scale(out, scale, out);
  1121. return out;
  1122. };
  1123. /**
  1124. * Transforms the vec4 with a mat4.
  1125. *
  1126. * @param {Vec4} a the vector to transform
  1127. * @param {Mat4} m matrix to transform with
  1128. * @returns {Vec4} out
  1129. * @param {Vec4} out the receiving vector
  1130. * @method module:vec4.transformMat4
  1131. */
  1132. vec4.transformMat4 = function(a, m, out) {
  1133. var x = a[0], y = a[1], z = a[2], w = a[3];
  1134. out[0] = m[0] * x + m[4] * y + m[8] * z + m[12] * w;
  1135. out[1] = m[1] * x + m[5] * y + m[9] * z + m[13] * w;
  1136. out[2] = m[2] * x + m[6] * y + m[10] * z + m[14] * w;
  1137. out[3] = m[3] * x + m[7] * y + m[11] * z + m[15] * w;
  1138. return out;
  1139. };
  1140. /**
  1141. * Transforms the vec4 with a quat
  1142. *
  1143. * @param {Vec4} a the vector to transform
  1144. * @param {Quat} q quaternion to transform with
  1145. * @returns {Vec4} out
  1146. * @param {Vec4} out the receiving vector
  1147. * @method module:vec4.transformQuat
  1148. */
  1149. vec4.transformQuat = function(a, q, out) {
  1150. var x = a[0], y = a[1], z = a[2],
  1151. qx = q[0], qy = q[1], qz = q[2], qw = q[3],
  1152. // calculate quat * vec
  1153. ix = qw * x + qy * z - qz * y,
  1154. iy = qw * y + qz * x - qx * z,
  1155. iz = qw * z + qx * y - qy * x,
  1156. iw = -qx * x - qy * y - qz * z;
  1157. // calculate result * inverse quat
  1158. out[0] = ix * qw + iw * -qx + iy * -qz - iz * -qy;
  1159. out[1] = iy * qw + iw * -qy + iz * -qx - ix * -qz;
  1160. out[2] = iz * qw + iw * -qz + ix * -qy - iy * -qx;
  1161. out[3] = a[3];
  1162. return out;
  1163. };
  1164. /**
  1165. * Perform some operation over an array of vec4s.
  1166. *
  1167. * @param {Array} a the array of vectors to iterate over
  1168. * @param {Number} stride Number of elements between the start of each vec4. If 0 assumes tightly packed
  1169. * @param {Number} offset Number of elements to skip at the beginning of the array
  1170. * @param {Number} count Number of vec4s to iterate over. If 0 iterates over entire array
  1171. * @param {Function} fn Function to call for each vector in the array
  1172. * @param {Object} [arg] additional argument to pass to fn
  1173. * @returns {Array} a
  1174. * @function
  1175. * @method module:vec4.forEach
  1176. */
  1177. vec4.forEach = (function() {
  1178. var vec = vec4.create();
  1179. return function(a, stride, offset, count, fn, arg) {
  1180. var i, l;
  1181. if(!stride) {
  1182. stride = 4;
  1183. }
  1184. if(!offset) {
  1185. offset = 0;
  1186. }
  1187. if(count) {
  1188. l = Math.min((count * stride) + offset, a.length);
  1189. } else {
  1190. l = a.length;
  1191. }
  1192. for(i = offset; i < l; i += stride) {
  1193. vec[0] = a[i]; vec[1] = a[i+1]; vec[2] = a[i+2]; vec[3] = a[i+3];
  1194. fn(vec, arg, vec);
  1195. a[i] = vec[0]; a[i+1] = vec[1]; a[i+2] = vec[2]; a[i+3] = vec[3];
  1196. }
  1197. return a;
  1198. };
  1199. })();
  1200. /**
  1201. * Returns a string representation of a vector
  1202. *
  1203. * @param {Vec4} vec vector to represent as a string
  1204. * @returns {String} string representation of the vector
  1205. * @method module:vec4.str
  1206. */
  1207. vec4.str = function (a) {
  1208. return 'vec4(' + a[0] + ', ' + a[1] + ', ' + a[2] + ', ' + a[3] + ')';
  1209. };
  1210. }
  1211. b4w.module["vec4"] = b4w.module["__vec4"];
  1212. b4w.module["__quat"] = function(exports, require) {
  1213. var vec3 = require("__vec3");
  1214. var vec4 = require("__vec4");
  1215. var mat3 = require("__mat3");
  1216. var GLMAT_EPSILON = 0.0000001;
  1217. var GLMAT_ARRAY_TYPE = (typeof Float32Array !== 'undefined') ? Float32Array : Array;
  1218. var GLMAT_RANDOM = Math.random;
  1219. /* Copyright (c) 2015, Brandon Jones, Colin MacKenzie IV.
  1220. Permission is hereby granted, free of charge, to any person obtaining a copy
  1221. of this software and associated documentation files (the "Software"), to deal
  1222. in the Software without restriction, including without limitation the rights
  1223. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  1224. copies of the Software, and to permit persons to whom the Software is
  1225. furnished to do so, subject to the following conditions:
  1226. The above copyright notice and this permission notice shall be included in
  1227. all copies or substantial portions of the Software.
  1228. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  1229. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  1230. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  1231. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  1232. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  1233. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  1234. THE SOFTWARE. */
  1235. /**
  1236. * @module Quaternion
  1237. * @name quat
  1238. */
  1239. var quat = exports;
  1240. /**
  1241. * Creates a new identity quat
  1242. *
  1243. * @returns {Quat} a new quaternion
  1244. * @method module:quat.create
  1245. */
  1246. quat.create = function() {
  1247. var out = new GLMAT_ARRAY_TYPE(4);
  1248. out[0] = 0;
  1249. out[1] = 0;
  1250. out[2] = 0;
  1251. out[3] = 1;
  1252. return out;
  1253. };
  1254. /**
  1255. * Sets a quaternion to represent the shortest rotation from one
  1256. * vector to another.
  1257. *
  1258. * Both vectors are assumed to be unit length.
  1259. *
  1260. * @param {Vec3} a the initial vector
  1261. * @param {Vec3} b the destination vector
  1262. * @returns {Quat} out
  1263. * @param {Quat} out the receiving quaternion.
  1264. * @method module:quat.rotationTo
  1265. */
  1266. quat.rotationTo = (function() {
  1267. var tmpvec3 = vec3.create();
  1268. var xUnitVec3 = vec3.fromValues(1,0,0);
  1269. var yUnitVec3 = vec3.fromValues(0,1,0);
  1270. return function(a, b, out) {
  1271. var dot = vec3.dot(a, b);
  1272. if (dot < -0.9999999) {
  1273. vec3.cross(xUnitVec3, a, tmpvec3); /* NOTE: CUSTOM REORDER: (tmpvec3, xUnitVec3, a)->(xUnitVec3, a ,tmpvec3) */
  1274. if (vec3.length(tmpvec3) < 0.000001)
  1275. vec3.cross(yUnitVec3, a, tmpvec3); /* NOTE: CUSTOM REORDER: (tmpvec3, yUnitVec3, a)->(yUnitVec3, a ,tmpvec3) */
  1276. vec3.normalize(tmpvec3, tmpvec3);
  1277. quat.setAxisAngle(tmpvec3, Math.PI, out); /* NOTE: CUSTOM REORDER: (out, tmpvec3, Math.PI)->(tmpvec3, Math.PI ,out)*/
  1278. return out;
  1279. } else if (dot > 0.9999999) {
  1280. out[0] = 0;
  1281. out[1] = 0;
  1282. out[2] = 0;
  1283. out[3] = 1;
  1284. return out;
  1285. } else {
  1286. vec3.cross(a, b, tmpvec3); /* NOTE: CUSTOM REORDER: (tmpvec3, a, b)->(a, b ,tmpvec3) */
  1287. out[0] = tmpvec3[0];
  1288. out[1] = tmpvec3[1];
  1289. out[2] = tmpvec3[2];
  1290. out[3] = 1 + dot;
  1291. return quat.normalize(out, out);
  1292. }
  1293. };
  1294. })();
  1295. /**
  1296. * Sets the specified quaternion with values corresponding to the given
  1297. * axes. Each axis is a vec3 and is expected to be unit length and
  1298. * perpendicular to all other specified axes.
  1299. *
  1300. * @param {Vec3} view the vector representing the viewing direction
  1301. * @param {Vec3} right the vector representing the local "right" direction
  1302. * @param {Vec3} up the vector representing the local "up" direction
  1303. * @returns {Quat} out
  1304. * @method module:quat.setAxes
  1305. */
  1306. quat.setAxes = (function() {
  1307. var matr = mat3.create();
  1308. return function(view, right, up, out) {
  1309. matr[0] = right[0];
  1310. matr[3] = right[1];
  1311. matr[6] = right[2];
  1312. matr[1] = up[0];
  1313. matr[4] = up[1];
  1314. matr[7] = up[2];
  1315. matr[2] = -view[0];
  1316. matr[5] = -view[1];
  1317. matr[8] = -view[2];
  1318. return quat.normalize(quat.fromMat3(matr, out), out); /* NOTE: DOUBLE CUSTOM REORDER */
  1319. };
  1320. })();
  1321. /**
  1322. * Creates a new quat initialized with values from an existing quaternion
  1323. *
  1324. * @param {Quat} a quaternion to clone
  1325. * @returns {Quat} a new quaternion
  1326. * @function
  1327. * @method module:quat.clone
  1328. */
  1329. quat.clone = vec4.clone;
  1330. /**
  1331. * Creates a new quat initialized with the given values
  1332. *
  1333. * @param {Number} x X component
  1334. * @param {Number} y Y component
  1335. * @param {Number} z Z component
  1336. * @param {Number} w W component
  1337. * @returns {Quat} a new quaternion
  1338. * @function
  1339. * @method module:quat.fromValues
  1340. */
  1341. quat.fromValues = vec4.fromValues;
  1342. /**
  1343. * Copy the values from one quat to another
  1344. *
  1345. * @param {Quat} a the source quaternion
  1346. * @returns {Quat} out
  1347. * @function
  1348. * @param {Quat} out the receiving quaternion
  1349. * @method module:quat.copy
  1350. */
  1351. quat.copy = vec4.copy;
  1352. /**
  1353. * Set the components of a quat to the given values
  1354. *
  1355. * @param {Number} x X component
  1356. * @param {Number} y Y component
  1357. * @param {Number} z Z component
  1358. * @param {Number} w W component
  1359. * @returns {Quat} out
  1360. * @function
  1361. * @param {Quat} out the receiving quaternion
  1362. * @method module:quat.set
  1363. */
  1364. quat.set = vec4.set;
  1365. /**
  1366. * Set a quat to the identity quaternion
  1367. *
  1368. * @returns {Quat} out
  1369. * @param {Quat} out the receiving quaternion
  1370. * @method module:quat.identity
  1371. */
  1372. quat.identity = function(out) {
  1373. out[0] = 0;
  1374. out[1] = 0;
  1375. out[2] = 0;
  1376. out[3] = 1;
  1377. return out;
  1378. };
  1379. /**
  1380. * Sets a quat from the given angle and rotation axis,
  1381. * then returns it.
  1382. *
  1383. * @param {Vec3} axis the axis around which to rotate
  1384. * @param {Number} rad the angle in radians
  1385. * @returns {Quat} out
  1386. * @param {Quat} out the receiving quaternion
  1387. * @method module:quat.setAxisAngle
  1388. */
  1389. quat.setAxisAngle = function(axis, rad, out) {
  1390. rad = rad * 0.5;
  1391. var s = Math.sin(rad);
  1392. out[0] = s * axis[0];
  1393. out[1] = s * axis[1];
  1394. out[2] = s * axis[2];
  1395. out[3] = Math.cos(rad);
  1396. return out;
  1397. };
  1398. /**
  1399. * Adds two quat's
  1400. *
  1401. * @param {Quat} a the first operand
  1402. * @param {Quat} b the second operand
  1403. * @returns {Quat} out
  1404. * @function
  1405. * @param {Quat} out the receiving quaternion
  1406. * @method module:quat.add
  1407. */
  1408. quat.add = vec4.add;
  1409. /**
  1410. * Multiplies two quat's
  1411. *
  1412. * @param {Quat} a the first operand
  1413. * @param {Quat} b the second operand
  1414. * @returns {Quat} out
  1415. * @param {Quat} out the receiving quaternion
  1416. * @method module:quat.multiply
  1417. */
  1418. quat.multiply = function(a, b, out) {
  1419. var ax = a[0], ay = a[1], az = a[2], aw = a[3],
  1420. bx = b[0], by = b[1], bz = b[2], bw = b[3];
  1421. out[0] = ax * bw + aw * bx + ay * bz - az * by;
  1422. out[1] = ay * bw + aw * by + az * bx - ax * bz;
  1423. out[2] = az * bw + aw * bz + ax * by - ay * bx;
  1424. out[3] = aw * bw - ax * bx - ay * by - az * bz;
  1425. return out;
  1426. };
  1427. /**
  1428. * Alias for {@link quat.multiply}
  1429. * @function
  1430. * @method module:quat.mul
  1431. */
  1432. quat.mul = quat.multiply;
  1433. /**
  1434. * Scales a quat by a scalar number
  1435. *
  1436. * @param {Quat} a the vector to scale
  1437. * @param {Number} b amount to scale the vector by
  1438. * @returns {Quat} out
  1439. * @function
  1440. * @param {Quat} out the receiving vector
  1441. * @method module:quat.scale
  1442. */
  1443. quat.scale = vec4.scale;
  1444. /**
  1445. * Rotates a quaternion by the given angle about the X axis
  1446. *
  1447. * @param {Quat} a quat to rotate
  1448. * @param {number} rad angle (in radians) to rotate
  1449. * @returns {Quat} out
  1450. * @param {Quat} out quat receiving operation result
  1451. * @method module:quat.rotateX
  1452. */
  1453. quat.rotateX = function (a, rad, out) {
  1454. rad *= 0.5;
  1455. var ax = a[0], ay = a[1], az = a[2], aw = a[3],
  1456. bx = Math.sin(rad), bw = Math.cos(rad);
  1457. out[0] = ax * bw + aw * bx;
  1458. out[1] = ay * bw + az * bx;
  1459. out[2] = az * bw - ay * bx;
  1460. out[3] = aw * bw - ax * bx;
  1461. return out;
  1462. };
  1463. /**
  1464. * Rotates a quaternion by the given angle about the Y axis
  1465. *
  1466. * @param {Quat} a quat to rotate
  1467. * @param {number} rad angle (in radians) to rotate
  1468. * @returns {Quat} out
  1469. * @param {Quat} out quat receiving operation result
  1470. * @method module:quat.rotateY
  1471. */
  1472. quat.rotateY = function (a, rad, out) {
  1473. rad *= 0.5;
  1474. var ax = a[0], ay = a[1], az = a[2], aw = a[3],
  1475. by = Math.sin(rad), bw = Math.cos(rad);
  1476. out[0] = ax * bw - az * by;
  1477. out[1] = ay * bw + aw * by;
  1478. out[2] = az * bw + ax * by;
  1479. out[3] = aw * bw - ay * by;
  1480. return out;
  1481. };
  1482. /**
  1483. * Rotates a quaternion by the given angle about the Z axis
  1484. *
  1485. * @param {Quat} a quat to rotate
  1486. * @param {number} rad angle (in radians) to rotate
  1487. * @returns {Quat} out
  1488. * @param {Quat} out quat receiving operation result
  1489. * @method module:quat.rotateZ
  1490. */
  1491. quat.rotateZ = function (a, rad, out) {
  1492. rad *= 0.5;
  1493. var ax = a[0], ay = a[1], az = a[2], aw = a[3],
  1494. bz = Math.sin(rad), bw = Math.cos(rad);
  1495. out[0] = ax * bw + ay * bz;
  1496. out[1] = ay * bw - ax * bz;
  1497. out[2] = az * bw + aw * bz;
  1498. out[3] = aw * bw - az * bz;
  1499. return out;
  1500. };
  1501. /**
  1502. * Calculates the W component of a quat from the X, Y, and Z components.
  1503. * Assumes that quaternion is 1 unit in length.
  1504. * Any existing W component will be ignored.
  1505. *
  1506. * @param {Quat} a quat to calculate W component of
  1507. * @returns {Quat} out
  1508. * @param {Quat} out the receiving quaternion
  1509. * @method module:quat.calculateW
  1510. */
  1511. quat.calculateW = function (a, out) {
  1512. var x = a[0], y = a[1], z = a[2];
  1513. out[0] = x;
  1514. out[1] = y;
  1515. out[2] = z;
  1516. out[3] = Math.sqrt(Math.abs(1.0 - x * x - y * y - z * z));
  1517. return out;
  1518. };
  1519. /**
  1520. * Calculates the dot product of two quat's
  1521. *
  1522. * @param {Quat} a the first operand
  1523. * @param {Quat} b the second operand
  1524. * @returns {Number} dot product of a and b
  1525. * @function
  1526. * @method module:quat.dot
  1527. */
  1528. quat.dot = vec4.dot;
  1529. /**
  1530. * Performs a linear interpolation between two quat's
  1531. *
  1532. * @param {Quat} a the first operand
  1533. * @param {Quat} b the second operand
  1534. * @param {Number} t interpolation amount between the two inputs
  1535. * @returns {Quat} out
  1536. * @function
  1537. * @param {Quat} out the receiving quaternion
  1538. * @method module:quat.lerp
  1539. */
  1540. quat.lerp = vec4.lerp;
  1541. /**
  1542. * Performs a spherical linear interpolation between two quat
  1543. *
  1544. * @param {Quat} a the first operand
  1545. * @param {Quat} b the second operand
  1546. * @param {Number} t interpolation amount between the two inputs
  1547. * @returns {Quat} out
  1548. * @param {Quat} out the receiving quaternion
  1549. * @method module:quat.slerp
  1550. */
  1551. quat.slerp = function (a, b, t, out) {
  1552. // benchmarks:
  1553. // http://jsperf.com/quaternion-slerp-implementations
  1554. var ax = a[0], ay = a[1], az = a[2], aw = a[3],
  1555. bx = b[0], by = b[1], bz = b[2], bw = b[3];
  1556. var omega, cosom, sinom, scale0, scale1;
  1557. // calc cosine
  1558. cosom = ax * bx + ay * by + az * bz + aw * bw;
  1559. // adjust signs (if necessary)
  1560. if ( cosom < 0.0 ) {
  1561. cosom = -cosom;
  1562. bx = - bx;
  1563. by = - by;
  1564. bz = - bz;
  1565. bw = - bw;
  1566. }
  1567. // calculate coefficients
  1568. if ( (1.0 - cosom) > 0.000001 ) {
  1569. // standard case (slerp)
  1570. omega = Math.acos(cosom);
  1571. sinom = Math.sin(omega);
  1572. scale0 = Math.sin((1.0 - t) * omega) / sinom;
  1573. scale1 = Math.sin(t * omega) / sinom;
  1574. } else {
  1575. // "from" and "to" quaternions are very close
  1576. // ... so we can do a linear interpolation
  1577. scale0 = 1.0 - t;
  1578. scale1 = t;
  1579. }
  1580. // calculate final values
  1581. out[0] = scale0 * ax + scale1 * bx;
  1582. out[1] = scale0 * ay + scale1 * by;
  1583. out[2] = scale0 * az + scale1 * bz;
  1584. out[3] = scale0 * aw + scale1 * bw;
  1585. return out;
  1586. };
  1587. /**
  1588. * Performs a spherical linear interpolation with two control points
  1589. *
  1590. * @param {Quat} a the first operand
  1591. * @param {Quat} b the second operand
  1592. * @param {Quat} c the third operand
  1593. * @param {Quat} d the fourth operand
  1594. * @param {Number} t interpolation amount
  1595. * @returns {Quat} out
  1596. * @param {Quat} out the receiving quaternion
  1597. * @method module:quat.sqlerp
  1598. */
  1599. quat.sqlerp = (function () {
  1600. var temp1 = quat.create();
  1601. var temp2 = quat.create();
  1602. return function (a, b, c, d, t, out) {
  1603. quat.slerp(a, d, t, temp1); /* NOTE: CUSTOM REORDER: */
  1604. quat.slerp(b, c, t, temp2); /* NOTE: CUSTOM REORDER: */
  1605. quat.slerp(temp1, temp2, 2 * t * (1 - t), out); /* NOTE: CUSTOM REORDER:*/
  1606. return out;
  1607. };
  1608. }());
  1609. /**
  1610. * Calculates the inverse of a quat
  1611. *
  1612. * @param {Quat} a quat to calculate inverse of
  1613. * @returns {Quat} out
  1614. * @param {Quat} out the receiving quaternion
  1615. * @method module:quat.invert
  1616. */
  1617. quat.invert = function(a, out) {
  1618. var a0 = a[0], a1 = a[1], a2 = a[2], a3 = a[3],
  1619. dot = a0*a0 + a1*a1 + a2*a2 + a3*a3,
  1620. invDot = dot ? 1.0/dot : 0;
  1621. // TODO: Would be faster to return [0,0,0,0] immediately if dot == 0
  1622. out[0] = -a0*invDot;
  1623. out[1] = -a1*invDot;
  1624. out[2] = -a2*invDot;
  1625. out[3] = a3*invDot;
  1626. return out;
  1627. };
  1628. /**
  1629. * Calculates the conjugate of a quat
  1630. * If the quaternion is normalized, this function is faster than quat.inverse and produces the same result.
  1631. *
  1632. * @param {Quat} a quat to calculate conjugate of
  1633. * @returns {Quat} out
  1634. * @param {Quat} out the receiving quaternion
  1635. * @method module:quat.conjugate
  1636. */
  1637. quat.conjugate = function (a, out) {
  1638. out[0] = -a[0];
  1639. out[1] = -a[1];
  1640. out[2] = -a[2];
  1641. out[3] = a[3];
  1642. return out;
  1643. };
  1644. /**
  1645. * Calculates the length of a quat
  1646. *
  1647. * @param {Quat} a vector to calculate length of
  1648. * @returns {Number} length of a
  1649. * @function
  1650. * @method module:quat.length
  1651. */
  1652. quat.length = vec4.length;
  1653. /**
  1654. * Alias for {@link quat.length}
  1655. * @function
  1656. * @method module:quat.len
  1657. */
  1658. quat.len = quat.length;
  1659. /**
  1660. * Calculates the squared length of a quat
  1661. *
  1662. * @param {Quat} a vector to calculate squared length of
  1663. * @returns {Number} squared length of a
  1664. * @function
  1665. * @method module:quat.squaredLength
  1666. */
  1667. quat.squaredLength = vec4.squaredLength;
  1668. /**
  1669. * Alias for {@link quat.squaredLength}
  1670. * @function
  1671. * @method module:quat.sqrLen
  1672. */
  1673. quat.sqrLen = quat.squaredLength;
  1674. /**
  1675. * Normalize a quat
  1676. *
  1677. * @param {Quat} a quaternion to normalize
  1678. * @returns {Quat} out
  1679. * @function
  1680. * @param {Quat} out the receiving quaternion
  1681. * @method module:quat.normalize
  1682. */
  1683. quat.normalize = vec4.normalize;
  1684. /**
  1685. * Creates a quaternion from the given 3x3 rotation matrix.
  1686. *
  1687. * NOTE: The resultant quaternion is not normalized, so you should be sure
  1688. * to renormalize the quaternion yourself where necessary.
  1689. *
  1690. * @param {Mat3} m rotation matrix
  1691. * @returns {Quat} out
  1692. * @function
  1693. * @param {Quat} out the receiving quaternion
  1694. * @method module:quat.fromMat3
  1695. */
  1696. quat.fromMat3 = function(m, out) {
  1697. // Algorithm in Ken Shoemake's article in 1987 SIGGRAPH course notes
  1698. // article "Quaternion Calculus and Fast Animation".
  1699. var fTrace = m[0] + m[4] + m[8];
  1700. var fRoot;
  1701. if ( fTrace > 0.0 ) {
  1702. // |w| > 1/2, may as well choose w > 1/2
  1703. fRoot = Math.sqrt(fTrace + 1.0); // 2w
  1704. out[3] = 0.5 * fRoot;
  1705. fRoot = 0.5/fRoot; // 1/(4w)
  1706. out[0] = (m[5]-m[7])*fRoot;
  1707. out[1] = (m[6]-m[2])*fRoot;
  1708. out[2] = (m[1]-m[3])*fRoot;
  1709. } else {
  1710. // |w| <= 1/2
  1711. var i = 0;
  1712. if ( m[4] > m[0] )
  1713. i = 1;
  1714. if ( m[8] > m[i*3+i] )
  1715. i = 2;
  1716. var j = (i+1)%3;
  1717. var k = (i+2)%3;
  1718. fRoot = Math.sqrt(m[i*3+i]-m[j*3+j]-m[k*3+k] + 1.0);
  1719. out[i] = 0.5 * fRoot;
  1720. fRoot = 0.5 / fRoot;
  1721. out[3] = (m[j*3+k] - m[k*3+j]) * fRoot;
  1722. out[j] = (m[j*3+i] + m[i*3+j]) * fRoot;
  1723. out[k] = (m[k*3+i] + m[i*3+k]) * fRoot;
  1724. }
  1725. return out;
  1726. };
  1727. /**
  1728. * Returns a string representation of a quatenion
  1729. *
  1730. * @param {Quat} vec vector to represent as a string
  1731. * @returns {String} string representation of the vector
  1732. * @method module:quat.str
  1733. */
  1734. quat.str = function (a) {
  1735. return 'quat(' + a[0] + ', ' + a[1] + ', ' + a[2] + ', ' + a[3] + ')';
  1736. };
  1737. }
  1738. b4w.module["quat"] = b4w.module["__quat"];
  1739. b4w.module["__mat3"] = function(exports, require) {
  1740. var GLMAT_EPSILON = 0.0000001;
  1741. var GLMAT_ARRAY_TYPE = (typeof Float32Array !== 'undefined') ? Float32Array : Array;
  1742. var GLMAT_RANDOM = Math.random;
  1743. /* Copyright (c) 2015, Brandon Jones, Colin MacKenzie IV.
  1744. Permission is hereby granted, free of charge, to any person obtaining a copy
  1745. of this software and associated documentation files (the "Software"), to deal
  1746. in the Software without restriction, including without limitation the rights
  1747. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  1748. copies of the Software, and to permit persons to whom the Software is
  1749. furnished to do so, subject to the following conditions:
  1750. The above copyright notice and this permission notice shall be included in
  1751. all copies or substantial portions of the Software.
  1752. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  1753. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  1754. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  1755. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  1756. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  1757. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  1758. THE SOFTWARE. */
  1759. /**
  1760. * @module 3x3 Matrix
  1761. * @name mat3
  1762. */
  1763. var mat3 = exports;
  1764. /**
  1765. * Creates a new identity mat3
  1766. *
  1767. * @returns {Mat3} a new 3x3 matrix
  1768. * @method module:mat3.create
  1769. */
  1770. mat3.create = function() {
  1771. var out = new GLMAT_ARRAY_TYPE(9);
  1772. out[0] = 1;
  1773. out[1] = 0;
  1774. out[2] = 0;
  1775. out[3] = 0;
  1776. out[4] = 1;
  1777. out[5] = 0;
  1778. out[6] = 0;
  1779. out[7] = 0;
  1780. out[8] = 1;
  1781. return out;
  1782. };
  1783. /**
  1784. * Copies the upper-left 3x3 values into the given mat3.
  1785. *
  1786. * @param {Mat4} a the source 4x4 matrix
  1787. * @returns {Mat3} out
  1788. * @param {Mat3} out the receiving 3x3 matrix
  1789. * @method module:mat3.fromMat4
  1790. */
  1791. mat3.fromMat4 = function(a, out) {
  1792. out[0] = a[0];
  1793. out[1] = a[1];
  1794. out[2] = a[2];
  1795. out[3] = a[4];
  1796. out[4] = a[5];
  1797. out[5] = a[6];
  1798. out[6] = a[8];
  1799. out[7] = a[9];
  1800. out[8] = a[10];
  1801. return out;
  1802. };
  1803. /**
  1804. * Creates a new mat3 initialized with values from an existing matrix
  1805. *
  1806. * @param {Mat3} a matrix to clone
  1807. * @returns {Mat3} a new 3x3 matrix
  1808. * @method module:mat3.clone
  1809. */
  1810. mat3.clone = function(a) {
  1811. var out = new GLMAT_ARRAY_TYPE(9);
  1812. out[0] = a[0];
  1813. out[1] = a[1];
  1814. out[2] = a[2];
  1815. out[3] = a[3];
  1816. out[4] = a[4];
  1817. out[5] = a[5];
  1818. out[6] = a[6];
  1819. out[7] = a[7];
  1820. out[8] = a[8];
  1821. return out;
  1822. };
  1823. /**
  1824. * Copy the values from one mat3 to another
  1825. *
  1826. * @param {Mat3} a the source matrix
  1827. * @returns {Mat3} out
  1828. * @param {Mat3} out the receiving matrix
  1829. * @method module:mat3.copy
  1830. */
  1831. mat3.copy = function(a, out) {
  1832. out[0] = a[0];
  1833. out[1] = a[1];
  1834. out[2] = a[2];
  1835. out[3] = a[3];
  1836. out[4] = a[4];
  1837. out[5] = a[5];
  1838. out[6] = a[6];
  1839. out[7] = a[7];
  1840. out[8] = a[8];
  1841. return out;
  1842. };
  1843. /**
  1844. * Set a mat3 to the identity matrix
  1845. *
  1846. * @returns {Mat3} out
  1847. * @param {Mat3} out the receiving matrix
  1848. * @method module:mat3.identity
  1849. */
  1850. mat3.identity = function(out) {
  1851. out[0] = 1;
  1852. out[1] = 0;
  1853. out[2] = 0;
  1854. out[3] = 0;
  1855. out[4] = 1;
  1856. out[5] = 0;
  1857. out[6] = 0;
  1858. out[7] = 0;
  1859. out[8] = 1;
  1860. return out;
  1861. };
  1862. /**
  1863. * Transpose the values of a mat3
  1864. *
  1865. * @param {Mat3} a the source matrix
  1866. * @returns {Mat3} out
  1867. * @param {Mat3} out the receiving matrix
  1868. * @method module:mat3.transpose
  1869. */
  1870. mat3.transpose = function(a, out) {
  1871. // If we are transposing ourselves we can skip a few steps but have to cache some values
  1872. if (out === a) {
  1873. var a01 = a[1], a02 = a[2], a12 = a[5];
  1874. out[1] = a[3];
  1875. out[2] = a[6];
  1876. out[3] = a01;
  1877. out[5] = a[7];
  1878. out[6] = a02;
  1879. out[7] = a12;
  1880. } else {
  1881. out[0] = a[0];
  1882. out[1] = a[3];
  1883. out[2] = a[6];
  1884. out[3] = a[1];
  1885. out[4] = a[4];
  1886. out[5] = a[7];
  1887. out[6] = a[2];
  1888. out[7] = a[5];
  1889. out[8] = a[8];
  1890. }
  1891. return out;
  1892. };
  1893. /**
  1894. * Inverts a mat3
  1895. *
  1896. * @param {Mat3} a the source matrix
  1897. * @returns {Mat3} out
  1898. * @param {Mat3} out the receiving matrix
  1899. * @method module:mat3.invert
  1900. */
  1901. mat3.invert = function(a, out) {
  1902. var a00 = a[0], a01 = a[1], a02 = a[2],
  1903. a10 = a[3], a11 = a[4], a12 = a[5],
  1904. a20 = a[6], a21 = a[7], a22 = a[8],
  1905. b01 = a22 * a11 - a12 * a21,
  1906. b11 = -a22 * a10 + a12 * a20,
  1907. b21 = a21 * a10 - a11 * a20,
  1908. // Calculate the determinant
  1909. det = a00 * b01 + a01 * b11 + a02 * b21;
  1910. if (!det) {
  1911. return null;
  1912. }
  1913. det = 1.0 / det;
  1914. out[0] = b01 * det;
  1915. out[1] = (-a22 * a01 + a02 * a21) * det;
  1916. out[2] = (a12 * a01 - a02 * a11) * det;
  1917. out[3] = b11 * det;
  1918. out[4] = (a22 * a00 - a02 * a20) * det;
  1919. out[5] = (-a12 * a00 + a02 * a10) * det;
  1920. out[6] = b21 * det;
  1921. out[7] = (-a21 * a00 + a01 * a20) * det;
  1922. out[8] = (a11 * a00 - a01 * a10) * det;
  1923. return out;
  1924. };
  1925. /**
  1926. * Calculates the adjugate of a mat3
  1927. *
  1928. * @param {Mat3} a the source matrix
  1929. * @returns {Mat3} out
  1930. * @param {Mat3} out the receiving matrix
  1931. * @method module:mat3.adjoint
  1932. */
  1933. mat3.adjoint = function(a, out) {
  1934. var a00 = a[0], a01 = a[1], a02 = a[2],
  1935. a10 = a[3], a11 = a[4], a12 = a[5],
  1936. a20 = a[6], a21 = a[7], a22 = a[8];
  1937. out[0] = (a11 * a22 - a12 * a21);
  1938. out[1] = (a02 * a21 - a01 * a22);
  1939. out[2] = (a01 * a12 - a02 * a11);
  1940. out[3] = (a12 * a20 - a10 * a22);
  1941. out[4] = (a00 * a22 - a02 * a20);
  1942. out[5] = (a02 * a10 - a00 * a12);
  1943. out[6] = (a10 * a21 - a11 * a20);
  1944. out[7] = (a01 * a20 - a00 * a21);
  1945. out[8] = (a00 * a11 - a01 * a10);
  1946. return out;
  1947. };
  1948. /**
  1949. * Calculates the determinant of a mat3
  1950. *
  1951. * @param {Mat3} a the source matrix
  1952. * @returns {Number} determinant of a
  1953. * @method module:mat3.determinant
  1954. */
  1955. mat3.determinant = function (a) {
  1956. var a00 = a[0], a01 = a[1], a02 = a[2],
  1957. a10 = a[3], a11 = a[4], a12 = a[5],
  1958. a20 = a[6], a21 = a[7], a22 = a[8];
  1959. return a00 * (a22 * a11 - a12 * a21) + a01 * (-a22 * a10 + a12 * a20) + a02 * (a21 * a10 - a11 * a20);
  1960. };
  1961. /**
  1962. * Multiplies two mat3's
  1963. *
  1964. * @param {Mat3} a the first operand
  1965. * @param {Mat3} b the second operand
  1966. * @returns {Mat3} out
  1967. * @param {Mat3} out the receiving matrix
  1968. * @method module:mat3.multiply
  1969. */
  1970. mat3.multiply = function (a, b, out) {
  1971. var a00 = a[0], a01 = a[1], a02 = a[2],
  1972. a10 = a[3], a11 = a[4], a12 = a[5],
  1973. a20 = a[6], a21 = a[7], a22 = a[8],
  1974. b00 = b[0], b01 = b[1], b02 = b[2],
  1975. b10 = b[3], b11 = b[4], b12 = b[5],
  1976. b20 = b[6], b21 = b[7], b22 = b[8];
  1977. out[0] = b00 * a00 + b01 * a10 + b02 * a20;
  1978. out[1] = b00 * a01 + b01 * a11 + b02 * a21;
  1979. out[2] = b00 * a02 + b01 * a12 + b02 * a22;
  1980. out[3] = b10 * a00 + b11 * a10 + b12 * a20;
  1981. out[4] = b10 * a01 + b11 * a11 + b12 * a21;
  1982. out[5] = b10 * a02 + b11 * a12 + b12 * a22;
  1983. out[6] = b20 * a00 + b21 * a10 + b22 * a20;
  1984. out[7] = b20 * a01 + b21 * a11 + b22 * a21;
  1985. out[8] = b20 * a02 + b21 * a12 + b22 * a22;
  1986. return out;
  1987. };
  1988. /**
  1989. * Alias for {@link mat3.multiply}
  1990. * @function
  1991. * @method module:mat3.mul
  1992. */
  1993. mat3.mul = mat3.multiply;
  1994. /**
  1995. * Translate a mat3 by the given vector
  1996. *
  1997. * @param {Mat3} a the matrix to translate
  1998. * @param {vec2} v vector to translate by
  1999. * @returns {Mat3} out
  2000. * @param {Mat3} out the receiving matrix
  2001. * @method module:mat3.translate
  2002. */
  2003. mat3.translate = function(a, v, out) {
  2004. var a00 = a[0], a01 = a[1], a02 = a[2],
  2005. a10 = a[3], a11 = a[4], a12 = a[5],
  2006. a20 = a[6], a21 = a[7], a22 = a[8],
  2007. x = v[0], y = v[1];
  2008. out[0] = a00;
  2009. out[1] = a01;
  2010. out[2] = a02;
  2011. out[3] = a10;
  2012. out[4] = a11;
  2013. out[5] = a12;
  2014. out[6] = x * a00 + y * a10 + a20;
  2015. out[7] = x * a01 + y * a11 + a21;
  2016. out[8] = x * a02 + y * a12 + a22;
  2017. return out;
  2018. };
  2019. /**
  2020. * Rotates a mat3 by the given angle
  2021. *
  2022. * @param {Mat3} a the matrix to rotate
  2023. * @param {Number} rad the angle to rotate the matrix by
  2024. * @returns {Mat3} out
  2025. * @param {Mat3} out the receiving matrix
  2026. * @method module:mat3.rotate
  2027. */
  2028. mat3.rotate = function (a, rad, out) {
  2029. var a00 = a[0], a01 = a[1], a02 = a[2],
  2030. a10 = a[3], a11 = a[4], a12 = a[5],
  2031. a20 = a[6], a21 = a[7], a22 = a[8],
  2032. s = Math.sin(rad),
  2033. c = Math.cos(rad);
  2034. out[0] = c * a00 + s * a10;
  2035. out[1] = c * a01 + s * a11;
  2036. out[2] = c * a02 + s * a12;
  2037. out[3] = c * a10 - s * a00;
  2038. out[4] = c * a11 - s * a01;
  2039. out[5] = c * a12 - s * a02;
  2040. out[6] = a20;
  2041. out[7] = a21;
  2042. out[8] = a22;
  2043. return out;
  2044. };
  2045. /**
  2046. * Scales the mat3 by the dimensions in the given vec2
  2047. *
  2048. * @param {Mat3} a the matrix to rotate
  2049. * @param {vec2} v the vec2 to scale the matrix by
  2050. * @returns {Mat3} out
  2051. * @param {Mat3} out the receiving matrix
  2052. * @method module:mat3.scale
  2053. */
  2054. mat3.scale = function(a, v, out) {
  2055. var x = v[0], y = v[1];
  2056. out[0] = x * a[0];
  2057. out[1] = x * a[1];
  2058. out[2] = x * a[2];
  2059. out[3] = y * a[3];
  2060. out[4] = y * a[4];
  2061. out[5] = y * a[5];
  2062. out[6] = a[6];
  2063. out[7] = a[7];
  2064. out[8] = a[8];
  2065. return out;
  2066. };
  2067. /**
  2068. * Creates a matrix from a vector translation
  2069. * This is equivalent to (but much faster than):
  2070. *
  2071. * mat3.identity(dest);
  2072. * mat3.translate(dest, dest, vec);
  2073. *
  2074. * @param {vec2} v Translation vector
  2075. * @returns {Mat3} out
  2076. * @param {Mat3} out mat3 receiving operation result
  2077. * @method module:mat3.fromTranslation
  2078. */
  2079. mat3.fromTranslation = function(v, out) {
  2080. out[0] = 1;
  2081. out[1] = 0;
  2082. out[2] = 0;
  2083. out[3] = 0;
  2084. out[4] = 1;
  2085. out[5] = 0;
  2086. out[6] = v[0];
  2087. out[7] = v[1];
  2088. out[8] = 1;
  2089. return out;
  2090. }
  2091. /**
  2092. * Creates a matrix from a given angle
  2093. * This is equivalent to (but much faster than):
  2094. *
  2095. * mat3.identity(dest);
  2096. * mat3.rotate(dest, dest, rad);
  2097. *
  2098. * @param {Number} rad the angle to rotate the matrix by
  2099. * @returns {Mat3} out
  2100. * @param {Mat3} out mat3 receiving operation result
  2101. * @method module:mat3.fromRotation
  2102. */
  2103. mat3.fromRotation = function(rad, out) {
  2104. var s = Math.sin(rad), c = Math.cos(rad);
  2105. out[0] = c;
  2106. out[1] = s;
  2107. out[2] = 0;
  2108. out[3] = -s;
  2109. out[4] = c;
  2110. out[5] = 0;
  2111. out[6] = 0;
  2112. out[7] = 0;
  2113. out[8] = 1;
  2114. return out;
  2115. }
  2116. /**
  2117. * Creates a matrix from a vector scaling
  2118. * This is equivalent to (but much faster than):
  2119. *
  2120. * mat3.identity(dest);
  2121. * mat3.scale(dest, dest, vec);
  2122. *
  2123. * @param {vec2} v Scaling vector
  2124. * @returns {Mat3} out
  2125. * @param {Mat3} out mat3 receiving operation result
  2126. * @method module:mat3.fromScaling
  2127. */
  2128. mat3.fromScaling = function(v, out) {
  2129. out[0] = v[0];
  2130. out[1] = 0;
  2131. out[2] = 0;
  2132. out[3] = 0;
  2133. out[4] = v[1];
  2134. out[5] = 0;
  2135. out[6] = 0;
  2136. out[7] = 0;
  2137. out[8] = 1;
  2138. return out;
  2139. }
  2140. /**
  2141. * Copies the values from a mat2d into a mat3
  2142. *
  2143. * @param {mat2d} a the matrix to copy
  2144. * @returns {Mat3} out
  2145. * @param {Mat3} out the receiving matrix
  2146. * @method module:mat3.fromMat2d
  2147. */
  2148. mat3.fromMat2d = function(a, out) {
  2149. out[0] = a[0];
  2150. out[1] = a[1];
  2151. out[2] = 0;
  2152. out[3] = a[2];
  2153. out[4] = a[3];
  2154. out[5] = 0;
  2155. out[6] = a[4];
  2156. out[7] = a[5];
  2157. out[8] = 1;
  2158. return out;
  2159. };
  2160. /**
  2161. * Calculates a 3x3 matrix from the given quaternion
  2162. *
  2163. * @param {Quat} q Quaternion to create matrix from
  2164. *
  2165. * @returns {Mat3} out
  2166. * @param {Mat3} out mat3 receiving operation result
  2167. * @method module:mat3.fromQuat
  2168. */
  2169. mat3.fromQuat = function (q, out) {
  2170. var x = q[0], y = q[1], z = q[2], w = q[3],
  2171. x2 = x + x,
  2172. y2 = y + y,
  2173. z2 = z + z,
  2174. xx = x * x2,
  2175. yx = y * x2,
  2176. yy = y * y2,
  2177. zx = z * x2,
  2178. zy = z * y2,
  2179. zz = z * z2,
  2180. wx = w * x2,
  2181. wy = w * y2,
  2182. wz = w * z2;
  2183. out[0] = 1 - yy - zz;
  2184. out[3] = yx - wz;
  2185. out[6] = zx + wy;
  2186. out[1] = yx + wz;
  2187. out[4] = 1 - xx - zz;
  2188. out[7] = zy - wx;
  2189. out[2] = zx - wy;
  2190. out[5] = zy + wx;
  2191. out[8] = 1 - xx - yy;
  2192. return out;
  2193. };
  2194. /**
  2195. * Calculates a 3x3 normal matrix (transpose inverse) from the 4x4 matrix
  2196. *
  2197. * @param {Mat4} a Mat4 to derive the normal matrix from
  2198. *
  2199. * @returns {Mat3} out
  2200. * @param {Mat3} out mat3 receiving operation result
  2201. * @method module:mat3.normalFromMat4
  2202. */
  2203. mat3.normalFromMat4 = function (a, out) {
  2204. var a00 = a[0], a01 = a[1], a02 = a[2], a03 = a[3],
  2205. a10 = a[4], a11 = a[5], a12 = a[6], a13 = a[7],
  2206. a20 = a[8], a21 = a[9], a22 = a[10], a23 = a[11],
  2207. a30 = a[12], a31 = a[13], a32 = a[14], a33 = a[15],
  2208. b00 = a00 * a11 - a01 * a10,
  2209. b01 = a00 * a12 - a02 * a10,
  2210. b02 = a00 * a13 - a03 * a10,
  2211. b03 = a01 * a12 - a02 * a11,
  2212. b04 = a01 * a13 - a03 * a11,
  2213. b05 = a02 * a13 - a03 * a12,
  2214. b06 = a20 * a31 - a21 * a30,
  2215. b07 = a20 * a32 - a22 * a30,
  2216. b08 = a20 * a33 - a23 * a30,
  2217. b09 = a21 * a32 - a22 * a31,
  2218. b10 = a21 * a33 - a23 * a31,
  2219. b11 = a22 * a33 - a23 * a32,
  2220. // Calculate the determinant
  2221. det = b00 * b11 - b01 * b10 + b02 * b09 + b03 * b08 - b04 * b07 + b05 * b06;
  2222. if (!det) {
  2223. return null;
  2224. }
  2225. det = 1.0 / det;
  2226. out[0] = (a11 * b11 - a12 * b10 + a13 * b09) * det;
  2227. out[1] = (a12 * b08 - a10 * b11 - a13 * b07) * det;
  2228. out[2] = (a10 * b10 - a11 * b08 + a13 * b06) * det;
  2229. out[3] = (a02 * b10 - a01 * b11 - a03 * b09) * det;
  2230. out[4] = (a00 * b11 - a02 * b08 + a03 * b07) * det;
  2231. out[5] = (a01 * b08 - a00 * b10 - a03 * b06) * det;
  2232. out[6] = (a31 * b05 - a32 * b04 + a33 * b03) * det;
  2233. out[7] = (a32 * b02 - a30 * b05 - a33 * b01) * det;
  2234. out[8] = (a30 * b04 - a31 * b02 + a33 * b00) * det;
  2235. return out;
  2236. };
  2237. /**
  2238. * Returns a string representation of a mat3
  2239. *
  2240. * @param {Mat3} mat matrix to represent as a string
  2241. * @returns {String} string representation of the matrix
  2242. * @method module:mat3.str
  2243. */
  2244. mat3.str = function (a) {
  2245. return 'mat3(' + a[0] + ', ' + a[1] + ', ' + a[2] + ', ' +
  2246. a[3] + ', ' + a[4] + ', ' + a[5] + ', ' +
  2247. a[6] + ', ' + a[7] + ', ' + a[8] + ')';
  2248. };
  2249. /**
  2250. * Returns Frobenius norm of a mat3
  2251. *
  2252. * @param {Mat3} a the matrix to calculate Frobenius norm of
  2253. * @returns {Number} Frobenius norm
  2254. * @method module:mat3.frob
  2255. */
  2256. mat3.frob = function (a) {
  2257. return(Math.sqrt(Math.pow(a[0], 2) + Math.pow(a[1], 2) + Math.pow(a[2], 2) + Math.pow(a[3], 2) + Math.pow(a[4], 2) + Math.pow(a[5], 2) + Math.pow(a[6], 2) + Math.pow(a[7], 2) + Math.pow(a[8], 2)))
  2258. };
  2259. }
  2260. b4w.module["mat3"] = b4w.module["__mat3"];
  2261. b4w.module["__mat4"] = function(exports, require) {
  2262. var GLMAT_EPSILON = 0.0000001;
  2263. var GLMAT_ARRAY_TYPE = (typeof Float32Array !== 'undefined') ? Float32Array : Array;
  2264. var GLMAT_RANDOM = Math.random;
  2265. /* Copyright (c) 2015, Brandon Jones, Colin MacKenzie IV.
  2266. Permission is hereby granted, free of charge, to any person obtaining a copy
  2267. of this software and associated documentation files (the "Software"), to deal
  2268. in the Software without restriction, including without limitation the rights
  2269. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  2270. copies of the Software, and to permit persons to whom the Software is
  2271. furnished to do so, subject to the following conditions:
  2272. The above copyright notice and this permission notice shall be included in
  2273. all copies or substantial portions of the Software.
  2274. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  2275. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  2276. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  2277. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  2278. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  2279. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  2280. THE SOFTWARE. */
  2281. /**
  2282. * @module 4x4 Matrix
  2283. * @name mat4
  2284. */
  2285. var mat4 = exports;
  2286. /**
  2287. * Creates a new identity mat4
  2288. *
  2289. * @returns {Mat4} a new 4x4 matrix
  2290. * @method module:mat4.create
  2291. */
  2292. mat4.create = function() {
  2293. var out = new GLMAT_ARRAY_TYPE(16);
  2294. out[0] = 1;
  2295. out[1] = 0;
  2296. out[2] = 0;
  2297. out[3] = 0;
  2298. out[4] = 0;
  2299. out[5] = 1;
  2300. out[6] = 0;
  2301. out[7] = 0;
  2302. out[8] = 0;
  2303. out[9] = 0;
  2304. out[10] = 1;
  2305. out[11] = 0;
  2306. out[12] = 0;
  2307. out[13] = 0;
  2308. out[14] = 0;
  2309. out[15] = 1;
  2310. return out;
  2311. };
  2312. /**
  2313. * Creates a new mat4 initialized with values from an existing matrix
  2314. *
  2315. * @param {Mat4} a matrix to clone
  2316. * @returns {Mat4} a new 4x4 matrix
  2317. * @method module:mat4.clone
  2318. */
  2319. mat4.clone = function(a) {
  2320. var out = new GLMAT_ARRAY_TYPE(16);
  2321. out[0] = a[0];
  2322. out[1] = a[1];
  2323. out[2] = a[2];
  2324. out[3] = a[3];
  2325. out[4] = a[4];
  2326. out[5] = a[5];
  2327. out[6] = a[6];
  2328. out[7] = a[7];
  2329. out[8] = a[8];
  2330. out[9] = a[9];
  2331. out[10] = a[10];
  2332. out[11] = a[11];
  2333. out[12] = a[12];
  2334. out[13] = a[13];
  2335. out[14] = a[14];
  2336. out[15] = a[15];
  2337. return out;
  2338. };
  2339. /**
  2340. * Copy the values from one mat4 to another
  2341. *
  2342. * @param {Mat4} a the source matrix
  2343. * @returns {Mat4} out
  2344. * @param {Mat4} out the receiving matrix
  2345. * @method module:mat4.copy
  2346. */
  2347. mat4.copy = function(a, out) {
  2348. out[0] = a[0];
  2349. out[1] = a[1];
  2350. out[2] = a[2];
  2351. out[3] = a[3];
  2352. out[4] = a[4];
  2353. out[5] = a[5];
  2354. out[6] = a[6];
  2355. out[7] = a[7];
  2356. out[8] = a[8];
  2357. out[9] = a[9];
  2358. out[10] = a[10];
  2359. out[11] = a[11];
  2360. out[12] = a[12];
  2361. out[13] = a[13];
  2362. out[14] = a[14];
  2363. out[15] = a[15];
  2364. return out;
  2365. };
  2366. /**
  2367. * Set a mat4 to the identity matrix
  2368. *
  2369. * @returns {Mat4} out
  2370. * @param {Mat4} out the receiving matrix
  2371. * @method module:mat4.identity
  2372. */
  2373. mat4.identity = function(out) {
  2374. out[0] = 1;
  2375. out[1] = 0;
  2376. out[2] = 0;
  2377. out[3] = 0;
  2378. out[4] = 0;
  2379. out[5] = 1;
  2380. out[6] = 0;
  2381. out[7] = 0;
  2382. out[8] = 0;
  2383. out[9] = 0;
  2384. out[10] = 1;
  2385. out[11] = 0;
  2386. out[12] = 0;
  2387. out[13] = 0;
  2388. out[14] = 0;
  2389. out[15] = 1;
  2390. return out;
  2391. };
  2392. /**
  2393. * Transpose the values of a mat4
  2394. *
  2395. * @param {Mat4} a the source matrix
  2396. * @returns {Mat4} out
  2397. * @param {Mat4} out the receiving matrix
  2398. * @method module:mat4.transpose
  2399. */
  2400. mat4.transpose = function(a, out) {
  2401. // If we are transposing ourselves we can skip a few steps but have to cache some values
  2402. if (out === a) {
  2403. var a01 = a[1], a02 = a[2], a03 = a[3],
  2404. a12 = a[6], a13 = a[7],
  2405. a23 = a[11];
  2406. out[1] = a[4];
  2407. out[2] = a[8];
  2408. out[3] = a[12];
  2409. out[4] = a01;
  2410. out[6] = a[9];
  2411. out[7] = a[13];
  2412. out[8] = a02;
  2413. out[9] = a12;
  2414. out[11] = a[14];
  2415. out[12] = a03;
  2416. out[13] = a13;
  2417. out[14] = a23;
  2418. } else {
  2419. out[0] = a[0];
  2420. out[1] = a[4];
  2421. out[2] = a[8];
  2422. out[3] = a[12];
  2423. out[4] = a[1];
  2424. out[5] = a[5];
  2425. out[6] = a[9];
  2426. out[7] = a[13];
  2427. out[8] = a[2];
  2428. out[9] = a[6];
  2429. out[10] = a[10];
  2430. out[11] = a[14];
  2431. out[12] = a[3];
  2432. out[13] = a[7];
  2433. out[14] = a[11];
  2434. out[15] = a[15];
  2435. }
  2436. return out;
  2437. };
  2438. /**
  2439. * Inverts a mat4
  2440. *
  2441. * @param {Mat4} a the source matrix
  2442. * @returns {Mat4} out
  2443. * @param {Mat4} out the receiving matrix
  2444. * @method module:mat4.invert
  2445. */
  2446. mat4.invert = function(a, out) {
  2447. var a00 = a[0], a01 = a[1], a02 = a[2], a03 = a[3],
  2448. a10 = a[4], a11 = a[5], a12 = a[6], a13 = a[7],
  2449. a20 = a[8], a21 = a[9], a22 = a[10], a23 = a[11],
  2450. a30 = a[12], a31 = a[13], a32 = a[14], a33 = a[15],
  2451. b00 = a00 * a11 - a01 * a10,
  2452. b01 = a00 * a12 - a02 * a10,
  2453. b02 = a00 * a13 - a03 * a10,
  2454. b03 = a01 * a12 - a02 * a11,
  2455. b04 = a01 * a13 - a03 * a11,
  2456. b05 = a02 * a13 - a03 * a12,
  2457. b06 = a20 * a31 - a21 * a30,
  2458. b07 = a20 * a32 - a22 * a30,
  2459. b08 = a20 * a33 - a23 * a30,
  2460. b09 = a21 * a32 - a22 * a31,
  2461. b10 = a21 * a33 - a23 * a31,
  2462. b11 = a22 * a33 - a23 * a32,
  2463. // Calculate the determinant
  2464. det = b00 * b11 - b01 * b10 + b02 * b09 + b03 * b08 - b04 * b07 + b05 * b06;
  2465. if (!det) {
  2466. return null;
  2467. }
  2468. det = 1.0 / det;
  2469. out[0] = (a11 * b11 - a12 * b10 + a13 * b09) * det;
  2470. out[1] = (a02 * b10 - a01 * b11 - a03 * b09) * det;
  2471. out[2] = (a31 * b05 - a32 * b04 + a33 * b03) * det;
  2472. out[3] = (a22 * b04 - a21 * b05 - a23 * b03) * det;
  2473. out[4] = (a12 * b08 - a10 * b11 - a13 * b07) * det;
  2474. out[5] = (a00 * b11 - a02 * b08 + a03 * b07) * det;
  2475. out[6] = (a32 * b02 - a30 * b05 - a33 * b01) * det;
  2476. out[7] = (a20 * b05 - a22 * b02 + a23 * b01) * det;
  2477. out[8] = (a10 * b10 - a11 * b08 + a13 * b06) * det;
  2478. out[9] = (a01 * b08 - a00 * b10 - a03 * b06) * det;
  2479. out[10] = (a30 * b04 - a31 * b02 + a33 * b00) * det;
  2480. out[11] = (a21 * b02 - a20 * b04 - a23 * b00) * det;
  2481. out[12] = (a11 * b07 - a10 * b09 - a12 * b06) * det;
  2482. out[13] = (a00 * b09 - a01 * b07 + a02 * b06) * det;
  2483. out[14] = (a31 * b01 - a30 * b03 - a32 * b00) * det;
  2484. out[15] = (a20 * b03 - a21 * b01 + a22 * b00) * det;
  2485. return out;
  2486. };
  2487. /**
  2488. * Calculates the adjugate of a mat4
  2489. *
  2490. * @param {Mat4} a the source matrix
  2491. * @returns {Mat4} out
  2492. * @param {Mat4} out the receiving matrix
  2493. * @method module:mat4.adjoint
  2494. */
  2495. mat4.adjoint = function(a, out) {
  2496. var a00 = a[0], a01 = a[1], a02 = a[2], a03 = a[3],
  2497. a10 = a[4], a11 = a[5], a12 = a[6], a13 = a[7],
  2498. a20 = a[8], a21 = a[9], a22 = a[10], a23 = a[11],
  2499. a30 = a[12], a31 = a[13], a32 = a[14], a33 = a[15];
  2500. out[0] = (a11 * (a22 * a33 - a23 * a32) - a21 * (a12 * a33 - a13 * a32) + a31 * (a12 * a23 - a13 * a22));
  2501. out[1] = -(a01 * (a22 * a33 - a23 * a32) - a21 * (a02 * a33 - a03 * a32) + a31 * (a02 * a23 - a03 * a22));
  2502. out[2] = (a01 * (a12 * a33 - a13 * a32) - a11 * (a02 * a33 - a03 * a32) + a31 * (a02 * a13 - a03 * a12));
  2503. out[3] = -(a01 * (a12 * a23 - a13 * a22) - a11 * (a02 * a23 - a03 * a22) + a21 * (a02 * a13 - a03 * a12));
  2504. out[4] = -(a10 * (a22 * a33 - a23 * a32) - a20 * (a12 * a33 - a13 * a32) + a30 * (a12 * a23 - a13 * a22));
  2505. out[5] = (a00 * (a22 * a33 - a23 * a32) - a20 * (a02 * a33 - a03 * a32) + a30 * (a02 * a23 - a03 * a22));
  2506. out[6] = -(a00 * (a12 * a33 - a13 * a32) - a10 * (a02 * a33 - a03 * a32) + a30 * (a02 * a13 - a03 * a12));
  2507. out[7] = (a00 * (a12 * a23 - a13 * a22) - a10 * (a02 * a23 - a03 * a22) + a20 * (a02 * a13 - a03 * a12));
  2508. out[8] = (a10 * (a21 * a33 - a23 * a31) - a20 * (a11 * a33 - a13 * a31) + a30 * (a11 * a23 - a13 * a21));
  2509. out[9] = -(a00 * (a21 * a33 - a23 * a31) - a20 * (a01 * a33 - a03 * a31) + a30 * (a01 * a23 - a03 * a21));
  2510. out[10] = (a00 * (a11 * a33 - a13 * a31) - a10 * (a01 * a33 - a03 * a31) + a30 * (a01 * a13 - a03 * a11));
  2511. out[11] = -(a00 * (a11 * a23 - a13 * a21) - a10 * (a01 * a23 - a03 * a21) + a20 * (a01 * a13 - a03 * a11));
  2512. out[12] = -(a10 * (a21 * a32 - a22 * a31) - a20 * (a11 * a32 - a12 * a31) + a30 * (a11 * a22 - a12 * a21));
  2513. out[13] = (a00 * (a21 * a32 - a22 * a31) - a20 * (a01 * a32 - a02 * a31) + a30 * (a01 * a22 - a02 * a21));
  2514. out[14] = -(a00 * (a11 * a32 - a12 * a31) - a10 * (a01 * a32 - a02 * a31) + a30 * (a01 * a12 - a02 * a11));
  2515. out[15] = (a00 * (a11 * a22 - a12 * a21) - a10 * (a01 * a22 - a02 * a21) + a20 * (a01 * a12 - a02 * a11));
  2516. return out;
  2517. };
  2518. /**
  2519. * Calculates the determinant of a mat4
  2520. *
  2521. * @param {Mat4} a the source matrix
  2522. * @returns {Number} determinant of a
  2523. * @method module:mat4.determinant
  2524. */
  2525. mat4.determinant = function (a) {
  2526. var a00 = a[0], a01 = a[1], a02 = a[2], a03 = a[3],
  2527. a10 = a[4], a11 = a[5], a12 = a[6], a13 = a[7],
  2528. a20 = a[8], a21 = a[9], a22 = a[10], a23 = a[11],
  2529. a30 = a[12], a31 = a[13], a32 = a[14], a33 = a[15],
  2530. b00 = a00 * a11 - a01 * a10,
  2531. b01 = a00 * a12 - a02 * a10,
  2532. b02 = a00 * a13 - a03 * a10,
  2533. b03 = a01 * a12 - a02 * a11,
  2534. b04 = a01 * a13 - a03 * a11,
  2535. b05 = a02 * a13 - a03 * a12,
  2536. b06 = a20 * a31 - a21 * a30,
  2537. b07 = a20 * a32 - a22 * a30,
  2538. b08 = a20 * a33 - a23 * a30,
  2539. b09 = a21 * a32 - a22 * a31,
  2540. b10 = a21 * a33 - a23 * a31,
  2541. b11 = a22 * a33 - a23 * a32;
  2542. // Calculate the determinant
  2543. return b00 * b11 - b01 * b10 + b02 * b09 + b03 * b08 - b04 * b07 + b05 * b06;
  2544. };
  2545. /**
  2546. * Multiplies two mat4's
  2547. *
  2548. * @param {Mat4} a the first operand
  2549. * @param {Mat4} b the second operand
  2550. * @returns {Mat4} out
  2551. * @param {Mat4} out the receiving matrix
  2552. * @method module:mat4.multiply
  2553. */
  2554. mat4.multiply = function (a, b, out) {
  2555. var a00 = a[0], a01 = a[1], a02 = a[2], a03 = a[3],
  2556. a10 = a[4], a11 = a[5], a12 = a[6], a13 = a[7],
  2557. a20 = a[8], a21 = a[9], a22 = a[10], a23 = a[11],
  2558. a30 = a[12], a31 = a[13], a32 = a[14], a33 = a[15];
  2559. // Cache only the current line of the second matrix
  2560. var b0 = b[0], b1 = b[1], b2 = b[2], b3 = b[3];
  2561. out[0] = b0*a00 + b1*a10 + b2*a20 + b3*a30;
  2562. out[1] = b0*a01 + b1*a11 + b2*a21 + b3*a31;
  2563. out[2] = b0*a02 + b1*a12 + b2*a22 + b3*a32;
  2564. out[3] = b0*a03 + b1*a13 + b2*a23 + b3*a33;
  2565. b0 = b[4]; b1 = b[5]; b2 = b[6]; b3 = b[7];
  2566. out[4] = b0*a00 + b1*a10 + b2*a20 + b3*a30;
  2567. out[5] = b0*a01 + b1*a11 + b2*a21 + b3*a31;
  2568. out[6] = b0*a02 + b1*a12 + b2*a22 + b3*a32;
  2569. out[7] = b0*a03 + b1*a13 + b2*a23 + b3*a33;
  2570. b0 = b[8]; b1 = b[9]; b2 = b[10]; b3 = b[11];
  2571. out[8] = b0*a00 + b1*a10 + b2*a20 + b3*a30;
  2572. out[9] = b0*a01 + b1*a11 + b2*a21 + b3*a31;
  2573. out[10] = b0*a02 + b1*a12 + b2*a22 + b3*a32;
  2574. out[11] = b0*a03 + b1*a13 + b2*a23 + b3*a33;
  2575. b0 = b[12]; b1 = b[13]; b2 = b[14]; b3 = b[15];
  2576. out[12] = b0*a00 + b1*a10 + b2*a20 + b3*a30;
  2577. out[13] = b0*a01 + b1*a11 + b2*a21 + b3*a31;
  2578. out[14] = b0*a02 + b1*a12 + b2*a22 + b3*a32;
  2579. out[15] = b0*a03 + b1*a13 + b2*a23 + b3*a33;
  2580. return out;
  2581. };
  2582. /**
  2583. * Alias for {@link mat4.multiply}
  2584. * @function
  2585. * @method module:mat4.mul
  2586. */
  2587. mat4.mul = mat4.multiply;
  2588. /**
  2589. * Translate a mat4 by the given vector
  2590. *
  2591. * @param {Mat4} a the matrix to translate
  2592. * @param {Vec3} v vector to translate by
  2593. * @returns {Mat4} out
  2594. * @param {Mat4} out the receiving matrix
  2595. * @method module:mat4.translate
  2596. */
  2597. mat4.translate = function (a, v, out) {
  2598. var x = v[0], y = v[1], z = v[2],
  2599. a00, a01, a02, a03,
  2600. a10, a11, a12, a13,
  2601. a20, a21, a22, a23;
  2602. if (a === out) {
  2603. out[12] = a[0] * x + a[4] * y + a[8] * z + a[12];
  2604. out[13] = a[1] * x + a[5] * y + a[9] * z + a[13];
  2605. out[14] = a[2] * x + a[6] * y + a[10] * z + a[14];
  2606. out[15] = a[3] * x + a[7] * y + a[11] * z + a[15];
  2607. } else {
  2608. a00 = a[0]; a01 = a[1]; a02 = a[2]; a03 = a[3];
  2609. a10 = a[4]; a11 = a[5]; a12 = a[6]; a13 = a[7];
  2610. a20 = a[8]; a21 = a[9]; a22 = a[10]; a23 = a[11];
  2611. out[0] = a00; out[1] = a01; out[2] = a02; out[3] = a03;
  2612. out[4] = a10; out[5] = a11; out[6] = a12; out[7] = a13;
  2613. out[8] = a20; out[9] = a21; out[10] = a22; out[11] = a23;
  2614. out[12] = a00 * x + a10 * y + a20 * z + a[12];
  2615. out[13] = a01 * x + a11 * y + a21 * z + a[13];
  2616. out[14] = a02 * x + a12 * y + a22 * z + a[14];
  2617. out[15] = a03 * x + a13 * y + a23 * z + a[15];
  2618. }
  2619. return out;
  2620. };
  2621. /**
  2622. * Scales the mat4 by the dimensions in the given vec3
  2623. *
  2624. * @param {Mat4} a the matrix to scale
  2625. * @param {Vec3} v the vec3 to scale the matrix by
  2626. * @returns {Mat4} out
  2627. * @param {Mat4} out the receiving matrix
  2628. * @method module:mat4.scale
  2629. */
  2630. mat4.scale = function(a, v, out) {
  2631. var x = v[0], y = v[1], z = v[2];
  2632. out[0] = a[0] * x;
  2633. out[1] = a[1] * x;
  2634. out[2] = a[2] * x;
  2635. out[3] = a[3] * x;
  2636. out[4] = a[4] * y;
  2637. out[5] = a[5] * y;
  2638. out[6] = a[6] * y;
  2639. out[7] = a[7] * y;
  2640. out[8] = a[8] * z;
  2641. out[9] = a[9] * z;
  2642. out[10] = a[10] * z;
  2643. out[11] = a[11] * z;
  2644. out[12] = a[12];
  2645. out[13] = a[13];
  2646. out[14] = a[14];
  2647. out[15] = a[15];
  2648. return out;
  2649. };
  2650. /**
  2651. * Rotates a mat4 by the given angle around the given axis
  2652. *
  2653. * @param {Mat4} a the matrix to rotate
  2654. * @param {Number} rad the angle to rotate the matrix by
  2655. * @param {Vec3} axis the axis to rotate around
  2656. * @returns {Mat4} out
  2657. * @param {Mat4} out the receiving matrix
  2658. * @method module:mat4.rotate
  2659. */
  2660. mat4.rotate = function (a, rad, axis, out) {
  2661. var x = axis[0], y = axis[1], z = axis[2],
  2662. len = Math.sqrt(x * x + y * y + z * z),
  2663. s, c, t,
  2664. a00, a01, a02, a03,
  2665. a10, a11, a12, a13,
  2666. a20, a21, a22, a23,
  2667. b00, b01, b02,
  2668. b10, b11, b12,
  2669. b20, b21, b22;
  2670. if (Math.abs(len) < GLMAT_EPSILON) { return null; }
  2671. len = 1 / len;
  2672. x *= len;
  2673. y *= len;
  2674. z *= len;
  2675. s = Math.sin(rad);
  2676. c = Math.cos(rad);
  2677. t = 1 - c;
  2678. a00 = a[0]; a01 = a[1]; a02 = a[2]; a03 = a[3];
  2679. a10 = a[4]; a11 = a[5]; a12 = a[6]; a13 = a[7];
  2680. a20 = a[8]; a21 = a[9]; a22 = a[10]; a23 = a[11];
  2681. // Construct the elements of the rotation matrix
  2682. b00 = x * x * t + c; b01 = y * x * t + z * s; b02 = z * x * t - y * s;
  2683. b10 = x * y * t - z * s; b11 = y * y * t + c; b12 = z * y * t + x * s;
  2684. b20 = x * z * t + y * s; b21 = y * z * t - x * s; b22 = z * z * t + c;
  2685. // Perform rotation-specific matrix multiplication
  2686. out[0] = a00 * b00 + a10 * b01 + a20 * b02;
  2687. out[1] = a01 * b00 + a11 * b01 + a21 * b02;
  2688. out[2] = a02 * b00 + a12 * b01 + a22 * b02;
  2689. out[3] = a03 * b00 + a13 * b01 + a23 * b02;
  2690. out[4] = a00 * b10 + a10 * b11 + a20 * b12;
  2691. out[5] = a01 * b10 + a11 * b11 + a21 * b12;
  2692. out[6] = a02 * b10 + a12 * b11 + a22 * b12;
  2693. out[7] = a03 * b10 + a13 * b11 + a23 * b12;
  2694. out[8] = a00 * b20 + a10 * b21 + a20 * b22;
  2695. out[9] = a01 * b20 + a11 * b21 + a21 * b22;
  2696. out[10] = a02 * b20 + a12 * b21 + a22 * b22;
  2697. out[11] = a03 * b20 + a13 * b21 + a23 * b22;
  2698. if (a !== out) { // If the source and destination differ, copy the unchanged last row
  2699. out[12] = a[12];
  2700. out[13] = a[13];
  2701. out[14] = a[14];
  2702. out[15] = a[15];
  2703. }
  2704. return out;
  2705. };
  2706. /**
  2707. * Rotates a matrix by the given angle around the X axis
  2708. *
  2709. * @param {Mat4} a the matrix to rotate
  2710. * @param {Number} rad the angle to rotate the matrix by
  2711. * @returns {Mat4} out
  2712. * @param {Mat4} out the receiving matrix
  2713. * @method module:mat4.rotateX
  2714. */
  2715. mat4.rotateX = function (a, rad, out) {
  2716. var s = Math.sin(rad),
  2717. c = Math.cos(rad),
  2718. a10 = a[4],
  2719. a11 = a[5],
  2720. a12 = a[6],
  2721. a13 = a[7],
  2722. a20 = a[8],
  2723. a21 = a[9],
  2724. a22 = a[10],
  2725. a23 = a[11];
  2726. if (a !== out) { // If the source and destination differ, copy the unchanged rows
  2727. out[0] = a[0];
  2728. out[1] = a[1];
  2729. out[2] = a[2];
  2730. out[3] = a[3];
  2731. out[12] = a[12];
  2732. out[13] = a[13];
  2733. out[14] = a[14];
  2734. out[15] = a[15];
  2735. }
  2736. // Perform axis-specific matrix multiplication
  2737. out[4] = a10 * c + a20 * s;
  2738. out[5] = a11 * c + a21 * s;
  2739. out[6] = a12 * c + a22 * s;
  2740. out[7] = a13 * c + a23 * s;
  2741. out[8] = a20 * c - a10 * s;
  2742. out[9] = a21 * c - a11 * s;
  2743. out[10] = a22 * c - a12 * s;
  2744. out[11] = a23 * c - a13 * s;
  2745. return out;
  2746. };
  2747. /**
  2748. * Rotates a matrix by the given angle around the Y axis
  2749. *
  2750. * @param {Mat4} a the matrix to rotate
  2751. * @param {Number} rad the angle to rotate the matrix by
  2752. * @returns {Mat4} out
  2753. * @param {Mat4} out the receiving matrix
  2754. * @method module:mat4.rotateY
  2755. */
  2756. mat4.rotateY = function (a, rad, out) {
  2757. var s = Math.sin(rad),
  2758. c = Math.cos(rad),
  2759. a00 = a[0],
  2760. a01 = a[1],
  2761. a02 = a[2],
  2762. a03 = a[3],
  2763. a20 = a[8],
  2764. a21 = a[9],
  2765. a22 = a[10],
  2766. a23 = a[11];
  2767. if (a !== out) { // If the source and destination differ, copy the unchanged rows
  2768. out[4] = a[4];
  2769. out[5] = a[5];
  2770. out[6] = a[6];
  2771. out[7] = a[7];
  2772. out[12] = a[12];
  2773. out[13] = a[13];
  2774. out[14] = a[14];
  2775. out[15] = a[15];
  2776. }
  2777. // Perform axis-specific matrix multiplication
  2778. out[0] = a00 * c - a20 * s;
  2779. out[1] = a01 * c - a21 * s;
  2780. out[2] = a02 * c - a22 * s;
  2781. out[3] = a03 * c - a23 * s;
  2782. out[8] = a00 * s + a20 * c;
  2783. out[9] = a01 * s + a21 * c;
  2784. out[10] = a02 * s + a22 * c;
  2785. out[11] = a03 * s + a23 * c;
  2786. return out;
  2787. };
  2788. /**
  2789. * Rotates a matrix by the given angle around the Z axis
  2790. *
  2791. * @param {Mat4} a the matrix to rotate
  2792. * @param {Number} rad the angle to rotate the matrix by
  2793. * @returns {Mat4} out
  2794. * @param {Mat4} out the receiving matrix
  2795. * @method module:mat4.rotateZ
  2796. */
  2797. mat4.rotateZ = function (a, rad, out) {
  2798. var s = Math.sin(rad),
  2799. c = Math.cos(rad),
  2800. a00 = a[0],
  2801. a01 = a[1],
  2802. a02 = a[2],
  2803. a03 = a[3],
  2804. a10 = a[4],
  2805. a11 = a[5],
  2806. a12 = a[6],
  2807. a13 = a[7];
  2808. if (a !== out) { // If the source and destination differ, copy the unchanged last row
  2809. out[8] = a[8];
  2810. out[9] = a[9];
  2811. out[10] = a[10];
  2812. out[11] = a[11];
  2813. out[12] = a[12];
  2814. out[13] = a[13];
  2815. out[14] = a[14];
  2816. out[15] = a[15];
  2817. }
  2818. // Perform axis-specific matrix multiplication
  2819. out[0] = a00 * c + a10 * s;
  2820. out[1] = a01 * c + a11 * s;
  2821. out[2] = a02 * c + a12 * s;
  2822. out[3] = a03 * c + a13 * s;
  2823. out[4] = a10 * c - a00 * s;
  2824. out[5] = a11 * c - a01 * s;
  2825. out[6] = a12 * c - a02 * s;
  2826. out[7] = a13 * c - a03 * s;
  2827. return out;
  2828. };
  2829. /**
  2830. * Creates a matrix from a vector translation
  2831. * This is equivalent to (but much faster than):
  2832. *
  2833. * mat4.identity(dest);
  2834. * mat4.translate(dest, dest, vec);
  2835. *
  2836. * @param {Vec3} v Translation vector
  2837. * @returns {Mat4} out
  2838. * @param {Mat4} out mat4 receiving operation result
  2839. * @method module:mat4.fromTranslation
  2840. */
  2841. mat4.fromTranslation = function(v, out) {
  2842. out[0] = 1;
  2843. out[1] = 0;
  2844. out[2] = 0;
  2845. out[3] = 0;
  2846. out[4] = 0;
  2847. out[5] = 1;
  2848. out[6] = 0;
  2849. out[7] = 0;
  2850. out[8] = 0;
  2851. out[9] = 0;
  2852. out[10] = 1;
  2853. out[11] = 0;
  2854. out[12] = v[0];
  2855. out[13] = v[1];
  2856. out[14] = v[2];
  2857. out[15] = 1;
  2858. return out;
  2859. }
  2860. /**
  2861. * Creates a matrix from a vector scaling
  2862. * This is equivalent to (but much faster than):
  2863. *
  2864. * mat4.identity(dest);
  2865. * mat4.scale(dest, dest, vec);
  2866. *
  2867. * @param {Vec3} v Scaling vector
  2868. * @returns {Mat4} out
  2869. * @param {Mat4} out mat4 receiving operation result
  2870. * @method module:mat4.fromScaling
  2871. */
  2872. mat4.fromScaling = function(v, out) {
  2873. out[0] = v[0];
  2874. out[1] = 0;
  2875. out[2] = 0;
  2876. out[3] = 0;
  2877. out[4] = 0;
  2878. out[5] = v[1];
  2879. out[6] = 0;
  2880. out[7] = 0;
  2881. out[8] = 0;
  2882. out[9] = 0;
  2883. out[10] = v[2];
  2884. out[11] = 0;
  2885. out[12] = 0;
  2886. out[13] = 0;
  2887. out[14] = 0;
  2888. out[15] = 1;
  2889. return out;
  2890. }
  2891. /**
  2892. * Creates a matrix from a given angle around a given axis
  2893. * This is equivalent to (but much faster than):
  2894. *
  2895. * mat4.identity(dest);
  2896. * mat4.rotate(dest, dest, rad, axis);
  2897. *
  2898. * @param {Number} rad the angle to rotate the matrix by
  2899. * @param {Vec3} axis the axis to rotate around
  2900. * @returns {Mat4} out
  2901. * @param {Mat4} out mat4 receiving operation result
  2902. * @method module:mat4.fromRotation
  2903. */
  2904. mat4.fromRotation = function(rad, axis, out) {
  2905. var x = axis[0], y = axis[1], z = axis[2],
  2906. len = Math.sqrt(x * x + y * y + z * z),
  2907. s, c, t;
  2908. if (Math.abs(len) < GLMAT_EPSILON) { return null; }
  2909. len = 1 / len;
  2910. x *= len;
  2911. y *= len;
  2912. z *= len;
  2913. s = Math.sin(rad);
  2914. c = Math.cos(rad);
  2915. t = 1 - c;
  2916. // Perform rotation-specific matrix multiplication
  2917. out[0] = x * x * t + c;
  2918. out[1] = y * x * t + z * s;
  2919. out[2] = z * x * t - y * s;
  2920. out[3] = 0;
  2921. out[4] = x * y * t - z * s;
  2922. out[5] = y * y * t + c;
  2923. out[6] = z * y * t + x * s;
  2924. out[7] = 0;
  2925. out[8] = x * z * t + y * s;
  2926. out[9] = y * z * t - x * s;
  2927. out[10] = z * z * t + c;
  2928. out[11] = 0;
  2929. out[12] = 0;
  2930. out[13] = 0;
  2931. out[14] = 0;
  2932. out[15] = 1;
  2933. return out;
  2934. }
  2935. /**
  2936. * Creates a matrix from the given angle around the X axis
  2937. * This is equivalent to (but much faster than):
  2938. *
  2939. * mat4.identity(dest);
  2940. * mat4.rotateX(dest, dest, rad);
  2941. *
  2942. * @param {Number} rad the angle to rotate the matrix by
  2943. * @returns {Mat4} out
  2944. * @param {Mat4} out mat4 receiving operation result
  2945. * @method module:mat4.fromXRotation
  2946. */
  2947. mat4.fromXRotation = function(rad, out) {
  2948. var s = Math.sin(rad),
  2949. c = Math.cos(rad);
  2950. // Perform axis-specific matrix multiplication
  2951. out[0] = 1;
  2952. out[1] = 0;
  2953. out[2] = 0;
  2954. out[3] = 0;
  2955. out[4] = 0;
  2956. out[5] = c;
  2957. out[6] = s;
  2958. out[7] = 0;
  2959. out[8] = 0;
  2960. out[9] = -s;
  2961. out[10] = c;
  2962. out[11] = 0;
  2963. out[12] = 0;
  2964. out[13] = 0;
  2965. out[14] = 0;
  2966. out[15] = 1;
  2967. return out;
  2968. }
  2969. /**
  2970. * Creates a matrix from the given angle around the Y axis
  2971. * This is equivalent to (but much faster than):
  2972. *
  2973. * mat4.identity(dest);
  2974. * mat4.rotateY(dest, dest, rad);
  2975. *
  2976. * @param {Number} rad the angle to rotate the matrix by
  2977. * @returns {Mat4} out
  2978. * @param {Mat4} out mat4 receiving operation result
  2979. * @method module:mat4.fromYRotation
  2980. */
  2981. mat4.fromYRotation = function(rad, out) {
  2982. var s = Math.sin(rad),
  2983. c = Math.cos(rad);
  2984. // Perform axis-specific matrix multiplication
  2985. out[0] = c;
  2986. out[1] = 0;
  2987. out[2] = -s;
  2988. out[3] = 0;
  2989. out[4] = 0;
  2990. out[5] = 1;
  2991. out[6] = 0;
  2992. out[7] = 0;
  2993. out[8] = s;
  2994. out[9] = 0;
  2995. out[10] = c;
  2996. out[11] = 0;
  2997. out[12] = 0;
  2998. out[13] = 0;
  2999. out[14] = 0;
  3000. out[15] = 1;
  3001. return out;
  3002. }
  3003. /**
  3004. * Creates a matrix from the given angle around the Z axis
  3005. * This is equivalent to (but much faster than):
  3006. *
  3007. * mat4.identity(dest);
  3008. * mat4.rotateZ(dest, dest, rad);
  3009. *
  3010. * @param {Number} rad the angle to rotate the matrix by
  3011. * @returns {Mat4} out
  3012. * @param {Mat4} out mat4 receiving operation result
  3013. * @method module:mat4.fromZRotation
  3014. */
  3015. mat4.fromZRotation = function(rad, out) {
  3016. var s = Math.sin(rad),
  3017. c = Math.cos(rad);
  3018. // Perform axis-specific matrix multiplication
  3019. out[0] = c;
  3020. out[1] = s;
  3021. out[2] = 0;
  3022. out[3] = 0;
  3023. out[4] = -s;
  3024. out[5] = c;
  3025. out[6] = 0;
  3026. out[7] = 0;
  3027. out[8] = 0;
  3028. out[9] = 0;
  3029. out[10] = 1;
  3030. out[11] = 0;
  3031. out[12] = 0;
  3032. out[13] = 0;
  3033. out[14] = 0;
  3034. out[15] = 1;
  3035. return out;
  3036. }
  3037. /**
  3038. * Creates a matrix from a quaternion rotation and vector translation
  3039. * This is equivalent to (but much faster than):
  3040. *
  3041. * mat4.identity(dest);
  3042. * mat4.translate(dest, vec);
  3043. * var quatMat = mat4.create();
  3044. * quat4.toMat4(quat, quatMat);
  3045. * mat4.multiply(dest, quatMat);
  3046. *
  3047. * @param {quat4} q Rotation quaternion
  3048. * @param {Vec3} v Translation vector
  3049. * @returns {Mat4} out
  3050. * @param {Mat4} out mat4 receiving operation result
  3051. * @method module:mat4.fromRotationTranslation
  3052. */
  3053. mat4.fromRotationTranslation = function (q, v, out) {
  3054. // Quaternion math
  3055. var x = q[0], y = q[1], z = q[2], w = q[3],
  3056. x2 = x + x,
  3057. y2 = y + y,
  3058. z2 = z + z,
  3059. xx = x * x2,
  3060. xy = x * y2,
  3061. xz = x * z2,
  3062. yy = y * y2,
  3063. yz = y * z2,
  3064. zz = z * z2,
  3065. wx = w * x2,
  3066. wy = w * y2,
  3067. wz = w * z2;
  3068. out[0] = 1 - (yy + zz);
  3069. out[1] = xy + wz;
  3070. out[2] = xz - wy;
  3071. out[3] = 0;
  3072. out[4] = xy - wz;
  3073. out[5] = 1 - (xx + zz);
  3074. out[6] = yz + wx;
  3075. out[7] = 0;
  3076. out[8] = xz + wy;
  3077. out[9] = yz - wx;
  3078. out[10] = 1 - (xx + yy);
  3079. out[11] = 0;
  3080. out[12] = v[0];
  3081. out[13] = v[1];
  3082. out[14] = v[2];
  3083. out[15] = 1;
  3084. return out;
  3085. };
  3086. /**
  3087. * Creates a matrix from a quaternion rotation, vector translation and vector scale
  3088. * This is equivalent to (but much faster than):
  3089. *
  3090. * mat4.identity(dest);
  3091. * mat4.translate(dest, vec);
  3092. * var quatMat = mat4.create();
  3093. * quat4.toMat4(quat, quatMat);
  3094. * mat4.multiply(dest, quatMat);
  3095. * mat4.scale(dest, scale)
  3096. *
  3097. * @param {quat4} q Rotation quaternion
  3098. * @param {Vec3} v Translation vector
  3099. * @param {Vec3} s Scaling vector
  3100. * @returns {Mat4} out
  3101. * @param {Mat4} out mat4 receiving operation result
  3102. * @method module:mat4.fromRotationTranslationScale
  3103. */
  3104. mat4.fromRotationTranslationScale = function (q, v, s, out) {
  3105. // Quaternion math
  3106. var x = q[0], y = q[1], z = q[2], w = q[3],
  3107. x2 = x + x,
  3108. y2 = y + y,
  3109. z2 = z + z,
  3110. xx = x * x2,
  3111. xy = x * y2,
  3112. xz = x * z2,
  3113. yy = y * y2,
  3114. yz = y * z2,
  3115. zz = z * z2,
  3116. wx = w * x2,
  3117. wy = w * y2,
  3118. wz = w * z2,
  3119. sx = s[0],
  3120. sy = s[1],
  3121. sz = s[2];
  3122. out[0] = (1 - (yy + zz)) * sx;
  3123. out[1] = (xy + wz) * sx;
  3124. out[2] = (xz - wy) * sx;
  3125. out[3] = 0;
  3126. out[4] = (xy - wz) * sy;
  3127. out[5] = (1 - (xx + zz)) * sy;
  3128. out[6] = (yz + wx) * sy;
  3129. out[7] = 0;
  3130. out[8] = (xz + wy) * sz;
  3131. out[9] = (yz - wx) * sz;
  3132. out[10] = (1 - (xx + yy)) * sz;
  3133. out[11] = 0;
  3134. out[12] = v[0];
  3135. out[13] = v[1];
  3136. out[14] = v[2];
  3137. out[15] = 1;
  3138. return out;
  3139. };
  3140. /**
  3141. * Creates a matrix from a quaternion rotation, vector translation and vector scale, rotating and scaling around the given origin
  3142. * This is equivalent to (but much faster than):
  3143. *
  3144. * mat4.identity(dest);
  3145. * mat4.translate(dest, vec);
  3146. * mat4.translate(dest, origin);
  3147. * var quatMat = mat4.create();
  3148. * quat4.toMat4(quat, quatMat);
  3149. * mat4.multiply(dest, quatMat);
  3150. * mat4.scale(dest, scale)
  3151. * mat4.translate(dest, negativeOrigin);
  3152. *
  3153. * @param {quat4} q Rotation quaternion
  3154. * @param {Vec3} v Translation vector
  3155. * @param {Vec3} s Scaling vector
  3156. * @param {Vec3} o The origin vector around which to scale and rotate
  3157. * @returns {Mat4} out
  3158. * @param {Mat4} out mat4 receiving operation result
  3159. * @method module:mat4.fromRotationTranslationScaleOrigin
  3160. */
  3161. mat4.fromRotationTranslationScaleOrigin = function (q, v, s, o, out) {
  3162. // Quaternion math
  3163. var x = q[0], y = q[1], z = q[2], w = q[3],
  3164. x2 = x + x,
  3165. y2 = y + y,
  3166. z2 = z + z,
  3167. xx = x * x2,
  3168. xy = x * y2,
  3169. xz = x * z2,
  3170. yy = y * y2,
  3171. yz = y * z2,
  3172. zz = z * z2,
  3173. wx = w * x2,
  3174. wy = w * y2,
  3175. wz = w * z2,
  3176. sx = s[0],
  3177. sy = s[1],
  3178. sz = s[2],
  3179. ox = o[0],
  3180. oy = o[1],
  3181. oz = o[2];
  3182. out[0] = (1 - (yy + zz)) * sx;
  3183. out[1] = (xy + wz) * sx;
  3184. out[2] = (xz - wy) * sx;
  3185. out[3] = 0;
  3186. out[4] = (xy - wz) * sy;
  3187. out[5] = (1 - (xx + zz)) * sy;
  3188. out[6] = (yz + wx) * sy;
  3189. out[7] = 0;
  3190. out[8] = (xz + wy) * sz;
  3191. out[9] = (yz - wx) * sz;
  3192. out[10] = (1 - (xx + yy)) * sz;
  3193. out[11] = 0;
  3194. out[12] = v[0] + ox - (out[0] * ox + out[4] * oy + out[8] * oz);
  3195. out[13] = v[1] + oy - (out[1] * ox + out[5] * oy + out[9] * oz);
  3196. out[14] = v[2] + oz - (out[2] * ox + out[6] * oy + out[10] * oz);
  3197. out[15] = 1;
  3198. return out;
  3199. };
  3200. mat4.fromQuat = function (q, out) {
  3201. var x = q[0], y = q[1], z = q[2], w = q[3],
  3202. x2 = x + x,
  3203. y2 = y + y,
  3204. z2 = z + z,
  3205. xx = x * x2,
  3206. yx = y * x2,
  3207. yy = y * y2,
  3208. zx = z * x2,
  3209. zy = z * y2,
  3210. zz = z * z2,
  3211. wx = w * x2,
  3212. wy = w * y2,
  3213. wz = w * z2;
  3214. out[0] = 1 - yy - zz;
  3215. out[1] = yx + wz;
  3216. out[2] = zx - wy;
  3217. out[3] = 0;
  3218. out[4] = yx - wz;
  3219. out[5] = 1 - xx - zz;
  3220. out[6] = zy + wx;
  3221. out[7] = 0;
  3222. out[8] = zx + wy;
  3223. out[9] = zy - wx;
  3224. out[10] = 1 - xx - yy;
  3225. out[11] = 0;
  3226. out[12] = 0;
  3227. out[13] = 0;
  3228. out[14] = 0;
  3229. out[15] = 1;
  3230. return out;
  3231. };
  3232. /**
  3233. * Generates a frustum matrix with the given bounds
  3234. *
  3235. * @param {Number} left Left bound of the frustum
  3236. * @param {Number} right Right bound of the frustum
  3237. * @param {Number} bottom Bottom bound of the frustum
  3238. * @param {Number} top Top bound of the frustum
  3239. * @param {Number} near Near bound of the frustum
  3240. * @param {Number} far Far bound of the frustum
  3241. * @returns {Mat4} out
  3242. * @param {Mat4} out mat4 frustum matrix will be written into
  3243. * @method module:mat4.frustum
  3244. */
  3245. mat4.frustum = function (left, right, bottom, top, near, far, out) {
  3246. var rl = 1 / (right - left),
  3247. tb = 1 / (top - bottom),
  3248. nf = 1 / (near - far);
  3249. out[0] = (near * 2) * rl;
  3250. out[1] = 0;
  3251. out[2] = 0;
  3252. out[3] = 0;
  3253. out[4] = 0;
  3254. out[5] = (near * 2) * tb;
  3255. out[6] = 0;
  3256. out[7] = 0;
  3257. out[8] = (right + left) * rl;
  3258. out[9] = (top + bottom) * tb;
  3259. out[10] = (far + near) * nf;
  3260. out[11] = -1;
  3261. out[12] = 0;
  3262. out[13] = 0;
  3263. out[14] = (far * near * 2) * nf;
  3264. out[15] = 0;
  3265. return out;
  3266. };
  3267. /**
  3268. * Generates a perspective projection matrix with the given bounds
  3269. *
  3270. * @param {number} fovy Vertical field of view in radians
  3271. * @param {number} aspect Aspect ratio. typically viewport width/height
  3272. * @param {number} near Near bound of the frustum
  3273. * @param {number} far Far bound of the frustum
  3274. * @returns {Mat4} out
  3275. * @param {Mat4} out mat4 frustum matrix will be written into
  3276. * @method module:mat4.perspective
  3277. */
  3278. mat4.perspective = function (fovy, aspect, near, far, out) {
  3279. var f = 1.0 / Math.tan(fovy / 2),
  3280. nf = 1 / (near - far);
  3281. out[0] = f / aspect;
  3282. out[1] = 0;
  3283. out[2] = 0;
  3284. out[3] = 0;
  3285. out[4] = 0;
  3286. out[5] = f;
  3287. out[6] = 0;
  3288. out[7] = 0;
  3289. out[8] = 0;
  3290. out[9] = 0;
  3291. out[10] = (far + near) * nf;
  3292. out[11] = -1;
  3293. out[12] = 0;
  3294. out[13] = 0;
  3295. out[14] = (2 * far * near) * nf;
  3296. out[15] = 0;
  3297. return out;
  3298. };
  3299. /**
  3300. * Generates a perspective projection matrix with the given field of view.
  3301. * This is primarily useful for generating projection matrices to be used
  3302. * with the still experiemental WebVR API.
  3303. *
  3304. * @param {number} fov Object containing the following values: upDegrees, downDegrees, leftDegrees, rightDegrees
  3305. * @param {number} near Near bound of the frustum
  3306. * @param {number} far Far bound of the frustum
  3307. * @returns {Mat4} out
  3308. * @param {Mat4} out mat4 frustum matrix will be written into
  3309. * @method module:mat4.perspectiveFromFieldOfView
  3310. */
  3311. mat4.perspectiveFromFieldOfView = function (fov, near, far, out) {
  3312. var upTan = Math.tan(fov.upDegrees * Math.PI/180.0),
  3313. downTan = Math.tan(fov.downDegrees * Math.PI/180.0),
  3314. leftTan = Math.tan(fov.leftDegrees * Math.PI/180.0),
  3315. rightTan = Math.tan(fov.rightDegrees * Math.PI/180.0),
  3316. xScale = 2.0 / (leftTan + rightTan),
  3317. yScale = 2.0 / (upTan + downTan);
  3318. out[0] = xScale;
  3319. out[1] = 0.0;
  3320. out[2] = 0.0;
  3321. out[3] = 0.0;
  3322. out[4] = 0.0;
  3323. out[5] = yScale;
  3324. out[6] = 0.0;
  3325. out[7] = 0.0;
  3326. out[8] = -((leftTan - rightTan) * xScale * 0.5);
  3327. out[9] = ((upTan - downTan) * yScale * 0.5);
  3328. out[10] = far / (near - far);
  3329. out[11] = -1.0;
  3330. out[12] = 0.0;
  3331. out[13] = 0.0;
  3332. out[14] = (far * near) / (near - far);
  3333. out[15] = 0.0;
  3334. return out;
  3335. }
  3336. /**
  3337. * Generates a orthogonal projection matrix with the given bounds
  3338. *
  3339. * @param {number} left Left bound of the frustum
  3340. * @param {number} right Right bound of the frustum
  3341. * @param {number} bottom Bottom bound of the frustum
  3342. * @param {number} top Top bound of the frustum
  3343. * @param {number} near Near bound of the frustum
  3344. * @param {number} far Far bound of the frustum
  3345. * @returns {Mat4} out
  3346. * @param {Mat4} out mat4 frustum matrix will be written into
  3347. * @method module:mat4.ortho
  3348. */
  3349. mat4.ortho = function (left, right, bottom, top, near, far, out) {
  3350. var lr = 1 / (left - right),
  3351. bt = 1 / (bottom - top),
  3352. nf = 1 / (near - far);
  3353. out[0] = -2 * lr;
  3354. out[1] = 0;
  3355. out[2] = 0;
  3356. out[3] = 0;
  3357. out[4] = 0;
  3358. out[5] = -2 * bt;
  3359. out[6] = 0;
  3360. out[7] = 0;
  3361. out[8] = 0;
  3362. out[9] = 0;
  3363. out[10] = 2 * nf;
  3364. out[11] = 0;
  3365. out[12] = (left + right) * lr;
  3366. out[13] = (top + bottom) * bt;
  3367. out[14] = (far + near) * nf;
  3368. out[15] = 1;
  3369. return out;
  3370. };
  3371. /**
  3372. * Generates a look-at matrix with the given eye position, focal point, and up axis
  3373. *
  3374. * @param {Vec3} eye Position of the viewer
  3375. * @param {Vec3} center Point the viewer is looking at
  3376. * @param {Vec3} up vec3 pointing up
  3377. * @returns {Mat4} out
  3378. * @param {Mat4} out mat4 frustum matrix will be written into
  3379. * @method module:mat4.lookAt
  3380. */
  3381. mat4.lookAt = function (eye, center, up, out) {
  3382. var x0, x1, x2, y0, y1, y2, z0, z1, z2, len,
  3383. eyex = eye[0],
  3384. eyey = eye[1],
  3385. eyez = eye[2],
  3386. upx = up[0],
  3387. upy = up[1],
  3388. upz = up[2],
  3389. centerx = center[0],
  3390. centery = center[1],
  3391. centerz = center[2];
  3392. if (Math.abs(eyex - centerx) < GLMAT_EPSILON &&
  3393. Math.abs(eyey - centery) < GLMAT_EPSILON &&
  3394. Math.abs(eyez - centerz) < GLMAT_EPSILON) {
  3395. return mat4.identity(out);
  3396. }
  3397. z0 = eyex - centerx;
  3398. z1 = eyey - centery;
  3399. z2 = eyez - centerz;
  3400. len = 1 / Math.sqrt(z0 * z0 + z1 * z1 + z2 * z2);
  3401. z0 *= len;
  3402. z1 *= len;
  3403. z2 *= len;
  3404. x0 = upy * z2 - upz * z1;
  3405. x1 = upz * z0 - upx * z2;
  3406. x2 = upx * z1 - upy * z0;
  3407. len = Math.sqrt(x0 * x0 + x1 * x1 + x2 * x2);
  3408. if (!len) {
  3409. x0 = 0;
  3410. x1 = 0;
  3411. x2 = 0;
  3412. } else {
  3413. len = 1 / len;
  3414. x0 *= len;
  3415. x1 *= len;
  3416. x2 *= len;
  3417. }
  3418. y0 = z1 * x2 - z2 * x1;
  3419. y1 = z2 * x0 - z0 * x2;
  3420. y2 = z0 * x1 - z1 * x0;
  3421. len = Math.sqrt(y0 * y0 + y1 * y1 + y2 * y2);
  3422. if (!len) {
  3423. y0 = 0;
  3424. y1 = 0;
  3425. y2 = 0;
  3426. } else {
  3427. len = 1 / len;
  3428. y0 *= len;
  3429. y1 *= len;
  3430. y2 *= len;
  3431. }
  3432. out[0] = x0;
  3433. out[1] = y0;
  3434. out[2] = z0;
  3435. out[3] = 0;
  3436. out[4] = x1;
  3437. out[5] = y1;
  3438. out[6] = z1;
  3439. out[7] = 0;
  3440. out[8] = x2;
  3441. out[9] = y2;
  3442. out[10] = z2;
  3443. out[11] = 0;
  3444. out[12] = -(x0 * eyex + x1 * eyey + x2 * eyez);
  3445. out[13] = -(y0 * eyex + y1 * eyey + y2 * eyez);
  3446. out[14] = -(z0 * eyex + z1 * eyey + z2 * eyez);
  3447. out[15] = 1;
  3448. return out;
  3449. };
  3450. /**
  3451. * Returns a string representation of a mat4
  3452. *
  3453. * @param {Mat4} mat matrix to represent as a string
  3454. * @returns {String} string representation of the matrix
  3455. * @method module:mat4.str
  3456. */
  3457. mat4.str = function (a) {
  3458. return 'mat4(' + a[0] + ', ' + a[1] + ', ' + a[2] + ', ' + a[3] + ', ' +
  3459. a[4] + ', ' + a[5] + ', ' + a[6] + ', ' + a[7] + ', ' +
  3460. a[8] + ', ' + a[9] + ', ' + a[10] + ', ' + a[11] + ', ' +
  3461. a[12] + ', ' + a[13] + ', ' + a[14] + ', ' + a[15] + ')';
  3462. };
  3463. /**
  3464. * Returns Frobenius norm of a mat4
  3465. *
  3466. * @param {Mat4} a the matrix to calculate Frobenius norm of
  3467. * @returns {Number} Frobenius norm
  3468. * @method module:mat4.frob
  3469. */
  3470. mat4.frob = function (a) {
  3471. return(Math.sqrt(Math.pow(a[0], 2) + Math.pow(a[1], 2) + Math.pow(a[2], 2) + Math.pow(a[3], 2) + Math.pow(a[4], 2) + Math.pow(a[5], 2) + Math.pow(a[6], 2) + Math.pow(a[7], 2) + Math.pow(a[8], 2) + Math.pow(a[9], 2) + Math.pow(a[10], 2) + Math.pow(a[11], 2) + Math.pow(a[12], 2) + Math.pow(a[13], 2) + Math.pow(a[14], 2) + Math.pow(a[15], 2) ))
  3472. };
  3473. }
  3474. b4w.module["mat4"] = b4w.module["__mat4"];