Represents 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:

* A point in 3D space.
* A direction and length in 3D space.
* Any arbitrary ordered triplet of numbers.

There are other things a 3D vector can be used to represent, such as momentum vectors and so on.

Iterating through a Vector3 instance will yield its components (x, y, z) in the corresponding order.

Example

const a = new Vector3( 0, 1, 0 );

//no arguments; will be initialised to (0, 0, 0)
const b = new Vector3( );

const d = a.distanceTo( b );

Hierarchy

  • Base
    • Vector3

Implements

Constructors

  • new Vector3(x??: number, y??: number, z??: number): Vector3
  • Creates a new Vector3.

    Parameters

    • Optional x: number = 0

      The x value of this vector.

    • Optional y: number = 0

      The y value of this vector.

    • Optional z: number = 0

      The z value of this vector.

    Returns Vector3

Properties

x: number

Default

0

y: number

Default

0

z: number

Default

0

Accessors

  • get isBox2(): boolean
  • Returns boolean

  • get isBox3(): boolean
  • Returns boolean

  • get isColor(): boolean
  • Returns boolean

  • get isCylindrical(): boolean
  • Returns boolean

  • get isEuler(): boolean
  • Returns boolean

  • get isLine3(): boolean
  • Returns boolean

  • get isMatrix3(): boolean
  • Returns boolean

  • get isMatrix4(): boolean
  • Returns boolean

  • get isPlane(): boolean
  • Returns boolean

  • get isQuaternion(): boolean
  • Returns boolean

  • get isRay(): boolean
  • Returns boolean

  • get isSphere(): boolean
  • Returns boolean

  • get isSpherical(): boolean
  • Returns boolean

  • get isTriangle(): boolean
  • Returns boolean

  • get isVector(): boolean
  • Read-only flag to check if a given object is of type Vector.

    Returns boolean

  • get isVector2(): boolean
  • Returns boolean

  • get isVector3(): boolean
  • Read-only flag to check if a given object is of type Vector3.

    Returns boolean

  • get isVector4(): boolean
  • Returns boolean

