echarts-gl
Version:
Extension pack of ECharts providing 3D plots and globe visualization
2,051 lines (1,849 loc) • 2.1 MB
JavaScript
(function webpackUniversalModuleDefinition(root, factory) {
if(typeof exports === 'object' && typeof module === 'object')
module.exports = factory(require("echarts"));
else if(typeof define === 'function' && define.amd)
define(["echarts"], factory);
else if(typeof exports === 'object')
exports["echarts-gl"] = factory(require("echarts"));
else
root["echarts-gl"] = factory(root["echarts"]);
})(self, function(__WEBPACK_EXTERNAL_MODULE_echarts_lib_echarts__) {
return /******/ (() => { // webpackBootstrap
/******/ "use strict";
/******/ var __webpack_modules__ = ({
/***/ "./src/export/all.js":
/*!*****************************************!*\
!*** ./src/export/all.js + 362 modules ***!
\*****************************************/
/***/ ((__unused_webpack_module, __webpack_exports__, __webpack_require__) => {
// ESM COMPAT FLAG
__webpack_require__.r(__webpack_exports__);
// EXTERNAL MODULE: external "echarts"
var external_echarts_ = __webpack_require__("echarts/lib/echarts");
;// CONCATENATED MODULE: ./node_modules/claygl/src/core/mixin/extend.js
/**
* Extend a sub class from base class
* @param {object|Function} makeDefaultOpt default option of this sub class, method of the sub can use this.xxx to access this option
* @param {Function} [initialize] Initialize after the sub class is instantiated
* @param {Object} [proto] Prototype methods/properties of the sub class
* @memberOf clay.core.mixin.extend
* @return {Function}
*/
function derive(makeDefaultOpt, initialize/*optional*/, proto/*optional*/) {
if (typeof initialize == 'object') {
proto = initialize;
initialize = null;
}
var _super = this;
var propList;
if (!(makeDefaultOpt instanceof Function)) {
// Optimize the property iterate if it have been fixed
propList = [];
for (var propName in makeDefaultOpt) {
if (makeDefaultOpt.hasOwnProperty(propName)) {
propList.push(propName);
}
}
}
var sub = function(options) {
// call super constructor
_super.apply(this, arguments);
if (makeDefaultOpt instanceof Function) {
// Invoke makeDefaultOpt each time if it is a function, So we can make sure each
// property in the object will not be shared by mutiple instances
extend(this, makeDefaultOpt.call(this, options));
}
else {
extendWithPropList(this, makeDefaultOpt, propList);
}
if (this.constructor === sub) {
// Initialize function will be called in the order of inherit
var initializers = sub.__initializers__;
for (var i = 0; i < initializers.length; i++) {
initializers[i].apply(this, arguments);
}
}
};
// save super constructor
sub.__super__ = _super;
// Initialize function will be called after all the super constructor is called
if (!_super.__initializers__) {
sub.__initializers__ = [];
} else {
sub.__initializers__ = _super.__initializers__.slice();
}
if (initialize) {
sub.__initializers__.push(initialize);
}
var Ctor = function() {};
Ctor.prototype = _super.prototype;
sub.prototype = new Ctor();
sub.prototype.constructor = sub;
extend(sub.prototype, proto);
// extend the derive method as a static method;
sub.extend = _super.extend;
// DEPCRATED
sub.derive = _super.extend;
return sub;
}
function extend(target, source) {
if (!source) {
return;
}
for (var name in source) {
if (source.hasOwnProperty(name)) {
target[name] = source[name];
}
}
}
function extendWithPropList(target, source, propList) {
for (var i = 0; i < propList.length; i++) {
var propName = propList[i];
target[propName] = source[propName];
}
}
/**
* @alias clay.core.mixin.extend
* @mixin
*/
/* harmony default export */ const mixin_extend = ({
extend: derive,
// DEPCRATED
derive: derive
});
;// CONCATENATED MODULE: ./node_modules/claygl/src/core/mixin/notifier.js
function Handler(action, context) {
this.action = action;
this.context = context;
}
/**
* @mixin
* @alias clay.core.mixin.notifier
*/
var notifier = {
/**
* Trigger event
* @param {string} name
*/
trigger: function(name) {
if (!this.hasOwnProperty('__handlers__')) {
return;
}
if (!this.__handlers__.hasOwnProperty(name)) {
return;
}
var hdls = this.__handlers__[name];
var l = hdls.length, i = -1, args = arguments;
// Optimize advise from backbone
switch (args.length) {
case 1:
while (++i < l) {
hdls[i].action.call(hdls[i].context);
}
return;
case 2:
while (++i < l) {
hdls[i].action.call(hdls[i].context, args[1]);
}
return;
case 3:
while (++i < l) {
hdls[i].action.call(hdls[i].context, args[1], args[2]);
}
return;
case 4:
while (++i < l) {
hdls[i].action.call(hdls[i].context, args[1], args[2], args[3]);
}
return;
case 5:
while (++i < l) {
hdls[i].action.call(hdls[i].context, args[1], args[2], args[3], args[4]);
}
return;
default:
while (++i < l) {
hdls[i].action.apply(hdls[i].context, Array.prototype.slice.call(args, 1));
}
return;
}
},
/**
* Register event handler
* @param {string} name
* @param {Function} action
* @param {Object} [context]
* @chainable
*/
on: function(name, action, context) {
if (!name || !action) {
return;
}
var handlers = this.__handlers__ || (this.__handlers__={});
if (!handlers[name]) {
handlers[name] = [];
}
else {
if (this.has(name, action)) {
return;
}
}
var handler = new Handler(action, context || this);
handlers[name].push(handler);
return this;
},
/**
* Register event, event will only be triggered once and then removed
* @param {string} name
* @param {Function} action
* @param {Object} [context]
* @chainable
*/
once: function(name, action, context) {
if (!name || !action) {
return;
}
var self = this;
function wrapper() {
self.off(name, wrapper);
action.apply(this, arguments);
}
return this.on(name, wrapper, context);
},
/**
* Alias of once('before' + name)
* @param {string} name
* @param {Function} action
* @param {Object} [context]
* @chainable
*/
before: function(name, action, context) {
if (!name || !action) {
return;
}
name = 'before' + name;
return this.on(name, action, context);
},
/**
* Alias of once('after' + name)
* @param {string} name
* @param {Function} action
* @param {Object} [context]
* @chainable
*/
after: function(name, action, context) {
if (!name || !action) {
return;
}
name = 'after' + name;
return this.on(name, action, context);
},
/**
* Alias of on('success')
* @param {Function} action
* @param {Object} [context]
* @chainable
*/
success: function(action, context) {
return this.once('success', action, context);
},
/**
* Alias of on('error')
* @param {Function} action
* @param {Object} [context]
* @chainable
*/
error: function(action, context) {
return this.once('error', action, context);
},
/**
* Remove event listener
* @param {Function} action
* @param {Object} [context]
* @chainable
*/
off: function(name, action) {
var handlers = this.__handlers__ || (this.__handlers__={});
if (!action) {
handlers[name] = [];
return;
}
if (handlers[name]) {
var hdls = handlers[name];
var retains = [];
for (var i = 0; i < hdls.length; i++) {
if (action && hdls[i].action !== action) {
retains.push(hdls[i]);
}
}
handlers[name] = retains;
}
return this;
},
/**
* If registered the event handler
* @param {string} name
* @param {Function} action
* @return {boolean}
*/
has: function(name, action) {
var handlers = this.__handlers__;
if (! handlers ||
! handlers[name]) {
return false;
}
var hdls = handlers[name];
for (var i = 0; i < hdls.length; i++) {
if (hdls[i].action === action) {
return true;
}
}
}
};
/* harmony default export */ const mixin_notifier = (notifier);
;// CONCATENATED MODULE: ./node_modules/claygl/src/core/util.js
var guid = 0;
var ArrayProto = Array.prototype;
var nativeForEach = ArrayProto.forEach;
/**
* Util functions
* @namespace clay.core.util
*/
var util = {
/**
* Generate GUID
* @return {number}
* @memberOf clay.core.util
*/
genGUID: function () {
return ++guid;
},
/**
* Relative path to absolute path
* @param {string} path
* @param {string} basePath
* @return {string}
* @memberOf clay.core.util
*/
relative2absolute: function (path, basePath) {
if (!basePath || path.match(/^\//)) {
return path;
}
var pathParts = path.split('/');
var basePathParts = basePath.split('/');
var item = pathParts[0];
while(item === '.' || item === '..') {
if (item === '..') {
basePathParts.pop();
}
pathParts.shift();
item = pathParts[0];
}
return basePathParts.join('/') + '/' + pathParts.join('/');
},
/**
* Extend target with source
* @param {Object} target
* @param {Object} source
* @return {Object}
* @memberOf clay.core.util
*/
extend: function (target, source) {
if (source) {
for (var name in source) {
if (source.hasOwnProperty(name)) {
target[name] = source[name];
}
}
}
return target;
},
/**
* Extend properties to target if not exist.
* @param {Object} target
* @param {Object} source
* @return {Object}
* @memberOf clay.core.util
*/
defaults: function (target, source) {
if (source) {
for (var propName in source) {
if (target[propName] === undefined) {
target[propName] = source[propName];
}
}
}
return target;
},
/**
* Extend properties with a given property list to avoid for..in.. iteration.
* @param {Object} target
* @param {Object} source
* @param {Array.<string>} propList
* @return {Object}
* @memberOf clay.core.util
*/
extendWithPropList: function (target, source, propList) {
if (source) {
for (var i = 0; i < propList.length; i++) {
var propName = propList[i];
target[propName] = source[propName];
}
}
return target;
},
/**
* Extend properties to target if not exist. With a given property list avoid for..in.. iteration.
* @param {Object} target
* @param {Object} source
* @param {Array.<string>} propList
* @return {Object}
* @memberOf clay.core.util
*/
defaultsWithPropList: function (target, source, propList) {
if (source) {
for (var i = 0; i < propList.length; i++) {
var propName = propList[i];
if (target[propName] == null) {
target[propName] = source[propName];
}
}
}
return target;
},
/**
* @param {Object|Array} obj
* @param {Function} iterator
* @param {Object} [context]
* @memberOf clay.core.util
*/
each: function (obj, iterator, context) {
if (!(obj && iterator)) {
return;
}
if (obj.forEach && obj.forEach === nativeForEach) {
obj.forEach(iterator, context);
}
else if (obj.length === + obj.length) {
for (var i = 0, len = obj.length; i < len; i++) {
iterator.call(context, obj[i], i, obj);
}
}
else {
for (var key in obj) {
if (obj.hasOwnProperty(key)) {
iterator.call(context, obj[key], key, obj);
}
}
}
},
/**
* Is object
* @param {} obj
* @return {boolean}
* @memberOf clay.core.util
*/
isObject: function (obj) {
return obj === Object(obj);
},
/**
* Is array ?
* @param {} obj
* @return {boolean}
* @memberOf clay.core.util
*/
isArray: function (obj) {
return Array.isArray(obj);
},
/**
* Is array like, which have a length property
* @param {} obj
* @return {boolean}
* @memberOf clay.core.util
*/
isArrayLike: function (obj) {
if (!obj) {
return false;
}
else {
return obj.length === + obj.length;
}
},
/**
* @param {} obj
* @return {}
* @memberOf clay.core.util
*/
clone: function (obj) {
if (!util.isObject(obj)) {
return obj;
}
else if (util.isArray(obj)) {
return obj.slice();
}
else if (util.isArrayLike(obj)) { // is typed array
var ret = new obj.constructor(obj.length);
for (var i = 0; i < obj.length; i++) {
ret[i] = obj[i];
}
return ret;
}
else {
return util.extend({}, obj);
}
}
};
/* harmony default export */ const core_util = (util);
;// CONCATENATED MODULE: ./node_modules/claygl/src/core/Base.js
/**
* Base class of all objects
* @constructor
* @alias clay.core.Base
* @mixes clay.core.mixin.notifier
*/
var Base = function () {
/**
* @type {number}
*/
this.__uid__ = core_util.genGUID();
};
Base.__initializers__ = [
function (opts) {
core_util.extend(this, opts);
}
];
core_util.extend(Base, mixin_extend);
core_util.extend(Base.prototype, mixin_notifier);
/* harmony default export */ const core_Base = (Base);
;// CONCATENATED MODULE: ./node_modules/claygl/src/glmatrix/common.js
var GLMAT_EPSILON = 0.000001;
// Use Array instead of Float32Array. It seems to be much faster and higher precision.
var GLMAT_ARRAY_TYPE = Array;
// if(!GLMAT_ARRAY_TYPE) {
// GLMAT_ARRAY_TYPE = (typeof Float32Array !== 'undefined') ? Float32Array : Array;
// }
var common_GLMAT_RANDOM = Math.random;
;// CONCATENATED MODULE: ./node_modules/claygl/src/glmatrix/vec3.js
/* Copyright (c) 2013, Brandon Jones, Colin MacKenzie IV. All rights reserved.
Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */
/**
* @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 GLMAT_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 GLMAT_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 GLMAT_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;
/**
* 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;
};
/**
* 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;
};
/**
* 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 = common_GLMAT_RANDOM() * 2.0 * Math.PI;
var z = (common_GLMAT_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);
}
};
/* harmony default export */ const glmatrix_vec3 = (vec3);
;// CONCATENATED MODULE: ./node_modules/claygl/src/math/Vector3.js
/**
* @constructor
* @alias clay.Vector3
* @param {number} x
* @param {number} y
* @param {number} z
*/
var Vector3 = function(x, y, z) {
x = x || 0;
y = y || 0;
z = z || 0;
/**
* Storage of Vector3, read and write of x, y, z will change the values in array
* All methods also operate on the array instead of x, y, z components
* @name array
* @type {Float32Array}
* @memberOf clay.Vector3#
*/
this.array = glmatrix_vec3.fromValues(x, y, z);
/**
* Dirty flag is used by the Node to determine
* if the matrix is updated to latest
* @name _dirty
* @type {boolean}
* @memberOf clay.Vector3#
*/
this._dirty = true;
};
Vector3.prototype = {
constructor: Vector3,
/**
* Add b to self
* @param {clay.Vector3} b
* @return {clay.Vector3}
*/
add: function (b) {
glmatrix_vec3.add(this.array, this.array, b.array);
this._dirty = true;
return this;
},
/**
* Set x, y and z components
* @param {number} x
* @param {number} y
* @param {number} z
* @return {clay.Vector3}
*/
set: function (x, y, z) {
this.array[0] = x;
this.array[1] = y;
this.array[2] = z;
this._dirty = true;
return this;
},
/**
* Set x, y and z components from array
* @param {Float32Array|number[]} arr
* @return {clay.Vector3}
*/
setArray: function (arr) {
this.array[0] = arr[0];
this.array[1] = arr[1];
this.array[2] = arr[2];
this._dirty = true;
return this;
},
/**
* Clone a new Vector3
* @return {clay.Vector3}
*/
clone: function () {
return new Vector3(this.x, this.y, this.z);
},
/**
* Copy from b
* @param {clay.Vector3} b
* @return {clay.Vector3}
*/
copy: function (b) {
glmatrix_vec3.copy(this.array, b.array);
this._dirty = true;
return this;
},
/**
* Cross product of self and b, written to a Vector3 out
* @param {clay.Vector3} a
* @param {clay.Vector3} b
* @return {clay.Vector3}
*/
cross: function (a, b) {
glmatrix_vec3.cross(this.array, a.array, b.array);
this._dirty = true;
return this;
},
/**
* Alias for distance
* @param {clay.Vector3} b
* @return {number}
*/
dist: function (b) {
return glmatrix_vec3.dist(this.array, b.array);
},
/**
* Distance between self and b
* @param {clay.Vector3} b
* @return {number}
*/
distance: function (b) {
return glmatrix_vec3.distance(this.array, b.array);
},
/**
* Alias for divide
* @param {clay.Vector3} b
* @return {clay.Vector3}
*/
div: function (b) {
glmatrix_vec3.div(this.array, this.array, b.array);
this._dirty = true;
return this;
},
/**
* Divide self by b
* @param {clay.Vector3} b
* @return {clay.Vector3}
*/
divide: function (b) {
glmatrix_vec3.divide(this.array, this.array, b.array);
this._dirty = true;
return this;
},
/**
* Dot product of self and b
* @param {clay.Vector3} b
* @return {number}
*/
dot: function (b) {
return glmatrix_vec3.dot(this.array, b.array);
},
/**
* Alias of length
* @return {number}
*/
len: function () {
return glmatrix_vec3.len(this.array);
},
/**
* Calculate the length
* @return {number}
*/
length: function () {
return glmatrix_vec3.length(this.array);
},
/**
* Linear interpolation between a and b
* @param {clay.Vector3} a
* @param {clay.Vector3} b
* @param {number} t
* @return {clay.Vector3}
*/
lerp: function (a, b, t) {
glmatrix_vec3.lerp(this.array, a.array, b.array, t);
this._dirty = true;
return this;
},
/**
* Minimum of self and b
* @param {clay.Vector3} b
* @return {clay.Vector3}
*/
min: function (b) {
glmatrix_vec3.min(this.array, this.array, b.array);
this._dirty = true;
return this;
},
/**
* Maximum of self and b
* @param {clay.Vector3} b
* @return {clay.Vector3}
*/
max: function (b) {
glmatrix_vec3.max(this.array, this.array, b.array);
this._dirty = true;
return this;
},
/**
* Alias for multiply
* @param {clay.Vector3} b
* @return {clay.Vector3}
*/
mul: function (b) {
glmatrix_vec3.mul(this.array, this.array, b.array);
this._dirty = true;
return this;
},
/**
* Mutiply self and b
* @param {clay.Vector3} b
* @return {clay.Vector3}
*/
multiply: function (b) {
glmatrix_vec3.multiply(this.array, this.array, b.array);
this._dirty = true;
return this;
},
/**
* Negate self
* @return {clay.Vector3}
*/
negate: function () {
glmatrix_vec3.negate(this.array, this.array);
this._dirty = true;
return this;
},
/**
* Normalize self
* @return {clay.Vector3}
*/
normalize: function () {
glmatrix_vec3.normalize(this.array, this.array);
this._dirty = true;
return this;
},
/**
* Generate random x, y, z components with a given scale
* @param {number} scale
* @return {clay.Vector3}
*/
random: function (scale) {
glmatrix_vec3.random(this.array, scale);
this._dirty = true;
return this;
},
/**
* Scale self
* @param {number} scale
* @return {clay.Vector3}
*/
scale: function (s) {
glmatrix_vec3.scale(this.array, this.array, s);
this._dirty = true;
return this;
},
/**
* Scale b and add to self
* @param {clay.Vector3} b
* @param {number} scale
* @return {clay.Vector3}
*/
scaleAndAdd: function (b, s) {
glmatrix_vec3.scaleAndAdd(this.array, this.array, b.array, s);
this._dirty = true;
return this;
},
/**
* Alias for squaredDistance
* @param {clay.Vector3} b
* @return {number}
*/
sqrDist: function (b) {
return glmatrix_vec3.sqrDist(this.array, b.array);
},
/**
* Squared distance between self and b
* @param {clay.Vector3} b
* @return {number}
*/
squaredDistance: function (b) {
return glmatrix_vec3.squaredDistance(this.array, b.array);
},
/**
* Alias for squaredLength
* @return {number}
*/
sqrLen: function () {
return glmatrix_vec3.sqrLen(this.array);
},
/**
* Squared length of self
* @return {number}
*/
squaredLength: function () {
return glmatrix_vec3.squaredLength(this.array);
},
/**
* Alias for subtract
* @param {clay.Vector3} b
* @return {clay.Vector3}
*/
sub: function (b) {
glmatrix_vec3.sub(this.array, this.array, b.array);
this._dirty = true;
return this;
},
/**
* Subtract b from self
* @param {clay.Vector3} b
* @return {clay.Vector3}
*/
subtract: function (b) {
glmatrix_vec3.subtract(this.array, this.array, b.array);
this._dirty = true;
return this;
},
/**
* Transform self with a Matrix3 m
* @param {clay.Matrix3} m
* @return {clay.Vector3}
*/
transformMat3: function (m) {
glmatrix_vec3.transformMat3(this.array, this.array, m.array);
this._dirty = true;
return this;
},
/**
* Transform self with a Matrix4 m
* @param {clay.Matrix4} m
* @return {clay.Vector3}
*/
transformMat4: function (m) {
glmatrix_vec3.transformMat4(this.array, this.array, m.array);
this._dirty = true;
return this;
},
/**
* Transform self with a Quaternion q
* @param {clay.Quaternion} q
* @return {clay.Vector3}
*/
transformQuat: function (q) {
glmatrix_vec3.transformQuat(this.array, this.array, q.array);
this._dirty = true;
return this;
},
/**
* Trasnform self into projection space with m
* @param {clay.Matrix4} m
* @return {clay.Vector3}
*/
applyProjection: function (m) {
var v = this.array;
m = m.array;
// Perspective projection
if (m[15] === 0) {
var w = -1 / v[2];
v[0] = m[0] * v[0] * w;
v[1] = m[5] * v[1] * w;
v[2] = (m[10] * v[2] + m[14]) * w;
}
else {
v[0] = m[0] * v[0] + m[12];
v[1] = m[5] * v[1] + m[13];
v[2] = m[10] * v[2] + m[14];
}
this._dirty = true;
return this;
},
eulerFromQuat: function(q, order) {
Vector3.eulerFromQuat(this, q, order);
},
eulerFromMat3: function (m, order) {
Vector3.eulerFromMat3(this, m, order);
},
toString: function() {
return '[' + Array.prototype.join.call(this.array, ',') + ']';
},
toArray: function () {
return Array.prototype.slice.call(this.array);
}
};
var defineProperty = Object.defineProperty;
// Getter and Setter
if (defineProperty) {
var proto = Vector3.prototype;
/**
* @name x
* @type {number}
* @memberOf clay.Vector3
* @instance
*/
defineProperty(proto, 'x', {
get: function () {
return this.array[0];
},
set: function (value) {
this.array[0] = value;
this._dirty = true;
}
});
/**
* @name y
* @type {number}
* @memberOf clay.Vector3
* @instance
*/
defineProperty(proto, 'y', {
get: function () {
return this.array[1];
},
set: function (value) {
this.array[1] = value;
this._dirty = true;
}
});
/**
* @name z
* @type {number}
* @memberOf clay.Vector3
* @instance
*/
defineProperty(proto, 'z', {
get: function () {
return this.array[2];
},
set: function (value) {
this.array[2] = value;
this._dirty = true;
}
});
}
// Supply methods that are not in place
/**
* @param {clay.Vector3} out
* @param {clay.Vector3} a
* @param {clay.Vector3} b
* @return {clay.Vector3}
*/
Vector3.add = function(out, a, b) {
glmatrix_vec3.add(out.array, a.array, b.array);
out._dirty = true;
return out;
};
/**
* @param {clay.Vector3} out
* @param {number} x
* @param {number} y
* @param {number} z
* @return {clay.Vector3}
*/
Vector3.set = function(out, x, y, z) {
glmatrix_vec3.set(out.array, x, y, z);
out._dirty = true;
};
/**
* @param {clay.Vector3} out
* @param {clay.Vector3} b
* @return {clay.Vector3}
*/
Vector3.copy = function(out, b) {
glmatrix_vec3.copy(out.array, b.array);
out._dirty = true;
return out;
};
/**
* @param {clay.Vector3} out
* @param {clay.Vector3} a
* @param {clay.Vector3} b
* @return {clay.Vector3}
*/
Vector3.cross = function(out, a, b) {
glmatrix_vec3.cross(out.array, a.array, b.array);
out._dirty = true;
return out;
};
/**
* @param {clay.Vector3} a
* @param {clay.Vector3} b
* @return {number}
*/
Vector3.dist = function(a, b) {
return glmatrix_vec3.distance(a.array, b.array);
};
/**
* @function
* @param {clay.Vector3} a
* @param {clay.Vector3} b
* @return {number}
*/
Vector3.distance = Vector3.dist;
/**
* @param {clay.Vector3} out
* @param {clay.Vector3} a
* @param {clay.Vector3} b
* @return {clay.Vector3}
*/
Vector3.div = function(out, a, b) {
glmatrix_vec3.divide(out.array, a.array, b.array);
out._dirty = true;
return out;
};
/**
* @function
* @param {clay.Vector3} out
* @param {clay.Vector3} a
* @param {clay.Vector3} b
* @return {clay.Vector3}
*/
Vector3.divide = Vector3.div;
/**
* @param {clay.Vector3} a
* @param {clay.Vector3} b
* @return {number}
*/
Vector3.dot = function(a, b) {
return glmatrix_vec3.dot(a.array, b.array);
};
/**
* @param {clay.Vector3} a
* @return {number}
*/
Vector3.len = function(b) {
return glmatrix_vec3.length(b.array);
};
// Vector3.length = Vector3.len;
/**
* @param {clay.Vector3} out
* @param {clay.Vector3} a
* @param {clay.Vector3} b
* @param {number} t
* @return {clay.Vector3}
*/
Vector3.lerp = function(out, a, b, t) {
glmatrix_vec3.lerp(out.array, a.array, b.array, t);
out._dirty = true;
return out;
};
/**
* @param {clay.Vector3} out
* @param {clay.Vector3} a
* @param {clay.Vector3} b
* @return {clay.Vector3}
*/
Vector3.min = function(out, a, b) {
glmatrix_vec3.min(out.array, a.array, b.array);
out._dirty = true;
return out;
};
/**
* @param {clay.Vector3} out
* @param {clay.Vector3} a
* @param {clay.Vector3} b
* @return {clay.Vector3}
*/
Vector3.max = function(out, a, b) {
glmatrix_vec3.max(out.array, a.array, b.array);
out._dirty = true;
return out;
};
/**
* @param {clay.Vector3} out
* @param {clay.Vector3} a
* @param {clay.Vector3} b
* @return {clay.Vector3}
*/
Vector3.mul = function(out, a, b) {
glmatrix_vec3.multiply(out.array, a.array, b.array);
out._dirty = true;
return out;
};
/**
* @function
* @param {clay.Vector3} out
* @param {clay.Vector3} a
* @param {clay.Vector3} b
* @return {clay.Vector3}
*/
Vector3.multiply = Vector3.mul;
/**
* @param {clay.Vector3} out
* @param {clay.Vector3} a
* @return {clay.Vector3}
*/
Vector3.negate = function(out, a) {
glmatrix_vec3.negate(out.array, a.array);
out._dirty = true;
return out;
};
/**
* @param {clay.Vector3} out
* @param {clay.Vector3} a
* @return {clay.Vector3}
*/
Vector3.normalize = function(out, a) {
glmatrix_vec3.normalize(out.array, a.array);
out._dirty = true;
return out;
};
/**
* @param {clay.Vector3} out
* @param {number} scale
* @return {clay.Vector3}
*/
Vector3.random = function(out, scale) {
glmatrix_vec3.random(out.array, scale);
out._dirty = true;
return out;
};
/**
* @param {clay.Vector3} out
* @param {clay.Vector3} a
* @param {number} scale
* @return {clay.Vector3}
*/
Vector3.scale = function(out, a, scale) {
glmatrix_vec3.scale(out.array, a.array, scale);
out._dirty = true;
return out;
};
/**
* @param {clay.Vector3} out
* @param {clay.Vector3} a
* @param {clay.Vector3} b
* @param {number} scale
* @return {clay.Vector3}
*/
Vector3.scaleAndAdd = function(out, a, b, scale) {
glmatrix_vec3.scaleAndAdd(out.array, a.array, b.array, scale);
out._dirty = true;
return out;
};
/**
* @param {clay.Vector3} a
* @param {clay.Vector3} b
* @return {number}
*/
Vector3.sqrDist = function(a, b) {
return glmatrix_vec3.sqrDist(a.array, b.array);
};
/**
* @function
* @param {clay.Vector3} a
* @param {clay.Vector3} b
* @return {number}
*/
Vector3.squaredDistance = Vector3.sqrDist;
/**
* @param {clay.Vector3} a
* @return {number}
*/
Vector3.sqrLen = function(a) {
return glmatrix_vec3.sqrLen(a.array);
};
/**
* @function
* @param {clay.Vector3} a
* @return {number}
*/
Vector3.squaredLength = Vector3.sqrLen;
/**
* @param {clay.Vector3} out
* @param {clay.Vector3} a
* @param {clay.Vector3} b
* @return {clay.Vector3}
*/
Vector3.sub = function(out, a, b) {
glmatrix_vec3.subtract(out.array, a.array, b.array);
out._dirty = true;
return out;
};
/**
* @function
* @param {clay.Vector3} out
* @param {clay.Vector3} a
* @param {clay.Vector3} b
* @return {clay.Vector3}
*/
Vector3.subtract = Vector3.sub;
/**
* @param {clay.Vector3} out
* @param {clay.Vector3} a
* @param {Matrix3} m
* @return {clay.Vector3}
*/
Vector3.transformMat3 = function(out, a, m) {
glmatrix_vec3.transformMat3(out.array, a.array, m.array);
out._dirty = true;
return out;
};
/**
* @param {clay.Vector3} out
* @param {clay.Vector3} a
* @param {clay.Matrix4} m
* @return {clay.Vector3}
*/
Vector3.transformMat4 = function(out, a, m) {
glmatrix_vec3.transformMat4(out.array, a.array, m.array);
out._dirty = true;
return out;
};
/**
* @param {clay.Vector3} out
* @param {clay.Vector3} a
* @param {clay.Quaternion} q
* @return {clay.Vector3}
*/
Vector3.transformQuat = function(out, a, q) {
glmatrix_vec3.transformQuat(out.array, a.array, q.array);
out._dirty = true;
return out;
};
function clamp(val, min, max) {
return val < min ? min : (val > max ? max : val);
}
var atan2 = Math.atan2;
var asin = Math.asin;
var abs = Math.abs;
/**
* Convert quaternion to euler angle
* Quaternion must be normalized
* From three.js
*/
Vector3.eulerFromQuat = function (out, q, order) {
out._dirty = true;
q = q.array;
var target = out.array;
var x = q[0], y = q[1], z = q[2], w = q[3];
var x2 = x * x;
var y2 = y * y;
var z2 = z * z;
var w2 = w * w;
var order = (order || 'XYZ').toUpperCase();
switch (order) {
case 'XYZ':
target[0] = atan2(2 * (x * w - y * z), (w2 - x2 - y2 + z2));
target[1] = asin(clamp(2 * (x * z + y * w), - 1, 1));
target[2] = atan2(2 * (z * w - x * y), (w2 + x2 - y2 - z2));
break;
case 'YXZ':
target[0] = asin(clamp(2 * (x * w - y * z), - 1, 1));
target[1] = atan2(2 * (x * z + y * w), (w2 - x2 - y2 + z2));
target[2] = atan2(2 * (x * y + z * w), (w2 - x2 + y2 - z2));
break;
case 'ZXY':
target[0] = asin(clamp(2 * (x * w + y * z), - 1, 1));
target[1] = atan2(2 * (y * w - z * x), (w2 - x2 - y2 + z2));
target[2] = atan2(2 * (z * w - x * y), (w2 - x2 + y2 - z2));
break;
case 'ZYX':
target[0] = atan2(2 * (x * w + z * y), (w2 - x2 - y2 + z2));
target[1] = asin(clamp(2 * (y * w - x * z), - 1, 1));
target[2] = atan2(2 * (x * y + z * w), (w2 + x2 - y2 - z2));
break;
case 'YZX':
target[0] = atan2(2 * (x * w - z * y), (w2 - x2 + y2 - z2));