Vec4
const vec4 = require("@vectarine/vec4")Module for 4D vectors. 4D vectors can represent colors and 3D rotations. You can use them for 3D by ignoring the last component.
Functions
Section titled “Functions”vec4.V4
Section titled “vec4.V4”vec4.V4(x: number, y: number, z: number, w: number): Vec4Create a new 4D vector
vec4.fromRotation
Section titled “vec4.fromRotation”vec4.fromRotation(axis: Vec4, angle: number): Vec4Returns a quaternion representing a rotation of angle radians around the given axis
The 4th component of the axis is ignored, it is treated as a 3d vector.
The length of the result is always 1.
vec4.createColor
Section titled “vec4.createColor”vec4.createColor(r: number, g: number, b: number, a: number): Vec4Alias for V4, but indicate that the Vec4 will be used as a color.
export type Vec4 = ...Implementation of a 4D vector
Operators
a + b — (a: Vec4, b: Vec4): Vec4
a - b — (a: Vec4, b: Vec4): Vec4
-a — (a: Vec4): Vec4
a * b — (a: Vec4, b: Vec4): Vec4
a / b — (a: Vec4, b: Vec4): Vec4
Vec4:scale
Section titled “Vec4:scale”Vec4:scale(k: number): Vec4Scales the vector by the given factor
Vec4:qmul
Section titled “Vec4:qmul”Vec4:qmul(b: Vec4): Vec4Performs a quaternion multiplication of a and b, treating them as quaternions
This is useful to apply rotations to 3D vectors
Note that a:qmul(b) != b:qmul(a) in general
Vec4:dot
Section titled “Vec4:dot”Vec4:dot(b: Vec4): numberReturns the dot product of the two vectors
Vec4:length
Section titled “Vec4:length”Vec4:length(): numberReturns the length of the vector, defined as math.sqrt(v.x*v.x + v.y*v.y + v.z*v.z + v.w*v.w)
Vec4:lengthSq
Section titled “Vec4:lengthSq”Vec4:lengthSq(): numberReturns the square of the length of the vector, defined as v.x*v.x + v.y*v.y + v.z*v.z + v.w*v.w
Vec4:distance
Section titled “Vec4:distance”Vec4:distance(b: Vec4): numberReturns the distance between the two vectors.
This is the same as (a - b):length()
Vec4:normalized
Section titled “Vec4:normalized”Vec4:normalized(): Vec4Returns a normalized version of the vector If the vector has zero length, returns a unit vector pointing in an arbitrary direction
Vec4:abs
Section titled “Vec4:abs”Vec4:abs(): Vec4Returns a vector with all components absolute values
Vec4:round
Section titled “Vec4:round”Vec4:round(digits_of_precision: number?): Vec4Returns a version of the vector rounded to the given number of digits of precision
If digits_of_precision is not provided, it defaults to 0 (round to nearest integer)
Vec4:floor
Section titled “Vec4:floor”Vec4:floor(): Vec4Returns a vector with all components rounded down to the nearest integer
Vec4:ceil
Section titled “Vec4:ceil”Vec4:ceil(): Vec4Returns a vector with all components rounded up to the nearest integer
Vec4:max
Section titled “Vec4:max”Vec4:max(b: Vec4): Vec4Returns the component-wise maximum of the two vectors
Vec4:min
Section titled “Vec4:min”Vec4:min(b: Vec4): Vec4Returns the component-wise minimum of the two vectors
Vec4:lerp
Section titled “Vec4:lerp”Vec4:lerp(b: Vec4, k: number): Vec4Returns a vector linearly interpolated between a and b by the factor k
Vec4:sign
Section titled “Vec4:sign”Vec4:sign(): Vec4Returns the vector containing the sign of each component of the vector
Vec4:noise
Section titled “Vec4:noise”Vec4:noise(): numberReturns the noise value at the given point Uses simplex noise
Vec4:worleyNoise
Section titled “Vec4:worleyNoise”Vec4:worleyNoise(): numberReturns the noise value at the given point Uses Worley noise
Vec4Data
Section titled “Vec4Data”export type Vec4Data = ...The type of 4D things
Constants
Section titled “Constants”vec4.ZERO4
Section titled “vec4.ZERO4”vec4.ZERO4 = ...The zero vector
vec4.BLACK
Section titled “vec4.BLACK”vec4.BLACK = ...vec4.WHITE
Section titled “vec4.WHITE”vec4.WHITE = ...vec4.TRANSPARENT
Section titled “vec4.TRANSPARENT”vec4.TRANSPARENT = ...vec4.RED
Section titled “vec4.RED”vec4.RED = ...vec4.GREEN
Section titled “vec4.GREEN”vec4.GREEN = ...vec4.BLUE
Section titled “vec4.BLUE”vec4.BLUE = ...vec4.YELLOW
Section titled “vec4.YELLOW”vec4.YELLOW = ...vec4.MAGENTA
Section titled “vec4.MAGENTA”vec4.MAGENTA = ...vec4.CYAN
Section titled “vec4.CYAN”vec4.CYAN = ...vec4.AZURE
Section titled “vec4.AZURE”vec4.AZURE = ...vec4.ORANGE
Section titled “vec4.ORANGE”vec4.ORANGE = ...vec4.PURPLE
Section titled “vec4.PURPLE”vec4.PURPLE = ...vec4.SPRING
Section titled “vec4.SPRING”vec4.SPRING = ...vec4.LIME
Section titled “vec4.LIME”vec4.LIME = ...vec4.PINK
Section titled “vec4.PINK”vec4.PINK = ...vec4.LIGHT_GRAY
Section titled “vec4.LIGHT_GRAY”vec4.LIGHT_GRAY = ...vec4.DARK_GRAY
Section titled “vec4.DARK_GRAY”vec4.DARK_GRAY = ...vec4.GRAY
Section titled “vec4.GRAY”vec4.GRAY = ...vec4.DARK_RED
Section titled “vec4.DARK_RED”vec4.DARK_RED = ...vec4.DARK_GREEN
Section titled “vec4.DARK_GREEN”vec4.DARK_GREEN = ...vec4.DARK_BLUE
Section titled “vec4.DARK_BLUE”vec4.DARK_BLUE = ...