Vector4

Class representing a 4D vector. A 4D vector is an ordered quadruplet of numbers (labeled x, y, z, and w), which can be used to represent a number of things, such as:

There are other things a 4D vector can be used to represent, however these are the most common uses in Verge3D.

Iterating through a Vector4 instance will yield its components (x, y, z, w) in the corresponding order.

Code Example

const a = new v3d.Vector4(0, 1, 0, 0); // no arguments; will be initialised to (0, 0, 0, 1) const b = new v3d.Vector4(); const d = a.dot(b);

Constructor

Vector4(x : Float, y : Float, z : Float, w : Float)

x — the x value of this vector. Default is 0.
y — the y value of this vector. Default is 0.
z — the z value of this vector. Default is 0.
w — the w value of this vector. Default is 1.

Creates a new Vector4.

Properties

.isVector4 : Boolean

Read-only flag to check if a given object is of type Vector4.

.x : Float

.y : Float

.z : Float

.w : Float

.width : Float

Alias for z.

.height : Float

Alias for w.

Methods

.add(v : Vector4) → this

Adds v to this vector.

.addScalar(s : Float) → this

Adds the scalar value s to this vector's x, y, z and w values.

.addScaledVector(v : Vector4, s : Float) → this

Adds the multiple of v and s to this vector.

.addVectors(a : Vector4, b : Vector4) → this

Sets this vector to a + b.

.applyMatrix4(m : Matrix4) → this

Multiplies this vector by 4 x 4 m.

.ceil() → this

The x, y, z and w components of this vector are rounded up to the nearest integer value.

.clamp(min : Vector4, max : Vector4) → this

min — the minimum x, y, z and w values.
max — the maximum x, y, z and w values in the desired range.

If this vector's x, y, z or w value is greater than the max vector's x, y, z or w value, it is replaced by the corresponding value.

If this vector's x, y, z or w value is less than the min vector's x, y, z or w value, it is replaced by the corresponding value.

.clampLength(min : Float, max : Float) → this

min — the minimum value the length will be clamped to.
max — the maximum value the length will be clamped to.

If this vector's length is greater than the max value, it is replaced by the max value. If this vector's length is less than the min value, it is replaced by the min value.

.clampScalar(min : Float, max : Float) → this

min — the minimum value the components will be clamped to.
max — the maximum value the components will be clamped to.

If this vector's x, y, z or w values are greater than the max value, they are replaced by the max value. If this vector's x, y, z or w values are less than the min value, they are replaced by the min value.

.clone() → Vector4

Returns a new Vector4 with the same x, y, z and w values as this one.

.copy(v : Vector4) → this

Copies the values of the passed Vector4's x, y, z and w properties to this Vector4.

.divideScalar(s : Float) → this

Divides this vector by scalar s.

.dot(v : Vector4) → Float

Calculates the dot product of this vector and v.

.equals(v : Vector4) → Boolean

Returns true if the components of this vector and v are strictly equal; false otherwise.

.floor() → this

The components of this vector are rounded down to the nearest integer value.

.fromArray(array : Array, offset : Integer) → this

array — the source array.
offset — (optional) offset into the array. Default is 0.

Sets this vector's x value to be array[offset + 0], y value to be array[offset + 1] z value to be array[offset + 2] and w value to be array[offset + 3].

.fromBufferAttribute(attribute : BufferAttribute, index : Integer) → this

attribute — the source attribute.
index — index in the attribute.

Sets this vector's x, y, z and w values from the attribute.

.getComponent(index : Integer) → Float

index0, 1, 2, or 3.

If index equals 0 returns the x value.
If index equals 1 returns the y value.
If index equals 2 returns the z value.
If index equals 3 returns the w value.

.length() → Float

Computes the Euclidean length (straight-line length) from (0, 0, 0, 0) to (x, y, z, w).

.manhattanLength() → Float

Computes the Manhattan length of this vector.

.lengthSq() → Float

Computes the square of the Euclidean length (straight-line length) from (0, 0, 0, 0) to (x, y, z, w). If you are comparing the lengths of vectors, you should compare the length squared instead as it is slightly more efficient to calculate.

.lerp(v : Vector4, alpha : Float) → this

vVector4 to interpolate towards.
alpha — interpolation factor, typically in the closed interval [0, 1].

Linearly interpolates between this vector and v, where alpha is the percent distance along the line: alpha = 0 will be this vector, and alpha = 1 will be v.

.lerpVectors(v1 : Vector4, v2 : Vector4, alpha : Float) → this

v1 — the starting Vector4.
v2Vector4 to interpolate towards.
alpha — interpolation factor, typically in the closed interval [0, 1].

Sets this vector to be the vector linearly interpolated between v1 and v2 where alpha is the percent distance along the line connecting the two vectors: alpha = 0 will be v1, and alpha = 1 will be v2.

.negate() → this

Inverts this vector — i.e. sets x = -x, y = -y, z = -z, and w = -w.

.normalize() → this

Converts this vector to a unit vector — that is, sets it equal to a vector with the same direction as this one, but length 1.

.max(v : Vector4) → this

If this vector's x, y, z or w value is less than v's x, y, z or w value, replace that value with the corresponding max value.

.min(v : Vector4) → this

If this vector's x, y, z or w value is greater than v's x, y, z or w value, replace that value with the corresponding min value.

.multiply(v : Vector4) → this

Multiplies this vector by v.

.multiplyScalar(s : Float) → this

Multiplies this vector by scalar s.

.round() → this

The components of this vector are rounded to the nearest integer value.

.roundToZero() → this

The components of this vector are rounded towards zero (up if negative, down if positive) to an integer value.

.set(x : Float, y : Float, z : Float, w : Float) → this

Sets the x, y, z and w components of this vector.

.setAxisAngleFromQuaternion(q : Quaternion) → this

q — a normalized Quaternion.

Sets the x, y and z components of this vector to the quaternion's axis and w to the angle.

.setAxisAngleFromRotationMatrix(m : Matrix4) → this

m — a Matrix4 of which the upper left 3x3 matrix is a pure rotation matrix.

Sets the x, y and z to the axis of rotation and w to the angle.

.setComponent(index : Integer, value : Float) → this

index0, 1, or 2.
valueFloat.

If index equals 0 set x to value.
If index equals 1 set y to value.
If index equals 2 set z to value.
If index equals 3 set w to value.

.setLength(l : Float) → this

Sets this vector to a vector with the same direction as this one, but length l.

.setScalar(scalar : Float) → this

Sets the x, y, z and w values of this vector both equal to scalar.

.setX(x : Float) → this

Replaces this vector's x value with x.

.setY(y : Float) → this

Replaces this vector's y value with y.

.setZ(z : Float) → this

Replaces this vector's z value with z.

.setW(w : Float) → this

Replaces this vector's w value with w.

.sub(v : Vector4) → this

Subtracts v from this vector.

.subScalar(s : Float) → this

Subtracts s from this vector's x, y, z and w components.

.subVectors(a : Vector4, b : Vector4) → this

Sets this vector to a - b.

.toArray(array : Array, offset : Integer) → Array

array — (optional) array to store this vector to. If this is not provided, a new array will be created.
offset — (optional) optional offset into the array.

Returns an array [x, y, z, w], or copies x, y, z and w into the provided array.

.random() → this

Sets each component of this vector to a pseudo-random value between 0 and 1, excluding 1.

Source

For more info on how to obtain the source code of this module see this page.