Vector3

Class representing a 3D vector. A 3D vector is an ordered triplet of numbers (labeled x, y, and z).

Math Class

Class representing a 3D vector. A 3D vector is an ordered triplet of numbers (labeled x, y, and z).

Implements

  • Vector

Constructors

Constructor

new Vector3(_x?, _y?, _z?): Vector3

Parameters

_x?

number

_y?

number

_z?

number

Returns

Vector3

Properties

x

x: number

the x value of this vector.

Default Value

0.


y

y: number

the y value of this vector.

Default Value

0.


z

z: number

the z value of this vector.

Default Value

0.


isVector3

isVector3: boolean

Check the type whether it belongs to Vector3. This value should not be changed by user.

Methods

hasNan()

hasNan(): boolean

If NaN exists in three elements, return true.

Returns

boolean


set()

set(x, y, z): Vector3

Set the x, y and z components of this vector.

Parameters

x

number

y

number

z

number

Returns

Vector3


setScalar()

setScalar(scalar): Vector3

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

Parameters

scalar

number

Returns

Vector3


setX()

setX(x): Vector3

Replace this vector’s x value with x.

Parameters

x

number

Returns

Vector3


setY()

setY(y): Vector3

Replace this vector’s y value with y.

Parameters

y

number

Returns

Vector3


setZ()

setZ(z): Vector3

Replace this vector’s z value with z.

Parameters

z

number

Returns

Vector3


setComponent()

setComponent(index, value): Vector3

If index equals 0 set x to Float| value. If index equals 1 set y to Float| value. If index equals 2 set z to Float| value.

Parameters

index

number

value

number

Returns

Vector3

Implementation of

Vector.setComponent


getComponent()

getComponent(index): number

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

Parameters

index

number

0, 1 or 2.

Returns

number

Implementation of

Vector.getComponent


clone()

clone(): Vector3

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

Returns

Vector3

Implementation of

Vector.clone


cloneReadonly()

cloneReadonly(): Readonly<Pick<ReadonlyMarked, "_readonly_mark" | "cloneReadonly" | "clone" | "equals" | "getSerializeData" | "x" | "y" | "z" | "getNumberCount" | "toArray" | "dot" | "lengthSq" | "length" | "manhattanLength" | "distanceTo" | "distanceToSquared" | "manhattanDistanceTo" | "angleTo">>

Returns

Readonly<Pick<ReadonlyMarked, "_readonly_mark" | "cloneReadonly" | "clone" | "equals" | "getSerializeData" | "x" | "y" | "z" | "getNumberCount" | "toArray" | "dot" | "lengthSq" | "length" | "manhattanLength" | "distanceTo" | "distanceToSquared" | "manhattanDistanceTo" | "angleTo">>


copy()

copy(v): Vector3

Copies the values of the passed vector3’s x, y and z properties to this vector3.

Parameters

v

Vector3

Returns

Vector3

Implementation of

Vector.copy


add()

add(v): Vector3

Adds v to this vector.

Parameters

v

Vector3

Returns

Vector3

Implementation of

Vector.add


addScalar()

addScalar(s): Vector3

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

Parameters

s

number

Returns

Vector3


addVectors()

addVectors(a, b): Vector3

Sets this vector to a + b.

Parameters

a

Vector3

b

Vector3

Returns

Vector3

Implementation of

Vector.addVectors


addScaledVector()

addScaledVector(v, s): Vector3

Adds the multiple of v and s to this vector.

Parameters

v

Vector3

s

number

Returns

Vector3


sub()

sub(v): Vector3

Subtracts v from this vector.

Parameters

v

Vector3

Returns

Vector3

Implementation of

Vector.sub


subScalar()

subScalar(s): Vector3

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

Parameters

s

number

Returns

Vector3


subVectors()

subVectors(a, b): Vector3

Sets this vector to a - b.

Parameters

a

Vector3

b

Vector3

Returns

Vector3

Implementation of

Vector.subVectors


multiply()

multiply(v): Vector3

Multiplies this vector by v.

Parameters

