mjs

Vector and Matrix math utilities for JavaScript, optimized for WebGL.

Summary
mjsVector and Matrix math utilities for JavaScript, optimized for WebGL.
Constants
MJS_VERSION0xaabbcc.
MJS_DO_ASSERTEnables or disables runtime assertions.
MJS_FLOAT_ARRAY_TYPEThe base float array type.
V3Methods for working with 3-element vectors.
Functions
V3.$Creates a new 3-element vector with the given values.
V3.cloneClone the given 3-element vector.
V3.addPerform r = a + b.
V3.subPerform r = a - b.
V3.negPerform r = - a.
V3.directionPerform r = (a - b) / |a - b|.
V3.lengthPerform r = |a|.
V3.lengthSquardPerform r = |a|*|a|.
V3.normalizePerform r = a / |a|.
V3.scalePerform r = a * k.
V3.dotPerform r = dot(a, b).
V3.crossPerform r = a x b.
M4x4Methods for working with 4x4 matrices.
Functions
M4x4.$Creates a new 4x4 matrix with the given values.
M4x4.cloneClone the given 4x4 matrix.
M4x4.topLeft3x3Return the top left 3x3 matrix from the given 4x4 matrix m.
M4x4.inverseOrthonormalComputes the inverse of the given matrix m, assuming that the matrix is orthonormal.
M4x4.inverseTo3x3Computes the inverse of the given matrix m, but calculates only the top left 3x3 values of the result.
M4x4.makeFrustumCreates a matrix for a projection frustum with the given parameters.
M4x4.makePerspectiveCreates a matrix for a perspective projection with the given parameters.
M4x4.makeOrthoCreates a matrix for an orthogonal frustum projection with the given parameters.
M4x4.makeOrthoCreates a matrix for a 2D orthogonal frustum projection with the given parameters.
M4x4.mulPerforms r = a * b.
M4x4.makeRotateCreates a transformation matrix for rotation by angle radians about the 3-element vector axis.
M4x4.rotateConcatenates a rotation of angle radians about the axis to the give matrix m.
M4x4.makeScale3Creates a transformation matrix for scaling by 3 scalar values, one for each of the x, y, and z directions.
M4x4.makeScale1Creates a transformation matrix for a uniform scale by a single scalar value.
M4x4.makeScaleCreates a transformation matrix for scaling each of the x, y, and z axes by the amount given in the corresponding element of the 3-element vector.
M4x4.scale3
M4x4.scale1
M4x4.scale1
M4x4.makeTranslate3
M4x4.makeTranslate1
M4x4.makeTranslate
M4x4.translate3
M4x4.translate1
M4x4.translate
M4x4.makeLookAt
M4x4.transpose

Constants

MJS_VERSION

mjs version number aa.bb.cc, encoded as an integer of the form

0xaabbcc.

MJS_DO_ASSERT

Enables or disables runtime assertions.

For potentially more performance, the assert methods can be commented out in each place where they are called.

MJS_FLOAT_ARRAY_TYPE

The base float array type.  By default, WebGLFloatArray.

mjs can work with any array-like elements, but if an array creation is requested, it will create an array of the type MJS_FLOAT_ARRAY_TYPE.  Also, the builtin constants such as (M4x4.I) will be of this type.

V3

Methods for working with 3-element vectors.  A vector is represented by a 3-element array.  Any valid JavaScript array type may be used, but if new vectors are created they are created using the configured MJS_FLOAT_ARRAY_TYPE.

Summary
Functions
V3.$Creates a new 3-element vector with the given values.
V3.cloneClone the given 3-element vector.
V3.addPerform r = a + b.
V3.subPerform r = a - b.
V3.negPerform r = - a.
V3.directionPerform r = (a - b) / |a - b|.
V3.lengthPerform r = |a|.
V3.lengthSquardPerform r = |a|*|a|.
V3.normalizePerform r = a / |a|.
V3.scalePerform r = a * k.
V3.dotPerform r = dot(a, b).
V3.crossPerform r = a x b.

Functions

V3.$

V3.$ = function V3_$(x,
y,
z)

