Vector3

Class representing a 3D vector.

Constructor

new Vector3(xopt, yopt, zopt)

Constructs a new 3D vector.

Parameters:
NameTypeAttributesDefaultDescription
xnumber<optional>
0

The x value of this vector.

ynumber<optional>
0

The y value of this vector.

znumber<optional>
0

The z value of this vector.

Members

(readonly) isVector3 :boolean

This flag can be used for type testing.

Type:
  • boolean
Default Value
  • true

x :number

The x value of this vector.

Type:
  • number

y :number

The y value of this vector.

Type:
  • number

z :number

The z value of this vector.

Type:
  • number

Methods

add(v) → {Vector3}

Adds the given vector to this instance.

Parameters:
NameTypeDescription
vVector3

The vector to add.

Returns:

A reference to this vector.

Type: 
Vector3

addScalar(s) → {Vector3}

Adds the given scalar value to all components of this instance.

Parameters:
NameTypeDescription
snumber

The scalar to add.

Returns:

A reference to this vector.

Type: 
Vector3

addScaledVector(v, s) → {Vector3}

Adds the given vector scaled by the given factor to this instance.

Parameters:
NameTypeDescription
vVector3 | Vector4

The vector.

snumber

The factor that scales v.

Returns:

A reference to this vector.

Type: 
Vector3

addVectors(a, b) → {Vector3}

Adds the given vectors and stores the result in this instance.

Parameters:
NameTypeDescription
aVector3

The first vector.

bVector3

The second vector.

Returns:

A reference to this vector.

Type: 
Vector3

angleTo(v) → {number}

Returns the angle between the given vector and this instance in radians.

Parameters:
NameTypeDescription
vVector3

The vector to compute the angle with.

Returns:

The angle in radians.

Type: 
number

applyMatrix3(m) → {Vector3}

Multiplies this vector with the given 3x3 matrix.

Parameters:
NameTypeDescription
mMatrix3

The 3x3 matrix.

Returns:

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.

Parameters:
NameTypeDescription
mMatrix4

The matrix to apply.

Returns:

A reference to this vector.

Type: 
Vector3

applyQuaternion(q) → {Vector3}

Applies the given Quaternion to this vector.

Parameters:
NameTypeDescription
qQuaternion

The Quaternion.

Returns:

A reference to this vector.

Type: 
Vector3

clone() → {Vector3}

Returns a new vector with copied values from this instance.

Returns:

A clone of this instance.

Type: 
Vector3

copy(v) → {Vector3}

Copies the values of the given vector to this instance.

Parameters:
NameTypeDescription
vVector3

The vector to copy.

Returns:

A reference to this vector.

Type: 
Vector3

cross(v) → {Vector3}

Calculates the cross product of the given vector with this instance.

Parameters:
NameTypeDescription
vVector3

The vector to compute the cross product with.

Returns:

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.

Parameters:
NameTypeDescription
aVector3

The first vector.

bVector3

The second vector.

Returns:

A reference to this vector.

Type: 
Vector3

distanceTo(v) → {number}

Computes the distance from the given vector to this instance.

Parameters:
NameTypeDescription
vVector3

The vector to compute the distance to.

Returns:

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.

Parameters:
NameTypeDescription
vVector3

The vector to compute the squared distance to.

Returns:

The squared distance.

Type: 
number

dot(v) → {number}

Calculates the dot product of the given vector with this instance.

Parameters:
NameTypeDescription
vVector3

The vector to compute the dot product with.

Returns:

The result of the dot product.

Type: 
number

equals(v) → {boolean}

Returns true if this vector is equal with the given one.

Parameters:
NameTypeDescription
vVector3

The vector to test for equality.

Returns:

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 ].

Parameters:
NameTypeAttributesDefaultDescription
arrayArray.<number>

An array holding the vector component values.

offsetnumber<optional>
0

The offset into the array.

denormalizeboolean<optional>
false

If true, denormalize the values, and array should be a typed array.

Returns:

A reference to this vector.

Type: 
Vector3

getLength() → {number}

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

