sm.util

Offers various math-related functions.

Constants:

Functions:

  • axesToQuat
  • bezier2
  • bezier3
  • clamp
  • clampUtf8String
  • easing
  • easing
  • geometricMedian
  • hash
  • lerp
  • positiveModulo
  • smootherstep
  • smoothstep
  • utf8StringLength

  • sm.util.easingFunctionIds table

    Easing function ids can be passed to the easing function instead of the easing function's name.

    The easings are:


    sm.util.axesToQuat(xAxis, yAxis)

    Constructs a quaternion from a X and Z axis

    Parameters:

    TypeNameDescription
    Vec3xAxisThe X axis.
    Vec3yAxisThe Z axis.

    Returns:

    TypeDescription
    Quatrotation The quaternion.

    sm.util.bezier2(c0, c1, c2, t)

    Quadratic Bezier interpolation. One dimensional bezier curve.

    Parameters:

    TypeNameDescription
    numberc0The start value.
    numberc1The control point.
    numberc2The end value.
    numbertThe interpolation step.

    Returns:

    TypeDescription
    numberThe interpolated value between two values.

    sm.util.bezier3(c0, c1, c2, c3, t)

    Cubic Bezier interpolation. One dimensional bezier curve.

    Parameters:

    TypeNameDescription
    numberc0The start value.
    numberc1The first control point.
    numberc2The second control point.
    numberc3The end value.
    numbertThe interpolation step.

    Returns:

    TypeDescription
    numberThe interpolated value between two values.

    sm.util.clamp(value, min, max)

    Restricts a value to a given range.

    Parameters:

    TypeNameDescription
    numbervalueThe value.
    numberminThe lower limit.
    numbermaxThe upper limit.

    Returns:

    TypeDescription
    numberThe clamped value.

    sm.util.clampUtf8String(string, maxLength)

    Clamps a UTF-8 string to a maximum number of characters without splitting multi-byte sequences.

    Parameters:

    TypeNameDescription
    stringstringThe input string.
    integermaxLengthThe maximum number of UTF-8 characters.

    Returns:

    TypeDescription
    stringThe clamped string.

    sm.util.easing(easingFunctionId, p)

    Applies an easing function to a given input.

    Parameters:

    TypeNameDescription
    integereasingFunctionIdThe easing function to use. (See sm.util.easingFunctionIds)
    numberpThe easing function input.

    Returns:

    TypeDescription
    numberThe output.

    sm.util.easing(easing, p)

    Applies an easing function to a given input.

    Easing function names:

    linear
    easeInQuad
    easeOutQuad
    easeInOutQuad
    easeInCubic
    easeOutCubic
    easeInOutCubic
    easeInQuart
    easeOutQuart
    easeInOutQuart
    easeInQuint
    easeOutQuint
    easeInOutQuint
    easeInSine
    easeOutSine
    easeInOutSine
    easeInCirc
    easeOutCirc
    easeInOutCirc
    easeInExpo
    easeOutExpo
    easeInOutExpo
    easeInElastic
    easeOutElastic
    easeInOutElastic
    easeInBack
    easeOutBack
    easeInOutBack
    easeInBounce
    easeOutBounce
    easeInOutBounce

    Parameters:

    TypeNameDescription
    stringeasingThe easing function name.
    numberpThe easing function input.

    Returns:

    TypeDescription
    numberThe output.

    sm.util.geometricMedian(points, initialGuess?, iterations?)

    Numerically approximates the geometric median from a table of points.

    If no initial guess is made, the mean position of all the points will be used as a guess.

    Parameters:

    TypeNameDescription
    tablepointsThe table of points. { Vec3, ... }.
    Vec3initialGuess?A guessed position to start searching from. (Optional)
    integeriterations?The number of iterations. (Defaults to 1)

    Returns:

    TypeDescription
    Vec3The estimated geometric median.

    sm.util.hash(...)

    Create a hash from arguments.

    Parameters:

    TypeNameDescription
    any...The arguments to be hashed.

    Returns:

    TypeDescription
    integerThe hash.

    sm.util.lerp(a, b, t)

    Linear interpolation between two values. This is known as a lerp.

    Parameters:

    TypeNameDescription
    numberaThe first value.
    numberbThe second value.
    numbertThe interpolation step.

    Returns:

    TypeDescription
    numberThe interpolated value between two values.

    sm.util.positiveModulo(x, n)

    Returns the positive remainder after division of x by n.

    Parameters:

    TypeNameDescription
    integerxThe number.
    integernThe modulo value.

    Returns:

    TypeDescription
    numberThe value.

    sm.util.smootherstep(edge0, edge1, x)

    An improved version of the smoothstep function which has zero 1st and 2nd order derivatives at `x = edge0` and `x = edge1`.

    Parameters:

    TypeNameDescription
    numberedge0The value of the lower edge of the Hermite function.
    numberedge1The value of the upper edge of the Hermite function.
    numberxThe source value for interpolation.

    Returns:

    TypeDescription
    numberThe value.

    sm.util.smoothstep(edge0, edge1, x)

    Performs smooth Hermite interpolation between 0 and 1 when `edge0 < x < edge1`. This is useful in cases where a threshold function with a smooth transition is desired.

    Parameters:

    TypeNameDescription
    numberedge0The value of the lower edge of the Hermite function.
    numberedge1The value of the upper edge of the Hermite function.
    numberxThe source value for interpolation.

    Returns:

    TypeDescription
    numberThe value.

    sm.util.utf8StringLength(string)

    Returns the number of UTF-8 characters in a string.

    Parameters:

    TypeNameDescription
    stringstringThe input string.

    Returns:

    TypeDescription
    integerThe number of UTF-8 characters.