v

Vector3

Returns

Vector3


multiplyScalar()

multiplyScalar(scalar): Vector3

Multiplies this vector by scalar s.

Parameters

scalar

number

Returns

Vector3

Implementation of

Vector.multiplyScalar


multiplyVectors()

multiplyVectors(a, b): Vector3

Sets this vector equal to a * b, component-wise.

Parameters

a

Vector3

b

Vector3

Returns

Vector3


applyMatrix3()

applyMatrix3(m): Vector3

Multiplies this vector by m

Parameters

m

Matrix3

Returns

Vector3


applyMatrix4()

applyMatrix4(m): Vector3

Multiplies this vector (with an implicit 1 in the 4th dimension) and m, and divides by perspective.

Parameters

m

Matrix4

Returns

Vector3


applyQuaternion()

applyQuaternion(q): Vector3

Applies a Quaternion transform to this vector.

Parameters

q

Quaternion

Returns

Vector3


project()

project(camera): Vector3

Projects this vector from world space into the camera’s normalized device coordinate (NDC) space.

Parameters

camera

Camera3D

camera to use in the projection.

Returns

Vector3


unproject()

unproject(camera): Vector3

Projects this vector from the camera’s normalized device coordinate (NDC) space into world space.

Parameters

camera

Camera3D

camera to use in the projection.

Returns

Vector3


transformDirection()

transformDirection(m): Vector3

Transforms the direction of this vector by a matrix (the upper left 3 x 3 subset of a m) and then normalizes the result.

Parameters

m

Matrix4

Returns

Vector3


divide()

divide(v): Vector3

Divides this vector by v.

Parameters

v

Vector3

Returns

Vector3


divideScalar()

divideScalar(scalar): Vector3

Divides this vector by scalar s. Sets vector to ( 0, 0, 0 ) if s = 0.

Parameters

scalar

number

Returns

Vector3

Implementation of

Vector.divideScalar


min()

min(v): Vector3

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.

Parameters

v

Vector3

Returns

Vector3


max()

max(v): Vector3

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.

Parameters

v

Vector3

Returns

Vector3


clamp()

clamp(min, max): Vector3

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.

Parameters

min

Vector3

the minimum x, y and z values.

max

Vector3

the maximum x, y and z values in the desired range.

Returns

Vector3


clampScalar()

clampScalar(minVal, maxVal): Vector3

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.

Parameters

minVal

number

maxVal

number

Returns

Vector3


clampLength()

clampLength(min, max): Vector3

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.

Parameters

min

number

the minimum value the length will be clamped to.

max

number

the maximum value the length will be clamped to.

Returns

Vector3


floor()

floor(): Vector3

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

Returns

Vector3


ceil()

ceil(): Vector3

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

Returns

Vector3


round()

round(): Vector3

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

Returns

Vector3


roundToZero()

roundToZero(): Vector3

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

Returns

Vector3


negate()

negate(): Vector3

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

Returns

Vector3

Implementation of

Vector.negate


dot()

dot(v): number

Calculate the dot product of this vector and v.

Parameters

v

Vector3

Returns

number

Implementation of

Vector.dot


lengthSq()

lengthSq(): 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

number

Implementation of

Vector.lengthSq


length()

length(): number

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

Returns

number

Implementation of

Vector.length


manhattanLength()

manhattanLength(): number

Computes the Manhattan length of this vector.

Returns

number


normalize()

normalize(): Vector3

Convert this vector to a unit vector

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

Returns

Vector3

Implementation of

Vector.normalize


setLength()

setLength(length): Vector3

Sets this vector to a vector with given length the same direction as this one.

Parameters

length

number

Returns

Vector3

Implementation of

Vector.setLength


lerp()

lerp(v, alpha): Vector3

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.

Parameters

v

Vector3

Vector3 to interpolate towards.

alpha

number

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

Returns

Vector3

Implementation of

Vector.lerp


lerpVectors()

lerpVectors(v1, v2, alpha): Vector3

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.

Parameters

v1

Vector3

the starting Vector3.