Creates a new 3-element vector with the given values.

Parameters

x,y,zthe 3 elements of the new vector.

Returns

A new vector containing with the given argument values.

V3.clone

V3.clone = function V3_clone(a)

Clone the given 3-element vector.

Parameters

athe 3-element vector to clone

Returns

A new vector with the same values as the passed-in one.

V3.add

V3.add = function V3_add(a,
b,
r)

Perform r = a + b.

Parameters

athe first vector operand
bthe second vector operand
roptional vector to store the result in

Returns

If r is specified, returns r after performing the operation.  Otherwise, returns a new 3-element vector with the result.

V3.sub

V3.sub = function V3_sub(a,
b,
r)

Perform r = a - b.

Parameters

athe first vector operand
bthe second vector operand
roptional vector to store the result in

Returns

If r is specified, returns r after performing the operation.  Otherwise, returns a new 3-element vector with the result.

V3.neg

V3.neg = function V3_neg(a,
r)

Perform r = - a.

Parameters

athe vector operand
roptional vector to store the result in

Returns

If r is specified, returns r after performing the operation.  Otherwise, returns a new 3-element vector with the result.

V3.direction

V3.direction = function V3_direction(a,
b,
r)

Perform r = (a - b) / |a - b|.  The result is the normalized direction from a to b.

Parameters

athe first vector operand
bthe second vector operand
roptional vector to store the result in

Returns

If r is specified, returns r after performing the operation.  Otherwise, returns a new 3-element vector with the result.

V3.length

V3.length = function V3_length(a)

Perform r = |a|.

Parameters

athe vector operand

Returns

The length of the given vector.

V3.lengthSquard

Perform r = |a|*|a|.

Parameters

athe vector operand

Returns

The square of the length of the given vector.

V3.normalize

V3.normalize = function V3_normalize(a,
r)

Perform r = a / |a|.

Parameters

athe vector operand
roptional vector to store the result in

Returns

If r is specified, returns r after performing the operation.  Otherwise, returns a new 3-element vector with the result.

V3.scale

V3.scale = function V3_scale(a,
k,
r)

Perform r = a * k.

Parameters

athe vector operand
ka scalar value
roptional vector to store the result in

Returns

If r is specified, returns r after performing the operation.  Otherwise, returns a new 3-element vector with the result.

V3.dot

V3.dot = function V3_dot(a,
b)

Perform r = dot(a, b).

Parameters

athe first vector operand
bthe second vector operand

Returns

The dot product of a and b.

V3.cross

V3.cross = function V3_cross(a,
b,
r)

Perform r = a x b.

Parameters

athe first vector operand
bthe second vector operand
roptional vector to store the result in

Returns

If r is specified, returns r after performing the operation.  Otherwise, returns a new 3-element vector with the result.

M4x4

Methods for working with 4x4 matrices.  A matrix is represented by a 16-element array in column-major order.  Any valid JavaScript array type may be used, but if new matrices are created they are created using the configured MJS_FLOAT_ARRAY_TYPE.

Summary
Functions
M4x4.$Creates a new 4x4 matrix with the given values.
M4x4.cloneClone the given 4x4 matrix.
M4x4.topLeft3x3Return the top left 3x3 matrix from the given 4x4 matrix m.
M4x4.inverseOrthonormalComputes the inverse of the given matrix m, assuming that the matrix is orthonormal.
M4x4.inverseTo3x3Computes the inverse of the given matrix m, but calculates only the top left 3x3 values of the result.
M4x4.makeFrustumCreates a matrix for a projection frustum with the given parameters.
M4x4.makePerspectiveCreates a matrix for a perspective projection with the given parameters.
M4x4.makeOrthoCreates a matrix for an orthogonal frustum projection with the given parameters.
M4x4.makeOrthoCreates a matrix for a 2D orthogonal frustum projection with the given parameters.
M4x4.mulPerforms r = a * b.
M4x4.makeRotateCreates a transformation matrix for rotation by angle radians about the 3-element vector axis.
M4x4.rotateConcatenates a rotation of angle radians about the axis to the give matrix m.
M4x4.makeScale3Creates a transformation matrix for scaling by 3 scalar values, one for each of the x, y, and z directions.
M4x4.makeScale1Creates a transformation matrix for a uniform scale by a single scalar value.
M4x4.makeScaleCreates a transformation matrix for scaling each of the x, y, and z axes by the amount given in the corresponding element of the 3-element vector.
M4x4.scale3
M4x4.scale1
M4x4.scale1
M4x4.makeTranslate3
M4x4.makeTranslate1
M4x4.makeTranslate
M4x4.translate3
M4x4.translate1
M4x4.translate
M4x4.makeLookAt
M4x4.transpose

