From 75f561d79665fb39151e37a4309a8eacf2053699 Mon Sep 17 00:00:00 2001 From: Leslie Leigh Date: Thu, 6 Sep 2018 14:21:34 +0800 Subject: [PATCH] add anisotropy(from 3d) and mipFilter(from 3d) properties to texture2d;upload 3d vmath (#3205) --- cocos/assets/CCTexture2D.js | 70 +- cocos/vmath/bits.js | 307 +++++++ cocos/vmath/color3.js | 282 ++++++ cocos/vmath/color4.js | 305 ++++++ cocos/vmath/index.js | 19 + cocos/vmath/mat2.js | 443 +++++++++ cocos/vmath/mat23.js | 547 +++++++++++ cocos/vmath/mat3.js | 851 +++++++++++++++++ cocos/vmath/mat4.js | 1729 +++++++++++++++++++++++++++++++++++ cocos/vmath/quat.js | 808 ++++++++++++++++ cocos/vmath/utils.js | 217 +++++ cocos/vmath/vec2.js | 691 ++++++++++++++ cocos/vmath/vec3.js | 908 ++++++++++++++++++ cocos/vmath/vec4.js | 715 +++++++++++++++ 14 files changed, 7888 insertions(+), 4 deletions(-) create mode 100644 cocos/vmath/bits.js create mode 100644 cocos/vmath/color3.js create mode 100644 cocos/vmath/color4.js create mode 100644 cocos/vmath/mat2.js create mode 100644 cocos/vmath/mat23.js create mode 100644 cocos/vmath/mat3.js create mode 100644 cocos/vmath/mat4.js create mode 100644 cocos/vmath/quat.js create mode 100644 cocos/vmath/utils.js create mode 100644 cocos/vmath/vec2.js create mode 100644 cocos/vmath/vec3.js create mode 100644 cocos/vmath/vec4.js diff --git a/cocos/assets/CCTexture2D.js b/cocos/assets/CCTexture2D.js index 50a13d20e5..0afe559ece 100644 --- a/cocos/assets/CCTexture2D.js +++ b/cocos/assets/CCTexture2D.js @@ -215,6 +215,7 @@ let _sharedOpts = { height: undefined, minFilter: undefined, magFilter: undefined, + mipFilter: undefined, wrapS: undefined, wrapT: undefined, format: undefined, @@ -222,7 +223,8 @@ let _sharedOpts = { images: undefined, image: undefined, flipY: undefined, - premultiplyAlpha: undefined + premultiplyAlpha: undefined, + anisotropy: undefined }; function _getSharedOptions () { for (var key in _sharedOpts) { @@ -268,8 +270,10 @@ var Texture2D = cc.Class({ _flipY: false, _minFilter: Filter.LINEAR, _magFilter: Filter.LINEAR, + _mipFilter: Filter.LINEAR, _wrapS: WrapMode.CLAMP_TO_EDGE, - _wrapT: WrapMode.CLAMP_TO_EDGE + _wrapT: WrapMode.CLAMP_TO_EDGE, + _anisotropy: 0 }, statics: { @@ -364,9 +368,11 @@ var Texture2D = cc.Class({ * @param {PixelFormat} options.format * @param {Filter} options.minFilter * @param {Filter} options.magFilter + * @param {Filter} options.mipFilter * @param {WrapMode} options.wrapS * @param {WrapMode} options.wrapT * @param {Boolean} options.premultiplyAlpha + * @param {Number} options.anisotropy */ update (options) { if (options) { @@ -385,6 +391,10 @@ var Texture2D = cc.Class({ this._magFilter = options.magFilter; options.magFilter = FilterIndex[options.magFilter]; } + if (options.mipFilter !== undefined) { + this._mipFilter = options.mipFilter; + options.mipFilter = FilterIndex[options.mipFilter]; + } if (options.wrapS !== undefined) { this._wrapS = options.wrapS; } @@ -402,6 +412,10 @@ var Texture2D = cc.Class({ this._premultiplyAlpha = options.premultiplyAlpha; updateImg = true; } + if (options.anisotropy !== undefined) { + this._anisotropy = options.anisotropy; + updateImg = true; + } if (options.mipmap !== undefined) { this._hasMipmap = options.mipmap; } @@ -475,9 +489,11 @@ var Texture2D = cc.Class({ opts.images = [opts.image]; opts.hasMipmap = this._hasMipmap; opts.premultiplyAlpha = this._premultiplyAlpha; + opts.anisotropy = this._anisotropy; opts.flipY = this._flipY; opts.minFilter = FilterIndex[this._minFilter]; opts.magFilter = FilterIndex[this._magFilter]; + opts.mipFilter = FilterIndex[this._mipFilter]; opts.wrapS = this._wrapS; opts.wrapT = this._wrapT; opts.format = pixelFormat; @@ -549,6 +565,16 @@ var Texture2D = cc.Class({ return this._premultiplyAlpha || false; }, + /** + * !#en Anisotropy of the texture. + * !#zh 获取纹理的各向异性。 + * @method getAnisotropy + * @return {Number} + */ + getAnisotropy() { + return this._anisotropy; + }, + /** * !#en * Whether or not use mipmap. @@ -583,9 +609,11 @@ var Texture2D = cc.Class({ opts.hasMipmap = this._hasMipmap; opts.format = this._format; opts.premultiplyAlpha = this._premultiplyAlpha; + opts.anisotropy = this._anisotropy; opts.flipY = this._flipY; opts.minFilter = FilterIndex[this._minFilter]; opts.magFilter = FilterIndex[this._magFilter]; + opts.mipFilter = FilterIndex[this._mipFilter]; opts.wrapS = this._wrapS; opts.wrapT = this._wrapT; @@ -666,6 +694,20 @@ var Texture2D = cc.Class({ } }, + /** + * !#en Sets the mipFilter options + * !#zh 设置纹理Mipmap过滤算法选项。 + * @method setMipFilter + * @param {Texture2D.Filter} mipFilter + */ + setMipFilter (mipFilter) { + if (this._mipFilter !== mipFilter) { + var opts = _getSharedOptions(); + opts.mipFilter = mipFilter; + this.update(opts); + } + }, + /** * !#en * Sets the flipY options @@ -695,6 +737,20 @@ var Texture2D = cc.Class({ this.update(opts); } }, + + /** + * !#en Sets the anisotropy of the texture + * !#zh 设置贴图的各向异性。 + * @method setAnisotropy + * @param {Boolean} anisotropy + */ + setAnisotropy(anisotropy) { + if (this._anisotropy != anisotropy) { + var opts = _getSharedOptions(); + opts.anisotropy = anisotropy; + this.update(opts); + } + }, /** * !#en @@ -742,7 +798,9 @@ var Texture2D = cc.Class({ let asset = "" + extId + "," + this._minFilter + "," + this._magFilter + "," + this._wrapS + "," + this._wrapT + "," + - (this._premultiplyAlpha ? 1 : 0); + (this._premultiplyAlpha ? 1 : 0) + "," + + this._mipFilter + "," + + this._anisotropy; return asset; }, @@ -785,7 +843,7 @@ var Texture2D = cc.Class({ this.url = url; } } - if (fields.length === 6) { + if (fields.length >= 6) { // decode filters this._minFilter = parseInt(fields[1]); this._magFilter = parseInt(fields[2]); @@ -795,6 +853,10 @@ var Texture2D = cc.Class({ // decode premultiply alpha this._premultiplyAlpha = fields[5].charCodeAt(0) === CHAR_CODE_1; } + if (fields.length >= 8) { + this._mipFilter = parseInt(fields[6]); + this._anisotropy = parseInt(fields[7]); + } } }); diff --git a/cocos/vmath/bits.js b/cocos/vmath/bits.js new file mode 100644 index 0000000000..c9aa4b49ff --- /dev/null +++ b/cocos/vmath/bits.js @@ -0,0 +1,307 @@ +/** + * Bit twiddling hacks for JavaScript. + * + * Author: Mikola Lysenko + * + * Ported from Stanford bit twiddling hack library: + * http://graphics.stanford.edu/~seander/bithacks.html + */ + +'use strict'; + +// Number of bits in an integer +/** + * @ignore + */ +export const INT_BITS = 32; +/** + * @ignore + */ +export const INT_MAX = 0x7fffffff; +/** + * @ignore + */ +export const INT_MIN = -1<<(INT_BITS-1); + +/** + * Returns -1, 0, +1 depending on sign of x + * + * @param {number} v + * @returns {number} + */ +export function sign(v) { + return (v > 0) - (v < 0); +} + +/** + * Computes absolute value of integer + * + * @param {number} v + * @returns {number} + */ +export function abs(v) { + let mask = v >> (INT_BITS-1); + return (v ^ mask) - mask; +} + +/** + * Computes minimum of integers x and y + * + * @param {number} x + * @param {number} y + * @returns {number} + */ +export function min(x, y) { + return y ^ ((x ^ y) & -(x < y)); +} + +/** + * Computes maximum of integers x and y + * + * @param {number} x + * @param {number} y + * @returns {number} + */ +export function max(x, y) { + return x ^ ((x ^ y) & -(x < y)); +} + +/** + * Checks if a number is a power of two + * + * @param {number} v + * @returns {boolean} + */ +export function isPow2(v) { + return !(v & (v-1)) && (!!v); +} + +/** + * Computes log base 2 of v + * + * @param {number} v + * @returns {number} + */ +export function log2(v) { + let r, shift; + r = (v > 0xFFFF) << 4; v >>>= r; + shift = (v > 0xFF ) << 3; v >>>= shift; r |= shift; + shift = (v > 0xF ) << 2; v >>>= shift; r |= shift; + shift = (v > 0x3 ) << 1; v >>>= shift; r |= shift; + return r | (v >> 1); +} + +/** + * Computes log base 10 of v + * + * @param {number} v + * @returns {number} + */ +export function log10(v) { + return (v >= 1000000000) ? 9 : (v >= 100000000) ? 8 : (v >= 10000000) ? 7 : + (v >= 1000000) ? 6 : (v >= 100000) ? 5 : (v >= 10000) ? 4 : + (v >= 1000) ? 3 : (v >= 100) ? 2 : (v >= 10) ? 1 : 0; +} + +/** + * Counts number of bits + * + * @param {number} v + * @returns {number} + */ +export function popCount(v) { + v = v - ((v >>> 1) & 0x55555555); + v = (v & 0x33333333) + ((v >>> 2) & 0x33333333); + return ((v + (v >>> 4) & 0xF0F0F0F) * 0x1010101) >>> 24; +} + +/** + * Counts number of trailing zeros + * + * @param {number} v + * @returns {number} + */ +export function countTrailingZeros(v) { + let c = 32; + v &= -v; + if (v) c--; + if (v & 0x0000FFFF) c -= 16; + if (v & 0x00FF00FF) c -= 8; + if (v & 0x0F0F0F0F) c -= 4; + if (v & 0x33333333) c -= 2; + if (v & 0x55555555) c -= 1; + return c; +} + +/** + * Rounds to next power of 2 + * + * @param {number} v + * @returns {number} + */ +export function nextPow2(v) { + v += v === 0; + --v; + v |= v >>> 1; + v |= v >>> 2; + v |= v >>> 4; + v |= v >>> 8; + v |= v >>> 16; + return v + 1; +} + +/** + * Rounds down to previous power of 2 + * + * @param {number} v + * @returns {number} + */ +export function prevPow2(v) { + v |= v >>> 1; + v |= v >>> 2; + v |= v >>> 4; + v |= v >>> 8; + v |= v >>> 16; + return v - (v>>>1); +} + +/** + * Computes parity of word + * + * @param {number} v + * @returns {number} + */ +export function parity(v) { + v ^= v >>> 16; + v ^= v >>> 8; + v ^= v >>> 4; + v &= 0xf; + return (0x6996 >>> v) & 1; +} + +/** + * @ignore + */ +const REVERSE_TABLE = new Array(256); + +(function(tab) { + for(let i=0; i<256; ++i) { + let v = i, r = i, s = 7; + for (v >>>= 1; v; v >>>= 1) { + r <<= 1; + r |= v & 1; + --s; + } + tab[i] = (r << s) & 0xff; + } +})(REVERSE_TABLE); + +/** + * Reverse bits in a 32 bit word + * + * @param {number} v + * @returns {number} + */ +export function reverse(v) { + return (REVERSE_TABLE[v & 0xff] << 24) | + (REVERSE_TABLE[(v >>> 8) & 0xff] << 16) | + (REVERSE_TABLE[(v >>> 16) & 0xff] << 8) | + REVERSE_TABLE[(v >>> 24) & 0xff]; +} + +/** + * Interleave bits of 2 coordinates with 16 bits. Useful for fast quadtree codes + * + * @param {number} x + * @param {number} y + * @returns {number} + */ +export function interleave2(x, y) { + x &= 0xFFFF; + x = (x | (x << 8)) & 0x00FF00FF; + x = (x | (x << 4)) & 0x0F0F0F0F; + x = (x | (x << 2)) & 0x33333333; + x = (x | (x << 1)) & 0x55555555; + + y &= 0xFFFF; + y = (y | (y << 8)) & 0x00FF00FF; + y = (y | (y << 4)) & 0x0F0F0F0F; + y = (y | (y << 2)) & 0x33333333; + y = (y | (y << 1)) & 0x55555555; + + return x | (y << 1); +} + +/** + * Extracts the nth interleaved component + * + * @param {number} v + * @param {number} n + * @returns {number} + */ +export function deinterleave2(v, n) { + v = (v >>> n) & 0x55555555; + v = (v | (v >>> 1)) & 0x33333333; + v = (v | (v >>> 2)) & 0x0F0F0F0F; + v = (v | (v >>> 4)) & 0x00FF00FF; + v = (v | (v >>> 16)) & 0x000FFFF; + return (v << 16) >> 16; +} + +/** + * Interleave bits of 3 coordinates, each with 10 bits. Useful for fast octree codes + * + * @param {number} x + * @param {number} y + * @param {number} z + * @returns {number} + */ +export function interleave3(x, y, z) { + x &= 0x3FF; + x = (x | (x<<16)) & 4278190335; + x = (x | (x<<8)) & 251719695; + x = (x | (x<<4)) & 3272356035; + x = (x | (x<<2)) & 1227133513; + + y &= 0x3FF; + y = (y | (y<<16)) & 4278190335; + y = (y | (y<<8)) & 251719695; + y = (y | (y<<4)) & 3272356035; + y = (y | (y<<2)) & 1227133513; + x |= (y << 1); + + z &= 0x3FF; + z = (z | (z<<16)) & 4278190335; + z = (z | (z<<8)) & 251719695; + z = (z | (z<<4)) & 3272356035; + z = (z | (z<<2)) & 1227133513; + + return x | (z << 2); +} + +/** + * Extracts nth interleaved component of a 3-tuple + * + * @param {number} v + * @param {number} n + * @returns {number} + */ +export function deinterleave3(v, n) { + v = (v >>> n) & 1227133513; + v = (v | (v>>>2)) & 3272356035; + v = (v | (v>>>4)) & 251719695; + v = (v | (v>>>8)) & 4278190335; + v = (v | (v>>>16)) & 0x3FF; + return (v<<22)>>22; +} + +/** + * Computes next combination in colexicographic order (this is mistakenly called nextPermutation on the bit twiddling hacks page) + * + * @param {number} v + * @returns {number} + */ +export function nextCombination(v) { + let t = v | (v - 1); + return (t + 1) | (((~t & -~t) - 1) >>> (countTrailingZeros(v) + 1)); +} \ No newline at end of file diff --git a/cocos/vmath/color3.js b/cocos/vmath/color3.js new file mode 100644 index 0000000000..aa3d80a3e4 --- /dev/null +++ b/cocos/vmath/color3.js @@ -0,0 +1,282 @@ +import { EPSILON } from './utils'; + +/** + * Represents a color with red(r), green(g), blue(b) component of that color. + */ +class color3 { + /** + * Creates a color, with components specified separately. + * + * @param {number} r - Value assigned to r component. + * @param {number} g - Value assigned to g component. + * @param {number} b - Value assigned to b component. + */ + constructor(r = 1, g = 1, b = 1) { + /** + * The r component. + * @type {number} + * */ + this.r = r; + + /** + * The g component. + * @type {number} + * */ + this.g = g; + + /** + * The b component. + * @type {number} + * */ + this.b = b; + } + + /** + * Creates a color, with components specified separately, or a black color if not specified + * + * @param {number} r - Value assigned to r component. + * @param {number} g - Value assigned to g component. + * @param {number} b - Value assigned to b component. + * @return {color3} The newly created color. + */ + static create(r = 1, g = 1, b = 1) { + return new color3(r, g, b); + } + + /** + * Clone a color. + * + * @param {color3} a - Color to clone. + * @returns {color3} The newly created color. + */ + static clone(a) { + return new color3(a.r, a.g, a.b); + } + + /** + * Copy content of a color into another. + * + * @param {color3} out - The color to modified. + * @param {color3} a - The specified color. + * @returns {color3} out. + */ + static copy(out, a) { + out.r = a.r; + out.g = a.g; + out.b = a.b; + return out; + } + + /** + * Set the components of a color to the given values. + * + * @param {color3} out - The color to modified. + * @param {number} r - Value assigned to r component. + * @param {number} g - Value assigned to g component. + * @param {number} b - Value assigned to b component. + * @returns {color3} out. + */ + static set(out, r, g, b) { + out.r = r; + out.g = g; + out.b = b; + return out; + } + + /** + * Converts the hexadecimal formal color into rgb formal. + * + * @param {color3} out - Color to store result. + * @param {Number} hex - The color's hexadecimal formal. + * @returns {color3} out. + * @function + */ + static fromHex(out, hex) { + let r = ((hex >> 16)) / 255.0; + let g = ((hex >> 8) & 0xff) / 255.0; + let b = ((hex) & 0xff) / 255.0; + + out.r = r; + out.g = g; + out.b = b; + return out; + } + + /** + * Add components of two colors, respectively. + * + * @param {color3} out - Color to store result. + * @param {color3} a - The first operand. + * @param {color3} b - The second operand. + * @returns {color3} out. + */ + static add(out, a, b) { + out.r = a.r + b.r; + out.g = a.g + b.g; + out.b = a.b + b.b; + return out; + } + + /** + * Subtract components of color b from components of color a, respectively. + * + * @param {color3} out - Color to store result. + * @param {color3} a - The a. + * @param {color3} b - The b. + * @returns {color3} out. + */ + static subtract(out, a, b) { + out.r = a.r - b.r; + out.g = a.g - b.g; + out.b = a.b - b.b; + return out; + } + + /** + * Alias of {@link color3.subtract}. + */ + static sub(out, a, b) { + return color3.subtract(out, a, b); + } + + /** + * Multiply components of two colors, respectively. + * + * @param {color3} out - Color to store result. + * @param {color3} a - The first operand. + * @param {color3} b - The second operand. + * @returns {color3} out. + */ + static multiply(out, a, b) { + out.r = a.r * b.r; + out.g = a.g * b.g; + out.b = a.b * b.b; + return out; + } + + /** + * Alias of {@link color3.multiply}. + */ + static mul(out, a, b) { + return color3.multiply(out, a, b); + } + + /** + * Divide components of color a by components of color b, respectively. + * + * @param {color3} out - Color to store result. + * @param {color3} a - The first operand. + * @param {color3} b - The second operand. + * @returns {color3} out. + */ + static divide(out, a, b) { + out.r = a.r / b.r; + out.g = a.g / b.g; + out.b = a.b / b.b; + return out; + } + + /** + * Alias of {@link color3.divide}. + */ + static div(out, a, b) { + return color3.divide(out, a, b); + } + + /** + * Scales a color by a number. + * + * @param {color3} out - Color to store result. + * @param {color3} a - Color to scale. + * @param {number} b - The scale number. + * @returns {color3} out. + */ + static scale(out, a, b) { + out.r = a.r * b; + out.g = a.g * b; + out.b = a.b * b; + return out; + } + + /** + * Performs a linear interpolation between two colors. + * + * @param {color3} out - Color to store result. + * @param {color3} a - The first operand. + * @param {color3} b - The second operand. + * @param {Number} t - The interpolation coefficient. + * @returns {color3} out. + */ + static lerp(out, a, b, t) { + let ar = a.r, + ag = a.g, + ab = a.b; + out.r = ar + t * (b.r - ar); + out.g = ag + t * (b.g - ag); + out.b = ab + t * (b.b - ab); + return out; + } + + /** + * Returns string representation of a color. + * + * @param {color3} a - The color. + * @returns {String} - String representation of this color. + */ + static str(a) { + return `color3(${a.r}, ${a.g}, ${a.b})`; + } + + /** + * Store components of a color into array. + * + * @param {Array} out - Array to store result. + * @param {color3} a - The color. + * @returns {Array} out. + */ + static array(out, a) { + out[0] = a.r; + out[1] = a.g; + out[2] = a.b; + + return out; + } + + /** + * Returns whether the specified colors are equal. (Compared using ===) + * + * @param {color3} a - The first color. + * @param {color3} b - The second color. + * @returns {Boolean} True if the colors are equal, false otherwise. + */ + static exactEquals(a, b) { + return a.r === b.r && a.g === b.g && a.b === b.b; + } + + /** + * Returns whether the specified colors are approximately equal. + * + * @param {color3} a - The first color. + * @param {color3} b - The second color. + * @returns {Boolean} True if the colors are approximately equal, false otherwise. + */ + static equals(a, b) { + let a0 = a.r, a1 = a.g, a2 = a.b; + let b0 = b.r, b1 = b.g, b2 = b.b; + return (Math.abs(a0 - b0) <= EPSILON * Math.max(1.0, Math.abs(a0), Math.abs(b0)) && + Math.abs(a1 - b1) <= EPSILON * Math.max(1.0, Math.abs(a1), Math.abs(b1)) && + Math.abs(a2 - b2) <= EPSILON * Math.max(1.0, Math.abs(a2), Math.abs(b2))); + } + + /** + * Converts a color's rgb formal into the hexadecimal one. + * + * @param {color3} a - The color. + * @returns {Number} - The color's hexadecimal formal. + */ + static hex(a) { + return (a.r * 255) << 16 | (a.g * 255) << 8 | (a.b * 255); + } +} + +export default color3; \ No newline at end of file diff --git a/cocos/vmath/color4.js b/cocos/vmath/color4.js new file mode 100644 index 0000000000..fbe0dde9bc --- /dev/null +++ b/cocos/vmath/color4.js @@ -0,0 +1,305 @@ +import { EPSILON } from './utils'; + +/** + * Represents a color with red(r), green(g), blue(b) component of that color and + * and an extra alpha(a) component indicating how opaque this color is. + */ +class color4 { + /** + * Creates a color, with components specified separately. + * + * @param {number} r - Value assigned to r component. + * @param {number} g - Value assigned to g component. + * @param {number} b - Value assigned to b component. + * @param {number} a - Value assigned to a component. + */ + constructor(r = 1, g = 1, b = 1, a = 1) { + /** + * The r component. + * @type {number} + * */ + this.r = r; + + /** + * The g component. + * @type {number} + * */ + this.g = g; + + /** + * The b component. + * @type {number} + * */ + this.b = b; + + /** + * The a component. + * @type {number} + * */ + this.a = a; + } + + /** + * Creates a white color, or components specified separately. + * + * @param {number} r - Value assigned to r component. + * @param {number} g - Value assigned to g component. + * @param {number} b - Value assigned to b component. + * @param {number} a - Value assigned to a component. + * @return {color4} The newly created color. + */ + static create(r = 1, g = 1, b = 1, a = 1) { + return new color4(r, g, b, a); + } + + /** + * Clone a color. + * + * @param {color4} a - Color to clone. + * @returns {color4} The newly created color. + */ + static clone(a) { + return new color4(a.r, a.g, a.b, a.a); + } + + /** + * Copy content of a color into another. + * + * @param {color4} out - The color to modified. + * @param {color4} a - The specified color. + * @returns {color4} out. + */ + static copy(out, a) { + out.r = a.r; + out.g = a.g; + out.b = a.b; + out.a = a.a; + return out; + } + + /** + * Set the components of a color to the given values. + * + * @param {color4} out - The color to modified. + * @param {number} r - Value assigned to r component. + * @param {number} g - Value assigned to g component. + * @param {number} b - Value assigned to b component. + * @param {number} a - Value assigned to a component. + * @returns {color4} out. + */ + static set(out, r, g, b, a) { + out.r = r; + out.g = g; + out.b = b; + out.a = a; + return out; + } + + /** + * Converts the hexadecimal formal color into rgb formal. + * + * @param {color4} out - Color to store result. + * @param {Number} hex - The color's hexadecimal formal. + * @returns {color4} out. + * @function + */ + static fromHex(out, hex) { + let r = ((hex >> 24)) / 255.0; + let g = ((hex >> 16) & 0xff) / 255.0; + let b = ((hex >> 8) & 0xff) / 255.0; + let a = ((hex) & 0xff) / 255.0; + + out.r = r; + out.g = g; + out.b = b; + out.a = a; + return out; + } + + /** + * Add components of two colors, respectively. + * + * @param {color4} out - Color to store result. + * @param {color4} a - The first operand. + * @param {color4} b - The second operand. + * @returns {color4} out. + */ + static add(out, a, b) { + out.r = a.r + b.r; + out.g = a.g + b.g; + out.b = a.b + b.b; + out.a = a.a + b.a; + return out; + } + + /** + * Subtract components of color b from components of color a, respectively. + * + * @param {color4} out - Color to store result. + * @param {color4} a - The a. + * @param {color4} b - The b. + * @returns {color4} out. + */ + static subtract(out, a, b) { + out.r = a.r - b.r; + out.g = a.g - b.g; + out.b = a.b - b.b; + out.a = a.a - b.a; + return out; + } + + /** + * Alias of {@link color4.subtract}. + */ + static sub(out, a, b) { + return color4.subtract(out, a, b); + } + + /** + * Multiply components of two colors, respectively. + * + * @param {color4} out - Color to store result. + * @param {color4} a - The first operand. + * @param {color4} b - The second operand. + * @returns {color4} out. + */ + static multiply(out, a, b) { + out.r = a.r * b.r; + out.g = a.g * b.g; + out.b = a.b * b.b; + out.a = a.a * b.a; + return out; + } + + /** + * Alias of {@link color4.multiply}. + */ + static mul(out, a, b) { + return color4.multiply(out, a, b); + } + + /** + * Divide components of color a by components of color b, respectively. + * + * @param {color4} out - Color to store result. + * @param {color4} a - The first operand. + * @param {color4} b - The second operand. + * @returns {color4} out. + */ + static divide(out, a, b) { + out.r = a.r / b.r; + out.g = a.g / b.g; + out.b = a.b / b.b; + out.a = a.a / b.a; + return out; + } + + /** + * Alias of {@link color4.divide}. + */ + static div(out, a, b) { + return color4.divide(out, a, b); + } + + /** + * Scales a color by a number. + * + * @param {color4} out - Color to store result. + * @param {color4} a - Color to scale. + * @param {number} b - The scale number. + * @returns {color4} out. + */ + static scale(out, a, b) { + out.r = a.r * b; + out.g = a.g * b; + out.b = a.b * b; + out.a = a.a * b; + return out; + } + + /** + * Performs a linear interpolation between two colors. + * + * @param {color4} out - Color to store result. + * @param {color4} a - The first operand. + * @param {color4} b - The second operand. + * @param {Number} t - The interpolation coefficient. + * @returns {color4} out. + */ + static lerp(out, a, b, t) { + let ar = a.r, + ag = a.g, + ab = a.b, + aa = a.a; + out.r = ar + t * (b.r - ar); + out.g = ag + t * (b.g - ag); + out.b = ab + t * (b.b - ab); + out.a = aa + t * (b.a - aa); + return out; + } + + /** + * Returns string representation of a color. + * + * @param {color4} a - The color. + * @returns {String} - String representation of this color. + */ + static str(a) { + return `color4(${a.r}, ${a.g}, ${a.b}, ${a.a})`; + } + + /** + * Store components of a color into array. + * + * @param {Array} out - Array to store result. + * @param {color4} a - The color. + * @returns {Array} out. + */ + static array(out, a) { + out[0] = a.r; + out[1] = a.g; + out[2] = a.b; + out[3] = a.a; + + return out; + } + + /** + * Returns whether the specified colors are equal. (Compared using ===) + * + * @param {color4} a - The first color. + * @param {color4} b - The second color. + * @returns {Boolean} True if the colors are equal, false otherwise. + */ + static exactEquals(a, b) { + return a.r === b.r && a.g === b.g && a.b === b.b && a.a === b.a; + } + + /** + * Returns whether the specified colors are approximately equal. + * + * @param {color4} a - The first color. + * @param {color4} b - The second color. + * @returns {Boolean} True if the colors are approximately equal, false otherwise. + */ + static equals(a, b) { + let a0 = a.r, a1 = a.g, a2 = a.b, a3 = a.a; + let b0 = b.r, b1 = b.g, b2 = b.b, b3 = b.a; + return (Math.abs(a0 - b0) <= EPSILON * Math.max(1.0, Math.abs(a0), Math.abs(b0)) && + Math.abs(a1 - b1) <= EPSILON * Math.max(1.0, Math.abs(a1), Math.abs(b1)) && + Math.abs(a2 - b2) <= EPSILON * Math.max(1.0, Math.abs(a2), Math.abs(b2)) && + Math.abs(a3 - b3) <= EPSILON * Math.max(1.0, Math.abs(a3), Math.abs(b3))); + } + + /** + * Converts a color's rgb formal into the hexadecimal one. + * + * @param {color4} a - The color. + * @returns {Number} - The color's hexadecimal formal. + */ + static hex(a) { + return ((a.r * 255) << 24 | (a.g * 255) << 16 | (a.b * 255) << 8 | a.a * 255) >>> 0; + } +} + +export default color4; \ No newline at end of file diff --git a/cocos/vmath/index.js b/cocos/vmath/index.js index e69de29bb2..75614255f6 100644 --- a/cocos/vmath/index.js +++ b/cocos/vmath/index.js @@ -0,0 +1,19 @@ +export * from './utils'; + +// NOTE: there is no syntax for: export {* as bits} from './lib/bits'; +import * as bits_ from './bits'; +/** + * Export module bits. + */ +export let bits = bits_; + +export { default as vec2 } from './vec2'; +export { default as vec3 } from './vec3'; +export { default as vec4 } from './vec4'; +export { default as quat } from './quat'; +export { default as mat2 } from './mat2'; +export { default as mat23 } from './mat23'; +export { default as mat3 } from './mat3'; +export { default as mat4 } from './mat4'; +export { default as color3 } from './color3'; +export { default as color4 } from './color4'; \ No newline at end of file diff --git a/cocos/vmath/mat2.js b/cocos/vmath/mat2.js new file mode 100644 index 0000000000..2466c53268 --- /dev/null +++ b/cocos/vmath/mat2.js @@ -0,0 +1,443 @@ +import { EPSILON } from './utils'; + +/** + * Mathematical 2x2 matrix. + */ +class mat2 { + /** + * Creates a matrix, with elements specified separately. + * + * @param {Number} m00 - Value assigned to element at column 0 row 0. + * @param {Number} m01 - Value assigned to element at column 0 row 1. + * @param {Number} m02 - Value assigned to element at column 1 row 0. + * @param {Number} m03 - Value assigned to element at column 1 row 1. + */ + constructor(m00 = 1, m01 = 0, m02 = 0, m03 = 1) { + /** + * The element at column 0 row 0. + * @type {number} + * */ + this.m00 = m00; + + /** + * The element at column 0 row 1. + * @type {number} + * */ + this.m01 = m01; + + /** + * The element at column 1 row 0. + * @type {number} + * */ + this.m02 = m02; + + /** + * The element at column 1 row 1. + * @type {number} + * */ + this.m03 = m03; + } + + /** + * Creates a matrix, with elements specified separately. + * + * @param {Number} m00 - Value assigned to element at column 0 row 0. + * @param {Number} m01 - Value assigned to element at column 0 row 1. + * @param {Number} m02 - Value assigned to element at column 1 row 0. + * @param {Number} m03 - Value assigned to element at column 1 row 1. + * @returns {mat2} The newly created matrix. + */ + static create(m00 = 1, m01 = 0, m02 = 0, m03 = 1) { + return new mat2(m00, m01, m02, m03); + } + + /** + * Clone a matrix. + * + * @param {mat2} a - Matrix to clone. + * @returns {mat2} The newly created matrix. + */ + static clone(a) { + return new mat2(a.m00, a.m01, a.m02, a.m03); + } + + /** + * Copy content of a matrix into another. + * + * @param {mat2} out - Matrix to modified. + * @param {mat2} a - The specified matrix. + * @returns {mat2} out. + */ + static copy(out, a) { + out.m00 = a.m00; + out.m01 = a.m01; + out.m02 = a.m02; + out.m03 = a.m03; + return out; + } + + /** + * Sets a matrix as identity matrix. + * + * @param {mat2} out - Matrix to modified. + * @returns {mat2} out. + */ + static identity(out) { + out.m00 = 1; + out.m01 = 0; + out.m02 = 0; + out.m03 = 1; + return out; + } + + /** + * Sets the elements of a matrix to the given values. + * + * @param {mat2} out - The matrix to modified. + * @param {Number} m00 - Value assigned to element at column 0 row 0. + * @param {Number} m01 - Value assigned to element at column 0 row 1. + * @param {Number} m10 - Value assigned to element at column 1 row 0. + * @param {Number} m11 - Value assigned to element at column 1 row 1. + * @returns {mat2} out. + */ + static set(out, m00, m01, m10, m11) { + out.m00 = m00; + out.m01 = m01; + out.m02 = m10; + out.m03 = m11; + return out; + } + + + /** + * Transposes a matrix. + * + * @param {mat2} out - Matrix to store result. + * @param {mat2} a - Matrix to transpose. + * @returns {mat2} out. + */ + static transpose(out, a) { + // If we are transposing ourselves we can skip a few steps but have to cache some values + if (out === a) { + let a1 = a.m01; + out.m01 = a.m02; + out.m02 = a1; + } else { + out.m00 = a.m00; + out.m01 = a.m02; + out.m02 = a.m01; + out.m03 = a.m03; + } + + return out; + } + + /** + * Inverts a matrix. + * + * @param {mat2} out - Matrix to store result. + * @param {mat2} a - Matrix to invert. + * @returns {mat2} out. + */ + static invert(out, a) { + let a0 = a.m00, a1 = a.m01, a2 = a.m02, a3 = a.m03; + + // Calculate the determinant + let det = a0 * a3 - a2 * a1; + + if (!det) { + return null; + } + det = 1.0 / det; + + out.m00 = a3 * det; + out.m01 = -a1 * det; + out.m02 = -a2 * det; + out.m03 = a0 * det; + + return out; + } + + /** + * Calculates the adjugate of a matrix. + * + * @param {mat2} out - Matrix to store result. + * @param {mat2} a - Matrix to calculate. + * @returns {mat2} out. + */ + static adjoint(out, a) { + // Caching this value is nessecary if out == a + let a0 = a.m00; + out.m00 = a.m03; + out.m01 = -a.m01; + out.m02 = -a.m02; + out.m03 = a0; + + return out; + } + + /** + * Calculates the determinant of a matrix. + * + * @param {mat2} a - Matrix to calculate. + * @returns {Number} Determinant of a. + */ + static determinant(a) { + return a.m00 * a.m03 - a.m02 * a.m01; + } + + /** + * Multiply two matrices explicitly. + * + * @param {mat2} out - Matrix to store result. + * @param {mat2} a - The first operand. + * @param {mat2} b - The second operand. + * @returns {mat2} out. + */ + static multiply(out, a, b) { + let a0 = a.m00, a1 = a.m01, a2 = a.m02, a3 = a.m03; + let b0 = b.m00, b1 = b.m01, b2 = b.m02, b3 = b.m03; + out.m00 = a0 * b0 + a2 * b1; + out.m01 = a1 * b0 + a3 * b1; + out.m02 = a0 * b2 + a2 * b3; + out.m03 = a1 * b2 + a3 * b3; + return out; + } + + /** + * Alias of {@link mat2.multiply}. + */ + static mul(out, a, b) { + return mat2.multiply(out, a, b); + } + + /** + * Rotates a matrix by the given angle. + * + * @param {mat2} out - Matrix to store result. + * @param {mat2} a - Matrix to rotate. + * @param {Number} rad - The rotation angle. + * @returns {mat2} out + */ + static rotate(out, a, rad) { + let a0 = a.m00, a1 = a.m01, a2 = a.m02, a3 = a.m03, + s = Math.sin(rad), + c = Math.cos(rad); + out.m00 = a0 * c + a2 * s; + out.m01 = a1 * c + a3 * s; + out.m02 = a0 * -s + a2 * c; + out.m03 = a1 * -s + a3 * c; + return out; + } + + /** + * Scales the matrix given by a scale vector. + * + * @param {mat2} out - Matrix to store result. + * @param {mat2} a - Matrix to scale. + * @param {vec2} v - The scale vector. + * @returns {mat2} out + **/ + static scale(out, a, v) { + let a0 = a.m00, a1 = a.m01, a2 = a.m02, a3 = a.m03, + v0 = v.x, v1 = v.y; + out.m00 = a0 * v0; + out.m01 = a1 * v0; + out.m02 = a2 * v1; + out.m03 = a3 * v1; + return out; + } + + /** + * Creates a matrix from a given angle. + * This is equivalent to (but much faster than): + * + * mat2.set(dest, 1, 0, 0, 1); + * mat2.rotate(dest, dest, rad); + * + * @param {mat2} out - Matrix to store result. + * @param {Number} rad - The rotation angle. + * @returns {mat2} out. + */ + static fromRotation(out, rad) { + let s = Math.sin(rad), + c = Math.cos(rad); + out.m00 = c; + out.m01 = s; + out.m02 = -s; + out.m03 = c; + return out; + } + + /** + * Creates a matrix from a scale vector. + * This is equivalent to (but much faster than): + * + * mat2.set(dest, 1, 0, 0, 1); + * mat2.scale(dest, dest, vec); + * + * @param {mat2} out - Matrix to store result. + * @param {vec2} v - Scale vector. + * @returns {mat2} out. + */ + static fromScaling(out, v) { + out.m00 = v.x; + out.m01 = 0; + out.m02 = 0; + out.m03 = v.y; + return out; + } + + /** + * Returns a string representation of a matrix. + * + * @param {mat2} a - The matrix. + * @returns {String} String representation of this matrix. + */ + static str(a) { + return `mat2(${a.m00}, ${a.m01}, ${a.m02}, ${a.m03})`; + } + + /** + * Store elements of a matrix into array. + * + * @param {array} out - Array to store result. + * @param {mat2} m - The matrix. + * @returns {Array} out. + */ + static array(out, m) { + out[0] = m.m00; + out[1] = m.m01; + out[2] = m.m02; + out[3] = m.m03; + + return out; + } + + /** + * Returns Frobenius norm of a matrix. + * + * @param {mat2} a - Matrix to calculate Frobenius norm of. + * @returns {Number} - The frobenius norm. + */ + static frob(a) { + return (Math.sqrt(Math.pow(a.m00, 2) + Math.pow(a.m01, 2) + Math.pow(a.m02, 2) + Math.pow(a.m03, 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. + */ + static LDU(L, D, U, a) { + L.m02 = a.m02 / a.m00; + U.m00 = a.m00; + U.m01 = a.m01; + U.m03 = a.m03 - L.m02 * U.m01; + } + + /** + * Adds two matrices. + * + * @param {mat2} out - Matrix to store result. + * @param {mat2} a - The first operand. + * @param {mat2} b - The second operand. + * @returns {mat2} out. + */ + static add(out, a, b) { + out.m00 = a.m00 + b.m00; + out.m01 = a.m01 + b.m01; + out.m02 = a.m02 + b.m02; + out.m03 = a.m03 + b.m03; + return out; + } + + /** + * Subtracts matrix b from matrix a. + * + * @param {mat2} out - Matrix to store result. + * @param {mat2} a - The first operand. + * @param {mat2} b - The second operand. + * @returns {mat2} out. + */ + static subtract(out, a, b) { + out.m00 = a.m00 - b.m00; + out.m01 = a.m01 - b.m01; + out.m02 = a.m02 - b.m02; + out.m03 = a.m03 - b.m03; + return out; + } + + /** + * Alias of {@link mat2.subtract}. + */ + static sub(out, a, b) { + return mat2.subtract(out, a, b); + } + + /** + * Returns whether the specified matrices are equal. (Compared using ===) + * + * @param {mat2} a - The first matrix. + * @param {mat2} b - The second matrix. + * @returns {Boolean} True if the matrices are equal, false otherwise. + */ + static exactEquals(a, b) { + return a.m00 === b.m00 && a.m01 === b.m01 && a.m02 === b.m02 && a.m03 === b.m03; + } + + /** + * Returns whether the specified matrices are approximately equal. + * + * @param {mat2} a - The first matrix. + * @param {mat2} b - The second matrix. + * @returns {Boolean} True if the matrices are equal, false otherwise. + */ + static equals(a, b) { + let a0 = a.m00, a1 = a.m01, a2 = a.m02, a3 = a.m03; + let b0 = b.m00, b1 = b.m01, b2 = b.m02, b3 = b.m03; + return ( + Math.abs(a0 - b0) <= EPSILON * Math.max(1.0, Math.abs(a0), Math.abs(b0)) && + Math.abs(a1 - b1) <= EPSILON * Math.max(1.0, Math.abs(a1), Math.abs(b1)) && + Math.abs(a2 - b2) <= EPSILON * Math.max(1.0, Math.abs(a2), Math.abs(b2)) && + Math.abs(a3 - b3) <= EPSILON * Math.max(1.0, Math.abs(a3), Math.abs(b3)) + ); + } + + /** + * Multiply each element of a matrix by a scalar number. + * + * @param {mat2} out - Matrix to store result. + * @param {mat2} a - Matrix to scale + * @param {Number} b - The scale number. + * @returns {mat2} out. + */ + static multiplyScalar(out, a, b) { + out.m00 = a.m00 * b; + out.m01 = a.m01 * b; + out.m02 = a.m02 * b; + out.m03 = a.m03 * b; + return out; + } + + /** + * Adds two matrices after multiplying each element of the second operand by a scalar number. + * + * @param {mat2} out - Matrix to store result. + * @param {mat2} a - The first operand. + * @param {mat2} b - The second operand. + * @param {Number} scale - The scale number. + * @returns {mat2} out. + */ + static multiplyScalarAndAdd(out, a, b, scale) { + out.m00 = a.m00 + (b.m00 * scale); + out.m01 = a.m01 + (b.m01 * scale); + out.m02 = a.m02 + (b.m02 * scale); + out.m03 = a.m03 + (b.m03 * scale); + return out; + } +} + +export default mat2; \ No newline at end of file diff --git a/cocos/vmath/mat23.js b/cocos/vmath/mat23.js new file mode 100644 index 0000000000..4c89ec8a8f --- /dev/null +++ b/cocos/vmath/mat23.js @@ -0,0 +1,547 @@ +import { EPSILON } from './utils'; + +/** + * Mathematical 2x3 matrix. + * + * A mat23 contains six elements defined as: + *
+ * [a, c, tx,
+ *  b, d, ty]
+ * 
+ * This is a short form for the 3x3 matrix: + *
+ * [a, c, tx,
+ *  b, d, ty,
+ *  0, 0, 1]
+ * 
+ * The last row is ignored so the array is shorter and operations are faster. + */ +class mat23 { + /** + * Creates a matrix, with elements specified separately. + * + * @param {Number} m00 - Value assigned to element a. + * @param {Number} m01 - Value assigned to element b. + * @param {Number} m02 - Value assigned to element c. + * @param {Number} m03 - Value assigned to element d. + * @param {Number} m04 - Value assigned to element tx. + * @param {Number} m05 - Value assigned to element ty. + */ + constructor(m00 = 1, m01 = 0, m02 = 0, m03 = 1, m04 = 0, m05 = 0) { + /** + * The element a. + * @type {number} + * */ + this.m00 = m00; + + /** + * The element b. + * @type {number} + * */ + this.m01 = m01; + + /** + * The element c. + * @type {number} + * */ + this.m02 = m02; + + /** + * The element d. + * @type {number} + * */ + this.m03 = m03; + + /** + * The element tx. + * @type {number} + * */ + this.m04 = m04; + + /** + * The element ty. + * @type {number} + * */ + this.m05 = m05; + } + + /** + * Creates a matrix, with elements specified separately. + * + * @param {Number} m00 - Value assigned to element a. + * @param {Number} m01 - Value assigned to element b. + * @param {Number} m02 - Value assigned to element c. + * @param {Number} m03 - Value assigned to element d. + * @param {Number} m04 - Value assigned to element tx. + * @param {Number} m05 - Value assigned to element ty. + * @returns {mat23} The newly created matrix. + */ + static create(m00 = 1, m01 = 0, m02 = 0, m03 = 1, m04 = 0, m05 = 0) { + return new mat23(m00, m01, m02, m03, m04, m05); + } + + /** + * Clone a matrix. + * + * @param {mat23} a - Matrix to clone. + * @returns {mat23} The newly created matrix. + */ + static clone(a) { + return new mat23( + a.m00, a.m01, + a.m02, a.m03, + a.m04, a.m05 + ); + } + + /** + * Copy content of a matrix into another. + * + * @param {mat23} out - Matrix to modified. + * @param {mat23} a - The specified matrix. + * @returns {mat23} out. + */ + static copy(out, a) { + out.m00 = a.m00; + out.m01 = a.m01; + out.m02 = a.m02; + out.m03 = a.m03; + out.m04 = a.m04; + out.m05 = a.m05; + return out; + } + + /** + * Sets a matrix as identity matrix. + * + * @param {mat23} out - Matrix to modified. + * @returns {mat23} out. + */ + static identity(out) { + out.m00 = 1; + out.m01 = 0; + out.m02 = 0; + out.m03 = 1; + out.m04 = 0; + out.m05 = 0; + return out; + } + + /** + * Sets the elements of a matrix to the given values. + * + * @param {mat23} out - The matrix to modified. + * @param {Number} a - Value assigned to element a. + * @param {Number} b - Value assigned to element b. + * @param {Number} c - Value assigned to element c. + * @param {Number} d - Value assigned to element d. + * @param {Number} tx - Value assigned to element tx. + * @param {Number} ty - Value assigned to element ty. + * @returns {mat23} out. + */ + static set(out, a, b, c, d, tx, ty) { + out.m00 = a; + out.m01 = b; + out.m02 = c; + out.m03 = d; + out.m04 = tx; + out.m05 = ty; + return out; + } + + /** + * Inverts a matrix. + * + * @param {mat23} out - Matrix to store result. + * @param {mat23} a - Matrix to invert. + * @returns {mat23} out. + */ + static invert(out, a) { + let aa = a.m00, ab = a.m01, ac = a.m02, ad = a.m03, + atx = a.m04, aty = a.m05; + + let det = aa * ad - ab * ac; + if (!det) { + return null; + } + det = 1.0 / det; + + out.m00 = ad * det; + out.m01 = -ab * det; + out.m02 = -ac * det; + out.m03 = aa * det; + out.m04 = (ac * aty - ad * atx) * det; + out.m05 = (ab * atx - aa * aty) * det; + return out; + } + + /** + * Calculates the determinant of a matrix. + * + * @param {mat23} a - Matrix to calculate. + * @returns {Number} Determinant of a. + */ + static determinant(a) { + return a.m00 * a.m03 - a.m01 * a.m02; + } + + /** + * Multiply two matrices explicitly. + * + * @param {mat23} out - Matrix to store result. + * @param {mat23} a - The first operand. + * @param {mat23} b - The second operand. + * @returns {mat23} out. + */ + static multiply(out, a, b) { + let a0 = a.m00, a1 = a.m01, a2 = a.m02, a3 = a.m03, a4 = a.m04, a5 = a.m05, + b0 = b.m00, b1 = b.m01, b2 = b.m02, b3 = b.m03, b4 = b.m04, b5 = b.m05; + out.m00 = a0 * b0 + a2 * b1; + out.m01 = a1 * b0 + a3 * b1; + out.m02 = a0 * b2 + a2 * b3; + out.m03 = a1 * b2 + a3 * b3; + out.m04 = a0 * b4 + a2 * b5 + a4; + out.m05 = a1 * b4 + a3 * b5 + a5; + return out; + } + + /** + * Alias of {@link mat23.multiply}. + */ + static mul(out, a, b) { + return mat23.multiply(out, a, b); + } + + /** + * Rotates a matrix by the given angle. + * + * @param {mat23} out - Matrix to store result. + * @param {mat23} a - Matrix to rotate. + * @param {Number} rad - The rotation angle. + * @returns {mat23} out + */ + static rotate(out, a, rad) { + let a0 = a.m00, a1 = a.m01, a2 = a.m02, a3 = a.m03, a4 = a.m04, a5 = a.m05, + s = Math.sin(rad), + c = Math.cos(rad); + out.m00 = a0 * c + a2 * s; + out.m01 = a1 * c + a3 * s; + out.m02 = a0 * -s + a2 * c; + out.m03 = a1 * -s + a3 * c; + out.m04 = a4; + out.m05 = a5; + return out; + } + + /** + * Multiply a matrix with a scale matrix given by a scale vector. + * + * @param {mat23} out - Matrix to store result. + * @param {mat23} a - Matrix to multiply. + * @param {vec2} v - The scale vector. + * @returns {mat23} out + **/ + static scale(out, a, v) { + let a0 = a.m00, a1 = a.m01, a2 = a.m02, a3 = a.m03, a4 = a.m04, a5 = a.m05, + v0 = v.x, v1 = v.y; + out.m00 = a0 * v0; + out.m01 = a1 * v0; + out.m02 = a2 * v1; + out.m03 = a3 * v1; + out.m04 = a4; + out.m05 = a5; + return out; + } + + /** + * Multiply a matrix with a translation matrix given by a translation offset. + * + * @param {mat23} out - Matrix to store result. + * @param {mat23} a - Matrix to multiply. + * @param {vec2} v - The translation offset. + * @returns {mat23} out. + */ + static translate(out, a, v) { + let a0 = a.m00, a1 = a.m01, a2 = a.m02, a3 = a.m03, a4 = a.m04, a5 = a.m05, + v0 = v.x, v1 = v.y; + out.m00 = a0; + out.m01 = a1; + out.m02 = a2; + out.m03 = a3; + out.m04 = a0 * v0 + a2 * v1 + a4; + out.m05 = a1 * v0 + a3 * v1 + a5; + return out; + } + + /** + * Creates a matrix from a given angle. + * This is equivalent to (but much faster than): + * + * mat23.identity(dest); + * mat23.rotate(dest, dest, rad); + * + * @param {mat23} out - Matrix to store result. + * @param {Number} rad - The rotation angle. + * @returns {mat23} out. + */ + static fromRotation(out, rad) { + let s = Math.sin(rad), c = Math.cos(rad); + out.m00 = c; + out.m01 = s; + out.m02 = -s; + out.m03 = c; + out.m04 = 0; + out.m05 = 0; + return out; + } + + /** + * Creates a matrix from a scale vector. + * This is equivalent to (but much faster than): + * + * mat23.identity(dest); + * mat23.scale(dest, dest, vec); + * + * @param {mat23} out - Matrix to store result. + * @param {vec2} v - Scale vector. + * @returns {mat23} out. + */ + static fromScaling(out, v) { + out.m00 = v.m00; + out.m01 = 0; + out.m02 = 0; + out.m03 = v.m01; + out.m04 = 0; + out.m05 = 0; + return out; + } + + /** + * Creates a matrix from a translation offset. + * This is equivalent to (but much faster than): + * + * mat23.identity(dest); + * mat23.translate(dest, dest, vec); + * + * @param {mat23} out - Matrix to store result. + * @param {vec2} v - The translation offset. + * @returns {mat23} out. + */ + static fromTranslation(out, v) { + out.m00 = 1; + out.m01 = 0; + out.m02 = 0; + out.m03 = 1; + out.m04 = v.x; + out.m05 = v.y; + return out; + } + + /** + * Creates a matrix from a rotation, translation offset and scale vector. + * This is equivalent to (but faster than): + * + * mat23.identity(dest); + * mat23.translate(dest, vec); + * let tmp = mat23.create(); + * mat23.fromRotation(tmp, rot); + * mat23.multiply(dest, dest, tmp); + * mat23.fromScaling(tmp, scale); + * mat23.multiply(dest, dest, tmp); + * + * @param {mat23} out - Matrix to store result. + * @param {number} r - Rotation radian. + * @param {vec2} t - Translation offset. + * @param {vec2} s - Scale vector. + * @returns {mat23} out. + */ + static fromRTS(out, r, t, s) { + let sr = Math.sin(r), cr = Math.cos(r); + out.m00 = cr * s.x; + out.m01 = sr * s.x; + out.m02 = -sr * s.y; + out.m03 = cr * s.y; + out.m04 = t.x; + out.m05 = t.y; + return out; + } + + /** + * Returns a string representation of a matrix. + * + * @param {mat23} a - The matrix. + * @returns {String} String representation of this matrix. + */ + static str(a) { + return `mat23(${a.m00}, ${a.m01}, ${a.m02}, ${a.m03}, ${a.m04}, ${a.m05})`; + } + + /** + * Store elements of a matrix into array. + * + * @param {array} out - Array to store result. + * @param {mat23} m - The matrix. + * @returns {Array} out. + */ + static array(out, m) { + out[0] = m.m00; + out[1] = m.m01; + out[2] = m.m02; + out[3] = m.m03; + out[4] = m.m04; + out[5] = m.m05; + + return out; + } + + /** + * Store elements of a matrix into 16 floats array. + * + * @param {array} out + * @param {mat23} m + * @returns {array} + */ + static array4x4(out, m) { + out[0] = m.m00; + out[1] = m.m01; + out[2] = 0; + out[3] = 0; + out[4] = m.m02; + out[5] = m.m03; + out[6] = 0; + out[7] = 0; + out[8] = 0; + out[9] = 0; + out[10] = 1; + out[11] = 0; + out[12] = m.m04; + out[13] = m.m05; + out[14] = 0; + out[15] = 1; + + return out; + } + + /** + * Returns Frobenius norm of a matrix. + * + * @param {mat23} a - Matrix to calculate Frobenius norm of. + * @returns {Number} - The frobenius norm. + */ + static frob(a) { + return (Math.sqrt(Math.pow(a.m00, 2) + Math.pow(a.m01, 2) + Math.pow(a.m02, 2) + Math.pow(a.m03, 2) + Math.pow(a.m04, 2) + Math.pow(a.m05, 2) + 1)); + } + + /** + * Adds two matrices. + * + * @param {mat23} out - Matrix to store result. + * @param {mat23} a - The first operand. + * @param {mat23} b - The second operand. + * @returns {mat23} out. + */ + static add(out, a, b) { + out.m00 = a.m00 + b.m00; + out.m01 = a.m01 + b.m01; + out.m02 = a.m02 + b.m02; + out.m03 = a.m03 + b.m03; + out.m04 = a.m04 + b.m04; + out.m05 = a.m05 + b.m05; + return out; + } + + /** + * Subtracts matrix b from matrix a. + * + * @param {mat23} out - Matrix to store result. + * @param {mat23} a - The first operand. + * @param {mat23} b - The second operand. + * @returns {mat23} out. + */ + static subtract(out, a, b) { + out.m00 = a.m00 - b.m00; + out.m01 = a.m01 - b.m01; + out.m02 = a.m02 - b.m02; + out.m03 = a.m03 - b.m03; + out.m04 = a.m04 - b.m04; + out.m05 = a.m05 - b.m05; + return out; + } + + /** + * Alias of {@link mat23.subtract}. + */ + static sub(out, a, b) { + return mat23.subtract(out, a, b); + } + + /** + * Multiply each element of a matrix by a scalar number. + * + * @param {mat23} out - Matrix to store result. + * @param {mat23} a - Matrix to scale + * @param {Number} b - The scale number. + * @returns {mat23} out. + */ + static multiplyScalar(out, a, b) { + out.m00 = a.m00 * b; + out.m01 = a.m01 * b; + out.m02 = a.m02 * b; + out.m03 = a.m03 * b; + out.m04 = a.m04 * b; + out.m05 = a.m05 * b; + return out; + } + + /** + * Adds two matrices after multiplying each element of the second operand by a scalar number. + * + * @param {mat23} out - Matrix to store result. + * @param {mat23} a - The first operand. + * @param {mat23} b - The second operand. + * @param {Number} scale - The scale number. + * @returns {mat23} out. + */ + static multiplyScalarAndAdd(out, a, b, scale) { + out.m00 = a.m00 + (b.m00 * scale); + out.m01 = a.m01 + (b.m01 * scale); + out.m02 = a.m02 + (b.m02 * scale); + out.m03 = a.m03 + (b.m03 * scale); + out.m04 = a.m04 + (b.m04 * scale); + out.m05 = a.m05 + (b.m05 * scale); + return out; + } + + /** + * Returns whether the specified matrices are equal. (Compared using ===) + * + * @param {mat23} a - The first matrix. + * @param {mat23} b - The second matrix. + * @returns {Boolean} True if the matrices are equal, false otherwise. + */ + static exactEquals(a, b) { + return a.m00 === b.m00 && a.m01 === b.m01 && a.m02 === b.m02 && a.m03 === b.m03 && a.m04 === b.m04 && a.m05 === b.m05; + } + + /** + * Returns whether the specified matrices are approximately equal. + * + * @param {mat23} a - The first matrix. + * @param {mat23} b - The second matrix. + * @returns {Boolean} True if the matrices are equal, false otherwise. + */ + static equals(a, b) { + let a0 = a.m00, a1 = a.m01, a2 = a.m02, a3 = a.m03, a4 = a.m04, a5 = a.m05; + let b0 = b.m00, b1 = b.m01, b2 = b.m02, b3 = b.m03, b4 = b.m04, b5 = b.m05; + return ( + Math.abs(a0 - b0) <= EPSILON * Math.max(1.0, Math.abs(a0), Math.abs(b0)) && + Math.abs(a1 - b1) <= EPSILON * Math.max(1.0, Math.abs(a1), Math.abs(b1)) && + Math.abs(a2 - b2) <= EPSILON * Math.max(1.0, Math.abs(a2), Math.abs(b2)) && + Math.abs(a3 - b3) <= EPSILON * Math.max(1.0, Math.abs(a3), Math.abs(b3)) && + Math.abs(a4 - b4) <= EPSILON * Math.max(1.0, Math.abs(a4), Math.abs(b4)) && + Math.abs(a5 - b5) <= EPSILON * Math.max(1.0, Math.abs(a5), Math.abs(b5)) + ); + } +} + +export default mat23; \ No newline at end of file diff --git a/cocos/vmath/mat3.js b/cocos/vmath/mat3.js new file mode 100644 index 0000000000..a87eeba5f4 --- /dev/null +++ b/cocos/vmath/mat3.js @@ -0,0 +1,851 @@ +import { EPSILON } from './utils'; +import vec3 from './vec3'; + +/** + * Mathematical 3x3 matrix. + * + * NOTE: we use column-major matrix for all matrix calculation. + * + * This may lead to some confusion when referencing OpenGL documentation, + * however, which represents out all matricies in column-major format. + * This means that while in code a matrix may be typed out as: + * + * [1, 0, 0, 0, + * 0, 1, 0, 0, + * 0, 0, 1, 0, + * x, y, z, 0] + * + * The same matrix in the [OpenGL documentation](https://www.khronos.org/registry/OpenGL-Refpages/gl2.1/xhtml/glTranslate.xml) + * is written as: + * + * 1 0 0 x + * 0 1 0 y + * 0 0 1 z + * 0 0 0 0 + * + * Please rest assured, however, that they are the same thing! + * This is not unique to glMatrix, either, as OpenGL developers have long been confused by the + * apparent lack of consistency between the memory layout and the documentation. + */ +class mat3 { + /** + * Creates a matrix, with elements specified separately. + * + * @param {Number} m00 - Value assigned to element at column 0 row 0. + * @param {Number} m01 - Value assigned to element at column 0 row 1. + * @param {Number} m02 - Value assigned to element at column 0 row 2. + * @param {Number} m03 - Value assigned to element at column 1 row 0. + * @param {Number} m04 - Value assigned to element at column 1 row 1. + * @param {Number} m05 - Value assigned to element at column 1 row 2. + * @param {Number} m06 - Value assigned to element at column 2 row 0. + * @param {Number} m07 - Value assigned to element at column 2 row 1. + * @param {Number} m08 - Value assigned to element at column 2 row 2. + */ + constructor( + m00 = 1, m01 = 0, m02 = 0, + m03 = 0, m04 = 1, m05 = 0, + m06 = 0, m07 = 0, m08 = 1 + ) { + /** + * The element at column 0 row 0. + * @type {number} + * */ + this.m00 = m00; + + /** + * The element at column 0 row 1. + * @type {number} + * */ + this.m01 = m01; + + /** + * The element at column 0 row 2. + * @type {number} + * */ + this.m02 = m02; + + /** + * The element at column 1 row 0. + * @type {number} + * */ + this.m03 = m03; + + /** + * The element at column 1 row 1. + * @type {number} + * */ + this.m04 = m04; + + /** + * The element at column 1 row 2. + * @type {number} + * */ + this.m05 = m05; + + /** + * The element at column 2 row 0. + * @type {number} + * */ + this.m06 = m06; + + /** + * The element at column 2 row 1. + * @type {number} + * */ + this.m07 = m07; + + /** + * The element at column 2 row 2. + * @type {number} + * */ + this.m08 = m08; + } + + /** + * Creates a matrix, with elements specified separately. + * + * @param {Number} m00 - Value assigned to element at column 0 row 0. + * @param {Number} m01 - Value assigned to element at column 0 row 1. + * @param {Number} m02 - Value assigned to element at column 0 row 2. + * @param {Number} m03 - Value assigned to element at column 1 row 0. + * @param {Number} m04 - Value assigned to element at column 1 row 1. + * @param {Number} m05 - Value assigned to element at column 1 row 2. + * @param {Number} m06 - Value assigned to element at column 2 row 0. + * @param {Number} m07 - Value assigned to element at column 2 row 1. + * @param {Number} m08 - Value assigned to element at column 2 row 2. + * @returns {mat3} The newly created matrix. + */ + static create(m00 = 1, m01 = 0, m02 = 0, m03 = 0, m04 = 1, m05 = 0, m06 = 0, m07 = 0, m08 = 1) { + return new mat3(m00, m01, m02, m03, m04, m05, m06, m07, m08); + } + + /** + * Clone a matrix. + * + * @param {mat3} a - Matrix to clone. + * @returns {mat3} The newly created matrix. + */ + static clone(a) { + return new mat3( + a.m00, a.m01, a.m02, + a.m03, a.m04, a.m05, + a.m06, a.m07, a.m08 + ); + } + + /** + * Copy content of a matrix into another. + * + * @param {mat3} out - Matrix to modified. + * @param {mat3} a - The specified matrix. + * @returns {mat3} out. + */ + static copy(out, a) { + out.m00 = a.m00; + out.m01 = a.m01; + out.m02 = a.m02; + out.m03 = a.m03; + out.m04 = a.m04; + out.m05 = a.m05; + out.m06 = a.m06; + out.m07 = a.m07; + out.m08 = a.m08; + return out; + } + + /** + * Sets the elements of a matrix to the given values. + * + * @param {mat3} out - The matrix to modified. + * @param {Number} m00 - Value assigned to element at column 0 row 0. + * @param {Number} m01 - Value assigned to element at column 0 row 1. + * @param {Number} m02 - Value assigned to element at column 0 row 2. + * @param {Number} m10 - Value assigned to element at column 1 row 0. + * @param {Number} m11 - Value assigned to element at column 1 row 1. + * @param {Number} m12 - Value assigned to element at column 1 row 2. + * @param {Number} m20 - Value assigned to element at column 2 row 0. + * @param {Number} m21 - Value assigned to element at column 2 row 1. + * @param {Number} m22 - Value assigned to element at column 2 row 2. + * @returns {mat3} out. + */ + static set(out, m00, m01, m02, m10, m11, m12, m20, m21, m22) { + out.m00 = m00; + out.m01 = m01; + out.m02 = m02; + out.m03 = m10; + out.m04 = m11; + out.m05 = m12; + out.m06 = m20; + out.m07 = m21; + out.m08 = m22; + return out; + } + + /** + * return an identity matrix. + * + * @returns {mat3} out. + */ + static identity(out) { + out.m00 = 1; + out.m01 = 0; + out.m02 = 0; + out.m03 = 0; + out.m04 = 1; + out.m05 = 0; + out.m06 = 0; + out.m07 = 0; + out.m08 = 1; + return out; + } + + /** + * Transposes a matrix. + * + * @param {mat3} out - Matrix to store result. + * @param {mat3} a - Matrix to transpose. + * @returns {mat3} out. + */ + static transpose(out, a) { + // If we are transposing ourselves we can skip a few steps but have to cache some values + if (out === a) { + let a01 = a.m01, a02 = a.m02, a12 = a.m05; + out.m01 = a.m03; + out.m02 = a.m06; + out.m03 = a01; + out.m05 = a.m07; + out.m06 = a02; + out.m07 = a12; + } else { + out.m00 = a.m00; + out.m01 = a.m03; + out.m02 = a.m06; + out.m03 = a.m01; + out.m04 = a.m04; + out.m05 = a.m07; + out.m06 = a.m02; + out.m07 = a.m05; + out.m08 = a.m08; + } + + return out; + } + + /** + * Inverts a matrix. + * + * @param {mat3} out - Matrix to store result. + * @param {mat3} a - Matrix to invert. + * @returns {mat3} out. + */ + static invert(out, a) { + let a00 = a.m00, a01 = a.m01, a02 = a.m02, + a10 = a.m03, a11 = a.m04, a12 = a.m05, + a20 = a.m06, a21 = a.m07, a22 = a.m08; + + let b01 = a22 * a11 - a12 * a21; + let b11 = -a22 * a10 + a12 * a20; + let b21 = a21 * a10 - a11 * a20; + + // Calculate the determinant + let det = a00 * b01 + a01 * b11 + a02 * b21; + + if (!det) { + return null; + } + det = 1.0 / det; + + out.m00 = b01 * det; + out.m01 = (-a22 * a01 + a02 * a21) * det; + out.m02 = (a12 * a01 - a02 * a11) * det; + out.m03 = b11 * det; + out.m04 = (a22 * a00 - a02 * a20) * det; + out.m05 = (-a12 * a00 + a02 * a10) * det; + out.m06 = b21 * det; + out.m07 = (-a21 * a00 + a01 * a20) * det; + out.m08 = (a11 * a00 - a01 * a10) * det; + return out; + } + + /** + * Calculates the adjugate of a matrix. + * + * @param {mat3} out - Matrix to store result. + * @param {mat3} a - Matrix to calculate. + * @returns {mat3} out. + */ + static adjoint(out, a) { + let a00 = a.m00, a01 = a.m01, a02 = a.m02, + a10 = a.m03, a11 = a.m04, a12 = a.m05, + a20 = a.m06, a21 = a.m07, a22 = a.m08; + + out.m00 = (a11 * a22 - a12 * a21); + out.m01 = (a02 * a21 - a01 * a22); + out.m02 = (a01 * a12 - a02 * a11); + out.m03 = (a12 * a20 - a10 * a22); + out.m04 = (a00 * a22 - a02 * a20); + out.m05 = (a02 * a10 - a00 * a12); + out.m06 = (a10 * a21 - a11 * a20); + out.m07 = (a01 * a20 - a00 * a21); + out.m08 = (a00 * a11 - a01 * a10); + return out; + } + + /** + * Calculates the determinant of a matrix. + * + * @param {mat3} a - Matrix to calculate. + * @returns {Number} Determinant of a. + */ + static determinant(a) { + let a00 = a.m00, a01 = a.m01, a02 = a.m02, + a10 = a.m03, a11 = a.m04, a12 = a.m05, + a20 = a.m06, a21 = a.m07, a22 = a.m08; + + return a00 * (a22 * a11 - a12 * a21) + a01 * (-a22 * a10 + a12 * a20) + a02 * (a21 * a10 - a11 * a20); + } + + /** + * Multiply two matrices explicitly. + * + * @param {mat3} out - Matrix to store result. + * @param {mat3} a - The first operand. + * @param {mat3} b - The second operand. + * @returns {mat3} out. + */ + static multiply(out, a, b) { + let a00 = a.m00, a01 = a.m01, a02 = a.m02, + a10 = a.m03, a11 = a.m04, a12 = a.m05, + a20 = a.m06, a21 = a.m07, a22 = a.m08; + + let b00 = b.m00, b01 = b.m01, b02 = b.m02; + let b10 = b.m03, b11 = b.m04, b12 = b.m05; + let b20 = b.m06, b21 = b.m07, b22 = b.m08; + + out.m00 = b00 * a00 + b01 * a10 + b02 * a20; + out.m01 = b00 * a01 + b01 * a11 + b02 * a21; + out.m02 = b00 * a02 + b01 * a12 + b02 * a22; + + out.m03 = b10 * a00 + b11 * a10 + b12 * a20; + out.m04 = b10 * a01 + b11 * a11 + b12 * a21; + out.m05 = b10 * a02 + b11 * a12 + b12 * a22; + + out.m06 = b20 * a00 + b21 * a10 + b22 * a20; + out.m07 = b20 * a01 + b21 * a11 + b22 * a21; + out.m08 = b20 * a02 + b21 * a12 + b22 * a22; + return out; + } + + /** + * Alias of {@link mat3.multiply}. + */ + static mul(out, a, b) { + return mat3.multiply(out, a, b); + } + + /** + * Multiply a matrix with a translation matrix given by a translation offset. + * + * @param {mat3} out - Matrix to store result. + * @param {mat3} a - Matrix to multiply. + * @param {vec2} v - The translation offset. + * @returns {mat3} out. + */ + static translate(out, a, v) { + let a00 = a.m00, a01 = a.m01, a02 = a.m02, + a10 = a.m03, a11 = a.m04, a12 = a.m05, + a20 = a.m06, a21 = a.m07, a22 = a.m08; + let x = v.x, y = v.y; + + out.m00 = a00; + out.m01 = a01; + out.m02 = a02; + + out.m03 = a10; + out.m04 = a11; + out.m05 = a12; + + out.m06 = x * a00 + y * a10 + a20; + out.m07 = x * a01 + y * a11 + a21; + out.m08 = x * a02 + y * a12 + a22; + return out; + } + + /** + * Rotates a matrix by the given angle. + * + * @param {mat3} out - Matrix to store result. + * @param {mat3} a - Matrix to rotate. + * @param {Number} rad - The rotation angle. + * @returns {mat3} out + */ + static rotate(out, a, rad) { + let a00 = a.m00, a01 = a.m01, a02 = a.m02, + a10 = a.m03, a11 = a.m04, a12 = a.m05, + a20 = a.m06, a21 = a.m07, a22 = a.m08; + + let s = Math.sin(rad); + let c = Math.cos(rad); + + out.m00 = c * a00 + s * a10; + out.m01 = c * a01 + s * a11; + out.m02 = c * a02 + s * a12; + + out.m03 = c * a10 - s * a00; + out.m04 = c * a11 - s * a01; + out.m05 = c * a12 - s * a02; + + out.m06 = a20; + out.m07 = a21; + out.m08 = a22; + return out; + } + + /** + * Multiply a matrix with a scale matrix given by a scale vector. + * + * @param {mat3} out - Matrix to store result. + * @param {mat3} a - Matrix to multiply. + * @param {vec2} v - The scale vector. + * @returns {mat3} out + **/ + static scale(out, a, v) { + let x = v.x, y = v.y; + + out.m00 = x * a.m00; + out.m01 = x * a.m01; + out.m02 = x * a.m02; + + out.m03 = y * a.m03; + out.m04 = y * a.m04; + out.m05 = y * a.m05; + + out.m06 = a.m06; + out.m07 = a.m07; + out.m08 = a.m08; + return out; + } + + /** + * Copies the upper-left 3x3 values of a 4x4 matrix into a 3x3 matrix. + * + * @param {mat3} out - Matrix to store result. + * @param {mat4} a - The 4x4 matrix. + * @returns {mat3} out. + */ + static fromMat4(out, a) { + out.m00 = a.m00; + out.m01 = a.m01; + out.m02 = a.m02; + out.m03 = a.m04; + out.m04 = a.m05; + out.m05 = a.m06; + out.m06 = a.m08; + out.m07 = a.m09; + out.m08 = a.m10; + return out; + } + + /** + * Creates a matrix from a translation offset. + * This is equivalent to (but much faster than): + * + * mat3.identity(dest); + * mat3.translate(dest, dest, vec); + * + * @param {mat3} out - Matrix to store result. + * @param {vec2} v - The translation offset. + * @returns {mat3} out. + */ + static fromTranslation(out, v) { + out.m00 = 1; + out.m01 = 0; + out.m02 = 0; + out.m03 = 0; + out.m04 = 1; + out.m05 = 0; + out.m06 = v.x; + out.m07 = v.y; + out.m08 = 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 - Matrix to store result. + * @param {Number} rad - The rotation angle. + * @returns {mat3} out. + */ + static fromRotation(out, rad) { + let s = Math.sin(rad), c = Math.cos(rad); + + out.m00 = c; + out.m01 = s; + out.m02 = 0; + + out.m03 = -s; + out.m04 = c; + out.m05 = 0; + + out.m06 = 0; + out.m07 = 0; + out.m08 = 1; + return out; + } + + /** + * Creates a matrix from a scale vector. + * This is equivalent to (but much faster than): + * + * mat3.identity(dest); + * mat3.scale(dest, dest, vec); + * + * @param {mat3} out - Matrix to store result. + * @param {vec2} v - Scale vector. + * @returns {mat3} out. + */ + static fromScaling(out, v) { + out.m00 = v.x; + out.m01 = 0; + out.m02 = 0; + + out.m03 = 0; + out.m04 = v.y; + out.m05 = 0; + + out.m06 = 0; + out.m07 = 0; + out.m08 = 1; + return out; + } + + /** + * Copies the values from a 2x3 matrix into a 3x3 matrix. + * + * @param {mat3} out - Matrix to store result. + * @param {mat23} a - The 2x3 matrix. + * @returns {mat3} out. + **/ + static fromMat2d(out, a) { + out.m00 = a.m00; + out.m01 = a.m01; + out.m02 = 0; + + out.m03 = a.m02; + out.m04 = a.m03; + out.m05 = 0; + + out.m06 = a.m04; + out.m07 = a.m05; + out.m08 = 1; + return out; + } + + /** + * Calculates a 3x3 matrix from the given quaternion. + * + * @param {mat3} out - Matrix to store result. + * @param {quat} q - The quaternion. + * + * @returns {mat3} out. + */ + static fromQuat(out, q) { + let x = q.x, y = q.y, z = q.z, w = q.w; + let x2 = x + x; + let y2 = y + y; + let z2 = z + z; + + let xx = x * x2; + let yx = y * x2; + let yy = y * y2; + let zx = z * x2; + let zy = z * y2; + let zz = z * z2; + let wx = w * x2; + let wy = w * y2; + let wz = w * z2; + + out.m00 = 1 - yy - zz; + out.m03 = yx - wz; + out.m06 = zx + wy; + + out.m01 = yx + wz; + out.m04 = 1 - xx - zz; + out.m07 = zy - wx; + + out.m02 = zx - wy; + out.m05 = zy + wx; + out.m08 = 1 - xx - yy; + + return out; + } + + /** + * Calculates a 3x3 matrix from view direction and up direction. + * + * @param {mat3} out - Matrix to store result. + * @param {vec3} view - View direction (must be normalized). + * @param {vec3} [up] - Up direction, default is (0,1,0) (must be normalized). + * + * @returns {mat3} out + */ + static fromViewUp(out, view, up) { + let _fromViewUpIIFE = (function () { + let default_up = vec3.create(0, 1, 0); + let x = vec3.create(0, 0, 0); + let y = vec3.create(0, 0, 0); + + return function (out, view, up) { + if (vec3.sqrMag(view) < EPSILON * EPSILON) { + mat3.identity(out); + return out; + } + + up = up || default_up; + vec3.normalize(x, vec3.cross(x, up, view)); + + if (vec3.sqrMag(x) < EPSILON * EPSILON) { + mat3.identity(out); + return out; + } + + vec3.cross(y, view, x); + mat3.set( + out, + x.x, x.y, x.z, + y.x, y.y, y.z, + view.x, view.y, view.z + ); + + return out; + }; + })(); + return _fromViewUpIIFE(out, view, up); + } + + /** + * Calculates a 3x3 normal matrix (transpose inverse) from the 4x4 matrix. + * + * @param {mat3} out - Matrix to store result. + * @param {mat4} a - A 4x4 matrix to derive the normal matrix from. + * + * @returns {mat3} out. + */ + static normalFromMat4(out, a) { + let a00 = a.m00, a01 = a.m01, a02 = a.m02, a03 = a.m03, + a10 = a.m04, a11 = a.m05, a12 = a.m06, a13 = a.m07, + a20 = a.m08, a21 = a.m09, a22 = a.m10, a23 = a.m11, + a30 = a.m12, a31 = a.m13, a32 = a.m14, a33 = a.m15; + + let b00 = a00 * a11 - a01 * a10; + let b01 = a00 * a12 - a02 * a10; + let b02 = a00 * a13 - a03 * a10; + let b03 = a01 * a12 - a02 * a11; + let b04 = a01 * a13 - a03 * a11; + let b05 = a02 * a13 - a03 * a12; + let b06 = a20 * a31 - a21 * a30; + let b07 = a20 * a32 - a22 * a30; + let b08 = a20 * a33 - a23 * a30; + let b09 = a21 * a32 - a22 * a31; + let b10 = a21 * a33 - a23 * a31; + let b11 = a22 * a33 - a23 * a32; + + // Calculate the determinant + let det = b00 * b11 - b01 * b10 + b02 * b09 + b03 * b08 - b04 * b07 + b05 * b06; + + if (!det) { + return null; + } + det = 1.0 / det; + + out.m00 = (a11 * b11 - a12 * b10 + a13 * b09) * det; + out.m01 = (a12 * b08 - a10 * b11 - a13 * b07) * det; + out.m02 = (a10 * b10 - a11 * b08 + a13 * b06) * det; + + out.m03 = (a02 * b10 - a01 * b11 - a03 * b09) * det; + out.m04 = (a00 * b11 - a02 * b08 + a03 * b07) * det; + out.m05 = (a01 * b08 - a00 * b10 - a03 * b06) * det; + + out.m06 = (a31 * b05 - a32 * b04 + a33 * b03) * det; + out.m07 = (a32 * b02 - a30 * b05 - a33 * b01) * det; + out.m08 = (a30 * b04 - a31 * b02 + a33 * b00) * det; + + return out; + } + + /** + * Returns a string representation of a matrix. + * + * @param {mat3} a - The matrix. + * @returns {String} String representation of this matrix. + */ + static str(a) { + return `mat3(${a.m00}, ${a.m01}, ${a.m02}, ${a.m03}, ${a.m04}, ${a.m05}, ${a.m06}, ${a.m07}, ${a.m08})`; + } + + /** + * Store elements of a matrix into array. + * + * @param {array} out - Array to store result. + * @param {mat3} m - The matrix. + * @returns {Array} out. + */ + static array(out, m) { + out[0] = m.m00; + out[1] = m.m01; + out[2] = m.m02; + out[3] = m.m03; + out[4] = m.m04; + out[5] = m.m05; + out[6] = m.m06; + out[7] = m.m07; + out[8] = m.m08; + + return out; + } + + /** + * Returns Frobenius norm of a matrix. + * + * @param {mat3} a - Matrix to calculate Frobenius norm of. + * @returns {Number} - The frobenius norm. + */ + static frob(a) { + return (Math.sqrt(Math.pow(a.m00, 2) + Math.pow(a.m01, 2) + Math.pow(a.m02, 2) + Math.pow(a.m03, 2) + Math.pow(a.m04, 2) + Math.pow(a.m05, 2) + Math.pow(a.m06, 2) + Math.pow(a.m07, 2) + Math.pow(a.m08, 2))); + } + + /** + * Adds two matrices. + * + * @param {mat3} out - Matrix to store result. + * @param {mat3} a - The first operand. + * @param {mat3} b - The second operand. + * @returns {mat3} out. + */ + static add(out, a, b) { + out.m00 = a.m00 + b.m00; + out.m01 = a.m01 + b.m01; + out.m02 = a.m02 + b.m02; + out.m03 = a.m03 + b.m03; + out.m04 = a.m04 + b.m04; + out.m05 = a.m05 + b.m05; + out.m06 = a.m06 + b.m06; + out.m07 = a.m07 + b.m07; + out.m08 = a.m08 + b.m08; + return out; + } + + /** + * Subtracts matrix b from matrix a. + * + * @param {mat3} out - Matrix to store result. + * @param {mat3} a - The first operand. + * @param {mat3} b - The second operand. + * @returns {mat3} out. + */ + static subtract(out, a, b) { + out.m00 = a.m00 - b.m00; + out.m01 = a.m01 - b.m01; + out.m02 = a.m02 - b.m02; + out.m03 = a.m03 - b.m03; + out.m04 = a.m04 - b.m04; + out.m05 = a.m05 - b.m05; + out.m06 = a.m06 - b.m06; + out.m07 = a.m07 - b.m07; + out.m08 = a.m08 - b.m08; + return out; + } + + /** + * Alias of {@link mat3.subtract}. + */ + static sub(out, a, b) { + return mat3.subtract(out, a, b); + } + + /** + * Multiply each element of a matrix by a scalar number. + * + * @param {mat3} out - Matrix to store result. + * @param {mat3} a - Matrix to scale + * @param {Number} b - The scale number. + * @returns {mat3} out. + */ + static multiplyScalar(out, a, b) { + out.m00 = a.m00 * b; + out.m01 = a.m01 * b; + out.m02 = a.m02 * b; + out.m03 = a.m03 * b; + out.m04 = a.m04 * b; + out.m05 = a.m05 * b; + out.m06 = a.m06 * b; + out.m07 = a.m07 * b; + out.m08 = a.m08 * b; + return out; + } + + /** + * Adds two matrices after multiplying each element of the second operand by a scalar number. + * + * @param {mat3} out - Matrix to store result. + * @param {mat3} a - The first operand. + * @param {mat3} b - The second operand. + * @param {Number} scale - The scale number. + * @returns {mat3} out. + */ + static multiplyScalarAndAdd(out, a, b, scale) { + out.m00 = a.m00 + (b.m00 * scale); + out.m01 = a.m01 + (b.m01 * scale); + out.m02 = a.m02 + (b.m02 * scale); + out.m03 = a.m03 + (b.m03 * scale); + out.m04 = a.m04 + (b.m04 * scale); + out.m05 = a.m05 + (b.m05 * scale); + out.m06 = a.m06 + (b.m06 * scale); + out.m07 = a.m07 + (b.m07 * scale); + out.m08 = a.m08 + (b.m08 * scale); + return out; + } + + /** + * Returns whether the specified matrices are equal. (Compared using ===) + * + * @param {mat3} a - The first matrix. + * @param {mat3} b - The second matrix. + * @returns {Boolean} True if the matrices are equal, false otherwise. + */ + static exactEquals(a, b) { + return a.m00 === b.m00 && a.m01 === b.m01 && a.m02 === b.m02 && + a.m03 === b.m03 && a.m04 === b.m04 && a.m05 === b.m05 && + a.m06 === b.m06 && a.m07 === b.m07 && a.m08 === b.m08; + } + + /** + * Returns whether the specified matrices are approximately equal. + * + * @param {mat3} a - The first matrix. + * @param {mat3} b - The second matrix. + * @returns {Boolean} True if the matrices are equal, false otherwise. + */ + static equals(a, b) { + let a0 = a.m00, a1 = a.m01, a2 = a.m02, a3 = a.m03, a4 = a.m04, a5 = a.m05, a6 = a.m06, a7 = a.m07, a8 = a.m08; + let b0 = b.m00, b1 = b.m01, b2 = b.m02, b3 = b.m03, b4 = b.m04, b5 = b.m05, b6 = b.m06, b7 = b.m07, b8 = b.m08; + return ( + Math.abs(a0 - b0) <= EPSILON * Math.max(1.0, Math.abs(a0), Math.abs(b0)) && + Math.abs(a1 - b1) <= EPSILON * Math.max(1.0, Math.abs(a1), Math.abs(b1)) && + Math.abs(a2 - b2) <= EPSILON * Math.max(1.0, Math.abs(a2), Math.abs(b2)) && + Math.abs(a3 - b3) <= EPSILON * Math.max(1.0, Math.abs(a3), Math.abs(b3)) && + Math.abs(a4 - b4) <= EPSILON * Math.max(1.0, Math.abs(a4), Math.abs(b4)) && + Math.abs(a5 - b5) <= EPSILON * Math.max(1.0, Math.abs(a5), Math.abs(b5)) && + Math.abs(a6 - b6) <= EPSILON * Math.max(1.0, Math.abs(a6), Math.abs(b6)) && + Math.abs(a7 - b7) <= EPSILON * Math.max(1.0, Math.abs(a7), Math.abs(b7)) && + Math.abs(a8 - b8) <= EPSILON * Math.max(1.0, Math.abs(a8), Math.abs(b8)) + ); + } +} + +export default mat3; \ No newline at end of file diff --git a/cocos/vmath/mat4.js b/cocos/vmath/mat4.js new file mode 100644 index 0000000000..08568f3d9b --- /dev/null +++ b/cocos/vmath/mat4.js @@ -0,0 +1,1729 @@ +import { EPSILON } from './utils'; + +/** + * Mathematical 4x4 matrix. + * + * NOTE: we use column-major matrix for all matrix calculation. + * + * This may lead to some confusion when referencing OpenGL documentation, + * however, which represents out all matricies in column-major format. + * This means that while in code a matrix may be typed out as: + * + * [1, 0, 0, 0, + * 0, 1, 0, 0, + * 0, 0, 1, 0, + * x, y, z, 0] + * + * The same matrix in the [OpenGL documentation](https://www.khronos.org/registry/OpenGL-Refpages/gl2.1/xhtml/glTranslate.xml) + * is written as: + * + * 1 0 0 x + * 0 1 0 y + * 0 0 1 z + * 0 0 0 0 + * + * Please rest assured, however, that they are the same thing! + * This is not unique to glMatrix, either, as OpenGL developers have long been confused by the + * apparent lack of consistency between the memory layout and the documentation. + */ +class mat4 { + /** + * Creates a matrix, with elements specified separately. + * + * @param {Number} m00 - Value assigned to element at column 0 row 0. + * @param {Number} m01 - Value assigned to element at column 0 row 1. + * @param {Number} m02 - Value assigned to element at column 0 row 2. + * @param {Number} m03 - Value assigned to element at column 0 row 3. + * @param {Number} m04 - Value assigned to element at column 1 row 0. + * @param {Number} m05 - Value assigned to element at column 1 row 1. + * @param {Number} m06 - Value assigned to element at column 1 row 2. + * @param {Number} m07 - Value assigned to element at column 1 row 3. + * @param {Number} m08 - Value assigned to element at column 2 row 0. + * @param {Number} m09 - Value assigned to element at column 2 row 1. + * @param {Number} m10 - Value assigned to element at column 2 row 2. + * @param {Number} m11 - Value assigned to element at column 2 row 3. + * @param {Number} m12 - Value assigned to element at column 3 row 0. + * @param {Number} m13 - Value assigned to element at column 3 row 1. + * @param {Number} m14 - Value assigned to element at column 3 row 2. + * @param {Number} m15 - Value assigned to element at column 3 row 3. + */ + constructor( + m00 = 1, m01 = 0, m02 = 0, m03 = 0, + m04 = 0, m05 = 1, m06 = 0, m07 = 0, + m08 = 0, m09 = 0, m10 = 1, m11 = 0, + m12 = 0, m13 = 0, m14 = 0, m15 = 1 + ) { + /** + * The element at column 0 row 0. + * @type {number} + * */ + this.m00 = m00; + + /** + * The element at column 0 row 1. + * @type {number} + * */ + this.m01 = m01; + + /** + * The element at column 0 row 2. + * @type {number} + * */ + this.m02 = m02; + + /** + * The element at column 0 row 3. + * @type {number} + * */ + this.m03 = m03; + + /** + * The element at column 1 row 0. + * @type {number} + * */ + this.m04 = m04; + + /** + * The element at column 1 row 1. + * @type {number} + * */ + this.m05 = m05; + + /** + * The element at column 1 row 2. + * @type {number} + * */ + this.m06 = m06; + + /** + * The element at column 1 row 3. + * @type {number} + * */ + this.m07 = m07; + + /** + * The element at column 2 row 0. + * @type {number} + * */ + this.m08 = m08; + + /** + * The element at column 2 row 1. + * @type {number} + * */ + this.m09 = m09; + + /** + * The element at column 2 row 2. + * @type {number} + * */ + this.m10 = m10; + + /** + * The element at column 2 row 3. + * @type {number} + * */ + this.m11 = m11; + + /** + * The element at column 3 row 0. + * @type {number} + * */ + this.m12 = m12; + + /** + * The element at column 3 row 1. + * @type {number} + * */ + this.m13 = m13; + + /** + * The element at column 3 row 2. + * @type {number} + * */ + this.m14 = m14; + + /** + * The element at column 3 row 3. + * @type {number} + * */ + this.m15 = m15; + } + + /** + * Creates a matrix, with elements specified separately. + * + * @param {Number} m00 - Value assigned to element at column 0 row 0. + * @param {Number} m01 - Value assigned to element at column 0 row 1. + * @param {Number} m02 - Value assigned to element at column 0 row 2. + * @param {Number} m03 - Value assigned to element at column 0 row 3. + * @param {Number} m04 - Value assigned to element at column 1 row 0. + * @param {Number} m05 - Value assigned to element at column 1 row 1. + * @param {Number} m06 - Value assigned to element at column 1 row 2. + * @param {Number} m07 - Value assigned to element at column 1 row 3. + * @param {Number} m08 - Value assigned to element at column 2 row 0. + * @param {Number} m09 - Value assigned to element at column 2 row 1. + * @param {Number} m10 - Value assigned to element at column 2 row 2. + * @param {Number} m11 - Value assigned to element at column 2 row 3. + * @param {Number} m12 - Value assigned to element at column 3 row 0. + * @param {Number} m13 - Value assigned to element at column 3 row 1. + * @param {Number} m14 - Value assigned to element at column 3 row 2. + * @param {Number} m15 - Value assigned to element at column 3 row 3. + * @returns {mat4} The newly created matrix. + */ + static create( + m00 = 1, m01 = 0, m02 = 0, m03 = 0, + m04 = 0, m05 = 1, m06 = 0, m07 = 0, + m08 = 0, m09 = 0, m10 = 1, m11 = 0, + m12 = 0, m13 = 0, m14 = 0, m15 = 1 + ) { + return new mat4( + m00, m01, m02, m03, + m04, m05, m06, m07, + m08, m09, m10, m11, + m12, m13, m14, m15); + } + + /** + * Clone a matrix. + * + * @param {mat4} a - Matrix to clone. + * @returns {mat4} The newly created matrix. + */ + static clone(a) { + return new mat4( + a.m00, a.m01, a.m02, a.m03, + a.m04, a.m05, a.m06, a.m07, + a.m08, a.m09, a.m10, a.m11, + a.m12, a.m13, a.m14, a.m15 + ); + } + + /** + * Copy content of a matrix into another. + * + * @param {mat4} out - Matrix to modified. + * @param {mat4} a - The specified matrix. + * @returns {mat4} out. + */ + static copy(out, a) { + out.m00 = a.m00; + out.m01 = a.m01; + out.m02 = a.m02; + out.m03 = a.m03; + out.m04 = a.m04; + out.m05 = a.m05; + out.m06 = a.m06; + out.m07 = a.m07; + out.m08 = a.m08; + out.m09 = a.m09; + out.m10 = a.m10; + out.m11 = a.m11; + out.m12 = a.m12; + out.m13 = a.m13; + out.m14 = a.m14; + out.m15 = a.m15; + return out; + } + + /** + * Sets the elements of a matrix to the given values. + * + * @param {mat4} out - The matrix to modified. + * @param {Number} m00 - Value assigned to element at column 0 row 0. + * @param {Number} m01 - Value assigned to element at column 0 row 1. + * @param {Number} m02 - Value assigned to element at column 0 row 2. + * @param {Number} m03 - Value assigned to element at column 0 row 3. + * @param {Number} m10 - Value assigned to element at column 1 row 0. + * @param {Number} m11 - Value assigned to element at column 1 row 1. + * @param {Number} m12 - Value assigned to element at column 1 row 2. + * @param {Number} m13 - Value assigned to element at column 1 row 3. + * @param {Number} m20 - Value assigned to element at column 2 row 0. + * @param {Number} m21 - Value assigned to element at column 2 row 1. + * @param {Number} m22 - Value assigned to element at column 2 row 2. + * @param {Number} m23 - Value assigned to element at column 2 row 3. + * @param {Number} m30 - Value assigned to element at column 3 row 0. + * @param {Number} m31 - Value assigned to element at column 3 row 1. + * @param {Number} m32 - Value assigned to element at column 3 row 2. + * @param {Number} m33 - Value assigned to element at column 3 row 3. + * @returns {mat4} out. + */ + static set(out, m00, m01, m02, m03, m10, m11, m12, m13, m20, m21, m22, m23, m30, m31, m32, m33) { + out.m00 = m00; + out.m01 = m01; + out.m02 = m02; + out.m03 = m03; + out.m04 = m10; + out.m05 = m11; + out.m06 = m12; + out.m07 = m13; + out.m08 = m20; + out.m09 = m21; + out.m10 = m22; + out.m11 = m23; + out.m12 = m30; + out.m13 = m31; + out.m14 = m32; + out.m15 = m33; + return out; + } + + + /** + * Sets a matrix as identity matrix. + * + * @param {mat4} out - Matrix to modified. + * @returns {mat4} out. + */ + static identity(out) { + out.m00 = 1; + out.m01 = 0; + out.m02 = 0; + out.m03 = 0; + out.m04 = 0; + out.m05 = 1; + out.m06 = 0; + out.m07 = 0; + out.m08 = 0; + out.m09 = 0; + out.m10 = 1; + out.m11 = 0; + out.m12 = 0; + out.m13 = 0; + out.m14 = 0; + out.m15 = 1; + return out; + } + + /** + * Transposes a matrix. + * + * @param {mat4} out - Matrix to store result. + * @param {mat4} a - Matrix to transpose. + * @returns {mat4} out. + */ + static transpose(out, a) { + // If we are transposing ourselves we can skip a few steps but have to cache some values + if (out === a) { + let a01 = a.m01, a02 = a.m02, a03 = a.m03, + a12 = a.m06, a13 = a.m07, + a23 = a.m11; + + out.m01 = a.m04; + out.m02 = a.m08; + out.m03 = a.m12; + out.m04 = a01; + out.m06 = a.m09; + out.m07 = a.m13; + out.m08 = a02; + out.m09 = a12; + out.m11 = a.m14; + out.m12 = a03; + out.m13 = a13; + out.m14 = a23; + } else { + out.m00 = a.m00; + out.m01 = a.m04; + out.m02 = a.m08; + out.m03 = a.m12; + out.m04 = a.m01; + out.m05 = a.m05; + out.m06 = a.m09; + out.m07 = a.m13; + out.m08 = a.m02; + out.m09 = a.m06; + out.m10 = a.m10; + out.m11 = a.m14; + out.m12 = a.m03; + out.m13 = a.m07; + out.m14 = a.m11; + out.m15 = a.m15; + } + + return out; + } + + /** + * Inverts a matrix. + * + * @param {mat4} out - Matrix to store result. + * @param {mat4} a - Matrix to invert. + * @returns {mat4} out. + */ + static invert(out, a) { + let a00 = a.m00, a01 = a.m01, a02 = a.m02, a03 = a.m03, + a10 = a.m04, a11 = a.m05, a12 = a.m06, a13 = a.m07, + a20 = a.m08, a21 = a.m09, a22 = a.m10, a23 = a.m11, + a30 = a.m12, a31 = a.m13, a32 = a.m14, a33 = a.m15; + + let b00 = a00 * a11 - a01 * a10; + let b01 = a00 * a12 - a02 * a10; + let b02 = a00 * a13 - a03 * a10; + let b03 = a01 * a12 - a02 * a11; + let b04 = a01 * a13 - a03 * a11; + let b05 = a02 * a13 - a03 * a12; + let b06 = a20 * a31 - a21 * a30; + let b07 = a20 * a32 - a22 * a30; + let b08 = a20 * a33 - a23 * a30; + let b09 = a21 * a32 - a22 * a31; + let b10 = a21 * a33 - a23 * a31; + let b11 = a22 * a33 - a23 * a32; + + // Calculate the determinant + let det = b00 * b11 - b01 * b10 + b02 * b09 + b03 * b08 - b04 * b07 + b05 * b06; + + if (!det) { + return null; + } + det = 1.0 / det; + + out.m00 = (a11 * b11 - a12 * b10 + a13 * b09) * det; + out.m01 = (a02 * b10 - a01 * b11 - a03 * b09) * det; + out.m02 = (a31 * b05 - a32 * b04 + a33 * b03) * det; + out.m03 = (a22 * b04 - a21 * b05 - a23 * b03) * det; + out.m04 = (a12 * b08 - a10 * b11 - a13 * b07) * det; + out.m05 = (a00 * b11 - a02 * b08 + a03 * b07) * det; + out.m06 = (a32 * b02 - a30 * b05 - a33 * b01) * det; + out.m07 = (a20 * b05 - a22 * b02 + a23 * b01) * det; + out.m08 = (a10 * b10 - a11 * b08 + a13 * b06) * det; + out.m09 = (a01 * b08 - a00 * b10 - a03 * b06) * det; + out.m10 = (a30 * b04 - a31 * b02 + a33 * b00) * det; + out.m11 = (a21 * b02 - a20 * b04 - a23 * b00) * det; + out.m12 = (a11 * b07 - a10 * b09 - a12 * b06) * det; + out.m13 = (a00 * b09 - a01 * b07 + a02 * b06) * det; + out.m14 = (a31 * b01 - a30 * b03 - a32 * b00) * det; + out.m15 = (a20 * b03 - a21 * b01 + a22 * b00) * det; + + return out; + } + + /** + * Calculates the adjugate of a matrix. + * + * @param {mat4} out - Matrix to store result. + * @param {mat4} a - Matrix to calculate. + * @returns {mat4} out. + */ + static adjoint(out, a) { + let a00 = a.m00, a01 = a.m01, a02 = a.m02, a03 = a.m03, + a10 = a.m04, a11 = a.m05, a12 = a.m06, a13 = a.m07, + a20 = a.m08, a21 = a.m09, a22 = a.m10, a23 = a.m11, + a30 = a.m12, a31 = a.m13, a32 = a.m14, a33 = a.m15; + + out.m00 = (a11 * (a22 * a33 - a23 * a32) - a21 * (a12 * a33 - a13 * a32) + a31 * (a12 * a23 - a13 * a22)); + out.m01 = -(a01 * (a22 * a33 - a23 * a32) - a21 * (a02 * a33 - a03 * a32) + a31 * (a02 * a23 - a03 * a22)); + out.m02 = (a01 * (a12 * a33 - a13 * a32) - a11 * (a02 * a33 - a03 * a32) + a31 * (a02 * a13 - a03 * a12)); + out.m03 = -(a01 * (a12 * a23 - a13 * a22) - a11 * (a02 * a23 - a03 * a22) + a21 * (a02 * a13 - a03 * a12)); + out.m04 = -(a10 * (a22 * a33 - a23 * a32) - a20 * (a12 * a33 - a13 * a32) + a30 * (a12 * a23 - a13 * a22)); + out.m05 = (a00 * (a22 * a33 - a23 * a32) - a20 * (a02 * a33 - a03 * a32) + a30 * (a02 * a23 - a03 * a22)); + out.m06 = -(a00 * (a12 * a33 - a13 * a32) - a10 * (a02 * a33 - a03 * a32) + a30 * (a02 * a13 - a03 * a12)); + out.m07 = (a00 * (a12 * a23 - a13 * a22) - a10 * (a02 * a23 - a03 * a22) + a20 * (a02 * a13 - a03 * a12)); + out.m08 = (a10 * (a21 * a33 - a23 * a31) - a20 * (a11 * a33 - a13 * a31) + a30 * (a11 * a23 - a13 * a21)); + out.m09 = -(a00 * (a21 * a33 - a23 * a31) - a20 * (a01 * a33 - a03 * a31) + a30 * (a01 * a23 - a03 * a21)); + out.m10 = (a00 * (a11 * a33 - a13 * a31) - a10 * (a01 * a33 - a03 * a31) + a30 * (a01 * a13 - a03 * a11)); + out.m11 = -(a00 * (a11 * a23 - a13 * a21) - a10 * (a01 * a23 - a03 * a21) + a20 * (a01 * a13 - a03 * a11)); + out.m12 = -(a10 * (a21 * a32 - a22 * a31) - a20 * (a11 * a32 - a12 * a31) + a30 * (a11 * a22 - a12 * a21)); + out.m13 = (a00 * (a21 * a32 - a22 * a31) - a20 * (a01 * a32 - a02 * a31) + a30 * (a01 * a22 - a02 * a21)); + out.m14 = -(a00 * (a11 * a32 - a12 * a31) - a10 * (a01 * a32 - a02 * a31) + a30 * (a01 * a12 - a02 * a11)); + out.m15 = (a00 * (a11 * a22 - a12 * a21) - a10 * (a01 * a22 - a02 * a21) + a20 * (a01 * a12 - a02 * a11)); + return out; + } + + /** + * Calculates the determinant of a matrix. + * + * @param {mat4} a - Matrix to calculate. + * @returns {Number} Determinant of a. + */ + static determinant(a) { + let a00 = a.m00, a01 = a.m01, a02 = a.m02, a03 = a.m03, + a10 = a.m04, a11 = a.m05, a12 = a.m06, a13 = a.m07, + a20 = a.m08, a21 = a.m09, a22 = a.m10, a23 = a.m11, + a30 = a.m12, a31 = a.m13, a32 = a.m14, a33 = a.m15; + + let b00 = a00 * a11 - a01 * a10; + let b01 = a00 * a12 - a02 * a10; + let b02 = a00 * a13 - a03 * a10; + let b03 = a01 * a12 - a02 * a11; + let b04 = a01 * a13 - a03 * a11; + let b05 = a02 * a13 - a03 * a12; + let b06 = a20 * a31 - a21 * a30; + let b07 = a20 * a32 - a22 * a30; + let b08 = a20 * a33 - a23 * a30; + let b09 = a21 * a32 - a22 * a31; + let b10 = a21 * a33 - a23 * a31; + let b11 = a22 * a33 - a23 * a32; + + // Calculate the determinant + return b00 * b11 - b01 * b10 + b02 * b09 + b03 * b08 - b04 * b07 + b05 * b06; + } + + /** + * Multiply two matrices explicitly. + * + * @param {mat4} out - Matrix to store result. + * @param {mat4} a - The first operand. + * @param {mat4} b - The second operand. + * @returns {mat4} out. + */ + static multiply(out, a, b) { + let a00 = a.m00, a01 = a.m01, a02 = a.m02, a03 = a.m03, + a10 = a.m04, a11 = a.m05, a12 = a.m06, a13 = a.m07, + a20 = a.m08, a21 = a.m09, a22 = a.m10, a23 = a.m11, + a30 = a.m12, a31 = a.m13, a32 = a.m14, a33 = a.m15; + + // Cache only the current line of the second matrix + let b0 = b.m00, b1 = b.m01, b2 = b.m02, b3 = b.m03; + out.m00 = b0 * a00 + b1 * a10 + b2 * a20 + b3 * a30; + out.m01 = b0 * a01 + b1 * a11 + b2 * a21 + b3 * a31; + out.m02 = b0 * a02 + b1 * a12 + b2 * a22 + b3 * a32; + out.m03 = b0 * a03 + b1 * a13 + b2 * a23 + b3 * a33; + + b0 = b.m04; b1 = b.m05; b2 = b.m06; b3 = b.m07; + out.m04 = b0 * a00 + b1 * a10 + b2 * a20 + b3 * a30; + out.m05 = b0 * a01 + b1 * a11 + b2 * a21 + b3 * a31; + out.m06 = b0 * a02 + b1 * a12 + b2 * a22 + b3 * a32; + out.m07 = b0 * a03 + b1 * a13 + b2 * a23 + b3 * a33; + + b0 = b.m08; b1 = b.m09; b2 = b.m10; b3 = b.m11; + out.m08 = b0 * a00 + b1 * a10 + b2 * a20 + b3 * a30; + out.m09 = b0 * a01 + b1 * a11 + b2 * a21 + b3 * a31; + out.m10 = b0 * a02 + b1 * a12 + b2 * a22 + b3 * a32; + out.m11 = b0 * a03 + b1 * a13 + b2 * a23 + b3 * a33; + + b0 = b.m12; b1 = b.m13; b2 = b.m14; b3 = b.m15; + out.m12 = b0 * a00 + b1 * a10 + b2 * a20 + b3 * a30; + out.m13 = b0 * a01 + b1 * a11 + b2 * a21 + b3 * a31; + out.m14 = b0 * a02 + b1 * a12 + b2 * a22 + b3 * a32; + out.m15 = b0 * a03 + b1 * a13 + b2 * a23 + b3 * a33; + return out; + } + + /** + * Alias of {@link mat4.multiply}. + */ + static mul(out, a, b) { + return mat4.multiply(out, a, b); + } + + /** + * Multiply a matrix with a translation matrix given by a translation offset. + * + * @param {mat4} out - Matrix to store result. + * @param {mat4} a - Matrix to multiply. + * @param {vec3} v - The translation offset. + * @returns {mat4} out. + */ + static translate(out, a, v) { + let x = v.x, y = v.y, z = v.z, + a00, a01, a02, a03, + a10, a11, a12, a13, + a20, a21, a22, a23; + + if (a === out) { + out.m12 = a.m00 * x + a.m04 * y + a.m08 * z + a.m12; + out.m13 = a.m01 * x + a.m05 * y + a.m09 * z + a.m13; + out.m14 = a.m02 * x + a.m06 * y + a.m10 * z + a.m14; + out.m15 = a.m03 * x + a.m07 * y + a.m11 * z + a.m15; + } else { + a00 = a.m00; a01 = a.m01; a02 = a.m02; a03 = a.m03; + a10 = a.m04; a11 = a.m05; a12 = a.m06; a13 = a.m07; + a20 = a.m08; a21 = a.m09; a22 = a.m10; a23 = a.m11; + + out.m00 = a00; out.m01 = a01; out.m02 = a02; out.m03 = a03; + out.m04 = a10; out.m05 = a11; out.m06 = a12; out.m07 = a13; + out.m08 = a20; out.m09 = a21; out.m10 = a22; out.m11 = a23; + + out.m12 = a00 * x + a10 * y + a20 * z + a.m12; + out.m13 = a01 * x + a11 * y + a21 * z + a.m13; + out.m14 = a02 * x + a12 * y + a22 * z + a.m14; + out.m15 = a03 * x + a13 * y + a23 * z + a.m15; + } + + return out; + } + + /** + * Multiply a matrix with a scale matrix given by a scale vector. + * + * @param {mat4} out - Matrix to store result. + * @param {mat4} a - Matrix to multiply. + * @param {vec3} v - The scale vector. + * @returns {mat4} out + **/ + static scale(out, a, v) { + let x = v.x, y = v.y, z = v.z; + + out.m00 = a.m00 * x; + out.m01 = a.m01 * x; + out.m02 = a.m02 * x; + out.m03 = a.m03 * x; + out.m04 = a.m04 * y; + out.m05 = a.m05 * y; + out.m06 = a.m06 * y; + out.m07 = a.m07 * y; + out.m08 = a.m08 * z; + out.m09 = a.m09 * z; + out.m10 = a.m10 * z; + out.m11 = a.m11 * z; + out.m12 = a.m12; + out.m13 = a.m13; + out.m14 = a.m14; + out.m15 = a.m15; + return out; + } + + /** + * Multiply a matrix with a rotation matrix denotes by the rotation around arbitrary axis. + * + * @param {mat4} out - Matrix to store result. + * @param {mat4} a - Matrix to multiply. + * @param {Number} rad - The rotation angle. + * @param {vec3} axis - The rotation axis. + * @returns {mat4} out. + */ + static rotate(out, a, rad, axis) { + let x = axis.x, y = axis.y, z = axis.z; + let s, c, t, + a00, a01, a02, a03, + a10, a11, a12, a13, + a20, a21, a22, a23, + b00, b01, b02, + b10, b11, b12, + b20, b21, b22; + + let len = Math.sqrt(x * x + y * y + z * z); + + if (Math.abs(len) < 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.m00; a01 = a.m01; a02 = a.m02; a03 = a.m03; + a10 = a.m04; a11 = a.m05; a12 = a.m06; a13 = a.m07; + a20 = a.m08; a21 = a.m09; a22 = a.m10; a23 = a.m11; + + // 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.m00 = a00 * b00 + a10 * b01 + a20 * b02; + out.m01 = a01 * b00 + a11 * b01 + a21 * b02; + out.m02 = a02 * b00 + a12 * b01 + a22 * b02; + out.m03 = a03 * b00 + a13 * b01 + a23 * b02; + out.m04 = a00 * b10 + a10 * b11 + a20 * b12; + out.m05 = a01 * b10 + a11 * b11 + a21 * b12; + out.m06 = a02 * b10 + a12 * b11 + a22 * b12; + out.m07 = a03 * b10 + a13 * b11 + a23 * b12; + out.m08 = a00 * b20 + a10 * b21 + a20 * b22; + out.m09 = a01 * b20 + a11 * b21 + a21 * b22; + out.m10 = a02 * b20 + a12 * b21 + a22 * b22; + out.m11 = a03 * b20 + a13 * b21 + a23 * b22; + + // If the source and destination differ, copy the unchanged last row + if (a !== out) { + out.m12 = a.m12; + out.m13 = a.m13; + out.m14 = a.m14; + out.m15 = a.m15; + } + + return out; + } + + /** + * Multiply a matrix with a rotation matrix denotes by the rotation around x-axis. + * + * @param {mat4} out - Matrix to store result. + * @param {mat4} a - Matrix to multiply. + * @param {Number} rad - The rotation angle. + * @returns {mat4} out. + */ + static rotateX(out, a, rad) { + let s = Math.sin(rad), + c = Math.cos(rad), + a10 = a.m04, + a11 = a.m05, + a12 = a.m06, + a13 = a.m07, + a20 = a.m08, + a21 = a.m09, + a22 = a.m10, + a23 = a.m11; + + if (a !== out) { // If the source and destination differ, copy the unchanged rows + out.m00 = a.m00; + out.m01 = a.m01; + out.m02 = a.m02; + out.m03 = a.m03; + out.m12 = a.m12; + out.m13 = a.m13; + out.m14 = a.m14; + out.m15 = a.m15; + } + + // Perform axis-specific matrix multiplication + out.m04 = a10 * c + a20 * s; + out.m05 = a11 * c + a21 * s; + out.m06 = a12 * c + a22 * s; + out.m07 = a13 * c + a23 * s; + out.m08 = a20 * c - a10 * s; + out.m09 = a21 * c - a11 * s; + out.m10 = a22 * c - a12 * s; + out.m11 = a23 * c - a13 * s; + + return out; + } + + /** + * Multiply a matrix with a rotation matrix denotes by the rotation around y-axis. + * + * @param {mat4} out - Matrix to store result. + * @param {mat4} a - Matrix to multiply. + * @param {Number} rad - The rotation angle. + * @returns {mat4} out. + */ + static rotateY(out, a, rad) { + let s = Math.sin(rad), + c = Math.cos(rad), + a00 = a.m00, + a01 = a.m01, + a02 = a.m02, + a03 = a.m03, + a20 = a.m08, + a21 = a.m09, + a22 = a.m10, + a23 = a.m11; + + if (a !== out) { // If the source and destination differ, copy the unchanged rows + out.m04 = a.m04; + out.m05 = a.m05; + out.m06 = a.m06; + out.m07 = a.m07; + out.m12 = a.m12; + out.m13 = a.m13; + out.m14 = a.m14; + out.m15 = a.m15; + } + + // Perform axis-specific matrix multiplication + out.m00 = a00 * c - a20 * s; + out.m01 = a01 * c - a21 * s; + out.m02 = a02 * c - a22 * s; + out.m03 = a03 * c - a23 * s; + out.m08 = a00 * s + a20 * c; + out.m09 = a01 * s + a21 * c; + out.m10 = a02 * s + a22 * c; + out.m11 = a03 * s + a23 * c; + + return out; + } + + /** + * Multiply a matrix with a rotation matrix denotes by the rotation around z-axis. + * + * @param {mat4} out - Matrix to store result. + * @param {mat4} a - Matrix to multiply. + * @param {Number} rad - The rotation angle. + * @returns {mat4} out. + */ + static rotateZ(out, a, rad) { + let s = Math.sin(rad), + c = Math.cos(rad), + a00 = a.m00, + a01 = a.m01, + a02 = a.m02, + a03 = a.m03, + a10 = a.m04, + a11 = a.m05, + a12 = a.m06, + a13 = a.m07; + + // If the source and destination differ, copy the unchanged last row + if (a !== out) { + out.m08 = a.m08; + out.m09 = a.m09; + out.m10 = a.m10; + out.m11 = a.m11; + out.m12 = a.m12; + out.m13 = a.m13; + out.m14 = a.m14; + out.m15 = a.m15; + } + + // Perform axis-specific matrix multiplication + out.m00 = a00 * c + a10 * s; + out.m01 = a01 * c + a11 * s; + out.m02 = a02 * c + a12 * s; + out.m03 = a03 * c + a13 * s; + out.m04 = a10 * c - a00 * s; + out.m05 = a11 * c - a01 * s; + out.m06 = a12 * c - a02 * s; + out.m07 = a13 * c - a03 * s; + + return out; + } + + /** + * Create a translation matrix from a translation offset. + * This is equivalent to (but much faster than): + * + * mat4.identity(dest); + * mat4.translate(dest, dest, vec); + * + * @param {mat4} out - Matrix to store result. + * @param {vec3} v - The translation offset. + * @returns {mat4} out. + */ + static fromTranslation(out, v) { + out.m00 = 1; + out.m01 = 0; + out.m02 = 0; + out.m03 = 0; + out.m04 = 0; + out.m05 = 1; + out.m06 = 0; + out.m07 = 0; + out.m08 = 0; + out.m09 = 0; + out.m10 = 1; + out.m11 = 0; + out.m12 = v.x; + out.m13 = v.y; + out.m14 = v.z; + out.m15 = 1; + return out; + } + + /** + * Creates a scale matrix from a scale vector. + * This is equivalent to (but much faster than): + * + * mat4.identity(dest); + * mat4.scale(dest, dest, vec); + * + * @param {mat4} out - Matrix to store result. + * @param {vec3} v - The scale vector. + * @returns {mat4} out. + */ + static fromScaling(out, v) { + out.m00 = v.x; + out.m01 = 0; + out.m02 = 0; + out.m03 = 0; + out.m04 = 0; + out.m05 = v.y; + out.m06 = 0; + out.m07 = 0; + out.m08 = 0; + out.m09 = 0; + out.m10 = v.z; + out.m11 = 0; + out.m12 = 0; + out.m13 = 0; + out.m14 = 0; + out.m15 = 1; + return out; + } + + /** + * Creates a rotation matrix from the rotation around arbitrary axis. + * This is equivalent to (but much faster than): + * + * mat4.identity(dest); + * mat4.rotate(dest, dest, rad, axis); + * + * @param {mat4} out - Matrix to store result. + * @param {Number} rad - The rotation angle. + * @param {vec3} axis - The rotation axis. + * @returns {mat4} out. + */ + static fromRotation(out, rad, axis) { + let x = axis.x, y = axis.y, z = axis.z; + let len = Math.sqrt(x * x + y * y + z * z); + let s, c, t; + + if (Math.abs(len) < 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.m00 = x * x * t + c; + out.m01 = y * x * t + z * s; + out.m02 = z * x * t - y * s; + out.m03 = 0; + out.m04 = x * y * t - z * s; + out.m05 = y * y * t + c; + out.m06 = z * y * t + x * s; + out.m07 = 0; + out.m08 = x * z * t + y * s; + out.m09 = y * z * t - x * s; + out.m10 = z * z * t + c; + out.m11 = 0; + out.m12 = 0; + out.m13 = 0; + out.m14 = 0; + out.m15 = 1; + return out; + } + + /** + * Creates a rotation matrix from the rotation around x-axis. + * This is equivalent to (but much faster than): + * + * mat4.identity(dest); + * mat4.rotateX(dest, dest, rad); + * + * @param {mat4} out - Matrix to store result. + * @param {Number} rad - The rotation angle. + * @returns {mat4} out. + */ + static fromXRotation(out, rad) { + let s = Math.sin(rad), + c = Math.cos(rad); + + // Perform axis-specific matrix multiplication + out.m00 = 1; + out.m01 = 0; + out.m02 = 0; + out.m03 = 0; + out.m04 = 0; + out.m05 = c; + out.m06 = s; + out.m07 = 0; + out.m08 = 0; + out.m09 = -s; + out.m10 = c; + out.m11 = 0; + out.m12 = 0; + out.m13 = 0; + out.m14 = 0; + out.m15 = 1; + return out; + } + + /** + * Creates a rotation matrix from the rotation around y-axis. + * This is equivalent to (but much faster than): + * + * mat4.identity(dest); + * mat4.rotateY(dest, dest, rad); + * + * @param {mat4} out - Matrix to store result. + * @param {Number} rad - The rotation angle. + * @returns {mat4} out. + */ + static fromYRotation(out, rad) { + let s = Math.sin(rad), + c = Math.cos(rad); + + // Perform axis-specific matrix multiplication + out.m00 = c; + out.m01 = 0; + out.m02 = -s; + out.m03 = 0; + out.m04 = 0; + out.m05 = 1; + out.m06 = 0; + out.m07 = 0; + out.m08 = s; + out.m09 = 0; + out.m10 = c; + out.m11 = 0; + out.m12 = 0; + out.m13 = 0; + out.m14 = 0; + out.m15 = 1; + return out; + } + + /** + * Creates a rotation matrix from the rotation around z-axis. + * This is equivalent to (but much faster than): + * + * mat4.identity(dest); + * mat4.rotateZ(dest, dest, rad); + * + * @param {mat4} out - Matrix to store result. + * @param {Number} rad - The rotation angle. + * @returns {mat4} out. + */ + static fromZRotation(out, rad) { + let s = Math.sin(rad), + c = Math.cos(rad); + + // Perform axis-specific matrix multiplication + out.m00 = c; + out.m01 = s; + out.m02 = 0; + out.m03 = 0; + out.m04 = -s; + out.m05 = c; + out.m06 = 0; + out.m07 = 0; + out.m08 = 0; + out.m09 = 0; + out.m10 = 1; + out.m11 = 0; + out.m12 = 0; + out.m13 = 0; + out.m14 = 0; + out.m15 = 1; + return out; + } + + /** + * Creates a matrix from a quaternion rotation and a translation offset. + * This is equivalent to (but much faster than): + * + * mat4.identity(dest); + * mat4.translate(dest, vec); + * let quatMat = mat4.create(); + * quat.toMat4(quat, quatMat); + * mat4.multiply(dest, quatMat); + * + * @param {mat4} out - Matrix to store result. + * @param {quat} q - Rotation quaternion. + * @param {vec3} v - Translation vector. + * @returns {mat4} out. + */ + static fromRT(out, q, v) { + // Quaternion math + let x = q.x, y = q.y, z = q.z, w = q.w; + let x2 = x + x; + let y2 = y + y; + let z2 = z + z; + + let xx = x * x2; + let xy = x * y2; + let xz = x * z2; + let yy = y * y2; + let yz = y * z2; + let zz = z * z2; + let wx = w * x2; + let wy = w * y2; + let wz = w * z2; + + out.m00 = 1 - (yy + zz); + out.m01 = xy + wz; + out.m02 = xz - wy; + out.m03 = 0; + out.m04 = xy - wz; + out.m05 = 1 - (xx + zz); + out.m06 = yz + wx; + out.m07 = 0; + out.m08 = xz + wy; + out.m09 = yz - wx; + out.m10 = 1 - (xx + yy); + out.m11 = 0; + out.m12 = v.x; + out.m13 = v.y; + out.m14 = v.z; + out.m15 = 1; + + return out; + } + + /** + * Returns the translation vector component of a transformation + * matrix. If a matrix is built with fromRT, + * the returned vector will be the same as the translation offset + * originally supplied. + * @param {vec3} out - Vector to store result. + * @param {mat4} mat - Matrix to be decomposed. + * @return {vec3} out. + */ + static getTranslation(out, mat) { + out.x = mat.m12; + out.y = mat.m13; + out.z = mat.m14; + + return out; + } + + /** + * Returns the scale component of a transformation + * matrix. If a matrix is built with fromRTS + * with a normalized Quaternion parameter, the returned vector will be + * the same as the scale vector + * originally supplied. + * @param {vec3} out - Vector to store result. + * @param {mat4} mat - Matrix to be decomposed. + * @return {vec3} out. + */ + static getScaling(out, mat) { + let m11 = mat.m00, + m12 = mat.m01, + m13 = mat.m02, + m21 = mat.m04, + m22 = mat.m05, + m23 = mat.m06, + m31 = mat.m08, + m32 = mat.m09, + m33 = mat.m10; + + out.x = Math.sqrt(m11 * m11 + m12 * m12 + m13 * m13); + out.y = Math.sqrt(m21 * m21 + m22 * m22 + m23 * m23); + out.z = Math.sqrt(m31 * m31 + m32 * m32 + m33 * m33); + + return out; + } + + /** + * Returns a quaternion representing the rotational component + * of a transformation matrix. If a matrix is built with + * fromRT, the returned quaternion will be the + * same as the quaternion originally supplied. + * @param {quat} out - Quaternion to store result. + * @param {mat4} mat - Matrix to be decomposed. + * @return {quat} out. + */ + static getRotation(out, mat) { + // Algorithm taken from http://www.euclideanspace.com/maths/geometry/rotations/conversions/matrixToQuaternion/index.htm + let trace = mat.m00 + mat.m05 + mat.m10; + let S = 0; + + if (trace > 0) { + S = Math.sqrt(trace + 1.0) * 2; + out.w = 0.25 * S; + out.x = (mat.m06 - mat.m09) / S; + out.y = (mat.m08 - mat.m02) / S; + out.z = (mat.m01 - mat.m04) / S; + } else if ((mat.m00 > mat.m05) & (mat.m00 > mat.m10)) { + S = Math.sqrt(1.0 + mat.m00 - mat.m05 - mat.m10) * 2; + out.w = (mat.m06 - mat.m09) / S; + out.x = 0.25 * S; + out.y = (mat.m01 + mat.m04) / S; + out.z = (mat.m08 + mat.m02) / S; + } else if (mat.m05 > mat.m10) { + S = Math.sqrt(1.0 + mat.m05 - mat.m00 - mat.m10) * 2; + out.w = (mat.m08 - mat.m02) / S; + out.x = (mat.m01 + mat.m04) / S; + out.y = 0.25 * S; + out.z = (mat.m06 + mat.m09) / S; + } else { + S = Math.sqrt(1.0 + mat.m10 - mat.m00 - mat.m05) * 2; + out.w = (mat.m01 - mat.m04) / S; + out.x = (mat.m08 + mat.m02) / S; + out.y = (mat.m06 + mat.m09) / S; + out.z = 0.25 * S; + } + + return out; + } + + /** + * Creates a matrix from a quaternion rotation, translation offset and scale vector. + * This is equivalent to (but much faster than): + * + * mat4.identity(dest); + * mat4.translate(dest, vec); + * let quatMat = mat4.create(); + * quat.toMat4(quat, quatMat); + * mat4.multiply(dest, quatMat); + * mat4.scale(dest, scale) + * + * @param {mat4} out mat4 - Matrix to store result. + * @param {quat} q - Rotation quaternion. + * @param {vec3} v - Translation offset. + * @param {vec3} s - Scale vector. + * @returns {mat4} out. + */ + static fromRTS(out, q, v, s) { + // Quaternion math + let x = q.x, y = q.y, z = q.z, w = q.w; + let x2 = x + x; + let y2 = y + y; + let z2 = z + z; + + let xx = x * x2; + let xy = x * y2; + let xz = x * z2; + let yy = y * y2; + let yz = y * z2; + let zz = z * z2; + let wx = w * x2; + let wy = w * y2; + let wz = w * z2; + let sx = s.x; + let sy = s.y; + let sz = s.z; + + out.m00 = (1 - (yy + zz)) * sx; + out.m01 = (xy + wz) * sx; + out.m02 = (xz - wy) * sx; + out.m03 = 0; + out.m04 = (xy - wz) * sy; + out.m05 = (1 - (xx + zz)) * sy; + out.m06 = (yz + wx) * sy; + out.m07 = 0; + out.m08 = (xz + wy) * sz; + out.m09 = (yz - wx) * sz; + out.m10 = (1 - (xx + yy)) * sz; + out.m11 = 0; + out.m12 = v.x; + out.m13 = v.y; + out.m14 = v.z; + out.m15 = 1; + + return out; + } + + /** + * Creates a matrix from a quaternion rotation, translation offset and scale vector, rotating and scaling around the given origin. + * This is equivalent to (but much faster than): + * + * mat4.identity(dest); + * mat4.translate(dest, vec); + * mat4.translate(dest, origin); + * let quatMat = mat4.create(); + * quat.toMat4(quat, quatMat); + * mat4.multiply(dest, quatMat); + * mat4.scale(dest, scale) + * mat4.translate(dest, negativeOrigin); + * + * @param {mat4} out mat4 - Matrix to store result. + * @param {quat} q - Rotation quaternion. + * @param {vec3} v - Translation offset. + * @param {vec3} s - Scale vector. + * @param {vec3} o The origin vector around which to scale and rotate. + * @returns {mat4} out. + */ + static fromRTSOrigin(out, q, v, s, o) { + // Quaternion math + let x = q.x, y = q.y, z = q.z, w = q.w; + let x2 = x + x; + let y2 = y + y; + let z2 = z + z; + + let xx = x * x2; + let xy = x * y2; + let xz = x * z2; + let yy = y * y2; + let yz = y * z2; + let zz = z * z2; + let wx = w * x2; + let wy = w * y2; + let wz = w * z2; + + let sx = s.x; + let sy = s.y; + let sz = s.z; + + let ox = o.x; + let oy = o.y; + let oz = o.z; + + out.m00 = (1 - (yy + zz)) * sx; + out.m01 = (xy + wz) * sx; + out.m02 = (xz - wy) * sx; + out.m03 = 0; + out.m04 = (xy - wz) * sy; + out.m05 = (1 - (xx + zz)) * sy; + out.m06 = (yz + wx) * sy; + out.m07 = 0; + out.m08 = (xz + wy) * sz; + out.m09 = (yz - wx) * sz; + out.m10 = (1 - (xx + yy)) * sz; + out.m11 = 0; + out.m12 = v.x + ox - (out.m00 * ox + out.m04 * oy + out.m08 * oz); + out.m13 = v.y + oy - (out.m01 * ox + out.m05 * oy + out.m09 * oz); + out.m14 = v.z + oz - (out.m02 * ox + out.m06 * oy + out.m10 * oz); + out.m15 = 1; + + return out; + } + + /** + * Calculates a 4x4 matrix from the given quaternion. + * + * @param {mat4} out mat4 - Matrix to store result. + * @param {quat} q - Quaternion to create matrix from. + * + * @returns {mat4} out. + */ + static fromQuat(out, q) { + let x = q.x, y = q.y, z = q.z, w = q.w; + let x2 = x + x; + let y2 = y + y; + let z2 = z + z; + + let xx = x * x2; + let yx = y * x2; + let yy = y * y2; + let zx = z * x2; + let zy = z * y2; + let zz = z * z2; + let wx = w * x2; + let wy = w * y2; + let wz = w * z2; + + out.m00 = 1 - yy - zz; + out.m01 = yx + wz; + out.m02 = zx - wy; + out.m03 = 0; + + out.m04 = yx - wz; + out.m05 = 1 - xx - zz; + out.m06 = zy + wx; + out.m07 = 0; + + out.m08 = zx + wy; + out.m09 = zy - wx; + out.m10 = 1 - xx - yy; + out.m11 = 0; + + out.m12 = 0; + out.m13 = 0; + out.m14 = 0; + out.m15 = 1; + + return out; + } + + /** + * Generates a frustum matrix with the given bounds. + * + * @param {mat4} out mat4 - Matrix to store result. + * @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. + */ + static frustum(out, left, right, bottom, top, near, far) { + let rl = 1 / (right - left); + let tb = 1 / (top - bottom); + let nf = 1 / (near - far); + + out.m00 = (near * 2) * rl; + out.m01 = 0; + out.m02 = 0; + out.m03 = 0; + out.m04 = 0; + out.m05 = (near * 2) * tb; + out.m06 = 0; + out.m07 = 0; + out.m08 = (right + left) * rl; + out.m09 = (top + bottom) * tb; + out.m10 = (far + near) * nf; + out.m11 = -1; + out.m12 = 0; + out.m13 = 0; + out.m14 = (far * near * 2) * nf; + out.m15 = 0; + return out; + } + + /** + * Generates a perspective projection matrix with the given bounds. + * + * @param {mat4} out - Matrix to store result. + * @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. + */ + static perspective(out, fovy, aspect, near, far) { + let f = 1.0 / Math.tan(fovy / 2); + let nf = 1 / (near - far); + + out.m00 = f / aspect; + out.m01 = 0; + out.m02 = 0; + out.m03 = 0; + out.m04 = 0; + out.m05 = f; + out.m06 = 0; + out.m07 = 0; + out.m08 = 0; + out.m09 = 0; + out.m10 = (far + near) * nf; + out.m11 = -1; + out.m12 = 0; + out.m13 = 0; + out.m14 = (2 * far * near) * nf; + out.m15 = 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 - Matrix to store result. + * @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. + */ + static perspectiveFromFieldOfView(out, fov, near, far) { + let upTan = Math.tan(fov.upDegrees * Math.PI / 180.0); + let downTan = Math.tan(fov.downDegrees * Math.PI / 180.0); + let leftTan = Math.tan(fov.leftDegrees * Math.PI / 180.0); + let rightTan = Math.tan(fov.rightDegrees * Math.PI / 180.0); + let xScale = 2.0 / (leftTan + rightTan); + let yScale = 2.0 / (upTan + downTan); + + out.m00 = xScale; + out.m01 = 0.0; + out.m02 = 0.0; + out.m03 = 0.0; + out.m04 = 0.0; + out.m05 = yScale; + out.m06 = 0.0; + out.m07 = 0.0; + out.m08 = -((leftTan - rightTan) * xScale * 0.5); + out.m09 = ((upTan - downTan) * yScale * 0.5); + out.m10 = far / (near - far); + out.m11 = -1.0; + out.m12 = 0.0; + out.m13 = 0.0; + out.m14 = (far * near) / (near - far); + out.m15 = 0.0; + return out; + } + + /** + * Generates a orthogonal projection matrix with the given bounds. + * + * @param {mat4} out - Matrix to store result. + * @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. + */ + static ortho(out, left, right, bottom, top, near, far) { + let lr = 1 / (left - right); + let bt = 1 / (bottom - top); + let nf = 1 / (near - far); + out.m00 = -2 * lr; + out.m01 = 0; + out.m02 = 0; + out.m03 = 0; + out.m04 = 0; + out.m05 = -2 * bt; + out.m06 = 0; + out.m07 = 0; + out.m08 = 0; + out.m09 = 0; + out.m10 = 2 * nf; + out.m11 = 0; + out.m12 = (left + right) * lr; + out.m13 = (top + bottom) * bt; + out.m14 = (far + near) * nf; + out.m15 = 1; + return out; + } + + /** + * Generates a look-at matrix with the given eye position, focal point, and up axis. + * `eye - center` mustn't be zero vector or parallel to `up` + * + * @param {mat4} out - Matrix to store result. + * @param {vec3} eye - Position of the viewer. + * @param {vec3} center - Point the viewer is looking at. + * @param {vec3} up - Vector pointing up. + * @returns {mat4} out + */ + static lookAt(out, eye, center, up) { + let x0, x1, x2, y0, y1, y2, z0, z1, z2, len; + let eyex = eye.x; + let eyey = eye.y; + let eyez = eye.z; + let upx = up.x; + let upy = up.y; + let upz = up.z; + let centerx = center.x; + let centery = center.y; + let centerz = center.z; + + 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 = 1 / Math.sqrt(x0 * x0 + x1 * x1 + x2 * x2); + x0 *= len; + x1 *= len; + x2 *= len; + + y0 = z1 * x2 - z2 * x1; + y1 = z2 * x0 - z0 * x2; + y2 = z0 * x1 - z1 * x0; + + out.m00 = x0; + out.m01 = y0; + out.m02 = z0; + out.m03 = 0; + out.m04 = x1; + out.m05 = y1; + out.m06 = z1; + out.m07 = 0; + out.m08 = x2; + out.m09 = y2; + out.m10 = z2; + out.m11 = 0; + out.m12 = -(x0 * eyex + x1 * eyey + x2 * eyez); + out.m13 = -(y0 * eyex + y1 * eyey + y2 * eyez); + out.m14 = -(z0 * eyex + z1 * eyey + z2 * eyez); + out.m15 = 1; + + return out; + } + + /** + * Returns a string representation of a matrix. + * + * @param {mat4} a - The matrix. + * @returns {String} String representation of this matrix. + */ + static str(a) { + return `mat4(${a.m00}, ${a.m01}, ${a.m02}, ${a.m03}, ${a.m04}, ${a.m05}, ${a.m06}, ${a.m07}, ${a.m08}, ${a.m09}, ${a.m10}, ${a.m11}, ${a.m12}, ${a.m13}, ${a.m14}, ${a.m15})`; + } + + /** + * Store elements of a matrix into array. + * + * @param {array} out - Array to store result. + * @param {mat4} m - The matrix. + * @returns {Array} out. + */ + static array(out, m) { + out[0] = m.m00; + out[1] = m.m01; + out[2] = m.m02; + out[3] = m.m03; + out[4] = m.m04; + out[5] = m.m05; + out[6] = m.m06; + out[7] = m.m07; + out[8] = m.m08; + out[9] = m.m09; + out[10] = m.m10; + out[11] = m.m11; + out[12] = m.m12; + out[13] = m.m13; + out[14] = m.m14; + out[15] = m.m15; + + return out; + } + + /** + * Returns Frobenius norm of a matrix. + * + * @param {mat4} a - Matrix to calculate Frobenius norm of. + * @returns {Number} - The frobenius norm. + */ + static frob(a) { + return (Math.sqrt(Math.pow(a.m00, 2) + Math.pow(a.m01, 2) + Math.pow(a.m02, 2) + Math.pow(a.m03, 2) + Math.pow(a.m04, 2) + Math.pow(a.m05, 2) + Math.pow(a.m06, 2) + Math.pow(a.m07, 2) + Math.pow(a.m08, 2) + Math.pow(a.m09, 2) + Math.pow(a.m10, 2) + Math.pow(a.m11, 2) + Math.pow(a.m12, 2) + Math.pow(a.m13, 2) + Math.pow(a.m14, 2) + Math.pow(a.m15, 2))) + } + + /** + * Adds two matrices. + * + * @param {mat4} out - Matrix to store result. + * @param {mat4} a - The first operand. + * @param {mat4} b - The second operand. + * @returns {mat4} out. + */ + static add(out, a, b) { + out.m00 = a.m00 + b.m00; + out.m01 = a.m01 + b.m01; + out.m02 = a.m02 + b.m02; + out.m03 = a.m03 + b.m03; + out.m04 = a.m04 + b.m04; + out.m05 = a.m05 + b.m05; + out.m06 = a.m06 + b.m06; + out.m07 = a.m07 + b.m07; + out.m08 = a.m08 + b.m08; + out.m09 = a.m09 + b.m09; + out.m10 = a.m10 + b.m10; + out.m11 = a.m11 + b.m11; + out.m12 = a.m12 + b.m12; + out.m13 = a.m13 + b.m13; + out.m14 = a.m14 + b.m14; + out.m15 = a.m15 + b.m15; + return out; + } + + /** + * Subtracts matrix b from matrix a. + * + * @param {mat4} out - Matrix to store result. + * @param {mat4} a - The first operand. + * @param {mat4} b - The second operand. + * @returns {mat4} out. + */ + static subtract(out, a, b) { + out.m00 = a.m00 - b.m00; + out.m01 = a.m01 - b.m01; + out.m02 = a.m02 - b.m02; + out.m03 = a.m03 - b.m03; + out.m04 = a.m04 - b.m04; + out.m05 = a.m05 - b.m05; + out.m06 = a.m06 - b.m06; + out.m07 = a.m07 - b.m07; + out.m08 = a.m08 - b.m08; + out.m09 = a.m09 - b.m09; + out.m10 = a.m10 - b.m10; + out.m11 = a.m11 - b.m11; + out.m12 = a.m12 - b.m12; + out.m13 = a.m13 - b.m13; + out.m14 = a.m14 - b.m14; + out.m15 = a.m15 - b.m15; + return out; + } + + /** + * Alias of {@link mat4.subtract}. + */ + static sub(out, a, b) { + return mat4.subtract(out, a, b); + } + + /** + * Multiply each element of a matrix by a scalar number. + * + * @param {mat4} out - Matrix to store result. + * @param {mat4} a - Matrix to scale + * @param {Number} b - The scale number. + * @returns {mat4} out. + */ + static multiplyScalar(out, a, b) { + out.m00 = a.m00 * b; + out.m01 = a.m01 * b; + out.m02 = a.m02 * b; + out.m03 = a.m03 * b; + out.m04 = a.m04 * b; + out.m05 = a.m05 * b; + out.m06 = a.m06 * b; + out.m07 = a.m07 * b; + out.m08 = a.m08 * b; + out.m09 = a.m09 * b; + out.m10 = a.m10 * b; + out.m11 = a.m11 * b; + out.m12 = a.m12 * b; + out.m13 = a.m13 * b; + out.m14 = a.m14 * b; + out.m15 = a.m15 * b; + return out; + } + + /** + * Adds two matrices after multiplying each element of the second operand by a scalar number. + * + * @param {mat4} out - Matrix to store result. + * @param {mat4} a - The first operand. + * @param {mat4} b - The second operand. + * @param {Number} scale - The scale number. + * @returns {mat4} out. + */ + static multiplyScalarAndAdd(out, a, b, scale) { + out.m00 = a.m00 + (b.m00 * scale); + out.m01 = a.m01 + (b.m01 * scale); + out.m02 = a.m02 + (b.m02 * scale); + out.m03 = a.m03 + (b.m03 * scale); + out.m04 = a.m04 + (b.m04 * scale); + out.m05 = a.m05 + (b.m05 * scale); + out.m06 = a.m06 + (b.m06 * scale); + out.m07 = a.m07 + (b.m07 * scale); + out.m08 = a.m08 + (b.m08 * scale); + out.m09 = a.m09 + (b.m09 * scale); + out.m10 = a.m10 + (b.m10 * scale); + out.m11 = a.m11 + (b.m11 * scale); + out.m12 = a.m12 + (b.m12 * scale); + out.m13 = a.m13 + (b.m13 * scale); + out.m14 = a.m14 + (b.m14 * scale); + out.m15 = a.m15 + (b.m15 * scale); + return out; + } + + /** + * Returns whether the specified matrices are equal. (Compared using ===) + * + * @param {mat4} a - The first matrix. + * @param {mat4} b - The second matrix. + * @returns {Boolean} True if the matrices are equal, false otherwise. + */ + static exactEquals(a, b) { + return a.m00 === b.m00 && a.m01 === b.m01 && a.m02 === b.m02 && a.m03 === b.m03 && + a.m04 === b.m04 && a.m05 === b.m05 && a.m06 === b.m06 && a.m07 === b.m07 && + a.m08 === b.m08 && a.m09 === b.m09 && a.m10 === b.m10 && a.m11 === b.m11 && + a.m12 === b.m12 && a.m13 === b.m13 && a.m14 === b.m14 && a.m15 === b.m15; + } + + /** + * Returns whether the specified matrices are approximately equal. + * + * @param {mat4} a - The first matrix. + * @param {mat4} b - The second matrix. + * @returns {Boolean} True if the matrices are equal, false otherwise. + */ + static equals(a, b) { + let a0 = a.m00, a1 = a.m01, a2 = a.m02, a3 = a.m03, + a4 = a.m04, a5 = a.m05, a6 = a.m06, a7 = a.m07, + a8 = a.m08, a9 = a.m09, a10 = a.m10, a11 = a.m11, + a12 = a.m12, a13 = a.m13, a14 = a.m14, a15 = a.m15; + + let b0 = b.m00, b1 = b.m01, b2 = b.m02, b3 = b.m03, + b4 = b.m04, b5 = b.m05, b6 = b.m06, b7 = b.m07, + b8 = b.m08, b9 = b.m09, b10 = b.m10, b11 = b.m11, + b12 = b.m12, b13 = b.m13, b14 = b.m14, b15 = b.m15; + + return ( + Math.abs(a0 - b0) <= EPSILON * Math.max(1.0, Math.abs(a0), Math.abs(b0)) && + Math.abs(a1 - b1) <= EPSILON * Math.max(1.0, Math.abs(a1), Math.abs(b1)) && + Math.abs(a2 - b2) <= EPSILON * Math.max(1.0, Math.abs(a2), Math.abs(b2)) && + Math.abs(a3 - b3) <= EPSILON * Math.max(1.0, Math.abs(a3), Math.abs(b3)) && + Math.abs(a4 - b4) <= EPSILON * Math.max(1.0, Math.abs(a4), Math.abs(b4)) && + Math.abs(a5 - b5) <= EPSILON * Math.max(1.0, Math.abs(a5), Math.abs(b5)) && + Math.abs(a6 - b6) <= EPSILON * Math.max(1.0, Math.abs(a6), Math.abs(b6)) && + Math.abs(a7 - b7) <= EPSILON * Math.max(1.0, Math.abs(a7), Math.abs(b7)) && + Math.abs(a8 - b8) <= EPSILON * Math.max(1.0, Math.abs(a8), Math.abs(b8)) && + Math.abs(a9 - b9) <= EPSILON * Math.max(1.0, Math.abs(a9), Math.abs(b9)) && + Math.abs(a10 - b10) <= EPSILON * Math.max(1.0, Math.abs(a10), Math.abs(b10)) && + Math.abs(a11 - b11) <= EPSILON * Math.max(1.0, Math.abs(a11), Math.abs(b11)) && + Math.abs(a12 - b12) <= EPSILON * Math.max(1.0, Math.abs(a12), Math.abs(b12)) && + Math.abs(a13 - b13) <= EPSILON * Math.max(1.0, Math.abs(a13), Math.abs(b13)) && + Math.abs(a14 - b14) <= EPSILON * Math.max(1.0, Math.abs(a14), Math.abs(b14)) && + Math.abs(a15 - b15) <= EPSILON * Math.max(1.0, Math.abs(a15), Math.abs(b15)) + ); + } +} + +export default mat4; \ No newline at end of file diff --git a/cocos/vmath/quat.js b/cocos/vmath/quat.js new file mode 100644 index 0000000000..d3928a9a4e --- /dev/null +++ b/cocos/vmath/quat.js @@ -0,0 +1,808 @@ +import vec3 from './vec3'; +import vec4 from './vec4'; +import mat3 from './mat3'; +import { toDegree } from './utils'; + +let halfToRad = 0.5 * Math.PI / 180.0; + +/** + * Mathematical quaternion. + * + * A quaternion is a hypercomplex number represented by w + xi + yj + zk, where + * x, y, z and w are real numbers(called here its components), and i, j, and k are the fundamental quaternion units. + */ +class quat { + /** + * Creates a quaternion, with components specified separately. + * + * @param {number} x - Value assigned to x component. + * @param {number} y - Value assigned to y component. + * @param {number} z - Value assigned to z component. + * @param {number} w - Value assigned to w component. + */ + constructor(x = 0, y = 0, z = 0, w = 1) { + /** + * The x component. + * @type {number} + * */ + this.x = x; + + /** + * The y component. + * @type {number} + * */ + this.y = y; + + /** + * The z component. + * @type {number} + * */ + this.z = z; + + /** + * The w component. + * @type {number} + * */ + this.w = w; + } + + /** + * Creates a quaternion, with components specified separately. + * + * @param {number} x - Value assigned to x component. + * @param {number} y - Value assigned to y component. + * @param {number} z - Value assigned to z component. + * @param {number} w - Value assigned to w component. + * @return {quat} The newly created quaternion. + */ + static create(x = 0, y = 0, z = 0, w = 1) { + return new quat(x, y, z, w); + } + + /** + * Clone a quaternion. + * + * @param {quat} a - Quaternion to clone. + * @returns {quat} The newly created quaternion. + */ + static clone(a) { + return new quat(a.x, a.y, a.z, a.w); + } + + /** + * Copy content of a quaternion into another. + * + * @param {quat} out - Quaternion to modified. + * @param {quat} a - The specified quaternion. + * @returns {quat} out. + */ + static copy(out, a) { + return vec4.copy(out, a); + } + + /** + * Sets the components of a quaternion to the given values. + * + * @param {quat} out - The quaternion to modified. + * @param {Number} x - Value set to x component. + * @param {Number} y - Value set to y component. + * @param {Number} z - Value set to z component. + * @param {Number} w - Value set to w component. + * @returns {quat} out. + */ + static set(out, x, y, z, w) { + out.x = x; + out.y = y; + out.z = z; + out.w = w; + return out; + } + + /** + * Sets a quaternion as identity quaternion. + * + * @param {quat} out - Quaternion to set. + * @returns {quat} out. + */ + static identity(out) { + out.x = 0; + out.y = 0; + out.z = 0; + out.w = 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 - Quaternion to set. + * @param {vec3} a - The initial vector. + * @param {vec3} b - The destination vector. + * @returns {quat} out. + */ + static rotationTo(out, a, b) { + let rotationToIIFE = (function () { + let tmpvec3 = vec3.create(0, 0, 0); + let xUnitVec3 = vec3.create(1, 0, 0); + let yUnitVec3 = vec3.create(0, 1, 0); + + return function (out, a, b) { + let dot = vec3.dot(a, b); + if (dot < -0.999999) { + vec3.cross(tmpvec3, xUnitVec3, a); + if (vec3.magnitude(tmpvec3) < 0.000001) { + vec3.cross(tmpvec3, yUnitVec3, a); + } + vec3.normalize(tmpvec3, tmpvec3); + quat.fromAxisAngle(out, tmpvec3, Math.PI); + return out; + } else if (dot > 0.999999) { + out.x = 0; + out.y = 0; + out.z = 0; + out.w = 1; + return out; + } else { + vec3.cross(tmpvec3, a, b); + out.x = tmpvec3.x; + out.y = tmpvec3.y; + out.z = tmpvec3.z; + out.w = 1 + dot; + return quat.normalize(out, out); + } + }; + })(); + + return rotationToIIFE(out, a, b); + } + + /** + * Gets the rotation axis and angle for a given + * quaternion. If a quaternion is created with + * fromAxisAngle, this method will return the same + * values as provided 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 to store the rotation axis. + * @param {quat} q - Quaternion to be decomposed. + * @return {Number} - Angle, in radians, of the rotation. + */ + static getAxisAngle(out_axis, q) { + let rad = Math.acos(q.w) * 2.0; + let s = Math.sin(rad / 2.0); + if (s != 0.0) { + out_axis.x = q.x / s; + out_axis.y = q.y / s; + out_axis.z = q.z / s; + } else { + // If s is zero, return any axis (no rotation - axis does not matter) + out_axis.x = 1; + out_axis.y = 0; + out_axis.z = 0; + } + return rad; + } + + /** + * Multiply two quaternions. + * + * @param {quat} out - Quaternion to store result. + * @param {quat} a - The first operand. + * @param {quat} b - The second operand. + * @returns {quat} out. + */ + static multiply(out, a, b) { + let ax = a.x, ay = a.y, az = a.z, aw = a.w, + bx = b.x, by = b.y, bz = b.z, bw = b.w; + + out.x = ax * bw + aw * bx + ay * bz - az * by; + out.y = ay * bw + aw * by + az * bx - ax * bz; + out.z = az * bw + aw * bz + ax * by - ay * bx; + out.w = aw * bw - ax * bx - ay * by - az * bz; + return out; + } + + /** + * Alias of {@link quat.multiply}. + */ + static mul(out, a, b) { + return quat.multiply(out, a, b); + } + + /** + * Scales a quaternion with a number. + * + * @param {quat} out - Quaternion to store result. + * @param {quat} a - Quaternion to scale. + * @param {quat} b - The scale number. + * @returns {quat} out. + * */ + static scale(out, a, b) { + out.x = a.x * b; + out.y = a.y * b; + out.z = a.z * b; + out.w = a.w * b; + return out; + } + + /** + * Rotates a quaternion by the given angle about the X axis. + * + * @param {quat} out - Quaternion to store result. + * @param {quat} a - Quaternion to rotate. + * @param {number} rad - Angle (in radians) to rotate. + * @returns {quat} out. + */ + static rotateX(out, a, rad) { + rad *= 0.5; + + let ax = a.x, ay = a.y, az = a.z, aw = a.w, + bx = Math.sin(rad), bw = Math.cos(rad); + + out.x = ax * bw + aw * bx; + out.y = ay * bw + az * bx; + out.z = az * bw - ay * bx; + out.w = aw * bw - ax * bx; + return out; + } + + /** + * Rotates a quaternion by the given angle about the Y axis. + * + * @param {quat} out - Quaternion to store result. + * @param {quat} a - Quaternion to rotate. + * @param {number} rad - Angle (in radians) to rotate. + * @returns {quat} out. + */ + static rotateY(out, a, rad) { + rad *= 0.5; + + let ax = a.x, ay = a.y, az = a.z, aw = a.w, + by = Math.sin(rad), bw = Math.cos(rad); + + out.x = ax * bw - az * by; + out.y = ay * bw + aw * by; + out.z = az * bw + ax * by; + out.w = aw * bw - ay * by; + return out; + } + + /** + * Rotates a quaternion by the given angle about the Z axis. + * + * @param {quat} out - Quaternion to store result. + * @param {quat} a - Quaternion to rotate. + * @param {number} rad - Angle (in radians) to rotate. + * @returns {quat} out. + */ + static rotateZ(out, a, rad) { + rad *= 0.5; + + let ax = a.x, ay = a.y, az = a.z, aw = a.w, + bz = Math.sin(rad), bw = Math.cos(rad); + + out.x = ax * bw + ay * bz; + out.y = ay * bw - ax * bz; + out.z = az * bw + aw * bz; + out.w = aw * bw - az * bz; + return out; + } + + /** + * Rotates a quaternion by the given angle about a world space axis. + * + * @param {quat} out - Quaternion to store result. + * @param {quat} rot - Quaternion to rotate. + * @param {vec3} axis - The axis around which to rotate in world space. + * @param {number} rad - Angle (in radians) to rotate. + * @returns {quat} out. + */ + static rotateAround(out, rot, axis, rad) { + let rotateAroundIIFE = (function () { + let v3_tmp = vec3.create(0, 0, 0); + let q_tmp = quat.create(); + + return function (out, rot, axis, rad) { + // get inv-axis (local to rot) + quat.invert(q_tmp, rot); + vec3.transformQuat(v3_tmp, axis, q_tmp); + // rotate by inv-axis + quat.fromAxisAngle(q_tmp, v3_tmp, rad); + quat.mul(out, rot, q_tmp); + + return out; + }; + })(); + return rotateAroundIIFE(out, rot, axis, rad); + } + + /** + * Rotates a quaternion by the given angle about a local space axis. + * + * @param {quat} out - Quaternion to store result. + * @param {quat} rot - Quaternion to rotate. + * @param {vec3} axis - The axis around which to rotate in local space. + * @param {number} rad - Angle (in radians) to rotate. + * @returns {quat} out. + */ + static rotateAroundLocal(out, rot, axis, rad) { + let rotateAroundLocalIIFE = (function () { + let q_tmp = quat.create(); + + return function (out, rot, axis, rad) { + quat.fromAxisAngle(q_tmp, axis, rad); + quat.mul(out, rot, q_tmp); + + return out; + }; + })(); + + return rotateAroundLocalIIFE(out, rot, axis, rad); + } + + /** + * Calculates the W component of a quaternion 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 - Quaternion to store result. + * @param {quat} a - Quaternion to calculate W. + * @returns {quat} out. + */ + static calculateW(out, a) { + let x = a.x, y = a.y, z = a.z; + + out.x = x; + out.y = y; + out.z = z; + out.w = Math.sqrt(Math.abs(1.0 - x * x - y * y - z * z)); + return out; + } + + /** + * Calculates the dot product of two quaternions. + * + * @param {quat} a - The first operand. + * @param {quat} b - The second operand. + * @returns {Number} - The dot product of a and b. + */ + static dot(a, b) { + return a.x * b.x + a.y * b.y + a.z * b.z + a.w * b.w; + } + + /** + * Performs a linear interpolation between two quaternions. + * + * @param {quat} out - Quaternion to store result. + * @param {quat} a - The first operand. + * @param {quat} b - The second operand. + * @param {Number} t - The interpolation coefficient. + * @returns {quat} out. + */ + static lerp(out, a, b, t) { + let ax = a.x, + ay = a.y, + az = a.z, + aw = a.w; + out.x = ax + t * (b.x - ax); + out.y = ay + t * (b.y - ay); + out.z = az + t * (b.z - az); + out.w = aw + t * (b.w - aw); + return out; + } + + /** + * Performs a spherical linear interpolation between two quaternions. + * + * @param {quat} out - Quaternion to store result. + * @param {quat} a - The first operand. + * @param {quat} b - The second operand. + * @param {Number} t - The interpolation coefficient. + * @returns {quat} out. + */ + static slerp(out, a, b, t) { + // benchmarks: + // http://jsperf.com/quaternion-slerp-implementations + + let ax = a.x, ay = a.y, az = a.z, aw = a.w, + bx = b.x, by = b.y, bz = b.z, bw = b.w; + + let 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.x = scale0 * ax + scale1 * bx; + out.y = scale0 * ay + scale1 * by; + out.z = scale0 * az + scale1 * bz; + out.w = scale0 * aw + scale1 * bw; + + return out; + } + + /** + * Performs a spherical linear interpolation with two control points. + * + * @param {quat} out - Quaternion to store result. + * @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 - The interpolation coefficient. + * @returns {quat} out + */ + static sqlerp(out, a, b, c, d, t) { + let sqlerpIIFE = (function () { + let temp1 = quat.create(); + let 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; + }; + }()); + return sqlerpIIFE(out, a, b, c, d, t); + } + + /** + * Calculates the inverse of a quaternion. + * + * @param {quat} out - Quaternion to store result. + * @param {quat} a - Quaternion to calculate inverse of. + * @returns {quat} out. + */ + static invert(out, a) { + let a0 = a.x, a1 = a.y, a2 = a.z, a3 = a.w; + let dot = a0 * a0 + a1 * a1 + a2 * a2 + a3 * a3; + let invDot = dot ? 1.0 / dot : 0; + + // TODO: Would be faster to return [0,0,0,0] immediately if dot == 0 + + out.x = -a0 * invDot; + out.y = -a1 * invDot; + out.z = -a2 * invDot; + out.w = a3 * invDot; + return out; + } + + /** + * Calculates the conjugate of a quaternion. + * If the quaternion is normalized, this function is faster than quat.inverse and produces the same result. + * + * @param {quat} out - Quaternion to store result. + * @param {quat} a - Quaternion to calculate conjugate of. + * @returns {quat} out. + */ + static conjugate(out, a) { + out.x = -a.x; + out.y = -a.y; + out.z = -a.z; + out.w = a.w; + return out; + } + + /** + * Calculates the length of a quaternion. + * + * @param {quat} a - The quaternion. + * @returns {Number} Length of the quaternion. + */ + static magnitude(a) { + let x = a.x, + y = a.y, + z = a.z, + w = a.w; + return Math.sqrt(x * x + y * y + z * z + w * w); + } + + /** + *Alias of {@link quat.magnitude}. + */ + static mag(a) { + return quat.magnitude(a); + } + + /** + * Calculates the squared length of a quaternion. + * + * @param {quat} a - The quaternion. + * @returns {Number} Squared length of the quaternion. + */ + static squaredMagnitude(a) { + let x = a.x, + y = a.y, + z = a.z, + w = a.w; + return x * x + y * y + z * z + w * w; + } + + /** + *Alias of {@link quat.squaredMagnitude} + */ + static sqrMag(a) { + return quat.squaredMagnitude(a); + } + + /** + * Normalizes a quaternion. + * + * @param {quat} out - Quaternion to store result. + * @param {quat} a - Quaternion to normalize. + * @returns {quat} out. + * @function + */ + static normalize(out, a) { + let x = a.x, + y = a.y, + z = a.z, + w = a.w; + let len = x * x + y * y + z * z + w * w; + if (len > 0) { + len = 1 / Math.sqrt(len); + out.x = x * len; + out.y = y * len; + out.z = z * len; + out.w = w * len; + } + return out; + } + + /** + * Sets the specified quaternion with values corresponding to the given + * axes. Each axis is a vec3 and is expected to be unit length and + * perpendicular to all other specified axes. + * + * @param {quat} out - Quaternion to store result. + * @param {vec3} xAxis - Vector representing the local "right" direction. + * @param {vec3} yAxis - Vector representing the local "up" direction. + * @param {vec3} zAxis - Vector representing the viewing direction. + * @returns {quat} out. + */ + static fromAxes(out, xAxis, yAxis, zAxis) { + let fromAxesIIFE = (function () { + let matr = mat3.create(); + + return function (out, xAxis, yAxis, zAxis) { + mat3.set(matr, + xAxis.x, xAxis.y, xAxis.z, + yAxis.x, yAxis.y, yAxis.z, + zAxis.x, zAxis.y, zAxis.z + ); + return quat.normalize(out, quat.fromMat3(out, matr)); + }; + })(); + return fromAxesIIFE(out, xAxis, yAxis, zAxis); + } + + /** + * Calculates a quaternion from view direction and up direction + * + * @param {quat} out - Quaternion to store result. + * @param {vec3} view - View direction (must be normalized). + * @param {vec3} [up] - Up direction, default is (0,1,0) (must be normalized). + * + * @returns {quat} out. + */ + static fromViewUp(out, view, up) { + let fromViewUpIIFE = (function () { + let matr = mat3.create(); + + return function (out, view, up) { + mat3.fromViewUp(matr, view, up); + if (!matr) { + return null; + } + + return quat.normalize(out, quat.fromMat3(out, matr)); + }; + })(); + return fromViewUpIIFE(out, view, up); + } + + /** + * Sets a quaternion from the given angle and rotation axis, + * then returns it. + * + * @param {quat} out - Quaternion to store result. + * @param {vec3} axis - The axis around which to rotate. + * @param {Number} rad - The angle in radians. + * @returns {quat} out. + **/ + static fromAxisAngle(out, axis, rad) { + rad = rad * 0.5; + let s = Math.sin(rad); + out.x = s * axis.x; + out.y = s * axis.y; + out.z = s * axis.z; + out.w = Math.cos(rad); + return out; + } + + /** + * Creates a quaternion from the given 3x3 rotation matrix. + * + * NOTE: The resultant quaternion is not normalized, so you should be sure + * to re-normalize the quaternion yourself where necessary. + * + * @param {quat} out - Quaternion to store result. + * @param {mat3} m - The rotation matrix. + * @returns {quat} out. + * @function + */ + static fromMat3(out, m) { + // http://www.euclideanspace.com/maths/geometry/rotations/conversions/matrixToQuaternion/index.htm + + let m00 = m.m00, m01 = m.m03, m02 = m.m06, + m10 = m.m01, m11 = m.m04, m12 = m.m07, + m20 = m.m02, m21 = m.m05, m22 = m.m08; + + let trace = m00 + m11 + m22; + + if (trace > 0) { + let s = 0.5 / Math.sqrt(trace + 1.0); + + out.w = 0.25 / s; + out.x = (m21 - m12) * s; + out.y = (m02 - m20) * s; + out.z = (m10 - m01) * s; + + } else if ((m00 > m11) && (m00 > m22)) { + let s = 2.0 * Math.sqrt(1.0 + m00 - m11 - m22); + + out.w = (m21 - m12) / s; + out.x = 0.25 * s; + out.y = (m01 + m10) / s; + out.z = (m02 + m20) / s; + + } else if (m11 > m22) { + let s = 2.0 * Math.sqrt(1.0 + m11 - m00 - m22); + + out.w = (m02 - m20) / s; + out.x = (m01 + m10) / s; + out.y = 0.25 * s; + out.z = (m12 + m21) / s; + + } else { + let s = 2.0 * Math.sqrt(1.0 + m22 - m00 - m11); + + out.w = (m10 - m01) / s; + out.x = (m02 + m20) / s; + out.y = (m12 + m21) / s; + out.z = 0.25 * s; + } + + return out; + } + + /** + * Creates a quaternion from the given euler angle x, y, z. + * + * @param {quat} out - Quaternion to store result. + * @param {number} x - Angle to rotate around X axis in degrees. + * @param {number} y - Angle to rotate around Y axis in degrees. + * @param {number} z - Angle to rotate around Z axis in degrees. + * @returns {quat} out. + * @function + */ + static fromEuler(out, x, y, z) { + x *= halfToRad; + y *= halfToRad; + z *= halfToRad; + + let sx = Math.sin(x); + let cx = Math.cos(x); + let sy = Math.sin(y); + let cy = Math.cos(y); + let sz = Math.sin(z); + let cz = Math.cos(z); + + out.x = sx * cy * cz - cx * sy * sz; + out.y = cx * sy * cz + sx * cy * sz; + out.z = cx * cy * sz - sx * sy * cz; + out.w = cx * cy * cz + sx * sy * sz; + + return out; + } + + /** + * Convert a quaternion back to euler angle (in degrees). + * + * @param {vec3} out - Euler angle stored as a vec3 + * @param {number} q - the quaternion to be converted + * @returns {vec3} out. + */ + static toEuler(out, q) { + let siny = 2.0 * (q.w * q.x + q.y * q.z); + let cosy = 1 - 2 * (q.x * q.x + q.y * q.y); + out.x = toDegree(Math.atan2(siny, cosy)); + + let sinp = 2 * (q.w * q.y - q.z * q.x); + if (Math.abs(sinp) >= 1) out.y = 90 * Math.sign(sinp); + else out.y = toDegree(Math.asin(sinp)); + + let sinr = 2 * (q.w * q.z + q.x * q.y); + let cosr = 1 - 2 * (q.y * q.y + q.z * q.z); + out.z = toDegree(Math.atan2(sinr, cosr)); + + return out; + } + + /** + * Returns string representation of a quaternion. + * + * @param {quat} a - The quaternion. + * @returns {String} - String representation of this quaternion. + */ + static str(a) { + return `quat(${a.x}, ${a.y}, ${a.z}, ${a.w})`; + } + + /** + * Store components of a quaternion into array. + * + * @param {Array} out - Array to store result. + * @param {quat} q - The quaternion. + * @returns {Array} out. + */ + static array(out, q) { + out[0] = q.x; + out[1] = q.y; + out[2] = q.z; + out[3] = q.w; + + return out; + } + + /** + * Returns whether the specified quaternions are equal. (Compared using ===) + * + * @param {quat} a - The first quaternion. + * @param {quat} b - The second quaternion. + * @returns {Boolean} True if the quaternions are equal, false otherwise. + */ + static exactEquals(a, b) { + return vec4.exactEquals(a, b); + } + + /** + * Returns whether the specified quaternions are approximately equal. + * + * @param {quat} a The first quaternion. + * @param {quat} b The second quaternion. + * @returns {Boolean} True if the quaternions are approximately equal, false otherwise. + */ + static equals(a, b) { + return vec4.equals(a, b); + } +} + +export default quat; \ No newline at end of file diff --git a/cocos/vmath/utils.js b/cocos/vmath/utils.js new file mode 100644 index 0000000000..836ecc645a --- /dev/null +++ b/cocos/vmath/utils.js @@ -0,0 +1,217 @@ +/** + * @ignore + */ +const _d2r = Math.PI / 180.0; +/** + * @ignore + */ +const _r2d = 180.0 / Math.PI; + +/** + * @property {number} EPSILON + */ +export const EPSILON = 0.000001; + +/** + * 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. + */ +export function equals(a, b) { + return Math.abs(a - b) <= EPSILON * Math.max(1.0, Math.abs(a), Math.abs(b)); +} + +/** + * Tests whether or not the arguments have approximately the same value by given maxDiff + * + * @param {Number} a The first number to test. + * @param {Number} b The second number to test. + * @param {Number} maxDiff Maximum difference. + * @returns {Boolean} True if the numbers are approximately equal, false otherwise. + */ +export function approx(a, b, maxDiff) { + maxDiff = maxDiff || EPSILON; + return Math.abs(a - b) <= maxDiff; +} + +/** + * Clamps a value between a minimum float and maximum float value. + * + * @method clamp + * @param {number} val + * @param {number} min + * @param {number} max + * @return {number} + */ +export function clamp(val, min, max) { + return val < min ? min : val > max ? max : val; +} + +/** + * Clamps a value between 0 and 1. + * + * @method clamp01 + * @param {number} val + * @return {number} + */ +export function clamp01(val) { + return val < 0 ? 0 : val > 1 ? 1 : val; +} + +/** + * @method lerp + * @param {number} from + * @param {number} to + * @param {number} ratio - the interpolation coefficient + * @return {number} + */ +export function lerp(from, to, ratio) { + return from + (to - from) * ratio; +} + +/** +* Convert Degree To Radian +* +* @param {Number} a Angle in Degrees +*/ +export function toRadian(a) { + return a * _d2r; +} + +/** +* Convert Radian To Degree +* +* @param {Number} a Angle in Radian +*/ +export function toDegree(a) { + return a * _r2d; +} + +/** +* @method random +*/ +export const random = Math.random; + +/** + * Returns a floating-point random number between min (inclusive) and max (exclusive). + * + * @method randomRange + * @param {number} min + * @param {number} max + * @return {number} the random number + */ +export function randomRange(min, max) { + return Math.random() * (max - min) + min; +} + +/** + * Returns a random integer between min (inclusive) and max (exclusive). + * + * @method randomRangeInt + * @param {number} min + * @param {number} max + * @return {number} the random integer + */ +export function randomRangeInt(min, max) { + return Math.floor(randomRange(min, max)); +} + +/** + * Linear congruential generator using Hull-Dobell Theorem. + * + * @method pseudoRandom + * @param {number} seed the random seed + * @return {number} the pseudo random + */ +export function pseudoRandom(seed) { + seed = (seed * 9301 + 49297) % 233280; + return seed / 233280.0; +} + +/** + * Returns a floating-point pseudo-random number between min (inclusive) and max (exclusive). + * + * @method pseudoRandomRange + * @param {number} seed + * @param {number} min + * @param {number} max + * @return {number} the random number + */ +export function pseudoRandomRange(seed, min, max) { + return pseudoRandom(seed) * (max - min) + min; +} + +/** + * Returns a pseudo-random integer between min (inclusive) and max (exclusive). + * + * @method pseudoRandomRangeInt + * @param {number} seed + * @param {number} min + * @param {number} max + * @return {number} the random integer + */ +export function pseudoRandomRangeInt(seed, min, max) { + return Math.floor(pseudoRandomRange(seed, min, max)); +} + +/** + * Returns the next power of two for the value + * + * @method nextPow2 + * @param {number} val + * @return {number} the the next power of two + */ +export function nextPow2(val) { + --val; + val = (val >> 1) | val; + val = (val >> 2) | val; + val = (val >> 4) | val; + val = (val >> 8) | val; + val = (val >> 16) | val; + ++val; + + return val; +} + +/** + * Returns float remainder for t / length + * + * @method repeat + * @param {number} t time start at 0 + * @param {number} length time of one cycle + * @return {number} the time wrapped in the first cycle + */ +export function repeat(t, length) { + return t - Math.floor(t / length) * length; +} + +/** + * Returns time wrapped in ping-pong mode + * + * @method repeat + * @param {number} t time start at 0 + * @param {number} length time of one cycle + * @return {number} the time wrapped in the first cycle + */ +export function pingPong(t, length) { + t = repeat(t, length * 2); + t = length - Math.abs(t - length); + return t; +} + +/** + * Returns ratio of a value within a given range + * + * @method repeat + * @param {number} from start value + * @param {number} to end value + * @param {number} value given value + * @return {number} the ratio between [from,to] + */ +export function inverseLerp(from, to, value) { + return (value - from) / (to - from); +} \ No newline at end of file diff --git a/cocos/vmath/vec2.js b/cocos/vmath/vec2.js new file mode 100644 index 0000000000..35fa2ad66f --- /dev/null +++ b/cocos/vmath/vec2.js @@ -0,0 +1,691 @@ +import { EPSILON, random } from './utils'; + +/** + * Mathematical 2-dimensional vector. + * + * x, y is alias of the first, second component of vector, respectively. + */ +class vec2 { + /** + * Creates a vector, with components specified separately. + * + * @param {number} x - Value assigned to x component. + * @param {number} y - Value assigned to y component. + */ + constructor(x = 0, y = 0) { + /** + * The x component. + * @type {number} + * */ + this.x = x; + + /** + * The y component. + * @type {number} + * */ + this.y = y; + } + + /** + * Creates a vector, with components specified separately. + * + * @param {number} x - Value assigned to x component. + * @param {number} y - Value assigned to y component. + * @return {vec2} The newly created vector. + */ + static create(x = 0, y = 0) { + return new vec2(x, y); + } + + /** + * Creates a zero vector. + * + * @return {vec2} The newly created vector. + */ + static zero(out) { + out.x = 0; + out.y = 0; + return out; + } + + /** + * Clone a vector. + * + * @param {vec2} a - Vector to clone. + * @returns {vec2} The newly created vector. + */ + static clone(a) { + return new vec2(a.x, a.y); + } + + /** + * Copy content of a vector into another. + * + * @param {vec2} out - The vector to modified. + * @param {vec2} a - The specified vector. + * @returns {vec2} out. + */ + static copy(out, a) { + out.x = a.x; + out.y = a.y; + return out; + } + + /** + * Sets the components of a vector to the given values. + * + * @param {vec2} out - The vector to modified. + * @param {Number} x - Value set to x component. + * @param {Number} y - Value set to y component. + * @returns {vec2} out. + */ + static set(out, x, y) { + out.x = x; + out.y = y; + return out; + } + + /** + * Add two vectors. + * + * It doesn't matter that any amount of these parameters refer to same vector. + * + * @param {vec2} out - Vector to store result. + * @param {vec2} a - The first operand. + * @param {vec2} b - The second operand. + * @returns {vec2} out. + */ + static add(out, a, b) { + out.x = a.x + b.x; + out.y = a.y + b.y; + return out; + } + + /** + * Subtract two vectors. + * + * It doesn't matter that any amount of these parameters refer to same vector. + * + * @param {vec2} out - Vector to store result. + * @param {vec2} a - The first operand. + * @param {vec2} b - The second operand. + * @returns {vec2} out. + */ + static subtract(out, a, b) { + out.x = a.x - b.x; + out.y = a.y - b.y; + return out; + } + + /** + *Alias of {@link vec2.subtract}. + */ + static sub(out, a, b) { + return vec2.subtract(out, a, b); + } + + /** + * Performs multiply on each component of two vectors respectively. + * + * It doesn't matter that any amount of these parameters refer to same vector. + * + * @param {vec2} out - Vector to store result. + * @param {vec2} a - The first operand. + * @param {vec2} b - The second operand. + * @returns {vec2} out. + */ + static multiply(out, a, b) { + out.x = a.x * b.x; + out.y = a.y * b.y; + return out; + } + + /** + *Alias of {@link vec2.multiply}. + */ + static mul(out, a, b) { + return vec2.multiply(out, a, b); + } + + /** + * Performs division on each component of two vectors respectively. + * + * It doesn't matter that any amount of these parameters refer to same vector. + * + * @param {vec2} out - Vector to store result. + * @param {vec2} a - The first operand. + * @param {vec2} b - The second operand. + * @returns {vec2} out. + */ + static divide(out, a, b) { + out.x = a.x / b.x; + out.y = a.y / b.y; + return out; + } + + /** + *Alias of {@link vec2.divide}. + */ + static div(out, a, b) { + return vec2.divide(out, a, b); + } + + /** + * Performs Math.ceil on each component of a vector. + * + * It doesn't matter that any amount of these parameters refer to same vector. + * + * @param {vec2} out - Vector to store result. + * @param {vec2} a - Vector to perform operation. + * @returns {vec2} out. + */ + static ceil(out, a) { + out.x = Math.ceil(a.x); + out.y = Math.ceil(a.y); + return out; + } + + /** + * Performs Math.floor on each component of a vector. + * + * It doesn't matter that any amount of these parameters refer to same vector. + * + * @param {vec2} out - Vector to store result. + * @param {vec2} a - Vector to perform operation. + * @returns {vec2} out. + */ + static floor(out, a) { + out.x = Math.floor(a.x); + out.y = Math.floor(a.y); + return out; + } + + /** + * Performs Math.min on each component of two vectors respectively. + * + * It doesn't matter that any amount of these parameters refer to same vector. + * + * @param {vec2} out - Vector to store result. + * @param {vec2} a - The first operand. + * @param {vec2} b - The second operand. + * @returns {vec2} out. + */ + static min(out, a, b) { + out.x = Math.min(a.x, b.x); + out.y = Math.min(a.y, b.y); + return out; + } + + /** + * Performs Math.min on each component of two vectors respectively. + * + * It doesn't matter that any amount of these parameters refer to same vector. + * + * @param {vec2} out - Vector to store result. + * @param {vec2} a - The first operand. + * @param {vec2} b - The second operand. + * @returns {vec2} out. + */ + static max(out, a, b) { + out.x = Math.max(a.x, b.x); + out.y = Math.max(a.y, b.y); + return out; + } + + /** + * Performs Math.round on each component of a vector. + * + * It doesn't matter that any amount of these parameters refer to same vector. + * + * @param {vec2} out - Vector to store result. + * @param {vec2} a - Vector to perform operation. + * @returns {vec2} out. + */ + static round(out, a) { + out.x = Math.round(a.x); + out.y = Math.round(a.y); + return out; + } + + /** + * Scales a vector with a number. + * + * @param {vec2} out - Vector to store result. + * @param {vec2} a - Vector to scale. + * @param {number} b - The scale number. + * @returns {vec2} out. + * */ + static scale(out, a, b) { + out.x = a.x * b; + out.y = a.y * b; + return out; + } + + /** + * Add two vectors after scaling the second operand by a number. + * + * @param {vec2} out - Vector to store result. + * @param {vec2} a - The first operand. + * @param {vec2} b - The second operand. + * @param {number} scale - The scale number before adding. + * @returns {vec2} out. + */ + static scaleAndAdd(out, a, b, scale) { + out.x = a.x + (b.x * scale); + out.y = a.y + (b.y * scale); + return out; + } + + /** + * Calculates the euclidian distance between two vectors. + * + * @param {vec2} a - The first operand. + * @param {vec2} b - The second operand. + * @returns {number} Distance between a and b. + */ + static distance(a, b) { + let x = b.x - a.x, + y = b.y - a.y; + return Math.sqrt(x * x + y * y); + } + + /** + *Alias of {@link vec2.distance}. + */ + static dist(a, b) { + return vec2.distance(a, b); + } + + /** + * Calculates the squared euclidian distance between two vectors. + * + * @param {vec2} a - The first operand. + * @param {vec2} b - The second operand. + * @returns {number} Squared distance between a and b. + */ + static squaredDistance(a, b) { + let x = b.x - a.x, + y = b.y - a.y; + return x * x + y * y; + } + + /** + *Alias of {@link vec2.squaredDistance}. + */ + static sqrDist(a, b) { + return vec2.squaredDistance(a, b); + } + + /** + * Calculates the length of a vector. + * + * @param {vec2} a - The vector. + * @returns {Number} Length of the vector. + */ + static magnitude(a) { + let x = a.x, + y = a.y; + return Math.sqrt(x * x + y * y); + } + + /** + *Alias of {@link vec2.magnitude}. + */ + static mag(a) { + return vec2.magnitude(a); + } + + /** + * Calculates the squared length of a vector. + * + * @param {vec2} a - The vector. + * @returns {Number} Squared length of the vector. + */ + static squaredMagnitude(a) { + let x = a.x, + y = a.y; + return x * x + y * y; + } + + /** + *Alias of {@link vec2.squaredMagnitude} + */ + static sqrMag(a) { + return vec2.squaredMagnitude(a); + } + + /** + * Negates each component of a vector. + * + * @param {vec2} out - Vector to store result. + * @param {vec2} a - Vector to negate. + * @returns {vec2} out. + */ + static negate(out, a) { + out.x = -a.x; + out.y = -a.y; + return out; + } + + /** + * Invert the components of a vector. + * + * @param {vec2} out - Vector to store result. + * @param {vec2} a - Vector to invert. + * @returns {vec2} out. + */ + static inverse(out, a) { + out.x = 1.0 / a.x; + out.y = 1.0 / a.y; + return out; + } + + /** + * Safely invert the components of a vector. + * + * @param {vec2} out - Vector to store result. + * @param {vec2} a - Vector to invert. + * @returns {vec2} out. + */ + static inverseSafe(out, a) { + let x = a.x, + y = a.y; + + if (Math.abs(x) < EPSILON) { + out.x = 0; + } else { + out.x = 1.0 / x; + } + + if (Math.abs(y) < EPSILON) { + out.y = 0; + } else { + out.y = 1.0 / a.y; + } + + return out; + } + + /** + * Normalizes a vector. + * + * @param {vec2} out - Vector to store result. + * @param {vec2} a - Vector to normalize. + * @returns {vec2} out. + */ + static normalize(out, a) { + let x = a.x, + y = a.y; + let len = x * x + y * y; + if (len > 0) { + //TODO: evaluate use of glm_invsqrt here? + len = 1 / Math.sqrt(len); + out.x = a.x * len; + out.y = a.y * len; + } + return out; + } + + /** + * Calculates the dot product of two vectors. + * + * @param {vec2} a - The first operand. + * @param {vec2} b - The second operand. + * @returns {Number} Dot product of a and b. + */ + static dot(a, b) { + return a.x * b.x + a.y * b.y; + } + + /** + * Calculate the cross product of two vectors. + * Note that the cross product must by definition produce a 3D vector. + * + * @param {vec2} out - Vector to store result. + * @param {vec2} a - The first operand. + * @param {vec2} b - The second operand. + * @returns {vec3} out. + */ + static cross(out, a, b) { + let z = a.x * b.y - a.y * b.x; + out.x = out.y = 0; + out.z = z; + return out; + } + + /** + * Performs a linear interpolation between two vectors. + * + * @param {vec2} out - Vector to store result. + * @param {vec2} a - The first operand. + * @param {vec2} b - The second operand. + * @param {Number} t - The interpolation coefficient. + * @returns {vec2} out. + */ + static lerp(out, a, b, t) { + let ax = a.x, + ay = a.y; + out.x = ax + t * (b.x - ax); + out.y = ay + t * (b.y - ay); + return out; + } + + /** + * Generates a random vector with the given scale. + * + * @param {vec2} out - Vector to store result. + * @param {Number} [scale] Length of the resulting vector. If ommitted, a unit vector will be returned. + * @returns {vec2} out. + */ + static random(out, scale) { + scale = scale || 1.0; + let r = random() * 2.0 * Math.PI; + out.x = Math.cos(r) * scale; + out.y = Math.sin(r) * scale; + return out; + } + + /** + * Transforms a vector with a 2x2 matrix. + * + * @param {vec2} out - Vector to store result. + * @param {vec2} a - Vector to transform. + * @param {mat2} m - The matrix. + * @returns {vec2} out. + */ + static transformMat2(out, a, m) { + let x = a.x, + y = a.y; + out.x = m.m00 * x + m.m02 * y; + out.y = m.m01 * x + m.m03 * y; + return out; + } + + /** + * Transforms a vector with a 2x3 matrix. + * + * @param {vec2} out - Vector to store result. + * @param {vec2} a - Vector to transform. + * @param {mat23} m - The matrix. + * @returns {vec2} out. + */ + static transformMat23(out, a, m) { + let x = a.x, + y = a.y; + out.x = m.m00 * x + m.m02 * y + m.m04; + out.y = m.m01 * x + m.m03 * y + m.m05; + return out; + } + + /** + * Transforms a vector with a 3x3 matrix. + * + * @param {vec2} out - Vector to store result. + * @param {vec2} a - Vector to transform. + * @param {mat3} m - The matrix. + * @returns {vec2} out. + */ + static transformMat3(out, a, m) { + let x = a.x, + y = a.y; + out.x = m.m00 * x + m.m03 * y + m.m06; + out.y = m.m01 * x + m.m04 * y + m.m07; + return out; + } + + /** + * Transforms a vector with a 4x4 matrix. + * 3rd vector component is implicitly '0'. + * 4th vector component is implicitly '1'. + * + * @param {vec2} out - Vector to store result. + * @param {vec2} a - Vector to transform. + * @param {mat4} m - The matrix. + * @returns {vec2} out. + */ + static transformMat4(out, a, m) { + let x = a.x, + y = a.y; + out.x = m.m00 * x + m.m04 * y + m.m12; + out.y = m.m01 * x + m.m05 * y + m.m13; + 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. + */ + static forEach(a, stride, offset, count, fn, arg) { + return vec2._forEach(a, stride, offset, count, fn, arg); + } + + /** + * Returns string representation of a vector. + * + * @param {vec2} a - The vector. + * @returns {String} - String representation of this vector. + */ + static str(a) { + return `vec2(${a.x}, ${a.y})`; + } + + /** + * Store components of a vector into array. + * + * @param {Array} out - Array to store result. + * @param {vec2} v - The vector. + * @returns {Array} out. + */ + static array(out, v) { + out[0] = v.x; + out[1] = v.y; + + return out; + } + + /** + * Returns whether the specified vectors are equal. (Compared using ===) + * + * @param {vec2} a - The first vector. + * @param {vec2} b - The second vector. + * @returns {Boolean} True if the vectors are equal, false otherwise. + */ + static exactEquals(a, b) { + return a.x === b.x && a.y === b.y; + } + + /** + * Returns whether the specified vectors are approximately equal. + * + * @param {vec2} a The first vector. + * @param {vec2} b The second vector. + * @returns {Boolean} True if the vectors are approximately equal, false otherwise. + */ + static equals(a, b) { + let a0 = a.x, a1 = a.y; + let b0 = b.x, b1 = b.y; + return (Math.abs(a0 - b0) <= EPSILON * Math.max(1.0, Math.abs(a0), Math.abs(b0)) && + Math.abs(a1 - b1) <= EPSILON * Math.max(1.0, Math.abs(a1), Math.abs(b1))); + } + + /** + * Returns the angle between the two vectors. + * + * @param {vec2} a The first vector. + * @param {vec2} b The second vector. + * @returns {Number} The angle in radians. + */ + static angle(a, b) { + return vec2._angle(a, b); + } +} + +/** + * 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. + * @ignore + */ +vec2._forEach = (function () { + let vec = vec2.create(0, 0); + + return function (a, stride, offset, count, fn, arg) { + let 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.x = a[i]; + vec.y = a[i + 1]; + fn(vec, vec, arg); + a[i] = vec.x; + a[i + 1] = vec.y; + } + + return a; + }; +})(); + +vec2._angle = (function () { + let tempA = vec2.create(0, 0); + let tempB = vec2.create(0, 0); + + return function (a, b) { + vec2.normalize(tempA, a); + vec2.normalize(tempB, b); + let cosine = vec2.dot(tempA, tempB); + if (cosine > 1.0) { + return 0; + } + if (cosine < -1.0) { + return Math.PI; + } + return Math.acos(cosine); + }; +})(); + +export default vec2; \ No newline at end of file diff --git a/cocos/vmath/vec3.js b/cocos/vmath/vec3.js new file mode 100644 index 0000000000..3644c28090 --- /dev/null +++ b/cocos/vmath/vec3.js @@ -0,0 +1,908 @@ +import { EPSILON, random } from './utils'; + +/** + * Mathematical 3-dimensional vector. + * + * x, y, z is alias of the first, second, third component of vector, respectively. + */ +class vec3 { + /** + * Creates a vector, with components specified separately. + * + * @param {number} x - Value assigned to x component. + * @param {number} y - Value assigned to y component. + * @param {number} z - Value assigned to z component. + */ + constructor(x = 0, y = 0, z = 0) { + /** + * The x component. + * @type {number} + * */ + this.x = x; + + /** + * The y component. + * @type {number} + * */ + this.y = y; + + /** + * The z component. + * @type {number} + * */ + this.z = z; + } + + /** + * Creates a vector, with components specified separately. + * + * @param {number} x - Value assigned to x component. + * @param {number} y - Value assigned to y component. + * @param {number} z - Value assigned to z component. + * @return {vec3} The newly created vector. + */ + static create(x = 0, y = 0, z = 0) { + return new vec3(x, y, z); + } + + /** + * Creates a zero vector. + * + * @return {vec3} The newly created vector. + */ + static zero(out) { + out.x = 0; + out.y = 0; + out.z = 0; + return out; + } + + /** + * Clone a vector. + * + * @param {vec3} a - Vector to clone. + * @returns {vec3} The newly created vector. + */ + static clone(a) { + return new vec3(a.x, a.y, a.z); + } + + /** + * Copy content of a vector into another. + * + * @param {vec3} out - The vector to modified. + * @param {vec3} a - The specified vector. + * @returns {vec3} out. + */ + static copy(out, a) { + out.x = a.x; + out.y = a.y; + out.z = a.z; + return out; + } + + /** + * Sets the components of a vector to the given values. + * + * @param {vec3} out - The vector to modified. + * @param {Number} x - Value set to x component. + * @param {Number} y - Value set to y component. + * @param {Number} z - Value set to z component. + * @returns {vec3} out. + */ + static set(out, x, y, z) { + out.x = x; + out.y = y; + out.z = z; + return out; + } + + /** + * Add two vectors. + * + * It doesn't matter that any amount of these parameters refer to same vector. + * + * @param {vec3} out - Vector to store result. + * @param {vec3} a - The first operand. + * @param {vec3} b - The second operand. + * @returns {vec3} out. + */ + static add(out, a, b) { + out.x = a.x + b.x; + out.y = a.y + b.y; + out.z = a.z + b.z; + return out; + } + + /** + * Subtract two vectors. + * + * It doesn't matter that any amount of these parameters refer to same vector. + * + * @param {vec3} out - Vector to store result. + * @param {vec3} a - The first operand. + * @param {vec3} b - The second operand. + * @returns {vec3} out. + */ + static subtract(out, a, b) { + out.x = a.x - b.x; + out.y = a.y - b.y; + out.z = a.z - b.z; + return out; + } + + /** + * Alias of {@link vec3.subtract}. + */ + static sub(out, a, b) { + return vec3.subtract(out, a, b); + } + + /** + * Performs multiply on each component of two vectors respectively. + * + * It doesn't matter that any amount of these parameters refer to same vector. + * + * @param {vec3} out - Vector to store result. + * @param {vec3} a - The first operand. + * @param {vec3} b - The second operand. + * @returns {vec3} out. + */ + static multiply(out, a, b) { + out.x = a.x * b.x; + out.y = a.y * b.y; + out.z = a.z * b.z; + return out; + } + + /** + * Alias of {@link vec3.multiply}. + */ + static mul(out, a, b) { + return vec3.multiply(out, a, b); + } + + /** + * Performs division on each component of two vectors respectively. + * + * It doesn't matter that any amount of these parameters refer to same vector. + * + * @param {vec3} out - Vector to store result. + * @param {vec3} a - The first operand. + * @param {vec3} b - The second operand. + * @returns {vec3} out. + */ + static divide(out, a, b) { + out.x = a.x / b.x; + out.y = a.y / b.y; + out.z = a.z / b.z; + return out; + } + + /** + * Alias of {@link vec3.divide}. + */ + static div(out, a, b) { + return vec3.divide(out, a, b); + } + + /** + * Performs Math.ceil on each component of a vector. + * + * It doesn't matter that any amount of these parameters refer to same vector. + * + * @param {vec3} out - Vector to store result. + * @param {vec3} a - Vector to perform operation. + * @returns {vec3} out. + */ + static ceil(out, a) { + out.x = Math.ceil(a.x); + out.y = Math.ceil(a.y); + out.z = Math.ceil(a.z); + return out; + } + + /** + * Performs Math.floor on each component of a vector. + * + * It doesn't matter that any amount of these parameters refer to same vector. + * + * @param {vec3} out - Vector to store result. + * @param {vec3} a - Vector to perform operation. + * @returns {vec3} out. + */ + static floor(out, a) { + out.x = Math.floor(a.x); + out.y = Math.floor(a.y); + out.z = Math.floor(a.z); + return out; + } + + /** + * Performs Math.min on each component of two vectors respectively. + * + * It doesn't matter that any amount of these parameters refer to same vector. + * + * @param {vec3} out - Vector to store result. + * @param {vec3} a - The first operand. + * @param {vec3} b - The second operand. + * @returns {vec3} out. + */ + static min(out, a, b) { + out.x = Math.min(a.x, b.x); + out.y = Math.min(a.y, b.y); + out.z = Math.min(a.z, b.z); + return out; + } + + /** + * Performs Math.min on each component of two vectors respectively. + * + * It doesn't matter that any amount of these parameters refer to same vector. + * + * @param {vec3} out - Vector to store result. + * @param {vec3} a - The first operand. + * @param {vec3} b - The second operand. + * @returns {vec3} out. + */ + static max(out, a, b) { + out.x = Math.max(a.x, b.x); + out.y = Math.max(a.y, b.y); + out.z = Math.max(a.z, b.z); + return out; + } + + /** + * Performs Math.round on each component of a vector. + * + * It doesn't matter that any amount of these parameters refer to same vector. + * + * @param {vec3} out - Vector to store result. + * @param {vec3} a - Vector to perform operation. + * @returns {vec3} out. + */ + static round(out, a) { + out.x = Math.round(a.x); + out.y = Math.round(a.y); + out.z = Math.round(a.z); + return out; + } + + /** + * Scales a vector with a number. + * + * @param {vec3} out - Vector to store result. + * @param {vec3} a - Vector to scale. + * @param {number} b - The scale number. + * @returns {vec3} out. + * */ + static scale(out, a, b) { + out.x = a.x * b; + out.y = a.y * b; + out.z = a.z * b; + return out; + } + + /** + * Add two vectors after scaling the second operand by a number. + * + * @param {vec3} out - Vector to store result. + * @param {vec3} a - The first operand. + * @param {vec3} b - The second operand. + * @param {number} scale - The scale number before adding. + * @returns {vec3} out. + */ + static scaleAndAdd(out, a, b, scale) { + out.x = a.x + (b.x * scale); + out.y = a.y + (b.y * scale); + out.z = a.z + (b.z * scale); + return out; + } + + /** + * Calculates the euclidian distance between two vectors. + * + * @param {vec3} a - The first operand. + * @param {vec3} b - The second operand. + * @returns {number} Distance between a and b. + */ + static distance(a, b) { + let x = b.x - a.x, + y = b.y - a.y, + z = b.z - a.z; + return Math.sqrt(x * x + y * y + z * z); + } + + /** + * Alias of {@link vec3.distance}. + */ + static dist(a, b) { + return vec3.distance(a, b); + } + + /** + * Calculates the squared euclidian distance between two vectors. + * + * @param {vec3} a - The first operand. + * @param {vec3} b - The second operand. + * @returns {number} Squared distance between a and b. + */ + static squaredDistance(a, b) { + let x = b.x - a.x, + y = b.y - a.y, + z = b.z - a.z; + return x * x + y * y + z * z; + } + + /** + * Alias of {@link vec3.squaredDistance}. + */ + static sqrDist(a, b) { + return vec3.squaredDistance(a, b); + } + + /** + * Calculates the length of a vector. + * + * @param {vec3} a - The vector. + * @returns {Number} Length of the vector. + */ + static magnitude(a) { + let x = a.x, + y = a.y, + z = a.z; + return Math.sqrt(x * x + y * y + z * z); + } + + /** + *Alias of {@link vec3.magnitude}. + */ + static mag(a) { + return vec3.magnitude(a); + } + + /** + * Calculates the squared length of a vector. + * + * @param {vec3} a - The vector. + * @returns {Number} Squared length of the vector. + */ + static squaredMagnitude(a) { + let x = a.x, + y = a.y, + z = a.z; + return x * x + y * y + z * z; + } + + /** + *Alias of {@link vec3.squaredMagnitude} + */ + static sqrMag(a) { + return vec3.squaredMagnitude(a); + } + + /** + * Negates each component of a vector. + * + * @param {vec3} out - Vector to store result. + * @param {vec3} a - Vector to negate. + * @returns {vec3} out. + */ + static negate(out, a) { + out.x = -a.x; + out.y = -a.y; + out.z = -a.z; + return out; + } + + /** + * Inverts the components of a vector. + * + * @param {vec3} out - Vector to store result. + * @param {vec3} a - Vector to invert. + * @returns {vec3} out. + */ + static inverse(out, a) { + out.x = 1.0 / a.x; + out.y = 1.0 / a.y; + out.z = 1.0 / a.z; + return out; + } + + /** + * Safely inverts the components of a vector. + * + * @param {vec3} out - Vector to store result. + * @param {vec3} a - Vector to invert. + * @returns {vec3} out. + */ + static inverseSafe(out, a) { + let x = a.x, + y = a.y, + z = a.z; + + if (Math.abs(x) < EPSILON) { + out.x = 0; + } else { + out.x = 1.0 / x; + } + + if (Math.abs(y) < EPSILON) { + out.y = 0; + } else { + out.y = 1.0 / y; + } + + if (Math.abs(z) < EPSILON) { + out.z = 0; + } else { + out.z = 1.0 / z; + } + + return out; + } + + /** + * Normalizes a vector. + * + * @param {vec3} out - Vector to store result. + * @param {vec3} a - Vector to normalize. + * @returns {vec3} out. + */ + static normalize(out, a) { + let x = a.x, + y = a.y, + z = a.z; + + let len = x * x + y * y + z * z; + if (len > 0) { + //TODO: evaluate use of glm_invsqrt here? + len = 1 / Math.sqrt(len); + out.x = x * len; + out.y = y * len; + out.z = z * len; + } + return out; + } + + /** + * Calculates the dot product of two vectors. + * + * @param {vec3} a - The first operand. + * @param {vec3} b - The second operand. + * @returns {Number} Dot product of a and b. + */ + static dot(a, b) { + return a.x * b.x + a.y * b.y + a.z * b.z; + } + + /** + * Calculates the cross product of two vectors. + * + * @param {vec3} out - Vector to store result. + * @param {vec3} a - The first operand. + * @param {vec3} b - The second operand. + * @returns {vec3} out. + */ + static cross(out, a, b) { + let ax = a.x, ay = a.y, az = a.z, + bx = b.x, by = b.y, bz = b.z; + + out.x = ay * bz - az * by; + out.y = az * bx - ax * bz; + out.z = ax * by - ay * bx; + return out; + } + + /** + * Performs a linear interpolation between two vectors. + * + * @param {vec3} out - Vector to store result. + * @param {vec3} a - The first operand. + * @param {vec3} b - The second operand. + * @param {Number} t - The interpolation coefficient. + * @returns {vec3} out. + */ + static lerp(out, a, b, t) { + let ax = a.x, + ay = a.y, + az = a.z; + out.x = ax + t * (b.x - ax); + out.y = ay + t * (b.y - ay); + out.z = az + t * (b.z - az); + return out; + } + + /** + * Performs a hermite interpolation with two control points. + * + * @param {vec3} out - Vector to store result. + * @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 - The interpolation coefficient. + * @returns {vec3} out. + */ + static hermite(out, a, b, c, d, t) { + let factorTimes2 = t * t, + factor1 = factorTimes2 * (2 * t - 3) + 1, + factor2 = factorTimes2 * (t - 2) + t, + factor3 = factorTimes2 * (t - 1), + factor4 = factorTimes2 * (3 - 2 * t); + + out.x = a.x * factor1 + b.x * factor2 + c.x * factor3 + d.x * factor4; + out.y = a.y * factor1 + b.y * factor2 + c.y * factor3 + d.y * factor4; + out.z = a.z * factor1 + b.z * factor2 + c.z * factor3 + d.z * factor4; + + return out; + } + + /** + * Performs a bezier interpolation with two control points. + * + * @param {vec3} out - Vector to store result. + * @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 - The interpolation coefficient. + * @returns {vec3} out. + */ + static bezier(out, a, b, c, d, t) { + let inverseFactor = 1 - t, + inverseFactorTimesTwo = inverseFactor * inverseFactor, + factorTimes2 = t * t, + factor1 = inverseFactorTimesTwo * inverseFactor, + factor2 = 3 * t * inverseFactorTimesTwo, + factor3 = 3 * factorTimes2 * inverseFactor, + factor4 = factorTimes2 * t; + + out.x = a.x * factor1 + b.x * factor2 + c.x * factor3 + d.x * factor4; + out.y = a.y * factor1 + b.y * factor2 + c.y * factor3 + d.y * factor4; + out.z = a.z * factor1 + b.z * factor2 + c.z * factor3 + d.z * factor4; + + return out; + } + + /** + * Generates a random vector with the given scale. + * + * @param {vec3} out - Vector to store result. + * @param {Number} [scale] Length of the resulting vector. If ommitted, a unit vector will be returned. + * @returns {vec3} out. + */ + static random(out, scale) { + scale = scale || 1.0; + + let r = random() * 2.0 * Math.PI; + let z = (random() * 2.0) - 1.0; + let zScale = Math.sqrt(1.0 - z * z) * scale; + + out.x = Math.cos(r) * zScale; + out.y = Math.sin(r) * zScale; + out.z = z * scale; + return out; + } + + /** + * Transforms a vector with a 4x4 matrix. 4th vector component is implicitly '1'. + * + * @param {vec3} out - Vector to store result. + * @param {vec3} a - Vector to transform. + * @param {mat4} m - The matrix. + * @returns {vec3} out. + */ + static transformMat4(out, a, m) { + let x = a.x, y = a.y, z = a.z, + w = m.m03 * x + m.m07 * y + m.m11 * z + m.m15; + w = w || 1.0; + out.x = (m.m00 * x + m.m04 * y + m.m08 * z + m.m12) / w; + out.y = (m.m01 * x + m.m05 * y + m.m09 * z + m.m13) / w; + out.z = (m.m02 * x + m.m06 * y + m.m10 * z + m.m14) / w; + return out; + } + + /** + * Transforms a vector with a 3x3 matrix. + * + * @param {vec3} out - Vector to store result. + * @param {vec3} a - Vector to transform. + * @param {mat3} m - The matrix. + * @returns {vec3} out. + */ + static transformMat3(out, a, m) { + let x = a.x, y = a.y, z = a.z; + out.x = x * m.m00 + y * m.m03 + z * m.m06; + out.y = x * m.m01 + y * m.m04 + z * m.m07; + out.z = x * m.m02 + y * m.m05 + z * m.m08; + return out; + } + + /** + * Transforms a vector with a quaternion. + * + * @param {vec3} out - Vector to store result. + * @param {vec3} a - Vector to transform. + * @param {quat} q - The quaternion. + * @returns {vec3} out. + */ + static transformQuat(out, a, q) { + // benchmarks: http://jsperf.com/quaternion-transform-vec3-implementations + + let x = a.x, y = a.y, z = a.z; + let qx = q.x, qy = q.y, qz = q.z, qw = q.w; + + // calculate quat * vec + let ix = qw * x + qy * z - qz * y; + let iy = qw * y + qz * x - qx * z; + let iz = qw * z + qx * y - qy * x; + let iw = -qx * x - qy * y - qz * z; + + // calculate result * inverse quat + out.x = ix * qw + iw * -qx + iy * -qz - iz * -qy; + out.y = iy * qw + iw * -qy + iz * -qx - ix * -qz; + out.z = iz * qw + iw * -qz + ix * -qy - iy * -qx; + return out; + } + + /** + * Rotates a 3D vector around the x-axis. + * @param {vec3} out - Vector to store result. + * @param {vec3} a - The point to rotate. + * @param {vec3} b - The origin of the rotation. + * @param {Number} c - The angle of rotation. + * @returns {vec3} out. + */ + static rotateX(out, a, b, c) { + // Translate point to the origin + let px = a.x - b.x; + let py = a.y - b.y; + let pz = a.z - b.z; + + //perform rotation + let rx = px; + let ry = py * Math.cos(c) - pz * Math.sin(c); + let rz = py * Math.sin(c) + pz * Math.cos(c); + + //translate to correct position + out.x = rx + b.x; + out.y = ry + b.y; + out.z = rz + b.z; + + return out; + } + + /** + * Rotates a 3D vector around the y-axis. + * @param {vec3} out - Vector to store result. + * @param {vec3} a - The point to rotate. + * @param {vec3} b - The origin of the rotation. + * @param {Number} c - The angle of rotation. + * @returns {vec3} out. + */ + static rotateY(out, a, b, c) { + //Translate point to the origin + let px = a.x - b.x; + let py = a.y - b.y; + let pz = a.z - b.z; + + //perform rotation + let rx = pz * Math.sin(c) + px * Math.cos(c); + let ry = py; + let rz = pz * Math.cos(c) - px * Math.sin(c); + + //translate to correct position + out.x = rx + b.x; + out.y = ry + b.y; + out.z = rz + b.z; + + return out; + } + + /** + * Rotates a 3D vector around the z-axis. + * @param {vec3} out - Vector to store result. + * @param {vec3} a - The point to rotate. + * @param {vec3} b - The origin of the rotation. + * @param {Number} c - The angle of rotation. + * @returns {vec3} out. + */ + static rotateZ(out, a, b, c) { + //Translate point to the origin + let px = a.x - b.x; + let py = a.y - b.y; + let pz = a.z - b.z; + + //perform rotation + let rx = px * Math.cos(c) - py * Math.sin(c); + let ry = px * Math.sin(c) + py * Math.cos(c); + let rz = pz; + + //translate to correct position + out.x = rx + b.x; + out.y = ry + b.y; + out.z = rz + b.z; + + return out; + } + + /** + * Returns string representation of a vector. + * + * @param {vec3} a - The vector. + * @returns {String} - String representation of this vector. + */ + static str(a) { + return `vec3(${a.x}, ${a.y}, ${a.z})`; + } + + /** + * Store components of a vector into array. + * + * @param {Array} out - Array to store result. + * @param {vec3} v - The vector. + * @returns {Array} out. + */ + static array(out, v) { + out[0] = v.x; + out[1] = v.y; + out[2] = v.z; + + return out; + } + + /** + * Returns whether the specified vectors are equal. (Compared using ===) + * + * @param {vec3} a - The first vector. + * @param {vec3} b - The second vector. + * @returns {Boolean} True if the vectors are equal, false otherwise. + */ + static exactEquals(a, b) { + return a.x === b.x && a.y === b.y && a.z === b.z; + } + + /** + * Returns whether the specified vectors are approximately equal. + * + * @param {vec3} a The first vector. + * @param {vec3} b The second vector. + * @returns {Boolean} True if the vectors are approximately equal, false otherwise. + */ + static equals(a, b) { + let a0 = a.x, a1 = a.y, a2 = a.z; + let b0 = b.x, b1 = b.y, b2 = b.z; + return (Math.abs(a0 - b0) <= EPSILON * Math.max(1.0, Math.abs(a0), Math.abs(b0)) && + Math.abs(a1 - b1) <= EPSILON * Math.max(1.0, Math.abs(a1), Math.abs(b1)) && + Math.abs(a2 - b2) <= EPSILON * Math.max(1.0, Math.abs(a2), Math.abs(b2))); + } + + /** + * 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. + */ + static forEach(a, stride, offset, count, fn, arg) { + return vec3._forEach(a, stride, offset, count, fn, arg); + } + + /** + * Gets the angle between two vectors. + * @param {vec3} a - The first operand. + * @param {vec3} b - The second operand. + * @returns {Number} - The angle in radians. + */ + static angle(a, b) { + return vec3._angle(a, b); + } + + /** + * Projects a vector onto a plane represented by its normal. + * @param {vec3} out The result vector. + * @param {vec3} a The vector. + * @param {vec3} n The plane's normal. + */ + static projectOnPlane(out, a, n) { + return vec3.sub(out, a, vec3.project(out, a, n)); + } + + /** + * Projects a vector onto another vector. + * @param {vec3} out The result vector. + * @param {vec3} a The vector to project. + * @param {vec3} b The vector onto which the projection performs. + */ + static project(out, a, b) { + let sqrLen = vec3.squaredMagnitude(b); + if (sqrLen < 0.000001) + return vec3.set(out, 0, 0, 0); + else + return vec3.scale(out, b, vec3.dot(a, b) / sqrLen); + } +} + +/** +* 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. +* @ignore. +*/ +vec3._forEach = (function () { + let vec = vec3.create(0, 0, 0); + + return function (a, stride, offset, count, fn, arg) { + let 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.x = a[i]; + vec.y = a[i + 1]; + vec.z = a[i + 2]; + fn(vec, vec, arg); + a[i] = vec.x; + a[i + 1] = vec.y; + a[i + 2] = vec.z; + } + + 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. + * @ignore + */ +vec3._angle = (function () { + let tempA = vec3.create(0, 0, 0); + let tempB = vec3.create(0, 0, 0); + + return function (a, b) { + vec3.copy(tempA, a); + vec3.copy(tempB, b); + + vec3.normalize(tempA, tempA); + vec3.normalize(tempB, tempB); + + let cosine = vec3.dot(tempA, tempB); + + if (cosine > 1.0) { + return 0; + } + + if (cosine < -1.0) { + return Math.PI; + } + + return Math.acos(cosine); + }; +})(); + +export default vec3; \ No newline at end of file diff --git a/cocos/vmath/vec4.js b/cocos/vmath/vec4.js new file mode 100644 index 0000000000..475c974dd2 --- /dev/null +++ b/cocos/vmath/vec4.js @@ -0,0 +1,715 @@ +import { EPSILON, random } from './utils'; + +/** + * Mathematical 4-dimensional vector. + * + * x, y, z, w is alias of the first, second, third, fourth component of vector, respectively. + */ +class vec4 { + /** + * Creates a vector, with components specified separately. + * + * @param {number} x - Value assigned to x component. + * @param {number} y - Value assigned to y component. + * @param {number} z - Value assigned to z component. + * @param {number} w - Value assigned to w component. + */ + constructor(x = 0, y = 0, z = 0, w = 1) { + /** + * The x component. + * @type {number} + * */ + this.x = x; + + /** + * The y component. + * @type {number} + * */ + this.y = y; + + /** + * The z component. + * @type {number} + * */ + this.z = z; + + /** + * The w component. + * @type {number} + * */ + this.w = w; + } + + /** + * Create a vector, with components specified separately. + * + * @param {number} x - Value assigned to x component. + * @param {number} y - Value assigned to y component. + * @param {number} z - Value assigned to z component. + * @param {number} w - Value assigned to w component. + * @return {vec4} The newly created vector. + */ + static create(x = 0, y = 0, z = 0, w = 1) { + return new vec4(x, y, z, w); + } + + /** + * Creates a zero vector. + * + * @return {vec4} The newly created vector. + */ + static zero(out) { + out.x = 0; + out.y = 0; + out.z = 0; + out.w = 0; + return out; + } + + /** + * Clone a vector. + * + * @param {vec4} a - Vector to clone. + * @returns {vec4} The newly created vector. + */ + static clone(a) { + return new vec4(a.x, a.y, a.z, a.w); + } + + /** + * Copy content of a vector into another. + * + * @param {vec4} out - The vector to modified. + * @param {vec4} a - The specified vector. + * @returns {vec4} out. + */ + static copy(out, a) { + out.x = a.x; + out.y = a.y; + out.z = a.z; + out.w = a.w; + return out; + } + + /** + * Sets the components of a vector to the given values. + * + * @param {vec4} out - The vector to modified. + * @param {Number} x - Value set to x component. + * @param {Number} y - Value set to y component. + * @param {Number} z - Value set to z component. + * @param {Number} w - Value set to w component. + * @returns {vec4} out. + */ + static set(out, x, y, z, w) { + out.x = x; + out.y = y; + out.z = z; + out.w = w; + return out; + } + + /** + * Add two vectors. + * + * It doesn't matter that any amount of these parameters refer to same vector. + * + * @param {vec4} out - Vector to store result. + * @param {vec4} a - The first operand. + * @param {vec4} b - The second operand. + * @returns {vec4} out. + */ + static add(out, a, b) { + out.x = a.x + b.x; + out.y = a.y + b.y; + out.z = a.z + b.z; + out.w = a.w + b.w; + return out; + } + + /** + * Subtract two vectors. + * + * It doesn't matter that any amount of these parameters refer to same vector. + * + * @param {vec4} out - Vector to store result. + * @param {vec4} a - The first operand. + * @param {vec4} b - The second operand. + * @returns {vec4} out. + */ + static subtract(out, a, b) { + out.x = a.x - b.x; + out.y = a.y - b.y; + out.z = a.z - b.z; + out.w = a.w - b.w; + return out; + } + + /** + * Alias of {@link vec4.subtract}. + */ + static sub(out, a, b) { + return vec4.subtract(out, a, b); + } + + /** + * Performs multiply on each component of two vectors respectively. + * + * It doesn't matter that any amount of these parameters refer to same vector. + * + * @param {vec4} out - Vector to store result. + * @param {vec4} a - The first operand. + * @param {vec4} b - The second operand. + * @returns {vec4} out. + */ + static multiply(out, a, b) { + out.x = a.x * b.x; + out.y = a.y * b.y; + out.z = a.z * b.z; + out.w = a.w * b.w; + return out; + } + + /** + * Alias of {@link vec4.multiply}. + */ + static mul(out, a, b) { + return vec4.multiply(out, a, b); + } + + /** + * Performs division on each component of two vectors respectively. + * + * It doesn't matter that any amount of these parameters refer to same vector. + * + * @param {vec4} out - Vector to store result. + * @param {vec4} a - The first operand. + * @param {vec4} b - The second operand. + * @returns {vec4} out. + */ + static divide(out, a, b) { + out.x = a.x / b.x; + out.y = a.y / b.y; + out.z = a.z / b.z; + out.w = a.w / b.w; + return out; + } + + /** + * Alias of {@link vec4.divide}. + */ + static div(out, a, b) { + return vec4.divide(out, a, b); + } + + /** + * Performs Math.ceil on each component of a vector. + * + * It doesn't matter that any amount of these parameters refer to same vector. + * + * @param {vec4} out - Vector to store result. + * @param {vec4} a - Vector to perform operation. + * @returns {vec4} out. + */ + static ceil(out, a) { + out.x = Math.ceil(a.x); + out.y = Math.ceil(a.y); + out.z = Math.ceil(a.z); + out.w = Math.ceil(a.w); + return out; + } + + /** + * Performs Math.floor on each component of a vector. + * + * It doesn't matter that any amount of these parameters refer to same vector. + * + * @param {vec4} out - Vector to store result. + * @param {vec4} a - Vector to perform operation. + * @returns {vec4} out. + */ + static floor(out, a) { + out.x = Math.floor(a.x); + out.y = Math.floor(a.y); + out.z = Math.floor(a.z); + out.w = Math.floor(a.w); + return out; + } + + /** + * Performs Math.min on each component of two vectors respectively. + * + * It doesn't matter that any amount of these parameters refer to same vector. + * + * @param {vec4} out - Vector to store result. + * @param {vec4} a - The first operand. + * @param {vec4} b - The second operand. + * @returns {vec4} out. + */ + static min(out, a, b) { + out.x = Math.min(a.x, b.x); + out.y = Math.min(a.y, b.y); + out.z = Math.min(a.z, b.z); + out.w = Math.min(a.w, b.w); + return out; + } + + /** + * Performs Math.min on each component of two vectors respectively. + * + * It doesn't matter that any amount of these parameters refer to same vector. + * + * @param {vec4} out - Vector to store result. + * @param {vec4} a - The first operand. + * @param {vec4} b - The second operand. + * @returns {vec4} out. + */ + static max(out, a, b) { + out.x = Math.max(a.x, b.x); + out.y = Math.max(a.y, b.y); + out.z = Math.max(a.z, b.z); + out.w = Math.max(a.w, b.w); + return out; + } + + /** + * Performs Math.round on each component of a vector. + * + * It doesn't matter that any amount of these parameters refer to same vector. + * + * @param {vec4} out - Vector to store result. + * @param {vec4} a - Vector to perform operation. + * @returns {vec4} out. + */ + static round(out, a) { + out.x = Math.round(a.x); + out.y = Math.round(a.y); + out.z = Math.round(a.z); + out.w = Math.round(a.w); + return out; + } + + /** + * Scales a vector with a number. + * + * @param {vec4} out - Vector to store result. + * @param {vec4} a - Vector to scale. + * @param {number} b - The scale number. + * @returns {vec4} out. + * */ + static scale(out, a, b) { + out.x = a.x * b; + out.y = a.y * b; + out.z = a.z * b; + out.w = a.w * b; + return out; + } + + /** + * Add two vectors after scaling the second operand by a number. + * + * @param {vec4} out - Vector to store result. + * @param {vec4} a - The first operand. + * @param {vec4} b - The second operand. + * @param {number} scale - The scale number before adding. + * @returns {vec4} out. + */ + static scaleAndAdd(out, a, b, scale) { + out.x = a.x + (b.x * scale); + out.y = a.y + (b.y * scale); + out.z = a.z + (b.z * scale); + out.w = a.w + (b.w * scale); + return out; + } + + /** + * Calculates the euclidian distance between two vectors. + * + * @param {vec4} a - The first operand. + * @param {vec4} b - The second operand. + * @returns {number} Distance between a and b. + */ + static distance(a, b) { + let x = b.x - a.x, + y = b.y - a.y, + z = b.z - a.z, + w = b.w - a.w; + return Math.sqrt(x * x + y * y + z * z + w * w); + } + + /** + * Alias of {@link vec4.distance}. + */ + static dist(a, b) { + return vec4.distance(a, b); + } + + /** + * Calculates the squared euclidian distance between two vectors. + * + * @param {vec4} a - The first operand. + * @param {vec4} b - The second operand. + * @returns {number} Squared distance between a and b. + */ + static squaredDistance(a, b) { + let x = b.x - a.x, + y = b.y - a.y, + z = b.z - a.z, + w = b.w - a.w; + return x * x + y * y + z * z + w * w; + } + + /** + * Alias of {@link vec4.squaredDistance}. + */ + static sqrDist(a, b) { + return vec4.squaredDistance(a, b); + } + + /** + * Calculates the length of a vector. + * + * @param {vec4} a - The vector. + * @returns {Number} Length of the vector. + */ + static magnitude(a) { + let x = a.x, + y = a.y, + z = a.z, + w = a.w; + return Math.sqrt(x * x + y * y + z * z + w * w); + } + + /** + *Alias of {@link vec4.magnitude}. + */ + static mag(a) { + return vec4.magnitude(a); + } + + /** + * Calculates the squared length of a vector. + * + * @param {vec4} a - The vector. + * @returns {Number} Squared length of the vector. + */ + static squaredMagnitude(a) { + let x = a.x, + y = a.y, + z = a.z, + w = a.w; + return x * x + y * y + z * z + w * w; + } + + /** + *Alias of {@link vec4.squaredMagnitude} + */ + static sqrMag(a) { + return vec4.squaredMagnitude(a); + } + + /** + * Negates each component of a vector. + * + * @param {vec4} out - Vector to store result. + * @param {vec4} a - Vector to negate. + * @returns {vec4} out. + */ + static negate(out, a) { + out.x = -a.x; + out.y = -a.y; + out.z = -a.z; + out.w = -a.w; + return out; + } + + /** + * Inverts the components of a vector. + * + * @param {vec4} out - Vector to store result. + * @param {vec4} a - Vector to invert. + * @returns {vec4} out. + */ + static inverse(out, a) { + out.x = 1.0 / a.x; + out.y = 1.0 / a.y; + out.z = 1.0 / a.z; + out.w = 1.0 / a.w; + return out; + } + + /** + * Safely inverts the components of a vector. + * + * @param {vec4} out - Vector to store result. + * @param {vec4} a - Vector to invert. + * @returns {vec4} out. + */ + static inverseSafe(out, a) { + let x = a.x, + y = a.y, + z = a.z, + w = a.w; + + if (Math.abs(x) < EPSILON) { + out.x = 0; + } else { + out.x = 1.0 / x; + } + + if (Math.abs(y) < EPSILON) { + out.y = 0; + } else { + out.y = 1.0 / y; + } + + if (Math.abs(z) < EPSILON) { + out.z = 0; + } else { + out.z = 1.0 / z; + } + + if (Math.abs(w) < EPSILON) { + out.w = 0; + } else { + out.w = 1.0 / w; + } + + return out; + } + + /** + * Normalizes a vector. + * + * @param {vec4} out - Vector to store result. + * @param {vec4} a - Vector to normalize. + * @returns {vec4} out. + */ + static normalize(out, a) { + let x = a.x, + y = a.y, + z = a.z, + w = a.w; + let len = x * x + y * y + z * z + w * w; + if (len > 0) { + len = 1 / Math.sqrt(len); + out.x = x * len; + out.y = y * len; + out.z = z * len; + out.w = w * len; + } + return out; + } + + /** + * Calculates the dot product of two vectors. + * + * @param {vec4} a - The first operand. + * @param {vec4} b - The second operand. + * @returns {Number} Dot product of a and b. + */ + static dot(a, b) { + return a.x * b.x + a.y * b.y + a.z * b.z + a.w * b.w; + } + + /** + * Performs a linear interpolation between two vectors. + * + * @param {vec4} out - Vector to store result. + * @param {vec4} a - The first operand. + * @param {vec4} b - The second operand. + * @param {Number} t - The interpolation coefficient. + * @returns {vec4} out. + */ + static lerp(out, a, b, t) { + let ax = a.x, + ay = a.y, + az = a.z, + aw = a.w; + out.x = ax + t * (b.x - ax); + out.y = ay + t * (b.y - ay); + out.z = az + t * (b.z - az); + out.w = aw + t * (b.w - aw); + return out; + } + + /** + * Generates a random vector with the given scale. + * + * @param {vec4} out - Vector to store result. + * @param {Number} [scale] Length of the resulting vector. If ommitted, a unit vector will be returned. + * @returns {vec4} out. + */ + static random(out, scale) { + scale = scale || 1.0; + + //TODO: This is a pretty awful way of doing this. Find something better. + out.x = random(); + out.y = random(); + out.z = random(); + out.w = random(); + vec4.normalize(out, out); + vec4.scale(out, out, scale); + return out; + } + + /** + * Transforms a vector with a 4x4 matrix. + * + * @param {vec4} out - Vector to store result. + * @param {vec4} a - Vector to transform. + * @param {mat4} m - The matrix. + * @returns {vec4} out. + */ + static transformMat4(out, a, m) { + let x = a.x, y = a.y, z = a.z, w = a.w; + out.x = m.m00 * x + m.m04 * y + m.m08 * z + m.m12 * w; + out.y = m.m01 * x + m.m05 * y + m.m09 * z + m.m13 * w; + out.z = m.m02 * x + m.m06 * y + m.m10 * z + m.m14 * w; + out.w = m.m03 * x + m.m07 * y + m.m11 * z + m.m15 * w; + return out; + } + + /** + * Transforms a vector with a quaternion. + * + * @param {vec4} out - Vector to store result. + * @param {vec4} a - Vector to transform. + * @param {quat} q - The quaternion. + * @returns {vec4} out. + */ + static transformQuat(out, a, q) { + let x = a.x, y = a.y, z = a.z; + let qx = q.x, qy = q.y, qz = q.z, qw = q.w; + + // calculate quat * vec + let ix = qw * x + qy * z - qz * y; + let iy = qw * y + qz * x - qx * z; + let iz = qw * z + qx * y - qy * x; + let iw = -qx * x - qy * y - qz * z; + + // calculate result * inverse quat + out.x = ix * qw + iw * -qx + iy * -qz - iz * -qy; + out.y = iy * qw + iw * -qy + iz * -qx - ix * -qz; + out.z = iz * qw + iw * -qz + ix * -qy - iy * -qx; + out.w = a.w; + return out; + } + + /** + * Returns string representation of a vector. + * + * @param {vec4} a - The vector. + * @returns {String} - String representation of this vector. + */ + static str(a) { + return `vec4(${a.x}, ${a.y}, ${a.z}, ${a.w})`; + } + + /** + * Store components of a vector into array. + * + * @param {Array} out - Array to store result. + * @param {vec4} v - The vector. + * @returns {Array} out. + */ + static array(out, v) { + out[0] = v.x; + out[1] = v.y; + out[2] = v.z; + out[3] = v.w; + + return out; + } + + /** + * Returns whether the specified vectors are equal. (Compared using ===) + * + * @param {vec4} a - The first vector. + * @param {vec4} b - The second vector. + * @returns {Boolean} True if the vectors are equal, false otherwise. + */ + static exactEquals(a, b) { + return a.x === b.x && a.y === b.y && a.z === b.z && a.w === b.w; + } + + /** + * Returns whether the specified vectors are approximately equal. + * + * @param {vec4} a The first vector. + * @param {vec4} b The second vector. + * @returns {Boolean} True if the vectors are approximately equal, false otherwise. + */ + static equals(a, b) { + let a0 = a.x, a1 = a.y, a2 = a.z, a3 = a.w; + let b0 = b.x, b1 = b.y, b2 = b.z, b3 = b.w; + return (Math.abs(a0 - b0) <= EPSILON * Math.max(1.0, Math.abs(a0), Math.abs(b0)) && + Math.abs(a1 - b1) <= EPSILON * Math.max(1.0, Math.abs(a1), Math.abs(b1)) && + Math.abs(a2 - b2) <= EPSILON * Math.max(1.0, Math.abs(a2), Math.abs(b2)) && + Math.abs(a3 - b3) <= EPSILON * Math.max(1.0, Math.abs(a3), Math.abs(b3))); + } + + /** + * Performs 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. + */ + static forEach(a, stride, offset, count, fn, arg) { + return vec4._forEach(a, stride, offset, count, fn, arg); + } +} + +/** + * 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. + * @ignore + */ +vec4._forEach = (function () { + let vec = vec4.create(0, 0, 0, 0); + + return function (a, stride, offset, count, fn, arg) { + let 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.x = a[i]; + vec.y = a[i + 1]; + vec.z = a[i + 2]; + vec.w = a[i + 3]; + fn(vec, vec, arg); + a[i] = vec.x; + a[i + 1] = vec.y; + a[i + 2] = vec.z; + a[i + 3] = vec.w; + } + + return a; + }; +})(); + +export default vec4; \ No newline at end of file