add anisotropy(from 3d) and mipFilter(from 3d) properties to texture2d;upload 3d vmath (#3205)

This commit is contained in:
Leslie Leigh
2018-09-06 14:21:34 +08:00
committed by pandamicro
parent 2e65ac93f0
commit 75f561d796
14 changed files with 7888 additions and 4 deletions
+66 -4
View File
@@ -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]);
}
}
});
+307
View File
@@ -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));
}
+282
View File
@@ -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;
+305
View File
@@ -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;
+19
View File
@@ -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';
+443
View File
@@ -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;
+547
View File
@@ -0,0 +1,547 @@
import { EPSILON } from './utils';
/**
* Mathematical 2x3 matrix.
*
* A mat23 contains six elements defined as:
* <pre>
* [a, c, tx,
* b, d, ty]
* </pre>
* This is a short form for the 3x3 matrix:
* <pre>
* [a, c, tx,
* b, d, ty,
* 0, 0, 1]
* </pre>
* The last row is ignored so the array is shorter and operations are faster.
*/
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;
+851
View File
@@ -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;
+1729
View File
File diff suppressed because it is too large Load Diff
+808
View File
@@ -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;
+217
View File
@@ -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);
}
+691
View File
@@ -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;
+908
View File
@@ -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;
+715
View File
@@ -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;