Functions

M4x4.$

M4x4.$ = function M4x4_$(m00,
m01,
m02,
m03,
m04,
m05,
m06,
m07,
m08,
m09,
m10,
m11,
m12,
m13,
m14,
m15)

Creates a new 4x4 matrix with the given values.

Parameters

m00..m15the 16 elements of the new matrix.

Returns

A new matrix filled with the given argument values.

M4x4.clone

M4x4.clone = function M4x4_clone(m)

Clone the given 4x4 matrix.

Parameters

mthe 4x4 matrix to clone

Returns

A new matrix with the same values as the passed-in one.

M4x4.topLeft3x3

M4x4.topLeft3x3 = function M4x4_topLeft3x3(m,
r)

Return the top left 3x3 matrix from the given 4x4 matrix m.

Parameters

mthe matrix
roptional 3x3 matrix to store the result in

Returns

If r is specified, returns r after performing the operation.  Otherwise, returns a new 3x3 matrix with the result.

M4x4.inverseOrthonormal

M4x4.inverseOrthonormal = function M4x4_inverseOrthonormal(m,
r)

Computes the inverse of the given matrix m, assuming that the matrix is orthonormal.

Parameters

mthe matrix
roptional 4x4 matrix to store the result in

Returns

If r is specified, returns r after performing the operation.  Otherwise, returns a new 4x4 matrix with the result.

M4x4.inverseTo3x3

M4x4.inverseTo3x3 = function M4x4_inverseTo3x3(m,
r)

Computes the inverse of the given matrix m, but calculates only the top left 3x3 values of the result.

Parameters

mthe matrix
roptional 3x3 matrix to store the result in

Returns

If r is specified, returns r after performing the operation.  Otherwise, returns a new 3x3 matrix with the result.

M4x4.makeFrustum

M4x4.makeFrustum = function M4x4_makeFrustum(left,
right,
bottom,
top,
znear,
zfar,
r)

Creates a matrix for a projection frustum with the given parameters.

Parameters

leftthe left coordinate of the frustum right- the right coordinate of the frustum
bottomthe bottom coordinate of the frustum
topthe top coordinate of the frustum
znearthe near z distance of the frustum
zfarthe far z distance of the frustum
roptional 4x4 matrix to store the result in

Returns

If r is specified, returns r after creating the projection matrix.  Otherwise, returns a new 4x4 matrix.

M4x4.makePerspective

M4x4.makePerspective = function M4x4_makePerspective (fovy,
aspect,
znear,
zfar,
r)

Creates a matrix for a perspective projection with the given parameters.

Parameters

fovyfield of view in the y axis, in degrees
aspectaspect ratio
znearthe near z distance of the projection
zfarthe far z distance of the projection
roptional 4x4 matrix to store the result in

Returns

If r is specified, returns r after creating the projection matrix.  Otherwise, returns a new 4x4 matrix.

M4x4.makeOrtho

M4x4.makeOrtho = function M4x4_makeOrtho (left,
right,
bottom,
top,
znear,
zfar,
r)

Creates a matrix for an orthogonal frustum projection with the given parameters.

Parameters

leftthe left coordinate of the frustum right- the right coordinate of the frustum
bottomthe bottom coordinate of the frustum
topthe top coordinate of the frustum
znearthe near z distance of the frustum
zfarthe far z distance of the frustum
roptional 4x4 matrix to store the result in

Returns

If r is specified, returns r after creating the projection matrix.  Otherwise, returns a new 4x4 matrix.

M4x4.makeOrtho

