Vector and Matrix math utilities for JavaScript, optimized for WebGL.
| mjs | Vector and Matrix math utilities for JavaScript, optimized for WebGL. |
| Constants | |
| MJS_VERSION | 0xaabbcc. |
| MJS_DO_ASSERT | Enables or disables runtime assertions. |
| MJS_FLOAT_ARRAY_TYPE | The base float array type. |
| V3 | Methods for working with 3-element vectors. |
| Functions | |
| V3.$ | Creates a new 3-element vector with the given values. |
| V3. | Clone the given 3-element vector. |
| V3.add | Perform r = a + b. |
| V3.sub | Perform r = a - b. |
| V3.neg | Perform r = - a. |
| V3. | Perform r = (a - b) / |a - b|. |
| V3. | Perform r = |a|. |
| V3. | Perform r = |a|*|a|. |
| V3. | Perform r = a / |a|. |
| V3. | Perform r = a * k. |
| V3.dot | Perform r = dot(a, b). |
| V3. | Perform r = a x b. |
| M4x4 | Methods for working with 4x4 matrices. |
| Functions | |
| M4x4.$ | Creates a new 4x4 matrix with the given values. |
| M4x4. | Clone the given 4x4 matrix. |
| M4x4. | Return the top left 3x3 matrix from the given 4x4 matrix m. |
| M4x4. | Computes the inverse of the given matrix m, assuming that the matrix is orthonormal. |
| M4x4. | Computes the inverse of the given matrix m, but calculates only the top left 3x3 values of the result. |
| M4x4. | Creates a matrix for a projection frustum with the given parameters. |
| M4x4. | Creates a matrix for a perspective projection with the given parameters. |
| M4x4. | Creates a matrix for an orthogonal frustum projection with the given parameters. |
| M4x4. | Creates a matrix for a 2D orthogonal frustum projection with the given parameters. |
| M4x4.mul | Performs r = a * b. |
| M4x4. | Creates a transformation matrix for rotation by angle radians about the 3-element vector axis. |
| M4x4. | Concatenates a rotation of angle radians about the axis to the give matrix m. |
| M4x4. | Creates a transformation matrix for scaling by 3 scalar values, one for each of the x, y, and z directions. |
| M4x4. | Creates a transformation matrix for a uniform scale by a single scalar value. |
| M4x4. | 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. | |
| M4x4. | |
| M4x4. | |
| M4x4. | |
| M4x4. | |
| M4x4. | |
| M4x4. | |
| M4x4. | |
| M4x4. | |
| M4x4. | |
| M4x4. |
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.
| Functions | |
| V3.$ | Creates a new 3-element vector with the given values. |
| V3. | Clone the given 3-element vector. |
| V3.add | Perform r = a + b. |
| V3.sub | Perform r = a - b. |
| V3.neg | Perform r = - a. |
| V3. | Perform r = (a - b) / |a - b|. |
| V3. | Perform r = |a|. |
| V3. | Perform r = |a|*|a|. |
| V3. | Perform r = a / |a|. |
| V3. | Perform r = a * k. |
| V3.dot | Perform r = dot(a, b). |
| V3. | Perform r = a x b. |
V3.direction = function V3_direction( a, b, r )
Perform r = (a - b) / |a - b|. The result is the normalized direction from a to b.
| a | the first vector operand |
| b | the second vector operand |
| r | optional vector to store the result in |
If r is specified, returns r after performing the operation. Otherwise, returns a new 3-element vector with the result.
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.
| Functions | |
| M4x4.$ | Creates a new 4x4 matrix with the given values. |
| M4x4. | Clone the given 4x4 matrix. |
| M4x4. | Return the top left 3x3 matrix from the given 4x4 matrix m. |
| M4x4. | Computes the inverse of the given matrix m, assuming that the matrix is orthonormal. |
| M4x4. | Computes the inverse of the given matrix m, but calculates only the top left 3x3 values of the result. |
| M4x4. | Creates a matrix for a projection frustum with the given parameters. |
| M4x4. | Creates a matrix for a perspective projection with the given parameters. |
| M4x4. | Creates a matrix for an orthogonal frustum projection with the given parameters. |
| M4x4. | Creates a matrix for a 2D orthogonal frustum projection with the given parameters. |
| M4x4.mul | Performs r = a * b. |
| M4x4. | Creates a transformation matrix for rotation by angle radians about the 3-element vector axis. |
| M4x4. | Concatenates a rotation of angle radians about the axis to the give matrix m. |
| M4x4. | Creates a transformation matrix for scaling by 3 scalar values, one for each of the x, y, and z directions. |
| M4x4. | Creates a transformation matrix for a uniform scale by a single scalar value. |
| M4x4. | 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. | |
| M4x4. | |
| M4x4. | |
| M4x4. | |
| M4x4. | |
| M4x4. | |
| M4x4. | |
| M4x4. | |
| M4x4. | |
| M4x4. | |
| M4x4. |
M4x4.topLeft3x3 = function M4x4_topLeft3x3( m, r )
Return the top left 3x3 matrix from the given 4x4 matrix m.
| m | the matrix |
| r | optional 3x3 matrix to store the result in |
If r is specified, returns r after performing the operation. Otherwise, returns a new 3x3 matrix with the result.
M4x4.inverseOrthonormal = function M4x4_inverseOrthonormal( m, r )
Computes the inverse of the given matrix m, assuming that the matrix is orthonormal.
| m | the matrix |
| r | optional 4x4 matrix to store the result in |
If r is specified, returns r after performing the operation. Otherwise, returns a new 4x4 matrix with the result.
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.
| m | the matrix |
| r | optional 3x3 matrix to store the result in |
If r is specified, returns r after performing the operation. Otherwise, returns a new 3x3 matrix with 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.
| left | the left coordinate of the frustum right- the right coordinate of the frustum |
| bottom | the bottom coordinate of the frustum |
| top | the top coordinate of the frustum |
| znear | the near z distance of the frustum |
| zfar | the far z distance of the frustum |
| r | optional 4x4 matrix to store the result in |
If r is specified, returns r after creating the projection matrix. Otherwise, returns a new 4x4 matrix.
M4x4.makePerspective = function M4x4_makePerspective ( fovy, aspect, znear, zfar, r )
Creates a matrix for a perspective projection with the given parameters.
| fovy | field of view in the y axis, in degrees |
| aspect | aspect ratio |
| znear | the near z distance of the projection |
| zfar | the far z distance of the projection |
| r | optional 4x4 matrix to store the result in |
If r is specified, returns r after creating the projection matrix. Otherwise, returns a new 4x4 matrix.
M4x4.makeOrtho = function M4x4_makeOrtho ( left, right, bottom, top, znear, zfar, r )
Creates a matrix for an orthogonal frustum projection with the given parameters.
| left | the left coordinate of the frustum right- the right coordinate of the frustum |
| bottom | the bottom coordinate of the frustum |
| top | the top coordinate of the frustum |
| znear | the near z distance of the frustum |
| zfar | the far z distance of the frustum |
| r | optional 4x4 matrix to store the result in |
If r is specified, returns r after creating the projection matrix. Otherwise, returns a new 4x4 matrix.
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.
| left | the left coordinate of the frustum right- the right coordinate of the frustum |
| bottom | the bottom coordinate of the frustum |
| top | the top coordinate of the frustum |
| r | optional 4x4 matrix to store the result in |
If r is specified, returns r after creating the projection matrix. Otherwise, returns a new 4x4 matrix.
M4x4.makeRotate = function M4x4_makeRotate( angle, axis, r )
Creates a transformation matrix for rotation by angle radians about the 3-element vector axis.
| angle | the angle of rotation, in radians |
| axis | the axis around which the rotation is performed, a 3-element vector |
| r | optional 4x4 matrix to store the result in |
If r is specified, returns r after creating the matrix. Otherwise, returns a new 4x4 matrix with the result.
M4x4.rotate = function M4x4_rotate( angle, axis, m, r )
Concatenates a rotation of angle radians about the axis to the give matrix m.
| angle | the angle of rotation, in radians |
| axis | the axis around which the rotation is performed, a 3-element vector |
| m | the matrix to concatenate the rotation to |
| r | optional 4x4 matrix to store the result in |
If r is specified, returns r after performing the operation. Otherwise, returns a new 4x4 matrix with the result.
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.
| x | the scale for the x axis |
| y | the scale for the y axis |
| z | the scale for the z axis |
| r | optional 4x4 matrix to store the result in |
If r is specified, returns r after creating the matrix. Otherwise, returns a new 4x4 matrix with the result.
M4x4.makeScale1 = function M4x4_makeScale1( k, r )
Creates a transformation matrix for a uniform scale by a single scalar value.
| k | the scale factor |
| r | optional 4x4 matrix to store the result in |
If r is specified, returns r after creating the matrix. Otherwise, returns a new 4x4 matrix with the result.
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.
| v | the 3-element vector containing the scale factors |
| r | optional 4x4 matrix to store the result in |
If r is specified, returns r after creating the matrix. Otherwise, returns a new 4x4 matrix with the result.
Creates a new 3-element vector with the given values.
V3.$ = function V3_$( x, y, z )
Clone the given 3-element vector.
V3.clone = function V3_clone( a )
Perform r = a + b.
V3.add = function V3_add( a, b, r )
Perform r = a - b.
V3.sub = function V3_sub( a, b, r )
Perform r = - a.
V3.neg = function V3_neg( a, r )
Perform r = (a - b) / |a - b|.
V3.direction = function V3_direction( a, b, r )
Perform r = |a|.
V3.length = function V3_length( a )
Perform r = a / |a|.
V3.normalize = function V3_normalize( a, r )
Perform r = a * k.
V3.scale = function V3_scale( a, k, r )
Perform r = dot(a, b).
V3.dot = function V3_dot( a, b )
Perform r = a x b.
V3.cross = function V3_cross( a, b, r )
Creates a new 4x4 matrix with the given values.
M4x4.$ = function M4x4_$( m00, m01, m02, m03, m04, m05, m06, m07, m08, m09, m10, m11, m12, m13, m14, m15 )
Clone the given 4x4 matrix.
M4x4.clone = function M4x4_clone( m )
Return the top left 3x3 matrix from the given 4x4 matrix m.
M4x4.topLeft3x3 = function M4x4_topLeft3x3( m, r )
Computes the inverse of the given matrix m, assuming that the matrix is orthonormal.
M4x4.inverseOrthonormal = function M4x4_inverseOrthonormal( m, r )
Computes the inverse of the given matrix m, but calculates only the top left 3x3 values of the result.
M4x4.inverseTo3x3 = function M4x4_inverseTo3x3( m, r )
Creates a matrix for a projection frustum with the given parameters.
M4x4.makeFrustum = function M4x4_makeFrustum( left, right, bottom, top, znear, zfar, r )
Creates a matrix for a perspective projection with the given parameters.
M4x4.makePerspective = function M4x4_makePerspective ( fovy, aspect, znear, zfar, r )
Creates a matrix for an orthogonal frustum projection with the given parameters.
M4x4.makeOrtho = function M4x4_makeOrtho ( left, right, bottom, top, znear, zfar, r )
Performs r = a * b.
M4x4.mul = function M4x4_mul( a, b, r )
Creates a transformation matrix for rotation by angle radians about the 3-element vector axis.
M4x4.makeRotate = function M4x4_makeRotate( angle, axis, r )
Concatenates a rotation of angle radians about the axis to the give matrix m.
M4x4.rotate = function M4x4_rotate( angle, axis, m, r )
Creates a transformation matrix for scaling by 3 scalar values, one for each of the x, y, and z directions.
M4x4.makeScale3 = function M4x4_makeScale3( x, y, z, r )
Creates a transformation matrix for a uniform scale by a single scalar value.
M4x4.makeScale1 = function M4x4_makeScale1( k, 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.makeScale = function M4x4_makeScale( v, r )
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 )