Returns:

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.

Returns:

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.

Parameters:
NameTypeDescription
vVector3

The vector to interpolate towards.

alphanumber

The interpolation factor, typically in the closed interval [0, 1].

Returns:

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.

Parameters:
NameTypeDescription
v1Vector3

The first vector.

v2Vector3

The second vector.

alphanumber

The interpolation factor, typically in the closed interval [0, 1].

Returns:

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.

Parameters:
NameTypeDescription
vVector3

The vector.

Returns:

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.

Parameters:
NameTypeDescription
vVector3

The vector.

Returns:

A reference to this vector.

Type: 
Vector3

multiply(v) → {Vector3}

Multiplies the given vector with this instance.

Parameters:
NameTypeDescription
vVector3

The vector to multiply.

Returns:

A reference to this vector.

Type: 
Vector3

multiplyScalar(scalar) → {Vector3}

Multiplies the given scalar value with all components of this instance.

Parameters:
NameTypeDescription
scalarnumber

The scalar to multiply.

Returns:

A reference to this vector.

Type: 
Vector3

negate() → {Vector3}

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

Returns:

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.

Parameters:
NameTypeAttributesDefaultDescription
thicknessnumber<optional>
1
Returns:

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.

Parameters:
NameTypeDescription
cameraCamera

The camera.

Returns:

A reference to this vector.

Type: 
Vector3

reflect(normal) → {Vector3}

Reflects this vector off a plane orthogonal to the given normal vector.

Parameters:
NameTypeDescription
normalVector3

The (normalized) normal vector.

Returns:

A reference to this vector.

Type: 
Vector3

set(x, y, z) → {Vector3}

Sets the vector components.

Parameters:
NameTypeDefaultDescription
xnumber0

The value of the x component.

ynumber0

The value of the y component.

znumber0

The value of the z component.

Returns:

A reference to this vector.

Type: 
Vector3

setFromMatrixColumn(m, index) → {Vector3}

Sets the vector components from the specified matrix column.

Parameters:
NameTypeDescription
mMatrix4

The 4x4 matrix.

indexnumber

The column index.

Returns:

A reference to this vector.

Type: 
Vector3

setFromMatrixPosition(m) → {Vector3}

Sets the vector components to the position elements of the given transformation matrix.

Parameters:
NameTypeDescription
mMatrix4

The 4x4 matrix.

Returns:

A reference to this vector.

Type: 
Vector3

setFromMatrixScale(m) → {Vector3}

Sets the vector components to the scale elements of the given transformation matrix.

Parameters:
NameTypeDescription
mMatrix4

The 4x4 matrix.

Returns:

A reference to this vector.

Type: 
Vector3

setFromSpherical(s) → {Vector3}

Sets the vector components from the given spherical coordinates.

Parameters:
NameTypeDescription
sSpherical

The spherical coordinates.

Returns:

A reference to this vector.

Type: 
Vector3

setScalar(scalar) → {Vector3}

Sets the vector components to the same value.

Parameters:
NameTypeDescription
scalarnumber

The value to set for all vector components.

Returns:

A reference to this vector.

Type: 
Vector3

sub(v) → {Vector3}

Subtracts the given vector from this instance.

Parameters:
NameTypeDescription
vVector3

The vector to subtract.

Returns:

A reference to this vector.

Type: 
Vector3

subVectors(a, b) → {Vector3}

Subtracts the given vectors and stores the result in this instance.

Parameters:
NameTypeDescription
aVector3

The first vector.

bVector3

The second vector.

Returns:

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.

Parameters:
NameTypeAttributesDefaultDescription
arrayArray.<number><optional>
[]

The target array holding the vector components.

offsetnumber<optional>
0

Index of the first element in the array.

normalizeboolean<optional>
false

if true, normalize the values, and array should be a typed array.

Returns:

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.

Parameters:
NameTypeDescription
mMatrix4

The matrix.

Returns:

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.

Parameters:
NameTypeDescription
cameraCamera

The camera.

Returns:

A reference to this vector.

Type: 
Vector3