Constructor
new Vector3(xopt, yopt, zopt)
Constructs a new 3D vector.
Name | Type | Attributes | Default | Description |
---|---|---|---|---|
x | number | <optional> | 0 | The x value of this vector. |
y | number | <optional> | 0 | The y value of this vector. |
z | number | <optional> | 0 | The z value of this vector. |
Members
(readonly) isVector3 :boolean
This flag can be used for type testing.
- boolean
- Default Value
- true
x :number
The x value of this vector.
- number
y :number
The y value of this vector.
- number
z :number
The z value of this vector.
- number
Methods
add(v) → {Vector3}
Adds the given vector to this instance.
Name | Type | Description |
---|---|---|
v | Vector3 | The vector to add. |
A reference to this vector.
- Type:
- Vector3
addScalar(s) → {Vector3}
Adds the given scalar value to all components of this instance.
Name | Type | Description |
---|---|---|
s | number | The scalar to add. |
A reference to this vector.
- Type:
- Vector3
addScaledVector(v, s) → {Vector3}
Adds the given vector scaled by the given factor to this instance.
A reference to this vector.
- Type:
- Vector3
addVectors(a, b) → {Vector3}
Adds the given vectors and stores the result in this instance.
A reference to this vector.
- Type:
- Vector3
angleTo(v) → {number}
Returns the angle between the given vector and this instance in radians.
Name | Type | Description |
---|---|---|
v | Vector3 | The vector to compute the angle with. |
The angle in radians.
- Type:
- number
applyMatrix3(m) → {Vector3}
Multiplies this vector with the given 3x3 matrix.
Name | Type | Description |
---|---|---|
m | Matrix3 | The 3x3 matrix. |
A reference to this vector.
- Type:
- Vector3
applyMatrix4(m) → {Vector3}
Multiplies this vector (with an implicit 1 in the 4th dimension) by m, and divides by perspective.
Name | Type | Description |
---|---|---|
m | Matrix4 | The matrix to apply. |
A reference to this vector.
- Type:
- Vector3
applyQuaternion(q) → {Vector3}
Applies the given Quaternion to this vector.
Name | Type | Description |
---|---|---|
q | Quaternion | The Quaternion. |
A reference to this vector.
- Type:
- Vector3
clone() → {Vector3}
Returns a new vector with copied values from this instance.
A clone of this instance.
- Type:
- Vector3
copy(v) → {Vector3}
Copies the values of the given vector to this instance.
Name | Type | Description |
---|---|---|
v | Vector3 | The vector to copy. |
A reference to this vector.
- Type:
- Vector3
cross(v) → {Vector3}
Calculates the cross product of the given vector with this instance.
Name | Type | Description |
---|---|---|
v | Vector3 | The vector to compute the cross product with. |
The result of the cross product.
- Type:
- Vector3
crossVectors(a, b) → {Vector3}
Calculates the cross product of the given vectors and stores the result in this instance.
A reference to this vector.
- Type:
- Vector3
distanceTo(v) → {number}
Computes the distance from the given vector to this instance.
Name | Type | Description |
---|---|---|
v | Vector3 | The vector to compute the distance to. |
The distance.
- Type:
- number
distanceToSquared(v) → {number}
Computes the squared distance from the given vector to this instance. 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.
Name | Type | Description |
---|---|---|
v | Vector3 | The vector to compute the squared distance to. |
The squared distance.
- Type:
- number
dot(v) → {number}
Calculates the dot product of the given vector with this instance.
Name | Type | Description |
---|---|---|
v | Vector3 | The vector to compute the dot product with. |
The result of the dot product.
- Type:
- number
equals(v) → {boolean}
Returns true
if this vector is equal with the given one.
Name | Type | Description |
---|---|---|
v | Vector3 | The vector to test for equality. |
Whether this vector is equal with the given one.
- Type:
- boolean
fromArray(array, offsetopt, denormalizeopt) → {Vector3}
Sets this vector's x value to be array[ offset ]
, y value to be array[ offset + 1 ]
and z value to be array[ offset + 2 ]
.
Name | Type | Attributes | Default | Description |
---|---|---|---|---|
array | Array.<number> | An array holding the vector component values. | ||
offset | number | <optional> | 0 | The offset into the array. |
denormalize | boolean | <optional> | false | If true, denormalize the values, and array should be a typed array. |
A reference to this vector.
- Type:
- Vector3
getLength() → {number}
Computes the Euclidean length (straight-line length) from (0, 0, 0) to (x, y, z).
The length of this vector.
- Type:
- number
getLengthSquared() → {number}
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.
The square length of this vector.
- Type:
- number
lerp(v, alpha) → {Vector3}
Linearly interpolates between the given vector and this instance, where alpha is the percent distance along the line - alpha = 0 will be this vector, and alpha = 1 will be the given one.
Name | Type | Description |
---|---|---|
v | Vector3 | The vector to interpolate towards. |
alpha | number | The interpolation factor, typically in the closed interval |
A reference to this vector.
- Type:
- Vector3
lerpVectors(v1, v2, alpha) → {Vector3}
Linearly interpolates between the given vectors, where alpha is the percent distance along the line - alpha = 0 will be first vector, and alpha = 1 will be the second one. The result is stored in this instance.
Name | Type | Description |
---|---|---|
v1 | Vector3 | The first vector. |
v2 | Vector3 | The second vector. |
alpha | number | The interpolation factor, typically in the closed interval |
A reference to this vector.
- Type:
- Vector3
max(v) → {Vector3}
If this vector's x, y or z value is less than the given vector's x, y or z value, replace that value with the corresponding max value.
Name | Type | Description |
---|---|---|
v | Vector3 | The vector. |
A reference to this vector.
- Type:
- Vector3
min(v) → {Vector3}
If this vector's x, y or z value is greater than the given vector's x, y or z value, replace that value with the corresponding min value.
Name | Type | Description |
---|---|---|
v | Vector3 | The vector. |
A reference to this vector.
- Type:
- Vector3
multiply(v) → {Vector3}
Multiplies the given vector with this instance.
Name | Type | Description |
---|---|---|
v | Vector3 | The vector to multiply. |
A reference to this vector.
- Type:
- Vector3
multiplyScalar(scalar) → {Vector3}
Multiplies the given scalar value with all components of this instance.
Name | Type | Description |
---|---|---|
scalar | number | The scalar to multiply. |
A reference to this vector.
- Type:
- Vector3
negate() → {Vector3}
Inverts this vector - i.e. sets x = -x, y = -y and z = -z.
A reference to this vector.
- Type:
- Vector3
normalize(thicknessopt) → {Vector3}
Converts this vector to a unit vector - that is, sets it equal to a vector with the same direction as this one, but with a vector length of 1
.
Name | Type | Attributes | Default | Description |
---|---|---|---|---|
thickness | number | <optional> | 1 |
A reference to this vector.
- Type:
- Vector3
project(camera) → {Vector3}
Projects this vector from world space into the camera's normalized device coordinate (NDC) space.
Name | Type | Description |
---|---|---|
camera | Camera | The camera. |
A reference to this vector.
- Type:
- Vector3
reflect(normal) → {Vector3}
Reflects this vector off a plane orthogonal to the given normal vector.
Name | Type | Description |
---|---|---|
normal | Vector3 | The (normalized) normal vector. |
A reference to this vector.
- Type:
- Vector3
set(x, y, z) → {Vector3}
Sets the vector components.
Name | Type | Default | Description |
---|---|---|---|
x | number | 0 | The value of the x component. |
y | number | 0 | The value of the y component. |
z | number | 0 | The value of the z component. |
A reference to this vector.
- Type:
- Vector3
setFromMatrixColumn(m, index) → {Vector3}
Sets the vector components from the specified matrix column.
Name | Type | Description |
---|---|---|
m | Matrix4 | The 4x4 matrix. |
index | number | The column index. |
A reference to this vector.
- Type:
- Vector3
setFromMatrixPosition(m) → {Vector3}
Sets the vector components to the position elements of the given transformation matrix.
Name | Type | Description |
---|---|---|
m | Matrix4 | The 4x4 matrix. |
A reference to this vector.
- Type:
- Vector3
setFromMatrixScale(m) → {Vector3}
Sets the vector components to the scale elements of the given transformation matrix.
Name | Type | Description |
---|---|---|
m | Matrix4 | The 4x4 matrix. |
A reference to this vector.
- Type:
- Vector3
setFromSpherical(s) → {Vector3}
Sets the vector components from the given spherical coordinates.
Name | Type | Description |
---|---|---|
s | Spherical | The spherical coordinates. |
A reference to this vector.
- Type:
- Vector3
setScalar(scalar) → {Vector3}
Sets the vector components to the same value.
Name | Type | Description |
---|---|---|
scalar | number | The value to set for all vector components. |
A reference to this vector.
- Type:
- Vector3
sub(v) → {Vector3}
Subtracts the given vector from this instance.
Name | Type | Description |
---|---|---|
v | Vector3 | The vector to subtract. |
A reference to this vector.
- Type:
- Vector3
subVectors(a, b) → {Vector3}
Subtracts the given vectors and stores the result in this instance.
A reference to this vector.
- Type:
- Vector3
toArray(arrayopt, offsetopt, normalizeopt) → {Array.<number>}
Writes the components of this vector to the given array. If no array is provided, the method returns a new instance.
Name | Type | Attributes | Default | Description |
---|---|---|---|---|
array | Array.<number> | <optional> | [] | The target array holding the vector components. |
offset | number | <optional> | 0 | Index of the first element in the array. |
normalize | boolean | <optional> | false | if true, normalize the values, and array should be a typed array. |
The vector components.
- Type:
- Array.<number>
transformDirection(m) → {Vector3}
Transforms the direction of this vector by a matrix (the upper left 3 x 3 subset of the given 4x4 matrix and then normalizes the result.
Name | Type | Description |
---|---|---|
m | Matrix4 | The matrix. |
A reference to this vector.
- Type:
- Vector3
unproject(camera) → {Vector3}
Unprojects this vector from the camera's normalized device coordinate (NDC) space into world space.
Name | Type | Description |
---|---|---|
camera | Camera | The camera. |
A reference to this vector.
- Type:
- Vector3