Engine API Reference - v2.23.0-beta.17
    Preparing search index...

    Class Vec4

    A 4-dimensional vector. Vec4 is commonly used to represent homogeneous coordinates or shader uniforms requiring four components.

    Operations follow one convention throughout the math classes: a method that modifies the vector it is called on returns it, so calls can be chained and nothing is allocated, while queries such as length and dot return a number. Two-operand forms such as add2 and mul2 write the result of lhs op rhs into this, and it is safe for this to also be one of the operands. Use clone for an independent copy and copy to overwrite one vector with another.

    The static constants ZERO, HALF and ONE are frozen shared instances: read them freely, but writing to one throws.

    // Interpolate between two 4-component values into a third, without allocating
    const from = new Vec4(0, 0, 0, 0);
    const to = new Vec4(1, 1, 1, 1);
    const result = new Vec4();
    result.lerp(from, to, 0.25); // result is now [0.25, 0.25, 0.25, 0.25]
    Index
    • Creates a new Vec4 instance.

      Parameters

      • Optionalx: number

        The x value. Defaults to 0.

      • Optionaly: number

        The y value. Defaults to 0.

      • Optionalz: number

        The z value. Defaults to 0.

      • Optionalw: number

        The w value. Defaults to 0.

      Returns Vec4

      const v1 = new Vec4(); // defaults to 0, 0, 0, 0
      const v2 = new Vec4(1, 2, 3, 4);
    • Creates a new Vec4 instance.

      Parameters

      • arr: number[]

        The array to set the vector values from.

      Returns Vec4

      const v = new Vec4([1, 2, 3, 4]);
      
    w: number

    The fourth component of the vector.

    x: number

    The first component of the vector.

    y: number

    The second component of the vector.

    z: number

    The third component of the vector.

    HALF: Vec4 = ...

    A constant vector set to [0.5, 0.5, 0.5, 0.5].

    ONE: Vec4 = ...

    A constant vector set to [1, 1, 1, 1].

    ZERO: Vec4 = ...

    A constant vector set to [0, 0, 0, 0].

    • Adds a 4-dimensional vector to another in place.

      Parameters

      • rhs: Vec4

        The vector to add to the specified vector.

      Returns Vec4

      Self for chaining.

      const a = new Vec4(10, 10, 10, 10);
      const b = new Vec4(20, 20, 20, 20);

      a.add(b);

      // Outputs [30, 30, 30, 30]
      console.log("The result of the addition is: " + a.toString());
    • Adds two 4-dimensional vectors together and returns the result.

      Parameters

      • lhs: Vec4

        The first vector operand for the addition.

      • rhs: Vec4

        The second vector operand for the addition.

      Returns Vec4

      Self for chaining.

      const a = new Vec4(10, 10, 10, 10);
      const b = new Vec4(20, 20, 20, 20);
      const r = new Vec4();

      r.add2(a, b);
      // Outputs [30, 30, 30, 30]

      console.log("The result of the addition is: " + r.toString());
    • Adds a number to each element of a vector.

      Parameters

      • scalar: number

        The number to add.

      Returns Vec4

      Self for chaining.

      const vec = new Vec4(3, 4, 5, 6);

      vec.addScalar(2);

      // Outputs [5, 6, 7, 8]
      console.log("The result of the addition is: " + vec.toString());
    • Adds a 4-dimensional vector scaled by scalar value. Does not modify the vector being added.

      Parameters

      • rhs: Vec4

        The vector to add to the specified vector.

      • scalar: number

        The number to multiply the added vector with.

      Returns Vec4

      Self for chaining.

      const vec = new Vec4(1, 2, 3, 4);

      vec.addScaled(Vec4.ONE, 2);

      // Outputs [3, 4, 5, 6]
      console.log("The result of the addition is: " + vec.toString());
    • Each element is rounded up to the next largest integer.

      Parameters

      • Optionalsrc: Vec4 = ...

        The vector to ceil. If not set, the operation is done in place.

      Returns Vec4

      Self for chaining.

      const v = new Vec4(1.2, 3.1, 5.9, 7.4);
      v.ceil();
      // v is now [2, 4, 6, 8]
    • Returns an identical copy of the specified 4-dimensional vector.

      Returns Vec4

      A 4-dimensional vector containing the result of the cloning.

      const v = new Vec4(10, 20, 30, 40);
      const vclone = v.clone();
      console.log("The result of the cloning is: " + vclone.toString());
    • Copies the contents of a source 4-dimensional vector to a destination 4-dimensional vector.

      Parameters

      • rhs: Vec4

        A vector to copy to the specified vector.

      Returns Vec4

      Self for chaining.

      const src = new Vec4(10, 20, 30, 40);
      const dst = new Vec4();

      dst.copy(src);

      console.log("The two vectors are " + (dst.equals(src) ? "equal" : "different"));
    • Divides a 4-dimensional vector by another in place.

      Parameters

      • rhs: Vec4

        The vector to divide the specified vector by.

      Returns Vec4

      Self for chaining.

      const a = new Vec4(4, 9, 16, 25);
      const b = new Vec4(2, 3, 4, 5);

      a.div(b);

      // Outputs [2, 3, 4, 5]
      console.log("The result of the division is: " + a.toString());
    • Divides one 4-dimensional vector by another and writes the result to the specified vector.

      Parameters

      • lhs: Vec4

        The dividend vector (the vector being divided).

      • rhs: Vec4

        The divisor vector (the vector dividing the dividend).

      Returns Vec4

      Self for chaining.

      const a = new Vec4(4, 9, 16, 25);
      const b = new Vec4(2, 3, 4, 5);
      const r = new Vec4();

      r.div2(a, b);

      // Outputs [2, 3, 4, 5]
      console.log("The result of the division is: " + r.toString());
    • Divides each element of a vector by a number.

      Parameters

      • scalar: number

        The number to divide by.

      Returns Vec4

      Self for chaining.

      const vec = new Vec4(3, 6, 9, 12);

      vec.divScalar(3);

      // Outputs [1, 2, 3, 4]
      console.log("The result of the division is: " + vec.toString());
    • Returns the result of a dot product operation performed on the two specified 4-dimensional vectors.

      Parameters

      • rhs: Vec4

        The second 4-dimensional vector operand of the dot product.

      Returns number

      The result of the dot product operation.

      const v1 = new Vec4(5, 10, 20, 40);
      const v2 = new Vec4(10, 20, 40, 80);
      const v1dotv2 = v1.dot(v2);
      console.log("The result of the dot product is: " + v1dotv2);
    • Reports whether two vectors are equal.

      Parameters

      • rhs: Vec4

        The vector to compare to the specified vector.

      Returns boolean

      True if the vectors are equal and false otherwise.

      const a = new Vec4(1, 2, 3, 4);
      const b = new Vec4(5, 6, 7, 8);
      console.log("The two vectors are " + (a.equals(b) ? "equal" : "different"));
    • Reports whether two vectors are equal using an absolute error tolerance.

      Parameters

      • rhs: Vec4

        The vector to be compared against.

      • Optionalepsilon: number = 1e-6

        The maximum difference between each component of the two vectors. Defaults to 1e-6.

      Returns boolean

      True if the vectors are equal and false otherwise.

      const a = new Vec4();
      const b = new Vec4();
      console.log("The two vectors are approximately " + (a.equalsApprox(b, 1e-9) ? "equal" : "different"));
    • Each element is set to the largest integer less than or equal to its value.

      Parameters

      • Optionalsrc: Vec4 = ...

        The vector to floor. If not set, the operation is done in place.

      Returns Vec4

      Self for chaining.

      const v = new Vec4(1.2, 3.9, 5.5, 7.8);
      v.floor();
      // v is now [1, 3, 5, 7]
    • Set the values of the vector from an array.

      Parameters

      • arr: number[] | ArrayBufferView<ArrayBufferLike>

        The array to set the vector values from.

      • Optionaloffset: number = 0

        The zero-based index at which to start copying elements from the array. Default is 0.

      Returns Vec4

      Self for chaining.

      const v = new Vec4();
      v.fromArray([20, 10, 5, 0]);
      // v is set to [20, 10, 5, 0]
    • Returns the magnitude of the specified 4-dimensional vector.

      Returns number

      The magnitude of the specified 4-dimensional vector.

      const vec = new Vec4(3, 4, 0, 0);
      const len = vec.length();
      // Outputs 5
      console.log("The length of the vector is: " + len);
    • Returns the magnitude squared of the specified 4-dimensional vector.

      Returns number

      The magnitude squared of the specified 4-dimensional vector.

      const vec = new Vec4(3, 4, 0, 0);
      const len = vec.lengthSq();
      // Outputs 25
      console.log("The length squared of the vector is: " + len);
    • Returns the result of a linear interpolation between two specified 4-dimensional vectors.

      Parameters

      • lhs: Vec4

        The 4-dimensional vector to interpolate from.

      • rhs: Vec4

        The 4-dimensional vector to interpolate to.

      • alpha: number

        The value controlling the point of interpolation. Between 0 and 1, the linear interpolant will occur on a straight line between lhs and rhs. Outside of this range, the linear interpolant will occur on a ray extrapolated from this line.

      Returns Vec4

      Self for chaining.

      const a = new Vec4(0, 0, 0, 0);
      const b = new Vec4(10, 10, 10, 10);
      const r = new Vec4();

      r.lerp(a, b, 0); // r is equal to a
      r.lerp(a, b, 0.5); // r is 5, 5, 5, 5
      r.lerp(a, b, 1); // r is equal to b
    • Each element is assigned a value from rhs parameter if it is larger.

      Parameters

      • rhs: Vec4

        The 4-dimensional vector used as the source of elements to compare to.

      Returns Vec4

      Self for chaining.

      const a = new Vec4(5, 1, 7, 3);
      const b = new Vec4(2, 8, 3, 9);
      a.max(b);
      // a is now [5, 8, 7, 9]
    • Each element is assigned a value from rhs parameter if it is smaller.

      Parameters

      • rhs: Vec4

        The 4-dimensional vector used as the source of elements to compare to.

      Returns Vec4

      Self for chaining.

      const a = new Vec4(5, 1, 7, 3);
      const b = new Vec4(2, 8, 3, 9);
      a.min(b);
      // a is now [2, 1, 3, 3]
    • Multiplies a 4-dimensional vector to another in place.

      Parameters

      • rhs: Vec4

        The 4-dimensional vector used as the second multiplicand of the operation.

      Returns Vec4

      Self for chaining.

      const a = new Vec4(2, 3, 4, 5);
      const b = new Vec4(4, 5, 6, 7);

      a.mul(b);

      // Outputs 8, 15, 24, 35
      console.log("The result of the multiplication is: " + a.toString());
    • Returns the result of multiplying the specified 4-dimensional vectors together.

      Parameters

      • lhs: Vec4

        The 4-dimensional vector used as the first multiplicand of the operation.

      • rhs: Vec4

        The 4-dimensional vector used as the second multiplicand of the operation.

      Returns Vec4

      Self for chaining.

      const a = new Vec4(2, 3, 4, 5);
      const b = new Vec4(4, 5, 6, 7);
      const r = new Vec4();

      r.mul2(a, b);

      // Outputs 8, 15, 24, 35
      console.log("The result of the multiplication is: " + r.toString());
    • Multiplies each element of a vector by a number.

      Parameters

      • scalar: number

        The number to multiply by.

      Returns Vec4

      Self for chaining.

      const vec = new Vec4(3, 6, 9, 12);

      vec.mulScalar(3);

      // Outputs [9, 18, 27, 36]
      console.log("The result of the multiplication is: " + vec.toString());
    • Returns this 4-dimensional vector converted to a unit vector in place. If the vector has a length of zero, the vector's elements will be set to zero.

      Parameters

      • Optionalsrc: Vec4 = ...

        The vector to normalize. If not set, the operation is done in place.

      Returns Vec4

      Self for chaining.

      const v = new Vec4(25, 0, 0, 0);

      v.normalize();

      // Outputs 1, 0, 0, 0
      console.log("The result of the vector normalization is: " + v.toString());
    • Each element is rounded up or down to the nearest integer.

      Parameters

      • Optionalsrc: Vec4 = ...

        The vector to round. If not set, the operation is done in place.

      Returns Vec4

      Self for chaining.

      const v = new Vec4(1.4, 3.6, 5.5, 7.2);
      v.round();
      // v is now [1, 4, 6, 7]
    • Sets the specified 4-dimensional vector to the supplied numerical values.

      Parameters

      • x: number

        The value to set on the first component of the vector.

      • y: number

        The value to set on the second component of the vector.

      • z: number

        The value to set on the third component of the vector.

      • w: number

        The value to set on the fourth component of the vector.

      Returns Vec4

      Self for chaining.

      const v = new Vec4();
      v.set(5, 10, 20, 40);

      // Outputs 5, 10, 20, 40
      console.log("The result of the vector set is: " + v.toString());
    • Subtracts a 4-dimensional vector from another in place.

      Parameters

      • rhs: Vec4

        The vector to subtract from the specified vector.

      Returns Vec4

      Self for chaining.

      const a = new Vec4(10, 10, 10, 10);
      const b = new Vec4(20, 20, 20, 20);

      a.sub(b);

      // Outputs [-10, -10, -10, -10]
      console.log("The result of the subtraction is: " + a.toString());
    • Subtracts two 4-dimensional vectors from one another and returns the result.

      Parameters

      • lhs: Vec4

        The first vector operand for the subtraction.

      • rhs: Vec4

        The second vector operand for the subtraction.

      Returns Vec4

      Self for chaining.

      const a = new Vec4(10, 10, 10, 10);
      const b = new Vec4(20, 20, 20, 20);
      const r = new Vec4();

      r.sub2(a, b);

      // Outputs [-10, -10, -10, -10]
      console.log("The result of the subtraction is: " + r.toString());
    • Subtracts a number from each element of a vector.

      Parameters

      • scalar: number

        The number to subtract.

      Returns Vec4

      Self for chaining.

      const vec = new Vec4(3, 4, 5, 6);

      vec.subScalar(2);

      // Outputs [1, 2, 3, 4]
      console.log("The result of the subtraction is: " + vec.toString());
    • Parameters

      • Optionalarr: number[]

        The array to populate with the vector's number components. If not specified, a new array is created.

      • Optionaloffset: number

        The zero-based index at which to start copying elements to the array. Default is 0.

      Returns number[]

      The vector as an array.

    • Parameters

      • arr: ArrayBufferView

        The array to populate with the vector's number components. If not specified, a new array is created.

      • Optionaloffset: number

        The zero-based index at which to start copying elements to the array. Default is 0.

      Returns ArrayBufferView

      The vector as an array.

    • Converts the vector to string form.

      Returns string

      The vector in string form.

      const v = new Vec4(20, 10, 5, 0);
      // Outputs [20, 10, 5, 0]
      console.log(v.toString());