v2

Vector3

Vector3 to interpolate towards.

alpha

number

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

Returns

Vector3


cross()

cross(v): Vector3

Sets this vector to cross product of itself and v.

Parameters

v

Vector3

Returns

Vector3


crossVectors()

crossVectors(a, b): Vector3

Sets this vector to cross product of a and b.

Parameters

a

Vector3

b

Vector3

Returns

Vector3


projectOnVector()

projectOnVector(vector): Vector3

Projects this vector onto vector.

Parameters

vector

Vector3

Returns

Vector3


projectOnPlane()

projectOnPlane(planeNormal): Vector3

Projects this vector onto a plane by subtracting this vector projected onto the plane’s normal from this vector.

Parameters

planeNormal

Vector3

A vector representing a plane normal.

Returns

Vector3


reflect()

reflect(normal): Vector3

Reflect this vector off of plane orthogonal to normal. Normal is assumed to have unit length.

Parameters

normal

Vector3

the normal to the reflecting plane.

Returns

Vector3


angleTo()

angleTo(v): number

Returns the angle between this vector and vector v in radians.

Parameters

v

Vector3

Returns

number


distanceTo()

distanceTo(v): number

Computes the distance from this vector to v.

Parameters

v

Vector3

Returns

number

Implementation of

Vector.distanceTo


distanceToSquared()

distanceToSquared(v): number

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.

Parameters

v

Vector3

Returns

number

Implementation of

Vector.distanceToSquared


manhattanDistanceTo()

manhattanDistanceTo(v): number

Computes the Manhattan distance from this vector to v.

Parameters

v

Vector3

Returns

number


setFromSpherical()

setFromSpherical(s): Vector3

Sets this vector from the spherical coordinates Spherical| s.

Parameters

s

Spherical

Returns

Vector3


setFromSphericalCoords()

setFromSphericalCoords(radius, phi, theta): Vector3

Sets this vector from the spherical coordinates Spherical| radius, Spherical| phi and Spherical| theta.

Parameters

radius

number

phi

number

theta

number

Returns

Vector3


setFromCylindrical()

setFromCylindrical(c): Vector3

Sets this vector from the cylindrical coordinates Cylindrical| c.

Parameters

c

Cylindrical

Returns

Vector3


setFromCylindricalCoords()

setFromCylindricalCoords(radius, theta, y): Vector3

Sets this vector from the cylindrical coordinates Cylindrical| radius, Cylindrical| theta and Cylindrical| y.

Parameters

radius

number

theta

number

y

number

Returns

Vector3


setFromMatrixPosition()

setFromMatrixPosition(m): Vector3

Sets this vector to the position elements of the transformation matrix m.

Parameters

m

Matrix4

Returns

Vector3


setFromMatrixColumn()

setFromMatrixColumn(matrix, index): Vector3

Sets this vector’s x, y and z components from Integer| index column of matrix.

Parameters

matrix

Matrix4

index

number

Returns

Vector3


setFromMatrixScale()

setFromMatrixScale(m): Vector3

Sets this vector to the scale elements of the transformation matrix m.

Parameters

m

Matrix4

Returns

Vector3


equals()

equals(v): boolean

Checks for strict equality of this vector and v.

Parameters

v

Vector3

Returns

boolean

Implementation of

Vector.equals


fromArray()

fromArray(array, offset?): Vector3

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

Parameters

array

ArrayLike<number>

the source array.

offset?

number

( optional) offset into the array.

Returns

Vector3

Default Value

0.


getNumberCount()

getNumberCount(): number

There are 3 elements in this vector.

Returns

number


toArray()

toArray(array?, offset?): number[]

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

Parameters

array?

number[]

(optional) array to store this vector to. If this is not provided a new array will be created.

offset?

number

(optional) optional offset into the array.

Returns

number[]

Implementation of

Vector.toArray


fromBufferAttribute()

fromBufferAttribute(attribute, index): Vector3

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

Parameters

attribute

BufferAttribute

the source attribute.

index

number

index in the attribute.

Returns

Vector3