maath

repository·main·Indexed 21 days ago

https://github.com/pmndrs/maath

A mathematical utility library providing high-performance random number generators, geometric distributions, and noise functions for graphics and simulations. It includes specialized tools for TypedArray buffer manipulation, physics-based damping for Three.js objects, standard easing functions, and geometry utilities such as RoundedPlaneGeometry and UV mapping (cylindrical, spherical, and box).

Tokens
10.5K
Snippets
63
Records
64
Agent score
76%

What's inside maath

  1. Animate angles with `dampAngle`

    main
    The dampAngle function is a wrapper around damp that uses deltaAngle to ensure the animation takes the shortest path between two angles, preventing
  2. Calculate the center of a buffer

    main

    Computes the average position (centroid) of all points in the buffer.

    • myBuffer: The TypedArray to process.
    • stride: The number of elements per point (2 or 3).

    Returns a V2 or V3 representing the center.

    // Example: Get the center of 3D points
    const centerPoint = center(myBuffer, 3);
  3. Calculate dot and cross products

    main

    Use dot(a, b) to calculate the dot product of two vectors, returning a scalar. Use cross(a, b) to calculate the cross product of two vectors, returning a new V3 perpendicular to both inputs.

    import { dot, cross } from 'maath/vector3';
    
    const a = [1, 0, 0];
    const b = [0, 1, 0];
    
    dot(a, b);  // 0
    cross(a, b); // [0, 0, 1]
  4. Compare vectors with epsilon

    main

    The vectorEquals function checks if two 3D vectors are approximately equal, accounting for floating-point errors using an epsilon value.

    // Returns true if vectors are within Number.EPSILON of each other
    vectorEquals(vectorA, vectorB);
    
    // Custom epsilon
    vectorEquals(vectorA, vectorB, 0.0001);
  5. Generate random points inside a 3D sphere with `inSphere`

    main

    The inSphere function populates a TypedArray with random 3D coordinates located within the volume of a sphere. You can specify the radius and the center (as an array of 3 numbers).

    import { inSphere } from '@maath/core/random';
    
    const buffer = new Float32Array(30);
    const sphere = { radius: 2, center: [1, 1, 1] };
    
    inSphere(buffer, sphere);
  6. Get the minor of a Matrix4

    main

    The getMinor function calculates the determinant of a submatrix (minor) derived from a Matrix4 by removing a specific row r and column c.

    Note: The function uses 1-based indexing for the row r and column c parameters (e.g., to remove the first row, pass 1).

    import { Matrix4 } from "three";
    import { getMinor } from "maath/matrix";
    
    const m = new Matrix4();
    // ... populate matrix
    
    // Get the determinant of the 3x3 matrix remaining after 
    // removing row 1 and column 1
    const minorDet = getMinor(m, 1, 1);
  7. Rotate points in a buffer

    main

    Rotates all points in a buffer using a quaternion. Rotation is performed around a specified center.

    • buffer: The TypedArray to mutate.
    • rotation: An object containing:
      • q: A three Quaternion representing the rotation.
      • center (optional): An array [x, y, z] representing the pivot point. Defaults to [0, 0, 0].
    import { Quaternion } from 'three';
    
    // Example: Rotate buffer around the origin
    rotate(myBuffer, { q: new Quaternion().setFromAxisAngle(new Vector3(0, 1, 0), Math.PI / 2) });
  8. Calculate vector dot products and lengths

    main

    Use these functions for geometric calculations:

    • dot(a, b): Returns the dot product of two vectors.
    • length(a): Returns the magnitude (length) of the vector. This involves a square root operation.
    • lengthSqr(a): Returns the squared length of the vector. Performance Tip: Use lengthSqr instead of length when comparing the relative lengths of vectors to avoid the computational cost of Math.sqrt.
    import { dot, length, lengthSqr } from 'maath/vector2';
    
    const a = [3, 4];
    const b = [1, 2];
    
    const d = dot(a, b);        // 11
    const l = length(a);        // 5
    const l2 = lengthSqr(a);   // 25
  9. Standard Easing Functions

    main

    The easing module provides several standard easing functions used to control the rate of change in animations. These functions typically take a value t (usually between 0 and 1) and return a transformed value.

    Available easing types:

    • linear: No change in rate.
    • exp: Exponential easing.
    • rsqw: A custom easing function.
    • sine: Sine-based easing with in, out, and inOut variants.
    • cubic: Cubic-based easing with in, out, and inOut variants.
    • quint: Quintic-based easing with in, out, and inOut variants.
    • circ: Circular-based easing with in, out, and inOut variants.
    • quart: Quartic-based easing with in, out, and inOut variants.
    • expo: Exponential-based easing with in, out, and inOut variants.
    import { linear, sine, cubic, quint, circ, quart, expo, exp, rsqw } from '@maath/easing';
    
    // Example usage of a sine in-out easing
    const easedValue = sine.inOut(0.5); // 0.5
  10. Interpolation and Easing

    main

    A collection of interpolation utilities:

    • lerp(v0, v1, t): Linear interpolation between v0 and v1 by factor t.
    • inverseLerp(v0, v1, t): Finds the linear parameter t that produces t within the range [v0, v1].
    • fade(t): An ease-in-out function (smoothstep-like) that goes to -Infinite before 0 and Infinite after 1.
    lerp(0, 10, 0.5); // 5
    inverseLerp(0, 10, 5); // 0.5
    fade(0.5);
  11. Sum two Matrix3 instances

    main

    Use matrixSum3 to perform element-wise addition of two Matrix3 objects. It returns a new Matrix3 instance representing the sum.

    import { Matrix3 } from "three";
    import { matrixSum3 } from "maath/matrix";
    
    const m1 = new Matrix3().set(1, 0, 0, 0, 1, 0, 0, 0, 1);
    const m2 = new Matrix3().set(2, 2, 2, 2, 2, 2, 2, 2, 2);
    
    const result = matrixSum3(m1, m2);
    // result is a Matrix3 with elements:
    // | 3 2 2 |
    // | 2 3 2 |
    // | 2 2 3 |
  12. Check if a point is inside a triangle

    main

    Use isPointInTriangle to determine if a 2D point is contained within the boundaries of a given triangle. The function uses a matrix determinant approach to perform the check.

    import { isPointInTriangle } from '@maath/geometry';
    
    const triangle: Triangle = [[0, 0], [1, 0], [0, 1]];
    const point = [0.2, 0.2];
    
    const isInside = isPointInTriangle(point, triangle);