Methods

  • [iterator](): IterableIterator<number>
  • Returns IterableIterator<number>

  • Add a vector to this vector.

    Returns

    This instance.

    Parameters

    • v: Vector3

      The vector to add to this vector.

    Returns Vector3

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

    Returns

    This instance.

    Parameters

    • s: number

      The scalar

    Returns Vector3

  • Adds the multiple of v and s to this vector.

    Returns

    This instance.

    Parameters

    • v: Vector3

      The source vector.

    • s: number

      The scale factor.

    Returns Vector3

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

    Returns

    The angle.

    Parameters

    Returns number

  • Applies a rotation specified by an axis and an angle to this vector.

    Returns

    This instance.

    Parameters

    Returns Vector3

  • Applies euler transform to this vector by converting the Euler object to a Quaternion and applying.

    Returns

    This instance.

    Parameters

    Returns Vector3

  • Multiplies this vector by m

    Returns

    This instance.

    Parameters

    Returns Vector3

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

    Returns

    This instance.

    Parameters

    Returns Vector3

  • Multiplies this vector by normal matrix m and normalizes the result.

    Returns

    This instance.

    Parameters

    Returns Vector3

  • Applies a Quaternion transform to this vector.

    Returns

    This instance.

    Parameters

    Returns Vector3

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

    Returns

    This instance.

    Returns Vector3

  • Restrict this vector component values to the range of their respective min and max vector component values.

    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.

    Returns

    This instance.

    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

  • clampLength(min: number, max: number): Vector3
  • Restrict this vector length the range [min,max].

    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.

    Returns

    This instance.

    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

  • clampScalar(minVal: number, maxVal: number): Vector3
  • Restrict this vector component values to the range [minVal,maxVal].

    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

    This instance.

    Parameters

    • minVal: number

      The minimum component value

    • maxVal: number

      The maximum component value

    Returns Vector3

  • Create a new vector using the component values of this vector.

    Returns

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

    Returns Vector3

  • Copies the values of a vector3's x, y and z properties to this vector3.

    Returns

    This instance.

    Parameters

    • v: Vector3

      The vector to copy onto this vector instance.

    Returns Vector3

  • Sets this vector to the cross product of itself and v.

    Returns

    This instance.

    Parameters

    Returns Vector3

  • Sets this vector to cross product of a and b.

    Returns

    This instance.

    Parameters

    Returns Vector3

  • Computes the distance from this vector to v.

    Returns

    The distance.

    Parameters

    Returns number

  • distanceToSquared(v: Vector3): 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.

    Returns

    The squared distance.

    Parameters

    Returns number

  • Divides this vector by v.

    Returns

    This instance.

    Parameters

    Returns Vector3

  • divideScalar(scalar: number): Vector3
  • Divides this vector by scalar s. Sets vector to ( 0, 0, 0 ) if s = 0.

    Returns

    This instance.

    Parameters

    • scalar: number

    Returns Vector3

  • Calculate the dot product of this vector and v.

    Returns

    The dot product.

    Parameters

    Returns number

  • Checks for strict equality of this vector and v.

    Returns

    true if equal; false otherwise.

    Parameters

    Returns boolean

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

    Returns

    This instance.

    Returns Vector3

  • fromArray(array: number[], offset??: number): 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 ].

    Returns

    This instance.

    Parameters

    • array: number[]

      The source array.

    • Optional offset: number = 0

      Offset into the array.

    Returns Vector3

  • getComponent(index: 0 | 1 | 2): number
  • Get a component value by index, [x,y,z]. If index equals 0 returns the x value. If index equals 1 returns the y value. If index equals 2 returns the z value.

    Returns

    The x, y or z component specified by index.

    Parameters

    • index: 0 | 1 | 2

      Index of component to access.

    Returns number

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

    Returns

    The square-root of the sum of the components squared.

    Returns number

  • 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

    The sum of the components squared.

    Returns number

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

    Returns

    This instance.

    Parameters

    • v: Vector3

      The vector to interpolate towards.

    • alpha: number

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

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

    Returns

    This instance.

    Parameters

    • v1: Vector3

      The starting Vector3.

    • v2: Vector3

      The vector to interpolate towards.

    • alpha: number

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

    Returns Vector3

  • manhattanDistanceTo(v: Vector3): number
  • Computes the Manhattan distance from this vector to v.

    Returns

    The Manhattan distance.

    Parameters

    Returns number

  • manhattanLength(): number
  • Computes the Manhattan length of this vector.

    Returns

    The Manhattan length.

    Returns number

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

    Returns

    This instance.

    Parameters

    Returns Vector3

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

    Returns

    This instance.

    Parameters

    Returns Vector3

  • Multiplies this vector by v.

    Returns

    This instance.

    Parameters

    Returns Vector3

  • multiplyScalar(scalar: number): Vector3
  • Multiplies this vector by scalar s.

    Returns

    This instance.

    Parameters

    • scalar: number

    Returns Vector3

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

    Returns

    This instance.

    Parameters

    Returns Vector3

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

    Returns

    This instance.

    Returns 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 1.

    Returns

    This instance.

    Returns Vector3

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

    Returns

    This instance.

    Parameters

    • planeNormal: Vector3

      A vector representing a plane normal.

    Returns Vector3

  • Sets each component of this vector to a pseudo-random value between 0 and 1, excluding 1.

    Returns

    This instance.

    Returns Vector3

  • Sets each component of this vector to a pseudo-random value between 0 and 1, excluding 1.

    Returns

    This instance.

    Returns Vector3

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

    Returns

    This instance.

    Parameters

    • normal: Vector3

      A vector representing a plane normal.

    Returns Vector3

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

    Returns

    This instance.

    Returns Vector3

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

    Returns

    This instance.

    Returns Vector3

  • set(x: number, y: number, z?: number): Vector3
  • Sets the x, y and z components of this vector.

    Returns

    This instance.

    Parameters

    • x: number

      The x value of this vector.

    • y: number

      The y value of this vector.

    • Optional z: number

      The z value of this vector.

    Returns Vector3

  • setComponent(index: 0 | 1 | 2, value: number): Vector3
  • Update a component by index. If index equals 0 set x to value. If index equals 1 set y to value. If index equals 2 set z to value

    Returns

    This instance.

    Parameters

    • index: 0 | 1 | 2

      The component to update

    • value: number

      New value

    Returns Vector3

  • Sets this vector from the cylindrical coordinates c.

    Returns

    This instance.

    Parameters

    Returns Vector3

  • setFromCylindricalCoords(radius: number, theta: number, y: number): Vector3
  • Sets this vector from the cylindrical coordinates radius, theta and y.

    Returns

    This instance.

    Parameters

    • radius: number
    • theta: number
    • y: number

    Returns Vector3

  • Sets this vector's x, y and z components from an euler's state.

    Returns

    This instance.

    Parameters

    • e: Euler

      The euler from which to set this state.

    Returns Vector3

  • Sets this vector's x, y and z components from index column of matrix.

    Returns

    This instance.

    Parameters

    Returns Vector3

  • Sets this vector's x, y and z components from index column of matrix.

    Returns

    This instance.

    Parameters

    Returns Vector3

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

    Returns

    This instance.

    Parameters

    Returns Vector3

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

    Returns

    This instance.

    Parameters

    Returns Vector3

  • Sets this vector from the spherical coordinates s.

    Returns

    This instance.

    Parameters

    Returns Vector3

  • setFromSphericalCoords(radius: number, phi: number, theta: number): Vector3
  • Sets this vector from the spherical coordinates radius, phi and theta.

    Returns

    This instance.

    Parameters

    • radius: number
    • phi: number
    • theta: number

    Returns Vector3

  • setLength(length: number): Vector3
  • Set this vector to a vector with the same direction as this one, but the specified length.

    Returns

    This instance.

    Parameters

    • length: number

    Returns Vector3

  • setScalar(scalar: number): Vector3
  • Set the x, y and z values of this vector equal to scalar.

    Returns

    This instance.

    Parameters

    • scalar: number

      The scalar value

    Returns Vector3

  • Replace this vector's x value with x.

    Returns

    This instance.

    Parameters

    • x: number

      The new x value.

    Returns Vector3

  • Replace this vector's y value with y.

    Returns

    This instance.

    Parameters

    • y: number

      The new x value.

    Returns Vector3

  • Replace this vector's z value with z.

    Returns

    This instance.

    Parameters

    • z: number

      The new z value.

    Returns Vector3

  • Subtracts v from this vector.

    Returns

    This instance.

    Parameters

    • v: Vector3

      The vector to subtract.

    Returns Vector3

  • Subtracts s from this vector's x, y and z compnents.

    Returns

    This instance.

    Parameters

    • s: number

      The subtracting vector

    Returns Vector3

  • toArray(array?: number[] | Vector3Tuple, offset?: number): number[]
  • Returns an array [x, y, z], or copies x, y and z into the provided array.

    Returns

    Array with this vector compoonent values, [x,y,z].

    Parameters

    • array: number[] | Vector3Tuple = ...

      Array to store this vector to. If this is not provided a new array will be created.

    • offset: number = 0

      Optional offset into the array.

    Returns number[]

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

    Returns

    This instance.

    Parameters

    Returns Vector3