diff --git a/README.md b/README.md new file mode 100644 index 0000000..0195950 --- /dev/null +++ b/README.md @@ -0,0 +1,2 @@ +Does not come with data. +See it in action [here](http://gta.rockstarvision.com/vehicleviewer/). diff --git a/gl-matrix.js b/gl-matrix.js new file mode 100644 index 0000000..fcaf428 --- /dev/null +++ b/gl-matrix.js @@ -0,0 +1,6888 @@ +/** + * @fileoverview gl-matrix - High performance matrix and vector operations + * @author Brandon Jones + * @author Colin MacKenzie IV + * @version 2.4.0 + */ + +/* Copyright (c) 2015, Brandon Jones, Colin MacKenzie IV. + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in +all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +THE SOFTWARE. */ + +(function webpackUniversalModuleDefinition(root, factory) { + if(typeof exports === 'object' && typeof module === 'object') + module.exports = factory(); + else if(typeof define === 'function' && define.amd) + define([], factory); + else { + var a = factory(); + for(var i in a) (typeof exports === 'object' ? exports : root)[i] = a[i]; + } +})(this, function() { +return /******/ (function(modules) { // webpackBootstrap +/******/ // The module cache +/******/ var installedModules = {}; +/******/ +/******/ // The require function +/******/ function __webpack_require__(moduleId) { +/******/ +/******/ // Check if module is in cache +/******/ if(installedModules[moduleId]) { +/******/ return installedModules[moduleId].exports; +/******/ } +/******/ // Create a new module (and put it into the cache) +/******/ var module = installedModules[moduleId] = { +/******/ i: moduleId, +/******/ l: false, +/******/ exports: {} +/******/ }; +/******/ +/******/ // Execute the module function +/******/ modules[moduleId].call(module.exports, module, module.exports, __webpack_require__); +/******/ +/******/ // Flag the module as loaded +/******/ module.l = true; +/******/ +/******/ // Return the exports of the module +/******/ return module.exports; +/******/ } +/******/ +/******/ +/******/ // expose the modules object (__webpack_modules__) +/******/ __webpack_require__.m = modules; +/******/ +/******/ // expose the module cache +/******/ __webpack_require__.c = installedModules; +/******/ +/******/ // define getter function for harmony exports +/******/ __webpack_require__.d = function(exports, name, getter) { +/******/ if(!__webpack_require__.o(exports, name)) { +/******/ Object.defineProperty(exports, name, { +/******/ configurable: false, +/******/ enumerable: true, +/******/ get: getter +/******/ }); +/******/ } +/******/ }; +/******/ +/******/ // getDefaultExport function for compatibility with non-harmony modules +/******/ __webpack_require__.n = function(module) { +/******/ var getter = module && module.__esModule ? +/******/ function getDefault() { return module['default']; } : +/******/ function getModuleExports() { return module; }; +/******/ __webpack_require__.d(getter, 'a', getter); +/******/ return getter; +/******/ }; +/******/ +/******/ // Object.prototype.hasOwnProperty.call +/******/ __webpack_require__.o = function(object, property) { return Object.prototype.hasOwnProperty.call(object, property); }; +/******/ +/******/ // __webpack_public_path__ +/******/ __webpack_require__.p = ""; +/******/ +/******/ // Load entry module and return exports +/******/ return __webpack_require__(__webpack_require__.s = 4); +/******/ }) +/************************************************************************/ +/******/ ([ +/* 0 */ +/***/ (function(module, exports, __webpack_require__) { + +"use strict"; + + +Object.defineProperty(exports, "__esModule", { + value: true +}); +exports.setMatrixArrayType = setMatrixArrayType; +exports.toRadian = toRadian; +exports.equals = equals; +/* Copyright (c) 2015, Brandon Jones, Colin MacKenzie IV. + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in +all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +THE SOFTWARE. */ + +/** + * Common utilities + * @module glMatrix + */ + +// Configuration Constants +var EPSILON = exports.EPSILON = 0.000001; +var ARRAY_TYPE = exports.ARRAY_TYPE = typeof Float32Array !== 'undefined' ? Float32Array : Array; +var RANDOM = exports.RANDOM = Math.random; + +/** + * Sets the type of array used when creating new vectors and matrices + * + * @param {Type} type Array type, such as Float32Array or Array + */ +function setMatrixArrayType(type) { + exports.ARRAY_TYPE = ARRAY_TYPE = type; +} + +var degree = Math.PI / 180; + +/** + * Convert Degree To Radian + * + * @param {Number} a Angle in Degrees + */ +function toRadian(a) { + return a * degree; +} + +/** + * Tests whether or not the arguments have approximately the same value, within an absolute + * or relative tolerance of glMatrix.EPSILON (an absolute tolerance is used for values less + * than or equal to 1.0, and a relative tolerance is used for larger values) + * + * @param {Number} a The first number to test. + * @param {Number} b The second number to test. + * @returns {Boolean} True if the numbers are approximately equal, false otherwise. + */ +function equals(a, b) { + return Math.abs(a - b) <= EPSILON * Math.max(1.0, Math.abs(a), Math.abs(b)); +} + +/***/ }), +/* 1 */ +/***/ (function(module, exports, __webpack_require__) { + +"use strict"; + + +Object.defineProperty(exports, "__esModule", { + value: true +}); +exports.sub = exports.mul = undefined; +exports.create = create; +exports.fromMat4 = fromMat4; +exports.clone = clone; +exports.copy = copy; +exports.fromValues = fromValues; +exports.set = set; +exports.identity = identity; +exports.transpose = transpose; +exports.invert = invert; +exports.adjoint = adjoint; +exports.determinant = determinant; +exports.multiply = multiply; +exports.translate = translate; +exports.rotate = rotate; +exports.scale = scale; +exports.fromTranslation = fromTranslation; +exports.fromRotation = fromRotation; +exports.fromScaling = fromScaling; +exports.fromMat2d = fromMat2d; +exports.fromQuat = fromQuat; +exports.normalFromMat4 = normalFromMat4; +exports.projection = projection; +exports.str = str; +exports.frob = frob; +exports.add = add; +exports.subtract = subtract; +exports.multiplyScalar = multiplyScalar; +exports.multiplyScalarAndAdd = multiplyScalarAndAdd; +exports.exactEquals = exactEquals; +exports.equals = equals; + +var _common = __webpack_require__(0); + +var glMatrix = _interopRequireWildcard(_common); + +function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) newObj[key] = obj[key]; } } newObj.default = obj; return newObj; } } + +/** + * 3x3 Matrix + * @module mat3 + */ + +/** + * Creates a new identity mat3 + * + * @returns {mat3} a new 3x3 matrix + */ +function create() { + var out = new glMatrix.ARRAY_TYPE(9); + out[0] = 1; + out[1] = 0; + out[2] = 0; + out[3] = 0; + out[4] = 1; + out[5] = 0; + out[6] = 0; + out[7] = 0; + out[8] = 1; + return out; +} + +/** + * Copies the upper-left 3x3 values into the given mat3. + * + * @param {mat3} out the receiving 3x3 matrix + * @param {mat4} a the source 4x4 matrix + * @returns {mat3} out + */ +/* Copyright (c) 2015, Brandon Jones, Colin MacKenzie IV. + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in +all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +THE SOFTWARE. */ + +function fromMat4(out, a) { + out[0] = a[0]; + out[1] = a[1]; + out[2] = a[2]; + out[3] = a[4]; + out[4] = a[5]; + out[5] = a[6]; + out[6] = a[8]; + out[7] = a[9]; + out[8] = a[10]; + return out; +} + +/** + * Creates a new mat3 initialized with values from an existing matrix + * + * @param {mat3} a matrix to clone + * @returns {mat3} a new 3x3 matrix + */ +function clone(a) { + var out = new glMatrix.ARRAY_TYPE(9); + out[0] = a[0]; + out[1] = a[1]; + out[2] = a[2]; + out[3] = a[3]; + out[4] = a[4]; + out[5] = a[5]; + out[6] = a[6]; + out[7] = a[7]; + out[8] = a[8]; + return out; +} + +/** + * Copy the values from one mat3 to another + * + * @param {mat3} out the receiving matrix + * @param {mat3} a the source matrix + * @returns {mat3} out + */ +function copy(out, a) { + out[0] = a[0]; + out[1] = a[1]; + out[2] = a[2]; + out[3] = a[3]; + out[4] = a[4]; + out[5] = a[5]; + out[6] = a[6]; + out[7] = a[7]; + out[8] = a[8]; + return out; +} + +/** + * Create a new mat3 with the given values + * + * @param {Number} m00 Component in column 0, row 0 position (index 0) + * @param {Number} m01 Component in column 0, row 1 position (index 1) + * @param {Number} m02 Component in column 0, row 2 position (index 2) + * @param {Number} m10 Component in column 1, row 0 position (index 3) + * @param {Number} m11 Component in column 1, row 1 position (index 4) + * @param {Number} m12 Component in column 1, row 2 position (index 5) + * @param {Number} m20 Component in column 2, row 0 position (index 6) + * @param {Number} m21 Component in column 2, row 1 position (index 7) + * @param {Number} m22 Component in column 2, row 2 position (index 8) + * @returns {mat3} A new mat3 + */ +function fromValues(m00, m01, m02, m10, m11, m12, m20, m21, m22) { + var out = new glMatrix.ARRAY_TYPE(9); + out[0] = m00; + out[1] = m01; + out[2] = m02; + out[3] = m10; + out[4] = m11; + out[5] = m12; + out[6] = m20; + out[7] = m21; + out[8] = m22; + return out; +} + +/** + * Set the components of a mat3 to the given values + * + * @param {mat3} out the receiving matrix + * @param {Number} m00 Component in column 0, row 0 position (index 0) + * @param {Number} m01 Component in column 0, row 1 position (index 1) + * @param {Number} m02 Component in column 0, row 2 position (index 2) + * @param {Number} m10 Component in column 1, row 0 position (index 3) + * @param {Number} m11 Component in column 1, row 1 position (index 4) + * @param {Number} m12 Component in column 1, row 2 position (index 5) + * @param {Number} m20 Component in column 2, row 0 position (index 6) + * @param {Number} m21 Component in column 2, row 1 position (index 7) + * @param {Number} m22 Component in column 2, row 2 position (index 8) + * @returns {mat3} out + */ +function set(out, m00, m01, m02, m10, m11, m12, m20, m21, m22) { + out[0] = m00; + out[1] = m01; + out[2] = m02; + out[3] = m10; + out[4] = m11; + out[5] = m12; + out[6] = m20; + out[7] = m21; + out[8] = m22; + return out; +} + +/** + * Set a mat3 to the identity matrix + * + * @param {mat3} out the receiving matrix + * @returns {mat3} out + */ +function identity(out) { + out[0] = 1; + out[1] = 0; + out[2] = 0; + out[3] = 0; + out[4] = 1; + out[5] = 0; + out[6] = 0; + out[7] = 0; + out[8] = 1; + return out; +} + +/** + * Transpose the values of a mat3 + * + * @param {mat3} out the receiving matrix + * @param {mat3} a the source matrix + * @returns {mat3} out + */ +function transpose(out, a) { + // If we are transposing ourselves we can skip a few steps but have to cache some values + if (out === a) { + var a01 = a[1], + a02 = a[2], + a12 = a[5]; + out[1] = a[3]; + out[2] = a[6]; + out[3] = a01; + out[5] = a[7]; + out[6] = a02; + out[7] = a12; + } else { + out[0] = a[0]; + out[1] = a[3]; + out[2] = a[6]; + out[3] = a[1]; + out[4] = a[4]; + out[5] = a[7]; + out[6] = a[2]; + out[7] = a[5]; + out[8] = a[8]; + } + + return out; +} + +/** + * Inverts a mat3 + * + * @param {mat3} out the receiving matrix + * @param {mat3} a the source matrix + * @returns {mat3} out + */ +function invert(out, a) { + var a00 = a[0], + a01 = a[1], + a02 = a[2]; + var a10 = a[3], + a11 = a[4], + a12 = a[5]; + var a20 = a[6], + a21 = a[7], + a22 = a[8]; + + var b01 = a22 * a11 - a12 * a21; + var b11 = -a22 * a10 + a12 * a20; + var b21 = a21 * a10 - a11 * a20; + + // Calculate the determinant + var det = a00 * b01 + a01 * b11 + a02 * b21; + + if (!det) { + return null; + } + det = 1.0 / det; + + out[0] = b01 * det; + out[1] = (-a22 * a01 + a02 * a21) * det; + out[2] = (a12 * a01 - a02 * a11) * det; + out[3] = b11 * det; + out[4] = (a22 * a00 - a02 * a20) * det; + out[5] = (-a12 * a00 + a02 * a10) * det; + out[6] = b21 * det; + out[7] = (-a21 * a00 + a01 * a20) * det; + out[8] = (a11 * a00 - a01 * a10) * det; + return out; +} + +/** + * Calculates the adjugate of a mat3 + * + * @param {mat3} out the receiving matrix + * @param {mat3} a the source matrix + * @returns {mat3} out + */ +function adjoint(out, a) { + var a00 = a[0], + a01 = a[1], + a02 = a[2]; + var a10 = a[3], + a11 = a[4], + a12 = a[5]; + var a20 = a[6], + a21 = a[7], + a22 = a[8]; + + out[0] = a11 * a22 - a12 * a21; + out[1] = a02 * a21 - a01 * a22; + out[2] = a01 * a12 - a02 * a11; + out[3] = a12 * a20 - a10 * a22; + out[4] = a00 * a22 - a02 * a20; + out[5] = a02 * a10 - a00 * a12; + out[6] = a10 * a21 - a11 * a20; + out[7] = a01 * a20 - a00 * a21; + out[8] = a00 * a11 - a01 * a10; + return out; +} + +/** + * Calculates the determinant of a mat3 + * + * @param {mat3} a the source matrix + * @returns {Number} determinant of a + */ +function determinant(a) { + var a00 = a[0], + a01 = a[1], + a02 = a[2]; + var a10 = a[3], + a11 = a[4], + a12 = a[5]; + var a20 = a[6], + a21 = a[7], + a22 = a[8]; + + return a00 * (a22 * a11 - a12 * a21) + a01 * (-a22 * a10 + a12 * a20) + a02 * (a21 * a10 - a11 * a20); +} + +/** + * Multiplies two mat3's + * + * @param {mat3} out the receiving matrix + * @param {mat3} a the first operand + * @param {mat3} b the second operand + * @returns {mat3} out + */ +function multiply(out, a, b) { + var a00 = a[0], + a01 = a[1], + a02 = a[2]; + var a10 = a[3], + a11 = a[4], + a12 = a[5]; + var a20 = a[6], + a21 = a[7], + a22 = a[8]; + + var b00 = b[0], + b01 = b[1], + b02 = b[2]; + var b10 = b[3], + b11 = b[4], + b12 = b[5]; + var b20 = b[6], + b21 = b[7], + b22 = b[8]; + + out[0] = b00 * a00 + b01 * a10 + b02 * a20; + out[1] = b00 * a01 + b01 * a11 + b02 * a21; + out[2] = b00 * a02 + b01 * a12 + b02 * a22; + + out[3] = b10 * a00 + b11 * a10 + b12 * a20; + out[4] = b10 * a01 + b11 * a11 + b12 * a21; + out[5] = b10 * a02 + b11 * a12 + b12 * a22; + + out[6] = b20 * a00 + b21 * a10 + b22 * a20; + out[7] = b20 * a01 + b21 * a11 + b22 * a21; + out[8] = b20 * a02 + b21 * a12 + b22 * a22; + return out; +} + +/** + * Translate a mat3 by the given vector + * + * @param {mat3} out the receiving matrix + * @param {mat3} a the matrix to translate + * @param {vec2} v vector to translate by + * @returns {mat3} out + */ +function translate(out, a, v) { + var a00 = a[0], + a01 = a[1], + a02 = a[2], + a10 = a[3], + a11 = a[4], + a12 = a[5], + a20 = a[6], + a21 = a[7], + a22 = a[8], + x = v[0], + y = v[1]; + + out[0] = a00; + out[1] = a01; + out[2] = a02; + + out[3] = a10; + out[4] = a11; + out[5] = a12; + + out[6] = x * a00 + y * a10 + a20; + out[7] = x * a01 + y * a11 + a21; + out[8] = x * a02 + y * a12 + a22; + return out; +} + +/** + * Rotates a mat3 by the given angle + * + * @param {mat3} out the receiving matrix + * @param {mat3} a the matrix to rotate + * @param {Number} rad the angle to rotate the matrix by + * @returns {mat3} out + */ +function rotate(out, a, rad) { + var a00 = a[0], + a01 = a[1], + a02 = a[2], + a10 = a[3], + a11 = a[4], + a12 = a[5], + a20 = a[6], + a21 = a[7], + a22 = a[8], + s = Math.sin(rad), + c = Math.cos(rad); + + out[0] = c * a00 + s * a10; + out[1] = c * a01 + s * a11; + out[2] = c * a02 + s * a12; + + out[3] = c * a10 - s * a00; + out[4] = c * a11 - s * a01; + out[5] = c * a12 - s * a02; + + out[6] = a20; + out[7] = a21; + out[8] = a22; + return out; +}; + +/** + * Scales the mat3 by the dimensions in the given vec2 + * + * @param {mat3} out the receiving matrix + * @param {mat3} a the matrix to rotate + * @param {vec2} v the vec2 to scale the matrix by + * @returns {mat3} out + **/ +function scale(out, a, v) { + var x = v[0], + y = v[1]; + + out[0] = x * a[0]; + out[1] = x * a[1]; + out[2] = x * a[2]; + + out[3] = y * a[3]; + out[4] = y * a[4]; + out[5] = y * a[5]; + + out[6] = a[6]; + out[7] = a[7]; + out[8] = a[8]; + return out; +} + +/** + * Creates a matrix from a vector translation + * This is equivalent to (but much faster than): + * + * mat3.identity(dest); + * mat3.translate(dest, dest, vec); + * + * @param {mat3} out mat3 receiving operation result + * @param {vec2} v Translation vector + * @returns {mat3} out + */ +function fromTranslation(out, v) { + out[0] = 1; + out[1] = 0; + out[2] = 0; + out[3] = 0; + out[4] = 1; + out[5] = 0; + out[6] = v[0]; + out[7] = v[1]; + out[8] = 1; + return out; +} + +/** + * Creates a matrix from a given angle + * This is equivalent to (but much faster than): + * + * mat3.identity(dest); + * mat3.rotate(dest, dest, rad); + * + * @param {mat3} out mat3 receiving operation result + * @param {Number} rad the angle to rotate the matrix by + * @returns {mat3} out + */ +function fromRotation(out, rad) { + var s = Math.sin(rad), + c = Math.cos(rad); + + out[0] = c; + out[1] = s; + out[2] = 0; + + out[3] = -s; + out[4] = c; + out[5] = 0; + + out[6] = 0; + out[7] = 0; + out[8] = 1; + return out; +} + +/** + * Creates a matrix from a vector scaling + * This is equivalent to (but much faster than): + * + * mat3.identity(dest); + * mat3.scale(dest, dest, vec); + * + * @param {mat3} out mat3 receiving operation result + * @param {vec2} v Scaling vector + * @returns {mat3} out + */ +function fromScaling(out, v) { + out[0] = v[0]; + out[1] = 0; + out[2] = 0; + + out[3] = 0; + out[4] = v[1]; + out[5] = 0; + + out[6] = 0; + out[7] = 0; + out[8] = 1; + return out; +} + +/** + * Copies the values from a mat2d into a mat3 + * + * @param {mat3} out the receiving matrix + * @param {mat2d} a the matrix to copy + * @returns {mat3} out + **/ +function fromMat2d(out, a) { + out[0] = a[0]; + out[1] = a[1]; + out[2] = 0; + + out[3] = a[2]; + out[4] = a[3]; + out[5] = 0; + + out[6] = a[4]; + out[7] = a[5]; + out[8] = 1; + return out; +} + +/** +* Calculates a 3x3 matrix from the given quaternion +* +* @param {mat3} out mat3 receiving operation result +* @param {quat} q Quaternion to create matrix from +* +* @returns {mat3} out +*/ +function fromQuat(out, q) { + var x = q[0], + y = q[1], + z = q[2], + w = q[3]; + var x2 = x + x; + var y2 = y + y; + var z2 = z + z; + + var xx = x * x2; + var yx = y * x2; + var yy = y * y2; + var zx = z * x2; + var zy = z * y2; + var zz = z * z2; + var wx = w * x2; + var wy = w * y2; + var wz = w * z2; + + out[0] = 1 - yy - zz; + out[3] = yx - wz; + out[6] = zx + wy; + + out[1] = yx + wz; + out[4] = 1 - xx - zz; + out[7] = zy - wx; + + out[2] = zx - wy; + out[5] = zy + wx; + out[8] = 1 - xx - yy; + + return out; +} + +/** +* Calculates a 3x3 normal matrix (transpose inverse) from the 4x4 matrix +* +* @param {mat3} out mat3 receiving operation result +* @param {mat4} a Mat4 to derive the normal matrix from +* +* @returns {mat3} out +*/ +function normalFromMat4(out, a) { + var a00 = a[0], + a01 = a[1], + a02 = a[2], + a03 = a[3]; + var a10 = a[4], + a11 = a[5], + a12 = a[6], + a13 = a[7]; + var a20 = a[8], + a21 = a[9], + a22 = a[10], + a23 = a[11]; + var a30 = a[12], + a31 = a[13], + a32 = a[14], + a33 = a[15]; + + var b00 = a00 * a11 - a01 * a10; + var b01 = a00 * a12 - a02 * a10; + var b02 = a00 * a13 - a03 * a10; + var b03 = a01 * a12 - a02 * a11; + var b04 = a01 * a13 - a03 * a11; + var b05 = a02 * a13 - a03 * a12; + var b06 = a20 * a31 - a21 * a30; + var b07 = a20 * a32 - a22 * a30; + var b08 = a20 * a33 - a23 * a30; + var b09 = a21 * a32 - a22 * a31; + var b10 = a21 * a33 - a23 * a31; + var b11 = a22 * a33 - a23 * a32; + + // Calculate the determinant + var det = b00 * b11 - b01 * b10 + b02 * b09 + b03 * b08 - b04 * b07 + b05 * b06; + + if (!det) { + return null; + } + det = 1.0 / det; + + out[0] = (a11 * b11 - a12 * b10 + a13 * b09) * det; + out[1] = (a12 * b08 - a10 * b11 - a13 * b07) * det; + out[2] = (a10 * b10 - a11 * b08 + a13 * b06) * det; + + out[3] = (a02 * b10 - a01 * b11 - a03 * b09) * det; + out[4] = (a00 * b11 - a02 * b08 + a03 * b07) * det; + out[5] = (a01 * b08 - a00 * b10 - a03 * b06) * det; + + out[6] = (a31 * b05 - a32 * b04 + a33 * b03) * det; + out[7] = (a32 * b02 - a30 * b05 - a33 * b01) * det; + out[8] = (a30 * b04 - a31 * b02 + a33 * b00) * det; + + return out; +} + +/** + * Generates a 2D projection matrix with the given bounds + * + * @param {mat3} out mat3 frustum matrix will be written into + * @param {number} width Width of your gl context + * @param {number} height Height of gl context + * @returns {mat3} out + */ +function projection(out, width, height) { + out[0] = 2 / width; + out[1] = 0; + out[2] = 0; + out[3] = 0; + out[4] = -2 / height; + out[5] = 0; + out[6] = -1; + out[7] = 1; + out[8] = 1; + return out; +} + +/** + * Returns a string representation of a mat3 + * + * @param {mat3} a matrix to represent as a string + * @returns {String} string representation of the matrix + */ +function str(a) { + return 'mat3(' + a[0] + ', ' + a[1] + ', ' + a[2] + ', ' + a[3] + ', ' + a[4] + ', ' + a[5] + ', ' + a[6] + ', ' + a[7] + ', ' + a[8] + ')'; +} + +/** + * Returns Frobenius norm of a mat3 + * + * @param {mat3} a the matrix to calculate Frobenius norm of + * @returns {Number} Frobenius norm + */ +function frob(a) { + 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)); +} + +/** + * Adds two mat3's + * + * @param {mat3} out the receiving matrix + * @param {mat3} a the first operand + * @param {mat3} b the second operand + * @returns {mat3} out + */ +function add(out, a, b) { + out[0] = a[0] + b[0]; + out[1] = a[1] + b[1]; + out[2] = a[2] + b[2]; + out[3] = a[3] + b[3]; + out[4] = a[4] + b[4]; + out[5] = a[5] + b[5]; + out[6] = a[6] + b[6]; + out[7] = a[7] + b[7]; + out[8] = a[8] + b[8]; + return out; +} + +/** + * Subtracts matrix b from matrix a + * + * @param {mat3} out the receiving matrix + * @param {mat3} a the first operand + * @param {mat3} b the second operand + * @returns {mat3} out + */ +function subtract(out, a, b) { + out[0] = a[0] - b[0]; + out[1] = a[1] - b[1]; + out[2] = a[2] - b[2]; + out[3] = a[3] - b[3]; + out[4] = a[4] - b[4]; + out[5] = a[5] - b[5]; + out[6] = a[6] - b[6]; + out[7] = a[7] - b[7]; + out[8] = a[8] - b[8]; + return out; +} + +/** + * Multiply each element of the matrix by a scalar. + * + * @param {mat3} out the receiving matrix + * @param {mat3} a the matrix to scale + * @param {Number} b amount to scale the matrix's elements by + * @returns {mat3} out + */ +function multiplyScalar(out, a, b) { + out[0] = a[0] * b; + out[1] = a[1] * b; + out[2] = a[2] * b; + out[3] = a[3] * b; + out[4] = a[4] * b; + out[5] = a[5] * b; + out[6] = a[6] * b; + out[7] = a[7] * b; + out[8] = a[8] * b; + return out; +} + +/** + * Adds two mat3's after multiplying each element of the second operand by a scalar value. + * + * @param {mat3} out the receiving vector + * @param {mat3} a the first operand + * @param {mat3} b the second operand + * @param {Number} scale the amount to scale b's elements by before adding + * @returns {mat3} out + */ +function multiplyScalarAndAdd(out, a, b, scale) { + out[0] = a[0] + b[0] * scale; + out[1] = a[1] + b[1] * scale; + out[2] = a[2] + b[2] * scale; + out[3] = a[3] + b[3] * scale; + out[4] = a[4] + b[4] * scale; + out[5] = a[5] + b[5] * scale; + out[6] = a[6] + b[6] * scale; + out[7] = a[7] + b[7] * scale; + out[8] = a[8] + b[8] * scale; + return out; +} + +/** + * Returns whether or not the matrices have exactly the same elements in the same position (when compared with ===) + * + * @param {mat3} a The first matrix. + * @param {mat3} b The second matrix. + * @returns {Boolean} True if the matrices are equal, false otherwise. + */ +function exactEquals(a, b) { + return a[0] === b[0] && a[1] === b[1] && a[2] === b[2] && a[3] === b[3] && a[4] === b[4] && a[5] === b[5] && a[6] === b[6] && a[7] === b[7] && a[8] === b[8]; +} + +/** + * Returns whether or not the matrices have approximately the same elements in the same position. + * + * @param {mat3} a The first matrix. + * @param {mat3} b The second matrix. + * @returns {Boolean} True if the matrices are equal, false otherwise. + */ +function equals(a, b) { + var a0 = a[0], + a1 = a[1], + a2 = a[2], + a3 = a[3], + a4 = a[4], + a5 = a[5], + a6 = a[6], + a7 = a[7], + a8 = a[8]; + var b0 = b[0], + b1 = b[1], + b2 = b[2], + b3 = b[3], + b4 = b[4], + b5 = b[5], + b6 = b[6], + b7 = b[7], + b8 = b[8]; + return Math.abs(a0 - b0) <= glMatrix.EPSILON * Math.max(1.0, Math.abs(a0), Math.abs(b0)) && Math.abs(a1 - b1) <= glMatrix.EPSILON * Math.max(1.0, Math.abs(a1), Math.abs(b1)) && Math.abs(a2 - b2) <= glMatrix.EPSILON * Math.max(1.0, Math.abs(a2), Math.abs(b2)) && Math.abs(a3 - b3) <= glMatrix.EPSILON * Math.max(1.0, Math.abs(a3), Math.abs(b3)) && Math.abs(a4 - b4) <= glMatrix.EPSILON * Math.max(1.0, Math.abs(a4), Math.abs(b4)) && Math.abs(a5 - b5) <= glMatrix.EPSILON * Math.max(1.0, Math.abs(a5), Math.abs(b5)) && Math.abs(a6 - b6) <= glMatrix.EPSILON * Math.max(1.0, Math.abs(a6), Math.abs(b6)) && Math.abs(a7 - b7) <= glMatrix.EPSILON * Math.max(1.0, Math.abs(a7), Math.abs(b7)) && Math.abs(a8 - b8) <= glMatrix.EPSILON * Math.max(1.0, Math.abs(a8), Math.abs(b8)); +} + +/** + * Alias for {@link mat3.multiply} + * @function + */ +var mul = exports.mul = multiply; + +/** + * Alias for {@link mat3.subtract} + * @function + */ +var sub = exports.sub = subtract; + +/***/ }), +/* 2 */ +/***/ (function(module, exports, __webpack_require__) { + +"use strict"; + + +Object.defineProperty(exports, "__esModule", { + value: true +}); +exports.forEach = exports.sqrLen = exports.len = exports.sqrDist = exports.dist = exports.div = exports.mul = exports.sub = undefined; +exports.create = create; +exports.clone = clone; +exports.length = length; +exports.fromValues = fromValues; +exports.copy = copy; +exports.set = set; +exports.add = add; +exports.subtract = subtract; +exports.multiply = multiply; +exports.divide = divide; +exports.ceil = ceil; +exports.floor = floor; +exports.min = min; +exports.max = max; +exports.round = round; +exports.scale = scale; +exports.scaleAndAdd = scaleAndAdd; +exports.distance = distance; +exports.squaredDistance = squaredDistance; +exports.squaredLength = squaredLength; +exports.negate = negate; +exports.inverse = inverse; +exports.normalize = normalize; +exports.dot = dot; +exports.cross = cross; +exports.lerp = lerp; +exports.hermite = hermite; +exports.bezier = bezier; +exports.random = random; +exports.transformMat4 = transformMat4; +exports.transformMat3 = transformMat3; +exports.transformQuat = transformQuat; +exports.rotateX = rotateX; +exports.rotateY = rotateY; +exports.rotateZ = rotateZ; +exports.angle = angle; +exports.str = str; +exports.exactEquals = exactEquals; +exports.equals = equals; + +var _common = __webpack_require__(0); + +var glMatrix = _interopRequireWildcard(_common); + +function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) newObj[key] = obj[key]; } } newObj.default = obj; return newObj; } } + +/** + * 3 Dimensional Vector + * @module vec3 + */ + +/** + * Creates a new, empty vec3 + * + * @returns {vec3} a new 3D vector + */ +function create() { + var out = new glMatrix.ARRAY_TYPE(3); + out[0] = 0; + out[1] = 0; + out[2] = 0; + return out; +} + +/** + * Creates a new vec3 initialized with values from an existing vector + * + * @param {vec3} a vector to clone + * @returns {vec3} a new 3D vector + */ +/* Copyright (c) 2015, Brandon Jones, Colin MacKenzie IV. + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in +all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +THE SOFTWARE. */ + +function clone(a) { + var out = new glMatrix.ARRAY_TYPE(3); + out[0] = a[0]; + out[1] = a[1]; + out[2] = a[2]; + return out; +} + +/** + * Calculates the length of a vec3 + * + * @param {vec3} a vector to calculate length of + * @returns {Number} length of a + */ +function length(a) { + var x = a[0]; + var y = a[1]; + var z = a[2]; + return Math.sqrt(x * x + y * y + z * z); +} + +/** + * Creates a new vec3 initialized with the given values + * + * @param {Number} x X component + * @param {Number} y Y component + * @param {Number} z Z component + * @returns {vec3} a new 3D vector + */ +function fromValues(x, y, z) { + var out = new glMatrix.ARRAY_TYPE(3); + out[0] = x; + out[1] = y; + out[2] = z; + return out; +} + +/** + * Copy the values from one vec3 to another + * + * @param {vec3} out the receiving vector + * @param {vec3} a the source vector + * @returns {vec3} out + */ +function copy(out, a) { + out[0] = a[0]; + out[1] = a[1]; + out[2] = a[2]; + return out; +} + +/** + * Set the components of a vec3 to the given values + * + * @param {vec3} out the receiving vector + * @param {Number} x X component + * @param {Number} y Y component + * @param {Number} z Z component + * @returns {vec3} out + */ +function set(out, x, y, z) { + out[0] = x; + out[1] = y; + out[2] = z; + return out; +} + +/** + * Adds two vec3's + * + * @param {vec3} out the receiving vector + * @param {vec3} a the first operand + * @param {vec3} b the second operand + * @returns {vec3} out + */ +function add(out, a, b) { + out[0] = a[0] + b[0]; + out[1] = a[1] + b[1]; + out[2] = a[2] + b[2]; + return out; +} + +/** + * Subtracts vector b from vector a + * + * @param {vec3} out the receiving vector + * @param {vec3} a the first operand + * @param {vec3} b the second operand + * @returns {vec3} out + */ +function subtract(out, a, b) { + out[0] = a[0] - b[0]; + out[1] = a[1] - b[1]; + out[2] = a[2] - b[2]; + return out; +} + +/** + * Multiplies two vec3's + * + * @param {vec3} out the receiving vector + * @param {vec3} a the first operand + * @param {vec3} b the second operand + * @returns {vec3} out + */ +function multiply(out, a, b) { + out[0] = a[0] * b[0]; + out[1] = a[1] * b[1]; + out[2] = a[2] * b[2]; + return out; +} + +/** + * Divides two vec3's + * + * @param {vec3} out the receiving vector + * @param {vec3} a the first operand + * @param {vec3} b the second operand + * @returns {vec3} out + */ +function divide(out, a, b) { + out[0] = a[0] / b[0]; + out[1] = a[1] / b[1]; + out[2] = a[2] / b[2]; + return out; +} + +/** + * Math.ceil the components of a vec3 + * + * @param {vec3} out the receiving vector + * @param {vec3} a vector to ceil + * @returns {vec3} out + */ +function ceil(out, a) { + out[0] = Math.ceil(a[0]); + out[1] = Math.ceil(a[1]); + out[2] = Math.ceil(a[2]); + return out; +} + +/** + * Math.floor the components of a vec3 + * + * @param {vec3} out the receiving vector + * @param {vec3} a vector to floor + * @returns {vec3} out + */ +function floor(out, a) { + out[0] = Math.floor(a[0]); + out[1] = Math.floor(a[1]); + out[2] = Math.floor(a[2]); + return out; +} + +/** + * Returns the minimum of two vec3's + * + * @param {vec3} out the receiving vector + * @param {vec3} a the first operand + * @param {vec3} b the second operand + * @returns {vec3} out + */ +function min(out, a, b) { + out[0] = Math.min(a[0], b[0]); + out[1] = Math.min(a[1], b[1]); + out[2] = Math.min(a[2], b[2]); + return out; +} + +/** + * Returns the maximum of two vec3's + * + * @param {vec3} out the receiving vector + * @param {vec3} a the first operand + * @param {vec3} b the second operand + * @returns {vec3} out + */ +function max(out, a, b) { + out[0] = Math.max(a[0], b[0]); + out[1] = Math.max(a[1], b[1]); + out[2] = Math.max(a[2], b[2]); + return out; +} + +/** + * Math.round the components of a vec3 + * + * @param {vec3} out the receiving vector + * @param {vec3} a vector to round + * @returns {vec3} out + */ +function round(out, a) { + out[0] = Math.round(a[0]); + out[1] = Math.round(a[1]); + out[2] = Math.round(a[2]); + return out; +} + +/** + * Scales a vec3 by a scalar number + * + * @param {vec3} out the receiving vector + * @param {vec3} a the vector to scale + * @param {Number} b amount to scale the vector by + * @returns {vec3} out + */ +function scale(out, a, b) { + out[0] = a[0] * b; + out[1] = a[1] * b; + out[2] = a[2] * b; + return out; +} + +/** + * Adds two vec3's after scaling the second operand by a scalar value + * + * @param {vec3} out the receiving vector + * @param {vec3} a the first operand + * @param {vec3} b the second operand + * @param {Number} scale the amount to scale b by before adding + * @returns {vec3} out + */ +function scaleAndAdd(out, a, b, scale) { + out[0] = a[0] + b[0] * scale; + out[1] = a[1] + b[1] * scale; + out[2] = a[2] + b[2] * scale; + return out; +} + +/** + * Calculates the euclidian distance between two vec3's + * + * @param {vec3} a the first operand + * @param {vec3} b the second operand + * @returns {Number} distance between a and b + */ +function distance(a, b) { + var x = b[0] - a[0]; + var y = b[1] - a[1]; + var z = b[2] - a[2]; + return Math.sqrt(x * x + y * y + z * z); +} + +/** + * Calculates the squared euclidian distance between two vec3's + * + * @param {vec3} a the first operand + * @param {vec3} b the second operand + * @returns {Number} squared distance between a and b + */ +function squaredDistance(a, b) { + var x = b[0] - a[0]; + var y = b[1] - a[1]; + var z = b[2] - a[2]; + return x * x + y * y + z * z; +} + +/** + * Calculates the squared length of a vec3 + * + * @param {vec3} a vector to calculate squared length of + * @returns {Number} squared length of a + */ +function squaredLength(a) { + var x = a[0]; + var y = a[1]; + var z = a[2]; + return x * x + y * y + z * z; +} + +/** + * Negates the components of a vec3 + * + * @param {vec3} out the receiving vector + * @param {vec3} a vector to negate + * @returns {vec3} out + */ +function negate(out, a) { + out[0] = -a[0]; + out[1] = -a[1]; + out[2] = -a[2]; + return out; +} + +/** + * Returns the inverse of the components of a vec3 + * + * @param {vec3} out the receiving vector + * @param {vec3} a vector to invert + * @returns {vec3} out + */ +function inverse(out, a) { + out[0] = 1.0 / a[0]; + out[1] = 1.0 / a[1]; + out[2] = 1.0 / a[2]; + return out; +} + +/** + * Normalize a vec3 + * + * @param {vec3} out the receiving vector + * @param {vec3} a vector to normalize + * @returns {vec3} out + */ +function normalize(out, a) { + var x = a[0]; + var y = a[1]; + var z = a[2]; + var len = x * x + y * y + z * z; + if (len > 0) { + //TODO: evaluate use of glm_invsqrt here? + len = 1 / Math.sqrt(len); + out[0] = a[0] * len; + out[1] = a[1] * len; + out[2] = a[2] * len; + } + return out; +} + +/** + * Calculates the dot product of two vec3's + * + * @param {vec3} a the first operand + * @param {vec3} b the second operand + * @returns {Number} dot product of a and b + */ +function dot(a, b) { + return a[0] * b[0] + a[1] * b[1] + a[2] * b[2]; +} + +/** + * Computes the cross product of two vec3's + * + * @param {vec3} out the receiving vector + * @param {vec3} a the first operand + * @param {vec3} b the second operand + * @returns {vec3} out + */ +function cross(out, a, b) { + var ax = a[0], + ay = a[1], + az = a[2]; + var bx = b[0], + by = b[1], + bz = b[2]; + + out[0] = ay * bz - az * by; + out[1] = az * bx - ax * bz; + out[2] = ax * by - ay * bx; + return out; +} + +/** + * Performs a linear interpolation between two vec3's + * + * @param {vec3} out the receiving vector + * @param {vec3} a the first operand + * @param {vec3} b the second operand + * @param {Number} t interpolation amount between the two inputs + * @returns {vec3} out + */ +function lerp(out, a, b, t) { + var ax = a[0]; + var ay = a[1]; + var az = a[2]; + out[0] = ax + t * (b[0] - ax); + out[1] = ay + t * (b[1] - ay); + out[2] = az + t * (b[2] - az); + return out; +} + +/** + * Performs a hermite interpolation with two control points + * + * @param {vec3} out the receiving vector + * @param {vec3} a the first operand + * @param {vec3} b the second operand + * @param {vec3} c the third operand + * @param {vec3} d the fourth operand + * @param {Number} t interpolation amount between the two inputs + * @returns {vec3} out + */ +function hermite(out, a, b, c, d, t) { + var factorTimes2 = t * t; + var factor1 = factorTimes2 * (2 * t - 3) + 1; + var factor2 = factorTimes2 * (t - 2) + t; + var factor3 = factorTimes2 * (t - 1); + var factor4 = factorTimes2 * (3 - 2 * t); + + out[0] = a[0] * factor1 + b[0] * factor2 + c[0] * factor3 + d[0] * factor4; + out[1] = a[1] * factor1 + b[1] * factor2 + c[1] * factor3 + d[1] * factor4; + out[2] = a[2] * factor1 + b[2] * factor2 + c[2] * factor3 + d[2] * factor4; + + return out; +} + +/** + * Performs a bezier interpolation with two control points + * + * @param {vec3} out the receiving vector + * @param {vec3} a the first operand + * @param {vec3} b the second operand + * @param {vec3} c the third operand + * @param {vec3} d the fourth operand + * @param {Number} t interpolation amount between the two inputs + * @returns {vec3} out + */ +function bezier(out, a, b, c, d, t) { + var inverseFactor = 1 - t; + var inverseFactorTimesTwo = inverseFactor * inverseFactor; + var factorTimes2 = t * t; + var factor1 = inverseFactorTimesTwo * inverseFactor; + var factor2 = 3 * t * inverseFactorTimesTwo; + var factor3 = 3 * factorTimes2 * inverseFactor; + var factor4 = factorTimes2 * t; + + out[0] = a[0] * factor1 + b[0] * factor2 + c[0] * factor3 + d[0] * factor4; + out[1] = a[1] * factor1 + b[1] * factor2 + c[1] * factor3 + d[1] * factor4; + out[2] = a[2] * factor1 + b[2] * factor2 + c[2] * factor3 + d[2] * factor4; + + return out; +} + +/** + * Generates a random vector with the given scale + * + * @param {vec3} out the receiving vector + * @param {Number} [scale] Length of the resulting vector. If ommitted, a unit vector will be returned + * @returns {vec3} out + */ +function random(out, scale) { + scale = scale || 1.0; + + var r = glMatrix.RANDOM() * 2.0 * Math.PI; + var z = glMatrix.RANDOM() * 2.0 - 1.0; + var zScale = Math.sqrt(1.0 - z * z) * scale; + + out[0] = Math.cos(r) * zScale; + out[1] = Math.sin(r) * zScale; + out[2] = z * scale; + return out; +} + +/** + * Transforms the vec3 with a mat4. + * 4th vector component is implicitly '1' + * + * @param {vec3} out the receiving vector + * @param {vec3} a the vector to transform + * @param {mat4} m matrix to transform with + * @returns {vec3} out + */ +function transformMat4(out, a, m) { + var x = a[0], + y = a[1], + z = a[2]; + var w = m[3] * x + m[7] * y + m[11] * z + m[15]; + w = w || 1.0; + out[0] = (m[0] * x + m[4] * y + m[8] * z + m[12]) / w; + out[1] = (m[1] * x + m[5] * y + m[9] * z + m[13]) / w; + out[2] = (m[2] * x + m[6] * y + m[10] * z + m[14]) / w; + return out; +} + +/** + * Transforms the vec3 with a mat3. + * + * @param {vec3} out the receiving vector + * @param {vec3} a the vector to transform + * @param {mat3} m the 3x3 matrix to transform with + * @returns {vec3} out + */ +function transformMat3(out, a, m) { + var x = a[0], + y = a[1], + z = a[2]; + out[0] = x * m[0] + y * m[3] + z * m[6]; + out[1] = x * m[1] + y * m[4] + z * m[7]; + out[2] = x * m[2] + y * m[5] + z * m[8]; + return out; +} + +/** + * Transforms the vec3 with a quat + * + * @param {vec3} out the receiving vector + * @param {vec3} a the vector to transform + * @param {quat} q quaternion to transform with + * @returns {vec3} out + */ +function transformQuat(out, a, q) { + // benchmarks: http://jsperf.com/quaternion-transform-vec3-implementations + + var x = a[0], + y = a[1], + z = a[2]; + var qx = q[0], + qy = q[1], + qz = q[2], + qw = q[3]; + + // calculate quat * vec + var ix = qw * x + qy * z - qz * y; + var iy = qw * y + qz * x - qx * z; + var iz = qw * z + qx * y - qy * x; + var iw = -qx * x - qy * y - qz * z; + + // calculate result * inverse quat + out[0] = ix * qw + iw * -qx + iy * -qz - iz * -qy; + out[1] = iy * qw + iw * -qy + iz * -qx - ix * -qz; + out[2] = iz * qw + iw * -qz + ix * -qy - iy * -qx; + return out; +} + +/** + * Rotate a 3D vector around the x-axis + * @param {vec3} out The receiving vec3 + * @param {vec3} a The vec3 point to rotate + * @param {vec3} b The origin of the rotation + * @param {Number} c The angle of rotation + * @returns {vec3} out + */ +function rotateX(out, a, b, c) { + var p = [], + r = []; + //Translate point to the origin + p[0] = a[0] - b[0]; + p[1] = a[1] - b[1]; + p[2] = a[2] - b[2]; + + //perform rotation + r[0] = p[0]; + r[1] = p[1] * Math.cos(c) - p[2] * Math.sin(c); + r[2] = p[1] * Math.sin(c) + p[2] * Math.cos(c); + + //translate to correct position + out[0] = r[0] + b[0]; + out[1] = r[1] + b[1]; + out[2] = r[2] + b[2]; + + return out; +} + +/** + * Rotate a 3D vector around the y-axis + * @param {vec3} out The receiving vec3 + * @param {vec3} a The vec3 point to rotate + * @param {vec3} b The origin of the rotation + * @param {Number} c The angle of rotation + * @returns {vec3} out + */ +function rotateY(out, a, b, c) { + var p = [], + r = []; + //Translate point to the origin + p[0] = a[0] - b[0]; + p[1] = a[1] - b[1]; + p[2] = a[2] - b[2]; + + //perform rotation + r[0] = p[2] * Math.sin(c) + p[0] * Math.cos(c); + r[1] = p[1]; + r[2] = p[2] * Math.cos(c) - p[0] * Math.sin(c); + + //translate to correct position + out[0] = r[0] + b[0]; + out[1] = r[1] + b[1]; + out[2] = r[2] + b[2]; + + return out; +} + +/** + * Rotate a 3D vector around the z-axis + * @param {vec3} out The receiving vec3 + * @param {vec3} a The vec3 point to rotate + * @param {vec3} b The origin of the rotation + * @param {Number} c The angle of rotation + * @returns {vec3} out + */ +function rotateZ(out, a, b, c) { + var p = [], + r = []; + //Translate point to the origin + p[0] = a[0] - b[0]; + p[1] = a[1] - b[1]; + p[2] = a[2] - b[2]; + + //perform rotation + r[0] = p[0] * Math.cos(c) - p[1] * Math.sin(c); + r[1] = p[0] * Math.sin(c) + p[1] * Math.cos(c); + r[2] = p[2]; + + //translate to correct position + out[0] = r[0] + b[0]; + out[1] = r[1] + b[1]; + out[2] = r[2] + b[2]; + + return out; +} + +/** + * Get the angle between two 3D vectors + * @param {vec3} a The first operand + * @param {vec3} b The second operand + * @returns {Number} The angle in radians + */ +function angle(a, b) { + var tempA = fromValues(a[0], a[1], a[2]); + var tempB = fromValues(b[0], b[1], b[2]); + + normalize(tempA, tempA); + normalize(tempB, tempB); + + var cosine = dot(tempA, tempB); + + if (cosine > 1.0) { + return 0; + } else if (cosine < -1.0) { + return Math.PI; + } else { + return Math.acos(cosine); + } +} + +/** + * Returns a string representation of a vector + * + * @param {vec3} a vector to represent as a string + * @returns {String} string representation of the vector + */ +function str(a) { + return 'vec3(' + a[0] + ', ' + a[1] + ', ' + a[2] + ')'; +} + +/** + * Returns whether or not the vectors have exactly the same elements in the same position (when compared with ===) + * + * @param {vec3} a The first vector. + * @param {vec3} b The second vector. + * @returns {Boolean} True if the vectors are equal, false otherwise. + */ +function exactEquals(a, b) { + return a[0] === b[0] && a[1] === b[1] && a[2] === b[2]; +} + +/** + * Returns whether or not the vectors have approximately the same elements in the same position. + * + * @param {vec3} a The first vector. + * @param {vec3} b The second vector. + * @returns {Boolean} True if the vectors are equal, false otherwise. + */ +function equals(a, b) { + var a0 = a[0], + a1 = a[1], + a2 = a[2]; + var b0 = b[0], + b1 = b[1], + b2 = b[2]; + return Math.abs(a0 - b0) <= glMatrix.EPSILON * Math.max(1.0, Math.abs(a0), Math.abs(b0)) && Math.abs(a1 - b1) <= glMatrix.EPSILON * Math.max(1.0, Math.abs(a1), Math.abs(b1)) && Math.abs(a2 - b2) <= glMatrix.EPSILON * Math.max(1.0, Math.abs(a2), Math.abs(b2)); +} + +/** + * Alias for {@link vec3.subtract} + * @function + */ +var sub = exports.sub = subtract; + +/** + * Alias for {@link vec3.multiply} + * @function + */ +var mul = exports.mul = multiply; + +/** + * Alias for {@link vec3.divide} + * @function + */ +var div = exports.div = divide; + +/** + * Alias for {@link vec3.distance} + * @function + */ +var dist = exports.dist = distance; + +/** + * Alias for {@link vec3.squaredDistance} + * @function + */ +var sqrDist = exports.sqrDist = squaredDistance; + +/** + * Alias for {@link vec3.length} + * @function + */ +var len = exports.len = length; + +/** + * Alias for {@link vec3.squaredLength} + * @function + */ +var sqrLen = exports.sqrLen = squaredLength; + +/** + * Perform some operation over an array of vec3s. + * + * @param {Array} a the array of vectors to iterate over + * @param {Number} stride Number of elements between the start of each vec3. If 0 assumes tightly packed + * @param {Number} offset Number of elements to skip at the beginning of the array + * @param {Number} count Number of vec3s to iterate over. If 0 iterates over entire array + * @param {Function} fn Function to call for each vector in the array + * @param {Object} [arg] additional argument to pass to fn + * @returns {Array} a + * @function + */ +var forEach = exports.forEach = function () { + var vec = create(); + + return function (a, stride, offset, count, fn, arg) { + var i = void 0, + l = void 0; + if (!stride) { + stride = 3; + } + + if (!offset) { + offset = 0; + } + + if (count) { + l = Math.min(count * stride + offset, a.length); + } else { + l = a.length; + } + + for (i = offset; i < l; i += stride) { + vec[0] = a[i];vec[1] = a[i + 1];vec[2] = a[i + 2]; + fn(vec, vec, arg); + a[i] = vec[0];a[i + 1] = vec[1];a[i + 2] = vec[2]; + } + + return a; + }; +}(); + +/***/ }), +/* 3 */ +/***/ (function(module, exports, __webpack_require__) { + +"use strict"; + + +Object.defineProperty(exports, "__esModule", { + value: true +}); +exports.forEach = exports.sqrLen = exports.len = exports.sqrDist = exports.dist = exports.div = exports.mul = exports.sub = undefined; +exports.create = create; +exports.clone = clone; +exports.fromValues = fromValues; +exports.copy = copy; +exports.set = set; +exports.add = add; +exports.subtract = subtract; +exports.multiply = multiply; +exports.divide = divide; +exports.ceil = ceil; +exports.floor = floor; +exports.min = min; +exports.max = max; +exports.round = round; +exports.scale = scale; +exports.scaleAndAdd = scaleAndAdd; +exports.distance = distance; +exports.squaredDistance = squaredDistance; +exports.length = length; +exports.squaredLength = squaredLength; +exports.negate = negate; +exports.inverse = inverse; +exports.normalize = normalize; +exports.dot = dot; +exports.lerp = lerp; +exports.random = random; +exports.transformMat4 = transformMat4; +exports.transformQuat = transformQuat; +exports.str = str; +exports.exactEquals = exactEquals; +exports.equals = equals; + +var _common = __webpack_require__(0); + +var glMatrix = _interopRequireWildcard(_common); + +function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) newObj[key] = obj[key]; } } newObj.default = obj; return newObj; } } + +/** + * 4 Dimensional Vector + * @module vec4 + */ + +/** + * Creates a new, empty vec4 + * + * @returns {vec4} a new 4D vector + */ +function create() { + var out = new glMatrix.ARRAY_TYPE(4); + out[0] = 0; + out[1] = 0; + out[2] = 0; + out[3] = 0; + return out; +} + +/** + * Creates a new vec4 initialized with values from an existing vector + * + * @param {vec4} a vector to clone + * @returns {vec4} a new 4D vector + */ +/* Copyright (c) 2015, Brandon Jones, Colin MacKenzie IV. + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in +all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +THE SOFTWARE. */ + +function clone(a) { + var out = new glMatrix.ARRAY_TYPE(4); + out[0] = a[0]; + out[1] = a[1]; + out[2] = a[2]; + out[3] = a[3]; + return out; +} + +/** + * Creates a new vec4 initialized with the given values + * + * @param {Number} x X component + * @param {Number} y Y component + * @param {Number} z Z component + * @param {Number} w W component + * @returns {vec4} a new 4D vector + */ +function fromValues(x, y, z, w) { + var out = new glMatrix.ARRAY_TYPE(4); + out[0] = x; + out[1] = y; + out[2] = z; + out[3] = w; + return out; +} + +/** + * Copy the values from one vec4 to another + * + * @param {vec4} out the receiving vector + * @param {vec4} a the source vector + * @returns {vec4} out + */ +function copy(out, a) { + out[0] = a[0]; + out[1] = a[1]; + out[2] = a[2]; + out[3] = a[3]; + return out; +} + +/** + * Set the components of a vec4 to the given values + * + * @param {vec4} out the receiving vector + * @param {Number} x X component + * @param {Number} y Y component + * @param {Number} z Z component + * @param {Number} w W component + * @returns {vec4} out + */ +function set(out, x, y, z, w) { + out[0] = x; + out[1] = y; + out[2] = z; + out[3] = w; + return out; +} + +/** + * Adds two vec4's + * + * @param {vec4} out the receiving vector + * @param {vec4} a the first operand + * @param {vec4} b the second operand + * @returns {vec4} out + */ +function add(out, a, b) { + out[0] = a[0] + b[0]; + out[1] = a[1] + b[1]; + out[2] = a[2] + b[2]; + out[3] = a[3] + b[3]; + return out; +} + +/** + * Subtracts vector b from vector a + * + * @param {vec4} out the receiving vector + * @param {vec4} a the first operand + * @param {vec4} b the second operand + * @returns {vec4} out + */ +function subtract(out, a, b) { + out[0] = a[0] - b[0]; + out[1] = a[1] - b[1]; + out[2] = a[2] - b[2]; + out[3] = a[3] - b[3]; + return out; +} + +/** + * Multiplies two vec4's + * + * @param {vec4} out the receiving vector + * @param {vec4} a the first operand + * @param {vec4} b the second operand + * @returns {vec4} out + */ +function multiply(out, a, b) { + out[0] = a[0] * b[0]; + out[1] = a[1] * b[1]; + out[2] = a[2] * b[2]; + out[3] = a[3] * b[3]; + return out; +} + +/** + * Divides two vec4's + * + * @param {vec4} out the receiving vector + * @param {vec4} a the first operand + * @param {vec4} b the second operand + * @returns {vec4} out + */ +function divide(out, a, b) { + out[0] = a[0] / b[0]; + out[1] = a[1] / b[1]; + out[2] = a[2] / b[2]; + out[3] = a[3] / b[3]; + return out; +} + +/** + * Math.ceil the components of a vec4 + * + * @param {vec4} out the receiving vector + * @param {vec4} a vector to ceil + * @returns {vec4} out + */ +function ceil(out, a) { + out[0] = Math.ceil(a[0]); + out[1] = Math.ceil(a[1]); + out[2] = Math.ceil(a[2]); + out[3] = Math.ceil(a[3]); + return out; +} + +/** + * Math.floor the components of a vec4 + * + * @param {vec4} out the receiving vector + * @param {vec4} a vector to floor + * @returns {vec4} out + */ +function floor(out, a) { + out[0] = Math.floor(a[0]); + out[1] = Math.floor(a[1]); + out[2] = Math.floor(a[2]); + out[3] = Math.floor(a[3]); + return out; +} + +/** + * Returns the minimum of two vec4's + * + * @param {vec4} out the receiving vector + * @param {vec4} a the first operand + * @param {vec4} b the second operand + * @returns {vec4} out + */ +function min(out, a, b) { + out[0] = Math.min(a[0], b[0]); + out[1] = Math.min(a[1], b[1]); + out[2] = Math.min(a[2], b[2]); + out[3] = Math.min(a[3], b[3]); + return out; +} + +/** + * Returns the maximum of two vec4's + * + * @param {vec4} out the receiving vector + * @param {vec4} a the first operand + * @param {vec4} b the second operand + * @returns {vec4} out + */ +function max(out, a, b) { + out[0] = Math.max(a[0], b[0]); + out[1] = Math.max(a[1], b[1]); + out[2] = Math.max(a[2], b[2]); + out[3] = Math.max(a[3], b[3]); + return out; +} + +/** + * Math.round the components of a vec4 + * + * @param {vec4} out the receiving vector + * @param {vec4} a vector to round + * @returns {vec4} out + */ +function round(out, a) { + out[0] = Math.round(a[0]); + out[1] = Math.round(a[1]); + out[2] = Math.round(a[2]); + out[3] = Math.round(a[3]); + return out; +} + +/** + * Scales a vec4 by a scalar number + * + * @param {vec4} out the receiving vector + * @param {vec4} a the vector to scale + * @param {Number} b amount to scale the vector by + * @returns {vec4} out + */ +function scale(out, a, b) { + out[0] = a[0] * b; + out[1] = a[1] * b; + out[2] = a[2] * b; + out[3] = a[3] * b; + return out; +} + +/** + * Adds two vec4's after scaling the second operand by a scalar value + * + * @param {vec4} out the receiving vector + * @param {vec4} a the first operand + * @param {vec4} b the second operand + * @param {Number} scale the amount to scale b by before adding + * @returns {vec4} out + */ +function scaleAndAdd(out, a, b, scale) { + out[0] = a[0] + b[0] * scale; + out[1] = a[1] + b[1] * scale; + out[2] = a[2] + b[2] * scale; + out[3] = a[3] + b[3] * scale; + return out; +} + +/** + * Calculates the euclidian distance between two vec4's + * + * @param {vec4} a the first operand + * @param {vec4} b the second operand + * @returns {Number} distance between a and b + */ +function distance(a, b) { + var x = b[0] - a[0]; + var y = b[1] - a[1]; + var z = b[2] - a[2]; + var w = b[3] - a[3]; + return Math.sqrt(x * x + y * y + z * z + w * w); +} + +/** + * Calculates the squared euclidian distance between two vec4's + * + * @param {vec4} a the first operand + * @param {vec4} b the second operand + * @returns {Number} squared distance between a and b + */ +function squaredDistance(a, b) { + var x = b[0] - a[0]; + var y = b[1] - a[1]; + var z = b[2] - a[2]; + var w = b[3] - a[3]; + return x * x + y * y + z * z + w * w; +} + +/** + * Calculates the length of a vec4 + * + * @param {vec4} a vector to calculate length of + * @returns {Number} length of a + */ +function length(a) { + var x = a[0]; + var y = a[1]; + var z = a[2]; + var w = a[3]; + return Math.sqrt(x * x + y * y + z * z + w * w); +} + +/** + * Calculates the squared length of a vec4 + * + * @param {vec4} a vector to calculate squared length of + * @returns {Number} squared length of a + */ +function squaredLength(a) { + var x = a[0]; + var y = a[1]; + var z = a[2]; + var w = a[3]; + return x * x + y * y + z * z + w * w; +} + +/** + * Negates the components of a vec4 + * + * @param {vec4} out the receiving vector + * @param {vec4} a vector to negate + * @returns {vec4} out + */ +function negate(out, a) { + out[0] = -a[0]; + out[1] = -a[1]; + out[2] = -a[2]; + out[3] = -a[3]; + return out; +} + +/** + * Returns the inverse of the components of a vec4 + * + * @param {vec4} out the receiving vector + * @param {vec4} a vector to invert + * @returns {vec4} out + */ +function inverse(out, a) { + out[0] = 1.0 / a[0]; + out[1] = 1.0 / a[1]; + out[2] = 1.0 / a[2]; + out[3] = 1.0 / a[3]; + return out; +} + +/** + * Normalize a vec4 + * + * @param {vec4} out the receiving vector + * @param {vec4} a vector to normalize + * @returns {vec4} out + */ +function normalize(out, a) { + var x = a[0]; + var y = a[1]; + var z = a[2]; + var w = a[3]; + var len = x * x + y * y + z * z + w * w; + if (len > 0) { + len = 1 / Math.sqrt(len); + out[0] = x * len; + out[1] = y * len; + out[2] = z * len; + out[3] = w * len; + } + return out; +} + +/** + * Calculates the dot product of two vec4's + * + * @param {vec4} a the first operand + * @param {vec4} b the second operand + * @returns {Number} dot product of a and b + */ +function dot(a, b) { + return a[0] * b[0] + a[1] * b[1] + a[2] * b[2] + a[3] * b[3]; +} + +/** + * Performs a linear interpolation between two vec4's + * + * @param {vec4} out the receiving vector + * @param {vec4} a the first operand + * @param {vec4} b the second operand + * @param {Number} t interpolation amount between the two inputs + * @returns {vec4} out + */ +function lerp(out, a, b, t) { + var ax = a[0]; + var ay = a[1]; + var az = a[2]; + var aw = a[3]; + out[0] = ax + t * (b[0] - ax); + out[1] = ay + t * (b[1] - ay); + out[2] = az + t * (b[2] - az); + out[3] = aw + t * (b[3] - aw); + return out; +} + +/** + * Generates a random vector with the given scale + * + * @param {vec4} out the receiving vector + * @param {Number} [scale] Length of the resulting vector. If ommitted, a unit vector will be returned + * @returns {vec4} out + */ +function random(out, vectorScale) { + vectorScale = vectorScale || 1.0; + + //TODO: This is a pretty awful way of doing this. Find something better. + out[0] = glMatrix.RANDOM(); + out[1] = glMatrix.RANDOM(); + out[2] = glMatrix.RANDOM(); + out[3] = glMatrix.RANDOM(); + normalize(out, out); + scale(out, out, vectorScale); + return out; +} + +/** + * Transforms the vec4 with a mat4. + * + * @param {vec4} out the receiving vector + * @param {vec4} a the vector to transform + * @param {mat4} m matrix to transform with + * @returns {vec4} out + */ +function transformMat4(out, a, m) { + var x = a[0], + y = a[1], + z = a[2], + w = a[3]; + out[0] = m[0] * x + m[4] * y + m[8] * z + m[12] * w; + out[1] = m[1] * x + m[5] * y + m[9] * z + m[13] * w; + out[2] = m[2] * x + m[6] * y + m[10] * z + m[14] * w; + out[3] = m[3] * x + m[7] * y + m[11] * z + m[15] * w; + return out; +} + +/** + * Transforms the vec4 with a quat + * + * @param {vec4} out the receiving vector + * @param {vec4} a the vector to transform + * @param {quat} q quaternion to transform with + * @returns {vec4} out + */ +function transformQuat(out, a, q) { + var x = a[0], + y = a[1], + z = a[2]; + var qx = q[0], + qy = q[1], + qz = q[2], + qw = q[3]; + + // calculate quat * vec + var ix = qw * x + qy * z - qz * y; + var iy = qw * y + qz * x - qx * z; + var iz = qw * z + qx * y - qy * x; + var iw = -qx * x - qy * y - qz * z; + + // calculate result * inverse quat + out[0] = ix * qw + iw * -qx + iy * -qz - iz * -qy; + out[1] = iy * qw + iw * -qy + iz * -qx - ix * -qz; + out[2] = iz * qw + iw * -qz + ix * -qy - iy * -qx; + out[3] = a[3]; + return out; +} + +/** + * Returns a string representation of a vector + * + * @param {vec4} a vector to represent as a string + * @returns {String} string representation of the vector + */ +function str(a) { + return 'vec4(' + a[0] + ', ' + a[1] + ', ' + a[2] + ', ' + a[3] + ')'; +} + +/** + * Returns whether or not the vectors have exactly the same elements in the same position (when compared with ===) + * + * @param {vec4} a The first vector. + * @param {vec4} b The second vector. + * @returns {Boolean} True if the vectors are equal, false otherwise. + */ +function exactEquals(a, b) { + return a[0] === b[0] && a[1] === b[1] && a[2] === b[2] && a[3] === b[3]; +} + +/** + * Returns whether or not the vectors have approximately the same elements in the same position. + * + * @param {vec4} a The first vector. + * @param {vec4} b The second vector. + * @returns {Boolean} True if the vectors are equal, false otherwise. + */ +function equals(a, b) { + var a0 = a[0], + a1 = a[1], + a2 = a[2], + a3 = a[3]; + var b0 = b[0], + b1 = b[1], + b2 = b[2], + b3 = b[3]; + return Math.abs(a0 - b0) <= glMatrix.EPSILON * Math.max(1.0, Math.abs(a0), Math.abs(b0)) && Math.abs(a1 - b1) <= glMatrix.EPSILON * Math.max(1.0, Math.abs(a1), Math.abs(b1)) && Math.abs(a2 - b2) <= glMatrix.EPSILON * Math.max(1.0, Math.abs(a2), Math.abs(b2)) && Math.abs(a3 - b3) <= glMatrix.EPSILON * Math.max(1.0, Math.abs(a3), Math.abs(b3)); +} + +/** + * Alias for {@link vec4.subtract} + * @function + */ +var sub = exports.sub = subtract; + +/** + * Alias for {@link vec4.multiply} + * @function + */ +var mul = exports.mul = multiply; + +/** + * Alias for {@link vec4.divide} + * @function + */ +var div = exports.div = divide; + +/** + * Alias for {@link vec4.distance} + * @function + */ +var dist = exports.dist = distance; + +/** + * Alias for {@link vec4.squaredDistance} + * @function + */ +var sqrDist = exports.sqrDist = squaredDistance; + +/** + * Alias for {@link vec4.length} + * @function + */ +var len = exports.len = length; + +/** + * Alias for {@link vec4.squaredLength} + * @function + */ +var sqrLen = exports.sqrLen = squaredLength; + +/** + * Perform some operation over an array of vec4s. + * + * @param {Array} a the array of vectors to iterate over + * @param {Number} stride Number of elements between the start of each vec4. If 0 assumes tightly packed + * @param {Number} offset Number of elements to skip at the beginning of the array + * @param {Number} count Number of vec4s to iterate over. If 0 iterates over entire array + * @param {Function} fn Function to call for each vector in the array + * @param {Object} [arg] additional argument to pass to fn + * @returns {Array} a + * @function + */ +var forEach = exports.forEach = function () { + var vec = create(); + + return function (a, stride, offset, count, fn, arg) { + var i = void 0, + l = void 0; + if (!stride) { + stride = 4; + } + + if (!offset) { + offset = 0; + } + + if (count) { + l = Math.min(count * stride + offset, a.length); + } else { + l = a.length; + } + + for (i = offset; i < l; i += stride) { + vec[0] = a[i];vec[1] = a[i + 1];vec[2] = a[i + 2];vec[3] = a[i + 3]; + fn(vec, vec, arg); + a[i] = vec[0];a[i + 1] = vec[1];a[i + 2] = vec[2];a[i + 3] = vec[3]; + } + + return a; + }; +}(); + +/***/ }), +/* 4 */ +/***/ (function(module, exports, __webpack_require__) { + +"use strict"; + + +Object.defineProperty(exports, "__esModule", { + value: true +}); +exports.vec4 = exports.vec3 = exports.vec2 = exports.quat = exports.mat4 = exports.mat3 = exports.mat2d = exports.mat2 = exports.glMatrix = undefined; + +var _common = __webpack_require__(0); + +var glMatrix = _interopRequireWildcard(_common); + +var _mat = __webpack_require__(5); + +var mat2 = _interopRequireWildcard(_mat); + +var _mat2d = __webpack_require__(6); + +var mat2d = _interopRequireWildcard(_mat2d); + +var _mat2 = __webpack_require__(1); + +var mat3 = _interopRequireWildcard(_mat2); + +var _mat3 = __webpack_require__(7); + +var mat4 = _interopRequireWildcard(_mat3); + +var _quat = __webpack_require__(8); + +var quat = _interopRequireWildcard(_quat); + +var _vec = __webpack_require__(9); + +var vec2 = _interopRequireWildcard(_vec); + +var _vec2 = __webpack_require__(2); + +var vec3 = _interopRequireWildcard(_vec2); + +var _vec3 = __webpack_require__(3); + +var vec4 = _interopRequireWildcard(_vec3); + +function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) newObj[key] = obj[key]; } } newObj.default = obj; return newObj; } } + +exports.glMatrix = glMatrix; +exports.mat2 = mat2; +exports.mat2d = mat2d; +exports.mat3 = mat3; +exports.mat4 = mat4; +exports.quat = quat; +exports.vec2 = vec2; +exports.vec3 = vec3; +exports.vec4 = vec4; /** + * @fileoverview gl-matrix - High performance matrix and vector operations + * @author Brandon Jones + * @author Colin MacKenzie IV + * @version 2.4.0 + */ + +/* Copyright (c) 2015, Brandon Jones, Colin MacKenzie IV. + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in +all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +THE SOFTWARE. */ +// END HEADER + +/***/ }), +/* 5 */ +/***/ (function(module, exports, __webpack_require__) { + +"use strict"; + + +Object.defineProperty(exports, "__esModule", { + value: true +}); +exports.sub = exports.mul = undefined; +exports.create = create; +exports.clone = clone; +exports.copy = copy; +exports.identity = identity; +exports.fromValues = fromValues; +exports.set = set; +exports.transpose = transpose; +exports.invert = invert; +exports.adjoint = adjoint; +exports.determinant = determinant; +exports.multiply = multiply; +exports.rotate = rotate; +exports.scale = scale; +exports.fromRotation = fromRotation; +exports.fromScaling = fromScaling; +exports.str = str; +exports.frob = frob; +exports.LDU = LDU; +exports.add = add; +exports.subtract = subtract; +exports.exactEquals = exactEquals; +exports.equals = equals; +exports.multiplyScalar = multiplyScalar; +exports.multiplyScalarAndAdd = multiplyScalarAndAdd; + +var _common = __webpack_require__(0); + +var glMatrix = _interopRequireWildcard(_common); + +function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) newObj[key] = obj[key]; } } newObj.default = obj; return newObj; } } + +/** + * 2x2 Matrix + * @module mat2 + */ + +/** + * Creates a new identity mat2 + * + * @returns {mat2} a new 2x2 matrix + */ +function create() { + var out = new glMatrix.ARRAY_TYPE(4); + out[0] = 1; + out[1] = 0; + out[2] = 0; + out[3] = 1; + return out; +} + +/** + * Creates a new mat2 initialized with values from an existing matrix + * + * @param {mat2} a matrix to clone + * @returns {mat2} a new 2x2 matrix + */ +/* Copyright (c) 2015, Brandon Jones, Colin MacKenzie IV. + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in +all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +THE SOFTWARE. */ + +function clone(a) { + var out = new glMatrix.ARRAY_TYPE(4); + out[0] = a[0]; + out[1] = a[1]; + out[2] = a[2]; + out[3] = a[3]; + return out; +} + +/** + * Copy the values from one mat2 to another + * + * @param {mat2} out the receiving matrix + * @param {mat2} a the source matrix + * @returns {mat2} out + */ +function copy(out, a) { + out[0] = a[0]; + out[1] = a[1]; + out[2] = a[2]; + out[3] = a[3]; + return out; +} + +/** + * Set a mat2 to the identity matrix + * + * @param {mat2} out the receiving matrix + * @returns {mat2} out + */ +function identity(out) { + out[0] = 1; + out[1] = 0; + out[2] = 0; + out[3] = 1; + return out; +} + +/** + * Create a new mat2 with the given values + * + * @param {Number} m00 Component in column 0, row 0 position (index 0) + * @param {Number} m01 Component in column 0, row 1 position (index 1) + * @param {Number} m10 Component in column 1, row 0 position (index 2) + * @param {Number} m11 Component in column 1, row 1 position (index 3) + * @returns {mat2} out A new 2x2 matrix + */ +function fromValues(m00, m01, m10, m11) { + var out = new glMatrix.ARRAY_TYPE(4); + out[0] = m00; + out[1] = m01; + out[2] = m10; + out[3] = m11; + return out; +} + +/** + * Set the components of a mat2 to the given values + * + * @param {mat2} out the receiving matrix + * @param {Number} m00 Component in column 0, row 0 position (index 0) + * @param {Number} m01 Component in column 0, row 1 position (index 1) + * @param {Number} m10 Component in column 1, row 0 position (index 2) + * @param {Number} m11 Component in column 1, row 1 position (index 3) + * @returns {mat2} out + */ +function set(out, m00, m01, m10, m11) { + out[0] = m00; + out[1] = m01; + out[2] = m10; + out[3] = m11; + return out; +} + +/** + * Transpose the values of a mat2 + * + * @param {mat2} out the receiving matrix + * @param {mat2} a the source matrix + * @returns {mat2} out + */ +function transpose(out, a) { + // If we are transposing ourselves we can skip a few steps but have to cache + // some values + if (out === a) { + var a1 = a[1]; + out[1] = a[2]; + out[2] = a1; + } else { + out[0] = a[0]; + out[1] = a[2]; + out[2] = a[1]; + out[3] = a[3]; + } + + return out; +} + +/** + * Inverts a mat2 + * + * @param {mat2} out the receiving matrix + * @param {mat2} a the source matrix + * @returns {mat2} out + */ +function invert(out, a) { + var a0 = a[0], + a1 = a[1], + a2 = a[2], + a3 = a[3]; + + // Calculate the determinant + var det = a0 * a3 - a2 * a1; + + if (!det) { + return null; + } + det = 1.0 / det; + + out[0] = a3 * det; + out[1] = -a1 * det; + out[2] = -a2 * det; + out[3] = a0 * det; + + return out; +} + +/** + * Calculates the adjugate of a mat2 + * + * @param {mat2} out the receiving matrix + * @param {mat2} a the source matrix + * @returns {mat2} out + */ +function adjoint(out, a) { + // Caching this value is nessecary if out == a + var a0 = a[0]; + out[0] = a[3]; + out[1] = -a[1]; + out[2] = -a[2]; + out[3] = a0; + + return out; +} + +/** + * Calculates the determinant of a mat2 + * + * @param {mat2} a the source matrix + * @returns {Number} determinant of a + */ +function determinant(a) { + return a[0] * a[3] - a[2] * a[1]; +} + +/** + * Multiplies two mat2's + * + * @param {mat2} out the receiving matrix + * @param {mat2} a the first operand + * @param {mat2} b the second operand + * @returns {mat2} out + */ +function multiply(out, a, b) { + var a0 = a[0], + a1 = a[1], + a2 = a[2], + a3 = a[3]; + var b0 = b[0], + b1 = b[1], + b2 = b[2], + b3 = b[3]; + out[0] = a0 * b0 + a2 * b1; + out[1] = a1 * b0 + a3 * b1; + out[2] = a0 * b2 + a2 * b3; + out[3] = a1 * b2 + a3 * b3; + return out; +} + +/** + * Rotates a mat2 by the given angle + * + * @param {mat2} out the receiving matrix + * @param {mat2} a the matrix to rotate + * @param {Number} rad the angle to rotate the matrix by + * @returns {mat2} out + */ +function rotate(out, a, rad) { + var a0 = a[0], + a1 = a[1], + a2 = a[2], + a3 = a[3]; + var s = Math.sin(rad); + var c = Math.cos(rad); + out[0] = a0 * c + a2 * s; + out[1] = a1 * c + a3 * s; + out[2] = a0 * -s + a2 * c; + out[3] = a1 * -s + a3 * c; + return out; +} + +/** + * Scales the mat2 by the dimensions in the given vec2 + * + * @param {mat2} out the receiving matrix + * @param {mat2} a the matrix to rotate + * @param {vec2} v the vec2 to scale the matrix by + * @returns {mat2} out + **/ +function scale(out, a, v) { + var a0 = a[0], + a1 = a[1], + a2 = a[2], + a3 = a[3]; + var v0 = v[0], + v1 = v[1]; + out[0] = a0 * v0; + out[1] = a1 * v0; + out[2] = a2 * v1; + out[3] = a3 * v1; + return out; +} + +/** + * Creates a matrix from a given angle + * This is equivalent to (but much faster than): + * + * mat2.identity(dest); + * mat2.rotate(dest, dest, rad); + * + * @param {mat2} out mat2 receiving operation result + * @param {Number} rad the angle to rotate the matrix by + * @returns {mat2} out + */ +function fromRotation(out, rad) { + var s = Math.sin(rad); + var c = Math.cos(rad); + out[0] = c; + out[1] = s; + out[2] = -s; + out[3] = c; + return out; +} + +/** + * Creates a matrix from a vector scaling + * This is equivalent to (but much faster than): + * + * mat2.identity(dest); + * mat2.scale(dest, dest, vec); + * + * @param {mat2} out mat2 receiving operation result + * @param {vec2} v Scaling vector + * @returns {mat2} out + */ +function fromScaling(out, v) { + out[0] = v[0]; + out[1] = 0; + out[2] = 0; + out[3] = v[1]; + return out; +} + +/** + * Returns a string representation of a mat2 + * + * @param {mat2} a matrix to represent as a string + * @returns {String} string representation of the matrix + */ +function str(a) { + return 'mat2(' + a[0] + ', ' + a[1] + ', ' + a[2] + ', ' + a[3] + ')'; +} + +/** + * Returns Frobenius norm of a mat2 + * + * @param {mat2} a the matrix to calculate Frobenius norm of + * @returns {Number} Frobenius norm + */ +function frob(a) { + return Math.sqrt(Math.pow(a[0], 2) + Math.pow(a[1], 2) + Math.pow(a[2], 2) + Math.pow(a[3], 2)); +} + +/** + * Returns L, D and U matrices (Lower triangular, Diagonal and Upper triangular) by factorizing the input matrix + * @param {mat2} L the lower triangular matrix + * @param {mat2} D the diagonal matrix + * @param {mat2} U the upper triangular matrix + * @param {mat2} a the input matrix to factorize + */ + +function LDU(L, D, U, a) { + L[2] = a[2] / a[0]; + U[0] = a[0]; + U[1] = a[1]; + U[3] = a[3] - L[2] * U[1]; + return [L, D, U]; +} + +/** + * Adds two mat2's + * + * @param {mat2} out the receiving matrix + * @param {mat2} a the first operand + * @param {mat2} b the second operand + * @returns {mat2} out + */ +function add(out, a, b) { + out[0] = a[0] + b[0]; + out[1] = a[1] + b[1]; + out[2] = a[2] + b[2]; + out[3] = a[3] + b[3]; + return out; +} + +/** + * Subtracts matrix b from matrix a + * + * @param {mat2} out the receiving matrix + * @param {mat2} a the first operand + * @param {mat2} b the second operand + * @returns {mat2} out + */ +function subtract(out, a, b) { + out[0] = a[0] - b[0]; + out[1] = a[1] - b[1]; + out[2] = a[2] - b[2]; + out[3] = a[3] - b[3]; + return out; +} + +/** + * Returns whether or not the matrices have exactly the same elements in the same position (when compared with ===) + * + * @param {mat2} a The first matrix. + * @param {mat2} b The second matrix. + * @returns {Boolean} True if the matrices are equal, false otherwise. + */ +function exactEquals(a, b) { + return a[0] === b[0] && a[1] === b[1] && a[2] === b[2] && a[3] === b[3]; +} + +/** + * Returns whether or not the matrices have approximately the same elements in the same position. + * + * @param {mat2} a The first matrix. + * @param {mat2} b The second matrix. + * @returns {Boolean} True if the matrices are equal, false otherwise. + */ +function equals(a, b) { + var a0 = a[0], + a1 = a[1], + a2 = a[2], + a3 = a[3]; + var b0 = b[0], + b1 = b[1], + b2 = b[2], + b3 = b[3]; + return Math.abs(a0 - b0) <= glMatrix.EPSILON * Math.max(1.0, Math.abs(a0), Math.abs(b0)) && Math.abs(a1 - b1) <= glMatrix.EPSILON * Math.max(1.0, Math.abs(a1), Math.abs(b1)) && Math.abs(a2 - b2) <= glMatrix.EPSILON * Math.max(1.0, Math.abs(a2), Math.abs(b2)) && Math.abs(a3 - b3) <= glMatrix.EPSILON * Math.max(1.0, Math.abs(a3), Math.abs(b3)); +} + +/** + * Multiply each element of the matrix by a scalar. + * + * @param {mat2} out the receiving matrix + * @param {mat2} a the matrix to scale + * @param {Number} b amount to scale the matrix's elements by + * @returns {mat2} out + */ +function multiplyScalar(out, a, b) { + out[0] = a[0] * b; + out[1] = a[1] * b; + out[2] = a[2] * b; + out[3] = a[3] * b; + return out; +} + +/** + * Adds two mat2's after multiplying each element of the second operand by a scalar value. + * + * @param {mat2} out the receiving vector + * @param {mat2} a the first operand + * @param {mat2} b the second operand + * @param {Number} scale the amount to scale b's elements by before adding + * @returns {mat2} out + */ +function multiplyScalarAndAdd(out, a, b, scale) { + out[0] = a[0] + b[0] * scale; + out[1] = a[1] + b[1] * scale; + out[2] = a[2] + b[2] * scale; + out[3] = a[3] + b[3] * scale; + return out; +} + +/** + * Alias for {@link mat2.multiply} + * @function + */ +var mul = exports.mul = multiply; + +/** + * Alias for {@link mat2.subtract} + * @function + */ +var sub = exports.sub = subtract; + +/***/ }), +/* 6 */ +/***/ (function(module, exports, __webpack_require__) { + +"use strict"; + + +Object.defineProperty(exports, "__esModule", { + value: true +}); +exports.sub = exports.mul = undefined; +exports.create = create; +exports.clone = clone; +exports.copy = copy; +exports.identity = identity; +exports.fromValues = fromValues; +exports.set = set; +exports.invert = invert; +exports.determinant = determinant; +exports.multiply = multiply; +exports.rotate = rotate; +exports.scale = scale; +exports.translate = translate; +exports.fromRotation = fromRotation; +exports.fromScaling = fromScaling; +exports.fromTranslation = fromTranslation; +exports.str = str; +exports.frob = frob; +exports.add = add; +exports.subtract = subtract; +exports.multiplyScalar = multiplyScalar; +exports.multiplyScalarAndAdd = multiplyScalarAndAdd; +exports.exactEquals = exactEquals; +exports.equals = equals; + +var _common = __webpack_require__(0); + +var glMatrix = _interopRequireWildcard(_common); + +function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) newObj[key] = obj[key]; } } newObj.default = obj; return newObj; } } + +/** + * 2x3 Matrix + * @module mat2d + * + * @description + * A mat2d contains six elements defined as: + *
+ * [a, c, tx,
+ *  b, d, ty]
+ * 
+ * This is a short form for the 3x3 matrix: + *
+ * [a, c, tx,
+ *  b, d, ty,
+ *  0, 0, 1]
+ * 
+ * The last row is ignored so the array is shorter and operations are faster. + */ + +/** + * Creates a new identity mat2d + * + * @returns {mat2d} a new 2x3 matrix + */ +function create() { + var out = new glMatrix.ARRAY_TYPE(6); + out[0] = 1; + out[1] = 0; + out[2] = 0; + out[3] = 1; + out[4] = 0; + out[5] = 0; + return out; +} + +/** + * Creates a new mat2d initialized with values from an existing matrix + * + * @param {mat2d} a matrix to clone + * @returns {mat2d} a new 2x3 matrix + */ +/* Copyright (c) 2015, Brandon Jones, Colin MacKenzie IV. + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in +all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +THE SOFTWARE. */ + +function clone(a) { + var out = new glMatrix.ARRAY_TYPE(6); + out[0] = a[0]; + out[1] = a[1]; + out[2] = a[2]; + out[3] = a[3]; + out[4] = a[4]; + out[5] = a[5]; + return out; +} + +/** + * Copy the values from one mat2d to another + * + * @param {mat2d} out the receiving matrix + * @param {mat2d} a the source matrix + * @returns {mat2d} out + */ +function copy(out, a) { + out[0] = a[0]; + out[1] = a[1]; + out[2] = a[2]; + out[3] = a[3]; + out[4] = a[4]; + out[5] = a[5]; + return out; +} + +/** + * Set a mat2d to the identity matrix + * + * @param {mat2d} out the receiving matrix + * @returns {mat2d} out + */ +function identity(out) { + out[0] = 1; + out[1] = 0; + out[2] = 0; + out[3] = 1; + out[4] = 0; + out[5] = 0; + return out; +} + +/** + * Create a new mat2d with the given values + * + * @param {Number} a Component A (index 0) + * @param {Number} b Component B (index 1) + * @param {Number} c Component C (index 2) + * @param {Number} d Component D (index 3) + * @param {Number} tx Component TX (index 4) + * @param {Number} ty Component TY (index 5) + * @returns {mat2d} A new mat2d + */ +function fromValues(a, b, c, d, tx, ty) { + var out = new glMatrix.ARRAY_TYPE(6); + out[0] = a; + out[1] = b; + out[2] = c; + out[3] = d; + out[4] = tx; + out[5] = ty; + return out; +} + +/** + * Set the components of a mat2d to the given values + * + * @param {mat2d} out the receiving matrix + * @param {Number} a Component A (index 0) + * @param {Number} b Component B (index 1) + * @param {Number} c Component C (index 2) + * @param {Number} d Component D (index 3) + * @param {Number} tx Component TX (index 4) + * @param {Number} ty Component TY (index 5) + * @returns {mat2d} out + */ +function set(out, a, b, c, d, tx, ty) { + out[0] = a; + out[1] = b; + out[2] = c; + out[3] = d; + out[4] = tx; + out[5] = ty; + return out; +} + +/** + * Inverts a mat2d + * + * @param {mat2d} out the receiving matrix + * @param {mat2d} a the source matrix + * @returns {mat2d} out + */ +function invert(out, a) { + var aa = a[0], + ab = a[1], + ac = a[2], + ad = a[3]; + var atx = a[4], + aty = a[5]; + + var det = aa * ad - ab * ac; + if (!det) { + return null; + } + det = 1.0 / det; + + out[0] = ad * det; + out[1] = -ab * det; + out[2] = -ac * det; + out[3] = aa * det; + out[4] = (ac * aty - ad * atx) * det; + out[5] = (ab * atx - aa * aty) * det; + return out; +} + +/** + * Calculates the determinant of a mat2d + * + * @param {mat2d} a the source matrix + * @returns {Number} determinant of a + */ +function determinant(a) { + return a[0] * a[3] - a[1] * a[2]; +} + +/** + * Multiplies two mat2d's + * + * @param {mat2d} out the receiving matrix + * @param {mat2d} a the first operand + * @param {mat2d} b the second operand + * @returns {mat2d} out + */ +function multiply(out, a, b) { + var a0 = a[0], + a1 = a[1], + a2 = a[2], + a3 = a[3], + a4 = a[4], + a5 = a[5]; + var b0 = b[0], + b1 = b[1], + b2 = b[2], + b3 = b[3], + b4 = b[4], + b5 = b[5]; + out[0] = a0 * b0 + a2 * b1; + out[1] = a1 * b0 + a3 * b1; + out[2] = a0 * b2 + a2 * b3; + out[3] = a1 * b2 + a3 * b3; + out[4] = a0 * b4 + a2 * b5 + a4; + out[5] = a1 * b4 + a3 * b5 + a5; + return out; +} + +/** + * Rotates a mat2d by the given angle + * + * @param {mat2d} out the receiving matrix + * @param {mat2d} a the matrix to rotate + * @param {Number} rad the angle to rotate the matrix by + * @returns {mat2d} out + */ +function rotate(out, a, rad) { + var a0 = a[0], + a1 = a[1], + a2 = a[2], + a3 = a[3], + a4 = a[4], + a5 = a[5]; + var s = Math.sin(rad); + var c = Math.cos(rad); + out[0] = a0 * c + a2 * s; + out[1] = a1 * c + a3 * s; + out[2] = a0 * -s + a2 * c; + out[3] = a1 * -s + a3 * c; + out[4] = a4; + out[5] = a5; + return out; +} + +/** + * Scales the mat2d by the dimensions in the given vec2 + * + * @param {mat2d} out the receiving matrix + * @param {mat2d} a the matrix to translate + * @param {vec2} v the vec2 to scale the matrix by + * @returns {mat2d} out + **/ +function scale(out, a, v) { + var a0 = a[0], + a1 = a[1], + a2 = a[2], + a3 = a[3], + a4 = a[4], + a5 = a[5]; + var v0 = v[0], + v1 = v[1]; + out[0] = a0 * v0; + out[1] = a1 * v0; + out[2] = a2 * v1; + out[3] = a3 * v1; + out[4] = a4; + out[5] = a5; + return out; +} + +/** + * Translates the mat2d by the dimensions in the given vec2 + * + * @param {mat2d} out the receiving matrix + * @param {mat2d} a the matrix to translate + * @param {vec2} v the vec2 to translate the matrix by + * @returns {mat2d} out + **/ +function translate(out, a, v) { + var a0 = a[0], + a1 = a[1], + a2 = a[2], + a3 = a[3], + a4 = a[4], + a5 = a[5]; + var v0 = v[0], + v1 = v[1]; + out[0] = a0; + out[1] = a1; + out[2] = a2; + out[3] = a3; + out[4] = a0 * v0 + a2 * v1 + a4; + out[5] = a1 * v0 + a3 * v1 + a5; + return out; +} + +/** + * Creates a matrix from a given angle + * This is equivalent to (but much faster than): + * + * mat2d.identity(dest); + * mat2d.rotate(dest, dest, rad); + * + * @param {mat2d} out mat2d receiving operation result + * @param {Number} rad the angle to rotate the matrix by + * @returns {mat2d} out + */ +function fromRotation(out, rad) { + var s = Math.sin(rad), + c = Math.cos(rad); + out[0] = c; + out[1] = s; + out[2] = -s; + out[3] = c; + out[4] = 0; + out[5] = 0; + return out; +} + +/** + * Creates a matrix from a vector scaling + * This is equivalent to (but much faster than): + * + * mat2d.identity(dest); + * mat2d.scale(dest, dest, vec); + * + * @param {mat2d} out mat2d receiving operation result + * @param {vec2} v Scaling vector + * @returns {mat2d} out + */ +function fromScaling(out, v) { + out[0] = v[0]; + out[1] = 0; + out[2] = 0; + out[3] = v[1]; + out[4] = 0; + out[5] = 0; + return out; +} + +/** + * Creates a matrix from a vector translation + * This is equivalent to (but much faster than): + * + * mat2d.identity(dest); + * mat2d.translate(dest, dest, vec); + * + * @param {mat2d} out mat2d receiving operation result + * @param {vec2} v Translation vector + * @returns {mat2d} out + */ +function fromTranslation(out, v) { + out[0] = 1; + out[1] = 0; + out[2] = 0; + out[3] = 1; + out[4] = v[0]; + out[5] = v[1]; + return out; +} + +/** + * Returns a string representation of a mat2d + * + * @param {mat2d} a matrix to represent as a string + * @returns {String} string representation of the matrix + */ +function str(a) { + return 'mat2d(' + a[0] + ', ' + a[1] + ', ' + a[2] + ', ' + a[3] + ', ' + a[4] + ', ' + a[5] + ')'; +} + +/** + * Returns Frobenius norm of a mat2d + * + * @param {mat2d} a the matrix to calculate Frobenius norm of + * @returns {Number} Frobenius norm + */ +function frob(a) { + 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) + 1); +} + +/** + * Adds two mat2d's + * + * @param {mat2d} out the receiving matrix + * @param {mat2d} a the first operand + * @param {mat2d} b the second operand + * @returns {mat2d} out + */ +function add(out, a, b) { + out[0] = a[0] + b[0]; + out[1] = a[1] + b[1]; + out[2] = a[2] + b[2]; + out[3] = a[3] + b[3]; + out[4] = a[4] + b[4]; + out[5] = a[5] + b[5]; + return out; +} + +/** + * Subtracts matrix b from matrix a + * + * @param {mat2d} out the receiving matrix + * @param {mat2d} a the first operand + * @param {mat2d} b the second operand + * @returns {mat2d} out + */ +function subtract(out, a, b) { + out[0] = a[0] - b[0]; + out[1] = a[1] - b[1]; + out[2] = a[2] - b[2]; + out[3] = a[3] - b[3]; + out[4] = a[4] - b[4]; + out[5] = a[5] - b[5]; + return out; +} + +/** + * Multiply each element of the matrix by a scalar. + * + * @param {mat2d} out the receiving matrix + * @param {mat2d} a the matrix to scale + * @param {Number} b amount to scale the matrix's elements by + * @returns {mat2d} out + */ +function multiplyScalar(out, a, b) { + out[0] = a[0] * b; + out[1] = a[1] * b; + out[2] = a[2] * b; + out[3] = a[3] * b; + out[4] = a[4] * b; + out[5] = a[5] * b; + return out; +} + +/** + * Adds two mat2d's after multiplying each element of the second operand by a scalar value. + * + * @param {mat2d} out the receiving vector + * @param {mat2d} a the first operand + * @param {mat2d} b the second operand + * @param {Number} scale the amount to scale b's elements by before adding + * @returns {mat2d} out + */ +function multiplyScalarAndAdd(out, a, b, scale) { + out[0] = a[0] + b[0] * scale; + out[1] = a[1] + b[1] * scale; + out[2] = a[2] + b[2] * scale; + out[3] = a[3] + b[3] * scale; + out[4] = a[4] + b[4] * scale; + out[5] = a[5] + b[5] * scale; + return out; +} + +/** + * Returns whether or not the matrices have exactly the same elements in the same position (when compared with ===) + * + * @param {mat2d} a The first matrix. + * @param {mat2d} b The second matrix. + * @returns {Boolean} True if the matrices are equal, false otherwise. + */ +function exactEquals(a, b) { + return a[0] === b[0] && a[1] === b[1] && a[2] === b[2] && a[3] === b[3] && a[4] === b[4] && a[5] === b[5]; +} + +/** + * Returns whether or not the matrices have approximately the same elements in the same position. + * + * @param {mat2d} a The first matrix. + * @param {mat2d} b The second matrix. + * @returns {Boolean} True if the matrices are equal, false otherwise. + */ +function equals(a, b) { + var a0 = a[0], + a1 = a[1], + a2 = a[2], + a3 = a[3], + a4 = a[4], + a5 = a[5]; + var b0 = b[0], + b1 = b[1], + b2 = b[2], + b3 = b[3], + b4 = b[4], + b5 = b[5]; + return Math.abs(a0 - b0) <= glMatrix.EPSILON * Math.max(1.0, Math.abs(a0), Math.abs(b0)) && Math.abs(a1 - b1) <= glMatrix.EPSILON * Math.max(1.0, Math.abs(a1), Math.abs(b1)) && Math.abs(a2 - b2) <= glMatrix.EPSILON * Math.max(1.0, Math.abs(a2), Math.abs(b2)) && Math.abs(a3 - b3) <= glMatrix.EPSILON * Math.max(1.0, Math.abs(a3), Math.abs(b3)) && Math.abs(a4 - b4) <= glMatrix.EPSILON * Math.max(1.0, Math.abs(a4), Math.abs(b4)) && Math.abs(a5 - b5) <= glMatrix.EPSILON * Math.max(1.0, Math.abs(a5), Math.abs(b5)); +} + +/** + * Alias for {@link mat2d.multiply} + * @function + */ +var mul = exports.mul = multiply; + +/** + * Alias for {@link mat2d.subtract} + * @function + */ +var sub = exports.sub = subtract; + +/***/ }), +/* 7 */ +/***/ (function(module, exports, __webpack_require__) { + +"use strict"; + + +Object.defineProperty(exports, "__esModule", { + value: true +}); +exports.sub = exports.mul = undefined; +exports.create = create; +exports.clone = clone; +exports.copy = copy; +exports.fromValues = fromValues; +exports.set = set; +exports.identity = identity; +exports.transpose = transpose; +exports.invert = invert; +exports.adjoint = adjoint; +exports.determinant = determinant; +exports.multiply = multiply; +exports.translate = translate; +exports.scale = scale; +exports.rotate = rotate; +exports.rotateX = rotateX; +exports.rotateY = rotateY; +exports.rotateZ = rotateZ; +exports.fromTranslation = fromTranslation; +exports.fromScaling = fromScaling; +exports.fromRotation = fromRotation; +exports.fromXRotation = fromXRotation; +exports.fromYRotation = fromYRotation; +exports.fromZRotation = fromZRotation; +exports.fromRotationTranslation = fromRotationTranslation; +exports.getTranslation = getTranslation; +exports.getScaling = getScaling; +exports.getRotation = getRotation; +exports.fromRotationTranslationScale = fromRotationTranslationScale; +exports.fromRotationTranslationScaleOrigin = fromRotationTranslationScaleOrigin; +exports.fromQuat = fromQuat; +exports.frustum = frustum; +exports.perspective = perspective; +exports.perspectiveFromFieldOfView = perspectiveFromFieldOfView; +exports.ortho = ortho; +exports.lookAt = lookAt; +exports.targetTo = targetTo; +exports.str = str; +exports.frob = frob; +exports.add = add; +exports.subtract = subtract; +exports.multiplyScalar = multiplyScalar; +exports.multiplyScalarAndAdd = multiplyScalarAndAdd; +exports.exactEquals = exactEquals; +exports.equals = equals; + +var _common = __webpack_require__(0); + +var glMatrix = _interopRequireWildcard(_common); + +function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) newObj[key] = obj[key]; } } newObj.default = obj; return newObj; } } + +/** + * 4x4 Matrix + * @module mat4 + */ + +/** + * Creates a new identity mat4 + * + * @returns {mat4} a new 4x4 matrix + */ +function create() { + var out = new glMatrix.ARRAY_TYPE(16); + out[0] = 1; + out[1] = 0; + out[2] = 0; + out[3] = 0; + out[4] = 0; + out[5] = 1; + out[6] = 0; + out[7] = 0; + out[8] = 0; + out[9] = 0; + out[10] = 1; + out[11] = 0; + out[12] = 0; + out[13] = 0; + out[14] = 0; + out[15] = 1; + return out; +} + +/** + * Creates a new mat4 initialized with values from an existing matrix + * + * @param {mat4} a matrix to clone + * @returns {mat4} a new 4x4 matrix + */ +/* Copyright (c) 2015, Brandon Jones, Colin MacKenzie IV. + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in +all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +THE SOFTWARE. */ + +function clone(a) { + var out = new glMatrix.ARRAY_TYPE(16); + out[0] = a[0]; + out[1] = a[1]; + out[2] = a[2]; + out[3] = a[3]; + out[4] = a[4]; + out[5] = a[5]; + out[6] = a[6]; + out[7] = a[7]; + out[8] = a[8]; + out[9] = a[9]; + out[10] = a[10]; + out[11] = a[11]; + out[12] = a[12]; + out[13] = a[13]; + out[14] = a[14]; + out[15] = a[15]; + return out; +} + +/** + * Copy the values from one mat4 to another + * + * @param {mat4} out the receiving matrix + * @param {mat4} a the source matrix + * @returns {mat4} out + */ +function copy(out, a) { + out[0] = a[0]; + out[1] = a[1]; + out[2] = a[2]; + out[3] = a[3]; + out[4] = a[4]; + out[5] = a[5]; + out[6] = a[6]; + out[7] = a[7]; + out[8] = a[8]; + out[9] = a[9]; + out[10] = a[10]; + out[11] = a[11]; + out[12] = a[12]; + out[13] = a[13]; + out[14] = a[14]; + out[15] = a[15]; + return out; +} + +/** + * Create a new mat4 with the given values + * + * @param {Number} m00 Component in column 0, row 0 position (index 0) + * @param {Number} m01 Component in column 0, row 1 position (index 1) + * @param {Number} m02 Component in column 0, row 2 position (index 2) + * @param {Number} m03 Component in column 0, row 3 position (index 3) + * @param {Number} m10 Component in column 1, row 0 position (index 4) + * @param {Number} m11 Component in column 1, row 1 position (index 5) + * @param {Number} m12 Component in column 1, row 2 position (index 6) + * @param {Number} m13 Component in column 1, row 3 position (index 7) + * @param {Number} m20 Component in column 2, row 0 position (index 8) + * @param {Number} m21 Component in column 2, row 1 position (index 9) + * @param {Number} m22 Component in column 2, row 2 position (index 10) + * @param {Number} m23 Component in column 2, row 3 position (index 11) + * @param {Number} m30 Component in column 3, row 0 position (index 12) + * @param {Number} m31 Component in column 3, row 1 position (index 13) + * @param {Number} m32 Component in column 3, row 2 position (index 14) + * @param {Number} m33 Component in column 3, row 3 position (index 15) + * @returns {mat4} A new mat4 + */ +function fromValues(m00, m01, m02, m03, m10, m11, m12, m13, m20, m21, m22, m23, m30, m31, m32, m33) { + var out = new glMatrix.ARRAY_TYPE(16); + out[0] = m00; + out[1] = m01; + out[2] = m02; + out[3] = m03; + out[4] = m10; + out[5] = m11; + out[6] = m12; + out[7] = m13; + out[8] = m20; + out[9] = m21; + out[10] = m22; + out[11] = m23; + out[12] = m30; + out[13] = m31; + out[14] = m32; + out[15] = m33; + return out; +} + +/** + * Set the components of a mat4 to the given values + * + * @param {mat4} out the receiving matrix + * @param {Number} m00 Component in column 0, row 0 position (index 0) + * @param {Number} m01 Component in column 0, row 1 position (index 1) + * @param {Number} m02 Component in column 0, row 2 position (index 2) + * @param {Number} m03 Component in column 0, row 3 position (index 3) + * @param {Number} m10 Component in column 1, row 0 position (index 4) + * @param {Number} m11 Component in column 1, row 1 position (index 5) + * @param {Number} m12 Component in column 1, row 2 position (index 6) + * @param {Number} m13 Component in column 1, row 3 position (index 7) + * @param {Number} m20 Component in column 2, row 0 position (index 8) + * @param {Number} m21 Component in column 2, row 1 position (index 9) + * @param {Number} m22 Component in column 2, row 2 position (index 10) + * @param {Number} m23 Component in column 2, row 3 position (index 11) + * @param {Number} m30 Component in column 3, row 0 position (index 12) + * @param {Number} m31 Component in column 3, row 1 position (index 13) + * @param {Number} m32 Component in column 3, row 2 position (index 14) + * @param {Number} m33 Component in column 3, row 3 position (index 15) + * @returns {mat4} out + */ +function set(out, m00, m01, m02, m03, m10, m11, m12, m13, m20, m21, m22, m23, m30, m31, m32, m33) { + out[0] = m00; + out[1] = m01; + out[2] = m02; + out[3] = m03; + out[4] = m10; + out[5] = m11; + out[6] = m12; + out[7] = m13; + out[8] = m20; + out[9] = m21; + out[10] = m22; + out[11] = m23; + out[12] = m30; + out[13] = m31; + out[14] = m32; + out[15] = m33; + return out; +} + +/** + * Set a mat4 to the identity matrix + * + * @param {mat4} out the receiving matrix + * @returns {mat4} out + */ +function identity(out) { + out[0] = 1; + out[1] = 0; + out[2] = 0; + out[3] = 0; + out[4] = 0; + out[5] = 1; + out[6] = 0; + out[7] = 0; + out[8] = 0; + out[9] = 0; + out[10] = 1; + out[11] = 0; + out[12] = 0; + out[13] = 0; + out[14] = 0; + out[15] = 1; + return out; +} + +/** + * Transpose the values of a mat4 + * + * @param {mat4} out the receiving matrix + * @param {mat4} a the source matrix + * @returns {mat4} out + */ +function transpose(out, a) { + // If we are transposing ourselves we can skip a few steps but have to cache some values + if (out === a) { + var a01 = a[1], + a02 = a[2], + a03 = a[3]; + var a12 = a[6], + a13 = a[7]; + var a23 = a[11]; + + out[1] = a[4]; + out[2] = a[8]; + out[3] = a[12]; + out[4] = a01; + out[6] = a[9]; + out[7] = a[13]; + out[8] = a02; + out[9] = a12; + out[11] = a[14]; + out[12] = a03; + out[13] = a13; + out[14] = a23; + } else { + out[0] = a[0]; + out[1] = a[4]; + out[2] = a[8]; + out[3] = a[12]; + out[4] = a[1]; + out[5] = a[5]; + out[6] = a[9]; + out[7] = a[13]; + out[8] = a[2]; + out[9] = a[6]; + out[10] = a[10]; + out[11] = a[14]; + out[12] = a[3]; + out[13] = a[7]; + out[14] = a[11]; + out[15] = a[15]; + } + + return out; +} + +/** + * Inverts a mat4 + * + * @param {mat4} out the receiving matrix + * @param {mat4} a the source matrix + * @returns {mat4} out + */ +function invert(out, a) { + var a00 = a[0], + a01 = a[1], + a02 = a[2], + a03 = a[3]; + var a10 = a[4], + a11 = a[5], + a12 = a[6], + a13 = a[7]; + var a20 = a[8], + a21 = a[9], + a22 = a[10], + a23 = a[11]; + var a30 = a[12], + a31 = a[13], + a32 = a[14], + a33 = a[15]; + + var b00 = a00 * a11 - a01 * a10; + var b01 = a00 * a12 - a02 * a10; + var b02 = a00 * a13 - a03 * a10; + var b03 = a01 * a12 - a02 * a11; + var b04 = a01 * a13 - a03 * a11; + var b05 = a02 * a13 - a03 * a12; + var b06 = a20 * a31 - a21 * a30; + var b07 = a20 * a32 - a22 * a30; + var b08 = a20 * a33 - a23 * a30; + var b09 = a21 * a32 - a22 * a31; + var b10 = a21 * a33 - a23 * a31; + var b11 = a22 * a33 - a23 * a32; + + // Calculate the determinant + var det = b00 * b11 - b01 * b10 + b02 * b09 + b03 * b08 - b04 * b07 + b05 * b06; + + if (!det) { + return null; + } + det = 1.0 / det; + + out[0] = (a11 * b11 - a12 * b10 + a13 * b09) * det; + out[1] = (a02 * b10 - a01 * b11 - a03 * b09) * det; + out[2] = (a31 * b05 - a32 * b04 + a33 * b03) * det; + out[3] = (a22 * b04 - a21 * b05 - a23 * b03) * det; + out[4] = (a12 * b08 - a10 * b11 - a13 * b07) * det; + out[5] = (a00 * b11 - a02 * b08 + a03 * b07) * det; + out[6] = (a32 * b02 - a30 * b05 - a33 * b01) * det; + out[7] = (a20 * b05 - a22 * b02 + a23 * b01) * det; + out[8] = (a10 * b10 - a11 * b08 + a13 * b06) * det; + out[9] = (a01 * b08 - a00 * b10 - a03 * b06) * det; + out[10] = (a30 * b04 - a31 * b02 + a33 * b00) * det; + out[11] = (a21 * b02 - a20 * b04 - a23 * b00) * det; + out[12] = (a11 * b07 - a10 * b09 - a12 * b06) * det; + out[13] = (a00 * b09 - a01 * b07 + a02 * b06) * det; + out[14] = (a31 * b01 - a30 * b03 - a32 * b00) * det; + out[15] = (a20 * b03 - a21 * b01 + a22 * b00) * det; + + return out; +} + +/** + * Calculates the adjugate of a mat4 + * + * @param {mat4} out the receiving matrix + * @param {mat4} a the source matrix + * @returns {mat4} out + */ +function adjoint(out, a) { + var a00 = a[0], + a01 = a[1], + a02 = a[2], + a03 = a[3]; + var a10 = a[4], + a11 = a[5], + a12 = a[6], + a13 = a[7]; + var a20 = a[8], + a21 = a[9], + a22 = a[10], + a23 = a[11]; + var a30 = a[12], + a31 = a[13], + a32 = a[14], + a33 = a[15]; + + out[0] = a11 * (a22 * a33 - a23 * a32) - a21 * (a12 * a33 - a13 * a32) + a31 * (a12 * a23 - a13 * a22); + out[1] = -(a01 * (a22 * a33 - a23 * a32) - a21 * (a02 * a33 - a03 * a32) + a31 * (a02 * a23 - a03 * a22)); + out[2] = a01 * (a12 * a33 - a13 * a32) - a11 * (a02 * a33 - a03 * a32) + a31 * (a02 * a13 - a03 * a12); + out[3] = -(a01 * (a12 * a23 - a13 * a22) - a11 * (a02 * a23 - a03 * a22) + a21 * (a02 * a13 - a03 * a12)); + out[4] = -(a10 * (a22 * a33 - a23 * a32) - a20 * (a12 * a33 - a13 * a32) + a30 * (a12 * a23 - a13 * a22)); + out[5] = a00 * (a22 * a33 - a23 * a32) - a20 * (a02 * a33 - a03 * a32) + a30 * (a02 * a23 - a03 * a22); + out[6] = -(a00 * (a12 * a33 - a13 * a32) - a10 * (a02 * a33 - a03 * a32) + a30 * (a02 * a13 - a03 * a12)); + out[7] = a00 * (a12 * a23 - a13 * a22) - a10 * (a02 * a23 - a03 * a22) + a20 * (a02 * a13 - a03 * a12); + out[8] = a10 * (a21 * a33 - a23 * a31) - a20 * (a11 * a33 - a13 * a31) + a30 * (a11 * a23 - a13 * a21); + out[9] = -(a00 * (a21 * a33 - a23 * a31) - a20 * (a01 * a33 - a03 * a31) + a30 * (a01 * a23 - a03 * a21)); + out[10] = a00 * (a11 * a33 - a13 * a31) - a10 * (a01 * a33 - a03 * a31) + a30 * (a01 * a13 - a03 * a11); + out[11] = -(a00 * (a11 * a23 - a13 * a21) - a10 * (a01 * a23 - a03 * a21) + a20 * (a01 * a13 - a03 * a11)); + out[12] = -(a10 * (a21 * a32 - a22 * a31) - a20 * (a11 * a32 - a12 * a31) + a30 * (a11 * a22 - a12 * a21)); + out[13] = a00 * (a21 * a32 - a22 * a31) - a20 * (a01 * a32 - a02 * a31) + a30 * (a01 * a22 - a02 * a21); + out[14] = -(a00 * (a11 * a32 - a12 * a31) - a10 * (a01 * a32 - a02 * a31) + a30 * (a01 * a12 - a02 * a11)); + out[15] = a00 * (a11 * a22 - a12 * a21) - a10 * (a01 * a22 - a02 * a21) + a20 * (a01 * a12 - a02 * a11); + return out; +} + +/** + * Calculates the determinant of a mat4 + * + * @param {mat4} a the source matrix + * @returns {Number} determinant of a + */ +function determinant(a) { + var a00 = a[0], + a01 = a[1], + a02 = a[2], + a03 = a[3]; + var a10 = a[4], + a11 = a[5], + a12 = a[6], + a13 = a[7]; + var a20 = a[8], + a21 = a[9], + a22 = a[10], + a23 = a[11]; + var a30 = a[12], + a31 = a[13], + a32 = a[14], + a33 = a[15]; + + var b00 = a00 * a11 - a01 * a10; + var b01 = a00 * a12 - a02 * a10; + var b02 = a00 * a13 - a03 * a10; + var b03 = a01 * a12 - a02 * a11; + var b04 = a01 * a13 - a03 * a11; + var b05 = a02 * a13 - a03 * a12; + var b06 = a20 * a31 - a21 * a30; + var b07 = a20 * a32 - a22 * a30; + var b08 = a20 * a33 - a23 * a30; + var b09 = a21 * a32 - a22 * a31; + var b10 = a21 * a33 - a23 * a31; + var b11 = a22 * a33 - a23 * a32; + + // Calculate the determinant + return b00 * b11 - b01 * b10 + b02 * b09 + b03 * b08 - b04 * b07 + b05 * b06; +} + +/** + * Multiplies two mat4s + * + * @param {mat4} out the receiving matrix + * @param {mat4} a the first operand + * @param {mat4} b the second operand + * @returns {mat4} out + */ +function multiply(out, a, b) { + var a00 = a[0], + a01 = a[1], + a02 = a[2], + a03 = a[3]; + var a10 = a[4], + a11 = a[5], + a12 = a[6], + a13 = a[7]; + var a20 = a[8], + a21 = a[9], + a22 = a[10], + a23 = a[11]; + var a30 = a[12], + a31 = a[13], + a32 = a[14], + a33 = a[15]; + + // Cache only the current line of the second matrix + var b0 = b[0], + b1 = b[1], + b2 = b[2], + b3 = b[3]; + out[0] = b0 * a00 + b1 * a10 + b2 * a20 + b3 * a30; + out[1] = b0 * a01 + b1 * a11 + b2 * a21 + b3 * a31; + out[2] = b0 * a02 + b1 * a12 + b2 * a22 + b3 * a32; + out[3] = b0 * a03 + b1 * a13 + b2 * a23 + b3 * a33; + + b0 = b[4];b1 = b[5];b2 = b[6];b3 = b[7]; + out[4] = b0 * a00 + b1 * a10 + b2 * a20 + b3 * a30; + out[5] = b0 * a01 + b1 * a11 + b2 * a21 + b3 * a31; + out[6] = b0 * a02 + b1 * a12 + b2 * a22 + b3 * a32; + out[7] = b0 * a03 + b1 * a13 + b2 * a23 + b3 * a33; + + b0 = b[8];b1 = b[9];b2 = b[10];b3 = b[11]; + out[8] = b0 * a00 + b1 * a10 + b2 * a20 + b3 * a30; + out[9] = b0 * a01 + b1 * a11 + b2 * a21 + b3 * a31; + out[10] = b0 * a02 + b1 * a12 + b2 * a22 + b3 * a32; + out[11] = b0 * a03 + b1 * a13 + b2 * a23 + b3 * a33; + + b0 = b[12];b1 = b[13];b2 = b[14];b3 = b[15]; + out[12] = b0 * a00 + b1 * a10 + b2 * a20 + b3 * a30; + out[13] = b0 * a01 + b1 * a11 + b2 * a21 + b3 * a31; + out[14] = b0 * a02 + b1 * a12 + b2 * a22 + b3 * a32; + out[15] = b0 * a03 + b1 * a13 + b2 * a23 + b3 * a33; + return out; +} + +/** + * Translate a mat4 by the given vector + * + * @param {mat4} out the receiving matrix + * @param {mat4} a the matrix to translate + * @param {vec3} v vector to translate by + * @returns {mat4} out + */ +function translate(out, a, v) { + var x = v[0], + y = v[1], + z = v[2]; + var a00 = void 0, + a01 = void 0, + a02 = void 0, + a03 = void 0; + var a10 = void 0, + a11 = void 0, + a12 = void 0, + a13 = void 0; + var a20 = void 0, + a21 = void 0, + a22 = void 0, + a23 = void 0; + + if (a === out) { + out[12] = a[0] * x + a[4] * y + a[8] * z + a[12]; + out[13] = a[1] * x + a[5] * y + a[9] * z + a[13]; + out[14] = a[2] * x + a[6] * y + a[10] * z + a[14]; + out[15] = a[3] * x + a[7] * y + a[11] * z + a[15]; + } else { + a00 = a[0];a01 = a[1];a02 = a[2];a03 = a[3]; + a10 = a[4];a11 = a[5];a12 = a[6];a13 = a[7]; + a20 = a[8];a21 = a[9];a22 = a[10];a23 = a[11]; + + out[0] = a00;out[1] = a01;out[2] = a02;out[3] = a03; + out[4] = a10;out[5] = a11;out[6] = a12;out[7] = a13; + out[8] = a20;out[9] = a21;out[10] = a22;out[11] = a23; + + out[12] = a00 * x + a10 * y + a20 * z + a[12]; + out[13] = a01 * x + a11 * y + a21 * z + a[13]; + out[14] = a02 * x + a12 * y + a22 * z + a[14]; + out[15] = a03 * x + a13 * y + a23 * z + a[15]; + } + + return out; +} + +/** + * Scales the mat4 by the dimensions in the given vec3 not using vectorization + * + * @param {mat4} out the receiving matrix + * @param {mat4} a the matrix to scale + * @param {vec3} v the vec3 to scale the matrix by + * @returns {mat4} out + **/ +function scale(out, a, v) { + var x = v[0], + y = v[1], + z = v[2]; + + out[0] = a[0] * x; + out[1] = a[1] * x; + out[2] = a[2] * x; + out[3] = a[3] * x; + out[4] = a[4] * y; + out[5] = a[5] * y; + out[6] = a[6] * y; + out[7] = a[7] * y; + out[8] = a[8] * z; + out[9] = a[9] * z; + out[10] = a[10] * z; + out[11] = a[11] * z; + out[12] = a[12]; + out[13] = a[13]; + out[14] = a[14]; + out[15] = a[15]; + return out; +} + +/** + * Rotates a mat4 by the given angle around the given axis + * + * @param {mat4} out the receiving matrix + * @param {mat4} a the matrix to rotate + * @param {Number} rad the angle to rotate the matrix by + * @param {vec3} axis the axis to rotate around + * @returns {mat4} out + */ +function rotate(out, a, rad, axis) { + var x = axis[0], + y = axis[1], + z = axis[2]; + var len = Math.sqrt(x * x + y * y + z * z); + var s = void 0, + c = void 0, + t = void 0; + var a00 = void 0, + a01 = void 0, + a02 = void 0, + a03 = void 0; + var a10 = void 0, + a11 = void 0, + a12 = void 0, + a13 = void 0; + var a20 = void 0, + a21 = void 0, + a22 = void 0, + a23 = void 0; + var b00 = void 0, + b01 = void 0, + b02 = void 0; + var b10 = void 0, + b11 = void 0, + b12 = void 0; + var b20 = void 0, + b21 = void 0, + b22 = void 0; + + if (Math.abs(len) < glMatrix.EPSILON) { + return null; + } + + len = 1 / len; + x *= len; + y *= len; + z *= len; + + s = Math.sin(rad); + c = Math.cos(rad); + t = 1 - c; + + a00 = a[0];a01 = a[1];a02 = a[2];a03 = a[3]; + a10 = a[4];a11 = a[5];a12 = a[6];a13 = a[7]; + a20 = a[8];a21 = a[9];a22 = a[10];a23 = a[11]; + + // Construct the elements of the rotation matrix + b00 = x * x * t + c;b01 = y * x * t + z * s;b02 = z * x * t - y * s; + b10 = x * y * t - z * s;b11 = y * y * t + c;b12 = z * y * t + x * s; + b20 = x * z * t + y * s;b21 = y * z * t - x * s;b22 = z * z * t + c; + + // Perform rotation-specific matrix multiplication + out[0] = a00 * b00 + a10 * b01 + a20 * b02; + out[1] = a01 * b00 + a11 * b01 + a21 * b02; + out[2] = a02 * b00 + a12 * b01 + a22 * b02; + out[3] = a03 * b00 + a13 * b01 + a23 * b02; + out[4] = a00 * b10 + a10 * b11 + a20 * b12; + out[5] = a01 * b10 + a11 * b11 + a21 * b12; + out[6] = a02 * b10 + a12 * b11 + a22 * b12; + out[7] = a03 * b10 + a13 * b11 + a23 * b12; + out[8] = a00 * b20 + a10 * b21 + a20 * b22; + out[9] = a01 * b20 + a11 * b21 + a21 * b22; + out[10] = a02 * b20 + a12 * b21 + a22 * b22; + out[11] = a03 * b20 + a13 * b21 + a23 * b22; + + if (a !== out) { + // If the source and destination differ, copy the unchanged last row + out[12] = a[12]; + out[13] = a[13]; + out[14] = a[14]; + out[15] = a[15]; + } + return out; +} + +/** + * Rotates a matrix by the given angle around the X axis + * + * @param {mat4} out the receiving matrix + * @param {mat4} a the matrix to rotate + * @param {Number} rad the angle to rotate the matrix by + * @returns {mat4} out + */ +function rotateX(out, a, rad) { + var s = Math.sin(rad); + var c = Math.cos(rad); + var a10 = a[4]; + var a11 = a[5]; + var a12 = a[6]; + var a13 = a[7]; + var a20 = a[8]; + var a21 = a[9]; + var a22 = a[10]; + var a23 = a[11]; + + if (a !== out) { + // If the source and destination differ, copy the unchanged rows + out[0] = a[0]; + out[1] = a[1]; + out[2] = a[2]; + out[3] = a[3]; + out[12] = a[12]; + out[13] = a[13]; + out[14] = a[14]; + out[15] = a[15]; + } + + // Perform axis-specific matrix multiplication + out[4] = a10 * c + a20 * s; + out[5] = a11 * c + a21 * s; + out[6] = a12 * c + a22 * s; + out[7] = a13 * c + a23 * s; + out[8] = a20 * c - a10 * s; + out[9] = a21 * c - a11 * s; + out[10] = a22 * c - a12 * s; + out[11] = a23 * c - a13 * s; + return out; +} + +/** + * Rotates a matrix by the given angle around the Y axis + * + * @param {mat4} out the receiving matrix + * @param {mat4} a the matrix to rotate + * @param {Number} rad the angle to rotate the matrix by + * @returns {mat4} out + */ +function rotateY(out, a, rad) { + var s = Math.sin(rad); + var c = Math.cos(rad); + var a00 = a[0]; + var a01 = a[1]; + var a02 = a[2]; + var a03 = a[3]; + var a20 = a[8]; + var a21 = a[9]; + var a22 = a[10]; + var a23 = a[11]; + + if (a !== out) { + // If the source and destination differ, copy the unchanged rows + out[4] = a[4]; + out[5] = a[5]; + out[6] = a[6]; + out[7] = a[7]; + out[12] = a[12]; + out[13] = a[13]; + out[14] = a[14]; + out[15] = a[15]; + } + + // Perform axis-specific matrix multiplication + out[0] = a00 * c - a20 * s; + out[1] = a01 * c - a21 * s; + out[2] = a02 * c - a22 * s; + out[3] = a03 * c - a23 * s; + out[8] = a00 * s + a20 * c; + out[9] = a01 * s + a21 * c; + out[10] = a02 * s + a22 * c; + out[11] = a03 * s + a23 * c; + return out; +} + +/** + * Rotates a matrix by the given angle around the Z axis + * + * @param {mat4} out the receiving matrix + * @param {mat4} a the matrix to rotate + * @param {Number} rad the angle to rotate the matrix by + * @returns {mat4} out + */ +function rotateZ(out, a, rad) { + var s = Math.sin(rad); + var c = Math.cos(rad); + var a00 = a[0]; + var a01 = a[1]; + var a02 = a[2]; + var a03 = a[3]; + var a10 = a[4]; + var a11 = a[5]; + var a12 = a[6]; + var a13 = a[7]; + + if (a !== out) { + // If the source and destination differ, copy the unchanged last row + out[8] = a[8]; + out[9] = a[9]; + out[10] = a[10]; + out[11] = a[11]; + out[12] = a[12]; + out[13] = a[13]; + out[14] = a[14]; + out[15] = a[15]; + } + + // Perform axis-specific matrix multiplication + out[0] = a00 * c + a10 * s; + out[1] = a01 * c + a11 * s; + out[2] = a02 * c + a12 * s; + out[3] = a03 * c + a13 * s; + out[4] = a10 * c - a00 * s; + out[5] = a11 * c - a01 * s; + out[6] = a12 * c - a02 * s; + out[7] = a13 * c - a03 * s; + return out; +} + +/** + * Creates a matrix from a vector translation + * This is equivalent to (but much faster than): + * + * mat4.identity(dest); + * mat4.translate(dest, dest, vec); + * + * @param {mat4} out mat4 receiving operation result + * @param {vec3} v Translation vector + * @returns {mat4} out + */ +function fromTranslation(out, v) { + out[0] = 1; + out[1] = 0; + out[2] = 0; + out[3] = 0; + out[4] = 0; + out[5] = 1; + out[6] = 0; + out[7] = 0; + out[8] = 0; + out[9] = 0; + out[10] = 1; + out[11] = 0; + out[12] = v[0]; + out[13] = v[1]; + out[14] = v[2]; + out[15] = 1; + return out; +} + +/** + * Creates a matrix from a vector scaling + * This is equivalent to (but much faster than): + * + * mat4.identity(dest); + * mat4.scale(dest, dest, vec); + * + * @param {mat4} out mat4 receiving operation result + * @param {vec3} v Scaling vector + * @returns {mat4} out + */ +function fromScaling(out, v) { + out[0] = v[0]; + out[1] = 0; + out[2] = 0; + out[3] = 0; + out[4] = 0; + out[5] = v[1]; + out[6] = 0; + out[7] = 0; + out[8] = 0; + out[9] = 0; + out[10] = v[2]; + out[11] = 0; + out[12] = 0; + out[13] = 0; + out[14] = 0; + out[15] = 1; + return out; +} + +/** + * Creates a matrix from a given angle around a given axis + * This is equivalent to (but much faster than): + * + * mat4.identity(dest); + * mat4.rotate(dest, dest, rad, axis); + * + * @param {mat4} out mat4 receiving operation result + * @param {Number} rad the angle to rotate the matrix by + * @param {vec3} axis the axis to rotate around + * @returns {mat4} out + */ +function fromRotation(out, rad, axis) { + var x = axis[0], + y = axis[1], + z = axis[2]; + var len = Math.sqrt(x * x + y * y + z * z); + var s = void 0, + c = void 0, + t = void 0; + + if (Math.abs(len) < glMatrix.EPSILON) { + return null; + } + + len = 1 / len; + x *= len; + y *= len; + z *= len; + + s = Math.sin(rad); + c = Math.cos(rad); + t = 1 - c; + + // Perform rotation-specific matrix multiplication + out[0] = x * x * t + c; + out[1] = y * x * t + z * s; + out[2] = z * x * t - y * s; + out[3] = 0; + out[4] = x * y * t - z * s; + out[5] = y * y * t + c; + out[6] = z * y * t + x * s; + out[7] = 0; + out[8] = x * z * t + y * s; + out[9] = y * z * t - x * s; + out[10] = z * z * t + c; + out[11] = 0; + out[12] = 0; + out[13] = 0; + out[14] = 0; + out[15] = 1; + return out; +} + +/** + * Creates a matrix from the given angle around the X axis + * This is equivalent to (but much faster than): + * + * mat4.identity(dest); + * mat4.rotateX(dest, dest, rad); + * + * @param {mat4} out mat4 receiving operation result + * @param {Number} rad the angle to rotate the matrix by + * @returns {mat4} out + */ +function fromXRotation(out, rad) { + var s = Math.sin(rad); + var c = Math.cos(rad); + + // Perform axis-specific matrix multiplication + out[0] = 1; + out[1] = 0; + out[2] = 0; + out[3] = 0; + out[4] = 0; + out[5] = c; + out[6] = s; + out[7] = 0; + out[8] = 0; + out[9] = -s; + out[10] = c; + out[11] = 0; + out[12] = 0; + out[13] = 0; + out[14] = 0; + out[15] = 1; + return out; +} + +/** + * Creates a matrix from the given angle around the Y axis + * This is equivalent to (but much faster than): + * + * mat4.identity(dest); + * mat4.rotateY(dest, dest, rad); + * + * @param {mat4} out mat4 receiving operation result + * @param {Number} rad the angle to rotate the matrix by + * @returns {mat4} out + */ +function fromYRotation(out, rad) { + var s = Math.sin(rad); + var c = Math.cos(rad); + + // Perform axis-specific matrix multiplication + out[0] = c; + out[1] = 0; + out[2] = -s; + out[3] = 0; + out[4] = 0; + out[5] = 1; + out[6] = 0; + out[7] = 0; + out[8] = s; + out[9] = 0; + out[10] = c; + out[11] = 0; + out[12] = 0; + out[13] = 0; + out[14] = 0; + out[15] = 1; + return out; +} + +/** + * Creates a matrix from the given angle around the Z axis + * This is equivalent to (but much faster than): + * + * mat4.identity(dest); + * mat4.rotateZ(dest, dest, rad); + * + * @param {mat4} out mat4 receiving operation result + * @param {Number} rad the angle to rotate the matrix by + * @returns {mat4} out + */ +function fromZRotation(out, rad) { + var s = Math.sin(rad); + var c = Math.cos(rad); + + // Perform axis-specific matrix multiplication + out[0] = c; + out[1] = s; + out[2] = 0; + out[3] = 0; + out[4] = -s; + out[5] = c; + out[6] = 0; + out[7] = 0; + out[8] = 0; + out[9] = 0; + out[10] = 1; + out[11] = 0; + out[12] = 0; + out[13] = 0; + out[14] = 0; + out[15] = 1; + return out; +} + +/** + * Creates a matrix from a quaternion rotation and vector translation + * This is equivalent to (but much faster than): + * + * mat4.identity(dest); + * mat4.translate(dest, vec); + * let quatMat = mat4.create(); + * quat4.toMat4(quat, quatMat); + * mat4.multiply(dest, quatMat); + * + * @param {mat4} out mat4 receiving operation result + * @param {quat4} q Rotation quaternion + * @param {vec3} v Translation vector + * @returns {mat4} out + */ +function fromRotationTranslation(out, q, v) { + // Quaternion math + var x = q[0], + y = q[1], + z = q[2], + w = q[3]; + var x2 = x + x; + var y2 = y + y; + var z2 = z + z; + + var xx = x * x2; + var xy = x * y2; + var xz = x * z2; + var yy = y * y2; + var yz = y * z2; + var zz = z * z2; + var wx = w * x2; + var wy = w * y2; + var wz = w * z2; + + out[0] = 1 - (yy + zz); + out[1] = xy + wz; + out[2] = xz - wy; + out[3] = 0; + out[4] = xy - wz; + out[5] = 1 - (xx + zz); + out[6] = yz + wx; + out[7] = 0; + out[8] = xz + wy; + out[9] = yz - wx; + out[10] = 1 - (xx + yy); + out[11] = 0; + out[12] = v[0]; + out[13] = v[1]; + out[14] = v[2]; + out[15] = 1; + + return out; +} + +/** + * Returns the translation vector component of a transformation + * matrix. If a matrix is built with fromRotationTranslation, + * the returned vector will be the same as the translation vector + * originally supplied. + * @param {vec3} out Vector to receive translation component + * @param {mat4} mat Matrix to be decomposed (input) + * @return {vec3} out + */ +function getTranslation(out, mat) { + out[0] = mat[12]; + out[1] = mat[13]; + out[2] = mat[14]; + + return out; +} + +/** + * Returns the scaling factor component of a transformation + * matrix. If a matrix is built with fromRotationTranslationScale + * with a normalized Quaternion paramter, the returned vector will be + * the same as the scaling vector + * originally supplied. + * @param {vec3} out Vector to receive scaling factor component + * @param {mat4} mat Matrix to be decomposed (input) + * @return {vec3} out + */ +function getScaling(out, mat) { + var m11 = mat[0]; + var m12 = mat[1]; + var m13 = mat[2]; + var m21 = mat[4]; + var m22 = mat[5]; + var m23 = mat[6]; + var m31 = mat[8]; + var m32 = mat[9]; + var m33 = mat[10]; + + out[0] = Math.sqrt(m11 * m11 + m12 * m12 + m13 * m13); + out[1] = Math.sqrt(m21 * m21 + m22 * m22 + m23 * m23); + out[2] = Math.sqrt(m31 * m31 + m32 * m32 + m33 * m33); + + return out; +} + +/** + * Returns a quaternion representing the rotational component + * of a transformation matrix. If a matrix is built with + * fromRotationTranslation, the returned quaternion will be the + * same as the quaternion originally supplied. + * @param {quat} out Quaternion to receive the rotation component + * @param {mat4} mat Matrix to be decomposed (input) + * @return {quat} out + */ +function getRotation(out, mat) { + // Algorithm taken from http://www.euclideanspace.com/maths/geometry/rotations/conversions/matrixToQuaternion/index.htm + var trace = mat[0] + mat[5] + mat[10]; + var S = 0; + + if (trace > 0) { + S = Math.sqrt(trace + 1.0) * 2; + out[3] = 0.25 * S; + out[0] = (mat[6] - mat[9]) / S; + out[1] = (mat[8] - mat[2]) / S; + out[2] = (mat[1] - mat[4]) / S; + } else if (mat[0] > mat[5] & mat[0] > mat[10]) { + S = Math.sqrt(1.0 + mat[0] - mat[5] - mat[10]) * 2; + out[3] = (mat[6] - mat[9]) / S; + out[0] = 0.25 * S; + out[1] = (mat[1] + mat[4]) / S; + out[2] = (mat[8] + mat[2]) / S; + } else if (mat[5] > mat[10]) { + S = Math.sqrt(1.0 + mat[5] - mat[0] - mat[10]) * 2; + out[3] = (mat[8] - mat[2]) / S; + out[0] = (mat[1] + mat[4]) / S; + out[1] = 0.25 * S; + out[2] = (mat[6] + mat[9]) / S; + } else { + S = Math.sqrt(1.0 + mat[10] - mat[0] - mat[5]) * 2; + out[3] = (mat[1] - mat[4]) / S; + out[0] = (mat[8] + mat[2]) / S; + out[1] = (mat[6] + mat[9]) / S; + out[2] = 0.25 * S; + } + + return out; +} + +/** + * Creates a matrix from a quaternion rotation, vector translation and vector scale + * This is equivalent to (but much faster than): + * + * mat4.identity(dest); + * mat4.translate(dest, vec); + * let quatMat = mat4.create(); + * quat4.toMat4(quat, quatMat); + * mat4.multiply(dest, quatMat); + * mat4.scale(dest, scale) + * + * @param {mat4} out mat4 receiving operation result + * @param {quat4} q Rotation quaternion + * @param {vec3} v Translation vector + * @param {vec3} s Scaling vector + * @returns {mat4} out + */ +function fromRotationTranslationScale(out, q, v, s) { + // Quaternion math + var x = q[0], + y = q[1], + z = q[2], + w = q[3]; + var x2 = x + x; + var y2 = y + y; + var z2 = z + z; + + var xx = x * x2; + var xy = x * y2; + var xz = x * z2; + var yy = y * y2; + var yz = y * z2; + var zz = z * z2; + var wx = w * x2; + var wy = w * y2; + var wz = w * z2; + var sx = s[0]; + var sy = s[1]; + var sz = s[2]; + + out[0] = (1 - (yy + zz)) * sx; + out[1] = (xy + wz) * sx; + out[2] = (xz - wy) * sx; + out[3] = 0; + out[4] = (xy - wz) * sy; + out[5] = (1 - (xx + zz)) * sy; + out[6] = (yz + wx) * sy; + out[7] = 0; + out[8] = (xz + wy) * sz; + out[9] = (yz - wx) * sz; + out[10] = (1 - (xx + yy)) * sz; + out[11] = 0; + out[12] = v[0]; + out[13] = v[1]; + out[14] = v[2]; + out[15] = 1; + + return out; +} + +/** + * Creates a matrix from a quaternion rotation, vector translation and vector scale, rotating and scaling around the given origin + * This is equivalent to (but much faster than): + * + * mat4.identity(dest); + * mat4.translate(dest, vec); + * mat4.translate(dest, origin); + * let quatMat = mat4.create(); + * quat4.toMat4(quat, quatMat); + * mat4.multiply(dest, quatMat); + * mat4.scale(dest, scale) + * mat4.translate(dest, negativeOrigin); + * + * @param {mat4} out mat4 receiving operation result + * @param {quat4} q Rotation quaternion + * @param {vec3} v Translation vector + * @param {vec3} s Scaling vector + * @param {vec3} o The origin vector around which to scale and rotate + * @returns {mat4} out + */ +function fromRotationTranslationScaleOrigin(out, q, v, s, o) { + // Quaternion math + var x = q[0], + y = q[1], + z = q[2], + w = q[3]; + var x2 = x + x; + var y2 = y + y; + var z2 = z + z; + + var xx = x * x2; + var xy = x * y2; + var xz = x * z2; + var yy = y * y2; + var yz = y * z2; + var zz = z * z2; + var wx = w * x2; + var wy = w * y2; + var wz = w * z2; + + var sx = s[0]; + var sy = s[1]; + var sz = s[2]; + + var ox = o[0]; + var oy = o[1]; + var oz = o[2]; + + out[0] = (1 - (yy + zz)) * sx; + out[1] = (xy + wz) * sx; + out[2] = (xz - wy) * sx; + out[3] = 0; + out[4] = (xy - wz) * sy; + out[5] = (1 - (xx + zz)) * sy; + out[6] = (yz + wx) * sy; + out[7] = 0; + out[8] = (xz + wy) * sz; + out[9] = (yz - wx) * sz; + out[10] = (1 - (xx + yy)) * sz; + out[11] = 0; + out[12] = v[0] + ox - (out[0] * ox + out[4] * oy + out[8] * oz); + out[13] = v[1] + oy - (out[1] * ox + out[5] * oy + out[9] * oz); + out[14] = v[2] + oz - (out[2] * ox + out[6] * oy + out[10] * oz); + out[15] = 1; + + return out; +} + +/** + * Calculates a 4x4 matrix from the given quaternion + * + * @param {mat4} out mat4 receiving operation result + * @param {quat} q Quaternion to create matrix from + * + * @returns {mat4} out + */ +function fromQuat(out, q) { + var x = q[0], + y = q[1], + z = q[2], + w = q[3]; + var x2 = x + x; + var y2 = y + y; + var z2 = z + z; + + var xx = x * x2; + var yx = y * x2; + var yy = y * y2; + var zx = z * x2; + var zy = z * y2; + var zz = z * z2; + var wx = w * x2; + var wy = w * y2; + var wz = w * z2; + + out[0] = 1 - yy - zz; + out[1] = yx + wz; + out[2] = zx - wy; + out[3] = 0; + + out[4] = yx - wz; + out[5] = 1 - xx - zz; + out[6] = zy + wx; + out[7] = 0; + + out[8] = zx + wy; + out[9] = zy - wx; + out[10] = 1 - xx - yy; + out[11] = 0; + + out[12] = 0; + out[13] = 0; + out[14] = 0; + out[15] = 1; + + return out; +} + +/** + * Generates a frustum matrix with the given bounds + * + * @param {mat4} out mat4 frustum matrix will be written into + * @param {Number} left Left bound of the frustum + * @param {Number} right Right bound of the frustum + * @param {Number} bottom Bottom bound of the frustum + * @param {Number} top Top bound of the frustum + * @param {Number} near Near bound of the frustum + * @param {Number} far Far bound of the frustum + * @returns {mat4} out + */ +function frustum(out, left, right, bottom, top, near, far) { + var rl = 1 / (right - left); + var tb = 1 / (top - bottom); + var nf = 1 / (near - far); + out[0] = near * 2 * rl; + out[1] = 0; + out[2] = 0; + out[3] = 0; + out[4] = 0; + out[5] = near * 2 * tb; + out[6] = 0; + out[7] = 0; + out[8] = (right + left) * rl; + out[9] = (top + bottom) * tb; + out[10] = (far + near) * nf; + out[11] = -1; + out[12] = 0; + out[13] = 0; + out[14] = far * near * 2 * nf; + out[15] = 0; + return out; +} + +/** + * Generates a perspective projection matrix with the given bounds + * + * @param {mat4} out mat4 frustum matrix will be written into + * @param {number} fovy Vertical field of view in radians + * @param {number} aspect Aspect ratio. typically viewport width/height + * @param {number} near Near bound of the frustum + * @param {number} far Far bound of the frustum + * @returns {mat4} out + */ +function perspective(out, fovy, aspect, near, far) { + var f = 1.0 / Math.tan(fovy / 2); + var nf = 1 / (near - far); + out[0] = f / aspect; + out[1] = 0; + out[2] = 0; + out[3] = 0; + out[4] = 0; + out[5] = f; + out[6] = 0; + out[7] = 0; + out[8] = 0; + out[9] = 0; + out[10] = (far + near) * nf; + out[11] = -1; + out[12] = 0; + out[13] = 0; + out[14] = 2 * far * near * nf; + out[15] = 0; + return out; +} + +/** + * Generates a perspective projection matrix with the given field of view. + * This is primarily useful for generating projection matrices to be used + * with the still experiemental WebVR API. + * + * @param {mat4} out mat4 frustum matrix will be written into + * @param {Object} fov Object containing the following values: upDegrees, downDegrees, leftDegrees, rightDegrees + * @param {number} near Near bound of the frustum + * @param {number} far Far bound of the frustum + * @returns {mat4} out + */ +function perspectiveFromFieldOfView(out, fov, near, far) { + var upTan = Math.tan(fov.upDegrees * Math.PI / 180.0); + var downTan = Math.tan(fov.downDegrees * Math.PI / 180.0); + var leftTan = Math.tan(fov.leftDegrees * Math.PI / 180.0); + var rightTan = Math.tan(fov.rightDegrees * Math.PI / 180.0); + var xScale = 2.0 / (leftTan + rightTan); + var yScale = 2.0 / (upTan + downTan); + + out[0] = xScale; + out[1] = 0.0; + out[2] = 0.0; + out[3] = 0.0; + out[4] = 0.0; + out[5] = yScale; + out[6] = 0.0; + out[7] = 0.0; + out[8] = -((leftTan - rightTan) * xScale * 0.5); + out[9] = (upTan - downTan) * yScale * 0.5; + out[10] = far / (near - far); + out[11] = -1.0; + out[12] = 0.0; + out[13] = 0.0; + out[14] = far * near / (near - far); + out[15] = 0.0; + return out; +} + +/** + * Generates a orthogonal projection matrix with the given bounds + * + * @param {mat4} out mat4 frustum matrix will be written into + * @param {number} left Left bound of the frustum + * @param {number} right Right bound of the frustum + * @param {number} bottom Bottom bound of the frustum + * @param {number} top Top bound of the frustum + * @param {number} near Near bound of the frustum + * @param {number} far Far bound of the frustum + * @returns {mat4} out + */ +function ortho(out, left, right, bottom, top, near, far) { + var lr = 1 / (left - right); + var bt = 1 / (bottom - top); + var nf = 1 / (near - far); + out[0] = -2 * lr; + out[1] = 0; + out[2] = 0; + out[3] = 0; + out[4] = 0; + out[5] = -2 * bt; + out[6] = 0; + out[7] = 0; + out[8] = 0; + out[9] = 0; + out[10] = 2 * nf; + out[11] = 0; + out[12] = (left + right) * lr; + out[13] = (top + bottom) * bt; + out[14] = (far + near) * nf; + out[15] = 1; + return out; +} + +/** + * Generates a look-at matrix with the given eye position, focal point, and up axis + * + * @param {mat4} out mat4 frustum matrix will be written into + * @param {vec3} eye Position of the viewer + * @param {vec3} center Point the viewer is looking at + * @param {vec3} up vec3 pointing up + * @returns {mat4} out + */ +function lookAt(out, eye, center, up) { + var x0 = void 0, + x1 = void 0, + x2 = void 0, + y0 = void 0, + y1 = void 0, + y2 = void 0, + z0 = void 0, + z1 = void 0, + z2 = void 0, + len = void 0; + var eyex = eye[0]; + var eyey = eye[1]; + var eyez = eye[2]; + var upx = up[0]; + var upy = up[1]; + var upz = up[2]; + var centerx = center[0]; + var centery = center[1]; + var centerz = center[2]; + + if (Math.abs(eyex - centerx) < glMatrix.EPSILON && Math.abs(eyey - centery) < glMatrix.EPSILON && Math.abs(eyez - centerz) < glMatrix.EPSILON) { + return mat4.identity(out); + } + + z0 = eyex - centerx; + z1 = eyey - centery; + z2 = eyez - centerz; + + len = 1 / Math.sqrt(z0 * z0 + z1 * z1 + z2 * z2); + z0 *= len; + z1 *= len; + z2 *= len; + + x0 = upy * z2 - upz * z1; + x1 = upz * z0 - upx * z2; + x2 = upx * z1 - upy * z0; + len = Math.sqrt(x0 * x0 + x1 * x1 + x2 * x2); + if (!len) { + x0 = 0; + x1 = 0; + x2 = 0; + } else { + len = 1 / len; + x0 *= len; + x1 *= len; + x2 *= len; + } + + y0 = z1 * x2 - z2 * x1; + y1 = z2 * x0 - z0 * x2; + y2 = z0 * x1 - z1 * x0; + + len = Math.sqrt(y0 * y0 + y1 * y1 + y2 * y2); + if (!len) { + y0 = 0; + y1 = 0; + y2 = 0; + } else { + len = 1 / len; + y0 *= len; + y1 *= len; + y2 *= len; + } + + out[0] = x0; + out[1] = y0; + out[2] = z0; + out[3] = 0; + out[4] = x1; + out[5] = y1; + out[6] = z1; + out[7] = 0; + out[8] = x2; + out[9] = y2; + out[10] = z2; + out[11] = 0; + out[12] = -(x0 * eyex + x1 * eyey + x2 * eyez); + out[13] = -(y0 * eyex + y1 * eyey + y2 * eyez); + out[14] = -(z0 * eyex + z1 * eyey + z2 * eyez); + out[15] = 1; + + return out; +} + +/** + * Generates a matrix that makes something look at something else. + * + * @param {mat4} out mat4 frustum matrix will be written into + * @param {vec3} eye Position of the viewer + * @param {vec3} center Point the viewer is looking at + * @param {vec3} up vec3 pointing up + * @returns {mat4} out + */ +function targetTo(out, eye, target, up) { + var eyex = eye[0], + eyey = eye[1], + eyez = eye[2], + upx = up[0], + upy = up[1], + upz = up[2]; + + var z0 = eyex - target[0], + z1 = eyey - target[1], + z2 = eyez - target[2]; + + var len = z0 * z0 + z1 * z1 + z2 * z2; + if (len > 0) { + len = 1 / Math.sqrt(len); + z0 *= len; + z1 *= len; + z2 *= len; + } + + var x0 = upy * z2 - upz * z1, + x1 = upz * z0 - upx * z2, + x2 = upx * z1 - upy * z0; + + out[0] = x0; + out[1] = x1; + out[2] = x2; + out[3] = 0; + out[4] = z1 * x2 - z2 * x1; + out[5] = z2 * x0 - z0 * x2; + out[6] = z0 * x1 - z1 * x0; + out[7] = 0; + out[8] = z0; + out[9] = z1; + out[10] = z2; + out[11] = 0; + out[12] = eyex; + out[13] = eyey; + out[14] = eyez; + out[15] = 1; + return out; +}; + +/** + * Returns a string representation of a mat4 + * + * @param {mat4} a matrix to represent as a string + * @returns {String} string representation of the matrix + */ +function str(a) { + return 'mat4(' + a[0] + ', ' + a[1] + ', ' + a[2] + ', ' + a[3] + ', ' + a[4] + ', ' + a[5] + ', ' + a[6] + ', ' + a[7] + ', ' + a[8] + ', ' + a[9] + ', ' + a[10] + ', ' + a[11] + ', ' + a[12] + ', ' + a[13] + ', ' + a[14] + ', ' + a[15] + ')'; +} + +/** + * Returns Frobenius norm of a mat4 + * + * @param {mat4} a the matrix to calculate Frobenius norm of + * @returns {Number} Frobenius norm + */ +function frob(a) { + 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)); +} + +/** + * Adds two mat4's + * + * @param {mat4} out the receiving matrix + * @param {mat4} a the first operand + * @param {mat4} b the second operand + * @returns {mat4} out + */ +function add(out, a, b) { + out[0] = a[0] + b[0]; + out[1] = a[1] + b[1]; + out[2] = a[2] + b[2]; + out[3] = a[3] + b[3]; + out[4] = a[4] + b[4]; + out[5] = a[5] + b[5]; + out[6] = a[6] + b[6]; + out[7] = a[7] + b[7]; + out[8] = a[8] + b[8]; + out[9] = a[9] + b[9]; + out[10] = a[10] + b[10]; + out[11] = a[11] + b[11]; + out[12] = a[12] + b[12]; + out[13] = a[13] + b[13]; + out[14] = a[14] + b[14]; + out[15] = a[15] + b[15]; + return out; +} + +/** + * Subtracts matrix b from matrix a + * + * @param {mat4} out the receiving matrix + * @param {mat4} a the first operand + * @param {mat4} b the second operand + * @returns {mat4} out + */ +function subtract(out, a, b) { + out[0] = a[0] - b[0]; + out[1] = a[1] - b[1]; + out[2] = a[2] - b[2]; + out[3] = a[3] - b[3]; + out[4] = a[4] - b[4]; + out[5] = a[5] - b[5]; + out[6] = a[6] - b[6]; + out[7] = a[7] - b[7]; + out[8] = a[8] - b[8]; + out[9] = a[9] - b[9]; + out[10] = a[10] - b[10]; + out[11] = a[11] - b[11]; + out[12] = a[12] - b[12]; + out[13] = a[13] - b[13]; + out[14] = a[14] - b[14]; + out[15] = a[15] - b[15]; + return out; +} + +/** + * Multiply each element of the matrix by a scalar. + * + * @param {mat4} out the receiving matrix + * @param {mat4} a the matrix to scale + * @param {Number} b amount to scale the matrix's elements by + * @returns {mat4} out + */ +function multiplyScalar(out, a, b) { + out[0] = a[0] * b; + out[1] = a[1] * b; + out[2] = a[2] * b; + out[3] = a[3] * b; + out[4] = a[4] * b; + out[5] = a[5] * b; + out[6] = a[6] * b; + out[7] = a[7] * b; + out[8] = a[8] * b; + out[9] = a[9] * b; + out[10] = a[10] * b; + out[11] = a[11] * b; + out[12] = a[12] * b; + out[13] = a[13] * b; + out[14] = a[14] * b; + out[15] = a[15] * b; + return out; +} + +/** + * Adds two mat4's after multiplying each element of the second operand by a scalar value. + * + * @param {mat4} out the receiving vector + * @param {mat4} a the first operand + * @param {mat4} b the second operand + * @param {Number} scale the amount to scale b's elements by before adding + * @returns {mat4} out + */ +function multiplyScalarAndAdd(out, a, b, scale) { + out[0] = a[0] + b[0] * scale; + out[1] = a[1] + b[1] * scale; + out[2] = a[2] + b[2] * scale; + out[3] = a[3] + b[3] * scale; + out[4] = a[4] + b[4] * scale; + out[5] = a[5] + b[5] * scale; + out[6] = a[6] + b[6] * scale; + out[7] = a[7] + b[7] * scale; + out[8] = a[8] + b[8] * scale; + out[9] = a[9] + b[9] * scale; + out[10] = a[10] + b[10] * scale; + out[11] = a[11] + b[11] * scale; + out[12] = a[12] + b[12] * scale; + out[13] = a[13] + b[13] * scale; + out[14] = a[14] + b[14] * scale; + out[15] = a[15] + b[15] * scale; + return out; +} + +/** + * Returns whether or not the matrices have exactly the same elements in the same position (when compared with ===) + * + * @param {mat4} a The first matrix. + * @param {mat4} b The second matrix. + * @returns {Boolean} True if the matrices are equal, false otherwise. + */ +function exactEquals(a, b) { + return a[0] === b[0] && a[1] === b[1] && a[2] === b[2] && a[3] === b[3] && a[4] === b[4] && a[5] === b[5] && a[6] === b[6] && a[7] === b[7] && a[8] === b[8] && a[9] === b[9] && a[10] === b[10] && a[11] === b[11] && a[12] === b[12] && a[13] === b[13] && a[14] === b[14] && a[15] === b[15]; +} + +/** + * Returns whether or not the matrices have approximately the same elements in the same position. + * + * @param {mat4} a The first matrix. + * @param {mat4} b The second matrix. + * @returns {Boolean} True if the matrices are equal, false otherwise. + */ +function equals(a, b) { + var a0 = a[0], + a1 = a[1], + a2 = a[2], + a3 = a[3]; + var a4 = a[4], + a5 = a[5], + a6 = a[6], + a7 = a[7]; + var a8 = a[8], + a9 = a[9], + a10 = a[10], + a11 = a[11]; + var a12 = a[12], + a13 = a[13], + a14 = a[14], + a15 = a[15]; + + var b0 = b[0], + b1 = b[1], + b2 = b[2], + b3 = b[3]; + var b4 = b[4], + b5 = b[5], + b6 = b[6], + b7 = b[7]; + var b8 = b[8], + b9 = b[9], + b10 = b[10], + b11 = b[11]; + var b12 = b[12], + b13 = b[13], + b14 = b[14], + b15 = b[15]; + + return Math.abs(a0 - b0) <= glMatrix.EPSILON * Math.max(1.0, Math.abs(a0), Math.abs(b0)) && Math.abs(a1 - b1) <= glMatrix.EPSILON * Math.max(1.0, Math.abs(a1), Math.abs(b1)) && Math.abs(a2 - b2) <= glMatrix.EPSILON * Math.max(1.0, Math.abs(a2), Math.abs(b2)) && Math.abs(a3 - b3) <= glMatrix.EPSILON * Math.max(1.0, Math.abs(a3), Math.abs(b3)) && Math.abs(a4 - b4) <= glMatrix.EPSILON * Math.max(1.0, Math.abs(a4), Math.abs(b4)) && Math.abs(a5 - b5) <= glMatrix.EPSILON * Math.max(1.0, Math.abs(a5), Math.abs(b5)) && Math.abs(a6 - b6) <= glMatrix.EPSILON * Math.max(1.0, Math.abs(a6), Math.abs(b6)) && Math.abs(a7 - b7) <= glMatrix.EPSILON * Math.max(1.0, Math.abs(a7), Math.abs(b7)) && Math.abs(a8 - b8) <= glMatrix.EPSILON * Math.max(1.0, Math.abs(a8), Math.abs(b8)) && Math.abs(a9 - b9) <= glMatrix.EPSILON * Math.max(1.0, Math.abs(a9), Math.abs(b9)) && Math.abs(a10 - b10) <= glMatrix.EPSILON * Math.max(1.0, Math.abs(a10), Math.abs(b10)) && Math.abs(a11 - b11) <= glMatrix.EPSILON * Math.max(1.0, Math.abs(a11), Math.abs(b11)) && Math.abs(a12 - b12) <= glMatrix.EPSILON * Math.max(1.0, Math.abs(a12), Math.abs(b12)) && Math.abs(a13 - b13) <= glMatrix.EPSILON * Math.max(1.0, Math.abs(a13), Math.abs(b13)) && Math.abs(a14 - b14) <= glMatrix.EPSILON * Math.max(1.0, Math.abs(a14), Math.abs(b14)) && Math.abs(a15 - b15) <= glMatrix.EPSILON * Math.max(1.0, Math.abs(a15), Math.abs(b15)); +} + +/** + * Alias for {@link mat4.multiply} + * @function + */ +var mul = exports.mul = multiply; + +/** + * Alias for {@link mat4.subtract} + * @function + */ +var sub = exports.sub = subtract; + +/***/ }), +/* 8 */ +/***/ (function(module, exports, __webpack_require__) { + +"use strict"; + + +Object.defineProperty(exports, "__esModule", { + value: true +}); +exports.setAxes = exports.sqlerp = exports.rotationTo = exports.equals = exports.exactEquals = exports.normalize = exports.sqrLen = exports.squaredLength = exports.len = exports.length = exports.lerp = exports.dot = exports.scale = exports.mul = exports.add = exports.set = exports.copy = exports.fromValues = exports.clone = undefined; +exports.create = create; +exports.identity = identity; +exports.setAxisAngle = setAxisAngle; +exports.getAxisAngle = getAxisAngle; +exports.multiply = multiply; +exports.rotateX = rotateX; +exports.rotateY = rotateY; +exports.rotateZ = rotateZ; +exports.calculateW = calculateW; +exports.slerp = slerp; +exports.invert = invert; +exports.conjugate = conjugate; +exports.fromMat3 = fromMat3; +exports.fromEuler = fromEuler; +exports.str = str; + +var _common = __webpack_require__(0); + +var glMatrix = _interopRequireWildcard(_common); + +var _mat = __webpack_require__(1); + +var mat3 = _interopRequireWildcard(_mat); + +var _vec = __webpack_require__(2); + +var vec3 = _interopRequireWildcard(_vec); + +var _vec2 = __webpack_require__(3); + +var vec4 = _interopRequireWildcard(_vec2); + +function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) newObj[key] = obj[key]; } } newObj.default = obj; return newObj; } } + +/** + * Quaternion + * @module quat + */ + +/** + * Creates a new identity quat + * + * @returns {quat} a new quaternion + */ +/* Copyright (c) 2015, Brandon Jones, Colin MacKenzie IV. + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in +all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +THE SOFTWARE. */ + +function create() { + var out = new glMatrix.ARRAY_TYPE(4); + out[0] = 0; + out[1] = 0; + out[2] = 0; + out[3] = 1; + return out; +} + +/** + * Set a quat to the identity quaternion + * + * @param {quat} out the receiving quaternion + * @returns {quat} out + */ +function identity(out) { + out[0] = 0; + out[1] = 0; + out[2] = 0; + out[3] = 1; + return out; +} + +/** + * Sets a quat from the given angle and rotation axis, + * then returns it. + * + * @param {quat} out the receiving quaternion + * @param {vec3} axis the axis around which to rotate + * @param {Number} rad the angle in radians + * @returns {quat} out + **/ +function setAxisAngle(out, axis, rad) { + rad = rad * 0.5; + var s = Math.sin(rad); + out[0] = s * axis[0]; + out[1] = s * axis[1]; + out[2] = s * axis[2]; + out[3] = Math.cos(rad); + return out; +} + +/** + * Gets the rotation axis and angle for a given + * quaternion. If a quaternion is created with + * setAxisAngle, this method will return the same + * values as providied in the original parameter list + * OR functionally equivalent values. + * Example: The quaternion formed by axis [0, 0, 1] and + * angle -90 is the same as the quaternion formed by + * [0, 0, 1] and 270. This method favors the latter. + * @param {vec3} out_axis Vector receiving the axis of rotation + * @param {quat} q Quaternion to be decomposed + * @return {Number} Angle, in radians, of the rotation + */ +function getAxisAngle(out_axis, q) { + var rad = Math.acos(q[3]) * 2.0; + var s = Math.sin(rad / 2.0); + if (s != 0.0) { + out_axis[0] = q[0] / s; + out_axis[1] = q[1] / s; + out_axis[2] = q[2] / s; + } else { + // If s is zero, return any axis (no rotation - axis does not matter) + out_axis[0] = 1; + out_axis[1] = 0; + out_axis[2] = 0; + } + return rad; +} + +/** + * Multiplies two quat's + * + * @param {quat} out the receiving quaternion + * @param {quat} a the first operand + * @param {quat} b the second operand + * @returns {quat} out + */ +function multiply(out, a, b) { + var ax = a[0], + ay = a[1], + az = a[2], + aw = a[3]; + var bx = b[0], + by = b[1], + bz = b[2], + bw = b[3]; + + out[0] = ax * bw + aw * bx + ay * bz - az * by; + out[1] = ay * bw + aw * by + az * bx - ax * bz; + out[2] = az * bw + aw * bz + ax * by - ay * bx; + out[3] = aw * bw - ax * bx - ay * by - az * bz; + return out; +} + +/** + * Rotates a quaternion by the given angle about the X axis + * + * @param {quat} out quat receiving operation result + * @param {quat} a quat to rotate + * @param {number} rad angle (in radians) to rotate + * @returns {quat} out + */ +function rotateX(out, a, rad) { + rad *= 0.5; + + var ax = a[0], + ay = a[1], + az = a[2], + aw = a[3]; + var bx = Math.sin(rad), + bw = Math.cos(rad); + + out[0] = ax * bw + aw * bx; + out[1] = ay * bw + az * bx; + out[2] = az * bw - ay * bx; + out[3] = aw * bw - ax * bx; + return out; +} + +/** + * Rotates a quaternion by the given angle about the Y axis + * + * @param {quat} out quat receiving operation result + * @param {quat} a quat to rotate + * @param {number} rad angle (in radians) to rotate + * @returns {quat} out + */ +function rotateY(out, a, rad) { + rad *= 0.5; + + var ax = a[0], + ay = a[1], + az = a[2], + aw = a[3]; + var by = Math.sin(rad), + bw = Math.cos(rad); + + out[0] = ax * bw - az * by; + out[1] = ay * bw + aw * by; + out[2] = az * bw + ax * by; + out[3] = aw * bw - ay * by; + return out; +} + +/** + * Rotates a quaternion by the given angle about the Z axis + * + * @param {quat} out quat receiving operation result + * @param {quat} a quat to rotate + * @param {number} rad angle (in radians) to rotate + * @returns {quat} out + */ +function rotateZ(out, a, rad) { + rad *= 0.5; + + var ax = a[0], + ay = a[1], + az = a[2], + aw = a[3]; + var bz = Math.sin(rad), + bw = Math.cos(rad); + + out[0] = ax * bw + ay * bz; + out[1] = ay * bw - ax * bz; + out[2] = az * bw + aw * bz; + out[3] = aw * bw - az * bz; + return out; +} + +/** + * Calculates the W component of a quat from the X, Y, and Z components. + * Assumes that quaternion is 1 unit in length. + * Any existing W component will be ignored. + * + * @param {quat} out the receiving quaternion + * @param {quat} a quat to calculate W component of + * @returns {quat} out + */ +function calculateW(out, a) { + var x = a[0], + y = a[1], + z = a[2]; + + out[0] = x; + out[1] = y; + out[2] = z; + out[3] = Math.sqrt(Math.abs(1.0 - x * x - y * y - z * z)); + return out; +} + +/** + * Performs a spherical linear interpolation between two quat + * + * @param {quat} out the receiving quaternion + * @param {quat} a the first operand + * @param {quat} b the second operand + * @param {Number} t interpolation amount between the two inputs + * @returns {quat} out + */ +function slerp(out, a, b, t) { + // benchmarks: + // http://jsperf.com/quaternion-slerp-implementations + var ax = a[0], + ay = a[1], + az = a[2], + aw = a[3]; + var bx = b[0], + by = b[1], + bz = b[2], + bw = b[3]; + + var omega = void 0, + cosom = void 0, + sinom = void 0, + scale0 = void 0, + scale1 = void 0; + + // calc cosine + cosom = ax * bx + ay * by + az * bz + aw * bw; + // adjust signs (if necessary) + if (cosom < 0.0) { + cosom = -cosom; + bx = -bx; + by = -by; + bz = -bz; + bw = -bw; + } + // calculate coefficients + if (1.0 - cosom > 0.000001) { + // standard case (slerp) + omega = Math.acos(cosom); + sinom = Math.sin(omega); + scale0 = Math.sin((1.0 - t) * omega) / sinom; + scale1 = Math.sin(t * omega) / sinom; + } else { + // "from" and "to" quaternions are very close + // ... so we can do a linear interpolation + scale0 = 1.0 - t; + scale1 = t; + } + // calculate final values + out[0] = scale0 * ax + scale1 * bx; + out[1] = scale0 * ay + scale1 * by; + out[2] = scale0 * az + scale1 * bz; + out[3] = scale0 * aw + scale1 * bw; + + return out; +} + +/** + * Calculates the inverse of a quat + * + * @param {quat} out the receiving quaternion + * @param {quat} a quat to calculate inverse of + * @returns {quat} out + */ +function invert(out, a) { + var a0 = a[0], + a1 = a[1], + a2 = a[2], + a3 = a[3]; + var dot = a0 * a0 + a1 * a1 + a2 * a2 + a3 * a3; + var invDot = dot ? 1.0 / dot : 0; + + // TODO: Would be faster to return [0,0,0,0] immediately if dot == 0 + + out[0] = -a0 * invDot; + out[1] = -a1 * invDot; + out[2] = -a2 * invDot; + out[3] = a3 * invDot; + return out; +} + +/** + * Calculates the conjugate of a quat + * If the quaternion is normalized, this function is faster than quat.inverse and produces the same result. + * + * @param {quat} out the receiving quaternion + * @param {quat} a quat to calculate conjugate of + * @returns {quat} out + */ +function conjugate(out, a) { + out[0] = -a[0]; + out[1] = -a[1]; + out[2] = -a[2]; + out[3] = a[3]; + return out; +} + +/** + * Creates a quaternion from the given 3x3 rotation matrix. + * + * NOTE: The resultant quaternion is not normalized, so you should be sure + * to renormalize the quaternion yourself where necessary. + * + * @param {quat} out the receiving quaternion + * @param {mat3} m rotation matrix + * @returns {quat} out + * @function + */ +function fromMat3(out, m) { + // Algorithm in Ken Shoemake's article in 1987 SIGGRAPH course notes + // article "Quaternion Calculus and Fast Animation". + var fTrace = m[0] + m[4] + m[8]; + var fRoot = void 0; + + if (fTrace > 0.0) { + // |w| > 1/2, may as well choose w > 1/2 + fRoot = Math.sqrt(fTrace + 1.0); // 2w + out[3] = 0.5 * fRoot; + fRoot = 0.5 / fRoot; // 1/(4w) + out[0] = (m[5] - m[7]) * fRoot; + out[1] = (m[6] - m[2]) * fRoot; + out[2] = (m[1] - m[3]) * fRoot; + } else { + // |w| <= 1/2 + var i = 0; + if (m[4] > m[0]) i = 1; + if (m[8] > m[i * 3 + i]) i = 2; + var j = (i + 1) % 3; + var k = (i + 2) % 3; + + fRoot = Math.sqrt(m[i * 3 + i] - m[j * 3 + j] - m[k * 3 + k] + 1.0); + out[i] = 0.5 * fRoot; + fRoot = 0.5 / fRoot; + out[3] = (m[j * 3 + k] - m[k * 3 + j]) * fRoot; + out[j] = (m[j * 3 + i] + m[i * 3 + j]) * fRoot; + out[k] = (m[k * 3 + i] + m[i * 3 + k]) * fRoot; + } + + return out; +} + +/** + * Creates a quaternion from the given euler angle x, y, z. + * + * @param {quat} out the receiving quaternion + * @param {x} Angle to rotate around X axis in degrees. + * @param {y} Angle to rotate around Y axis in degrees. + * @param {z} Angle to rotate around Z axis in degrees. + * @returns {quat} out + * @function + */ +function fromEuler(out, x, y, z) { + var halfToRad = 0.5 * Math.PI / 180.0; + x *= halfToRad; + y *= halfToRad; + z *= halfToRad; + + var sx = Math.sin(x); + var cx = Math.cos(x); + var sy = Math.sin(y); + var cy = Math.cos(y); + var sz = Math.sin(z); + var cz = Math.cos(z); + + out[0] = sx * cy * cz - cx * sy * sz; + out[1] = cx * sy * cz + sx * cy * sz; + out[2] = cx * cy * sz - sx * sy * cz; + out[3] = cx * cy * cz + sx * sy * sz; + + return out; +} + +/** + * Returns a string representation of a quatenion + * + * @param {quat} a vector to represent as a string + * @returns {String} string representation of the vector + */ +function str(a) { + return 'quat(' + a[0] + ', ' + a[1] + ', ' + a[2] + ', ' + a[3] + ')'; +} + +/** + * Creates a new quat initialized with values from an existing quaternion + * + * @param {quat} a quaternion to clone + * @returns {quat} a new quaternion + * @function + */ +var clone = exports.clone = vec4.clone; + +/** + * Creates a new quat initialized with the given values + * + * @param {Number} x X component + * @param {Number} y Y component + * @param {Number} z Z component + * @param {Number} w W component + * @returns {quat} a new quaternion + * @function + */ +var fromValues = exports.fromValues = vec4.fromValues; + +/** + * Copy the values from one quat to another + * + * @param {quat} out the receiving quaternion + * @param {quat} a the source quaternion + * @returns {quat} out + * @function + */ +var copy = exports.copy = vec4.copy; + +/** + * Set the components of a quat to the given values + * + * @param {quat} out the receiving quaternion + * @param {Number} x X component + * @param {Number} y Y component + * @param {Number} z Z component + * @param {Number} w W component + * @returns {quat} out + * @function + */ +var set = exports.set = vec4.set; + +/** + * Adds two quat's + * + * @param {quat} out the receiving quaternion + * @param {quat} a the first operand + * @param {quat} b the second operand + * @returns {quat} out + * @function + */ +var add = exports.add = vec4.add; + +/** + * Alias for {@link quat.multiply} + * @function + */ +var mul = exports.mul = multiply; + +/** + * Scales a quat by a scalar number + * + * @param {quat} out the receiving vector + * @param {quat} a the vector to scale + * @param {Number} b amount to scale the vector by + * @returns {quat} out + * @function + */ +var scale = exports.scale = vec4.scale; + +/** + * Calculates the dot product of two quat's + * + * @param {quat} a the first operand + * @param {quat} b the second operand + * @returns {Number} dot product of a and b + * @function + */ +var dot = exports.dot = vec4.dot; + +/** + * Performs a linear interpolation between two quat's + * + * @param {quat} out the receiving quaternion + * @param {quat} a the first operand + * @param {quat} b the second operand + * @param {Number} t interpolation amount between the two inputs + * @returns {quat} out + * @function + */ +var lerp = exports.lerp = vec4.lerp; + +/** + * Calculates the length of a quat + * + * @param {quat} a vector to calculate length of + * @returns {Number} length of a + */ +var length = exports.length = vec4.length; + +/** + * Alias for {@link quat.length} + * @function + */ +var len = exports.len = length; + +/** + * Calculates the squared length of a quat + * + * @param {quat} a vector to calculate squared length of + * @returns {Number} squared length of a + * @function + */ +var squaredLength = exports.squaredLength = vec4.squaredLength; + +/** + * Alias for {@link quat.squaredLength} + * @function + */ +var sqrLen = exports.sqrLen = squaredLength; + +/** + * Normalize a quat + * + * @param {quat} out the receiving quaternion + * @param {quat} a quaternion to normalize + * @returns {quat} out + * @function + */ +var normalize = exports.normalize = vec4.normalize; + +/** + * Returns whether or not the quaternions have exactly the same elements in the same position (when compared with ===) + * + * @param {quat} a The first quaternion. + * @param {quat} b The second quaternion. + * @returns {Boolean} True if the vectors are equal, false otherwise. + */ +var exactEquals = exports.exactEquals = vec4.exactEquals; + +/** + * Returns whether or not the quaternions have approximately the same elements in the same position. + * + * @param {quat} a The first vector. + * @param {quat} b The second vector. + * @returns {Boolean} True if the vectors are equal, false otherwise. + */ +var equals = exports.equals = vec4.equals; + +/** + * Sets a quaternion to represent the shortest rotation from one + * vector to another. + * + * Both vectors are assumed to be unit length. + * + * @param {quat} out the receiving quaternion. + * @param {vec3} a the initial vector + * @param {vec3} b the destination vector + * @returns {quat} out + */ +var rotationTo = exports.rotationTo = function () { + var tmpvec3 = vec3.create(); + var xUnitVec3 = vec3.fromValues(1, 0, 0); + var yUnitVec3 = vec3.fromValues(0, 1, 0); + + return function (out, a, b) { + var dot = vec3.dot(a, b); + if (dot < -0.999999) { + vec3.cross(tmpvec3, xUnitVec3, a); + if (vec3.len(tmpvec3) < 0.000001) vec3.cross(tmpvec3, yUnitVec3, a); + vec3.normalize(tmpvec3, tmpvec3); + setAxisAngle(out, tmpvec3, Math.PI); + return out; + } else if (dot > 0.999999) { + out[0] = 0; + out[1] = 0; + out[2] = 0; + out[3] = 1; + return out; + } else { + vec3.cross(tmpvec3, a, b); + out[0] = tmpvec3[0]; + out[1] = tmpvec3[1]; + out[2] = tmpvec3[2]; + out[3] = 1 + dot; + return normalize(out, out); + } + }; +}(); + +/** + * Performs a spherical linear interpolation with two control points + * + * @param {quat} out the receiving quaternion + * @param {quat} a the first operand + * @param {quat} b the second operand + * @param {quat} c the third operand + * @param {quat} d the fourth operand + * @param {Number} t interpolation amount + * @returns {quat} out + */ +var sqlerp = exports.sqlerp = function () { + var temp1 = create(); + var temp2 = create(); + + return function (out, a, b, c, d, t) { + slerp(temp1, a, d, t); + slerp(temp2, b, c, t); + slerp(out, temp1, temp2, 2 * t * (1 - t)); + + return out; + }; +}(); + +/** + * Sets the specified quaternion with values corresponding to the given + * axes. Each axis is a vec3 and is expected to be unit length and + * perpendicular to all other specified axes. + * + * @param {vec3} view the vector representing the viewing direction + * @param {vec3} right the vector representing the local "right" direction + * @param {vec3} up the vector representing the local "up" direction + * @returns {quat} out + */ +var setAxes = exports.setAxes = function () { + var matr = mat3.create(); + + return function (out, view, right, up) { + matr[0] = right[0]; + matr[3] = right[1]; + matr[6] = right[2]; + + matr[1] = up[0]; + matr[4] = up[1]; + matr[7] = up[2]; + + matr[2] = -view[0]; + matr[5] = -view[1]; + matr[8] = -view[2]; + + return normalize(out, fromMat3(out, matr)); + }; +}(); + +/***/ }), +/* 9 */ +/***/ (function(module, exports, __webpack_require__) { + +"use strict"; + + +Object.defineProperty(exports, "__esModule", { + value: true +}); +exports.forEach = exports.sqrLen = exports.sqrDist = exports.dist = exports.div = exports.mul = exports.sub = exports.len = undefined; +exports.create = create; +exports.clone = clone; +exports.fromValues = fromValues; +exports.copy = copy; +exports.set = set; +exports.add = add; +exports.subtract = subtract; +exports.multiply = multiply; +exports.divide = divide; +exports.ceil = ceil; +exports.floor = floor; +exports.min = min; +exports.max = max; +exports.round = round; +exports.scale = scale; +exports.scaleAndAdd = scaleAndAdd; +exports.distance = distance; +exports.squaredDistance = squaredDistance; +exports.length = length; +exports.squaredLength = squaredLength; +exports.negate = negate; +exports.inverse = inverse; +exports.normalize = normalize; +exports.dot = dot; +exports.cross = cross; +exports.lerp = lerp; +exports.random = random; +exports.transformMat2 = transformMat2; +exports.transformMat2d = transformMat2d; +exports.transformMat3 = transformMat3; +exports.transformMat4 = transformMat4; +exports.str = str; +exports.exactEquals = exactEquals; +exports.equals = equals; + +var _common = __webpack_require__(0); + +var glMatrix = _interopRequireWildcard(_common); + +function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) newObj[key] = obj[key]; } } newObj.default = obj; return newObj; } } + +/** + * 2 Dimensional Vector + * @module vec2 + */ + +/** + * Creates a new, empty vec2 + * + * @returns {vec2} a new 2D vector + */ +function create() { + var out = new glMatrix.ARRAY_TYPE(2); + out[0] = 0; + out[1] = 0; + return out; +} + +/** + * Creates a new vec2 initialized with values from an existing vector + * + * @param {vec2} a vector to clone + * @returns {vec2} a new 2D vector + */ +/* Copyright (c) 2015, Brandon Jones, Colin MacKenzie IV. + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in +all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +THE SOFTWARE. */ + +function clone(a) { + var out = new glMatrix.ARRAY_TYPE(2); + out[0] = a[0]; + out[1] = a[1]; + return out; +} + +/** + * Creates a new vec2 initialized with the given values + * + * @param {Number} x X component + * @param {Number} y Y component + * @returns {vec2} a new 2D vector + */ +function fromValues(x, y) { + var out = new glMatrix.ARRAY_TYPE(2); + out[0] = x; + out[1] = y; + return out; +} + +/** + * Copy the values from one vec2 to another + * + * @param {vec2} out the receiving vector + * @param {vec2} a the source vector + * @returns {vec2} out + */ +function copy(out, a) { + out[0] = a[0]; + out[1] = a[1]; + return out; +} + +/** + * Set the components of a vec2 to the given values + * + * @param {vec2} out the receiving vector + * @param {Number} x X component + * @param {Number} y Y component + * @returns {vec2} out + */ +function set(out, x, y) { + out[0] = x; + out[1] = y; + return out; +} + +/** + * Adds two vec2's + * + * @param {vec2} out the receiving vector + * @param {vec2} a the first operand + * @param {vec2} b the second operand + * @returns {vec2} out + */ +function add(out, a, b) { + out[0] = a[0] + b[0]; + out[1] = a[1] + b[1]; + return out; +} + +/** + * Subtracts vector b from vector a + * + * @param {vec2} out the receiving vector + * @param {vec2} a the first operand + * @param {vec2} b the second operand + * @returns {vec2} out + */ +function subtract(out, a, b) { + out[0] = a[0] - b[0]; + out[1] = a[1] - b[1]; + return out; +} + +/** + * Multiplies two vec2's + * + * @param {vec2} out the receiving vector + * @param {vec2} a the first operand + * @param {vec2} b the second operand + * @returns {vec2} out + */ +function multiply(out, a, b) { + out[0] = a[0] * b[0]; + out[1] = a[1] * b[1]; + return out; +}; + +/** + * Divides two vec2's + * + * @param {vec2} out the receiving vector + * @param {vec2} a the first operand + * @param {vec2} b the second operand + * @returns {vec2} out + */ +function divide(out, a, b) { + out[0] = a[0] / b[0]; + out[1] = a[1] / b[1]; + return out; +}; + +/** + * Math.ceil the components of a vec2 + * + * @param {vec2} out the receiving vector + * @param {vec2} a vector to ceil + * @returns {vec2} out + */ +function ceil(out, a) { + out[0] = Math.ceil(a[0]); + out[1] = Math.ceil(a[1]); + return out; +}; + +/** + * Math.floor the components of a vec2 + * + * @param {vec2} out the receiving vector + * @param {vec2} a vector to floor + * @returns {vec2} out + */ +function floor(out, a) { + out[0] = Math.floor(a[0]); + out[1] = Math.floor(a[1]); + return out; +}; + +/** + * Returns the minimum of two vec2's + * + * @param {vec2} out the receiving vector + * @param {vec2} a the first operand + * @param {vec2} b the second operand + * @returns {vec2} out + */ +function min(out, a, b) { + out[0] = Math.min(a[0], b[0]); + out[1] = Math.min(a[1], b[1]); + return out; +}; + +/** + * Returns the maximum of two vec2's + * + * @param {vec2} out the receiving vector + * @param {vec2} a the first operand + * @param {vec2} b the second operand + * @returns {vec2} out + */ +function max(out, a, b) { + out[0] = Math.max(a[0], b[0]); + out[1] = Math.max(a[1], b[1]); + return out; +}; + +/** + * Math.round the components of a vec2 + * + * @param {vec2} out the receiving vector + * @param {vec2} a vector to round + * @returns {vec2} out + */ +function round(out, a) { + out[0] = Math.round(a[0]); + out[1] = Math.round(a[1]); + return out; +}; + +/** + * Scales a vec2 by a scalar number + * + * @param {vec2} out the receiving vector + * @param {vec2} a the vector to scale + * @param {Number} b amount to scale the vector by + * @returns {vec2} out + */ +function scale(out, a, b) { + out[0] = a[0] * b; + out[1] = a[1] * b; + return out; +}; + +/** + * Adds two vec2's after scaling the second operand by a scalar value + * + * @param {vec2} out the receiving vector + * @param {vec2} a the first operand + * @param {vec2} b the second operand + * @param {Number} scale the amount to scale b by before adding + * @returns {vec2} out + */ +function scaleAndAdd(out, a, b, scale) { + out[0] = a[0] + b[0] * scale; + out[1] = a[1] + b[1] * scale; + return out; +}; + +/** + * Calculates the euclidian distance between two vec2's + * + * @param {vec2} a the first operand + * @param {vec2} b the second operand + * @returns {Number} distance between a and b + */ +function distance(a, b) { + var x = b[0] - a[0], + y = b[1] - a[1]; + return Math.sqrt(x * x + y * y); +}; + +/** + * Calculates the squared euclidian distance between two vec2's + * + * @param {vec2} a the first operand + * @param {vec2} b the second operand + * @returns {Number} squared distance between a and b + */ +function squaredDistance(a, b) { + var x = b[0] - a[0], + y = b[1] - a[1]; + return x * x + y * y; +}; + +/** + * Calculates the length of a vec2 + * + * @param {vec2} a vector to calculate length of + * @returns {Number} length of a + */ +function length(a) { + var x = a[0], + y = a[1]; + return Math.sqrt(x * x + y * y); +}; + +/** + * Calculates the squared length of a vec2 + * + * @param {vec2} a vector to calculate squared length of + * @returns {Number} squared length of a + */ +function squaredLength(a) { + var x = a[0], + y = a[1]; + return x * x + y * y; +}; + +/** + * Negates the components of a vec2 + * + * @param {vec2} out the receiving vector + * @param {vec2} a vector to negate + * @returns {vec2} out + */ +function negate(out, a) { + out[0] = -a[0]; + out[1] = -a[1]; + return out; +}; + +/** + * Returns the inverse of the components of a vec2 + * + * @param {vec2} out the receiving vector + * @param {vec2} a vector to invert + * @returns {vec2} out + */ +function inverse(out, a) { + out[0] = 1.0 / a[0]; + out[1] = 1.0 / a[1]; + return out; +}; + +/** + * Normalize a vec2 + * + * @param {vec2} out the receiving vector + * @param {vec2} a vector to normalize + * @returns {vec2} out + */ +function normalize(out, a) { + var x = a[0], + y = a[1]; + var len = x * x + y * y; + if (len > 0) { + //TODO: evaluate use of glm_invsqrt here? + len = 1 / Math.sqrt(len); + out[0] = a[0] * len; + out[1] = a[1] * len; + } + return out; +}; + +/** + * Calculates the dot product of two vec2's + * + * @param {vec2} a the first operand + * @param {vec2} b the second operand + * @returns {Number} dot product of a and b + */ +function dot(a, b) { + return a[0] * b[0] + a[1] * b[1]; +}; + +/** + * Computes the cross product of two vec2's + * Note that the cross product must by definition produce a 3D vector + * + * @param {vec3} out the receiving vector + * @param {vec2} a the first operand + * @param {vec2} b the second operand + * @returns {vec3} out + */ +function cross(out, a, b) { + var z = a[0] * b[1] - a[1] * b[0]; + out[0] = out[1] = 0; + out[2] = z; + return out; +}; + +/** + * Performs a linear interpolation between two vec2's + * + * @param {vec2} out the receiving vector + * @param {vec2} a the first operand + * @param {vec2} b the second operand + * @param {Number} t interpolation amount between the two inputs + * @returns {vec2} out + */ +function lerp(out, a, b, t) { + var ax = a[0], + ay = a[1]; + out[0] = ax + t * (b[0] - ax); + out[1] = ay + t * (b[1] - ay); + return out; +}; + +/** + * Generates a random vector with the given scale + * + * @param {vec2} out the receiving vector + * @param {Number} [scale] Length of the resulting vector. If ommitted, a unit vector will be returned + * @returns {vec2} out + */ +function random(out, scale) { + scale = scale || 1.0; + var r = glMatrix.RANDOM() * 2.0 * Math.PI; + out[0] = Math.cos(r) * scale; + out[1] = Math.sin(r) * scale; + return out; +}; + +/** + * Transforms the vec2 with a mat2 + * + * @param {vec2} out the receiving vector + * @param {vec2} a the vector to transform + * @param {mat2} m matrix to transform with + * @returns {vec2} out + */ +function transformMat2(out, a, m) { + var x = a[0], + y = a[1]; + out[0] = m[0] * x + m[2] * y; + out[1] = m[1] * x + m[3] * y; + return out; +}; + +/** + * Transforms the vec2 with a mat2d + * + * @param {vec2} out the receiving vector + * @param {vec2} a the vector to transform + * @param {mat2d} m matrix to transform with + * @returns {vec2} out + */ +function transformMat2d(out, a, m) { + var x = a[0], + y = a[1]; + out[0] = m[0] * x + m[2] * y + m[4]; + out[1] = m[1] * x + m[3] * y + m[5]; + return out; +}; + +/** + * Transforms the vec2 with a mat3 + * 3rd vector component is implicitly '1' + * + * @param {vec2} out the receiving vector + * @param {vec2} a the vector to transform + * @param {mat3} m matrix to transform with + * @returns {vec2} out + */ +function transformMat3(out, a, m) { + var x = a[0], + y = a[1]; + out[0] = m[0] * x + m[3] * y + m[6]; + out[1] = m[1] * x + m[4] * y + m[7]; + return out; +}; + +/** + * Transforms the vec2 with a mat4 + * 3rd vector component is implicitly '0' + * 4th vector component is implicitly '1' + * + * @param {vec2} out the receiving vector + * @param {vec2} a the vector to transform + * @param {mat4} m matrix to transform with + * @returns {vec2} out + */ +function transformMat4(out, a, m) { + var x = a[0]; + var y = a[1]; + out[0] = m[0] * x + m[4] * y + m[12]; + out[1] = m[1] * x + m[5] * y + m[13]; + return out; +} + +/** + * Returns a string representation of a vector + * + * @param {vec2} a vector to represent as a string + * @returns {String} string representation of the vector + */ +function str(a) { + return 'vec2(' + a[0] + ', ' + a[1] + ')'; +} + +/** + * Returns whether or not the vectors exactly have the same elements in the same position (when compared with ===) + * + * @param {vec2} a The first vector. + * @param {vec2} b The second vector. + * @returns {Boolean} True if the vectors are equal, false otherwise. + */ +function exactEquals(a, b) { + return a[0] === b[0] && a[1] === b[1]; +} + +/** + * Returns whether or not the vectors have approximately the same elements in the same position. + * + * @param {vec2} a The first vector. + * @param {vec2} b The second vector. + * @returns {Boolean} True if the vectors are equal, false otherwise. + */ +function equals(a, b) { + var a0 = a[0], + a1 = a[1]; + var b0 = b[0], + b1 = b[1]; + return Math.abs(a0 - b0) <= glMatrix.EPSILON * Math.max(1.0, Math.abs(a0), Math.abs(b0)) && Math.abs(a1 - b1) <= glMatrix.EPSILON * Math.max(1.0, Math.abs(a1), Math.abs(b1)); +} + +/** + * Alias for {@link vec2.length} + * @function + */ +var len = exports.len = length; + +/** + * Alias for {@link vec2.subtract} + * @function + */ +var sub = exports.sub = subtract; + +/** + * Alias for {@link vec2.multiply} + * @function + */ +var mul = exports.mul = multiply; + +/** + * Alias for {@link vec2.divide} + * @function + */ +var div = exports.div = divide; + +/** + * Alias for {@link vec2.distance} + * @function + */ +var dist = exports.dist = distance; + +/** + * Alias for {@link vec2.squaredDistance} + * @function + */ +var sqrDist = exports.sqrDist = squaredDistance; + +/** + * Alias for {@link vec2.squaredLength} + * @function + */ +var sqrLen = exports.sqrLen = squaredLength; + +/** + * Perform some operation over an array of vec2s. + * + * @param {Array} a the array of vectors to iterate over + * @param {Number} stride Number of elements between the start of each vec2. If 0 assumes tightly packed + * @param {Number} offset Number of elements to skip at the beginning of the array + * @param {Number} count Number of vec2s to iterate over. If 0 iterates over entire array + * @param {Function} fn Function to call for each vector in the array + * @param {Object} [arg] additional argument to pass to fn + * @returns {Array} a + * @function + */ +var forEach = exports.forEach = function () { + var vec = create(); + + return function (a, stride, offset, count, fn, arg) { + var i = void 0, + l = void 0; + if (!stride) { + stride = 2; + } + + if (!offset) { + offset = 0; + } + + if (count) { + l = Math.min(count * stride + offset, a.length); + } else { + l = a.length; + } + + for (i = offset; i < l; i += stride) { + vec[0] = a[i];vec[1] = a[i + 1]; + fn(vec, vec, arg); + a[i] = vec[0];a[i + 1] = vec[1]; + } + + return a; + }; +}(); + +/***/ }) +/******/ ]); +}); \ No newline at end of file diff --git a/gl-util.js b/gl-util.js new file mode 100644 index 0000000..b343383 --- /dev/null +++ b/gl-util.js @@ -0,0 +1,105 @@ +var gl; + +const ATTRIB_POS = 0; +const ATTRIB_NORMAL = 1; +const ATTRIB_COLOR = 2; +const ATTRIB_TEXCOORDS0 = 3; +const ATTRIB_TEXCOORDS1 = 4; + +function +loadTexture(url) +{ + if(gl === undefined) + return null; + + const texid = gl.createTexture(); + + gl.bindTexture(gl.TEXTURE_2D, texid); + gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, + 1, 1, 0, gl.RGBA, gl.UNSIGNED_BYTE, + new Uint8Array([255, 255, 255, 255])); + + const image = new Image(); + image.onload = function() { + gl.bindTexture(gl.TEXTURE_2D, texid); + gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, + gl.RGBA, gl.UNSIGNED_BYTE, image); + gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.REPEAT); + gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.REPEAT); + gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR); + gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.LINEAR); + }; + image.src = url; + + return texid; +} + +function +loadShaders(vs, fs) +{ + const shaderProgram = initShaderProgram(vs, fs); + + programInfo = { + program: shaderProgram, + a: [ + gl.getAttribLocation(shaderProgram, 'in_pos'), + gl.getAttribLocation(shaderProgram, 'in_normal'), + gl.getAttribLocation(shaderProgram, 'in_color'), + gl.getAttribLocation(shaderProgram, 'in_tex0'), + gl.getAttribLocation(shaderProgram, 'in_tex1'), + ], + u: { + u_proj: gl.getUniformLocation(shaderProgram, 'u_proj'), + u_view: gl.getUniformLocation(shaderProgram, 'u_view'), + u_world: gl.getUniformLocation(shaderProgram, 'u_world'), + u_env: gl.getUniformLocation(shaderProgram, 'u_env'), + u_matColor: gl.getUniformLocation(shaderProgram, 'u_matColor'), + u_surfaceProps: gl.getUniformLocation(shaderProgram, 'u_surfaceProps'), + u_ambLight: gl.getUniformLocation(shaderProgram, 'u_ambLight'), + u_lightDir: gl.getUniformLocation(shaderProgram, 'u_lightDir'), + u_lightCol: gl.getUniformLocation(shaderProgram, 'u_lightCol'), + u_alphaRef: gl.getUniformLocation(shaderProgram, 'u_alphaRef'), + u_tex0: gl.getUniformLocation(shaderProgram, 'tex0'), + u_tex1: gl.getUniformLocation(shaderProgram, 'tex1'), + u_tex2: gl.getUniformLocation(shaderProgram, 'tex2'), + }, + }; +console.log(programInfo.u.u_alphaRef); + return programInfo; +} + +function +initShaderProgram(vsSource, fsSource) +{ + const vertexShader = loadShader(gl.VERTEX_SHADER, vsSource); + const fragmentShader = loadShader(gl.FRAGMENT_SHADER, fsSource); + + const shaderProgram = gl.createProgram(); + gl.attachShader(shaderProgram, vertexShader); + gl.attachShader(shaderProgram, fragmentShader); + gl.linkProgram(shaderProgram); + + if(!gl.getProgramParameter(shaderProgram, gl.LINK_STATUS)){ + alert('Unable to initialize the shader program: ' + gl.getProgramInfoLog(shaderProgram)); + return null; + } + + return shaderProgram; +} + +function +loadShader(type, source) +{ + const shader = gl.createShader(type); + + gl.shaderSource(shader, source); + gl.compileShader(shader); + + if(!gl.getShaderParameter(shader, gl.COMPILE_STATUS)){ + alert('An error occurred compiling the shaders: ' + gl.getShaderInfoLog(shader)); + gl.deleteShader(shader); + return null; + } + + return shader; +} diff --git a/index.html b/index.html new file mode 100644 index 0000000..422cee4 --- /dev/null +++ b/index.html @@ -0,0 +1,83 @@ + + + + WebGL + + + + + +
+III +VC +SA +
+ +
+ +
+ + +
+
+ +
+
+ + + + + + + + + + + + + + diff --git a/loaddata.js b/loaddata.js new file mode 100644 index 0000000..409c06a --- /dev/null +++ b/loaddata.js @@ -0,0 +1,213 @@ +var VehicleColours = []; +var ModelInfos = {}; +var ModelInfosName = {}; +var CurrentModel = null; + +function +SetCarColors(cols) +{ + carColors = [ + [ 0, 0, 0, 255 ], + [ 0, 0, 0, 255 ], + [ 0, 0, 0, 255 ], + [ 0, 0, 0, 255 ] + ]; + if(cols !== undefined) + for(let i = 0; i < cols.length; i++){ + let col = VehicleColours[cols[i]]; + carColors[i][0] = col[0]; + carColors[i][1] = col[1]; + carColors[i][2] = col[2]; + } +} + +function +LoadColors(cols) +{ + SetCarColors(cols); + setVehicleColors(modelinfo, carColors[0], carColors[1], carColors[2], carColors[3]); +} + +function +SelectModel(model) +{ + let colortable = document.getElementById('colors'); + removeChildren(colortable); + + CurrentModel = ModelInfosName[model]; + let col1 = [ 0, 0, 0, 255 ]; + let col2 = [ 0, 0, 0, 255 ]; + for(let i = 0; i < CurrentModel.colors.length; i++){ + let c = CurrentModel.colors[i]; + let c1 = VehicleColours[c[0]]; + let c2 = VehicleColours[c[1]]; + if(i == 0){ + col1[0] = c1[0]; + col1[1] = c1[1]; + col1[2] = c1[2]; + col2[0] = c2[0]; + col2[1] = c2[1]; + col2[2] = c2[2]; + } + let tr = document.createElement('tr'); + for(let j = 0; j < c.length; j++){ + let td = document.createElement('td'); + td.width = "16px"; + td.height = "16px"; + let col = VehicleColours[c[j]]; + td.style = "background-color: rgb("+col[0]+","+col[1]+","+col[2]+")"; + tr.appendChild(td); + } + tr.onclick = function() { LoadColors(c); }; + colortable.appendChild(tr); + } + + camDist = 5.0; + camPitch = 0.3; + camYaw = 1.0; + SetCarColors(CurrentModel.colors[0]); + + loadCar(model + ".dff"); +} + +function +StartUI() +{ + let objects = document.getElementById('objects'); + removeChildren(objects); + for(let model in ModelInfosName){ + let option = document.createElement('option'); + option.innerHTML = model; + option.onclick = function(){ SelectModel(model); }; + objects.appendChild(option); + } +} + +function +LoadVehicle(fields) +{ + let id = Number(fields[0]); + let mi = {}; + mi.id = id; + mi.type = "vehicle"; + mi.model = fields[1]; + mi.txd = fields[2]; + mi.vehtype = fields[3]; + mi.handling = fields[4]; + if(mi.vehtype == "car"){ + // TODO: check SA + mi.wheelId = Number(fields[11]); + mi.wheelScale = Number(fields[12]); + } + mi.colors = []; + ModelInfos[mi.id] = mi; + ModelInfosName[mi.model] = mi; +} + +function +LoadColour(fields) +{ + let r = Number(fields[0]); + let g = Number(fields[1]); + let b = Number(fields[2]); + VehicleColours.push([r, g, b]); +} + +function +LoadVehicleColour(fields) +{ + let mi = ModelInfosName[fields[0]]; + for(let i = 1; i < fields.length; i += 2){ + let c1 = Number(fields[i]); + let c2 = Number(fields[i+1]); + mi.colors.push([c1, c2]); + } +} + +function +LoadVehicleColour4(fields) +{ + let mi = ModelInfosName[fields[0]]; + for(let i = 1; i < fields.length; i += 4){ + let c1 = Number(fields[i]); + let c2 = Number(fields[i+1]); + let c3 = Number(fields[i+2]); + let c4 = Number(fields[i+3]); + mi.colors.push([c1, c2, c3, c4]); + } +} + +function +LoadObjectTypes(text) +{ + LoadSectionedFile(text, { + "cars": LoadVehicle + }); +} + +function +LoadVehicleColours(text) +{ + LoadSectionedFile(text, { + "col": LoadColour, + "car": LoadVehicleColour, + "car4": LoadVehicleColour4 + }); +} + +function +LoadSectionedFile(text, sections) +{ + let section = "end"; + let lines = text.split("\n"); + for(let i = 0; i < lines.length; i++){ + let line = lines[i].replace(/,/g, " ").replace(/#.*/g, "").trim().toLowerCase(); + if(line.length == 0) + continue; + let fields = line.split(/[\t ]+/); + + if(section == "end"){ + section = fields[0]; + continue; + } + if(fields[0] == "end"){ + section = "end"; + continue; + } + + if(section in sections) + sections[section](fields); + } +} + +function +loadText(filename, cb) +{ + let req = new XMLHttpRequest(); + req.open("GET", DataDirPath + "/" + filename, true); + req.responseType = "text"; + + req.onload = function(oEvent){ + cb(req.response); + }; + + req.send(null); +} + +function +loadVehicleViewer(idefile, CB) +{ + VehicleColours = []; + ModelInfos = {}; + ModelInfosName = {}; + CurrentModel = null; + + loadText(idefile, function(text){ + LoadObjectTypes(text); + loadText("carcols.dat", function(text){ + LoadVehicleColours(text); + StartUI(); + CB(); + }); + }); +} \ No newline at end of file diff --git a/main.js b/main.js new file mode 100644 index 0000000..1f999a6 --- /dev/null +++ b/main.js @@ -0,0 +1,430 @@ +var DataDirPath; +var ModelsDirPath; +var TexturesDirPath; + +// init info +var isIIICar; +var isSACar; +var carColors; + +// the scene +var running = false; +var myclump; +var modelinfo; +var camPitch; +var camYaw; +var camDist; + +// gl things +var state = {}; +var whitetex; +var camera; + +var envFrame; +var defaultProgram; +var envMapProgram; +var carPS2Program; + +function deg2rad(d) { return d / 180.0 * Math.PI; } + +var rotating, zooming; + +function +mouseDown(e) +{ + if(e.button == 0) + rotating = true; + else if(e.button == 1) + zooming = true; + old_x = e.pageX; + old_y = e.pageY; + e.preventDefault(); +} + +function +mouseUp(e) +{ + rotating = false; + zooming = false; +} + +function +mouseMove(e) +{ + let dX, dY; + if(rotating){ + dX = (e.pageX-old_x)*2*Math.PI/gl.canvas.width, + dY = (e.pageY-old_y)*2*Math.PI/gl.canvas.height; + + camYaw -= dX; + camPitch += dY; + if(camPitch > Math.PI/2 - 0.01) camPitch = Math.PI/2 - 0.01 + if(camPitch < -Math.PI/2 + 0.01) camPitch = -Math.PI/2 + 0.01 + + old_x = e.pageX; + old_y = e.pageY; + e.preventDefault(); + } + if(zooming){ + dY = (e.pageY-old_y)/gl.canvas.height; + + camDist += dY; + if(camDist < 0.1) camDist = 0.1; + + old_x = e.pageX; + e.preventDefault(); + } +}; + +function +InitRW() +{ +console.log("InitRW()"); + let canvas = document.querySelector('#glcanvas'); + gl = canvas.getContext('webgl'); + + if(!gl){ + alert('Unable to initialize WebGL. Your browser or machine may not support it.'); + return; + } + + canvas.addEventListener("mousedown", mouseDown, false); + canvas.addEventListener("mouseup", mouseUp, false); + canvas.addEventListener("mouseout", mouseUp, false); + canvas.addEventListener("mousemove", mouseMove, false); + + whitetex = loadTexture("textures/white.png"); + + defaultProgram = loadShaders(defaultVS, defaultFS); + envMapProgram = loadShaders(envVS, envFS); + carPS2Program = loadShaders(carPS2VS, carPS2FS); + + state.alphaRef = 0.1; + state.projectionMatrix = mat4.create(); + state.viewMatrix = mat4.create(); + state.worldMatrix = mat4.create(); + state.envMatrix = mat4.create(); + state.matColor = vec4.create(); + state.surfaceProps = vec4.create(); + state.ambLight = vec3.fromValues(0.4, 0.4, 0.4); + const alpha = 45; + const beta = 45; + state.lightDir = vec3.fromValues( + -Math.cos(deg2rad(beta))*Math.cos(deg2rad(alpha)), + -Math.sin(deg2rad(beta))*Math.cos(deg2rad(alpha)), + -Math.sin(deg2rad(alpha)) + ); + state.lightCol = vec3.fromValues(1.0, 1.0, 1.0); + + + AttachPlugins(); + + camera = RwCameraCreate(); + camera.nearPlane = 0.1; + camera.farPlane = 100.0; + let frm = RwFrameCreate(); + RwCameraSetFrame(camera, frm); + + const fov = deg2rad(70); + const aspect = gl.canvas.clientWidth / gl.canvas.clientHeight; + camera.viewWindow[1] = Math.tan(fov / 2); + camera.viewWindow[0] = camera.viewWindow[1]*aspect; + + envFrame = RwFrameCreate(); + mat4.rotateX(envFrame.matrix, envFrame.matrix, deg2rad(60)); + rwFrameSynchLTM(envFrame); +} + +function +displayFrames(frame, parelem) +{ + let li = document.createElement('li'); + li.innerHTML = frame.name; + for(let i = 0; i < frame.objects.length; i++){ + let o = frame.objects[i]; + if(o.type == rwID_ATOMIC){ + let checkbox = document.createElement('input'); + checkbox.type = "checkbox"; + checkbox.onclick = function() { o.visible = checkbox.checked; }; + checkbox.checked = o.visible; + li.appendChild(checkbox); + } + } + parelem.appendChild(li); + if(frame.child){ + let ul = document.createElement('ul'); + parelem.appendChild(ul); + for(let c = frame.child; c != null; c = c.next) + displayFrames(c, ul); + } +} + +function +putControl() +{ +// let ctl = document.getElementById('control'); +// let reloadbtn = document.createElement('input'); +// reloadbtn.type = "button"; +// reloadbtn.value = "reload"; +// reloadbtn.onclick = reload; +// ctl.appendChild(reloadbtn); +} + + +function +loadCarIII(filename) +{ + loadDFF(filename, function(clump){ + myclump = clump; + modelinfo = processVehicle(myclump); + setupIIICar(myclump); + setVehicleColors(modelinfo, carColors[0], carColors[1]); + main(); + }); +} + +function +loadCarVC(filename) +{ + loadDFF(filename, function(clump){ + myclump = clump; + modelinfo = processVehicle(myclump); + setVehicleColors(modelinfo, carColors[0], carColors[1]); + main(); + }); +} + +function +loadCarSA(filename) +{ + loadDFF(filename, function(clump){ + myclump = clump; + modelinfo = processVehicle(myclump); + setupSACar(myclump); + setVehicleColors(modelinfo, + carColors[0], carColors[1], carColors[2], carColors[3]); +// setVehicleLightColors(modelinfo, +// [ 128, 0, 0, 255 ], +// [ 128, 0, 0, 255 ], +// [ 128, 0, 0, 255 ], +// [ 128, 0, 0, 255 ]); + main(); + }); +} + +function +loadModel(filename) +{ + loadDFF(filename, function(clump){ + myclump = clump; + main(); + }); +} + +function +removeChildren(x) +{ + while(x.firstChild) + x.removeChild(x.firstChild); +} + +function +main() +{ + let ul = document.getElementById('frames'); + removeChildren(ul); + displayFrames(myclump.frame, ul); + + if(!running){ + running = true; + + putControl(); + + let then = 0; + function render(now){ + now *= 0.001; // convert to seconds + const deltaTime = now - then; + then = now; + + drawScene(deltaTime); + + requestAnimationFrame(render); + } + requestAnimationFrame(render); + } +} + +function +setupIIICar(clump) +{ + for(let i = 0; i < clump.atomics.length; i++){ + let a = clump.atomics[i]; + a.pipeline = matFXPipe; + for(let j = 0; j < a.geometry.materials.length; j++){ + m = a.geometry.materials[j]; + if(m.surfaceProperties[1] <= 0.0) + continue; + m.matfx = { + type: 2, + envCoefficient: m.surfaceProperties[1], + envTex: RwTextureRead("reflection01", "") + }; + } + } +} + +function +setupSACar(clump) +{ + for(let i = 0; i < clump.atomics.length; i++){ + let a = clump.atomics[i]; + a.pipeline = carPipe; + for(let j = 0; j < a.geometry.materials.length; j++){ + m = a.geometry.materials[j]; + m.fxFlags = 0; + if(!m.matfx || m.matfx.type != 2) continue; + + if(m.matfx.envTex && m.envMap && m.envMap.shininess != 0){ + m.envMap.texture = m.matfx.envTex; + if(m.envMap.texture.name[0] == 'x') + m.fxFlags |= 2; + else + m.fxFlags |= 1; + } + + if(m.specMap && m.specMap.specularity != 0) + m.fxFlags |= 4; + } + } +} + +function +setVehicleColors(vehinfo, c1, c2, c3, c4) +{ + for(let i = 0; i < vehinfo.firstMaterials.length; i++) + vehinfo.firstMaterials[i].color = c1; + for(let i = 0; i < vehinfo.secondMaterials.length; i++) + vehinfo.secondMaterials[i].color = c2; + for(let i = 0; i < vehinfo.thirdMaterials.length; i++) + vehinfo.thirdMaterials[i].color = c3; + for(let i = 0; i < vehinfo.fourthMaterials.length; i++) + vehinfo.fourthMaterials[i].color = c4; +} + +function +setVehicleLightColors(vehinfo, c1, c2, c3, c4) +{ + for(let i = 0; i < vehinfo.firstLightMaterials.length; i++) + vehinfo.firstLightMaterials[i].color = c1; + for(let i = 0; i < vehinfo.secondLightMaterials.length; i++) + vehinfo.secondLightMaterials[i].color = c2; + for(let i = 0; i < vehinfo.thirdLightMaterials.length; i++) + vehinfo.thirdLightMaterials[i].color = c3; + for(let i = 0; i < vehinfo.fourthLightMaterials.length; i++) + vehinfo.fourthLightMaterials[i].color = c4; +} + +function +findEditableMaterials(geo, vehinfo) +{ + for(let i = 0; i < geo.materials.length; i++){ + m = geo.materials[i]; + if(m.color[0] == 0x3C && m.color[1] == 0xFF && m.color[2] == 0) + vehinfo.firstMaterials.push(m); + else if(m.color[0] == 0xFF && m.color[1] == 0 && m.color[2] == 0xAF) + vehinfo.secondMaterials.push(m); + else if(m.color[0] == 0 && m.color[1] == 0xFF && m.color[2] == 0xFF) + vehinfo.thirdMaterials.push(m); + else if(m.color[0] == 0xFF && m.color[1] == 0x00 && m.color[2] == 0xFF) + vehinfo.fourthMaterials.push(m); + else if(m.color[0] == 0xFF && m.color[1] == 0xAF && m.color[2] == 0) + vehinfo.firstLightMaterials.push(m); + else if(m.color[0] == 0 && m.color[1] == 0xFF && m.color[2] == 0xC8) + vehinfo.secondLightMaterials.push(m); + else if(m.color[0] == 0xB9 && m.color[1] == 0xFF && m.color[2] == 0) + vehinfo.thirdLightMaterials.push(m); + else if(m.color[0] == 0xFF && m.color[1] == 0x3C && m.color[2] == 0) + vehinfo.fourthLightMaterials.push(m); + } +} + +function +processVehicle(clump) +{ + let vehicleInfo = { + firstMaterials: [], + secondMaterials: [], + thirdMaterials: [], + fourthMaterials: [], + firstLightMaterials: [], // front left + secondLightMaterials: [], // front right + thirdLightMaterials: [], // back left + fourthLightMaterials: [], // back right + clump: clump + }; + for(let i = 0; i < clump.atomics.length; i++){ + a = clump.atomics[i]; + f = a.frame; + if(f.name.endsWith("_dam") || + f.name.endsWith("_lo") || + f.name.endsWith("_vlo")) + a.visible = false; + findEditableMaterials(a.geometry, vehicleInfo); + } + return vehicleInfo; +} + +function +drawScene(deltaTime) +{ +// camYaw += deltaTime; + + gl.clearColor(0.5, 0.5, 0.5, 1.0); + gl.clearDepth(1.0); + gl.enable(gl.DEPTH_TEST); + gl.depthFunc(gl.LEQUAL); + + gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT); + + let x = camDist * Math.cos(camYaw)* Math.cos(camPitch); + let y = camDist * Math.sin(camYaw)* Math.cos(camPitch); + let z = camDist * Math.sin(camPitch); + RwFrameLookAt(camera.frame, + [ x, y, z ], + [ 0.0, 0.0, 0.0 ], + [ 0.0, 0.0, 1.0 ]); + rwFrameSynchLTM(camera.frame); + + RwCameraBeginUpdate(camera); + + RenderPass = 0; + RpClumpRender(myclump); + RenderPass = 1; + RpClumpRender(myclump); + RenderPass = -1; +} + + +function +loadDFF(filename, cb) +{ + let req = new XMLHttpRequest(); + req.open("GET", ModelsDirPath + "/" + filename, true); + req.responseType = "arraybuffer"; + + req.onload = function(oEvent){ + let arrayBuffer = req.response; + if(arrayBuffer){ + stream = RwStreamCreate(arrayBuffer); + + if(RwStreamFindChunk(stream, rwID_CLUMP)){ + let c = RpClumpStreamRead(stream); + if(c != null) + cb(c); + } + return null; + } + }; + + req.send(null); +} diff --git a/rw.js b/rw.js new file mode 100644 index 0000000..4bf7242 --- /dev/null +++ b/rw.js @@ -0,0 +1,556 @@ +// core +var rwID_STRUCT = 0x01; +var rwID_STRING = 0x02; +var rwID_EXTENSION = 0x03; +var rwID_CAMERA = 0x05; +var rwID_TEXTURE = 0x06; +var rwID_MATERIAL = 0x07; +var rwID_MATLIST = 0x08; +var rwID_FRAMELIST = 0x0E; +var rwID_GEOMETRY = 0x0F; +var rwID_CLUMP = 0x10; +var rwID_LIGHT = 0x12; +var rwID_ATOMIC = 0x14; +var rwID_GEOMETRYLIST = 0x1A; +// tk +var rwID_HANIMPLUGIN = 0x11E; +var rwID_MATERIALEFFECTSPLUGIN = 0x120; +// world +var rwID_BINMESHPLUGIN = 0x50E; +// R* +var rwID_NODENAME = 0x0253F2FE; +var rwID_ENVMAT = 0x0253F2FC; +var rwID_SPECMAT = 0x0253F2F6; + + +var frameTKList = {}; +var textureTKList = {}; +var materialTKList = {}; +var geometryTKList = {}; +var atomicTKList = {}; +var clumpTKList = {}; + +/* RwFrame */ + +function +rwSetHierarchyRoot(frame, root) +{ + frame.root = root; + for(frame = frame.child; frame != null; frame = frame.next) + rwSetHierarchyRoot(frame, root); +} + +function +RwFrameRemoveChild(c) +{ + let f = c.parent.child; + // remove as child + if(f == c) + c.parent.child = c.next; + else{ + while(f.next != c) + f = f.next; + f.next = c.next; + } + // now make this the root of a new hierarchy + c.parent = null; + c.next = null; + rwSetHierarchyRoot(c, c); +} + +function +RwFrameAddChild(p, child) +{ + if(child.parent != null) + RwFrameRemoveChild(child); + // append as child of p + if(p.child == null) + p.child = child; + else{ + let c; + for(c = p.child; c.next != null; c = c.next); + c.next = child; + } + child.next = null; + + child.parent = p; + rwSetHierarchyRoot(child, p.root); +} + +function +rwFrameSynchLTM(f) +{ + if(f.parent == null) + mat4.copy(f.ltm, f.matrix); + else + mat4.multiply(f.ltm, f.matrix, f.parent.ltm); + for(let c = f.child; c != null; c = c.next) + rwFrameSynchLTM(c); +} + +function +RwFrameLookAt(frm, pos, target, up) +{ + let at = vec3.create(); + vec3.subtract(at, target, pos); + vec3.normalize(at, at); + let left = vec3.create(); + vec3.cross(left, up, at); + vec3.normalize(left, left); + vec3.cross(up, at, left); + m = frm.matrix; + m[0] = left[0]; + m[1] = left[1]; + m[2] = left[2]; + m[4] = up[0]; + m[5] = up[1]; + m[6] = up[2]; + m[8] = at[0]; + m[9] = at[1]; + m[10] = at[2]; + m[12] = pos[0]; + m[13] = pos[1]; + m[14] = pos[2]; +} + +function +RwFrameCreate() +{ + let f = { + parent: null, + root: null, + child: null, + next: null, + matrix: mat4.create(), + ltm: mat4.create(), + objects: [], + name: "" + }; + f.root = f; + mat4.copy(f.ltm, f.matrix); + return f; +} + +/* RwCamera */ + +function +RwCameraCreate() +{ + cam = { + type: rwID_CAMERA, + frame: null, + viewWindow: [ 1.0, 1.0 ], + viewOffset: [ 0.0, 0.0 ], + nearPlane: [ 0.5 ], + farPlane: [ 10.0 ], + fogPlane: [ 5.0 ], + projmat: mat4.create() + }; + return cam; +} + +function +RwCameraSetFrame(c, f) +{ + c.frame = f; + f.objects.push(c); +} + +function +RwCameraBeginUpdate(cam) +{ + mat4.invert(state.viewMatrix, camera.frame.ltm); + state.viewMatrix[0] = -state.viewMatrix[0]; + state.viewMatrix[4] = -state.viewMatrix[4]; + state.viewMatrix[8] = -state.viewMatrix[8]; + state.viewMatrix[12] = -state.viewMatrix[12]; + + p = cam.projmat; + let xscl = 1.0/cam.viewWindow[0]; + let yscl = 1.0/cam.viewWindow[1]; + let zscl = 1.0/(cam.farPlane-cam.nearPlane); + + p[0] = xscl; + p[1] = 0; + p[2] = 0; + p[3] = 0; + + p[4] = 0; + p[5] = yscl; + p[6] = 0; + p[7] = 0; + + p[8] = cam.viewOffset[0]*xscl; + p[9] = cam.viewOffset[1]*yscl; + p[12] = -p[8]; + p[13] = -p[9]; + + p[10] = (cam.farPlane+cam.nearPlane)*zscl; + p[11] = 1.0; + p[14] = -2.0*cam.nearPlane*cam.farPlane*zscl; + p[15] = 0.0; + + mat4.copy(state.projectionMatrix, p); +} + +/* RwTexture */ + +function +RwTextureRead(name, mask) +{ + return { + name: name, + mask: mask, + tex: loadTexture(TexturesDirPath + "/" + name + ".png") + }; +} + +/* RpMaterial */ + +function +RpMaterialCreate() +{ + let mat = { + color: [ 255, 255, 255, 255 ], + surfaceProperties: [ 1.0, 1.0, 1.0 ] + }; + return mat; +} + +/* RpGeometry */ + +function +RpGeometryCreate(flags, numMorphTargets) +{ + let geo = { + numVertices: 0, // used for instancing + triangles: [], + morphTargets: [], + materials: [], + meshtype: 0, + meshes: [], + totalMeshIndices: 0 // used for instancing + }; + + let numTexCoords = (flags >> 16) & 0xFF; + if(numTexCoords == 0){ + if(flags & 0x04) numTexCoords = 1; + if(flags & 0x80) numTexCoords = 2; + } + geo.texCoords = []; + if(numTexCoords > 0) + while(numTexCoords--) + geo.texCoords.push([]); + + if(flags & 0x08) + geo.prelit = []; + + while(numMorphTargets--){ + let mt = { vertices: [] }; + if(flags & 0x10) + mt.normals = []; + geo.morphTargets.push(mt); + } + + return geo; +} + +/* RpAtomic */ + +function +RpAtomicCreate() +{ + return { + type: rwID_ATOMIC, + frame: null, + geometry: null, + visible: true, + pipeline: defaultPipe + }; +} + +function +RpAtomicSetFrame(a, f) +{ + a.frame = f; + f.objects.push(a); +} + +function +RpAtomicRender(atomic) +{ + if(atomic.geometry.instData === undefined) + instanceGeo(atomic.geometry); + atomic.pipeline.renderCB(atomic); +} + +/* RpClump */ + +function +RpClumpCreate() +{ + return { + type: rwID_CLUMP, + frame: null, + atomics: [] + }; +} + +function RpClumpSetFrame(c, f) { c.frame = f; } + +function +RpClumpRender(clump) +{ + for(let i = 0; i < clump.atomics.length; i++) + RpAtomicRender(clump.atomics[i]); +} + + +/* Instancing */ + +function +instanceGeo(geo) +{ + let header = { + prim: gl.TRIANGLES, + totalNumVertices: geo.numVertices, + totalNumIndices: geo.totalMeshIndices, + vbo: gl.createBuffer(), + ibo: gl.createBuffer(), + inst: [], + attribs: [] + }; + if(geo.meshtype == 1) + header.prim = gl.TRIANGLE_STRIP; + + // instance indices + let buffer = new ArrayBuffer(header.totalNumIndices*2); + offset = 0; + for(let i = 0; i < geo.meshes.length; i++){ + m = geo.meshes[i]; + inst = { + material: m.material, + numIndices: m.indices.length, + offset: offset + }; + let indices = new Uint16Array(buffer, inst.offset, inst.numIndices); + indices.set(m.indices); + offset += inst.numIndices*2; + header.inst.push(inst); + } + gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, header.ibo); + gl.bufferData(gl.ELEMENT_ARRAY_BUFFER, buffer, gl.STATIC_DRAW); + geo.instData = header; + + instanceVertices(geo); +} + +function +instanceVertices(geo) +{ + let i; + let stride = 0; + let attribs = []; + + attribs.push({ + index: ATTRIB_POS, + size: 3, + type: gl.FLOAT, + normalized: false, + offset: stride + }); + stride += 12; + + if(geo.morphTargets[0].normals){ + attribs.push({ + index: ATTRIB_NORMAL, + size: 3, + type: gl.FLOAT, + normalized: false, + offset: stride + }); + stride += 12; + } + + if(geo.prelit){ + attribs.push({ + index: ATTRIB_COLOR, + size: 4, + type: gl.UNSIGNED_BYTE, + normalized: true, + offset: stride + }); + stride += 4; + } + + for(i = 0; i < geo.texCoords.length; i++){ + attribs.push({ + index: ATTRIB_TEXCOORDS0 + i, + size: 2, + type: gl.FLOAT, + normalized: false, + offset: stride + }); + stride += 8; + } + + for(i = 0; i < attribs.length; i++) + attribs[i].stride = stride; + + header = geo.instData; + header.attribs = attribs; + let buffer = new ArrayBuffer(header.totalNumVertices*stride); + + // instance verts + for(i = 0; attribs[i].index != ATTRIB_POS; i++); + let a = attribs[i]; + instV3d(buffer, a.offset, a.stride, geo.morphTargets[0].vertices, header.totalNumVertices); + + if(geo.morphTargets[0].normals){ + for(i = 0; attribs[i].index != ATTRIB_NORMAL; i++); + let a = attribs[i]; + instV3d(buffer, a.offset, a.stride, geo.morphTargets[0].normals, header.totalNumVertices); + } + + if(geo.prelit){ + for(i = 0; attribs[i].index != ATTRIB_COLOR; i++); + let a = attribs[i]; + instRGBA(buffer, a.offset, a.stride, geo.prelit, header.totalNumVertices); + } + + for(i = 0; i < geo.texCoords.length; i++){ + for(j = 0; attribs[j].index != ATTRIB_TEXCOORDS0 + i; j++); + let a = attribs[j]; + instV2d(buffer, a.offset, a.stride, geo.texCoords[i], header.totalNumVertices); + } + + gl.bindBuffer(gl.ARRAY_BUFFER, header.vbo); + gl.bufferData(gl.ARRAY_BUFFER, buffer, gl.STATIC_DRAW); +} + +function +instV3d(buffer, offset, stride, src, n) +{ + let view = new DataView(buffer); + let o = offset; + for(let i = 0; i < n; i++){ + view.setFloat32(o+0, src[i][0], true); + view.setFloat32(o+4, src[i][1], true); + view.setFloat32(o+8, src[i][2], true); + o += stride; + } +} + +function +instV2d(buffer, offset, stride, src, n) +{ + let view = new DataView(buffer); + let o = offset; + for(let i = 0; i < n; i++){ + view.setFloat32(o+0, src[i][0], true); + view.setFloat32(o+4, src[i][1], true); + o += stride; + } +} + +function +instRGBA(buffer, offset, stride, src, n) +{ + let view = new DataView(buffer); + let o = offset; + for(let i = 0; i < n; i++){ + view.setUint8(o+0, src[i][0]); + view.setUint8(o+1, src[i][1]); + view.setUint8(o+2, src[i][2]); + view.setUint8(o+3, src[i][3]); + o += stride; + } +} + +/* The Init functions are for when we + * read a json structure produced by convdff */ + +function +rwFrameInit(f) +{ + f.parent = null; + f.root = f; + f.child = null; + f.next = null; + f.objects = []; + m = f.matrix; + f.matrix = mat4.fromValues( + m[0], m[1], m[2], 0, + m[3], m[4], m[5], 0, + m[6], m[7], m[8], 0, + m[9], m[10], m[11], 1); + f.ltm = mat4.create(); + mat4.copy(f.ltm, f.matrix); +} + +function +rpMaterialInit(m) +{ + if(m.texture) + m.texture = RwTextureRead(m.texture.name, m.texture.mask); + if(m.matfx && m.matfx.envTex) + m.matfx.envTex = RwTextureRead(m.matfx.envTex.name, m.matfx.envTex.mask); + if(m.specMat) + m.specMat.texture = RwTextureRead(m.specMat.texture, ""); +} + +function +rpGeometryInit(g) +{ + for(let i = 0; i < g.materials.length; i++){ + m = g.materials[i]; + rpMaterialInit(m); + } + for(let i = 0; i < g.meshes.length; i++){ + g.meshes[i].material = g.materials[g.meshes[i].matId]; + delete g.meshes[i].matId; + } + g.numVertices = g.morphTargets[0].vertices.length; + g.totalMeshIndices = 0; + for(let i = 0; i < g.meshes.length; i++) + g.totalMeshIndices += g.meshes[i].indices.length; +} + +function +rpAtomicInit(atomic) +{ + atomic.type = rwID_ATOMIC; + atomic.frame = null; + atomic.visible = true; + atomic.pipeline = defaultPipe; + atomic.objects = []; + if(atomic.matfx) + atomic.pipeline = matFXPipe; +} + +function +rpClumpInit(clump) +{ + for(let i = 0; i < clump.atomics.length; i++){ + let a = clump.atomics[i]; + let f = clump.frames[a.frame]; + rpAtomicInit(a); + RpAtomicSetFrame(a, f); + + rpGeometryInit(a.geometry); + instanceGeo(a.geometry) + } + clump.frame = null; + for(let i = 0; i < clump.frames.length; i++){ + let f = clump.frames[i]; + p = f.parent; + rwFrameInit(f); + if(p >= 0) + RwFrameAddChild(clump.frames[p], f); + else + RpClumpSetFrame(clump, f); + } + delete clump.frames; + + rwFrameSynchLTM(clump.frame); +} diff --git a/rwrender.js b/rwrender.js new file mode 100644 index 0000000..b4f32f2 --- /dev/null +++ b/rwrender.js @@ -0,0 +1,260 @@ +var RenderPass = -1; + +var defaultPipe = { + renderCB: defaultRenderCB +}; +var matFXPipe = { + renderCB: matfxRenderCB +}; +var carPipe = { + renderCB: carRenderCB +}; + +function +RenderThisPass(mat) +{ + switch(RenderPass){ + case 0: // opaque + return mat.color[3] == 255; + case 1: // transparent + return mat.color[3] != 255; + } + return true; +} + + +function +uploadState(proginfo) +{ + gl.uniformMatrix4fv(proginfo.u.u_proj, false, state.projectionMatrix); + gl.uniformMatrix4fv(proginfo.u.u_view, false, state.viewMatrix); + gl.uniformMatrix4fv(proginfo.u.u_world, false, state.worldMatrix); + if(proginfo.u.u_env) + gl.uniformMatrix4fv(proginfo.u.u_env, false, state.envMatrix); + + gl.uniform3fv(proginfo.u.u_ambLight, state.ambLight); + gl.uniform3fv(proginfo.u.u_lightDir, state.lightDir); + gl.uniform3fv(proginfo.u.u_lightCol, state.lightCol); + + gl.uniform1i(proginfo.u.u_tex0, 0); + gl.uniform1i(proginfo.u.u_tex1, 1); + gl.uniform1i(proginfo.u.u_tex2, 2); + + gl.uniform1f(proginfo.u.u_alphaRef, state.alphaRef); +} + +function +setAttributes(attribs, proginfo) +{ + for(let i = 0; i < attribs.length; i++){ + a = attribs[i]; + if(proginfo.a[a.index] < 0) + continue; + gl.vertexAttribPointer(proginfo.a[a.index], + a.size, a.type, + a.normalized, + a.stride, a.offset); + gl.enableVertexAttribArray(proginfo.a[a.index]); + } +} + +function +resetAttributes(attribs, proginfo) +{ + for(let i = 0; i < attribs.length; i++){ + a = attribs[i]; + if(proginfo.a[a.index] >= 0) + gl.disableVertexAttribArray(proginfo.a[a.index]); + } +} + +function +defaultRenderCB(atomic) +{ + if(!atomic.visible) + return; + + mat4.copy(state.worldMatrix, atomic.frame.ltm); + + let header = atomic.geometry.instData; + gl.bindBuffer(gl.ARRAY_BUFFER, header.vbo); + gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, header.ibo); + + let prg = defaultProgram; + gl.useProgram(prg.program); + + setAttributes(header.attribs, prg); + uploadState(prg); + + for(let i = 0; i < header.inst.length; i++){ + inst = header.inst[i]; + m = inst.material; + + if(!RenderThisPass(m)) + continue; + + gl.activeTexture(gl.TEXTURE0); + if(m.texture) + gl.bindTexture(gl.TEXTURE_2D, m.texture.tex); + else + gl.bindTexture(gl.TEXTURE_2D, whitetex); + + vec4.scale(state.matColor, m.color, 1.0/255.0); + state.surfaceProps[0] = m.surfaceProperties[0]; + state.surfaceProps[1] = m.surfaceProperties[1]; + state.surfaceProps[2] = m.surfaceProperties[2]; + + gl.uniform4fv(prg.u.u_matColor, state.matColor); + gl.uniform4fv(prg.u.u_surfaceProps, state.surfaceProps); + + if(m.color[3] != 255 || m.texture){ + gl.enable(gl.BLEND); + gl.blendFunc(gl.SRC_ALPHA, gl.ONE_MINUS_SRC_ALPHA); + }else + gl.disable(gl.BLEND); + + gl.drawElements(header.prim, inst.numIndices, gl.UNSIGNED_SHORT, inst.offset); + } + + resetAttributes(header.attribs, programInfo); +} + +var envMatScale = mat4.fromValues( + -0.5, 0.0, 0.0, 0.0, + 0.0, -0.5, 0.0, 0.0, + 0.0, 0.0, 1.0, 0.0, + 0.5, 0.5, 0.0, 1.0 +); + +function +matfxRenderCB(atomic) +{ + if(!atomic.visible) + return; + + mat4.copy(state.worldMatrix, atomic.frame.ltm); + + let tmp = mat4.create(); + mat4.invert(tmp, envFrame.ltm); + mat4.multiply(tmp, tmp, state.viewMatrix); + tmp[12] = tmp[13] = tmp[14] = 0.0; + + mat4.multiply(state.envMatrix, envMatScale, tmp); + + let header = atomic.geometry.instData; + gl.bindBuffer(gl.ARRAY_BUFFER, header.vbo); + gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, header.ibo); + + let prg = envMapProgram; + gl.useProgram(prg.program); + + setAttributes(header.attribs, prg); + uploadState(prg); + + for(let i = 0; i < header.inst.length; i++){ + inst = header.inst[i]; + m = inst.material; + + if(!RenderThisPass(m)) + continue; + + gl.activeTexture(gl.TEXTURE0); + if(m.texture) + gl.bindTexture(gl.TEXTURE_2D, m.texture.tex); + else + gl.bindTexture(gl.TEXTURE_2D, whitetex); + + gl.activeTexture(gl.TEXTURE1); + envcoef = 0.0; + if(m.matfx && m.matfx.envTex){ + envcoef = m.matfx.envCoefficient; + gl.bindTexture(gl.TEXTURE_2D, m.matfx.envTex.tex); + }else + gl.bindTexture(gl.TEXTURE_2D, null); + + vec4.scale(state.matColor, m.color, 1.0/255.0); + state.surfaceProps[0] = m.surfaceProperties[0]; + state.surfaceProps[1] = envcoef; + state.surfaceProps[2] = m.surfaceProperties[2]; + + gl.uniform4fv(prg.u.u_matColor, state.matColor); + gl.uniform4fv(prg.u.u_surfaceProps, state.surfaceProps); + + if(m.color[3] != 255 || m.texture){ + gl.enable(gl.BLEND); + gl.blendFunc(gl.SRC_ALPHA, gl.ONE_MINUS_SRC_ALPHA); + }else + gl.disable(gl.BLEND); + + gl.drawElements(header.prim, inst.numIndices, gl.UNSIGNED_SHORT, inst.offset); + } + + resetAttributes(header.attribs, programInfo); +} + +function +carRenderCB(atomic) +{ + if(!atomic.visible) + return; + + mat4.copy(state.worldMatrix, atomic.frame.ltm); + + let header = atomic.geometry.instData; + gl.bindBuffer(gl.ARRAY_BUFFER, header.vbo); + gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, header.ibo); + + let prg = carPS2Program; + gl.useProgram(prg.program); + + setAttributes(header.attribs, prg); + uploadState(prg); + + for(let i = 0; i < header.inst.length; i++){ + inst = header.inst[i]; + m = inst.material; + + if(!RenderThisPass(m)) + continue; + + gl.activeTexture(gl.TEXTURE0); + if(m.texture) + gl.bindTexture(gl.TEXTURE_2D, m.texture.tex); + else + gl.bindTexture(gl.TEXTURE_2D, whitetex); + + let shininess = 0.0; + let specularity = 0.0; + + if(m.fxFlags & 3){ + shininess = m.envMap.shininess; + gl.activeTexture(gl.TEXTURE1); + gl.bindTexture(gl.TEXTURE_2D, m.envMap.texture.tex); + } + + if(m.fxFlags & 4){ + specularity = m.specMap.specularity; + gl.activeTexture(gl.TEXTURE2); + gl.bindTexture(gl.TEXTURE_2D, m.specMap.texture.tex); + } + + vec4.scale(state.matColor, m.color, 1.0/255.0); + state.surfaceProps[0] = m.surfaceProperties[0]; + state.surfaceProps[1] = specularity; + state.surfaceProps[2] = m.surfaceProperties[2]; + state.surfaceProps[3] = shininess; + + gl.uniform4fv(prg.u.u_matColor, state.matColor); + gl.uniform4fv(prg.u.u_surfaceProps, state.surfaceProps); + + if(m.color[3] != 255 || m.texture){ + gl.enable(gl.BLEND); + gl.blendFunc(gl.SRC_ALPHA, gl.ONE_MINUS_SRC_ALPHA); + }else + gl.disable(gl.BLEND); + + gl.drawElements(header.prim, inst.numIndices, gl.UNSIGNED_SHORT, inst.offset); + } + + resetAttributes(header.attribs, programInfo); +} diff --git a/rwstream.js b/rwstream.js new file mode 100644 index 0000000..b251010 --- /dev/null +++ b/rwstream.js @@ -0,0 +1,623 @@ +function +AttachPlugins() +{ + frameTKList[rwID_NODENAME] = { streamRead: NodeNameStreamRead }; + geometryTKList[rwID_BINMESHPLUGIN] = { streamRead: rpMeshRead }; + materialTKList[rwID_MATERIALEFFECTSPLUGIN] = { streamRead: rpMatfxMaterialStreamRead }; + materialTKList[rwID_ENVMAT] = { streamRead: envMatStreamRead }; + materialTKList[rwID_SPECMAT] = { streamRead: specMatStreamRead }; + atomicTKList[rwID_MATERIALEFFECTSPLUGIN] = { streamRead: rpMatfxAtomicStreamRead }; +} + +function +RwStreamCreate(buffer) +{ + return { + buffer: buffer, + view: new DataView(buffer), + offset: 0, + eof: false + }; +} + +function +RwStreamSkip(stream, len) +{ + stream.offset += len; +} + +function +RwStreamReadUInt8(stream) +{ + if(stream.offset >= stream.buffer.byteLength){ + stream.eof = true; + return null; + } + let v = stream.view.getUint8(stream.offset, true); + stream.offset += 1; + return v; +} + +function +RwStreamReadUInt16(stream) +{ + if(stream.offset >= stream.buffer.byteLength){ + stream.eof = true; + return null; + } + let v = stream.view.getUint16(stream.offset, true); + stream.offset += 2; + return v; +} + +function +RwStreamReadUInt32(stream) +{ + if(stream.offset >= stream.buffer.byteLength){ + stream.eof = true; + return null; + } + let v = stream.view.getUint32(stream.offset, true); + stream.offset += 4; + return v; +} + +function +RwStreamReadInt32(stream) +{ + if(stream.offset >= stream.buffer.byteLength){ + stream.eof = true; + return null; + } + let v = stream.view.getInt32(stream.offset, true); + stream.offset += 4; + return v; +} + +function +RwStreamReadReal(stream) +{ + if(stream.offset >= stream.buffer.byteLength){ + stream.eof = true; + return null; + } + let v = stream.view.getFloat32(stream.offset, true); + stream.offset += 4; + return v; +} + +function +RwStreamReadString(stream, length) +{ + if(stream.offset >= stream.buffer.byteLength){ + stream.eof = true; + return null; + } + let a = new Uint8Array(stream.buffer, stream.offset, length); + let s = ""; + for(let i = 0; i < length && a[i] != 0; i++) + s += String.fromCharCode(a[i]); + stream.offset += length; + return s; +} + +function +rwStreamReadChunkHeader(stream) +{ + let t = RwStreamReadUInt32(stream); + let l = RwStreamReadUInt32(stream); + let id = RwStreamReadUInt32(stream); + if(stream.eof) + return null; + let version = 0; + let build = 0; + if((id & 0xFFFF0000) == 0){ + version = id<<8; + build = 0; + }else{ + version = ((id>>14) & 0x3FF00) + 0x30000 | + ((id>>16) & 3); + build = id & 0xFFFF; + } + return { + type: t, + length: l, + version: version, + build: build, + }; +} + +function +RwStreamFindChunk(stream, type) +{ + let header; + while(header = rwStreamReadChunkHeader(stream)){ + if(header.type == type) + return header; + RwStreamSkip(stream, header.length); + } + return null; +} + +function +rwPluginRegistryReadDataChunks(tklist, stream, object) +{ + let header; + if((header = RwStreamFindChunk(stream, rwID_EXTENSION)) == null) + return null; + let end = stream.offset + header.length; + while(stream.offset < end){ + header = rwStreamReadChunkHeader(stream); + if(header.type in tklist && tklist[header.type].streamRead){ + if(!tklist[header.type].streamRead(stream, object, header.length)) + return null; + }else + RwStreamSkip(stream, header.length); + } + return 1; +} + +function +rwStringStreamFindAndRead(stream) +{ + let header; + if((header = RwStreamFindChunk(stream, rwID_STRING)) == null) + return null; + return RwStreamReadString(stream, header.length); +} + +function +rwFrameListStreamRead(stream) +{ + let header; + if((header = RwStreamFindChunk(stream, rwID_STRUCT)) == null) + return null; + let numFrames = RwStreamReadInt32(stream); + let frames = []; + for(let i = 0; i < numFrames; i++){ + let xx = RwStreamReadReal(stream); + let xy = RwStreamReadReal(stream); + let xz = RwStreamReadReal(stream); + let yx = RwStreamReadReal(stream); + let yy = RwStreamReadReal(stream); + let yz = RwStreamReadReal(stream); + let zx = RwStreamReadReal(stream); + let zy = RwStreamReadReal(stream); + let zz = RwStreamReadReal(stream); + let wx = RwStreamReadReal(stream); + let wy = RwStreamReadReal(stream); + let wz = RwStreamReadReal(stream); + + frame = RwFrameCreate(); + mat4.set(frame.matrix, + xx, xy, xz, 0, + yx, yy, yz, 0, + zx, zy, zz, 0, + wx, wy, wz, 1); + frames.push(frame); + let parent = RwStreamReadInt32(stream); + RwStreamReadInt32(stream); // unused + if(parent >= 0) + RwFrameAddChild(frames[parent], frame); + } + for(let i = 0; i < numFrames; i++) + if(!rwPluginRegistryReadDataChunks(frameTKList, stream, frames[i])) + return null; + return frames; +} + +function +RwTextureStreamRead(stream) +{ + let header; + if((header = RwStreamFindChunk(stream, rwID_STRUCT)) == null) + return null; + let flags = RwStreamReadUInt32(stream); // we ignore this + let name = rwStringStreamFindAndRead(stream); + if(name == null) return null; + let mask = rwStringStreamFindAndRead(stream); + if(mask == null) return null; + let tex = RwTextureRead(name, mask); + if(!rwPluginRegistryReadDataChunks(textureTKList, stream, tex)) + return null; + return tex; +} + +function +RpMaterialStreamRead(stream) +{ + let header; + if((header = RwStreamFindChunk(stream, rwID_STRUCT)) == null) + return null; + let mat = RpMaterialCreate(); + RwStreamReadInt32(stream); // flags, unused + mat.color[0] = RwStreamReadUInt8(stream); + mat.color[1] = RwStreamReadUInt8(stream); + mat.color[2] = RwStreamReadUInt8(stream); + mat.color[3] = RwStreamReadUInt8(stream); + RwStreamReadInt32(stream); // unused + let textured = RwStreamReadInt32(stream); + mat.surfaceProperties[0] = RwStreamReadReal(stream); + mat.surfaceProperties[1] = RwStreamReadReal(stream); + mat.surfaceProperties[2] = RwStreamReadReal(stream); + if(textured){ + if((header = RwStreamFindChunk(stream, rwID_TEXTURE)) == null) + return null; + mat.texture = RwTextureStreamRead(stream); + if(mat.texture == null) + return null; + } + if(!rwPluginRegistryReadDataChunks(materialTKList, stream, mat)) + return null; + return mat; +} + +function +rpMaterialListStreamRead(stream) +{ + let header; + if((header = RwStreamFindChunk(stream, rwID_STRUCT)) == null) + return null; + let numMaterials = RwStreamReadInt32(stream); + let indices = []; + while(numMaterials--) + indices.push(RwStreamReadInt32(stream)); + let materials = [] + for(let i = 0; i < indices.length; i++){ + if(indices[i] >= 0) + materials.push(materials[indices[i]]); + else{ + if((header = RwStreamFindChunk(stream, rwID_MATERIAL)) == null) + return null; + let m = RpMaterialStreamRead(stream); + if(m == null) + return null; + materials.push(m); + } + } + return materials; +} + +function +RpGeometryStreamRead(stream) +{ + let header; + if((header = RwStreamFindChunk(stream, rwID_STRUCT)) == null) + return null; + let flags = RwStreamReadUInt32(stream); + let numTriangles = RwStreamReadInt32(stream); + let numVertices = RwStreamReadInt32(stream); + let numMorphTargets = RwStreamReadInt32(stream); + if(header.version < 0x34000) + RwStreamSkip(stream, 12); + if(flags & 0x01000000) return null; // native geometry not supported + + let geo = RpGeometryCreate(flags, numMorphTargets); + geo.numVertices = numVertices; + + if(geo.prelit) + for(let i = 0; i < numVertices; i++){ + let r = RwStreamReadUInt8(stream); + let g = RwStreamReadUInt8(stream); + let b = RwStreamReadUInt8(stream); + let a = RwStreamReadUInt8(stream); + geo.prelit.push([r, g, b, a]); + } + + for(let i = 0; i < geo.texCoords.length; i++){ + let texCoords = geo.texCoords[i]; + for(let j = 0; j < numVertices; j++){ + let u = RwStreamReadReal(stream); + let v = RwStreamReadReal(stream); + texCoords.push([u, v]); + } + } + + for(let i = 0; i < numTriangles; i++){ + let w1 = RwStreamReadUInt32(stream); + let w2 = RwStreamReadUInt32(stream); + let v1 = w1>>16 & 0xFFFF; + let v2 = w1 & 0xFFFF; + let v3 = w2>>16 & 0xFFFF; + let matid = w2 & 0xFFFF; + geo.triangles.push([v1, v2, v3, matid]); + } + + for(let i = 0; i < numMorphTargets; i++){ + let mt = geo.morphTargets[i]; + + RwStreamSkip(stream, 4*4 + 4 + 4); // ignore bounding sphere and flags + for(let j = 0; j < numVertices; j++){ + let x = RwStreamReadReal(stream); + let y = RwStreamReadReal(stream); + let z = RwStreamReadReal(stream); + mt.vertices.push([x, y, z]); + } + if(mt.normals) + for(let j = 0; j < numVertices; j++){ + let x = RwStreamReadReal(stream); + let y = RwStreamReadReal(stream); + let z = RwStreamReadReal(stream); + mt.normals.push([x, y, z]); + } + } + + if((header = RwStreamFindChunk(stream, rwID_MATLIST)) == null) + return null; + geo.materials = rpMaterialListStreamRead(stream); + if(geo.materials == null) + return null; + + if(!rwPluginRegistryReadDataChunks(geometryTKList, stream, geo)) + return null; + + return geo; +} + +function +rpMeshRead(stream, geo, length) +{ + geo.meshtype = RwStreamReadInt32(stream); + let numMeshes = RwStreamReadInt32(stream); + geo.totalMeshIndices = RwStreamReadInt32(stream); + while(numMeshes--){ + let numIndices = RwStreamReadInt32(stream); + let matid = RwStreamReadInt32(stream); + let m = { + indices: [], + material: geo.materials[matid] + }; + while(numIndices--) + m.indices.push(RwStreamReadInt32(stream)); + geo.meshes.push(m); + } + return geo; +} + +function +rpGeometryListStreamRead(stream) +{ + let header; + if((header = RwStreamFindChunk(stream, rwID_STRUCT)) == null) + return null; + let numGeoms = RwStreamReadInt32(stream); + let geoms = [] + while(numGeoms--){ + if((header = RwStreamFindChunk(stream, rwID_GEOMETRY)) == null) + return null; + let g = RpGeometryStreamRead(stream); + if(g == null) + return null; + geoms.push(g); + } + return geoms; +} + +function +rpClumpAtomicStreamRead(stream, frames, geos) +{ + let header; + if((header = RwStreamFindChunk(stream, rwID_STRUCT)) == null) + return null; + let atomic = RpAtomicCreate(); + let frame = RwStreamReadInt32(stream); + let geometry = RwStreamReadInt32(stream); + let flags = RwStreamReadInt32(stream); // ignored + RwStreamReadInt32(stream); // unused + RpAtomicSetFrame(atomic, frames[frame]); + atomic.geometry = geos[geometry]; + + if(!rwPluginRegistryReadDataChunks(atomicTKList, stream, atomic)) + return null; + + return atomic; +} + +function +RpClumpStreamRead(stream) +{ + let header; + if((header = RwStreamFindChunk(stream, rwID_STRUCT)) == null) + return null; + + let numAtomics = RwStreamReadInt32(stream); + let numLights = 0; + let numCameras = 0; + if(header.version > 0x33000){ + numLights = RwStreamReadInt32(stream); + numCameras = RwStreamReadInt32(stream); + } + + if((header = RwStreamFindChunk(stream, rwID_FRAMELIST)) == null) + return null; + frames = rwFrameListStreamRead(stream); + if(frames == null) + return null; + + clump = RpClumpCreate(); + RpClumpSetFrame(clump, frames[0]); + + if((header = RwStreamFindChunk(stream, rwID_GEOMETRYLIST)) == null) + return null; + geos = rpGeometryListStreamRead(stream); + if(geos == null) + return null; + + while(numAtomics--){ + if((header = RwStreamFindChunk(stream, rwID_ATOMIC)) == null) + return null; + let a = rpClumpAtomicStreamRead(stream, frames, geos); + if(a == null) + return null; + clump.atomics.push(a); + } + + if(!rwPluginRegistryReadDataChunks(clumpTKList, stream, clump)) + return null; + + rwFrameSynchLTM(clump.frame); + + // TODO? lights, cameras + + return clump; +} + + +/* + * Plugins + */ + +/* MatFX */ + +var rpMATFXEFFECTBUMPMAP = 1; +var rpMATFXEFFECTENVMAP = 2; +var rpMATFXEFFECTBUMPENVMAP = 3; +var rpMATFXEFFECTDUAL = 4; +var rpMATFXEFFECTUVTRANSFORM = 5; +var rpMATFXEFFECTDUALUVTRANSFORM = 6; + +function +RpMatFXMaterialSetEffects(mat, effects) +{ + mat.matfx = { + type: effects, + bump: false, + env: false, + dual: false, + uvxform: false + }; + // TODO: init the relevant fields here + switch(effects){ + case rpMATFXEFFECTBUMPMAP: + mat.matfx.bump = true; + break; + case rpMATFXEFFECTENVMAP: + mat.matfx.env = true; + break; + case rpMATFXEFFECTBUMPENVMAP: + mat.matfx.bump = true; + mat.matfx.env = true; + break; + case rpMATFXEFFECTDUAL: + mat.matfx.dual = true; + break; + case rpMATFXEFFECTUVTRANSFORM: + mat.matfx.uvxform = true; + break; + case rpMATFXEFFECTDUALUVTRANSFORM: + mat.matfx.dual = true; + mat.matfx.uvxform = true; + break; + } +} + +function +rpMatfxMaterialStreamRead(stream, mat, length) +{ + let header; + + let effects = RwStreamReadInt32(stream); + RpMatFXMaterialSetEffects(mat, effects); + let mfx = mat.matfx; + + for(let i = 0; i < 2; i++){ + let type = RwStreamReadInt32(stream); + switch(type){ + case rpMATFXEFFECTBUMPMAP: + mfx.bumpCoefficient = RwStreamReadReal(stream); + if(RwStreamReadInt32(stream)){ + if((header = RwStreamFindChunk(stream, rwID_TEXTURE)) == null) + return null; + mfx.bumpedTex = RwTextureStreamRead(stream); + if(mfx.bumpedTex == null) + return null; + } + if(RwStreamReadInt32(stream)){ + if((header = RwStreamFindChunk(stream, rwID_TEXTURE)) == null) + return null; + mfx.bumpTex = RwTextureStreamRead(stream); + if(mfx.bumpTex == null) + return null; + } + break; + case rpMATFXEFFECTENVMAP: + mfx.envCoefficient = RwStreamReadReal(stream); + mfx.envFBalpha = RwStreamReadInt32(stream); + if(RwStreamReadInt32(stream)){ + if((header = RwStreamFindChunk(stream, rwID_TEXTURE)) == null) + return null; + mfx.envTex = RwTextureStreamRead(stream); + if(mfx.envTex == null) + return null; + } + break; + case rpMATFXEFFECTDUAL: + mfs.srcBlend = RwStreamReadInt32(stream); + mfs.dstBlend = RwStreamReadInt32(stream); + if(RwStreamReadInt32(stream)){ + if((header = RwStreamFindChunk(stream, rwID_TEXTURE)) == null) + return null; + mfx.dualTex = RwTextureStreamRead(stream); + if(mfx.dualTex == null) + return null; + } + break; + } + } + + return mat; +} + +function +rpMatfxAtomicStreamRead(stream, atomic, length) +{ + atomic.matfx = RwStreamReadInt32(stream); + if(atomic.matfx) + atomic.pipeline = matFXPipe; + return atomic; +} + + +/* GTA Node Name */ + +function +NodeNameStreamRead(stream, frame, length) +{ + frame.name = RwStreamReadString(stream, length); + return frame; +} + +/* GTA Env Map */ + +function +envMatStreamRead(stream, mat, length) +{ + let sclX = RwStreamReadReal(stream); + let sclY = RwStreamReadReal(stream); + let transSclX = RwStreamReadReal(stream); + let transSclY = RwStreamReadReal(stream); + let shininess = RwStreamReadReal(stream); + RwStreamReadInt32(stream); // ignore + + mat.envMap = { + scale: [ sclX, sclY ], + transScale: [ transSclX, transSclY ], + shininess: shininess + }; + return mat; +} + +/* GTA Spec Map */ + +function +specMatStreamRead(stream, mat, length) +{ + let specularity = RwStreamReadReal(stream); + let texname = RwStreamReadString(stream, 24); + + mat.specMap = { + specularity: specularity, + texture: RwTextureRead(texname, "") + }; + return mat; +} diff --git a/shaders.js b/shaders.js new file mode 100644 index 0000000..e79f053 --- /dev/null +++ b/shaders.js @@ -0,0 +1,189 @@ +const defaultVS = ` +attribute vec3 in_pos; +attribute vec3 in_normal; +attribute vec4 in_color; +attribute vec2 in_tex0; + +uniform mat4 u_world; +uniform mat4 u_view; +uniform mat4 u_proj; + +uniform vec4 u_matColor; +uniform vec4 u_surfaceProps; + +uniform vec3 u_ambLight; +uniform vec3 u_lightDir; +uniform vec3 u_lightCol; + +varying highp vec4 v_color; +varying highp vec2 v_tex0; + +void main() { + gl_Position = u_proj * u_view * u_world * vec4(in_pos, 1.0); + v_tex0 = in_tex0; + + v_color = in_color; + + v_color.rgb += u_ambLight*u_surfaceProps.x; + vec3 N = mat3(u_world) * in_normal; + float L = max(0.0, dot(N, -normalize(u_lightDir))); + v_color.rgb += L*u_lightCol*u_surfaceProps.z; + v_color = clamp(v_color, 0.0, 1.0); + v_color *= u_matColor; +} +`; + +const defaultFS = ` +uniform sampler2D tex; + +uniform highp float u_alphaRef; + +varying highp vec4 v_color; +varying highp vec2 v_tex0; + +void main() { + gl_FragColor = v_color*texture2D(tex, v_tex0); + if(gl_FragColor.a < u_alphaRef) + discard; +} +`; + + +const envVS = ` +attribute vec3 in_pos; +attribute vec3 in_normal; +attribute vec4 in_color; +attribute vec2 in_tex0; + +uniform mat4 u_world; +uniform mat4 u_view; +uniform mat4 u_proj; +uniform mat4 u_env; + +uniform vec4 u_matColor; +uniform vec4 u_surfaceProps; + +uniform vec3 u_ambLight; +uniform vec3 u_lightDir; +uniform vec3 u_lightCol; + +varying highp vec4 v_color0; +varying highp vec4 v_color1; +varying highp vec2 v_tex0; +varying highp vec2 v_tex1; + +void main() { + gl_Position = u_proj * u_view * u_world * vec4(in_pos, 1.0); + v_tex0 = in_tex0; + + v_color0 = in_color; + + v_color0.rgb += u_ambLight*u_surfaceProps.x; + vec3 N = mat3(u_world) * in_normal; + float L = max(0.0, dot(N, -normalize(u_lightDir))); + v_color0.rgb += L*u_lightCol*u_surfaceProps.z; + v_color0 = clamp(v_color0, 0.0, 1.0); + v_color0 *= u_matColor; + + v_color1 = v_color0*u_surfaceProps.y; + + v_tex1 = (u_env*vec4(N, 1.0)).xy; +} +`; + +const envFS = ` +uniform sampler2D tex0; +uniform sampler2D tex1; + +uniform highp float u_alphaRef; + +varying highp vec4 v_color0; +varying highp vec4 v_color1; +varying highp vec2 v_tex0; +varying highp vec2 v_tex1; + +void main() { + gl_FragColor = v_color0*texture2D(tex0, v_tex0); + if(gl_FragColor.a < u_alphaRef) + discard; + gl_FragColor.rgb += (v_color1*texture2D(tex1, v_tex1)).rgb; +} +`; + + +const carPS2VS = ` +attribute vec3 in_pos; +attribute vec3 in_normal; +attribute vec4 in_color; +attribute vec2 in_tex0; +attribute vec2 in_tex1; + +uniform mat4 u_world; +uniform mat4 u_view; +uniform mat4 u_proj; +uniform mat4 u_env; + +uniform vec4 u_matColor; +uniform vec4 u_surfaceProps; + +uniform vec3 u_ambLight; +uniform vec3 u_lightDir; +uniform vec3 u_lightCol; + +varying highp vec4 v_color0; +varying highp vec4 v_color1; +varying highp vec4 v_color2; +varying highp vec2 v_tex0; +varying highp vec2 v_tex1; +varying highp vec2 v_tex2; + +void main() { + gl_Position = u_proj * u_view * u_world * vec4(in_pos, 1.0); + v_tex0 = in_tex0; + + v_color0 = in_color; + + v_color0.rgb += u_ambLight*u_surfaceProps.x; + vec3 N = mat3(u_world) * in_normal; + float L = max(0.0, dot(N, -normalize(u_lightDir))); + v_color0.rgb += L*u_lightCol*u_surfaceProps.z; + v_color0 = clamp(v_color0, 0.0, 1.0); + v_color0 *= u_matColor; + + v_tex1 = in_tex1; + v_color1 = vec4(1.5*u_surfaceProps.w); + + + N = mat3(u_view) * N; + vec3 D = mat3(u_view) * u_lightDir; + N = D - 2.0*N*dot(N, D); + v_tex2 = (N.xy + vec2(1.0, 1.0))/2.0; + if(N.z < 0.0) + v_color2 = vec4(0.75*u_surfaceProps.y); + else + v_color2 = vec4(0.0); +} +`; + +const carPS2FS = ` +uniform sampler2D tex0; +uniform sampler2D tex1; +uniform sampler2D tex2; + +uniform highp float u_alphaRef; + +varying highp vec4 v_color0; +varying highp vec4 v_color1; +varying highp vec4 v_color2; +varying highp vec2 v_tex0; +varying highp vec2 v_tex1; +varying highp vec2 v_tex2; + +void main() { + gl_FragColor = v_color0*texture2D(tex0, v_tex0); + if(gl_FragColor.a < u_alphaRef) + discard; + gl_FragColor.rgb += (v_color1*texture2D(tex1, v_tex1)).rgb; + gl_FragColor.rgb += (v_color2*texture2D(tex2, v_tex2)).rgb; +} +`; diff --git a/webgl.css b/webgl.css new file mode 100644 index 0000000..2d293b7 --- /dev/null +++ b/webgl.css @@ -0,0 +1,7 @@ +canvas { + border: 2px solid black; + background-color: black; +} +video { + display: none; +} \ No newline at end of file