M4x4.makeOrtho2D = function M4x4_makeOrtho2D (left,
right,
bottom,
top,
r)

Creates a matrix for a 2D orthogonal frustum projection with the given parameters. znear and zfar are assumed to be -1 and 1, respectively.

Parameters

leftthe left coordinate of the frustum right- the right coordinate of the frustum
bottomthe bottom coordinate of the frustum
topthe top coordinate of the frustum
roptional 4x4 matrix to store the result in

Returns

If r is specified, returns r after creating the projection matrix.  Otherwise, returns a new 4x4 matrix.

M4x4.mul

M4x4.mul = function M4x4_mul(a,
b,
r)

Performs r = a * b.

Parameters

athe first matrix operand
bthe second matrix operand
roptional 4x4 matrix to store the result in

Returns

If r is specified, returns r after performing the operation.  Otherwise, returns a new 4x4 matrix with the result.

M4x4.makeRotate

M4x4.makeRotate = function M4x4_makeRotate(angle,
axis,
r)

Creates a transformation matrix for rotation by angle radians about the 3-element vector axis.

Parameters

anglethe angle of rotation, in radians
axisthe axis around which the rotation is performed, a 3-element vector
roptional 4x4 matrix to store the result in

Returns

If r is specified, returns r after creating the matrix.  Otherwise, returns a new 4x4 matrix with the result.

M4x4.rotate

M4x4.rotate = function M4x4_rotate(angle,
axis,
m,
r)

Concatenates a rotation of angle radians about the axis to the give matrix m.

Parameters

anglethe angle of rotation, in radians
axisthe axis around which the rotation is performed, a 3-element vector
mthe matrix to concatenate the rotation to
roptional 4x4 matrix to store the result in

Returns

If r is specified, returns r after performing the operation.  Otherwise, returns a new 4x4 matrix with the result.

M4x4.makeScale3

M4x4.makeScale3 = function M4x4_makeScale3(x,
y,
z,
r)

Creates a transformation matrix for scaling by 3 scalar values, one for each of the x, y, and z directions.

Parameters

xthe scale for the x axis
ythe scale for the y axis
zthe scale for the z axis
roptional 4x4 matrix to store the result in

Returns

If r is specified, returns r after creating the matrix.  Otherwise, returns a new 4x4 matrix with the result.

M4x4.makeScale1

M4x4.makeScale1 = function M4x4_makeScale1(k,
r)

Creates a transformation matrix for a uniform scale by a single scalar value.

Parameters

kthe scale factor
roptional 4x4 matrix to store the result in

Returns

If r is specified, returns r after creating the matrix.  Otherwise, returns a new 4x4 matrix with the result.

M4x4.makeScale

M4x4.makeScale = function M4x4_makeScale(v,
r)

Creates a transformation matrix for scaling each of the x, y, and z axes by the amount given in the corresponding element of the 3-element vector.

Parameters

vthe 3-element vector containing the scale factors
roptional 4x4 matrix to store the result in

Returns

If r is specified, returns r after creating the matrix.  Otherwise, returns a new 4x4 matrix with the result.

M4x4.scale3

M4x4.scale3 = function M4x4_scale3(x,
y,
z,
m,
r)

M4x4.scale1

M4x4.scale1 = function M4x4_scale1(k,
m,
r)

M4x4.scale1

M4x4.makeTranslate3

M4x4.makeTranslate3 = function M4x4_makeTranslate3(x,
y,
z,
r)

M4x4.makeTranslate1

M4x4.makeTranslate1 = function M4x4_makeTranslate1 (k,
r)

M4x4.makeTranslate

M4x4.makeTranslate = function M4x4_makeTranslate (v,
r)

M4x4.translate3

M4x4.translate3 = function M4x4_translate3 (x,
y,
z,
m,
r)

M4x4.translate1

M4x4.translate1 = function M4x4_translate1 (k,
m,
r)

M4x4.translate

M4x4.translate = function M4x4_translate (v,
m,
r)

M4x4.makeLookAt

M4x4.makeLookAt = function M4x4_makeLookAt (eye,
center,
up,
r)

