UNPKG

phaser-ce

Version:

Phaser CE (Community Edition) is a fast, free and fun HTML5 Game Framework for Desktop and Mobile web browsers.

10,880 lines 304 kB
/**
 * @fileoverview gl-matrix - High performance matrix and vector operations
 * @author Brandon Jones
 * @author Colin MacKenzie IV
 * @version 2.3.2
 */

/* 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] = {
/******/ 			exports: {},
/******/ 			id: moduleId,
/******/ 			loaded: false
/******/ 		};

/******/ 		// Execute the module function
/******/ 		modules[moduleId].call(module.exports, module, module.exports, __webpack_require__);

/******/ 		// Flag the module as loaded
/******/ 		module.loaded = 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;

/******/ 	// __webpack_public_path__
/******/ 	__webpack_require__.p = "";

/******/ 	// Load entry module and return exports
/******/ 	return __webpack_require__(0);
/******/ })
/************************************************************************/
/******/ ([
/* 0 */
/***/ function(module, exports, __webpack_require__) {

	/**
	 * @fileoverview gl-matrix - High performance matrix and vector operations
	 * @author Brandon Jones
	 * @author Colin MacKenzie IV
	 * @version 2.3.2
	 */

	/* 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

	exports.glMatrix = __webpack_require__(1);
	exports.mat2 = __webpack_require__(2);
	exports.mat2d = __webpack_require__(3);
	exports.mat3 = __webpack_require__(4);
	exports.mat4 = __webpack_require__(5);
	exports.quat = __webpack_require__(6);
	exports.vec2 = __webpack_require__(9);
	exports.vec3 = __webpack_require__(7);
	exports.vec4 = __webpack_require__(8);

/***/ },
/* 1 */
/***/ function(module, exports) {

	/* 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. */

	/**
	 * @class Common utilities
	 * @name glMatrix
	 */
	var glMatrix = {};

	// Configuration Constants
	glMatrix.EPSILON = 0.000001;
	glMatrix.ARRAY_TYPE = (typeof Float32Array !== 'undefined') ? Float32Array : Array;
	glMatrix.RANDOM = Math.random;
	glMatrix.ENABLE_SIMD = false;

	// Capability detection
	glMatrix.SIMD_AVAILABLE = (glMatrix.ARRAY_TYPE === this.Float32Array) && ('SIMD' in this);
	glMatrix.USE_SIMD = glMatrix.ENABLE_SIMD && glMatrix.SIMD_AVAILABLE;

	/**
	 * Sets the type of array used when creating new vectors and matrices
	 *
	 * @param {Type} type Array type, such as Float32Array or Array
	 */
	glMatrix.setMatrixArrayType = function(type) {
	    glMatrix.ARRAY_TYPE = type;
	}

	var degree = Math.PI / 180;

	/**
	* Convert Degree To Radian
	*
	* @param {Number} a Angle in Degrees
	*/
	glMatrix.toRadian = function(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.
	 */
	glMatrix.equals = function(a, b) {
		return Math.abs(a - b) <= glMatrix.EPSILON*Math.max(1.0, Math.abs(a), Math.abs(b));
	}

	module.exports = glMatrix;


/***/ },
/* 2 */
/***/ function(module, exports, __webpack_require__) {

	/* 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. */

	var glMatrix = __webpack_require__(1);

	/**
	 * @class 2x2 Matrix
	 * @name mat2
	 */
	var mat2 = {};

	/**
	 * Creates a new identity mat2
	 *
	 * @returns {mat2} a new 2x2 matrix
	 */
	mat2.create = function() {
	    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
	 */
	mat2.clone = function(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
	 */
	mat2.copy = function(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
	 */
	mat2.identity = function(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
	 */
	mat2.fromValues = function(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
	 */
	mat2.set = function(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
	 */
	mat2.transpose = function(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
	 */
	mat2.invert = function(out, a) {
	    var a0 = a[0], a1 = a[1], a2 = a[2], a3 = a[3],

	        // Calculate the determinant
	        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
	 */
	mat2.adjoint = function(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
	 */
	mat2.determinant = function (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
	 */
	mat2.multiply = function (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;
	};

	/**
	 * Alias for {@link mat2.multiply}
	 * @function
	 */
	mat2.mul = mat2.multiply;

	/**
	 * 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
	 */
	mat2.rotate = function (out, a, rad) {
	    var a0 = a[0], a1 = a[1], a2 = a[2], a3 = a[3],
	        s = Math.sin(rad),
	        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
	 **/
	mat2.scale = function(out, a, v) {
	    var a0 = a[0], a1 = a[1], a2 = a[2], a3 = a[3],
	        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
	 */
	mat2.fromRotation = function(out, rad) {
	    var s = Math.sin(rad),
	        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
	 */
	mat2.fromScaling = function(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
	 */
	mat2.str = function (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
	 */
	mat2.frob = function (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
	 */

	mat2.LDU = function (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
	 */
	mat2.add = function(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
	 */
	mat2.subtract = function(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;
	};

	/**
	 * Alias for {@link mat2.subtract}
	 * @function
	 */
	mat2.sub = mat2.subtract;

	/**
	 * 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.
	 */
	mat2.exactEquals = function (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.
	 */
	mat2.equals = function (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
	 */
	mat2.multiplyScalar = function(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
	 */
	mat2.multiplyScalarAndAdd = function(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;
	};

	module.exports = mat2;


/***/ },
/* 3 */
/***/ function(module, exports, __webpack_require__) {

	/* 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. */

	var glMatrix = __webpack_require__(1);

	/**
	 * @class 2x3 Matrix
	 * @name mat2d
	 * 
	 * @description 
	 * A mat2d contains six elements defined as:
	 * <pre>
	 * [a, c, tx,
	 *  b, d, ty]
	 * </pre>
	 * This is a short form for the 3x3 matrix:
	 * <pre>
	 * [a, c, tx,
	 *  b, d, ty,
	 *  0, 0, 1]
	 * </pre>
	 * The last row is ignored so the array is shorter and operations are faster.
	 */
	var mat2d = {};

	/**
	 * Creates a new identity mat2d
	 *
	 * @returns {mat2d} a new 2x3 matrix
	 */
	mat2d.create = function() {
	    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
	 */
	mat2d.clone = function(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
	 */
	mat2d.copy = function(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
	 */
	mat2d.identity = function(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
	 */
	mat2d.fromValues = function(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
	 */
	mat2d.set = function(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
	 */
	mat2d.invert = function(out, a) {
	    var aa = a[0], ab = a[1], ac = a[2], ad = a[3],
	        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
	 */
	mat2d.determinant = function (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
	 */
	mat2d.multiply = function (out, a, b) {
	    var a0 = a[0], a1 = a[1], a2 = a[2], a3 = a[3], a4 = a[4], a5 = a[5],
	        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;
	};

	/**
	 * Alias for {@link mat2d.multiply}
	 * @function
	 */
	mat2d.mul = mat2d.multiply;

	/**
	 * 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
	 */
	mat2d.rotate = function (out, a, rad) {
	    var a0 = a[0], a1 = a[1], a2 = a[2], a3 = a[3], a4 = a[4], a5 = a[5],
	        s = Math.sin(rad),
	        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
	 **/
	mat2d.scale = function(out, a, v) {
	    var a0 = a[0], a1 = a[1], a2 = a[2], a3 = a[3], a4 = a[4], a5 = a[5],
	        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
	 **/
	mat2d.translate = function(out, a, v) {
	    var a0 = a[0], a1 = a[1], a2 = a[2], a3 = a[3], a4 = a[4], a5 = a[5],
	        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
	 */
	mat2d.fromRotation = function(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
	 */
	mat2d.fromScaling = function(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
	 */
	mat2d.fromTranslation = function(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
	 */
	mat2d.str = function (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
	 */
	mat2d.frob = function (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
	 */
	mat2d.add = function(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
	 */
	mat2d.subtract = function(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;
	};

	/**
	 * Alias for {@link mat2d.subtract}
	 * @function
	 */
	mat2d.sub = mat2d.subtract;

	/**
	 * 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
	 */
	mat2d.multiplyScalar = function(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
	 */
	mat2d.multiplyScalarAndAdd = function(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.
	 */
	mat2d.exactEquals = function (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.
	 */
	mat2d.equals = function (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)));
	};

	module.exports = mat2d;


/***/ },
/* 4 */
/***/ function(module, exports, __webpack_require__) {

	/* 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. */

	var glMatrix = __webpack_require__(1);

	/**
	 * @class 3x3 Matrix
	 * @name mat3
	 */
	var mat3 = {};

	/**
	 * Creates a new identity mat3
	 *
	 * @returns {mat3} a new 3x3 matrix
	 */
	mat3.create = function() {
	    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
	 */
	mat3.fromMat4 = function(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
	 */
	mat3.clone = function(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
	 */
	mat3.copy = function(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
	 */
	mat3.fromValues = function(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
	 */
	mat3.set = function(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
	 */
	mat3.identity = function(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
	 */
	mat3.transpose = function(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
	 */
	mat3.invert = function(out, a) {
	    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],

	        b01 = a22 * a11 - a12 * a21,
	        b11 = -a22 * a10 + a12 * a20,
	        b21 = a21 * a10 - a11 * a20,

	        // Calculate the determinant
	        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
	 */
	mat3.adjoint = function(out, a) {
	    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];

	    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
	 */
	mat3.determinant = function (a) {
	    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];

	    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
	 */
	mat3.multiply = function (out, a, b) {
	    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],

	        b00 = b[0], b01 = b[1], b02 = b[2],
	        b10 = b[3], b11 = b[4], b12 = b[5],
	        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;
	};

	/**
	 * Alias for {@link mat3.multiply}
	 * @function
	 */
	mat3.mul = mat3.multiply;

	/**
	 * 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
	 */
	mat3.translate = function(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
	 */
	mat3.rotate = function (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
	 **/
	mat3.scale = function(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
	 */
	mat3.fromTranslation = function(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
	 */
	mat3.fromRotation = function(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
	 */
	mat3.fromScaling = function(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
	 **/
	mat3.fromMat2d = function(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
	*/
	mat3.fromQuat = function (out, q) {
	    var x = q[0], y = q[1], z = q[2], w = q[3],
	        x2 = x + x,
	        y2 = y + y,
	        z2 = z + z,

	        xx = x * x2,
	        yx = y * x2,
	        yy = y * y2,
	        zx = z * x2,
	        zy = z * y2,
	        zz = z * z2,
	        wx = w * x2,
	        wy = w * y2,
	        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
	*/
	mat3.normalFromMat4 = function (out, a) {
	    var 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],
	        a30 = a[12], a31 = a[13], a32 = a[14], a33 = a[15],

	        b00 = a00 * a11 - a01 * a10,
	        b01 = a00 * a12 - a02 * a10,
	        b02 = a00 * a13 - a03 * a10,
	        b03 = a01 * a12 - a02 * a11,
	        b04 = a01 * a13 - a03 * a11,
	        b05 = a02 * a13 - a03 * a12,
	        b06 = a20 * a31 - a21 * a30,
	        b07 = a20 * a32 - a22 * a30,
	        b08 = a20 * a33 - a23 * a30,
	        b09 = a21 * a32 - a22 * a31,
	        b10 = a21 * a33 - a23 * a31,
	        b11 = a22 * a33 - a23 * a32,

	        // Calculate the determinant
	        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;
	};

	/**
	 * Returns a string representation of a mat3
	 *
	 * @param {mat3} a matrix to represent as a string
	 * @returns {String} string representation of the matrix
	 */
	mat3.str = function (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
	 */
	mat3.frob = function (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
	 */
	mat3.add = function(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
	 */
	mat3.subtract = function(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;
	};

	/**
	 * Alias for {@link mat3.subtract}
	 * @function
	 */
	mat3.sub = mat3.subtract;

	/**
	 * 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
	 */
	mat3.multiplyScalar = function(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
	 */
	mat3.multiplyScalarAndAdd = function(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.
	 */
	mat3.exactEquals = function (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.
	 */
	mat3.equals = function (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 = a[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)));
	};


	module.exports = mat3;


/***/ },
/* 5 */
/***/ function(module, exports, __webpack_require__) {

	/* 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. */

	var glMatrix = __webpack_require__(1);

	/**
	 * @class 4x4 Matrix
	 * @name mat4
	 */
	var mat4 = {
	  scalar: {},
	  SIMD: {}
	};

	/**
	 * Creates a new identity mat4
	 *
	 * @returns {mat4} a new 4x4 matrix
	 */
	mat4.create = function() {
	    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
	 */
	mat4.clone = function(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
	 */
	mat4.copy = function(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
	 */
	mat4.fromValues = function(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
	 */
	mat4.set = function(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
	 */
	mat4.identity = function(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 not using SIMD
	 *
	 * @param {mat4} out the receiving matrix
	 * @param {mat4} a the source matrix
	 * @returns {mat4} out
	 */
	mat4.scalar.transpose = function(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],
	            a12 = a[6], a13 = a[7],
	            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;
	};

	/**
	 * Transpose the values of a mat4 using SIMD
	 *
	 * @param {mat4} out the receiving matrix
	 * @param {mat4} a the source matrix
	 * @returns {mat4} out
	 */
	mat4.SIMD.transpose = function(out, a) {
	    var a0, a1, a2, a3,
	        tmp01, tmp23,
	        out0, out1, out2, out3;

	    a0 = SIMD.Float32x4.load(a, 0);
	    a1 = SIMD.Float32x4.load(a, 4);
	    a2 = SIMD.Float32x4.load(a, 8);
	    a3 = SIMD.Float32x4.load(a, 12);

	    tmp01 = SIMD.Float32x4.shuffle(a0, a1, 0, 1, 4, 5);
	    tmp23 = SIMD.Float32x4.shuffle(a2, a3, 0, 1, 4, 5);
	    out0  = SIMD.Float32x4.shuffle(tmp01, tmp23, 0, 2, 4, 6);
	    out1  = SIMD.Float32x4.shuffle(tmp01, tmp23, 1, 3, 5, 7);
	    SIMD.Float32x4.store(out, 0,  out0);
	    SIMD.Float32x4.store(out, 4,  out1);

	    tmp01 = SIMD.Float32x4.shuffle(a0, a1, 2, 3, 6, 7);
	    tmp23 = SIMD.Float32x4.shuffle(a2, a3, 2, 3, 6, 7);
	    out2  = SIMD.Float32x4.shuffle(tmp01, tmp23, 0, 2, 4, 6);
	    out3  = SIMD.Float32x4.shuffle(tmp01, tmp23, 1, 3, 5, 7);
	    SIMD.Float32x4.store(out, 8,  out2);
	    SIMD.Float32x4.store(out, 12, out3);

	    return out;
	};

	/**
	 * Transpse a mat4 using SIMD if available and enabled
	 *
	 * @param {mat4} out the receiving matrix
	 * @param {mat4} a the source matrix
	 * @returns {mat4} out
	 */
	mat4.transpose = glMatrix.USE_SIMD ? mat4.SIMD.transpose : mat4.scalar.transpose;

	/**
	 * Inverts a mat4 not using SIMD
	 *
	 * @param {mat4} out the receiving matrix
	 * @param {mat4} a the source matrix
	 * @returns {mat4} out
	 */
	mat4.scalar.invert = function(out, a) {
	    var 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],
	        a30 = a[12], a31 = a[13], a32 = a[14], a33 = a[15],

	        b00 = a00 * a11 - a01 * a10,
	        b01 = a00 * a12 - a02 * a10,
	        b02 = a00 * a13 - a03 * a10,
	        b03 = a01 * a12 - a02 * a11,
	        b04 = a01 * a13 - a03 * a11,
	        b05 = a02 * a13 - a03 * a12,
	        b06 = a20 * a31 - a21 * a30,
	        b07 = a20 * a32 - a22 * a30,
	        b08 = a20 * a33 - a23 * a30,
	        b09 = a21 * a32 - a22 * a31,
	        b10 = a21 * a33 - a23 * a31,
	        b11 = a22 * a33 - a23 * a32,

	        // Calculate the determinant
	        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;
	};

	/**
	 * Inverts a mat4 using SIMD
	 *
	 * @param {mat4} out the receiving matrix
	 * @param {mat4} a the source matrix
	 * @returns {mat4} out
	 */
	mat4.SIMD.invert = function(out, a) {
	  var row0, row1, row2, row3,
	      tmp1,
	      minor0, minor1, minor2, minor3,
	      det,
	      a0 = SIMD.Float32x4.load(a, 0),
	      a1 = SIMD.Float32x4.load(a, 4),
	      a2 = SIMD.Float32x4.load(a, 8),
	      a3 = SIMD.Float32x4.load(a, 12);

	  // Compute matrix adjugate
	  tmp1 = SIMD.Float32x4.shuffle(a0, a1, 0, 1, 4, 5);
	  row1 = SIMD.Float32x4.shuffle(a2, a3, 0, 1, 4, 5);
	  row0 = SIMD.Float32x4.shuffle(tmp1, row1, 0, 2, 4, 6);
	  row1 = SIMD.Float32x4.shuffle(row1, tmp1, 1, 3, 5, 7);
	  tmp1 = SIMD.Float32x4.shuffle(a0, a1, 2, 3, 6, 7);
	  row3 = SIMD.Float32x4.shuffle(a2, a3, 2, 3, 6, 7);
	  row2 = SIMD.Float32x4.shuffle(tmp1, row3, 0, 2, 4, 6);
	  row3 = SIMD.Float32x4.shuffle(row3, tmp1, 1, 3, 5, 7);

	  tmp1   = SIMD.Float32x4.mul(row2, row3);
	  tmp1   = SIMD.Float32x4.swizzle(tmp1, 1, 0, 3, 2);
	  minor0 = SIMD.Float32x4.mul(row1, tmp1);
	  minor1 = SIMD.Float32x4.mul(row0, tmp1);
	  tmp1   = SIMD.Float32x4.swizzle(tmp1, 2, 3, 0, 1);
	  minor0 = SIMD.Float32x4.sub(SIMD.Float32x4.mul(row1, tmp1), minor0);
	  minor1 = SIMD.Float32x4.sub(SIMD.Float32x4.mul(row0, tmp1), minor1);
	  minor1 = SIMD.Float32x4.swizzle(minor1, 2, 3, 0, 1);

	  tmp1   = SIMD.Float32x4.mul(row1, row2);
	  tmp1   = SIMD.Float32x4.swizzle(tmp1, 1, 0, 3, 2);
	  minor0 = SIMD.Float32x4.add(SIMD.Float32x4.mul(row3, tmp1), minor0);
	  minor3 = SIMD.Float32x4.mul(row0, tmp1);
	  tmp1   = SIMD.Float32x4.swizzle(tmp1, 2, 3, 0, 1);
	  minor0 = SIMD.Float32x4.sub(minor0, SIMD.Float32x4.mul(row3, tmp1));
	  minor3 = SIMD.Float32x4.sub(SIMD.Float32x4.mul(row0, tmp1), minor3);
	  minor3 = SIMD.Float32x4.swizzle(minor3, 2, 3, 0, 1);

	  tmp1   = SIMD.Float32x4.mul(SIMD.Float32x4.swizzle(row1, 2, 3, 0, 1), row3);
	  tmp1   = SIMD.Float32x4.swizzle(tmp1, 1, 0, 3, 2);
	  row2   = SIMD.Float32x4.swizzle(row2, 2, 3, 0, 1);
	  minor0 = SIMD.Float32x4.add(SIMD.Float32x4.mul(row2, tmp1), minor0);
	  minor2 = SIMD.Float32x4.mul(row0, tmp1);
	  tmp1   = SIMD.Float32x4.swizzle(tmp1, 2, 3, 0, 1);
	  minor0 = SIMD.Float32x4.sub(minor0, SIMD.Float32x4.mul(row2, tmp1));
	  minor2 = SIMD.Float32x4.sub(SIMD.Float32x4.mul(row0, tmp1), minor2);
	  minor2 = SIMD.Float32x4.swizzle(minor2, 2, 3, 0, 1);

	  tmp1   = SIMD.Float32x4.mul(row0, row1);
	  tmp1   = SIMD.Float32x4.swizzle(tmp1, 1, 0, 3, 2);
	  minor2 = SIMD.Float32x4.add(SIMD.Float32x4.mul(row3, tmp1), minor2);
	  minor3 = SIMD.Float32x4.sub(SIMD.Float32x4.mul(row2, tmp1), minor3);
	  tmp1   = SIMD.Float32x4.swizzle(tmp1, 2, 3, 0, 1);
	  minor2 = SIMD.Float32x4.sub(SIMD.Float32x4.mul(row3, tmp1), minor2);
	  minor3 = SIMD.Float32x4.sub(minor3, SIMD.Float32x4.mul(row2, tmp1));

	  tmp1   = SIMD.Float32x4.mul(row0, row3);
	  tmp1   = SIMD.Float32x4.swizzle(tmp1, 1, 0, 3, 2);
	  minor1 = SIMD.Float32x4.sub(minor1, SIMD.Float32x4.mul(row2, tmp1));
	  minor2 = SIMD.Float32x4.add(SIMD.Float32x4.mul(row1, tmp1), minor2);
	  tmp1   = SIMD.Float32x4.swizzle(tmp1, 2, 3, 0, 1);
	  minor1 = SIMD.Float32x4.add(SIMD.Float32x4.mul(row2, tmp1), minor1);
	  minor2 = SIMD.Float32x4.sub(minor2, SIMD.Float32x4.mul(row1, tmp1));

	  tmp1   = SIMD.Float32x4.mul(row0, row2);
	  tmp1   = SIMD.Float32x4.swizzle(tmp1, 1, 0, 3, 2);
	  minor1 = SIMD.Float32x4.add(SIMD.Float32x4.mul(row3, tmp1), minor1);
	  minor3 = SIMD.Float32x4.sub(minor3, SIMD.Float32x4.mul(row1, tmp1));
	  tmp1   = SIMD.Float32x4.swizzle(tmp1, 2, 3, 0, 1);
	  minor1 = SIMD.Float32x4.sub(minor1, SIMD.Float32x4.mul(row3, tmp1));
	  minor3 = SIMD.Float32x4.add(SIMD.Float32x4.mul(row1, tmp1), minor3);

	  // Compute matrix determinant
	  det   = SIMD.Float32x4.mul(row0, minor0);
	  det   = SIMD.Float32x4.add(SIMD.Float32x4.swizzle(det, 2, 3, 0, 1), det);
	  det   = SIMD.Float32x4.add(SIMD.Float32x4.swizzle(det, 1, 0, 3, 2), det);
	  tmp1  = SIMD.Float32x4.reciprocalApproximation(det);
	  det   = SIMD.Float32x4.sub(
	               SIMD.Float32x4.add(tmp1, tmp1),
	               SIMD.Float32x4.mul(det, SIMD.Float32x4.mul(tmp1, tmp1)));
	  det   = SIMD.Float32x4.swizzle(det, 0, 0, 0, 0);
	  if (!det) {
	      return null;
	  }

	  // Compute matrix inverse
	  SIMD.Float32x4.store(out, 0,  SIMD.Float32x4.mul(det, minor0));
	  SIMD.Float32x4.store(out, 4,  SIMD.Float32x4.mul(det, minor1));
	  SIMD.Float32x4.store(out, 8,  SIMD.Float32x4.mul(det, minor2));
	  SIMD.Float32x4.store(out, 12, SIMD.Float32x4.mul(det, minor3));
	  return out;
	}

	/**
	 * Inverts a mat4 using SIMD if available and enabled
	 *
	 * @param {mat4} out the receiving matrix
	 * @param {mat4} a the source matrix
	 * @returns {mat4} out
	 */
	mat4.invert = glMatrix.USE_SIMD ? mat4.SIMD.invert : mat4.scalar.invert;

	/**
	 * Calculates the adjugate of a mat4 not using SIMD
	 *
	 * @param {mat4} out the receiving matrix
	 * @param {mat4} a the source matrix
	 * @returns {mat4} out
	 */
	mat4.scalar.adjoint = function(out, a) {
	    var 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],
	        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 adjugate of a mat4 using SIMD
	 *
	 * @param {mat4} out the receiving matrix
	 * @param {mat4} a the source matrix
	 * @returns {mat4} out
	 */
	mat4.SIMD.adjoint = function(out, a) {
	  var a0, a1, a2, a3;
	  var row0, row1, row2, row3;
	  var tmp1;
	  var minor0, minor1, minor2, minor3;

	  a0 = SIMD.Float32x4.load(a, 0);
	  a1 = SIMD.Float32x4.load(a, 4);
	  a2 = SIMD.Float32x4.load(a, 8);
	  a3 = SIMD.Float32x4.load(a, 12);

	  // Transpose the source matrix.  Sort of.  Not a true transpose operation
	  tmp1 = SIMD.Float32x4.shuffle(a0, a1, 0, 1, 4, 5);
	  row1 = SIMD.Float32x4.shuffle(a2, a3, 0, 1, 4, 5);
	  row0 = SIMD.Float32x4.shuffle(tmp1, row1, 0, 2, 4, 6);
	  row1 = SIMD.Float32x4.shuffle(row1, tmp1, 1, 3, 5, 7);

	  tmp1 = SIMD.Float32x4.shuffle(a0, a1, 2, 3, 6, 7);
	  row3 = SIMD.Float32x4.shuffle(a2, a3, 2, 3, 6, 7);
	  row2 = SIMD.Float32x4.shuffle(tmp1, row3, 0, 2, 4, 6);
	  row3 = SIMD.Float32x4.shuffle(row3, tmp1, 1, 3, 5, 7);

	  tmp1   = SIMD.Float32x4.mul(row2, row3);
	  tmp1   = SIMD.Float32x4.swizzle(tmp1, 1, 0, 3, 2);
	  minor0 = SIMD.Float32x4.mul(row1, tmp1);
	  minor1 = SIMD.Float32x4.mul(row0, tmp1);
	  tmp1   = SIMD.Float32x4.swizzle(tmp1, 2, 3, 0, 1);
	  minor0 = SIMD.Float32x4.sub(SIMD.Float32x4.mul(row1, tmp1), minor0);
	  minor1 = SIMD.Float32x4.sub(SIMD.Float32x4.mul(row0, tmp1), minor1);
	  minor1 = SIMD.Float32x4.swizzle(minor1, 2, 3, 0, 1);

	  tmp1   = SIMD.Float32x4.mul(row1, row2);
	  tmp1   = SIMD.Float32x4.swizzle(tmp1, 1, 0, 3, 2);
	  minor0 = SIMD.Float32x4.add(SIMD.Float32x4.mul(row3, tmp1), minor0);
	  minor3 = SIMD.Float32x4.mul(row0, tmp1);
	  tmp1   = SIMD.Float32x4.swizzle(tmp1, 2, 3, 0, 1);
	  minor0 = SIMD.Float32x4.sub(minor0, SIMD.Float32x4.mul(row3, tmp1));
	  minor3 = SIMD.Float32x4.sub(SIMD.Float32x4.mul(row0, tmp1), minor3);
	  minor3 = SIMD.Float32x4.swizzle(minor3, 2, 3, 0, 1);

	  tmp1   = SIMD.Float32x4.mul(SIMD.Float32x4.swizzle(row1, 2, 3, 0, 1), row3);
	  tmp1   = SIMD.Float32x4.swizzle(tmp1, 1, 0, 3, 2);
	  row2   = SIMD.Float32x4.swizzle(row2, 2, 3, 0, 1);
	  minor0 = SIMD.Float32x4.add(SIMD.Float32x4.mul(row2, tmp1), minor0);
	  minor2 = SIMD.Float32x4.mul(row0, tmp1);
	  tmp1   = SIMD.Float32x4.swizzle(tmp1, 2, 3, 0, 1);
	  minor0 = SIMD.Float32x4.sub(minor0, SIMD.Float32x4.mul(row2, tmp1));
	  minor2 = SIMD.Float32x4.sub(SIMD.Float32x4.mul(row0, tmp1), minor2);
	  minor2 = SIMD.Float32x4.swizzle(minor2, 2, 3, 0, 1);

	  tmp1   = SIMD.Float32x4.mul(row0, row1);
	  tmp1   = SIMD.Float32x4.swizzle(tmp1, 1, 0, 3, 2);
	  minor2 = SIMD.Float32x4.add(SIMD.Float32x4.mul(row3, tmp1), minor2);
	  minor3 = SIMD.Float32x4.sub(SIMD.Float32x4.mul(row2, tmp1), minor3);
	  tmp1   = SIMD.Float32x4.swizzle(tmp1, 2, 3, 0, 1);
	  minor2 = SIMD.Float32x4.sub(SIMD.Float32x4.mul(row3, tmp1), minor2);
	  minor3 = SIMD.Float32x4.sub(minor3, SIMD.Float32x4.mul(row2, tmp1));

	  tmp1   = SIMD.Float32x4.mul(row0, row3);
	  tmp1   = SIMD.Float32x4.swizzle(tmp1, 1, 0, 3, 2);
	  minor1 = SIMD.Float32x4.sub(minor1, SIMD.Float32x4.mul(row2, tmp1));
	  minor2 = SIMD.Float32x4.add(SIMD.Float32x4.mul(row1, tmp1), minor2);
	  tmp1   = SIMD.Float32x4.swizzle(tmp1, 2, 3, 0, 1);
	  minor1 = SIMD.Float32x4.add(SIMD.Float32x4.mul(row2, tmp1), minor1);
	  minor2 = SIMD.Float32x4.sub(minor2, SIMD.Float32x4.mul(row1, tmp1));

	  tmp1   = SIMD.Float32x4.mul(row0, row2);
	  tmp1   = SIMD.Float32x4.swizzle(tmp1, 1, 0, 3, 2);
	  minor1 = SIMD.Float32x4.add(SIMD.Float32x4.mul(row3, tmp1), minor1);
	  minor3 = SIMD.Float32x4.sub(minor3, SIMD.Float32x4.mul(row1, tmp1));
	  tmp1   = SIMD.Float32x4.swizzle(tmp1, 2, 3, 0, 1);
	  minor1 = SIMD.Float32x4.sub(minor1, SIMD.Float32x4.mul(row3, tmp1));
	  minor3 = SIMD.Float32x4.add(SIMD.Float32x4.mul(row1, tmp1), minor3);

	  SIMD.Float32x4.store(out, 0,  minor0);
	  SIMD.Float32x4.store(out, 4,  minor1);
	  SIMD.Float32x4.store(out, 8,  minor2);
	  SIMD.Float32x4.store(out, 12, minor3);
	  return out;
	};

	/**
	 * Calculates the adjugate of a mat4 using SIMD if available and enabled
	 *
	 * @param {mat4} out the receiving matrix
	 * @param {mat4} a the source matrix
	 * @returns {mat4} out
	 */
	 mat4.adjoint = glMatrix.USE_SIMD ? mat4.SIMD.adjoint : mat4.scalar.adjoint;

	/**
	 * Calculates the determinant of a mat4
	 *
	 * @param {mat4} a the source matrix
	 * @returns {Number} determinant of a
	 */
	mat4.determinant = function (a) {
	    var 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],
	        a30 = a[12], a31 = a[13], a32 = a[14], a33 = a[15],

	        b00 = a00 * a11 - a01 * a10,
	        b01 = a00 * a12 - a02 * a10,
	        b02 = a00 * a13 - a03 * a10,
	        b03 = a01 * a12 - a02 * a11,
	        b04 = a01 * a13 - a03 * a11,
	        b05 = a02 * a13 - a03 * a12,
	        b06 = a20 * a31 - a21 * a30,
	        b07 = a20 * a32 - a22 * a30,
	        b08 = a20 * a33 - a23 * a30,
	        b09 = a21 * a32 - a22 * a31,
	        b10 = a21 * a33 - a23 * a31,
	        b11 = a22 * a33 - a23 * a32;

	    // Calculate the determinant
	    return b00 * b11 - b01 * b10 + b02 * b09 + b03 * b08 - b04 * b07 + b05 * b06;
	};

	/**
	 * Multiplies two mat4's explicitly using SIMD
	 *
	 * @param {mat4} out the receiving matrix
	 * @param {mat4} a the first operand, must be a Float32Array
	 * @param {mat4} b the second operand, must be a Float32Array
	 * @returns {mat4} out
	 */
	mat4.SIMD.multiply = function (out, a, b) {
	    var a0 = SIMD.Float32x4.load(a, 0);
	    var a1 = SIMD.Float32x4.load(a, 4);
	    var a2 = SIMD.Float32x4.load(a, 8);
	    var a3 = SIMD.Float32x4.load(a, 12);

	    var b0 = SIMD.Float32x4.load(b, 0);
	    var out0 = SIMD.Float32x4.add(
	                   SIMD.Float32x4.mul(SIMD.Float32x4.swizzle(b0, 0, 0, 0, 0), a0),
	                   SIMD.Float32x4.add(
	                       SIMD.Float32x4.mul(SIMD.Float32x4.swizzle(b0, 1, 1, 1, 1), a1),
	                       SIMD.Float32x4.add(
	                           SIMD.Float32x4.mul(SIMD.Float32x4.swizzle(b0, 2, 2, 2, 2), a2),
	                           SIMD.Float32x4.mul(SIMD.Float32x4.swizzle(b0, 3, 3, 3, 3), a3))));
	    SIMD.Float32x4.store(out, 0, out0);

	    var b1 = SIMD.Float32x4.load(b, 4);
	    var out1 = SIMD.Float32x4.add(
	                   SIMD.Float32x4.mul(SIMD.Float32x4.swizzle(b1, 0, 0, 0, 0), a0),
	                   SIMD.Float32x4.add(
	                       SIMD.Float32x4.mul(SIMD.Float32x4.swizzle(b1, 1, 1, 1, 1), a1),
	                       SIMD.Float32x4.add(
	                           SIMD.Float32x4.mul(SIMD.Float32x4.swizzle(b1, 2, 2, 2, 2), a2),
	                           SIMD.Float32x4.mul(SIMD.Float32x4.swizzle(b1, 3, 3, 3, 3), a3))));
	    SIMD.Float32x4.store(out, 4, out1);

	    var b2 = SIMD.Float32x4.load(b, 8);
	    var out2 = SIMD.Float32x4.add(
	                   SIMD.Float32x4.mul(SIMD.Float32x4.swizzle(b2, 0, 0, 0, 0), a0),
	                   SIMD.Float32x4.add(
	                       SIMD.Float32x4.mul(SIMD.Float32x4.swizzle(b2, 1, 1, 1, 1), a1),
	                       SIMD.Float32x4.add(
	                               SIMD.Float32x4.mul(SIMD.Float32x4.swizzle(b2, 2, 2, 2, 2), a2),
	                               SIMD.Float32x4.mul(SIMD.Float32x4.swizzle(b2, 3, 3, 3, 3), a3))));
	    SIMD.Float32x4.store(out, 8, out2);

	    var b3 = SIMD.Float32x4.load(b, 12);
	    var out3 = SIMD.Float32x4.add(
	                   SIMD.Float32x4.mul(SIMD.Float32x4.swizzle(b3, 0, 0, 0, 0), a0),
	                   SIMD.Float32x4.add(
	                        SIMD.Float32x4.mul(SIMD.Float32x4.swizzle(b3, 1, 1, 1, 1), a1),
	                        SIMD.Float32x4.add(
	                            SIMD.Float32x4.mul(SIMD.Float32x4.swizzle(b3, 2, 2, 2, 2), a2),
	                            SIMD.Float32x4.mul(SIMD.Float32x4.swizzle(b3, 3, 3, 3, 3), a3))));
	    SIMD.Float32x4.store(out, 12, out3);

	    return out;
	};

	/**
	 * Multiplies two mat4's explicitly not using SIMD
	 *
	 * @param {mat4} out the receiving matrix
	 * @param {mat4} a the first operand
	 * @param {mat4} b the second operand
	 * @returns {mat4} out
	 */
	mat4.scalar.multiply = function (out, a, b) {
	    var 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],
	        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;
	};

	/**
	 * Multiplies two mat4's using SIMD if available and enabled
	 *
	 * @param {mat4} out the receiving matrix
	 * @param {mat4} a the first operand
	 * @param {mat4} b the second operand
	 * @returns {mat4} out
	 */
	mat4.multiply = glMatrix.USE_SIMD ? mat4.SIMD.multiply : mat4.scalar.multiply;

	/**
	 * Alias for {@link mat4.multiply}
	 * @function
	 */
	mat4.mul = mat4.multiply;

	/**
	 * Translate a mat4 by the given vector not using SIMD
	 *
	 * @param {mat4} out the receiving matrix
	 * @param {mat4} a the matrix to translate
	 * @param {vec3} v vector to translate by
	 * @returns {mat4} out
	 */
	mat4.scalar.translate = function (out, a, v) {
	    var x = v[0], y = v[1], z = v[2],
	        a00, a01, a02, a03,
	        a10, a11, a12, a13,
	        a20, a21, a22, a23;

	    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;
	};

	/**
	 * Translates a mat4 by the given vector using SIMD
	 *
	 * @param {mat4} out the receiving matrix
	 * @param {mat4} a the matrix to translate
	 * @param {vec3} v vector to translate by
	 * @returns {mat4} out
	 */
	mat4.SIMD.translate = function (out, a, v) {
	    var a0 = SIMD.Float32x4.load(a, 0),
	        a1 = SIMD.Float32x4.load(a, 4),
	        a2 = SIMD.Float32x4.load(a, 8),
	        a3 = SIMD.Float32x4.load(a, 12),
	        vec = SIMD.Float32x4(v[0], v[1], v[2] , 0);

	    if (a !== out) {
	        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];
	    }

	    a0 = SIMD.Float32x4.mul(a0, SIMD.Float32x4.swizzle(vec, 0, 0, 0, 0));
	    a1 = SIMD.Float32x4.mul(a1, SIMD.Float32x4.swizzle(vec, 1, 1, 1, 1));
	    a2 = SIMD.Float32x4.mul(a2, SIMD.Float32x4.swizzle(vec, 2, 2, 2, 2));

	    var t0 = SIMD.Float32x4.add(a0, SIMD.Float32x4.add(a1, SIMD.Float32x4.add(a2, a3)));
	    SIMD.Float32x4.store(out, 12, t0);

	    return out;
	};

	/**
	 * Translates a mat4 by the given vector using SIMD if available and enabled
	 *
	 * @param {mat4} out the receiving matrix
	 * @param {mat4} a the matrix to translate
	 * @param {vec3} v vector to translate by
	 * @returns {mat4} out
	 */
	mat4.translate = glMatrix.USE_SIMD ? mat4.SIMD.translate : mat4.scalar.translate;

	/**
	 * 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
	 **/
	mat4.scalar.scale = function(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;
	};

	/**
	 * Scales the mat4 by the dimensions in the given vec3 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
	 **/
	mat4.SIMD.scale = function(out, a, v) {
	    var a0, a1, a2;
	    var vec = SIMD.Float32x4(v[0], v[1], v[2], 0);

	    a0 = SIMD.Float32x4.load(a, 0);
	    SIMD.Float32x4.store(
	        out, 0, SIMD.Float32x4.mul(a0, SIMD.Float32x4.swizzle(vec, 0, 0, 0, 0)));

	    a1 = SIMD.Float32x4.load(a, 4);
	    SIMD.Float32x4.store(
	        out, 4, SIMD.Float32x4.mul(a1, SIMD.Float32x4.swizzle(vec, 1, 1, 1, 1)));

	    a2 = SIMD.Float32x4.load(a, 8);
	    SIMD.Float32x4.store(
	        out, 8, SIMD.Float32x4.mul(a2, SIMD.Float32x4.swizzle(vec, 2, 2, 2, 2)));

	    out[12] = a[12];
	    out[13] = a[13];
	    out[14] = a[14];
	    out[15] = a[15];
	    return out;
	};

	/**
	 * Scales the mat4 by the dimensions in the given vec3 using SIMD if available and enabled
	 *
	 * @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
	 */
	mat4.scale = glMatrix.USE_SIMD ? mat4.SIMD.scale : mat4.scalar.scale;

	/**
	 * 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
	 */
	mat4.rotate = function (out, a, rad, axis) {
	    var x = axis[0], y = axis[1], z = axis[2],
	        len = Math.sqrt(x * x + y * y + z * z),
	        s, c, t,
	        a00, a01, a02, a03,
	        a10, a11, a12, a13,
	        a20, a21, a22, a23,
	        b00, b01, b02,
	        b10, b11, b12,
	        b20, b21, b22;

	    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 not using SIMD
	 *
	 * @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
	 */
	mat4.scalar.rotateX = function (out, a, rad) {
	    var s = Math.sin(rad),
	        c = Math.cos(rad),
	        a10 = a[4],
	        a11 = a[5],
	        a12 = a[6],
	        a13 = a[7],
	        a20 = a[8],
	        a21 = a[9],
	        a22 = a[10],
	        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 X axis using SIMD
	 *
	 * @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
	 */
	mat4.SIMD.rotateX = function (out, a, rad) {
	    var s = SIMD.Float32x4.splat(Math.sin(rad)),
	        c = SIMD.Float32x4.splat(Math.cos(rad));

	    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
	    var a_1 = SIMD.Float32x4.load(a, 4);
	    var a_2 = SIMD.Float32x4.load(a, 8);
	    SIMD.Float32x4.store(out, 4,
	                         SIMD.Float32x4.add(SIMD.Float32x4.mul(a_1, c), SIMD.Float32x4.mul(a_2, s)));
	    SIMD.Float32x4.store(out, 8,
	                         SIMD.Float32x4.sub(SIMD.Float32x4.mul(a_2, c), SIMD.Float32x4.mul(a_1, s)));
	    return out;
	};

	/**
	 * Rotates a matrix by the given angle around the X axis using SIMD if availabe and enabled
	 *
	 * @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
	 */
	mat4.rotateX = glMatrix.USE_SIMD ? mat4.SIMD.rotateX : mat4.scalar.rotateX;

	/**
	 * Rotates a matrix by the given angle around the Y axis not using SIMD
	 *
	 * @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
	 */
	mat4.scalar.rotateY = function (out, a, rad) {
	    var s = Math.sin(rad),
	        c = Math.cos(rad),
	        a00 = a[0],
	        a01 = a[1],
	        a02 = a[2],
	        a03 = a[3],
	        a20 = a[8],
	        a21 = a[9],
	        a22 = a[10],
	        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 Y axis using SIMD
	 *
	 * @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
	 */
	mat4.SIMD.rotateY = function (out, a, rad) {
	    var s = SIMD.Float32x4.splat(Math.sin(rad)),
	        c = SIMD.Float32x4.splat(Math.cos(rad));

	    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
	    var a_0 = SIMD.Float32x4.load(a, 0);
	    var a_2 = SIMD.Float32x4.load(a, 8);
	    SIMD.Float32x4.store(out, 0,
	                         SIMD.Float32x4.sub(SIMD.Float32x4.mul(a_0, c), SIMD.Float32x4.mul(a_2, s)));
	    SIMD.Float32x4.store(out, 8,
	                         SIMD.Float32x4.add(SIMD.Float32x4.mul(a_0, s), SIMD.Float32x4.mul(a_2, c)));
	    return out;
	};

	/**
	 * Rotates a matrix by the given angle around the Y axis if SIMD available and enabled
	 *
	 * @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
	 */
	 mat4.rotateY = glMatrix.USE_SIMD ? mat4.SIMD.rotateY : mat4.scalar.rotateY;

	/**
	 * Rotates a matrix by the given angle around the Z axis not using SIMD
	 *
	 * @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
	 */
	mat4.scalar.rotateZ = function (out, a, rad) {
	    var s = Math.sin(rad),
	        c = Math.cos(rad),
	        a00 = a[0],
	        a01 = a[1],
	        a02 = a[2],
	        a03 = a[3],
	        a10 = a[4],
	        a11 = a[5],
	        a12 = a[6],
	        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;
	};

	/**
	 * Rotates a matrix by the given angle around the Z axis using SIMD
	 *
	 * @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
	 */
	mat4.SIMD.rotateZ = function (out, a, rad) {
	    var s = SIMD.Float32x4.splat(Math.sin(rad)),
	        c = SIMD.Float32x4.splat(Math.cos(rad));

	    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
	    var a_0 = SIMD.Float32x4.load(a, 0);
	    var a_1 = SIMD.Float32x4.load(a, 4);
	    SIMD.Float32x4.store(out, 0,
	                         SIMD.Float32x4.add(SIMD.Float32x4.mul(a_0, c), SIMD.Float32x4.mul(a_1, s)));
	    SIMD.Float32x4.store(out, 4,
	                         SIMD.Float32x4.sub(SIMD.Float32x4.mul(a_1, c), SIMD.Float32x4.mul(a_0, s)));
	    return out;
	};

	/**
	 * Rotates a matrix by the given angle around the Z axis if SIMD available and enabled
	 *
	 * @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
	 */
	 mat4.rotateZ = glMatrix.USE_SIMD ? mat4.SIMD.rotateZ : mat4.scalar.rotateZ;

	/**
	 * 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
	 */
	mat4.fromTranslation = function(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
	 */
	mat4.fromScaling = function(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
	 */
	mat4.fromRotation = function(out, rad, axis) {
	    var x = axis[0], y = axis[1], z = axis[2],
	        len = Math.sqrt(x * x + y * y + z * z),
	        s, c, t;

	    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
	 */
	mat4.fromXRotation = function(out, rad) {
	    var s = Math.sin(rad),
	        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
	 */
	mat4.fromYRotation = function(out, rad) {
	    var s = Math.sin(rad),
	        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
	 */
	mat4.fromZRotation = function(out, rad) {
	    var s = Math.sin(rad),
	        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);
	 *     var 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
	 */
	mat4.fromRotationTranslation = function (out, q, v) {
	    // Quaternion math
	    var x = q[0], y = q[1], z = q[2], w = q[3],
	        x2 = x + x,
	        y2 = y + y,
	        z2 = z + z,

	        xx = x * x2,
	        xy = x * y2,
	        xz = x * z2,
	        yy = y * y2,
	        yz = y * z2,
	        zz = z * z2,
	        wx = w * x2,
	        wy = w * y2,
	        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
	 */
	mat4.getTranslation = function (out, mat) {
	  out[0] = mat[12];
	  out[1] = mat[13];
	  out[2] = mat[14];

	  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
	 */
	mat4.getRotation = function (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);
	 *     var 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
	 */
	mat4.fromRotationTranslationScale = function (out, q, v, s) {
	    // Quaternion math
	    var x = q[0], y = q[1], z = q[2], w = q[3],
	        x2 = x + x,
	        y2 = y + y,
	        z2 = z + z,

	        xx = x * x2,
	        xy = x * y2,
	        xz = x * z2,
	        yy = y * y2,
	        yz = y * z2,
	        zz = z * z2,
	        wx = w * x2,
	        wy = w * y2,
	        wz = w * z2,
	        sx = s[0],
	        sy = s[1],
	        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);
	 *     var 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
	 */
	mat4.fromRotationTranslationScaleOrigin = function (out, q, v, s, o) {
	  // Quaternion math
	  var x = q[0], y = q[1], z = q[2], w = q[3],
	      x2 = x + x,
	      y2 = y + y,
	      z2 = z + z,

	      xx = x * x2,
	      xy = x * y2,
	      xz = x * z2,
	      yy = y * y2,
	      yz = y * z2,
	      zz = z * z2,
	      wx = w * x2,
	      wy = w * y2,
	      wz = w * z2,

	      sx = s[0],
	      sy = s[1],
	      sz = s[2],

	      ox = o[0],
	      oy = o[1],
	      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
	 */
	mat4.fromQuat = function (out, q) {
	    var x = q[0], y = q[1], z = q[2], w = q[3],
	        x2 = x + x,
	        y2 = y + y,
	        z2 = z + z,

	        xx = x * x2,
	        yx = y * x2,
	        yy = y * y2,
	        zx = z * x2,
	        zy = z * y2,
	        zz = z * z2,
	        wx = w * x2,
	        wy = w * y2,
	        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
	 */
	mat4.frustum = function (out, left, right, bottom, top, near, far) {
	    var rl = 1 / (right - left),
	        tb = 1 / (top - bottom),
	        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
	 */
	mat4.perspective = function (out, fovy, aspect, near, far) {
	    var f = 1.0 / Math.tan(fovy / 2),
	        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
	 */
	mat4.perspectiveFromFieldOfView = function (out, fov, near, far) {
	    var upTan = Math.tan(fov.upDegrees * Math.PI/180.0),
	        downTan = Math.tan(fov.downDegrees * Math.PI/180.0),
	        leftTan = Math.tan(fov.leftDegrees * Math.PI/180.0),
	        rightTan = Math.tan(fov.rightDegrees * Math.PI/180.0),
	        xScale = 2.0 / (leftTan + rightTan),
	        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
	 */
	mat4.ortho = function (out, left, right, bottom, top, near, far) {
	    var lr = 1 / (left - right),
	        bt = 1 / (bottom - top),
	        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
	 */
	mat4.lookAt = function (out, eye, center, up) {
	    var x0, x1, x2, y0, y1, y2, z0, z1, z2, len,
	        eyex = eye[0],
	        eyey = eye[1],
	        eyez = eye[2],
	        upx = up[0],
	        upy = up[1],
	        upz = up[2],
	        centerx = center[0],
	        centery = center[1],
	        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;
	};

	/**
	 * Returns a string representation of a mat4
	 *
	 * @param {mat4} a matrix to represent as a string
	 * @returns {String} string representation of the matrix
	 */
	mat4.str = function (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
	 */
	mat4.frob = function (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
	 */
	mat4.add = function(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
	 */
	mat4.subtract = function(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;
	};

	/**
	 * Alias for {@link mat4.subtract}
	 * @function
	 */
	mat4.sub = mat4.subtract;

	/**
	 * 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
	 */
	mat4.multiplyScalar = function(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
	 */
	mat4.multiplyScalarAndAdd = function(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.
	 */
	mat4.exactEquals = function (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.
	 */
	mat4.equals = function (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],  a9  = a[9],  a10 = a[10], a11 = a[11], 
	        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],
	        b4  = b[4],  b5  = b[5],  b6  = b[6],  b7  = b[7], 
	        b8  = b[8],  b9  = b[9],  b10 = b[10], b11 = b[11], 
	        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)));
	};



	module.exports = mat4;


/***/ },
/* 6 */
/***/ function(module, exports, __webpack_require__) {

	/* 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. */

	var glMatrix = __webpack_require__(1);
	var mat3 = __webpack_require__(4);
	var vec3 = __webpack_require__(7);
	var vec4 = __webpack_require__(8);

	/**
	 * @class Quaternion
	 * @name quat
	 */
	var quat = {};

	/**
	 * Creates a new identity quat
	 *
	 * @returns {quat} a new quaternion
	 */
	quat.create = function() {
	    var out = new glMatrix.ARRAY_TYPE(4);
	    out[0] = 0;
	    out[1] = 0;
	    out[2] = 0;
	    out[3] = 1;
	    return out;
	};

	/**
	 * 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
	 */
	quat.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.length(tmpvec3) < 0.000001)
	                vec3.cross(tmpvec3, yUnitVec3, a);
	            vec3.normalize(tmpvec3, tmpvec3);
	            quat.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 quat.normalize(out, 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
	 */
	quat.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 quat.normalize(out, quat.fromMat3(out, matr));
	    };
	})();

	/**
	 * Creates a new quat initialized with values from an existing quaternion
	 *
	 * @param {quat} a quaternion to clone
	 * @returns {quat} a new quaternion
	 * @function
	 */
	quat.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
	 */
	quat.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
	 */
	quat.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
	 */
	quat.set = vec4.set;

	/**
	 * Set a quat to the identity quaternion
	 *
	 * @param {quat} out the receiving quaternion
	 * @returns {quat} out
	 */
	quat.identity = function(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
	 **/
	quat.setAxisAngle = function(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
	 */
	quat.getAxisAngle = function(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;
	};

	/**
	 * 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
	 */
	quat.add = vec4.add;

	/**
	 * 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
	 */
	quat.multiply = function(out, a, b) {
	    var ax = a[0], ay = a[1], az = a[2], aw = a[3],
	        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;
	};

	/**
	 * Alias for {@link quat.multiply}
	 * @function
	 */
	quat.mul = quat.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
	 */
	quat.scale = vec4.scale;

	/**
	 * 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
	 */
	quat.rotateX = function (out, a, rad) {
	    rad *= 0.5; 

	    var ax = a[0], ay = a[1], az = a[2], aw = a[3],
	        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
	 */
	quat.rotateY = function (out, a, rad) {
	    rad *= 0.5; 

	    var ax = a[0], ay = a[1], az = a[2], aw = a[3],
	        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
	 */
	quat.rotateZ = function (out, a, rad) {
	    rad *= 0.5; 

	    var ax = a[0], ay = a[1], az = a[2], aw = a[3],
	        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
	 */
	quat.calculateW = function (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;
	};

	/**
	 * 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
	 */
	quat.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
	 */
	quat.lerp = vec4.lerp;

	/**
	 * 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
	 */
	quat.slerp = function (out, a, b, t) {
	    // benchmarks:
	    //    http://jsperf.com/quaternion-slerp-implementations

	    var ax = a[0], ay = a[1], az = a[2], aw = a[3],
	        bx = b[0], by = b[1], bz = b[2], bw = b[3];

	    var        omega, cosom, sinom, scale0, scale1;

	    // 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;
	};

	/**
	 * 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
	 */
	quat.sqlerp = (function () {
	  var temp1 = quat.create();
	  var temp2 = quat.create();
	  
	  return function (out, a, b, c, d, t) {
	    quat.slerp(temp1, a, d, t);
	    quat.slerp(temp2, b, c, t);
	    quat.slerp(out, temp1, temp2, 2 * t * (1 - t));
	    
	    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
	 */
	quat.invert = function(out, a) {
	    var a0 = a[0], a1 = a[1], a2 = a[2], a3 = a[3],
	        dot = a0*a0 + a1*a1 + a2*a2 + a3*a3,
	        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
	 */
	quat.conjugate = function (out, a) {
	    out[0] = -a[0];
	    out[1] = -a[1];
	    out[2] = -a[2];
	    out[3] = a[3];
	    return out;
	};

	/**
	 * Calculates the length of a quat
	 *
	 * @param {quat} a vector to calculate length of
	 * @returns {Number} length of a
	 * @function
	 */
	quat.length = vec4.length;

	/**
	 * Alias for {@link quat.length}
	 * @function
	 */
	quat.len = quat.length;

	/**
	 * Calculates the squared length of a quat
	 *
	 * @param {quat} a vector to calculate squared length of
	 * @returns {Number} squared length of a
	 * @function
	 */
	quat.squaredLength = vec4.squaredLength;

	/**
	 * Alias for {@link quat.squaredLength}
	 * @function
	 */
	quat.sqrLen = quat.squaredLength;

	/**
	 * Normalize a quat
	 *
	 * @param {quat} out the receiving quaternion
	 * @param {quat} a quaternion to normalize
	 * @returns {quat} out
	 * @function
	 */
	quat.normalize = vec4.normalize;

	/**
	 * 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
	 */
	quat.fromMat3 = function(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;

	    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;
	};

	/**
	 * Returns a string representation of a quatenion
	 *
	 * @param {quat} a vector to represent as a string
	 * @returns {String} string representation of the vector
	 */
	quat.str = function (a) {
	    return 'quat(' + a[0] + ', ' + a[1] + ', ' + a[2] + ', ' + a[3] + ')';
	};

	/**
	 * 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.
	 */
	quat.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.
	 */
	quat.equals = vec4.equals;

	module.exports = quat;


/***/ },
/* 7 */
/***/ function(module, exports, __webpack_require__) {

	/* 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. */

	var glMatrix = __webpack_require__(1);

	/**
	 * @class 3 Dimensional Vector
	 * @name vec3
	 */
	var vec3 = {};

	/**
	 * Creates a new, empty vec3
	 *
	 * @returns {vec3} a new 3D vector
	 */
	vec3.create = function() {
	    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
	 */
	vec3.clone = function(a) {
	    var out = new glMatrix.ARRAY_TYPE(3);
	    out[0] = a[0];
	    out[1] = a[1];
	    out[2] = a[2];
	    return out;
	};

	/**
	 * 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
	 */
	vec3.fromValues = function(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
	 */
	vec3.copy = function(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
	 */
	vec3.set = function(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
	 */
	vec3.add = function(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
	 */
	vec3.subtract = function(out, a, b) {
	    out[0] = a[0] - b[0];
	    out[1] = a[1] - b[1];
	    out[2] = a[2] - b[2];
	    return out;
	};

	/**
	 * Alias for {@link vec3.subtract}
	 * @function
	 */
	vec3.sub = vec3.subtract;

	/**
	 * 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
	 */
	vec3.multiply = function(out, a, b) {
	    out[0] = a[0] * b[0];
	    out[1] = a[1] * b[1];
	    out[2] = a[2] * b[2];
	    return out;
	};

	/**
	 * Alias for {@link vec3.multiply}
	 * @function
	 */
	vec3.mul = vec3.multiply;

	/**
	 * 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
	 */
	vec3.divide = function(out, a, b) {
	    out[0] = a[0] / b[0];
	    out[1] = a[1] / b[1];
	    out[2] = a[2] / b[2];
	    return out;
	};

	/**
	 * Alias for {@link vec3.divide}
	 * @function
	 */
	vec3.div = vec3.divide;

	/**
	 * Math.ceil the components of a vec3
	 *
	 * @param {vec3} out the receiving vector
	 * @param {vec3} a vector to ceil
	 * @returns {vec3} out
	 */
	vec3.ceil = function (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
	 */
	vec3.floor = function (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
	 */
	vec3.min = function(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
	 */
	vec3.max = function(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
	 */
	vec3.round = function (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
	 */
	vec3.scale = function(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
	 */
	vec3.scaleAndAdd = function(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
	 */
	vec3.distance = function(a, b) {
	    var x = b[0] - a[0],
	        y = b[1] - a[1],
	        z = b[2] - a[2];
	    return Math.sqrt(x*x + y*y + z*z);
	};

	/**
	 * Alias for {@link vec3.distance}
	 * @function
	 */
	vec3.dist = vec3.distance;

	/**
	 * 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
	 */
	vec3.squaredDistance = function(a, b) {
	    var x = b[0] - a[0],
	        y = b[1] - a[1],
	        z = b[2] - a[2];
	    return x*x + y*y + z*z;
	};

	/**
	 * Alias for {@link vec3.squaredDistance}
	 * @function
	 */
	vec3.sqrDist = vec3.squaredDistance;

	/**
	 * Calculates the length of a vec3
	 *
	 * @param {vec3} a vector to calculate length of
	 * @returns {Number} length of a
	 */
	vec3.length = function (a) {
	    var x = a[0],
	        y = a[1],
	        z = a[2];
	    return Math.sqrt(x*x + y*y + z*z);
	};

	/**
	 * Alias for {@link vec3.length}
	 * @function
	 */
	vec3.len = vec3.length;

	/**
	 * Calculates the squared length of a vec3
	 *
	 * @param {vec3} a vector to calculate squared length of
	 * @returns {Number} squared length of a
	 */
	vec3.squaredLength = function (a) {
	    var x = a[0],
	        y = a[1],
	        z = a[2];
	    return x*x + y*y + z*z;
	};

	/**
	 * Alias for {@link vec3.squaredLength}
	 * @function
	 */
	vec3.sqrLen = vec3.squaredLength;

	/**
	 * Negates the components of a vec3
	 *
	 * @param {vec3} out the receiving vector
	 * @param {vec3} a vector to negate
	 * @returns {vec3} out
	 */
	vec3.negate = function(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
	 */
	vec3.inverse = function(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
	 */
	vec3.normalize = function(out, a) {
	    var x = a[0],
	        y = a[1],
	        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
	 */
	vec3.dot = function (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
	 */
	vec3.cross = function(out, a, b) {
	    var ax = a[0], ay = a[1], az = a[2],
	        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
	 */
	vec3.lerp = function (out, a, b, t) {
	    var ax = a[0],
	        ay = a[1],
	        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
	 */
	vec3.hermite = function (out, a, b, c, d, t) {
	  var factorTimes2 = t * t,
	      factor1 = factorTimes2 * (2 * t - 3) + 1,
	      factor2 = factorTimes2 * (t - 2) + t,
	      factor3 = factorTimes2 * (t - 1),
	      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
	 */
	vec3.bezier = function (out, a, b, c, d, t) {
	  var inverseFactor = 1 - t,
	      inverseFactorTimesTwo = inverseFactor * inverseFactor,
	      factorTimes2 = t * t,
	      factor1 = inverseFactorTimesTwo * inverseFactor,
	      factor2 = 3 * t * inverseFactorTimesTwo,
	      factor3 = 3 * factorTimes2 * inverseFactor,
	      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
	 */
	vec3.random = function (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
	 */
	vec3.transformMat4 = function(out, a, m) {
	    var x = a[0], y = a[1], z = a[2],
	        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 {mat4} m the 3x3 matrix to transform with
	 * @returns {vec3} out
	 */
	vec3.transformMat3 = function(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
	 */
	vec3.transformQuat = function(out, a, q) {
	    // benchmarks: http://jsperf.com/quaternion-transform-vec3-implementations

	    var x = a[0], y = a[1], z = a[2],
	        qx = q[0], qy = q[1], qz = q[2], qw = q[3],

	        // calculate quat * vec
	        ix = qw * x + qy * z - qz * y,
	        iy = qw * y + qz * x - qx * z,
	        iz = qw * z + qx * y - qy * x,
	        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
	 */
	vec3.rotateX = function(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
	 */
	vec3.rotateY = function(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
	 */
	vec3.rotateZ = function(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;
	};

	/**
	 * 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
	 */
	vec3.forEach = (function() {
	    var vec = vec3.create();

	    return function(a, stride, offset, count, fn, arg) {
	        var i, l;
	        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;
	    };
	})();

	/**
	 * 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
	 */
	vec3.angle = function(a, b) {
	   
	    var tempA = vec3.fromValues(a[0], a[1], a[2]);
	    var tempB = vec3.fromValues(b[0], b[1], b[2]);
	 
	    vec3.normalize(tempA, tempA);
	    vec3.normalize(tempB, tempB);
	 
	    var cosine = vec3.dot(tempA, tempB);

	    if(cosine > 1.0){
	        return 0;
	    } 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
	 */
	vec3.str = function (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.
	 */
	vec3.exactEquals = function (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.
	 */
	vec3.equals = function (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)));
	};

	module.exports = vec3;


/***/ },
/* 8 */
/***/ function(module, exports, __webpack_require__) {

	/* 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. */

	var glMatrix = __webpack_require__(1);

	/**
	 * @class 4 Dimensional Vector
	 * @name vec4
	 */
	var vec4 = {};

	/**
	 * Creates a new, empty vec4
	 *
	 * @returns {vec4} a new 4D vector
	 */
	vec4.create = function() {
	    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
	 */
	vec4.clone = function(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
	 */
	vec4.fromValues = function(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
	 */
	vec4.copy = function(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
	 */
	vec4.set = function(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
	 */
	vec4.add = function(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
	 */
	vec4.subtract = function(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;
	};

	/**
	 * Alias for {@link vec4.subtract}
	 * @function
	 */
	vec4.sub = vec4.subtract;

	/**
	 * 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
	 */
	vec4.multiply = function(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;
	};

	/**
	 * Alias for {@link vec4.multiply}
	 * @function
	 */
	vec4.mul = vec4.multiply;

	/**
	 * 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
	 */
	vec4.divide = function(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;
	};

	/**
	 * Alias for {@link vec4.divide}
	 * @function
	 */
	vec4.div = vec4.divide;

	/**
	 * Math.ceil the components of a vec4
	 *
	 * @param {vec4} out the receiving vector
	 * @param {vec4} a vector to ceil
	 * @returns {vec4} out
	 */
	vec4.ceil = function (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
	 */
	vec4.floor = function (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
	 */
	vec4.min = function(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
	 */
	vec4.max = function(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
	 */
	vec4.round = function (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
	 */
	vec4.scale = function(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
	 */
	vec4.scaleAndAdd = function(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
	 */
	vec4.distance = function(a, b) {
	    var x = b[0] - a[0],
	        y = b[1] - a[1],
	        z = b[2] - a[2],
	        w = b[3] - a[3];
	    return Math.sqrt(x*x + y*y + z*z + w*w);
	};

	/**
	 * Alias for {@link vec4.distance}
	 * @function
	 */
	vec4.dist = vec4.distance;

	/**
	 * 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
	 */
	vec4.squaredDistance = function(a, b) {
	    var x = b[0] - a[0],
	        y = b[1] - a[1],
	        z = b[2] - a[2],
	        w = b[3] - a[3];
	    return x*x + y*y + z*z + w*w;
	};

	/**
	 * Alias for {@link vec4.squaredDistance}
	 * @function
	 */
	vec4.sqrDist = vec4.squaredDistance;

	/**
	 * Calculates the length of a vec4
	 *
	 * @param {vec4} a vector to calculate length of
	 * @returns {Number} length of a
	 */
	vec4.length = function (a) {
	    var x = a[0],
	        y = a[1],
	        z = a[2],
	        w = a[3];
	    return Math.sqrt(x*x + y*y + z*z + w*w);
	};

	/**
	 * Alias for {@link vec4.length}
	 * @function
	 */
	vec4.len = vec4.length;

	/**
	 * Calculates the squared length of a vec4
	 *
	 * @param {vec4} a vector to calculate squared length of
	 * @returns {Number} squared length of a
	 */
	vec4.squaredLength = function (a) {
	    var x = a[0],
	        y = a[1],
	        z = a[2],
	        w = a[3];
	    return x*x + y*y + z*z + w*w;
	};

	/**
	 * Alias for {@link vec4.squaredLength}
	 * @function
	 */
	vec4.sqrLen = vec4.squaredLength;

	/**
	 * Negates the components of a vec4
	 *
	 * @param {vec4} out the receiving vector
	 * @param {vec4} a vector to negate
	 * @returns {vec4} out
	 */
	vec4.negate = function(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
	 */
	vec4.inverse = function(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
	 */
	vec4.normalize = function(out, a) {
	    var x = a[0],
	        y = a[1],
	        z = a[2],
	        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
	 */
	vec4.dot = function (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
	 */
	vec4.lerp = function (out, a, b, t) {
	    var ax = a[0],
	        ay = a[1],
	        az = a[2],
	        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
	 */
	vec4.random = function (out, scale) {
	    scale = scale || 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();
	    vec4.normalize(out, out);
	    vec4.scale(out, out, scale);
	    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
	 */
	vec4.transformMat4 = function(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
	 */
	vec4.transformQuat = function(out, a, q) {
	    var x = a[0], y = a[1], z = a[2],
	        qx = q[0], qy = q[1], qz = q[2], qw = q[3],

	        // calculate quat * vec
	        ix = qw * x + qy * z - qz * y,
	        iy = qw * y + qz * x - qx * z,
	        iz = qw * z + qx * y - qy * x,
	        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;
	};

	/**
	 * 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
	 */
	vec4.forEach = (function() {
	    var vec = vec4.create();

	    return function(a, stride, offset, count, fn, arg) {
	        var i, l;
	        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;
	    };
	})();

	/**
	 * Returns a string representation of a vector
	 *
	 * @param {vec4} a vector to represent as a string
	 * @returns {String} string representation of the vector
	 */
	vec4.str = function (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.
	 */
	vec4.exactEquals = function (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.
	 */
	vec4.equals = function (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)));
	};

	module.exports = vec4;


/***/ },
/* 9 */
/***/ function(module, exports, __webpack_require__) {

	/* 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. */

	var glMatrix = __webpack_require__(1);

	/**
	 * @class 2 Dimensional Vector
	 * @name vec2
	 */
	var vec2 = {};

	/**
	 * Creates a new, empty vec2
	 *
	 * @returns {vec2} a new 2D vector
	 */
	vec2.create = function() {
	    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
	 */
	vec2.clone = function(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
	 */
	vec2.fromValues = function(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
	 */
	vec2.copy = function(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
	 */
	vec2.set = function(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
	 */
	vec2.add = function(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
	 */
	vec2.subtract = function(out, a, b) {
	    out[0] = a[0] - b[0];
	    out[1] = a[1] - b[1];
	    return out;
	};

	/**
	 * Alias for {@link vec2.subtract}
	 * @function
	 */
	vec2.sub = vec2.subtract;

	/**
	 * 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
	 */
	vec2.multiply = function(out, a, b) {
	    out[0] = a[0] * b[0];
	    out[1] = a[1] * b[1];
	    return out;
	};

	/**
	 * Alias for {@link vec2.multiply}
	 * @function
	 */
	vec2.mul = vec2.multiply;

	/**
	 * 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
	 */
	vec2.divide = function(out, a, b) {
	    out[0] = a[0] / b[0];
	    out[1] = a[1] / b[1];
	    return out;
	};

	/**
	 * Alias for {@link vec2.divide}
	 * @function
	 */
	vec2.div = vec2.divide;

	/**
	 * Math.ceil the components of a vec2
	 *
	 * @param {vec2} out the receiving vector
	 * @param {vec2} a vector to ceil
	 * @returns {vec2} out
	 */
	vec2.ceil = function (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
	 */
	vec2.floor = function (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
	 */
	vec2.min = function(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
	 */
	vec2.max = function(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
	 */
	vec2.round = function (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
	 */
	vec2.scale = function(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
	 */
	vec2.scaleAndAdd = function(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
	 */
	vec2.distance = function(a, b) {
	    var x = b[0] - a[0],
	        y = b[1] - a[1];
	    return Math.sqrt(x*x + y*y);
	};

	/**
	 * Alias for {@link vec2.distance}
	 * @function
	 */
	vec2.dist = vec2.distance;

	/**
	 * 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
	 */
	vec2.squaredDistance = function(a, b) {
	    var x = b[0] - a[0],
	        y = b[1] - a[1];
	    return x*x + y*y;
	};

	/**
	 * Alias for {@link vec2.squaredDistance}
	 * @function
	 */
	vec2.sqrDist = vec2.squaredDistance;

	/**
	 * Calculates the length of a vec2
	 *
	 * @param {vec2} a vector to calculate length of
	 * @returns {Number} length of a
	 */
	vec2.length = function (a) {
	    var x = a[0],
	        y = a[1];
	    return Math.sqrt(x*x + y*y);
	};

	/**
	 * Alias for {@link vec2.length}
	 * @function
	 */
	vec2.len = vec2.length;

	/**
	 * Calculates the squared length of a vec2
	 *
	 * @param {vec2} a vector to calculate squared length of
	 * @returns {Number} squared length of a
	 */
	vec2.squaredLength = function (a) {
	    var x = a[0],
	        y = a[1];
	    return x*x + y*y;
	};

	/**
	 * Alias for {@link vec2.squaredLength}
	 * @function
	 */
	vec2.sqrLen = vec2.squaredLength;

	/**
	 * Negates the components of a vec2
	 *
	 * @param {vec2} out the receiving vector
	 * @param {vec2} a vector to negate
	 * @returns {vec2} out
	 */
	vec2.negate = function(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
	 */
	vec2.inverse = function(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
	 */
	vec2.normalize = function(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
	 */
	vec2.dot = function (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
	 */
	vec2.cross = function(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
	 */
	vec2.lerp = function (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
	 */
	vec2.random = function (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
	 */
	vec2.transformMat2 = function(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
	 */
	vec2.transformMat2d = function(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
	 */
	vec2.transformMat3 = function(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
	 */
	vec2.transformMat4 = function(out, a, m) {
	    var x = a[0], 
	        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;
	};

	/**
	 * 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
	 */
	vec2.forEach = (function() {
	    var vec = vec2.create();

	    return function(a, stride, offset, count, fn, arg) {
	        var i, l;
	        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;
	    };
	})();

	/**
	 * Returns a string representation of a vector
	 *
	 * @param {vec2} a vector to represent as a string
	 * @returns {String} string representation of the vector
	 */
	vec2.str = function (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.
	 */
	vec2.exactEquals = function (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.
	 */
	vec2.equals = function (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)));
	};

	module.exports = vec2;


/***/ }
/******/ ])
});
;
/******************************************************************************
 * Creature Runtimes License
 * 
 * Copyright (c) 2015, Kestrel Moon Studios
 * All rights reserved.
 * 
 * Preamble: This Agreement governs the relationship between Licensee and Kestrel Moon Studios(Hereinafter: Licensor).
 * This Agreement sets the terms, rights, restrictions and obligations on using [Creature Runtimes] (hereinafter: The Software) created and owned by Licensor,
 * as detailed herein:
 * License Grant: Licensor hereby grants Licensee a Sublicensable, Non-assignable & non-transferable, Commercial, Royalty free,
 * Including the rights to create but not distribute derivative works, Non-exclusive license, all with accordance with the terms set forth and
 * other legal restrictions set forth in 3rd party software used while running Software.
 * Limited: Licensee may use Software for the purpose of:
 * Running Software on Licensee’s Website[s] and Server[s];
 * Allowing 3rd Parties to run Software on Licensee’s Website[s] and Server[s];
 * Publishing Software’s output to Licensee and 3rd Parties;
 * Distribute verbatim copies of Software’s output (including compiled binaries);
 * Modify Software to suit Licensee’s needs and specifications.
 * Binary Restricted: Licensee may sublicense Software as a part of a larger work containing more than Software,
 * distributed solely in Object or Binary form under a personal, non-sublicensable, limited license. Such redistribution shall be limited to unlimited codebases.
 * Non Assignable & Non-Transferable: Licensee may not assign or transfer his rights and duties under this license.
 * Commercial, Royalty Free: Licensee may use Software for any purpose, including paid-services, without any royalties
 * Including the Right to Create Derivative Works: Licensee may create derivative works based on Software, 
 * including amending Software’s source code, modifying it, integrating it into a larger work or removing portions of Software, 
 * as long as no distribution of the derivative works is made
 * 
 * THE RUNTIMES 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 RUNTIMES OR THE USE OR OTHER DEALINGS IN THE
 * RUNTIMES.
 *****************************************************************************/


// dualQuat

var Q_X = 0;
var Q_Y = 1;
var Q_Z = 2;
var Q_W = 3;

function dualQuat()
{
	this.real = quat.create();
	this.real[Q_W] = 0;
	
	this.imaginary = quat.create();
	this.imaginary[Q_W] = 0;
	
	this.tmpQ1 = quat.create();
};

dualQuat.prototype.reset = function()
{
  quat.identity(this.real);
  this.real[Q_W] = 0;
  quat.identity(this.imaginary);
  this.imaginary[Q_W] = 0;
  quat.identity(this.tmpQ1);
};

dualQuat.prototype.createFromData = function(q0, t)
{
	this.real = q0;
	this.imaginary = quat.create();
	this.imaginary[Q_W] = -0.5 * ( t[Q_X] * q0[Q_X] + t[Q_Y] * q0[Q_Y] + t[Q_Z] * q0[Q_Z]);
    this.imaginary[Q_X] =  0.5 * ( t[Q_X] * q0[Q_W] + t[Q_Y] * q0[Q_Z] - t[Q_Z] * q0[Q_Y]);
    this.imaginary[Q_Y] =  0.5 * (-t[Q_X] * q0[Q_Z] + t[Q_Y] * q0[Q_W] + t[Q_Z] * q0[Q_X]);
    this.imaginary[Q_Z] =  0.5 * ( t[Q_X] * q0[Q_Y] - t[Q_Y] * q0[Q_X] + t[Q_Z] * q0[Q_W]); 
    
};

dualQuat.prototype.add = function(quat_in, real_factor, imaginary_factor)
{
	//real = real.add((quat_in.real.cpy().mul(real_factor)));
	//var tmpQ = quat.clone(quat_in.real);
	quat.copy(this.tmpQ1, quat_in.real);
	
	quat.scale(this.tmpQ1, this.tmpQ1, real_factor);
	quat.add(this.real, this.tmpQ1, this.real);
	
    //imaginary = imaginary.add(quat_in.imaginary.cpy().mul(imaginary_factor));
    //tmpQ = quat.clone(quat_in.imaginary);
  quat.copy(this.tmpQ1, quat_in.imaginary);
  quat.scale(this.tmpQ1, this.tmpQ1, imaginary_factor);
	quat.add(this.imaginary, this.tmpQ1, this.imaginary);
};

dualQuat.prototype.normalize = function()
{
	var norm = quat.length(this.real);
	
	this.real = quat.scale(this.real, this.real, 1.0 / norm);
	this.imaginary = quat.scale(this.imaginary, this.imaginary, 1.0 / norm);
};

var v0 = vec3.create();
var ve = vec3.create();
var trans = vec3.create();
var tmpVec1 = vec3.create();
var tmpVec2 = vec3.create();
var tmpVec0 = vec3.create();
var aVec = vec3.create();
var rot = vec3.create();

dualQuat.prototype.transform = function(p)
{
        v0[Q_X] = this.real[Q_X]; v0[Q_Y] = this.real[Q_Y]; v0[Q_Z] = this.real[Q_Z];

        ve[Q_X] = this.imaginary[Q_X]; ve[Q_Y] = this.imaginary[Q_Y]; ve[Q_Z] = this.imaginary[Q_Z];

        //trans = (ve*real.w - v0*imaginary.w + Vector3.Cross(v0, ve)) * 2.0f;

//        var tmpVec1 = v0.cpy().scl((float)imaginary.w);
        tmpVec1 = vec3.scale(tmpVec1, v0, this.imaginary[Q_W]);
        
//        var tmpVec2 = v0.cpy().crs(ve);
		tmpVec2 = vec3.cross(tmpVec2, v0, ve);
        
        //var tmpVec0 = ve.cpy().scl(real.w);
        //trans = tmpVec0.sub(tmpVec1).add(tmpVec2);
        //trans.scl(2.0f);
        
        tmpVec0 = vec3.scale(tmpVec0, ve, this.real[Q_W]);
        
        aVec = vec3.subtract(aVec, tmpVec0, tmpVec1);
        trans = vec3.add(trans, aVec, tmpVec2);
        trans = vec3.scale(trans, trans, 2.0);

        //var rot = real.transform(p.cpy());
        rot = vec3.transformQuat(rot, p, this.real);

        //return rot.add(trans);
        rot = vec3.add(rot, rot, trans);
        
        return rot;
};

// Utils
var Utils = {};

Utils.setAxisMatrix = function(xAxis, yAxis, zAxis)
{
	var retMat = mat4.create();
	
	var M00 = 0;
	var M01 = 4;
	var M02 = 8;
	var M03 = 12;
	var M10 = 1;
	var M11 = 5;
	var M12 = 9;
	var M13 = 13;
	var M20 = 2;
	var M21 = 6;
	var M22 = 10;
	var M23 = 14;
	var M30 = 3;
	var M31 = 7;
	var M32 = 11;
	var M33 = 15;
	
	retMat[M00] = xAxis[Q_X];
	retMat[M01] = xAxis[Q_Y];
	retMat[M02] = xAxis[Q_Z];
	retMat[M10] = yAxis[Q_X];
	retMat[M11] = yAxis[Q_Y];
	retMat[M12] = yAxis[Q_Z];
	retMat[M20] = zAxis[Q_X];
	retMat[M21] = zAxis[Q_Y];
	retMat[M22] = zAxis[Q_Z];
	retMat[M03] = 0;
	retMat[M13] = 0;
	retMat[M23] = 0;
	retMat[M30] = 0;
	retMat[M31] = 0;
	retMat[M32] = 0;
	retMat[M33] = 1;
	
	retMat = mat4.transpose(retMat, retMat);
	
	return retMat;
};

Utils.matrixToQuat = function(mat_in)
{
	var retQuat = quat.create();
	var te = mat_in,

    m11 = te[ 0 ], m12 = te[ 4 ], m13 = te[ 8 ],
    m21 = te[ 1 ], m22 = te[ 5 ], m23 = te[ 9 ],
    m31 = te[ 2 ], m32 = te[ 6 ], m33 = te[ 10 ],

    trace = m11 + m22 + m33,
    s;

	if ( trace > 0 ) {

  		s = 0.5 / Math.sqrt( trace + 1.0 );

  		retQuat[Q_W] = 0.25 / s;
  		retQuat[Q_X] = ( m32 - m23 ) * s;
  		retQuat[Q_Y] = ( m13 - m31 ) * s;
  		retQuat[Q_Z] = ( m21 - m12 ) * s;

	} else if ( m11 > m22 && m11 > m33 ) {

  		s = 2.0 * Math.sqrt( 1.0 + m11 - m22 - m33 );

  		retQuat[Q_W] = ( m32 - m23 ) / s;
  		retQuat[Q_X] = 0.25 * s;
  		retQuat[Q_Y] = ( m12 + m21 ) / s;
		retQuat[Q_Z] = ( m13 + m31 ) / s;

	} else if ( m22 > m33 ) {

  		s = 2.0 * Math.sqrt( 1.0 + m22 - m11 - m33 );

	  	retQuat[Q_W] = ( m13 - m31 ) / s;
	  	retQuat[Q_X] = ( m12 + m21 ) / s;
	  	retQuat[Q_Y] = 0.25 * s;
	  	retQuat[Q_Z] = ( m23 + m32 ) / s;

	} else {

  		s = 2.0 * Math.sqrt( 1.0 + m33 - m11 - m22 );

  		retQuat[Q_W] = ( m21 - m12 ) / s;
  		retQuat[Q_X] = ( m13 + m31 ) / s;
  		retQuat[Q_Y] = ( m23 + m32 ) / s;
  		retQuat[Q_Z] = 0.25 * s;

	}	
	
	return retQuat;
};

Utils.rotateVec_90 = function(vec_in)
{
	var ret_vec = vec3.fromValues(-vec_in[Q_Y], vec_in[Q_X], vec_in[Q_Z]);
	
	return ret_vec;
};

Utils.calcRotateMat = function(vec_in)
{
	var dir = vec3.clone(vec_in);
	dir = vec3.normalize(dir, dir);
	
	var pep_dir = Utils.rotateVec_90(dir);
	
	var cur_tangent = vec3.fromValues(dir[Q_X], dir[Q_Y], 0);
	var cur_normal = vec3.fromValues(pep_dir[Q_X], pep_dir[Q_Y], 0);
	var cur_binormal = vec3.fromValues(0, 0, 1);
	
	var cur_rotate = mat4.create();
	cur_rotate = Utils.setAxisMatrix(cur_tangent, cur_normal, cur_binormal);
	
	return cur_rotate;
};

Utils.getMatTranslate = function(mat_in)
{
	var ret_pos = vec3.create();
	ret_pos[Q_X] = mat_in[12];
	ret_pos[Q_Y] = mat_in[13];
	ret_pos[Q_Z] = mat_in[14];
	
	return ret_pos;
};

Utils.addMat = function(mat1, mat2)
{
	var retMat = mat4.create();
	for(var i = 0; i < 16; i++)
	{
		retMat[i] = mat1[i] + mat2[i];
	}
	
	return retMat;
};

Utils.mulMat = function(mat_in, factor)
{
	var retMat = mat4.create();
	for(var i = 0; i < 16; i++)
	{
		retMat[i] = mat_in[i] * factor;	
	}
	
	return retMat;
};

Utils.clamp = function(num, min, max) {
    return num < min ? min : (num > max ? max : num);
};

  var newVec1 = vec3.create();
  var newVec2 = vec3.create();

Utils.vecInterp = function(vec1, vec2, ratio)
{
	newVec1 = vec3.scale(newVec1, vec1, 1.0 - ratio);
	newVec2 = vec3.scale(newVec2, vec2, ratio);
	
	var retVec = vec3.create();
	retVec = vec3.add(retVec, newVec1, newVec2);
	
	return retVec;
};

Utils.vec2Interp = function(vec_1, vec_2, ratio)
{
	var newVec1 = vec2.create();
	var newVec2 = vec2.create();
	
	newVec1 = vec2.scale(newVec1, vec_1, 1.0 - ratio);
	newVec2 = vec2.scale(newVec2, vec_2, ratio);
	
	var retVec = vec2.create();
	retVec = vec2.add(retVec, newVec1, newVec2);
	
	return retVec;
};

Utils.ptsInterp = function(src_pts, target_pts, fraction)
{
    var ret_pts = [];
    for (var i = 0; i < src_pts.length; i++)
    {
        ret_pts.push(Utils.vec2Interp(src_pts[i], target_pts[i], fraction));
    }

    return ret_pts;
};

Utils.scalarInterp = function(src_val, target_val, fraction)
{
    return ((1.0 - fraction) * src_val) + (fraction * target_val);
};

// MeshBone
function MeshBone(key_in, start_pt_in, end_pt_in, parent_transform)
{
	this.key = key_in;
	this.world_rest_angle = 0;
    this.rest_parent_mat = mat4.create();
    this.rest_parent_inv_mat = mat4.create();
    this.rest_world_mat = mat4.create();
    this.rest_world_inv_mat = mat4.create();
    this.bind_world_mat = mat4.create();
    this.bind_world_inv_mat = mat4.create();
    this.parent_world_mat = mat4.create();
    this.parent_world_inv_mat = mat4.create();
    this.local_rest_start_pt = null;
    this.local_rest_end_pt = null;

    this.setRestParentMat(parent_transform, null);
    this.setLocalRestStartPt(start_pt_in);
    this.setLocalRestEndPt(end_pt_in);
    this.setParentWorldInvMat(mat4.create());
    this.setParentWorldMat(mat4.create());
    
    this.local_binormal_dir = vec3.fromValues(0.0,0.0,1.0);
    this.tag_id = 0;
    
    this.children = [];
};

MeshBone.prototype.setRestParentMat = function(transform_in, inverse_in)
{
	this.rest_parent_mat = transform_in;
        if(inverse_in == null) {
            this.rest_parent_inv_mat = mat4.clone(this.rest_parent_mat);
            //rest_parent_inv_mat.inv();
            mat4.invert(this.rest_parent_inv_mat, this.rest_parent_inv_mat);
        }
        else {
            this.rest_parent_inv_mat = mat4.clone(inverse_in);
        }
};

MeshBone.prototype.setParentWorldMat = function(transform_in)
{
  this.parent_world_mat = transform_in;
};

MeshBone.prototype.setParentWorldInvMat = function(transform_in)
{
  this.parent_world_inv_mat = transform_in;
};

MeshBone.prototype.getLocalRestStartPt = function()
{
  return this.local_rest_start_pt;
};

MeshBone.prototype.getLocalRestEndPt = function()
{
  return this.local_rest_end_pt;
};

MeshBone.prototype.setLocalRestStartPt = function(world_pt_in)
{
  //local_rest_start_pt = Vector3.Transform(world_pt_in, rest_parent_inv_mat);
  //this.local_rest_start_pt = world_pt_in.cpy().traMul(rest_parent_inv_mat);
  this.local_rest_start_pt = vec3.create();
  this.local_rest_start_pt = vec3.transformMat4(this.local_rest_start_pt, world_pt_in, this.rest_parent_inv_mat);
  this.calcRestData();
};

MeshBone.prototype.setLocalRestEndPt = function(world_pt_in)
{
  //local_rest_end_pt = Vector3.Transform(world_pt_in, rest_parent_inv_mat);
  //this.local_rest_end_pt = world_pt_in.cpy().traMul(rest_parent_inv_mat);
  this.local_rest_end_pt = vec3.create();
  this.local_rest_end_pt = vec3.transformMat4(this.local_rest_end_pt, world_pt_in, this.rest_parent_inv_mat);
  this.calcRestData();
};

MeshBone.prototype.calcRestData = function()
{
  if(this.local_rest_start_pt == null || this.local_rest_end_pt == null)
  {
    return;
  }

  var calc = this.computeDirs(this.local_rest_start_pt, this.local_rest_end_pt);

  this.local_rest_dir = calc.first;
  this.local_rest_normal_dir = calc.second;

  this.computeRestLength();
};

MeshBone.prototype.setWorldStartPt = function(world_pt_in)
{
  this.world_start_pt = world_pt_in;
};

MeshBone.prototype.setWorldEndPt = function(world_pt_in)
{
  this.world_end_pt = world_pt_in;
};

MeshBone.prototype.fixDQs = function(ref_dq)
{
  //        if( Quaternion.Dot(world_dq.real, ref_dq.real) < 0) {
  //if( world_dq.real.dot(ref_dq.real) < 0) {
  if(quat.dot(this.world_dq.real, ref_dq.real) < 0) {
    //this.world_dq.real = world_dq.real.cpy().mul(-1);
    this.world_dq.real = quat.scale(this.world_dq.real, this.world_dq.real, -1);
    //this.world_dq.imaginary = world_dq.imaginary.cpy().mul(-1);
    this.world_dq.imaginary = quat.scale(this.world_dq.imaginary, this.world_dq.imaginary, -1);
  }

  for(var i = 0; i < this.children.length; i++) {
    var cur_child = this.children[i];
    cur_child.fixDQs(this.world_dq);
  }
};

MeshBone.prototype.initWorldPts = function()
{
  this.setWorldStartPt(this.getWorldRestStartPt());
  this.setWorldEndPt(this.getWorldRestEndPt());

  for(var i = 0; i < this.children.length; i++) {
    this.children[i].initWorldPts();
  }
};

MeshBone.prototype.getWorldRestStartPt = function()
{
  //Vector3 ret_vec = Vector3.Transform(local_rest_start_pt, rest_parent_mat);
  var tmp_mat = this.rest_parent_mat;
  var ret_vec = vec3.create();
  ret_vec = vec3.transformMat4(ret_vec, this.local_rest_start_pt, tmp_mat);

  return ret_vec;
};

MeshBone.prototype.getWorldRestEndPt = function()
{
  //        Vector3 ret_vec = Vector3.Transform(local_rest_end_pt, rest_parent_mat);
  var tmp_mat = this.rest_parent_mat;
  var ret_vec = vec3.create();
  ret_vec = vec3.transformMat4(ret_vec, this.local_rest_end_pt, tmp_mat);

  return ret_vec;
};

MeshBone.prototype.getWorldRestAngle = function()
{
  return this.world_rest_angle;
};

MeshBone.prototype.getWorldRestPos = function()
{
  return this.world_rest_pos;
};

MeshBone.prototype.getWorldStartPt = function()
{
  return this.world_start_pt;
};

MeshBone.prototype.getWorldEndPt = function()
{
  return this.world_end_pt;
};

MeshBone.prototype.getRestParentMat = function()
{
  return this.rest_parent_mat;
};

MeshBone.prototype.getRestWorldMat = function()
{
  return this.rest_world_mat;
};

MeshBone.prototype.getWorldDeltaMat = function()
{
  return this.world_delta_mat;
};

MeshBone.prototype.getParentWorldMat = function()
{
  return this.parent_world_mat;
};

MeshBone.prototype.getParentWorldInvMat = function()
{
  return this.parent_world_inv_mat;
};

MeshBone.prototype.getWorldDq = function()
{
  return this.world_dq;
};

MeshBone.prototype.computeRestParentTransforms = function()
{
  var cur_tangent = vec3.fromValues(this.local_rest_dir[Q_X], this.local_rest_dir[Q_Y], 0);
  var cur_binormal = vec3.fromValues(this.local_binormal_dir[Q_X], this.local_binormal_dir[Q_Y], this.local_binormal_dir[Q_Z]);
  var cur_normal = vec3.fromValues(this.local_rest_normal_dir[Q_X], this.local_rest_normal_dir[Q_Y], 0);

  var cur_translate = mat4.create();
  //cur_translate.setTranslation(local_rest_end_pt.x, local_rest_end_pt.y, 0);
  mat4.translate(cur_translate, cur_translate, this.local_rest_end_pt);

  var cur_rotate = mat4.create();
  /*
     cur_rotate.Right = cur_tangent;
     cur_rotate.Up = cur_normal;
     cur_rotate.Backward = cur_binormal;
   */
  //cur_rotate.set(cur_tangent, cur_normal, cur_binormal, new Vector3(0,0,0));
  cur_rotate = Utils.setAxisMatrix(cur_tangent, cur_normal, cur_binormal);
  //cur_rotate.tra();

  //Matrix4 cur_final = cur_translate.cpy().mul(cur_rotate);
  var cur_final = mat4.create();
  cur_final = mat4.multiply(cur_final, cur_translate, cur_rotate);

  //rest_world_mat = rest_parent_mat.cpy().mul(cur_final);
  this.rest_world_mat = mat4.create();
  this.rest_world_mat = mat4.multiply(this.rest_world_mat, this.rest_parent_mat, cur_final); 

  this.rest_world_inv_mat = mat4.clone(this.rest_world_mat);
  this.rest_world_inv_mat = mat4.invert(this.rest_world_inv_mat, this.rest_world_inv_mat);
  //Matrix4.Invert(ref rest_world_mat, out rest_world_inv_mat);

//  var world_rest_dir = getWorldRestEndPt().cpy().sub( getWorldRestStartPt());
  var world_rest_dir = vec3.clone(this.getWorldRestEndPt());
  world_rest_dir = vec3.subtract(world_rest_dir, world_rest_dir, this.getWorldRestStartPt());
  
  world_rest_dir = vec3.normalize(world_rest_dir, world_rest_dir);
  this.world_rest_pos = this.getWorldRestStartPt();


  var bind_translate = mat4.create();
  //bind_translate.setTranslation(getWorldRestStartPt().x, getWorldRestStartPt().y, 0);
  bind_translate = mat4.translate(bind_translate, bind_translate, this.getWorldRestStartPt());

  var tVec = vec3.create();
  tVec = vec3.sub(tVec, this.getWorldRestEndPt(), this.getWorldRestStartPt());
  var bind_rotate = Utils.calcRotateMat(tVec);
  //Matrix4 cur_bind_final = bind_translate.cpy().mul(bind_rotate);
  var cur_bind_final = mat4.create();
  cur_bind_final = mat4.multiply(cur_bind_final, bind_translate, bind_rotate);

  this.bind_world_mat = mat4.clone(cur_bind_final);
  this.bind_world_inv_mat = mat4.clone(this.bind_world_mat);
  this.bind_world_inv_mat = mat4.invert(this.bind_world_inv_mat, this.bind_world_inv_mat);
  //Matrix4.Invert(ref bind_world_mat, out bind_world_inv_mat);

  for(var i = 0; i < this.children.length; i++) {
    var cur_bone = this.children[i];
    cur_bone.setRestParentMat(this.rest_world_mat, this.rest_world_inv_mat);
    cur_bone.computeRestParentTransforms();
  }
};

MeshBone.prototype.computeParentTransforms = function()
{
  var translate_parent = mat4.create();
  translate_parent = mat4.translate(translate_parent, translate_parent, this.getWorldEndPt());

  var tVec = vec3.create();
  tVec = vec3.subtract(tVec, this.getWorldEndpt(), this.getWorldStartPt());
  var rotate_parent = Utils.calcRotateMat(tVec);

  var final_transform = mat4.create();
  final_transform = mat4.multiply(final_transform, translate_parent, rotate_parent);

  var final_inv_transform = mat4.clone(final_transform);
  //final_inv_transform.inv();
  final_inv_transform = mat4.invert(final_inv_transform, final_inv_transform);

  for(var i = 0; i < children.length; i++) {
    var cur_bone = children[i];
    cur_bone.setParentWorldMat(final_transform);
    cur_bone.setParentWorldInvMat(final_inv_transform);
    cur_bone.computeParentTransforms();
  }
};

MeshBone.prototype.computeWorldDeltaTransforms = function()
{
  var calc = this.computeDirs(this.world_start_pt, this.world_end_pt);
  var cur_tangent = vec3.fromValues(calc["first"][Q_X], calc["first"][Q_Y], 0);
  var cur_normal = vec3.fromValues(calc["second"][Q_X], calc["second"][Q_Y], 0);
  var cur_binormal = vec3.fromValues(this.local_binormal_dir[Q_X], this.local_binormal_dir[Q_Y], this.local_binormal_dir[Q_Z]);

  var cur_rotate = mat4.create();
  /*
     cur_rotate.Right = cur_tangent;
     cur_rotate.Up = cur_normal;
     cur_rotate.Backward = cur_binormal;
   */
  //cur_rotate.set(cur_tangent, cur_normal, cur_binormal, new Vector3(0,0,0));
  cur_rotate = Utils.setAxisMatrix(cur_tangent, cur_normal, cur_binormal);
  
  //cur_rotate.tra();

  var cur_translate = mat4.create();
  //cur_translate.setTranslation(world_start_pt.x, world_start_pt.y, 0);
  cur_translate = mat4.translate(cur_translate, cur_translate, this.world_start_pt);

  /*
     world_delta_mat = (cur_translate * cur_rotate)
   * bind_world_inv_mat;
   */

  this.world_delta_mat = mat4.create();
//  world_delta_mat = (cur_translate.cpy().mul(cur_rotate)).mul(bind_world_inv_mat);
  this.world_delta_mat = mat4.multiply(this.world_delta_mat, cur_translate, cur_rotate);
  this.world_delta_mat = mat4.multiply(this.world_delta_mat, this.world_delta_mat, this.bind_world_inv_mat);


  //        Quaternion cur_quat = XnaGeometry.Quaternion.CreateFromRotationMatrix(world_delta_mat);
  //var tmpMat = mat3.create();
  //tmpMat = mat3.fromMat4(tmpMat, this.world_delta_mat);
  var cur_quat = Utils.matrixToQuat(this.world_delta_mat);


  var tmp_pos =  Utils.getMatTranslate(this.world_delta_mat);
  this.world_dq = new dualQuat();
  this.world_dq.createFromData(cur_quat, tmp_pos);

  for(var i = 0; i < this.children.length; i++) {
    var cur_bone = this.children[i];
    cur_bone.computeWorldDeltaTransforms();
  }
};

MeshBone.prototype.addChild = function(bone_in)
{
  bone_in.setRestParentMat(this.rest_world_mat, this.rest_world_inv_mat);
  this.children.push(bone_in);
};

MeshBone.prototype.getChildren = function() 
{
  return this.children;
};

MeshBone.prototype.hasBone = function(bone_in)
{
  for(var i = 0; i < this.children.length; i++) {
    var cur_bone = this.children[i];
    if(cur_bone == bone_in) {
      return true;
    }
  }

  return false;
};

MeshBone.prototype.getChildByKey = function(search_key)
{
  if(this.key === search_key) {
    return this;
  }

  var ret_data = null;
  for(var i = 0; i < this.children.length; i++) {
    var cur_bone = this.children[i];

    var result = cur_bone.getChildByKey(search_key);
    if(result != null) {
      ret_data = result;
      break;
    }
  }

  return ret_data;
};

MeshBone.prototype.getKey = function()
{
  return this.key;
};

MeshBone.prototype.getAllBoneKeys = function()
{
  var ret_data = [];
  ret_data.push(this.getKey());

  for(var i = 0; i < this.children.length; i++) {
    var append_data = this.children[i].getAllBoneKeys();
    ret_data = ret_data.concat(append_data);
  }

  return ret_data;
};

MeshBone.prototype.getAllChildren = function()
{
  var ret_data = [];
  ret_data.push(this);
  for(var i = 0; i < this.children.length; i++) {
    var append_data = this.children[i].getAllChildren();
    ret_data = ret_data.concat(append_data);
  }

  return ret_data;
};

MeshBone.prototype.getBoneDepth = function(bone_in, depth)
{
  if(bone_in == this) {
    return depth;
  }

  for(var i = 0; i < this.children.length; i++) {
    var cur_bone = this.children[i];
    var ret_val = cur_bone.getBoneDepth(bone_in, depth + 1);
    if(ret_val != -1) {
      return ret_val;
    }
  }

  return -1;
};

MeshBone.prototype.isLeaf = function() 
{
  return this.children.length == 0;
};

MeshBone.prototype.deleteChildren = function()
{
  for(var i = 0; i < this.children.length; i++) {
    var cur_bone = this.children[i];
    cur_bone.deleteChildren();
  }

  this.children = [];
};

MeshBone.prototype.setTagId = function(value_in)
{
  this.tag_id = value_in;
};

MeshBone.prototype.getTagId = function()
{
  return this.tag_id;
};

MeshBone.prototype.computeDirs = function(start_pt, end_pt)
{
  var tangent = vec3.create();
  tangent = vec3.subtract(tangent, end_pt, start_pt);
  tangent = vec3.normalize(tangent, tangent);

  var normal = Utils.rotateVec_90(tangent);

  var retData = {};
  retData["first"] = tangent;
  retData["second"] = normal;
  
  return retData;
};

MeshBone.prototype.computeRestLength = function()
{
  var tmp_dir = vec3.create();
  //Vector3 tmp_dir = local_rest_end_pt.cpy().sub(local_rest_start_pt);
  tmp_dir = vec3.subtract(tmp_dir, this.local_rest_end_pt, this.local_rest_start_pt);
  
  this.rest_length = vec3.length(tmp_dir);
};

// MeshRenderRegion
function MeshRenderRegion(indices_in, rest_pts_in, uvs_in, start_pt_index_in, end_pt_index_in,
									start_index_in, end_index_in)
{
	this.store_indices = indices_in;
	this.store_rest_pts = rest_pts_in;
	this.store_uvs = uvs_in;

	this.use_local_displacements = false;
	this.use_post_displacements = false;
	this.use_uv_warp = false;
	this.uv_warp_local_offset = vec2.fromValues(0,0);
	this.uv_warp_global_offset = vec2.fromValues(0,0);
	this.uv_warp_scale = vec2.fromValues(1,1);
	this.opacity = 100.0;
	this.start_pt_index = start_pt_index_in;
	this.end_pt_index = end_pt_index_in;
	this.start_index = start_index_in;
	this.end_index = end_index_in;
	this.main_bone = null;
	this.local_displacements = [];
	this.post_displacements = [];
	this.uv_warp_ref_uvs = [];
	this.normal_weight_map = {};
	this.fast_normal_weight_map = [];
	this.fast_bones_map = [];
	this.relevant_bones_indices = [];
	this.use_dq = true;
	this.tag_id = -1;

	this.initUvWarp();	
};

MeshRenderRegion.prototype.getIndicesIndex = function()
{
  // return store_indices + (start_index);
  return this.start_index;
};

MeshRenderRegion.prototype.getRestPtsIndex = function()
{
  // return store_rest_pts + (3 * start_pt_index);
  return 3 * this.start_pt_index;
};

MeshRenderRegion.prototype.getUVsIndex = function()
{
  // return store_uvs + (2  * start_pt_index);
  return 2  * this.start_pt_index;
};

MeshRenderRegion.prototype.getNumPts = function()
{
  return this.end_pt_index - this.start_pt_index + 1;
};

MeshRenderRegion.prototype.getStartPtIndex = function()
{
  return this.start_pt_index;
};

MeshRenderRegion.prototype.getEndPtIndex = function()
{
  return this.end_pt_index;
};

MeshRenderRegion.prototype.getNumIndices = function()
{
  return this.end_index - this.start_index + 1;
};

MeshRenderRegion.prototype.getStartIndex = function()
{
  return this.start_index;
};

MeshRenderRegion.prototype.getEndIndex = function()
{
  return this.end_index;
};

var accum_dq = new dualQuat();
var accum_mat = mat4.create();
var final_pt = vec3.create();
var tmp1 = vec3.create();
var tmp2 = vec3.create();

MeshRenderRegion.prototype.poseFinalPts = function(output_pts, output_start_index, bones_map)
{
  var read_pt_index = this.getRestPtsIndex();
  var write_pt_index = output_start_index;

  // point posing
  for(var i = 0; i < 16; i++)
  {
  	accum_mat[i] = 0.0;
  }

  var boneKeys = Object.keys(bones_map);
  var boneKeyLength = boneKeys.length;
  
  for(var i = 0, l = this.getNumPts(); i < l; i++) {
    var cur_rest_pt =
      vec3.set(tmp1, this.store_rest_pts[0 + read_pt_index],
          this.store_rest_pts[1 + read_pt_index],
          this.store_rest_pts[2 + read_pt_index]);
      // vec3.fromValues(this.store_rest_pts[0 + read_pt_index],
      //     this.store_rest_pts[1 + read_pt_index],
      //     this.store_rest_pts[2 + read_pt_index]);

    if(this.use_local_displacements == true) {
      cur_rest_pt[Q_X] += this.local_displacements[i][Q_X];
      cur_rest_pt[Q_Y] += this.local_displacements[i][Q_Y];
    }

  	for(var j = 0; j < 16; j++)
  	{
	  	accum_mat[j] = 0.0;
  	}
    // reuse
    // var accum_dq = new dualQuat();
    accum_dq.reset();

	var curBoneIndices = this.relevant_bones_indices[i];
  	var relevantIndicesLength = curBoneIndices.length;
    for (var j = 0; j < relevantIndicesLength; j++)
    {
      var idx_lookup = curBoneIndices[j];
      var cur_bone = this.fast_bones_map[idx_lookup];
      var cur_weight_val = this.fast_normal_weight_map[idx_lookup][i];
      var cur_im_weight_val = cur_weight_val;

       var world_dq = cur_bone.getWorldDq();
       accum_dq.add(world_dq, cur_weight_val, cur_im_weight_val);
    }

    accum_dq.normalize();
    var tmp_pt = vec3.set(tmp2, cur_rest_pt[Q_X], cur_rest_pt[Q_Y], cur_rest_pt[Q_Z]);
    // var tmp_pt = vec3.fromValues(cur_rest_pt[Q_X], cur_rest_pt[Q_Y], cur_rest_pt[Q_Z]);
    final_pt = accum_dq.transform(tmp_pt);

    // debug start

    // debug end

    if(this.use_post_displacements == true) {
      final_pt[Q_X] += this.post_displacements[i][Q_X];
      final_pt[Q_Y] += this.post_displacements[i][Q_Y];
    }

    output_pts[0 + write_pt_index] = final_pt[Q_X];
    output_pts[1 + write_pt_index] = final_pt[Q_Y];
    output_pts[2 + write_pt_index] = final_pt[Q_Z];



    read_pt_index += 3;
    write_pt_index += 3;
  }

  // uv warping
  if(this.use_uv_warp == true) {
    this.runUvWarp();
  }
};

MeshRenderRegion.prototype.setMainBoneKey = function(key_in)
{
  this.main_bone_key = key_in;
};

MeshRenderRegion.prototype.determineMainBone = function(root_bone_in)
{
  this.main_bone = root_bone_in.getChildByKey(this.main_bone_key);
};

MeshRenderRegion.prototype.setUseDq = function(flag_in)
{
  this.use_dq = flag_in;
};

MeshRenderRegion.prototype.setName = function(name_in)
{
  this.name = name_in;
};

MeshRenderRegion.prototype.getName = function()
{
  return this.name;
};

MeshRenderRegion.prototype.setUseLocalDisplacements = function(flag_in)
{
  this.use_local_displacements = flag_in;
  if((this.local_displacements.length != this.getNumPts())
      && this.use_local_displacements)
  {
    this.local_displacements = [];
    for(var i = 0; i < this.getNumPts(); i++) {
      this.local_displacements.push (vec2.create());
    }
  }
};

MeshRenderRegion.prototype. getUseLocalDisplacements = function()
{
  return this.use_local_displacements;
};

MeshRenderRegion.prototype.setUsePostDisplacements = function(flag_in)
{
  this.use_post_displacements = flag_in;
  if((this.post_displacements.length != this.getNumPts())
      && this.use_post_displacements)
  {
    this.post_displacements = [];
    for(var i = 0; i < this.getNumPts(); i++) {
      this.post_displacements.push(vec2.create());
    }
  }
};

MeshRenderRegion.prototype.getUsePostDisplacements = function()
{
  return this.use_post_displacements;
};

MeshRenderRegion.prototype.getRestLocalPt = function(index_in)
{
  var read_pt_index = this.getRestPtsIndex() + (3 * index_in);
  var return_pt = vec2.fromValues(this.store_rest_pts[0 + read_pt_index],
      this.store_rest_pts[1 + read_pt_index]);
  return return_pt;
};

MeshRenderRegion.prototype.getLocalIndex = function(index_in)
{
  var read_index = this.getIndicesIndex() + index_in;
  return this.store_indices[read_index];
};

MeshRenderRegion.prototype.clearLocalDisplacements = function()
{
  for(var i = 0; i < this.local_displacements.length; i++) {
    this.local_displacements[i] = vec2.create();
  }
};

MeshRenderRegion.prototype.clearPostDisplacements = function()
{
  for(var i = 0; i < this.post_displacements.length; i++) {
    this.post_displacements[i] = vec2.create();
  }
};

MeshRenderRegion.prototype.setUseUvWarp = function(flag_in)
{
  this.use_uv_warp = flag_in;
  if(this.use_uv_warp == false) {
    this.restoreRefUv();
  }
};

MeshRenderRegion.prototype. getUseUvWarp = function()
{
  return this.use_uv_warp;
};

MeshRenderRegion.prototype.setUvWarpLocalOffset = function(vec_in)
{
  this.uv_warp_local_offset = vec_in;
};

MeshRenderRegion.prototype.setUvWarpGlobalOffset = function(vec_in)
{
  this.uv_warp_global_offset = vec_in;
};

MeshRenderRegion.prototype.setUvWarpScale = function(vec_in)
{
  this.uv_warp_scale = vec_in;
};

MeshRenderRegion.prototype. getUvWarpLocalOffset = function()
{
  return this.uv_warp_local_offset;
};

MeshRenderRegion.prototype. getUvWarpGlobalOffset = function()
{
  return this.uv_warp_global_offset;
};

MeshRenderRegion.prototype. getUvWarpScale = function()
{
  return this.uv_warp_scale;
};

MeshRenderRegion.prototype.runUvWarp = function()
{
  var cur_uvs_index = this.getUVsIndex();
  for(var i = 0; i < this.uv_warp_ref_uvs.length; i++) {
    var set_uv = vec2.clone(this.uv_warp_ref_uvs[i]);
    
   
    set_uv = vec2.subtract(set_uv, set_uv, this.uv_warp_local_offset);
    set_uv[Q_X] *= this.uv_warp_scale[Q_X];
    set_uv[Q_Y] *= this.uv_warp_scale[Q_Y];
    set_uv = vec2.add(set_uv, set_uv, this.uv_warp_global_offset);
    
   
    /*
    set_uv.sub(uv_warp_local_offset);
    set_uv.scl(uv_warp_scale);
    set_uv.add(uv_warp_global_offset);
    */


    this.store_uvs[0 + cur_uvs_index] = set_uv[Q_X];
    this.store_uvs[1 + cur_uvs_index] = set_uv[Q_Y];


    cur_uvs_index += 2;
  }
};

MeshRenderRegion.prototype.restoreRefUv = function()
{
  var cur_uvs_index = this.getUVsIndex();
  for(var i = 0; i < this.uv_warp_ref_uvs.length; i++) {
    var set_uv = this.uv_warp_ref_uvs[i];

    this.store_uvs[0 + cur_uvs_index] = set_uv[Q_X];
    this.store_uvs[1 + cur_uvs_index] = set_uv[Q_Y];

    cur_uvs_index += 2;
  }
};

MeshRenderRegion.prototype.getTagId = function()
{
  return this.tag_id;
};

MeshRenderRegion.prototype.setTagId = function(value_in)
{
  this.tag_id = value_in;
};

MeshRenderRegion.prototype.initFastNormalWeightMap = function(bones_map)
{
  this.relevant_bones_indices = [];
  
  // fast normal weight map lookup, avoids hash lookups
  for (var cur_key in bones_map) {
    var values = this.normal_weight_map[cur_key];
    this.fast_normal_weight_map.push(values);
  }
  
  // relevant bone indices
  var cutoff_val = 0.05;
   for(var i = 0; i < this.getNumPts(); i++) {
  	var curIndicesArray = [];
   	for (var j = 0; j < this.fast_normal_weight_map.length; j++)
  	{
  		var cur_val = this.fast_normal_weight_map[j][i];
  		if(cur_val > cutoff_val)
  		{
  			curIndicesArray.push(j);
  		}  		
  	}
  	
  	this.relevant_bones_indices.push(curIndicesArray);
  }
  
  // fast bone map lookup
    for (var cur_key in bones_map) {
    	var cur_bone = bones_map[cur_key];
    	this.fast_bones_map.push(cur_bone);
    }
};

MeshRenderRegion.prototype.initUvWarp = function()
{
  var cur_uvs_index = this.getUVsIndex();
  //        uv_warp_ref_uvs = new java.util.Vector<Vector2>(new Vector2[getNumPts()]);
  this.uv_warp_ref_uvs = [];;

  for(var i = 0; i < this.getNumPts(); i++) {
    this.uv_warp_ref_uvs.push(vec2.create());
    
    this.uv_warp_ref_uvs[i] = vec2.fromValues(this.store_uvs[cur_uvs_index],
    										this.store_uvs[cur_uvs_index + 1]);
     


    cur_uvs_index += 2;
  }
};


// MeshRenderBoneComposition
function MeshRenderBoneComposition()
{
	this.root_bone = null;
    this.bones_map = {};
    this.regions = [];
    this.regions_map = {};
};

MeshRenderBoneComposition.prototype.addRegion = function(region_in)
{
  this.regions.push(region_in);
};

MeshRenderBoneComposition.prototype.setRootBone = function(root_bone_in)
{
  this.root_bone = root_bone_in;
};

MeshRenderBoneComposition.prototype.getRootBone = function()
{
  return this.root_bone;
};

MeshRenderBoneComposition.prototype.initBoneMap = function()
{
  this.bones_map = MeshRenderBoneComposition.genBoneMap(this.root_bone);
};

MeshRenderBoneComposition.prototype.initRegionsMap = function()
{
  this.regions_map = {};
  for(var i = 0; i < this.regions.length; i++) {
    cur_key = this.regions[i].getName();
    this.regions_map[cur_key] = this.regions[i];
  }
};

MeshRenderBoneComposition.genBoneMap = function(input_bone)
{
  var ret_map = {};
  var all_keys = input_bone.getAllBoneKeys();
  for(var i = 0; i < all_keys.length; i++) {
    var cur_key = all_keys[i];
    ret_map[cur_key] = input_bone.getChildByKey(cur_key);
  }

  return ret_map;
};

MeshRenderBoneComposition.prototype.getBonesMap = function()
{
  return this.bones_map;
};

MeshRenderBoneComposition.prototype.getRegionsMap = function()
{
  return this.regions_map;
};

MeshRenderBoneComposition.prototype.getRegions = function()
{
  return this.regions;
};

MeshRenderBoneComposition.prototype.getRegionWithId = function(id_in)
{
  for(var i = 0; i < this.regions.length; i++) {
    var cur_region = this.regions[i];
    if(cur_region.getTagId() == id_in) {
      return cur_region;
    }
  }

  return null;
};

MeshRenderBoneComposition.prototype.resetToWorldRestPts = function()
{
  this.getRootBone().initWorldPts();
};

MeshRenderBoneComposition.prototype.updateAllTransforms = function(update_parent_xf)
{
  if(update_parent_xf) {
    this.getRootBone().computeParentTransforms();
  }

  this.getRootBone().computeWorldDeltaTransforms();
  this.getRootBone().fixDQs(this.getRootBone().getWorldDq());
};

// MeshBoneCache
function MeshBoneCache(key_in)
{
	this.key = key_in;
};

MeshBoneCache.prototype.setWorldStartPt = function(pt_in) {
  this.world_start_pt = pt_in;
};

MeshBoneCache.prototype.setWorldEndPt = function(pt_in) {
  this.world_end_pt = pt_in;
};

MeshBoneCache.prototype.getWorldStartPt = function() {
  return this.world_start_pt;
};

MeshBoneCache.prototype.getWorldEndPt = function() {
  return this.world_end_pt;
};

MeshBoneCache.prototype.getKey = function() {
  return this.key;
};

// MeshDisplacementCache
function MeshDisplacementCache(key_in)
{
	this.key = key_in;
	this.local_displacements = [];
	this.post_displacements = [];
};

MeshDisplacementCache.prototype.setLocalDisplacements = function(displacements_in)
{
  this.local_displacements = displacements_in;
};

MeshDisplacementCache.prototype.setPostDisplacements = function(displacements_in)
{
  this.post_displacements = displacements_in;
};

MeshDisplacementCache.prototype.getKey = function() {
  return this.key;
};

MeshDisplacementCache.prototype.getLocalDisplacements = function()
{
  return this.local_displacements;
};

MeshDisplacementCache.prototype.getPostDisplacements = function()
{
  return this.post_displacements;
};


// MeshUVWarpCache
function MeshUVWarpCache(key_in)
{
	this.uv_warp_global_offset = vec2.create();
    this.uv_warp_local_offset = vec2.create();
    this.uv_warp_scale = vec2.fromValues(-1,-1);
    this.key = key_in;
    this.enabled = false;
};

MeshUVWarpCache.prototype.setUvWarpLocalOffset = function(vec_in)
{
  this.uv_warp_local_offset = vec_in;
};

MeshUVWarpCache.prototype.setUvWarpGlobalOffset = function(vec_in)
{
  this.uv_warp_global_offset = vec_in;
};

MeshUVWarpCache.prototype.setUvWarpScale = function(vec_in)
{
  this.uv_warp_scale = vec_in;
};

MeshUVWarpCache.prototype.getUvWarpLocalOffset = function()
{
  return this.uv_warp_local_offset;
};

MeshUVWarpCache.prototype.getUvWarpGlobalOffset = function()
{
  return this.uv_warp_global_offset;
};

MeshUVWarpCache.prototype.getUvWarpScale = function()
{
  return this.uv_warp_scale;
};

MeshUVWarpCache.prototype.getKey = function() {
  return this.key;
};

MeshUVWarpCache.prototype.setEnabled = function(flag_in)
{
  this.enabled = flag_in;
};

MeshUVWarpCache.prototype.getEnabled = function() {
  return this.enabled;
};

// MeshOpacityCache
function MeshOpacityCache(key_in)
{
  this.opacity = 100.0;
  this.key = key_in;
};

MeshOpacityCache.prototype.setOpacity = function(value_in)
{
  this.opacity = value_in;
};

MeshOpacityCache.prototype.getOpacity = function()
{
  return this.opacity;
};

MeshOpacityCache.prototype.getKey = function() {
  return this.key;
};

// MeshBoneCacheManager
function MeshBoneCacheManager()
{
	this.is_ready = false;
	this.bone_cache_table = null;
	this.bone_cache_data_ready = null;
	this.bone_cache_table = [];
	this.bone_cache_data_ready = [];
};

MeshBoneCacheManager.prototype.init = function(start_time_in, end_time_in)
{
  this.start_time = start_time_in;
  this.end_time = end_time_in;

  var num_frames = this.end_time - this.start_time + 1;
  this.bone_cache_table = [];

  this.bone_cache_data_ready = [];
  for(var i = 0; i < num_frames; i++) {
    this.bone_cache_table.push([]);
    this.bone_cache_data_ready.push(false);
  }

  this.is_ready = false;
};

MeshBoneCacheManager.prototype.getStartTime = function()
{
  return this.start_time;
};

MeshBoneCacheManager.prototype.getEndime = function()
{
  return this.end_time;
};

MeshBoneCacheManager.prototype.getIndexByTime = function(time_in)
{
  var retval = time_in - this.start_time;
  retval = Utils.clamp(retval, 0, (this.bone_cache_table.length) - 1);

  return retval;
};

MeshBoneCacheManager.prototype.retrieveValuesAtTime = function(time_in, bone_map)
{
  var base_time = this.getIndexByTime(Math.floor(time_in));
  var end_time = this.getIndexByTime(Math.ceil(time_in));

  var ratio = (time_in - Math.floor(time_in));

  if(this.bone_cache_data_ready.length == 0) {
    return;
  }

  if((this.bone_cache_data_ready[base_time] == false)
      || ((this.bone_cache_data_ready[end_time] == false)))
  {
    return;
  }

  var base_cache = this.bone_cache_table[base_time];
  var end_cache = this.bone_cache_table[end_time];

  for(var i = 0, l = base_cache.length; i < l; i++) {
    var base_data = base_cache[i];
    var end_data = end_cache[i];
    var cur_key = base_data.getKey();

    var final_world_start_pt = Utils.vecInterp(base_data.getWorldStartPt(), end_data.getWorldStartPt(), ratio);

    var final_world_end_pt = Utils.vecInterp(base_data.getWorldEndPt(), end_data.getWorldEndPt(), ratio);

    /*
       Vector3 final_world_start_pt = ((1.0f - ratio) * base_data.getWorldStartPt()) +
       (ratio * end_data.getWorldStartPt());

       Vector3 final_world_end_pt = ((1.0f - ratio) * base_data.getWorldEndPt()) +
       (ratio * end_data.getWorldEndPt());
     */

    bone_map[cur_key].setWorldStartPt(final_world_start_pt);
    bone_map[cur_key].setWorldEndPt(final_world_end_pt);
  }
};

MeshBoneCacheManager.prototype.allReady = function()
{
  if(this.is_ready) {
    return true;
  }
  else {
    var num_frames = this.end_time - this.start_time + 1;
    var ready_cnt = 0;
    for(var i = 0; i < this.bone_cache_data_ready.size(); i++) {
      if(this.bone_cache_data_ready[i]) {
        ready_cnt++;
      }
    }

    if(ready_cnt == num_frames) {
      this.is_ready = true;
    }
  }

  return this.is_ready;
};

MeshBoneCacheManager.prototype.makeAllReady = function()
{
  for(var i = 0; i < this.bone_cache_data_ready.length; i++) {
    this.bone_cache_data_ready[i] = true;
  }
};

// MeshDisplacementCacheManager
function MeshDisplacementCacheManager()
{
	this.is_ready = false;
    this.displacement_cache_table = null;
    this.displacement_cache_data_ready = null;
    this.displacement_cache_table = [];
    this.displacement_cache_data_ready = [];
};

MeshDisplacementCacheManager.prototype.init = function(start_time_in, end_time_in)
{
  this.start_time = start_time_in;
  this.end_time = end_time_in;

  var num_frames = this.end_time - this.start_time + 1;
  this.displacement_cache_table = [];

  this.displacement_cache_data_ready = [];
  for(var i = 0; i < num_frames; i++) {
    this.displacement_cache_table.push([]);
    this.displacement_cache_data_ready.push(false);
  }

  this.is_ready = false;
};

MeshDisplacementCacheManager.prototype.getStartTime = function()
{
  return this.start_time;
};

MeshDisplacementCacheManager.prototype.getEndime = function()
{
  return this.end_time;
};

MeshDisplacementCacheManager.prototype.getIndexByTime = function(time_in)
{
  var retval = time_in - this.start_time;
  retval = Utils.clamp(retval, 0, (this.displacement_cache_table.length) - 1);

  return retval;
};

MeshDisplacementCacheManager.prototype.retrieveValuesAtTime = function(time_in, regions_map)
{
  var base_time = this.getIndexByTime(Math.floor(time_in));
  var end_time = this.getIndexByTime(Math.ceil(time_in));

  var ratio = (time_in - Math.floor(time_in));

  if(this.displacement_cache_data_ready.length == 0) {
    return;
  }

  if((this.displacement_cache_data_ready[base_time] == false)
      || (this.displacement_cache_data_ready[end_time] == false))
  {
    return;
  }

  var base_cache = this.displacement_cache_table[base_time];
  var end_cache = this.displacement_cache_table[end_time];

  for(var i = 0; i < base_cache.length; i++) {
    var base_data = base_cache[i];
    var end_data = end_cache[i];
    var cur_key = base_data.getKey();

    var set_region = regions_map[cur_key];

    if(set_region.getUseLocalDisplacements()) {
      var displacements =
        set_region.local_displacements;
      if((base_data.getLocalDisplacements().length == displacements.length)
          && (end_data.getLocalDisplacements().length == displacements.length))
      {
        for(var j = 0; j < displacements.length; j++) {
          var interp_val = Utils.vec2Interp(base_data.getLocalDisplacements()[j], 
          									end_data.getLocalDisplacements()[j],
          									ratio);
                
          /*
             Vector2 interp_val =
             ((1.0f - ratio) * base_data.getLocalDisplacements().get(j)) +
             (ratio * end_data.getLocalDisplacements().get(j));
           */

          displacements[j] = interp_val;
        }
      }
      else {
        for(var j = 0; j < displacements.length; j++) {
          displacements[j] = vec2.create();
        }
      }
    }

    if(set_region.getUsePostDisplacements()) {
      var displacements =
        set_region.post_displacements;
      if((base_data.getPostDisplacements().length == displacements.length)
          && (end_data.getPostDisplacements().length == displacements.length))
      {

        for(var j = 0; j < displacements.length; j++) {
          var interp_val = Utils.vec2Interp(base_data.getPostDisplacements()[j], 
          									end_data.getPostDisplacements()[j],
          									ratio);                
                
          /*
             Vector2 interp_val =
             ((1.0f - ratio) * base_data.getPostDisplacements()[j]) +
             (ratio * end_data.getPostDisplacements()[j]);
           */
          displacements[j] = interp_val;
        }
      }
      else {
        for(var j = 0; j < displacements.length; j++) {
          displacements[j] = vec2.create();
        }
      }
    }
  }
};

MeshDisplacementCacheManager.prototype.allReady = function()
{
  if(this.is_ready) {
    return true;
  }
  else {
    var num_frames = this.end_time - this.start_time + 1;
    var ready_cnt = 0;
    for(var i = 0; i < this.displacement_cache_data_ready.length; i++) {
      if(this.displacement_cache_data_ready[i]) {
        ready_cnt++;
      }
    }

    if(ready_cnt == num_frames) {
      this.is_ready = true;
    }
  }

  return this.is_ready;
};

MeshDisplacementCacheManager.prototype.makeAllReady = function()
{
  for(var i = 0; i < this.displacement_cache_data_ready.length; i++) {
    this.displacement_cache_data_ready[i] = true;
  }
};

// MeshUVWarpCacheManager
function MeshUVWarpCacheManager()
{
	this.is_ready = false;
    this.uv_cache_table = null;
    this.uv_cache_data_ready = null;
    this.uv_cache_table = [];
    this.uv_cache_data_ready = [];	
};

MeshUVWarpCacheManager.prototype.init = function(start_time_in, end_time_in)
{
  this.start_time = start_time_in;
  this.end_time = end_time_in;

  var num_frames = this.end_time - this.start_time + 1;
  this.uv_cache_table = [];

  this.uv_cache_data_ready = [];
  for(var i = 0; i < num_frames; i++) {
    this.uv_cache_table.push([]);
    this.uv_cache_data_ready.push(false);
  }

  this.is_ready = false;
};

MeshUVWarpCacheManager.prototype.getStartTime = function()
{
  return this.start_time;
};

MeshUVWarpCacheManager.prototype.getEndime = function()
{
  return this.end_time;
};

MeshUVWarpCacheManager.prototype.getIndexByTime = function(time_in)
{
  var retval = time_in - this.start_time;
  retval = Utils.clamp(retval, 0, (this.uv_cache_table.length) - 1);

  return retval;
};

MeshUVWarpCacheManager.prototype.retrieveValuesAtTime = function(time_in, regions_map)
{
  var base_time = this.getIndexByTime(Math.floor(time_in));
  var end_time = this.getIndexByTime(Math.ceil(time_in));

  var ratio = (time_in - Math.floor(time_in));

  if(this.uv_cache_data_ready.length == 0) {
    return;
  }

  if((this.uv_cache_data_ready[base_time] == false)
      || (this.uv_cache_data_ready[end_time] == false))
  {
    return;
  }

  var base_cache = this.uv_cache_table[base_time];
  var end_cache = this.uv_cache_table[end_time];

  for(var i = 0; i < base_cache.length; i++) {
    var base_data = base_cache[i];
    var end_data = end_cache[i];
    var cur_key = base_data.getKey();

    var set_region = regions_map[cur_key];
    if(set_region.getUseUvWarp()) {
      var final_local_offset = base_data.getUvWarpLocalOffset();
      
 
      var final_global_offset = base_data.getUvWarpGlobalOffset();

      var final_scale = base_data.getUvWarpScale();
      /*
         Vector2 final_local_offset = ((1.0f - ratio) * base_data.getUvWarpLocalOffset()) +
         (ratio * end_data.getUvWarpLocalOffset());

         Vector2 final_global_offset = ((1.0f - ratio) * base_data.getUvWarpGlobalOffset()) +
         (ratio * end_data.getUvWarpGlobalOffset());

         Vector2 final_scale = ((1.0f - ratio) * base_data.getUvWarpScale()) +
         (ratio * end_data.getUvWarpScale());

       */


      set_region.setUvWarpLocalOffset(final_local_offset);
      set_region.setUvWarpGlobalOffset(final_global_offset);
      set_region.setUvWarpScale(final_scale);
    }
  }
};

MeshUVWarpCacheManager.prototype.allReady = function()
{
  if(this.is_ready) {
    return true;
  }
  else {
    var num_frames = this.end_time - this.start_time + 1;
    var ready_cnt = 0;
    for(var i = 0; i < this.uv_cache_data_ready.length; i++) {
      if(uv_cache_data_ready[i]) {
        ready_cnt++;
      }
    }

    if(ready_cnt == num_frames) {
      this.is_ready = true;
    }
  }

  return this.is_ready;
};

MeshUVWarpCacheManager.prototype.makeAllReady = function()
{
  for(var i = 0; i < this.uv_cache_data_ready.length; i++) {
    this.uv_cache_data_ready[i] = true;
  }
};

// MeshOpacityCacheManager
function MeshOpacityCacheManager()
{
  this.is_ready = false;
  this.opacity_cache_table = null;
  this.opacity_cache_data_ready = null;
  this.opacity_cache_table = [];
  this.opacity_cache_data_ready = [];	
};

MeshOpacityCacheManager.prototype.init = function(start_time_in, end_time_in)
{
  this.start_time = start_time_in;
  this.end_time = end_time_in;

  var num_frames = this.end_time - this.start_time + 1;
  this.opacity_cache_table = [];

  this.opacity_cache_data_ready = [];
  for(var i = 0; i < num_frames; i++) {
    this.opacity_cache_table.push([]);
    this.opacity_cache_data_ready.push(false);
  }

  this.is_ready = false;
};

MeshOpacityCacheManager.prototype.getStartTime = function()
{
  return this.start_time;
};

MeshOpacityCacheManager.prototype.getEndime = function()
{
  return this.end_time;
};

MeshOpacityCacheManager.prototype.getIndexByTime = function(time_in)
{
  var retval = time_in - this.start_time;
  retval = Utils.clamp(retval, 0, (this.opacity_cache_table.length) - 1);

  return retval;
};

MeshOpacityCacheManager.prototype.retrieveValuesAtTime = function(time_in, regions_map)
{
  var base_time = this.getIndexByTime(Math.floor(time_in));
  var end_time = this.getIndexByTime(Math.ceil(time_in));

  var ratio = (time_in - Math.floor(time_in));

  if(this.opacity_cache_data_ready.length == 0) {
    return;
  }

  if((this.opacity_cache_data_ready[base_time] == false)
      || (this.opacity_cache_data_ready[end_time] == false))
  {
    return;
  }

  var base_cache = this.opacity_cache_table[base_time];

  for(var i = 0; i < base_cache.length; i++) {
    var base_data = base_cache[i];
    var cur_key = base_data.getKey();

    var set_region = regions_map[cur_key];
    set_region.opacity = base_data.getOpacity();
  }
};

MeshOpacityCacheManager.prototype.allReady = function()
{
  if(this.is_ready) {
    return true;
  }
  else {
    var num_frames = this.end_time - this.start_time + 1;
    var ready_cnt = 0;
    for(var i = 0; i < this.opacity_cache_data_ready.length; i++) {
      if(opacity_cache_data_ready[i]) {
        ready_cnt++;
      }
    }

    if(ready_cnt == num_frames) {
      this.is_ready = true;
    }
  }

  return this.is_ready;
};

MeshOpacityCacheManager.prototype.makeAllReady = function()
{
  for(var i = 0; i < this.opacity_cache_data_ready.length; i++) {
    this.opacity_cache_data_ready[i] = true;
  }
};

// CreatureModuleUtils
var CreatureModuleUtils = {};

CreatureModuleUtils.LoadCreatureFlatData = function(input_bytes)
{
	var buf = new flatbuffers.ByteBuffer(input_bytes);
	return CreatureFlatData.rootData.getRootAsrootData(buf);
};

CreatureModuleUtils.BuildCreatureMetaData = function(json_data)
{
  var meta_data = new CreatureMetaData();
  // Skin Swap
  if("skinSwapList" in json_data)
  {
    var skin_swap_obj = json_data["skinSwapList"];
    for(var swap_name in skin_swap_obj)
    {
      var swap_data = skin_swap_obj[swap_name]["swap"];
      var swap_items = swap_data["swap_items"];
      var swap_set = {};
      for(var i = 0; i < swap_items.length; i++)
      {
        var cur_item = swap_items[i];
        swap_set[cur_item] = cur_item;
      }

      meta_data.skin_swaps[swap_name] = swap_set;
    }
  }

  // Events Triggers
  if("eventTriggers" in json_data)
  {
    var events_obj = json_data["eventTriggers"];
    for (var cur_anim_name in events_obj)
    {
        var cur_events_map = {};
        var cur_obj_array = events_obj[cur_anim_name];

        for (var cur_events_obj in cur_obj_array)
        {
            var cur_event_name = cur_events_obj["event_name"];
            var switch_time = Number(cur_events_obj["switch_time"]);

            cur_events_map[switch_time] = cur_event_name;
        }

        meta_data.anim_events_map[cur_anim_name] = cur_events_map;
    }
  }  

  return meta_data;
};

CreatureModuleUtils.GetAllAnimationNames = function(json_data)
{
  var json_animations = json_data["animation"];
  var keys = [];
  for (var name in json_animations)
  {
    keys.push(name);
  }

  return keys;
};

CreatureModuleUtils.getFloatArray = function(raw_data)
{
  return raw_data;
};

CreatureModuleUtils.getIntArray = function(raw_data)
{
  return raw_data;
};


CreatureModuleUtils.ReadPointsArray2DJSON = function(data, key)
{
  var raw_array = CreatureModuleUtils.getFloatArray(data[key]);
  var ret_list = [];
  var num_points = raw_array.length / 2;
  for (var i = 0; i < num_points; i++)
  {
    var cur_index = i * 2;
    ret_list.push(
        vec2.fromValues(raw_array[0 + cur_index], raw_array[1 + cur_index]));
  }

  return ret_list;
};

CreatureModuleUtils.ReadPointsArray2DFlat = function(data)
{
  var raw_array = data;
  var ret_list = [];
  var num_points = raw_array.length / 2;
  for (var i = 0; i < num_points; i++)
  {
    var cur_index = i * 2;
    ret_list.push(
        vec2.fromValues(raw_array[0 + cur_index], raw_array[1 + cur_index]));
  }

  return ret_list;
};

CreatureModuleUtils.ReadFloatArray3DJSON = function(data, key)
{
  var raw_array = CreatureModuleUtils.getFloatArray(data[key]);

  var ret_list = [];
  var num_points = raw_array.length / 2;
  for (var i = 0; i < num_points; i++)
  {
    var cur_index = i * 2;
    ret_list.push(raw_array[0 + cur_index]);
    ret_list.push(raw_array[1 + cur_index]);
    ret_list.push(0);
  }

  return ret_list;
};

CreatureModuleUtils.ReadFloatArray3DFlat = function(data)
{
  var raw_array = data;

  var ret_list = [];
  var num_points = raw_array.length / 2;
  for (var i = 0; i < num_points; i++)
  {
    var cur_index = i * 2;
    ret_list.push(raw_array[0 + cur_index]);
    ret_list.push(raw_array[1 + cur_index]);
    ret_list.push(0);
  }

  return ret_list;
};

CreatureModuleUtils.ReadBoolJSON = function(data, key)
{
  var val = data[key];
  return val;
};

CreatureModuleUtils.ReadFloatArrayJSON = function(data, key)
{
  /*
  var raw_array = getFloatArray(data.get[key]);
  var ret_list = [];
  for(var i = 0; i < raw_array.length; i++)
  {
    ret_list.push(raw_array[i]);
  }

  return ret_list;
  */
 
  return data[key];
};

CreatureModuleUtils.ReadIntArrayJSON = function(data, key)
{
  /*
  int[] raw_array = getIntArray (data.get(key));
  java.util.Vector<Integer> ret_list = new java.util.Vector<Integer>();

  for(int i = 0; i < raw_array.length; i++) {
    ret_list.add(raw_array[i]);
  }

  return ret_list;
  */
   return data[key];
};

CreatureModuleUtils.ReadMatrixJSON = function(data, key)
{
  var raw_array = CreatureModuleUtils.getFloatArray(data[key]);
  var retMat = mat4.create();
  for(var i = 0; i < 16; i++)
  {
  	retMat[i] = raw_array[i];
  }
  
  return retMat;
};

CreatureModuleUtils.ReadMatrixFlat = function(data)
{
  var raw_array = data;
  var retMat = mat4.create();
  for(var i = 0; i < 16; i++)
  {
  	retMat[i] = raw_array[i];
  }
  
  return retMat;
};

CreatureModuleUtils.ReadVector2JSON = function(data, key)
{
  var raw_array = CreatureModuleUtils.getFloatArray(data[key]);
  return vec2.fromValues(raw_array[0], raw_array[1]);
};

CreatureModuleUtils.ReadVector2Flat = function(data)
{
  var raw_array = data;
  return vec2.fromValues(raw_array[0], raw_array[1]);
};

CreatureModuleUtils.ReadVector3JSON = function(data, key)
{
  var raw_array = CreatureModuleUtils.getFloatArray(data[key]);
  return vec3.fromValues(raw_array[0], raw_array[1], 0);
};

CreatureModuleUtils.ReadVectorFlat = function(data)
{
  var raw_array = data;
  return vec3.fromValues(raw_array[0], raw_array[1], 0);
};

CreatureModuleUtils.FormBoneHierarchy = function(child_set, bone_data)
{
  var root_bone = null;
  // Find root
  for(var cur_id in bone_data)
  {
    if( (cur_id in child_set) == false) {
      // not a child, so is root
	  var cur_data = bone_data[cur_id]; 
      root_bone = cur_data.first;
      break;
    }
  }

  // construct hierarchy
  for(var cur_id in bone_data)
  {
 	var cur_data = bone_data[cur_id]; 

    var cur_bone = cur_data.first;
    var children_ids = cur_data.second;
    for(var i = 0; i < children_ids.length; i++)
    {
      var cur_child_id = children_ids[i];
      var child_bone = bone_data[cur_child_id].first;
      cur_bone.addChild(child_bone);
    }

  }

  return root_bone;
};

CreatureModuleUtils.CreateBones = function(json_obj, key) {
  var root_bone = null;
  var base_obj = json_obj[key];
  //var bone_data = new HashMap<Integer, Tuple<MeshBone, Vector<Integer>>>();
  var bone_data = {};
  var child_set = {};

  // layout bones
  for (var cur_name in base_obj)
  {
  	
    var cur_node = base_obj[cur_name];

    var cur_id = cur_node["id"]; //GetJSONNodeFromKey(*cur_node, "id")->value.toNumber();
    var cur_parent_mat = CreatureModuleUtils.ReadMatrixJSON(cur_node, "restParentMat");

    var cur_local_rest_start_pt = CreatureModuleUtils.ReadVector3JSON(cur_node, "localRestStartPt");
    var cur_local_rest_end_pt = CreatureModuleUtils.ReadVector3JSON(cur_node, "localRestEndPt");
    var cur_children_ids = CreatureModuleUtils.ReadIntArrayJSON(cur_node, "children");

    var new_bone = new MeshBone(cur_name,
        vec3.create(),
        vec3.create(),
        cur_parent_mat);
    new_bone.local_rest_start_pt = cur_local_rest_start_pt;
    new_bone.local_rest_end_pt = cur_local_rest_end_pt;
    new_bone.calcRestData();
    new_bone.setTagId(cur_id);

    bone_data[cur_id] = {first:new_bone, second:cur_children_ids};

    for(var i = 0; i < cur_children_ids.length; i++){
      var cur_child_id = cur_children_ids[i];
      child_set[cur_child_id] = cur_child_id;
    }
  }

  // Find root
  return this.FormBoneHierarchy(child_set, bone_data);
};

CreatureModuleUtils.CreateBonesFlat = function(skelIn) {
  var root_bone = null;
  //var bone_data = new HashMap<Integer, Tuple<MeshBone, Vector<Integer>>>();
  var bone_data = {};
  var child_set = {};

  // layout bones
  for (var i = 0; i < skelIn.bonesLength(); i++)
  {
    var cur_node = skelIn.bones(i);
    var cur_name = cur_node.name();

    var cur_id = cur_node.id(); //GetJSONNodeFromKey(*cur_node, "id")->value.toNumber();
    var cur_parent_mat = CreatureModuleUtils.ReadMatrixFlat(cur_node.restParentMatArray());

    var cur_local_rest_start_pt = CreatureModuleUtils.ReadFloatArray3DFlat(cur_node.localRestStartPtArray());
    var cur_local_rest_end_pt = CreatureModuleUtils.ReadFloatArray3DFlat(cur_node.localRestEndPtArray());
    var cur_children_ids = CreatureModuleUtils.getIntArray(cur_node.childrenArray());

    var new_bone = new MeshBone(cur_name,
        vec3.create(),
        vec3.create(),
        cur_parent_mat);
    new_bone.local_rest_start_pt = cur_local_rest_start_pt;
    new_bone.local_rest_end_pt = cur_local_rest_end_pt;
    new_bone.calcRestData();
    new_bone.setTagId(cur_id);

    bone_data[cur_id] = {first:new_bone, second:cur_children_ids};

    for(var j = 0; j < cur_children_ids.length; j++){
      var cur_child_id = cur_children_ids[j];
      child_set[cur_child_id] = cur_child_id;
    }
  }

  // Find root
  return this.FormBoneHierarchy(child_set, bone_data);
};

CreatureModuleUtils.CreateRegions = function(json_obj, key, indices_in, rest_pts_in, uvs_in)
{
  var ret_regions = [];
  var base_obj = json_obj[key];

  for (var cur_name in base_obj)
  {
  	var cur_node = base_obj[cur_name];

    var cur_id = cur_node["id"]; //(int)GetJSONNodeFromKey(*cur_node, "id")->value.toNumber();
    var cur_start_pt_index = cur_node["start_pt_index"]; //(int)GetJSONNodeFromKey(*cur_node, "start_pt_index")->value.toNumber();
    var cur_end_pt_index = cur_node["end_pt_index"]; //(int)GetJSONNodeFromKey(*cur_node, "end_pt_index")->value.toNumber();
    var cur_start_index = cur_node["start_index"]; //(int)GetJSONNodeFromKey(*cur_node, "start_index")->value.toNumber();
    var cur_end_index = cur_node["end_index"]; //(int)GetJSONNodeFromKey(*cur_node, "end_index")->value.toNumber();

    var new_region = new MeshRenderRegion(indices_in,
        rest_pts_in,
        uvs_in,
        cur_start_pt_index,
        cur_end_pt_index,
        cur_start_index,
        cur_end_index);

    new_region.setName(cur_name);
    new_region.setTagId(cur_id);

    // Read in weights
    var weight_map =
      new_region.normal_weight_map;
    var weight_obj = cur_node["weights"];

    for (var w_key in weight_obj)
    {
      var w_node = weight_obj[w_key];
      var values = CreatureModuleUtils.ReadFloatArrayJSON(weight_obj, w_key);
      weight_map[w_key] = values;
    }

    ret_regions.push(new_region);
  }

  return ret_regions;
};

CreatureModuleUtils.CreateRegionsFlat = function(meshIn, indices_in, rest_pts_in, uvs_in)
{
  var ret_regions = [];

  for (var i = 0; i < meshIn.regionsLength(); i++)
  {
  	var cur_node = meshIn.regions(i);
  	var cur_name = cur_node.name();

    var cur_id = cur_node.id(); //(int)GetJSONNodeFromKey(*cur_node, "id")->value.toNumber();
    var cur_start_pt_index = cur_node.startPtIndex(); //(int)GetJSONNodeFromKey(*cur_node, "start_pt_index")->value.toNumber();
    var cur_end_pt_index = cur_node.endPtIndex(); //(int)GetJSONNodeFromKey(*cur_node, "end_pt_index")->value.toNumber();
    var cur_start_index = cur_node.startIndex(); //(int)GetJSONNodeFromKey(*cur_node, "start_index")->value.toNumber();
    var cur_end_index = cur_node.endIndex(); //(int)GetJSONNodeFromKey(*cur_node, "end_index")->value.toNumber();

    var new_region = new MeshRenderRegion(indices_in,
        rest_pts_in,
        uvs_in,
        cur_start_pt_index,
        cur_end_pt_index,
        cur_start_index,
        cur_end_index);

    new_region.setName(cur_name);
    new_region.setTagId(cur_id);

    // Read in weights
    var weight_map =
      new_region.normal_weight_map;
    var weight_obj = cur_node["weights"];

    for (var j = 0; j < cur_node.weightsLength(); j++)
    {
      var w_node = cur_node.weights(j);
      var w_key = w_node.name();
      var values = CreatureModuleUtils.getFloatArray(w_node.weightsArray());
      weight_map[w_key] = values;
    }

    ret_regions.push(new_region);
  }

  return ret_regions;
};

CreatureModuleUtils.GetStartEndTimes = function(json_obj, key)
{
  var start_time = 0;
  var end_time = 0;
  var first = true;
  var base_obj = json_obj[key];

  for (var cur_val in base_obj)
  {
    var cur_node = base_obj[cur_val];
    var cur_num = parseInt(cur_val);
    if(first) {
      start_time = cur_num;
      end_time = cur_num;
      first = false;
    }
    else {
      if(cur_num > end_time) {
        end_time = cur_num;
      }
      
      if(cur_num < start_time) {
        start_time = cur_num;
      }
    }
  }

  return {first:start_time, second:end_time};
};

CreatureModuleUtils.GetStartEndTimesFlat = function(animBonesList)
{
  var start_time = 0;
  var end_time = 0;
  var first = true;

  for (var i = 0; i < animBonesList.timeSamplesLength(); i++)
  {
    var cur_node = animBonesList.timeSamples(i);
    var cur_num = cur_node.time();
    if(first) {
      start_time = cur_num;
      end_time = cur_num;
      first = false;
    }
    else {
      if(cur_num > end_time) {
        end_time = cur_num;
      }
      
      if(cur_num < start_time) {
        start_time = cur_num;
      }
    }
  }

  return {first:start_time, second:end_time};
};

CreatureModuleUtils.FillBoneGapCache = function(prev_time, cur_time, set_index, cache_manager, cache_list)
{
	var gap_diff = cur_time - prev_time;
	if (gap_diff > 1)
	{
		// Gap Step
		var prev_index = cache_manager.getIndexByTime(prev_time);
		for (var j = 1; j < gap_diff; j++)
		{
			var gap_fraction = j / gap_diff;
			var gap_cache_list = [];
			for (var k = 0; k < cache_list.length; k++)
			{
				var cur_data = cache_manager.bone_cache_table[set_index][k];
				var prev_data = cache_manager.bone_cache_table[prev_index][k];
				var gap_cache_data = new MeshBoneCache(cur_data.getKey());
				gap_cache_data.setWorldStartPt(
					Utils.vecInterp(prev_data.getWorldStartPt(), cur_data.getWorldStartPt(), gap_fraction));
				gap_cache_data.setWorldEndPt(
					Utils.vecInterp(prev_data.getWorldEndPt(), cur_data.getWorldEndPt(), gap_fraction));

				gap_cache_list.push(gap_cache_data);
			}

			cache_manager.bone_cache_table[prev_index + j] = gap_cache_list;
		}
	}	
};

CreatureModuleUtils.FillBoneCache = function(json_obj, key, start_time, end_time, cache_manager)
{
  var base_obj = json_obj[key];

  cache_manager.init(start_time, end_time);

  var prev_time = start_time;
  for (var cur_time in base_obj)
  {
  	var cur_node = base_obj[cur_time];

    cache_list = [];

    for (var cur_name in cur_node)
    {
      var bone_node = cur_node[cur_name];

      var cur_start_pt = CreatureModuleUtils.ReadVector3JSON(bone_node, "start_pt"); //ReadJSONVec4_2(*bone_node, "start_pt");
      var cur_end_pt = CreatureModuleUtils.ReadVector3JSON(bone_node, "end_pt"); //ReadJSONVec4_2(*bone_node, "end_pt");

      var cache_data = new MeshBoneCache(cur_name);
      cache_data.setWorldStartPt(cur_start_pt);
      cache_data.setWorldEndPt(cur_end_pt);

      cache_list.push(cache_data);
    }

    var set_index = cache_manager.getIndexByTime(cur_time);
    cache_manager.bone_cache_table[set_index] = cache_list;
    
    CreatureModuleUtils.FillBoneGapCache(prev_time, cur_time, set_index, cache_manager, cache_list);   
	prev_time = cur_time;    
  }

  cache_manager.makeAllReady();
};

CreatureModuleUtils.FillBoneCacheFlat = function(animBonesList, start_time, end_time, cache_manager)
{
  cache_manager.init(start_time, end_time);
  
  var prev_time = start_time;
  for (var i = 0; i < animBonesList.timeSamplesLength(); i++)
  {
  	var cur_node = animBonesList.timeSamples(i);
  	var cur_time = cur_node.time();

    cache_list = [];

    for (var j = 0; j < cur_node.bonesLength(); j++)
    {
      var bone_node = cur_node.bones(j);
      var cur_name = bone_node.name();

      var cur_start_pt = CreatureModuleUtils.ReadFloatArray3DFlat(bone_node.startPtArray()); //ReadJSONVec4_2(*bone_node, "start_pt");
      var cur_end_pt = CreatureModuleUtils.ReadFloatArray3DFlat(bone_node.endPtArray()); //ReadJSONVec4_2(*bone_node, "end_pt");

      var cache_data = new MeshBoneCache(cur_name);
      cache_data.setWorldStartPt(cur_start_pt);
      cache_data.setWorldEndPt(cur_end_pt);

      cache_list.push(cache_data);
    }

    var set_index = cache_manager.getIndexByTime(cur_time);
    cache_manager.bone_cache_table[set_index] = cache_list;
    
    CreatureModuleUtils.FillBoneGapCache(prev_time, cur_time, set_index, cache_manager, cache_list);   
	prev_time = cur_time;        
  }

  cache_manager.makeAllReady();
};

CreatureModuleUtils.FillDeformationGapCache = function(prev_time, cur_time, set_index, cache_manager, cache_list)
{
	var gap_diff = cur_time - prev_time;
	if (gap_diff > 1)
	{
		// Gap Step
		var prev_index = cache_manager.getIndexByTime(prev_time);
		for (var j = 1; j < gap_diff; j++)
		{
			var gap_fraction = j / gap_diff;
			var gap_cache_list = [];

			for (var k = 0; k < cache_list.length; k++)
			{
				var cur_data = cache_manager.displacement_cache_table[set_index][k];
				var prev_data = cache_manager.displacement_cache_table[prev_index][k];
				var gap_cache_data = new MeshDisplacementCache(cur_data.getKey());
				if (cur_data.getLocalDisplacements().length > 0)
				{
					gap_cache_data.setLocalDisplacements(
						Utils.ptsInterp(prev_data.getLocalDisplacements(), cur_data.getLocalDisplacements(), gap_fraction));
				}
				else {
					gap_cache_data.setPostDisplacements(
						Utils.ptsInterp(prev_data.getPostDisplacements(), cur_data.getPostDisplacements(), gap_fraction));
				}

				gap_cache_list.push(gap_cache_data);
			}

			cache_manager.displacement_cache_table[prev_index + j] = gap_cache_list;
		}
	}	
};

CreatureModuleUtils.FillDeformationCache = function(json_obj, key, start_time, end_time, cache_manager)
{
  var base_obj = json_obj[key];

  cache_manager.init(start_time, end_time);
  
  var prev_time = start_time;
  for (var cur_time in base_obj)
  {
  	var cur_node = base_obj[cur_time];

    var cache_list = [];

    for (var cur_name in cur_node)
    {
      var mesh_node = cur_node[cur_name];

      var cache_data = new MeshDisplacementCache(cur_name);

      var use_local_displacement = CreatureModuleUtils.ReadBoolJSON(mesh_node, "use_local_displacements"); //GetJSONNodeFromKey(*mesh_node, "use_local_displacements")->value.toBool();
      var use_post_displacement = CreatureModuleUtils.ReadBoolJSON(mesh_node, "use_post_displacements"); //GetJSONNodeFromKey(*mesh_node, "use_post_displacements")->value.toBool();

      if(use_local_displacement == true) {
        var read_pts = CreatureModuleUtils.ReadPointsArray2DJSON(mesh_node, "local_displacements"); //ReadJSONPoints2DVector(*mesh_node, "local_displacements");
        cache_data.setLocalDisplacements(read_pts);
      }

      if(use_post_displacement == true) {
        var read_pts = CreatureModuleUtils.ReadPointsArray2DJSON(mesh_node, "post_displacements"); //ReadJSONPoints2DVector(*mesh_node, "post_displacements");
        cache_data.setPostDisplacements(read_pts);
      }

      cache_list.push(cache_data);
    }

    var set_index = cache_manager.getIndexByTime(cur_time);
    cache_manager.displacement_cache_table[set_index] = cache_list;
    
    CreatureModuleUtils.FillDeformationGapCache(prev_time, cur_time, set_index, cache_manager, cache_list);
    prev_time = cur_time;
  }

  cache_manager.makeAllReady();
};

CreatureModuleUtils.FillDeformationCacheFlat = function(animMeshList, start_time, end_time, cache_manager)
{
  cache_manager.init(start_time, end_time);

  var prev_time = start_time;
  for (var i = 0; i < animMeshList.timeSamplesLength(); i++)
  {
  	var cur_node = animMeshList.timeSamples(i);
  	var cur_time = cur_node.time();

    var cache_list = [];

    for (var j = 0; j < cur_node.meshesLength(); j++)
    {
      var mesh_node = cur_node.meshes(j);
      var cur_name = mesh_node.name();

      var cache_data = new MeshDisplacementCache(cur_name);

      var use_local_displacement = mesh_node.useLocalDisplacements(); //GetJSONNodeFromKey(*mesh_node, "use_local_displacements")->value.toBool();
      var use_post_displacement = mesh_node.usePostDisplacements(); //GetJSONNodeFromKey(*mesh_node, "use_post_displacements")->value.toBool();

      if(use_local_displacement == true) {
        var read_pts = CreatureModuleUtils.ReadPointsArray2DFlat(mesh_node.localDisplacementsArray()); //ReadJSONPoints2DVector(*mesh_node, "local_displacements");
        cache_data.setLocalDisplacements(read_pts);
      }

      if(use_post_displacement == true) {
      	var tmpNum = mesh_node.postDisplacementsLength();
        var read_pts = CreatureModuleUtils.ReadPointsArray2DFlat(mesh_node.postDisplacementsArray()); //ReadJSONPoints2DVector(*mesh_node, "post_displacements");
        cache_data.setPostDisplacements(read_pts);
      }

      cache_list.push(cache_data);
    }

    var set_index = cache_manager.getIndexByTime(cur_time);
    cache_manager.displacement_cache_table[set_index] = cache_list;
    
    CreatureModuleUtils.FillDeformationGapCache(prev_time, cur_time, set_index, cache_manager, cache_list);
    prev_time = cur_time;    
  }

  cache_manager.makeAllReady();
};


CreatureModuleUtils.FillUVSwapCache = function(json_obj, key, start_time, end_time, cache_manager)
{
  var base_obj = json_obj[key];

  cache_manager.init(start_time, end_time);

  for (var cur_time in base_obj)
  {
  	var cur_node = base_obj[cur_time];

    var cache_list = [];

    for (var cur_name in cur_node)
    {
      var uv_node = cur_node[cur_name];

      var cache_data = new MeshUVWarpCache(cur_name);
      var use_uv = CreatureModuleUtils.ReadBoolJSON(uv_node, "enabled"); //GetJSONNodeFromKey(*uv_node, "enabled")->value.toBool();
      cache_data.setEnabled(use_uv);
      if(use_uv == true) {
        var local_offset = CreatureModuleUtils.ReadVector2JSON(uv_node, "local_offset"); //ReadJSONVec2(*uv_node, "local_offset");
        var global_offset = CreatureModuleUtils.ReadVector2JSON(uv_node, "global_offset"); //ReadJSONVec2(*uv_node, "global_offset");
        var scale = CreatureModuleUtils.ReadVector2JSON(uv_node, "scale"); //ReadJSONVec2(*uv_node, "scale");
        cache_data.setUvWarpLocalOffset(local_offset);
        cache_data.setUvWarpGlobalOffset(global_offset);
        cache_data.setUvWarpScale(scale);
      }

      cache_list.push(cache_data);
    }

    var set_index = cache_manager.getIndexByTime(cur_time);
    cache_manager.uv_cache_table[set_index] = cache_list;
  }

  cache_manager.makeAllReady();
};

CreatureModuleUtils.FillUVSwapCacheFlat = function(animUVList, start_time, end_time, cache_manager)
{
  cache_manager.init(start_time, end_time);

  for (var i = 0; i < animUVList.timeSamplesLength(); i++)
  {
  	var cur_node = animUVList.timeSamples(i);
  	var cur_time = cur_node.time();

    var cache_list = [];

    for (var j = 0; j < cur_node.uvSwapsLength(); j++)
    {
      var uv_node = cur_node.uvSwaps(j);
      var cur_name = uv_node.name();

      var cache_data = new MeshUVWarpCache(cur_name);
      var use_uv = uv_node.enabled(); //GetJSONNodeFromKey(*uv_node, "enabled")->value.toBool();
      cache_data.setEnabled(use_uv);
      if(use_uv == true) {
        var local_offset = CreatureModuleUtils.ReadVector2Flat(uv_node.localOffsetArray()); //ReadJSONVec2(*uv_node, "local_offset");
        var global_offset = CreatureModuleUtils.ReadVector2Flat(uv_node.globalOffsetArray()); //ReadJSONVec2(*uv_node, "global_offset");
        var scale = CreatureModuleUtils.ReadVector2Flat(uv_node.scaleArray()); //ReadJSONVec2(*uv_node, "scale");
        cache_data.setUvWarpLocalOffset(local_offset);
        cache_data.setUvWarpGlobalOffset(global_offset);
        cache_data.setUvWarpScale(scale);
      }

      cache_list.push(cache_data);
    }

    var set_index = cache_manager.getIndexByTime(cur_time);
    cache_manager.uv_cache_table[set_index] = cache_list;
  }

  cache_manager.makeAllReady();
};

CreatureModuleUtils.FillOpacityGapCache = function(prev_time, cur_time, set_index, cache_manager, cache_list)
{
	var gap_diff = cur_time - prev_time;
	if (gap_diff > 1)
	{
		// Gap Step
		var prev_index = cache_manager.getIndexByTime(prev_time);
		for (var j = 1; j < gap_diff; j++)
		{
			var gap_fraction = j / gap_diff;
			var gap_cache_list = [];
			for (var k = 0; k < cache_list.length; k++)
			{
				var cur_data = cache_manager.opacity_cache_table[set_index][k];
				var prev_data = cache_manager.opacity_cache_table[prev_index][k];
				var gap_cache_data = new MeshOpacityCache(cur_data.getKey());
				gap_cache_data.setOpacity(Utils.scalarInterp(prev_data.getOpacity(), cur_data.getOpacity(), gap_fraction));

				gap_cache_list.push(gap_cache_data);
			}

			cache_manager.opacity_cache_table[prev_index + j] = gap_cache_list;
		}
	}	
};

CreatureModuleUtils.FillOpacityCache = function(json_obj, key, start_time, end_time, cache_manager)
{
  var base_obj = json_obj[key];

  cache_manager.init(start_time, end_time);

  var prev_time = start_time;
  for (var cur_time in base_obj)
  {
  	var cur_node = base_obj[cur_time];

    var cache_list = [];

    for (var cur_name in cur_node)
    {
      var opacity_node = cur_node[cur_name];

      var cache_data = new MeshOpacityCache(cur_name);
      cache_data.setOpacity(opacity_node["opacity"]);

      cache_list.push(cache_data);
    }

    var set_index = cache_manager.getIndexByTime(cur_time);
    cache_manager.opacity_cache_table[set_index] = cache_list;
    
    CreatureModuleUtils.FillOpacityGapCache(prev_time, cur_time, set_index, cache_manager, cache_list);
    prev_time = cur_time;
  }

  cache_manager.makeAllReady();
};

CreatureModuleUtils.FillOpacityCacheFlat = function(animOpacityList, start_time, end_time, cache_manager)
{
  cache_manager.init(start_time, end_time);

  var prev_time = start_time;
  for (var i = 0; i < animOpacityList.timeSamplesLength(); i++)
  {
  	var cur_node = animOpacityList.timeSamples(i);
  	var cur_time = cur_node.time();

    var cache_list = [];

    for (var j = 0; j < cur_node.meshOpacitiesLength(); j++)
    {
      var opacity_node = cur_node.meshOpacities(j);
      var cur_name = opacity_node.name();

      var cache_data = new MeshOpacityCache(cur_name);
      cache_data.setOpacity(opacity_node.opacity());

      cache_list.push(cache_data);
    }

    var set_index = cache_manager.getIndexByTime(cur_time);
    cache_manager.opacity_cache_table[set_index] = cache_list;
    
    CreatureModuleUtils.FillOpacityGapCache(prev_time, cur_time, set_index, cache_manager, cache_list);
    prev_time = cur_time;    
  }

  cache_manager.makeAllReady();
};

function CreatureUVSwapPacket (local_offset_in, global_offset_in, scale_in, tag_in)
{
	this.local_offset = local_offset_in;
	this.global_offset = global_offset_in;
	this.scale = scale_in;
	this.tag = tag_in;
};

CreatureModuleUtils.FillSwapUVPacketMap = function(json_obj)
{
	ret_map = {};
	for (var cur_key in json_obj)
	{
		var cur_node = json_obj[cur_key];
		var cur_name = cur_key;
		var cur_packets = [];

		for (var i = 0; i < cur_node.length; i++)
		{
			var packet_node = cur_node[i];
			var local_offset = CreatureModuleUtils.ReadVector2JSON(packet_node, "local_offset");
			var global_offset = CreatureModuleUtils.ReadVector2JSON(packet_node, "global_offset");
			var scale = CreatureModuleUtils.ReadVector2JSON(packet_node, "scale");
			var tag = packet_node["tag"];
			
			var new_packet = new CreatureUVSwapPacket(local_offset, global_offset, scale, tag);
			cur_packets.push(new_packet);
		}

		
		ret_map[cur_name] = cur_packets;
	};

	return ret_map;
};

CreatureModuleUtils.FillSwapUVPacketMapFlat = function(uvSwapItemFlatHolder)
{
	ret_map = {};
	for (var i = 0; i < uvSwapItemFlatHolder.meshesLength(); i++)
	{
		var cur_node = uvSwapItemFlatHolder.meshes(i);
		var cur_name = cur_node.name();
		var cur_packets = [];

		for (var j = 0; j < cur_node.itemsLength(); j++)
		{
			var packet_node = cur_node.items(j);
			var local_offset = CreatureModuleUtils.ReadVector2Flat(packet_node.localOffsetArray());
			var global_offset = CreatureModuleUtils.ReadVector2Flat(packet_node.globalOffsetArray());
			var scale = CreatureModuleUtils.ReadVector2Flat(packet_node.scaleArray());
			var tag = packet_node.tag();
			
			var new_packet = new CreatureUVSwapPacket(local_offset, global_offset, scale, tag);
			cur_packets.push(new_packet);
		}

		
		ret_map[cur_name] = cur_packets;
	};

	return ret_map;
};

CreatureModuleUtils.FillAnchorPointMap = function(json_obj)
{
	var anchor_data_node = json_obj["AnchorPoints"];

	ret_map = {};
	for (var i = 0; i < anchor_data_node.length; i++)
	{
		var cur_node = anchor_data_node[i];
		var cur_pt = CreatureModuleUtils.ReadVector2JSON(cur_node, "point");
		var cur_name = cur_node["anim_clip_name"];

		ret_map[cur_name] = cur_pt;
	}

	return ret_map;
};

CreatureModuleUtils.FillAnchorPointMapFlat = function(anchorFlatHolder)
{
	ret_map = {};
	for (var i = 0; i < anchorFlatHolder.anchorPointsLength(); i++)
	{
		var cur_node = anchorFlatHolder.anchorPoints(i);
		var cur_pt = CreatureModuleUtils.ReadVector2Flat(cur_node.pointArray());
		var cur_name = cur_node.animClipName();

		ret_map[cur_name] = cur_pt;
	}

	return ret_map;
};

// Creature
function Creature(load_data, use_flat_data)
{
	this.InitDefaultData();
	
	if(use_flat_data)
	{
	    this.LoadFromDataFlat(load_data);			
	}
	else {	
	    this.LoadFromData(load_data);	
	}
};

Creature.prototype.InitDefaultData = function()
{
	  this.total_num_pts = 0;
    this.total_num_indices = 0;
    this.global_indices = null;
    this.global_pts = null;
    this.global_uvs = null;
    this.render_pts = null;
    this.render_colours = null;
    this.render_composition = null;
    this.boundary_indices = [];
    this.boundary_min = vec2.create();
    this.boundary_max = vec2.create();
    this.uv_swap_packets = {};
    this.active_uv_swap_actions = {};
    this.anchor_point_map = {};
    this.anchor_points_active = false;
    this.skin_swap_active = false;
    this.skin_swap_name = "";
    this.final_skin_swap_indices = null;
    this.creature_meta_data = null;
};

Creature.prototype.SetMetaData = function(creature_meta_data)
{
  this.creature_meta_data = creature_meta_data;
};

Creature.prototype.EnableSkinSwap = function(swap_name_in, active)
{
  this.skin_swap_active = active;
  if(!this.skin_swap_active)
  {
    this.skin_swap_name = "";
    this.final_skin_swap_indices = null;
  }
  else
  {
    this.skin_swap_name = swap_name_in;
    this.final_skin_swap_indices = this.creature_meta_data.buildSkinSwapIndices(
      this.skin_swap_name, 
      this.render_composition);    
  }
};

Creature.prototype.DisableSkinSwap = function()
{
  this.EnableSkinSwap("", false);
};

Creature.prototype.ShouldSkinSwap = function()
{
  return this.creature_meta_data && this.skin_swap_active && this.final_skin_swap_indices;
};

Creature.prototype.SetActiveItemSwap = function(region_name, swap_idx)
{
	this.active_uv_swap_actions[region_name] = swap_idx;
};

Creature.prototype.RemoveActiveItemSwap = function(region_name)
{
	delete this.active_uv_swap_actions[region_name];
};

Creature.prototype.GetAnchorPoint = function(anim_clip_name_in)
{
	if(anim_clip_name_in in this.anchor_point_map)
	{
		return this.anchor_point_map[anim_clip_name_in];
	}
	
	return vec2.fromValues(0, 0);
};

// Fills entire mesh with (r,g,b,a) colours
Creature.prototype.FillRenderColours = function(r, g, b, a)
{
  for(var i = 0; i < this.total_num_pts; i++)
  {
    var cur_colour_index = i * 4;
    this.render_colours[0 + cur_colour_index] = r;
    this.render_colours[1 + cur_colour_index] = g;
    this.render_colours[2 + cur_colour_index] = b;
    this.render_colours[3 + cur_colour_index] = a;
  }
};

// Compute boundary indices

Creature.prototype.ComputeBoundaryIndices = function()
{
	var freq_table = {};
	for(var i = 0; i < this.total_num_pts; i++)
	{
		freq_table[i] = 0;
	}
	
	var cur_regions = this.render_composition.getRegions();
	for(var i = 0; i < this.global_indices.length; i++)
	{
		var cur_idx = this.global_indices[i];
		var is_found = false;
		for(var j = 0; j < cur_regions.length; j++)
		{
    		var cur_region = cur_regions[j];
    		var cur_start_index = cur_region.getStartPtIndex();
    		var cur_end_index = cur_region.getEndPtIndex();
    		
    		if(cur_idx >= cur_start_index && cur_idx <= cur_end_index)
    		{
    			is_found = true;
    			break;
    		}
    	}


		if(is_found)
		{
			freq_table[cur_idx]++;
		}
	}
	
	// now find the boundary indices who have <= 5 referenced triangles
	this.boundary_indices = [];
	for(var i = 0; i < this.total_num_pts; i++)
	{
		if(freq_table[i] <=5)
		{
			this.boundary_indices.push(i);
		}
	}
};

// Compute min and max bounds of the animated mesh
Creature.prototype.ComputeBoundaryMinMax = function()
{
	
	if(this.boundary_indices.length <= 0)
	{
		this.ComputeBoundaryIndices();
	}
	
	
	var firstIdx = this.boundary_indices[0] * 3;
	var minPt = vec2.fromValues(this.render_pts[firstIdx + 0], this.render_pts[firstIdx + 1]);
	var maxPt = vec2.fromValues(minPt[0], minPt[1]);
	
	
	for(var i = 0; i < this.boundary_indices.length; i++)
	{
		var ref_idx = this.boundary_indices[i] * 3;
		var ref_x = this.render_pts[ref_idx];
		var ref_y = this.render_pts[ref_idx + 1];
		
		if(minPt[0] > ref_x)
		{
			minPt[0] = ref_x;
		}
		
		if(minPt[1] > ref_y)
		{
			minPt[1] = ref_y;
		}
		
		if(maxPt[0] < ref_x)
		{
			maxPt[0] = ref_x;
		}
		
		if(maxPt[1] < ref_y)
		{
			maxPt[1] = ref_y;
		}
	}
	
	this.boundary_min = minPt;
	this.boundary_max = maxPt;
};


// Load data
Creature.prototype.LoadFromData = function(load_data)
{
  // Load points and topology
  var json_mesh = load_data["mesh"];

  this.global_pts = CreatureModuleUtils.ReadFloatArray3DJSON(json_mesh, "points");
  this.total_num_pts = this.global_pts.length / 3;

  this.global_indices = CreatureModuleUtils.ReadIntArrayJSON (json_mesh, "indices");
  this.total_num_indices = this.global_indices.length;

  this.global_uvs = CreatureModuleUtils.ReadFloatArrayJSON (json_mesh, "uvs");
  
  
  this.render_colours = [];
  for(var i = 0; i < this.total_num_pts * 4; i++)
  {
    this.render_colours.push(0);
  }
  this.FillRenderColours(1, 1, 1, 1);

  this.render_pts = [];

  // Load bones
  var root_bone = CreatureModuleUtils.CreateBones(load_data, "skeleton");


  // Load regions
  var regions = CreatureModuleUtils.CreateRegions(json_mesh,
      "regions",
      this.global_indices,
      this.global_pts,
      this.global_uvs);

  // Add into composition
  this.render_composition = new MeshRenderBoneComposition();
  this.render_composition.setRootBone(root_bone);
  this.render_composition.getRootBone().computeRestParentTransforms();

  for(var i = 0; i < regions.length; i++) {
  	var cur_region = regions[i];
    cur_region.setMainBoneKey(root_bone.getKey());
    cur_region.determineMainBone(root_bone);
    this.render_composition.addRegion(cur_region);
  }

  this.render_composition.initBoneMap();
  this.render_composition.initRegionsMap();

  for(var i = 0; i < regions.length; i++) {
  	var cur_region = regions[i];
    cur_region.initFastNormalWeightMap(this.render_composition.bones_map);
  }

  this.render_composition.resetToWorldRestPts();
  
  // Fill up uv swap packets
  if("uv_swap_items" in load_data)
  {
  	var json_uv_swap_base = load_data["uv_swap_items"];
  	this.uv_swap_packets = CreatureModuleUtils.FillSwapUVPacketMap(json_uv_swap_base);
  }
  
  // Load Anchor Points
  if("anchor_points_items" in load_data)
  {
  	var anchor_point_base = load_data["anchor_points_items"];
  	this.anchor_point_map = CreatureModuleUtils.FillAnchorPointMap(anchor_point_base);
  }
  
};

// Load data Flat
Creature.prototype.LoadFromDataFlat = function(flatRoot)
{
  // Load points and topology
  var flat_mesh = flatRoot.dataMesh();
  var flat_skeleton = flatRoot.dataSkeleton();

  this.global_pts = CreatureModuleUtils.ReadFloatArray3DFlat(flat_mesh.pointsArray());
  this.total_num_pts = this.global_pts.length / 3;

  this.global_indices = CreatureModuleUtils.getIntArray(flat_mesh.indicesArray());
  this.total_num_indices = this.global_indices.length;

  this.global_uvs = CreatureModuleUtils.getFloatArray(flat_mesh.uvsArray());
  
  
  this.render_colours = [];
  for(var i = 0; i < this.total_num_pts * 4; i++)
  {
    this.render_colours.push(0);
  }
  this.FillRenderColours(1, 1, 1, 1);

  this.render_pts = [];

  // Load bones
  var root_bone = CreatureModuleUtils.CreateBonesFlat(flat_skeleton);


  // Load regions
  var regions = CreatureModuleUtils.CreateRegionsFlat(flat_mesh,
      this.global_indices,
      this.global_pts,
      this.global_uvs);

  // Add into composition
  this.render_composition = new MeshRenderBoneComposition();
  this.render_composition.setRootBone(root_bone);
  this.render_composition.getRootBone().computeRestParentTransforms();

  for(var i = 0; i < regions.length; i++) {
  	var cur_region = regions[i];
    cur_region.setMainBoneKey(root_bone.getKey());
    cur_region.determineMainBone(root_bone);
    this.render_composition.addRegion(cur_region);
  }

  this.render_composition.initBoneMap();
  this.render_composition.initRegionsMap();

  for(var i = 0; i < regions.length; i++) {
  	var cur_region = regions[i];
    cur_region.initFastNormalWeightMap(this.render_composition.bones_map);
  }

  this.render_composition.resetToWorldRestPts();
  
  // Fill up uv swap packets
  var flat_uv_swap_item_holder = flatRoot.dataUvSwapItem();
  this.uv_swap_packets = CreatureModuleUtils.FillSwapUVPacketMapFlat(flat_uv_swap_item_holder);
  
  // Load Anchor Points
  var flat_anchor_holder = flatRoot.dataAnchorPoints();
  this.anchor_point_map = CreatureModuleUtils.FillAnchorPointMapFlat(flat_anchor_holder);
  
};

// CreatureAnimation
function CreatureAnimation(load_data, name_in, use_flat_data)
{
	this.initDefaultData(name_in);
	
	if(use_flat_data)
	{
	    this.LoadFromDataFlat(name_in, load_data.dataAnimation());			
	}
	else {
	    this.LoadFromData(name_in, load_data);			
	}
};

CreatureAnimation.prototype.initDefaultData = function(name_in)
{
	this.name = name_in;
    this.bones_cache = new MeshBoneCacheManager();
    this.displacement_cache = new MeshDisplacementCacheManager();
    this.uv_warp_cache = new MeshUVWarpCacheManager();
    this.opacity_cache = new MeshOpacityCacheManager();
    this.cache_pts = [];
    this.fill_cache_pts = [];
};

CreatureAnimation.prototype.LoadFromData = function(name_in, load_data)
{
  var json_anim_base = load_data["animation"];
  var json_clip = json_anim_base[name_in];

  var start_end_times = CreatureModuleUtils.GetStartEndTimes(json_clip, "bones");
  this.start_time = start_end_times.first;
  this.end_time = start_end_times.second;

  // bone animation
  CreatureModuleUtils.FillBoneCache(json_clip,
      "bones",
      this.start_time,
      this.end_time,
      this.bones_cache);

  // mesh deformation animation
  CreatureModuleUtils.FillDeformationCache(json_clip,
      "meshes",
      this.start_time,
      this.end_time,
      this.displacement_cache);

  // uv swapping animation
  CreatureModuleUtils.FillUVSwapCache(json_clip,
      "uv_swaps",
      this.start_time,
      this.end_time,
      this.uv_warp_cache);
      
  // opacity animation
  CreatureModuleUtils.FillOpacityCache(json_clip,
      "mesh_opacities",
      this.start_time,
      this.end_time,
      this.opacity_cache);  
};

CreatureAnimation.prototype.LoadFromDataFlat = function(name_in, animFlat)
{
  var flat_clip = null;
  for(var i = 0; i < animFlat.clipsLength(); i++)
  {
  	if(animFlat.clips(i).name() == name_in)
  	{
  		flat_clip = animFlat.clips(i);
  		break;
  	}
  }
	
  var start_end_times = CreatureModuleUtils.GetStartEndTimesFlat(flat_clip.bones());
  this.start_time = start_end_times.first;
  this.end_time = start_end_times.second;

  // bone animation
  CreatureModuleUtils.FillBoneCacheFlat(flat_clip.bones(),
      this.start_time,
      this.end_time,
      this.bones_cache);

  // mesh deformation animation
  CreatureModuleUtils.FillDeformationCacheFlat(flat_clip.meshes(),
      this.start_time,
      this.end_time,
      this.displacement_cache);

  // uv swapping animation
  CreatureModuleUtils.FillUVSwapCacheFlat(flat_clip.uvSwaps(),
      this.start_time,
      this.end_time,
      this.uv_warp_cache);
      
  // opacity animation
  CreatureModuleUtils.FillOpacityCacheFlat(flat_clip.meshOpacities(),
      this.start_time,
      this.end_time,
      this.opacity_cache);
};

CreatureAnimation.prototype.getIndexByTime = function(time_in)
{
  var retval = time_in - this.start_time;
  retval = Utils.clamp(retval, 0, (this.cache_pts.length) - 1);

  return retval;
};

CreatureAnimation.prototype.verifyFillCache = function()
{
	if(this.fill_cache_pts.length == (this.end_time - this.start_time + 1))
	{
		// ready to switch over
		this.cache_pts = this.fill_cache_pts;
	}
};

CreatureAnimation.prototype.poseFromCachePts = function(time_in, target_pts, num_pts)
{
        var cur_floor_time = this.getIndexByTime(Math.floor(time_in));
        var cur_ceil_time = this.getIndexByTime(Math.ceil(time_in));
        var cur_ratio = time_in - Math.floor(time_in);
        
        var set_pt = target_pts;
        var floor_pts = this.cache_pts[cur_floor_time];
        var ceil_pts = this.cache_pts[cur_ceil_time];
        
        var set_idx = 0;
        var floor_idx = 0;
        var ceil_idx = 0;
        
        for(var i = 0; i < num_pts; i++)
        {
            set_pt[set_idx + 0] = ((1.0 - cur_ratio) * floor_pts[floor_idx + 0]) + (cur_ratio * ceil_pts[ceil_idx + 0]);
            set_pt[set_idx + 1] = ((1.0 - cur_ratio) * floor_pts[floor_idx + 1]) + (cur_ratio * ceil_pts[ceil_idx + 1]);
            set_pt[set_idx + 2] = ((1.0 - cur_ratio) * floor_pts[floor_idx + 2]) + (cur_ratio * ceil_pts[ceil_idx + 2]);

            set_idx += 3;
            floor_idx += 3;
            ceil_idx += 3;
        }
};

// CreatureFrameCallback
function CreatureFrameCallback()
{
  this.callback = null;
  this.name = "";
  this.animClipName = "";
  this.frame = 0;
  this.triggered = false;
};

CreatureFrameCallback.prototype.resetCallback = function()
{
  this.triggered = false;
};

CreatureFrameCallback.prototype.tryTrigger = function(frameIn)
{
  if(this.triggered)
  {
    return false;
  }

  if (Math.round(frameIn) >= this.frame)
  {
    this.triggered = true;
    return true;
  }

  return false;
};

// CreatureGameController
function CreatureGameController(meta_asset)
{
  this.meta_asset = meta_asset;
  this.event_callbacks = [];
  this.run_time = 0;
};

// Builds Event Callbacks from MetaData
CreatureGameController.prototype.BuildFrameCallbacks = function(assignCallbackFn)
{
  if(this.meta_asset == null)
  {
    return;
  }

  for (var cur_key in this.meta_asset.anim_events_map)
  {
    var cur_val = this.meta_asset.anim_events_map[cur_key];
    for (var cur_evt_key in cur_val)
    {
      var cur_evt = cur_val[cur_evt_key];
      var new_callback = new CreatureFrameCallback();
      new_callback.animClipName = cur_key;
      new_callback.name = cur_evt;
      new_callback.frame = Number(cur_evt_key);
      new_callback.callback = assignCallbackFn(animClipName, name);

      this.event_callbacks.push(new_callback);
    }
  }
};

// Manually adds a new event callback
CreatureGameController.prototype.AddFrameCallback = function(animClipName, name, frame, callbackFb)
{
  var new_callback = new CreatureFrameCallback();
  new_callback.animClipName = animClipName;
  new_callback.name = name;
  new_callback.frame = frame;
  new_callback.callback = callbackFb;

  this.event_callbacks.push(new_callback);
};

// Resets the callback triggers, Remember to call this whenever you Switch Animations
CreatureGameController.prototype.ResetFrameCallbacks = function()
{
  for (var cur_key in this.event_callbacks)
  {
    var frame_callback = this.event_callbacks[cur_key];
    frame_callback.resetCallback();
  }
};

// Call this after every animation update/timestep of your creatureManager
CreatureGameController.prototype.ProcessCallbacks = function(creature_manager)
{
  var cur_runtime = creature_manager.getActualRuntime();
  if(cur_runtime < this.run_time)
  {
    this.ResetFrameCallbacks();
  }

  this.run_time = cur_runtime;

  for (var i  = 0; i < this.event_callbacks.length; i++)
  {
    var frame_callback = this.event_callbacks[i];
    if (frame_callback.animClipName == creature_manager.active_animation_name)
    {
        var should_trigger = frame_callback.tryTrigger(cur_runtime);
        if (should_trigger && (frame_callback.callback != null))
        {
          frame_callback.callback(frame_callback.name, cur_runtime);
        }
    }
  }
};

// CreatureMetaData
function CreatureMetaData()
{
  this.skin_swaps = {};
  this.anim_events_map = {};
};

CreatureMetaData.prototype.clear = function()
{
  this.skin_swaps = {};
  this.anim_events_map = {};
};

CreatureMetaData.prototype.buildSkinSwapIndices = function(swap_name, bone_composition)
{
  var skin_swap_indices = null;
  if(!(swap_name in this.skin_swaps))
  {
    skin_swap_indices = [];
    return skin_swap_indices;
  }

  var swap_set = this.skin_swaps[swap_name];
  var total_size = 0;
  var regions_map = bone_composition.getRegionsMap();
  for(var region_name in regions_map)
  {
    if(region_name in swap_set)
    {
      var cur_region = regions_map[region_name];
      total_size += cur_region.getNumIndices();
    }
  }

  skin_swap_indices = [];
  var offset = 0;
  for(var region_name in regions_map)
  {
    if(region_name in swap_set)
    {
      var cur_region = regions_map[region_name];
      for(var j = 0; j < cur_region.getNumIndices(); j++)
      {
        skin_swap_indices.push(cur_region.getLocalIndex(j));
      }

      offset += cur_region.getNumIndices();
    }
  }

  return skin_swap_indices;
};

// CreatureManager
function CreatureManager(target_creature_in)
{
    this.target_creature = target_creature_in;
    this.is_playing = false;
    this.run_time = 0;
    this.time_scale = 30.0;
    this.blending_factor = 0;
    this.should_loop = true;
    this.use_custom_time_range = false;
    this.custom_start_time = 0;
    this.custom_end_time = 0;
    this.animations = {};
    this.bones_override_callback = null;

    this.blend_render_pts = [];
    this.blend_render_pts.push([]);
    this.blend_render_pts.push([]);
    this.do_blending = false;

    this.active_blend_animation_names = [];
    this.active_blend_animation_names.push("");
    this.active_blend_animation_names.push("");	
    
    this.do_auto_blending = false;
    this.auto_blend_delta = 0.0;
    
    this.auto_blend_names = [];
    this.auto_blend_names.push("");
    this.auto_blend_names.push("");
    
    this.active_blend_run_times = {};
};

// Create an animation
CreatureManager.prototype.CreateAnimation = function(load_data, name_in)
{
  var new_animation = new CreatureAnimation(load_data, name_in);
  this.AddAnimation(new_animation);
};

// Create all animations
CreatureManager.prototype.CreateAllAnimations = function(load_data)
{
  var all_animation_names = CreatureModuleUtils.GetAllAnimationNames (load_data);
  for(var i = 0; i < all_animation_names.length; i++)
  {
  	var cur_name = all_animation_names[i];
    this.CreateAnimation(load_data, cur_name);
  }

  this.SetActiveAnimationName (all_animation_names[0]);
};

// Add an animation
CreatureManager.prototype.AddAnimation = function(animation_in)
{
  this.animations[animation_in.name] = animation_in;
};

// Return an animation
CreatureManager.prototype.GetAnimation = function(name_in)
{
  return this.animations[name_in];
};

// Return the creature
CreatureManager.prototype.GetCreature = function()
{
  return this.target_creature;
};

// Returns all the animation names
CreatureManager.prototype.GetAnimationNames = function()
{
  var ret_names = [];
  for(var cur_name in animations) {
    ret_names.push(cur_name);
  }

  return ret_names;
};

// Sets the current animation to be active by name
CreatureManager.prototype.SetActiveAnimationName = function(name_in, check_already_active)
{
  if (name_in == null || (name_in in this.animations) == false) {
    return false;
  }
  
  if(check_already_active == true)
  {
  	if(this.active_animation_name == name_in)
  	{
  		return false;
  	}
  }

  this.active_animation_name = name_in;
  var cur_animation = this.animations[this.active_animation_name];
  this.run_time = cur_animation.start_time;
  this.UpdateRegionsSwitches(name_in);
  
  return true;
};

// Returns the name of the currently active animation
CreatureManager.prototype.GetActiveAnimationName = function()
{
  return this.active_animation_name;
};

// Returns the table of all animations
CreatureManager.prototype.GetAllAnimations = function()
{
  return this.animations;
};

// Creates a point cache for the current animation
CreatureManager.prototype.MakePointCache = function(animation_name_in)
{
        var store_run_time = this.getRunTime();
        var cur_animation = this.animations[animation_name_in];
        if(cur_animation.length > 0)
        {
            // cache already generated, just exit
            return;
        }
        
        var cache_pts_list = cur_animation.cache_pts;
        this.UpdateRegionsSwitches(animation_name_in);

        for(var i = cur_animation.start_time; i <= cur_animation.end_time; i++)
        {
            this.setRunTime(i);
            var new_pts = [];
            for (var j = 0; j < this.target_creature.total_num_pts * 3; j++) new_pts[j] = 0; 
            //auto new_pts = new glm::float32[target_creature->GetTotalNumPoints() * 3];
            this.PoseCreature(animation_name_in, new_pts, this.getRunTime());
            
            cache_pts_list.push(new_pts);
        }
        
        this.setRunTime(store_run_time);
};

// Fills up a single frame for a point cache animation
// Point caching is only enabled when the cache is FULLY filled up
// Remember the new filled cache is Appended onto the end of a list
// There is no indexing by time here so MAKE SURE this cache is filled up sequentially!
CreatureManager.prototype.FillSinglePointCacheFrame = function(animation_name_in, time_in)
{
	var store_run_time = this.getRunTime();
    var cur_animation = this.animations[animation_name_in];
	
	this.setRunTime(time_in);
    var new_pts = [];
    for (var j = 0; j < this.target_creature.total_num_pts * 3; j++) new_pts[j] = 0; 
    this.PoseCreature(animation_name_in, new_pts, time_in);
    
    cur_animation.fill_cache_pts.push(new_pts);
    cur_animation.verifyFillCache();

    this.setRunTime(store_run_time);
};

// Returns if animation is playing
CreatureManager.prototype.GetIsPlaying = function()
{
  return this.is_playing;
};

// Sets whether to loop the animation
CreatureManager.prototype.SetShouldLoop = function(flag_in)
{
	this.should_loop = flag_in;
};

// Sets whether to use a user defined custom time range for the currently
// active animation clip
CreatureManager.prototype.SetUseCustomTimeRange = function(flag_in)
{
	this.use_custom_time_range = flag_in;
};

// Sets the user defined custom time range
CreatureManager.prototype.SetCustomTimeRange = function(start_time_in, end_time_in)
{
	this.custom_start_time = start_time_in;
	this.custom_end_time = end_time_in;
};

// Sets whether the animation is playing
CreatureManager.prototype.SetIsPlaying = function(flag_in)
{
  this.is_playing = flag_in;
};

CreatureManager.prototype.ProcessAutoBlending = function()
{
	// process blending factor
	this.blending_factor += this.auto_blend_delta;
	if(this.blending_factor > 1)
	{
		this.blending_factor = 1;
	}
};

CreatureManager.prototype.IncreAutoBlendRunTimes = function(delta_in)
{
	set_animation_name = "";
	for(var j = 0; j < this.auto_blend_names.length; j++)
	{
		var cur_animation_name = this.auto_blend_names[j];				
		if ((cur_animation_name in this.animations) 
				&& (set_animation_name != cur_animation_name))
		{
			cur_run_time = this.active_blend_run_times[cur_animation_name];
			cur_run_time += delta_in;
			cur_run_time = this.correctRunTime(cur_run_time, cur_animation_name);

			this.active_blend_run_times[cur_animation_name] = cur_run_time;
					
			set_animation_name = cur_animation_name;
		}
	}
};

CreatureManager.prototype.correctRunTime = function(time_in, animation_name)
{
	ret_time = time_in;
	cur_animation = this.animations[animation_name];
	anim_start_time = cur_animation.start_time;
	anim_end_time = cur_animation.end_time;
			
	if (ret_time > anim_end_time)
	{
		if (this.should_loop)
		{
			ret_time = anim_start_time;
		}
		else {
			ret_time = anim_end_time;
		}
	}
	else if (ret_time < anim_start_time)
	{
		if (this.should_loop)
		{
			ret_time = anim_end_time;
		}
		else {
			ret_time = anim_start_time;
		}
	}
			
	return ret_time;
};


// Resets animation to start time
CreatureManager.prototype.ResetToStartTimes = function()
{
  var cur_animation = this.animations[active_animation_name];
  this.run_time = cur_animation.start_time;
};

// Sets the run time of the animation
CreatureManager.prototype.setRunTime = function(time_in)
{
  this.run_time = time_in;
  this.correctTime ();
};

// Increments the run time of the animation by a delta value
CreatureManager.prototype.increRunTime = function(delta_in)
{
  this.run_time += delta_in;
  this.correctTime ();
};

CreatureManager.prototype.correctTime = function()
{
  var cur_animation = this.animations[this.active_animation_name];
  var anim_start_time = cur_animation.start_time;
  var anim_end_time = cur_animation.end_time;
  
  if(this.use_custom_time_range)
  {
  	anim_start_time = this.custom_start_time;
  	anim_end_time = this.custom_end_time;
  }
  
  if(this.run_time > anim_end_time)
  {
  	if(this.should_loop)
  	{
    	this.run_time = anim_start_time;
    }
    else {
    	this.run_time = anim_end_time;
    }
  }
  else if(this.run_time < anim_start_time)
  {
  	if(this.should_loop)
  	{
    	this.run_time = anim_end_time;
    }
    else {
    	this.run_time = anim_start_time;	
    }
  }
};

// Returns the current run time of the animation
CreatureManager.prototype.getRunTime = function()
{
  return this.run_time;
};

// Returns the true run time taking into account of blending
CreatureManager.prototype.getActualRuntime = function()
{
  if (this.do_auto_blending)
  {
      if (this.active_animation_name in this.active_blend_run_times)
      {
          return this.active_blend_run_times[this.active_animation_name];
      }
  }

  return this.run_time;
};

CreatureManager.prototype.checkAnimationBlendValid = function()
{
  for(var i = 0; i < 2; i++)
  {
    cur_animation_name = this.active_blend_animation_names[i];
    if(!(cur_animation_name in this.animations) ||
      !(cur_animation_name in this.active_blend_run_times))
    {
      return false;      
    }
  }

  return true;
};

CreatureManager.prototype.UpdateRegionsSwitches = function(animation_name_in)
{
  var cur_animation = this.animations[animation_name_in];

  var displacement_cache_manager = cur_animation.displacement_cache;
  var displacement_table =
    displacement_cache_manager.displacement_cache_table[0];

  var uv_warp_cache_manager = cur_animation.uv_warp_cache;
  var uv_swap_table =
    uv_warp_cache_manager.uv_cache_table[0];

  var render_composition =
    this.target_creature.render_composition;

  var all_regions = render_composition.getRegions();

  var index = 0;
  for(var i = 0; i < all_regions.length; i++)
  {
  	var cur_region = all_regions[i];
    // Setup active or inactive displacements
    var use_local_displacements = !(displacement_table[index].getLocalDisplacements().length == 0);
    var use_post_displacements = !(displacement_table[index].getPostDisplacements().length == 0);
    cur_region.setUseLocalDisplacements(use_local_displacements);
    cur_region.setUsePostDisplacements(use_post_displacements);

    // Setup active or inactive uv swaps
    cur_region.setUseUvWarp(uv_swap_table[index].getEnabled());

    index++;
  }
};

// Runs a single step of the animation for a given delta timestep
CreatureManager.prototype.Update = function(delta)
{
  if(!this.is_playing)
  {
    return;
  }

  this.increRunTime(delta * this.time_scale);
  
  if(this.do_auto_blending) {
  	this.ProcessAutoBlending();
  	this.IncreAutoBlendRunTimes(delta * this.time_scale);
  }

  this.RunCreature();
};

CreatureManager.prototype.RunAtTime = function(time_in)
{
  if(!this.is_playing)
  {
    return;
  }

  this.setRunTime(time_in);
  this.RunCreature ();
};

CreatureManager.prototype.RunCreature = function()
{
  if(this.do_blending)
  {
    for(var i = 0; i < 2; i++) {
      cur_animation_name = this.active_blend_animation_names[i];
      var cur_animation = this.animations[this.active_blend_animation_names[i]];
      cur_animation_run_time = this.active_blend_run_times[cur_animation_name];

      if(cur_animation.cache_pts.length > 0)
      {
        this.UpdateRegionsSwitches(cur_animation_name);
      	cur_animation.poseFromCachePts(cur_animation_run_time, this.blend_render_pts[i], this.target_creature.total_num_pts);
      }
      else {
        this.UpdateRegionsSwitches(cur_animation_name);
	    	this.PoseCreature(this.active_blend_animation_names[i], this.blend_render_pts[i], cur_animation_run_time);
  	  }
    }

    for(var j = 0; j < this.target_creature.total_num_pts * 3; j++)
    {
      var set_data_index = j;
      var read_data_1 = this.blend_render_pts[0][j];
      var read_data_2 = this.blend_render_pts[1][j];
      /*
         target_creature.render_pts[set_data_index] =
         ((1.0f - blending_factor) * (read_data_1)) +
         (blending_factor * (read_data_2));
       */
      this.target_creature.render_pts[set_data_index] =
          ((1.0 - this.blending_factor) * (read_data_1)) +
          (this.blending_factor * (read_data_2));

    }
  }
  else {
    var cur_animation = this.animations[this.active_animation_name];
    if(cur_animation.cache_pts.length > 0)
    {
    	cur_animation.poseFromCachePts(this.getRunTime(), this.target_creature.render_pts, this.target_creature.total_num_pts);
    	// cur_animation->poseFromCachePts(getRunTime(), target_creature->GetRenderPts(), target_creature->GetTotalNumPoints());
    }
    else {
		  this.PoseCreature(this.active_animation_name, this.target_creature.render_pts, this.getRunTime());
	  }
  }
  
  this.RunUVItemSwap();
};

function isDictEmpty(ob){
   for(var i in ob){ return false;}
  return true;
}


CreatureManager.prototype.RunUVItemSwap = function()
{
	var render_composition =
    	this.target_creature.render_composition;
	var regions_map =
	    render_composition.getRegionsMap();
	
	var swap_packets = this.target_creature.uv_swap_packets;
	var active_swap_actions = this.target_creature.active_uv_swap_actions;

	if (isDictEmpty(swap_packets) || isDictEmpty(active_swap_actions))
	{
		return;
	}

	for(var cur_action_key in active_swap_actions)
	{
		if (cur_action_key in regions_map)
		{
			var swap_tag = active_swap_actions[cur_action_key];
			var swap_list = swap_packets[cur_action_key];
			for(var j = 0; j < swap_list.length; j++)
			{
				var cur_item = swap_list[j];
				
				if (cur_item.tag == swap_tag)
				{
					// Perfrom UV Item Swap
					var cur_region = regions_map[cur_action_key];
					cur_region.setUvWarpLocalOffset(cur_item.local_offset);
					cur_region.setUvWarpGlobalOffset(cur_item.global_offset);
					cur_region.setUvWarpScale(cur_item.scale);
					cur_region.runUvWarp();

					break;
				}
			}
		}
	}
};

CreatureManager.prototype.AlterBonesByAnchor = function(bones_map, animation_name_in)
{
	if(this.target_creature.anchor_points_active == false)
	{
		return;
	}
	
	var anchor_point = this.target_creature.GetAnchorPoint(animation_name_in);
	for(var cur_bone_key in bones_map)
	{
		var cur_bone = bones_map[cur_bone_key];
		var start_pt = cur_bone.getWorldStartPt();
		var end_pt = cur_bone.getWorldEndPt();
		
		start_pt = vec3.subtract(start_pt, start_pt, vec3.fromValues(anchor_point[0], anchor_point[1], 0));
		end_pt = vec3.subtract(end_pt, end_pt, vec3.fromValues(anchor_point[0], anchor_point[1], 0));
		
		cur_bone.setWorldStartPt(start_pt);
		cur_bone.setWorldEndPt(end_pt);
	}
};

// Sets scaling for time
CreatureManager.prototype.SetTimeScale = function(scale_in)
{
  this.time_scale = scale_in;
};

// Enables/Disables blending
CreatureManager.prototype.SetBlending = function(flag_in)
{
  this.do_blending = flag_in;

  if (this.do_blending) {
    if (this.blend_render_pts[0].length == 0) {
      var new_vec = [];
      for(var i = 0; i < this.target_creature.total_num_pts * 3; i++)
      {
        new_vec.push(0);
      }

      this.blend_render_pts.push(new_vec);
    }

    if (this.blend_render_pts[1].length == 0) {
      var new_vec = [];
      for(var i = 0; i < this.target_creature.total_num_pts * 3; i++)
      {
        new_vec.push(0);
      }

      this.blend_render_pts[1] = new_vec;
    }

  }
};

// Sets auto blending
CreatureManager.prototype.SetAutoBlending = function(flag_in)
{
	this.do_auto_blending = flag_in;
	this.SetBlending(flag_in);
			
	if(this.do_auto_blending)
	{
		this.AutoBlendTo(this.active_animation_name, 0.1);
	}	
};

// Use auto blending to blend to the next animation
CreatureManager.prototype.AutoBlendTo = function(animation_name_in, blend_delta)
{
	if(animation_name_in == this.auto_blend_names[1])
	{
		// already blending to that so just return
		return;
	}
			
	this.ResetBlendTime(animation_name_in);
			
	this.auto_blend_delta = blend_delta;
	this.auto_blend_names[0] = this.active_animation_name;
	this.auto_blend_names[1] = animation_name_in;
	this.blending_factor = 0;
			
	this.active_animation_name = animation_name_in;
			
	this.SetBlendingAnimations(this.auto_blend_names[0], this.auto_blend_names[1]);
};

CreatureManager.prototype.ResetBlendTime = function(name_in)
{
	cur_animation = this.animations[name_in];
	this.active_blend_run_times[name_in] = cur_animation.start_time;
};

// Sets blending animation names
CreatureManager.prototype.SetBlendingAnimations = function(name_1, name_2)
{
  this.active_blend_animation_names[0] = name_1;
  this.active_blend_animation_names[1] = name_2;
};

// Sets the blending factor
CreatureManager.prototype.SetBlendingFactor = function(value_in)
{
  this.blending_factor = value_in;
};

// Given a set of coordinates in local creature space,
// see if any bone is in contact
CreatureManager.prototype.IsContactBone = function(pt_in, radius)
{
  var cur_bone = this.target_creature.render_composition.getRootBone();
  return this.ProcessContactBone(pt_in, radius, cur_bone);
};


CreatureManager.prototype.PoseCreature = function(animation_name_in, target_pts, input_run_time)
{
  var cur_animation = this.animations[animation_name_in];

  var bone_cache_manager = cur_animation.bones_cache;
  var displacement_cache_manager = cur_animation.displacement_cache;
  var uv_warp_cache_manager = cur_animation.uv_warp_cache;
  var opacity_cache_manager = cur_animation.opacity_cache;

  var render_composition =
    this.target_creature.render_composition;

  // Extract values from caches
  var bones_map =
    render_composition.getBonesMap();
  var regions_map =
    render_composition.getRegionsMap();

  bone_cache_manager.retrieveValuesAtTime(input_run_time,
      bones_map);
      
  this.AlterBonesByAnchor(bones_map, animation_name_in);
      
  if(this.bones_override_callback != null)
  {
  	this.bones_override_callback(bones_map);
  }

  displacement_cache_manager.retrieveValuesAtTime(input_run_time,
      regions_map);
  uv_warp_cache_manager.retrieveValuesAtTime(input_run_time,
      regions_map);
  opacity_cache_manager.retrieveValuesAtTime(input_run_time,
			                                 regions_map);

  // Do posing, decide if we are blending or not
  var cur_regions =
    render_composition.getRegions();
  var cur_bones =
    render_composition.getBonesMap();

  render_composition.updateAllTransforms(false);
  for(var j = 0, l = cur_regions.length; j < l; j++) {
    var cur_region = cur_regions[j];

    var cur_pt_index = cur_region.getStartPtIndex();


    cur_region.poseFinalPts(target_pts,
        cur_pt_index * 3,
        cur_bones);

    // add in z offsets for different regions
    
    var start = cur_region.getStartPtIndex() * 3;
    var end = cur_region.getEndPtIndex() * 3;
    for(var k = start;
       k <= end;
       k+=3)
    {
       target_pts[k + 2] = -j * 0.001;
    }
     
  }
};