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:
(0, 0, 0, 0)
to (x, y, z, w)
and the direction is also
measured from (0, 0, 0, 0)
towards (x, y, z, w)
.
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.
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);
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.
Read-only flag to check if a given object is of type Vector4.
Alias for z.
Alias for w.
Adds v to this vector.
Adds the scalar value s to this vector's x, y, z and w values.
Adds the multiple of v and s to this vector.
Multiplies this vector by 4 x 4 m.
The x, y, z and w components of this vector are rounded up to the nearest integer value.
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.
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.
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.
Returns a new Vector4 with the same x, y, z and w values as this one.
Copies the values of the passed Vector4's x, y, z and w properties to this Vector4.
Divides this vector by scalar s.
Calculates the dot product of this vector and v.
Returns true
if the components of this vector and v are strictly equal; false
otherwise.
The components of this vector are rounded down to the nearest integer value.
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]
.
attribute — the source attribute.
index — index in the attribute.
Sets this vector's x, y, z and w values from the attribute.
index — 0
, 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.
Computes the Euclidean length (straight-line length) from (0, 0, 0, 0)
to (x, y, z, w)
.
Computes the Manhattan length of this vector.
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.
v — Vector4 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.
v1 — the starting Vector4.
v2 — Vector4 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.
Inverts this vector — i.e. sets x = -x
, y = -y
, z = -z
, and w = -w
.
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
.
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.
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.
Multiplies this vector by v.
Multiplies this vector by scalar s.
The components of this vector are rounded to the nearest integer value.
The components of this vector are rounded towards zero (up if negative, down if positive) to an integer value.
Sets the x, y, z and w components of this vector.
q — a normalized Quaternion.
Sets the x, y and z components of this vector to the quaternion's axis and w to the angle.
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.
index — 0
, 1
, or 2
.
value — Float.
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.
Sets this vector to a vector with the same direction as this one, but length l.
Sets the x, y, z and w values of this vector both equal to scalar.
Replaces this vector's x value with x.
Replaces this vector's y value with y.
Replaces this vector's z value with z.
Replaces this vector's w value with w.
Subtracts v from this vector.
Subtracts s from this vector's x, y, z and w components.
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.
Sets each component of this vector to a pseudo-random value between 0 and 1, excluding 1.
For more info on how to obtain the source code of this module see this page.