M4x4.transpose

M4x4.transpose = function M4x4_transpose (m,
r)
V3.$ = function V3_$(x,
y,
z)
Creates a new 3-element vector with the given values.
V3.clone = function V3_clone(a)
Clone the given 3-element vector.
V3.add = function V3_add(a,
b,
r)
Perform r = a + b.
V3.sub = function V3_sub(a,
b,
r)
Perform r = a - b.
V3.neg = function V3_neg(a,
r)
Perform r = - a.
V3.direction = function V3_direction(a,
b,
r)
Perform r = (a - b) / |a - b|.
V3.length = function V3_length(a)
Perform r = |a|.
V3.normalize = function V3_normalize(a,
r)
Perform r = a / |a|.
V3.scale = function V3_scale(a,
k,
r)
Perform r = a * k.
V3.dot = function V3_dot(a,
b)
Perform r = dot(a, b).
V3.cross = function V3_cross(a,
b,
r)
Perform r = a x b.
M4x4.$ = function M4x4_$(m00,
m01,
m02,
m03,
m04,
m05,
m06,
m07,
m08,
m09,
m10,
m11,
m12,
m13,
m14,
m15)
Creates a new 4x4 matrix with the given values.
M4x4.clone = function M4x4_clone(m)
Clone the given 4x4 matrix.
M4x4.topLeft3x3 = function M4x4_topLeft3x3(m,
r)
Return the top left 3x3 matrix from the given 4x4 matrix m.
M4x4.inverseOrthonormal = function M4x4_inverseOrthonormal(m,
r)
Computes the inverse of the given matrix m, assuming that the matrix is orthonormal.
M4x4.inverseTo3x3 = function M4x4_inverseTo3x3(m,
r)
Computes the inverse of the given matrix m, but calculates only the top left 3x3 values of the result.
M4x4.makeFrustum = function M4x4_makeFrustum(left,
right,
bottom,
top,
znear,
zfar,
r)
Creates a matrix for a projection frustum with the given parameters.
M4x4.makePerspective = function M4x4_makePerspective (fovy,
aspect,
znear,
zfar,
r)
Creates a matrix for a perspective projection with the given parameters.
M4x4.makeOrtho = function M4x4_makeOrtho (left,
right,
bottom,
top,
znear,
zfar,
r)
Creates a matrix for an orthogonal frustum projection with the given parameters.
M4x4.mul = function M4x4_mul(a,
b,
r)
Performs r = a * b.
M4x4.makeRotate = function M4x4_makeRotate(angle,
axis,
r)
Creates a transformation matrix for rotation by angle radians about the 3-element vector axis.
M4x4.rotate = function M4x4_rotate(angle,
axis,
m,
r)
Concatenates a rotation of angle radians about the axis to the give matrix m.
M4x4.makeScale3 = function M4x4_makeScale3(x,
y,
z,
r)
Creates a transformation matrix for scaling by 3 scalar values, one for each of the x, y, and z directions.
M4x4.makeScale1 = function M4x4_makeScale1(k,
r)
Creates a transformation matrix for a uniform scale by a single scalar value.
M4x4.makeScale = function M4x4_makeScale(v,
r)
Creates a transformation matrix for scaling each of the x, y, and z axes by the amount given in the corresponding element of the 3-element vector.
M4x4.scale3 = function M4x4_scale3(x,
y,
z,
m,
r)
M4x4.scale1 = function M4x4_scale1(k,
m,
r)
M4x4.makeTranslate3 = function M4x4_makeTranslate3(x,
y,
z,
r)
M4x4.makeTranslate1 = function M4x4_makeTranslate1 (k,
r)
M4x4.makeTranslate = function M4x4_makeTranslate (v,
r)
M4x4.translate3 = function M4x4_translate3 (x,
y,
z,
m,
r)
M4x4.translate1 = function M4x4_translate1 (k,
m,
r)
M4x4.translate = function M4x4_translate (v,
m,
r)
M4x4.makeLookAt = function M4x4_makeLookAt (eye,
center,
up,
r)
M4x4.transpose = function M4x4_transpose (m,
r)
Close