Class representing a 3D vector. A 3D vector is an ordered triplet of numbers (labeled x, y, and z), which can be used to represent a number of things, such as:
(0, 0, 0)
to (x, y, z)
and the direction is also
measured from (0, 0, 0)
towards (x, y, z)
.
There are other things a 3D vector can be used to represent, such as momentum vectors and so on, however these are the most common uses in Verge3D.
Iterating through a Vector3 instance will yield its components (x, y, z)
in the corresponding order.
const a = new v3d.Vector3(0, 1, 0);
// no arguments; will be initialised to (0, 0, 0)
const b = new v3d.Vector3();
const d = a.distanceTo(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
.
Creates a new Vector3.
Read-only flag to check if a given object is of type Vector3.
Adds v to this vector.
Adds the scalar value s to this vector's x, y and z values.
Adds the multiple of v and s to this vector.
axis — A normalized Vector3.
angle — An angle in radians.
Applies a rotation specified by an axis and an angle to this vector.
Applies euler transform to this vector by converting the Euler object to a Quaternion and applying.
Multiplies this vector by m
Multiplies this vector (with an implicit 1 in the 4th dimension) and m, and divides by perspective.
Multiplies this vector by normal matrix m and normalizes the result.
Applies a Quaternion transform to this vector.
Returns the angle between this vector and vector v in radians.
The x, y and z components of this vector are rounded up to the nearest integer value.
min — the minimum x, y and z values.
max — the maximum x, y and z values in the desired range.
If this vector's x, y or z value is greater than the max vector's x, y or z value, it is replaced by the corresponding value. If this vector's x, y or z value is less than the min vector's x, y or z 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, the vector will be scaled down so its length is the max value. If this vector's length is less than the min value, the vector will be scaled up so its length is 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 or z values are greater than the max value, they are replaced by the max value. If this vector's x, y or z values are less than the min value, they are replaced by the min value.
Returns a new vector3 with the same x, y and z values as this one.
Copies the values of the passed vector3's x, y and z properties to this vector3.
Sets this vector to cross product of itself and v.
Sets this vector to cross product of a and b.
Computes the distance from this vector to v.
Computes the Manhattan distance from this vector to v.
Computes the squared distance from this vector to v. If you are just comparing the distance with another distance, you should compare the distance squared instead as it is slightly more efficient to calculate.
Divides this vector by v.
Divides this vector by scalar s.
Calculate 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]
and z value to be array[offset + 2]
.
attribute — the source attribute.
index — index in the attribute.
Sets this vector's x, y and z values from the attribute.
index — 0
, 1
, or 2
.
If index equals 0 returns the x value.
If index equals 1 returns the y value.
If index equals 2 returns the z value.
Computes the Euclidean length (straight-line length) from (0, 0, 0) to (x, y, z).
Computes the Manhattan length of this vector.
Computes the square of the Euclidean length (straight-line length) from (0, 0, 0) to (x, y, z). If you are comparing the lengths of vectors, you should compare the length squared instead as it is slightly more efficient to calculate.
v — Vector3 to interpolate towards.
alpha — interpolation factor, typically in the closed interval [0, 1]
.
Linearly interpolate 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 Vector3.
v2 - Vector3 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. If
alpha = 0
it will be v1, and if alpha = 1
it will be v2.
If this vector's x, y or z value is less than v's x, y or z value, replace that value with the corresponding max value.
If this vector's x, y or z value is greater than v's x, y or z value, replace that value with the corresponding min value.
Multiplies this vector by v.
Multiplies this vector by scalar s.
Sets this vector equal to a * b, component-wise.
Inverts this vector — i.e. sets x = -x
, y = -y
, and z = -z
.
Convert this vector to a unit vector — that is, sets it equal to a vector with the same direction as this one, but length 1.
camera — camera to use in the projection.
Projects this vector from world space into the camera's normalized device coordinate (NDC) space.
planeNormal — A vector representing a plane normal.
Projects this vector onto a plane by subtracting this vector projected onto the plane's normal from this vector.
normal — the normal to the reflecting plane.
Reflect this vector off of plane orthogonal to normal. Normal is assumed to have unit length.
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 and z components of this vector.
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
Sets this vector from the cylindrical coordinates c.
Sets this vector from the cylindrical coordinates radius, theta and y.
Sets this vector's x, y and z components from the x, y, and z components of the specified Euler Angle.
Sets this vector's x, y and z components from index column of matrix.
Sets this vector's x, y and z components from index column of matrix.
Sets this vector to the position elements of the transformation matrix m.
Sets this vector to the scale elements of the transformation matrix m.
Sets this vector from the spherical coordinates s.
Sets this vector from the spherical coordinates radius, phi and theta.
Set this vector to a vector with the same direction as this one, but length l.
Set the x, y and z values of this vector both equal to scalar.
Replace this vector's x value with x.
Replace this vector's y value with y.
Replace this vector's z value with z.
Subtracts v from this vector.
Subtracts s from this vector's x, y and z 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], or copies x, y and z into the provided array.
Transforms the direction of this vector by a matrix (the upper left 3 x 3 subset of a m) and then normalizes the result.
camera — camera to use in the projection.
Projects this vector from the camera's normalized device coordinate (NDC) space into world space.
Sets each component of this vector to a pseudo-random value between 0 and 1, excluding 1.
Sets this vector to a uniformly random point on a unit sphere.
The following puzzles can be used to create and perform math operations on vectors in a visual manner:
For more info on how to obtain the source code of this module see this page.