glMatrix
repository·master·Indexed 27 days ago
https://github.com/toji/gl-matrixA high-performance JavaScript library for vector and matrix mathematics, optimized for WebGL and computationally intensive tasks like physics simulations. Version 3.4.4 provides modules for 2D/3D vectors (vec2, vec3, vec4), matrices (mat2, mat2d, mat3, mat4), and quaternions (quat, quat2).
What's inside gl-matrix
- glMatrix is a high-performance library designed for vector and matrix operations in JavaScript, specifically optimized for real-time 3D graphics (WebGL) and physics simulations. It achieves high performance by hand-tuning functions and encouraging efficient usage patterns that leverage the browser's JavaScript engine.
Optimize performance using setMatrixArrayType
masterIn modern web browsers, you can potentially increase performance by allowing glMatrix to use normal arrays instead of the defaultFloat32Array. You can achieve this by callingglMatrix.setMatrixArrayType(Array).Understand the mat2d structure
masterA
mat2dis a 2x3 matrix used for 2D transformations. It is represented as a 6-element array containing:[a, b, c, d, tx, ty].This is a compact representation of a 3x3 matrix:
[a, b, 0, c, d, 0, tx, ty, 1]The last column (the third column) is ignored to optimize performance.
Compare 2x2 matrices
masterCheck for equality between two 2x2 matrices:
exactEquals(a, b): Returnstrueif the matrices have exactly the same elements in the same position (using===).equals(a, b): Returnstrueif the matrices are approximately equal, accounting for floating-point precision usingglMatrix.EPSILON.
Generate 2D projection matrices
masterUseprojection(out, width, height)to generate a 2D projection matrix suitable for a WebGL context with the specifiedwidthandheight.Convert other types to mat3
masterYou can derive a
mat3from other matrix types using these functions:fromMat4(out, a): Copies the upper-left 3x3 values from a 4x4 matrix.fromMat2d(out, a): Copies values from a 2D matrix.fromQuat(out, q): Calculates a 3x3 matrix from a quaternion.fromTranslation(out, v): Creates a matrix from a 2D translation vector.fromRotation(out, rad): Creates a matrix from a rotation angle (radians).fromScaling(out, v): Creates a matrix from a 2D scaling vector.
Create and initialize mat2d matrices
masterUse these functions to instantiate new 2x3 matrices or reset existing ones.
create(): Returns a new identitymat2d.clone(a): Returns a newmat2dcontaining the values of matrixa.copy(out, a): Copies values from source matrixainto receiving matrixout.identity(out): Sets the receiving matrixoutto the identity matrix.fromValues(a, b, c, d, tx, ty): Creates a newmat2dwith the specified components.set(out, a, b, c, d, tx, ty): Sets the components of the receiving matrixoutto the specified values.
Compare vec2 vectors
masterCheck for equality between vectors:
exactEquals(a, b): Returnstrueif elements are exactly equal (===).equals(a, b): Returnstrueif elements are approximately equal, accounting for floating-point epsilon.str(a): Returns a string representation:vec2(x, y).
Iterate over an array of vec2s with forEach
masterThe
forEachfunction allows performing an operation over an array ofvec2elements. It handles both tightly packed arrays and arrays with a stride.Parameters:
a: The array of vectors to iterate over.stride: Number of elements between the start of eachvec2(defaults to 2 if 0).offset: Number of elements to skip at the beginning (defaults to 0).count: Number ofvec2s to iterate over (0 for entire array).fn: Function to call for each vector. Signature:fn(out, a, arg).arg: Additional argument passed tofn.
Calculate matrix properties (Inverse, Determinant, Adjoint)
masterUse these functions for advanced matrix calculations:
invert(out, a): Inverts the matrix. Returnsnullif the matrix is not invertible.determinant(a): Returns the determinant of the matrix.adjoint(out, a): Calculates the adjugate (adjoint) of the matrix.normalFromMat4(out, a): Calculates a 3x3 normal matrix (transpose inverse) from a 4x4 matrix. Returnsnullif not invertible.
Import glMatrix modules
masterThe
gl-matrixpackage provides high-performance mathematical operations for graphics programming. You can import specific modules for 2D/3D vectors, matrices, and quaternions. The main entrypoint exports the following modules:glMatrix: Common utilities and constants.mat2,mat2d,mat3,mat4: 2x2, 2D affine, 3x3, and 4x4 matrix operations.quat,quat2: Quaternion operations.vec2,vec3,vec4: 2D, 3D, and 4D vector operations.
Calculate 2x2 matrix properties
masterExtract mathematical properties from a 2x2 matrix:
determinant(a): Returns the determinant of matrixa.invert(out, a): Inverts matrixaand stores the result inout. Returnsnullif the matrix is not invertible.adjoint(out, a): Calculates the adjugate (adjoint) of matrixaand stores it inout.frob(a): Returns the Frobenius norm of matrixa.LDU(L, D, U, a): Factorizes matrixainto Lower triangular (L), Diagonal (D), and Upper triangular (U) matrices.