glMatrix

repository·master·Indexed 27 days ago

https://github.com/toji/gl-matrix

A 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).

Tokens
3.5K
Snippets
0
Records
38
Agent score
91%

What's inside gl-matrix

  1. Install and use glMatrix for high-performance math

    master
    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.
  2. Optimize performance using setMatrixArrayType

    master
    In modern web browsers, you can potentially increase performance by allowing glMatrix to use normal arrays instead of the default Float32Array. You can achieve this by calling glMatrix.setMatrixArrayType(Array).
  3. Understand the mat2d structure

    master

    A mat2d is 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.

  4. Compare 2x2 matrices

    master

    Check for equality between two 2x2 matrices:

    • exactEquals(a, b): Returns true if the matrices have exactly the same elements in the same position (using ===).
    • equals(a, b): Returns true if the matrices are approximately equal, accounting for floating-point precision using glMatrix.EPSILON.
  5. Convert other types to mat3

    master

    You can derive a mat3 from 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.
  6. Create and initialize mat2d matrices

    master

    Use these functions to instantiate new 2x3 matrices or reset existing ones.

    • create(): Returns a new identity mat2d.
    • clone(a): Returns a new mat2d containing the values of matrix a.
    • copy(out, a): Copies values from source matrix a into receiving matrix out.
    • identity(out): Sets the receiving matrix out to the identity matrix.
    • fromValues(a, b, c, d, tx, ty): Creates a new mat2d with the specified components.
    • set(out, a, b, c, d, tx, ty): Sets the components of the receiving matrix out to the specified values.
  7. Compare vec2 vectors

    master

    Check for equality between vectors:

    • exactEquals(a, b): Returns true if elements are exactly equal (===).
    • equals(a, b): Returns true if elements are approximately equal, accounting for floating-point epsilon.
    • str(a): Returns a string representation: vec2(x, y).
  8. Iterate over an array of vec2s with forEach

    master

    The forEach function allows performing an operation over an array of vec2 elements. 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 each vec2 (defaults to 2 if 0).
    • offset: Number of elements to skip at the beginning (defaults to 0).
    • count: Number of vec2s to iterate over (0 for entire array).
    • fn: Function to call for each vector. Signature: fn(out, a, arg).
    • arg: Additional argument passed to fn.
  9. Calculate matrix properties (Inverse, Determinant, Adjoint)

    master

    Use these functions for advanced matrix calculations:

    • invert(out, a): Inverts the matrix. Returns null if 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. Returns null if not invertible.
  10. Import glMatrix modules

    master

    The gl-matrix package 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.
  11. Calculate 2x2 matrix properties

    master

    Extract mathematical properties from a 2x2 matrix:

    • determinant(a): Returns the determinant of matrix a.
    • invert(out, a): Inverts matrix a and stores the result in out. Returns null if the matrix is not invertible.
    • adjoint(out, a): Calculates the adjugate (adjoint) of matrix a and stores it in out.
    • frob(a): Returns the Frobenius norm of matrix a.
    • LDU(L, D, U, a): Factorizes matrix a into Lower triangular (L), Diagonal (D), and Upper triangular (U) matrices.