Vector4

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

Math Class

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

Constructors

Constructor

new Vector4(_x?, _y?, _z?, _w?): Vector4

Parameters

_x?

number

_y?

number

_z?

number

_w?

number

Returns

Vector4

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.


w

w: number

the w value of this vector.

Default Value

0.


isVector4

isVector4: boolean

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

Methods

set()

set(x, y, z, w): Vector4

Sets the x, y, z and w components of this vector.

Parameters

x

number

y

number

z

number

w

number

Returns

Vector4


setScalar()

setScalar(scalar): Vector4

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

Parameters

scalar

number

Returns

Vector4


setX()

setX(x): Vector4

Replaces this vector’s x value with x.

Parameters

x

number

Returns

Vector4


setY()

setY(y): Vector4

Replaces this vector’s y value with y.

Parameters

y

number

Returns

Vector4


setZ()

setZ(z): Vector4

Replaces this vector’s z value with z.

Parameters

z

number

Returns

Vector4


setW()

setW(w): Vector4

Replaces this vector’s w value with w.

Parameters

w

number

Returns

Vector4


setComponent()

setComponent(index, value): Vector4

If index equals 0 set x to value. If index equals 1 set y to value. If index equals 2 set z to value. If index equals 3 set w to value.

Parameters

index

number

0, 1 or 2.

value

number

Returns

Vector4


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. If index equals 3 returns the w value.

Parameters

index

number

0, 1, 2 or 3.

Returns

number


clone()

clone(): Vector4

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

Returns

Vector4


cloneReadonly()

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

Returns

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


copy()

copy(v): Vector4

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

Parameters

v

Vector4

Returns

Vector4


add()

add(v): Vector4

Adds v to this vector.

Parameters

v

Vector4

Returns

Vector4


addScalar()

addScalar(s): Vector4

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

Parameters

s

number

Returns

Vector4


addVectors()

addVectors(a, b): Vector4

Sets this vector to a + b.

Parameters

a

Vector4

b

Vector4

Returns

Vector4


addScaledVector()

addScaledVector(v, s): Vector4

Adds the multiple of v and s to this vector.

Parameters

v

Vector4

s

number

Returns

Vector4


sub()

sub(v): Vector4

Subtracts v from this vector.

Parameters

v

Vector4

Returns

Vector4


subScalar()

subScalar(s): Vector4

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

Parameters

s

number

Returns

Vector4


subVectors()

subVectors(a, b): Vector4

Sets this vector to a - b.

Parameters

a

Vector4

b

Vector4

Returns

Vector4


multiplyScalar()

multiplyScalar(scalar): Vector4

Multiplies this vector by scalar s.

Parameters

scalar

number

Returns

Vector4


applyMatrix4()

applyMatrix4(m): Vector4

Multiplies this vector by 4 x 4 m.

Parameters

m

Matrix4

Returns

Vector4


divideScalar()

divideScalar(scalar): Vector4

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

Parameters

scalar

number

Returns

Vector4


min()

min(v): Vector4

If this vector’s x, y, z or w value is greater than the max vector’s x, y, z or w value, it is replaced by the corresponding value.

If this vector’s x, y, z or w value is less than the min vector’s x, y, z or w value, it is replaced by the corresponding value.

Parameters

v

Vector4

Returns

Vector4


max()

max(v): Vector4

If this vector’s x, y, z or w value is greater than the max vector’s x, y, z or w value, it is replaced by the corresponding value.

If this vector’s x, y, z or w value is less than the min vector’s x, y, z or w value, it is replaced by the corresponding value.

Parameters

v

Vector4

Returns

Vector4


clamp()

clamp(min, max): Vector4

If this vector’s x, y, z or w value is greater than the max vector’s x, y, z or w value, it is replaced by the corresponding value.

If this vector’s x, y, z or w value is less than the min vector’s x, y, z or w value, it is replaced by the corresponding value.

Parameters

min

Vector4

the minimum x, y, z and w values.

max

Vector4

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

Returns

Vector4


clampScalar()

clampScalar(minVal, maxVal): Vector4

If this vector’s x, y, z or w values are greater than the max value, they are replaced by the max value.

If this vector’s x, y, z or w values are less than the min value, they are replaced by the min value.

Parameters

minVal

number

maxVal

number

Returns

Vector4


clampLength()

clampLength(min, max): Vector4

If this vector’s length is greater than the max value, it is replaced by the max value.

If this vector’s length is less than the min value, it is replaced by the min value.

Parameters

min

number

the minimum x, y, z and w values.

max

number

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

Returns

Vector4


floor()

floor(): Vector4

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

Returns

Vector4


ceil()

ceil(): Vector4

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

Returns

Vector4


round()

round(): Vector4

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

Returns

Vector4


roundToZero()

roundToZero(): Vector4

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

Returns

Vector4


negate()

negate(): Vector4

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

Returns

Vector4


dot()

dot(v): number

Calculates the dot product of this vector and v.

Parameters

v

Vector4

Returns

number


lengthSq()

lengthSq(): number

Computes the square of the Euclidean length (straight-line length) from (0, 0, 0, 0) to (x, y, z, w). 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


length()

length(): number

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

Returns

number


manhattanLength()

manhattanLength(): number

Computes the Manhattan length of this vector.

Returns

number


normalize()

normalize(): Vector4

Converts 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

Vector4


setLength()

setLength(length): Vector4

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

Parameters

length

number

Returns

Vector4


lerp()

lerp(v, alpha): Vector4

Linearly interpolates 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

Vector4

Vector4 to interpolate towards.

alpha

number

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

Returns

Vector4


lerpVectors()

lerpVectors(v1, v2, alpha): Vector4

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

Vector4

the starting Vector4.

v2

Vector4

Vector4 to interpolate towards.

alpha

number

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

Returns

Vector4


equals()

equals(v): boolean

Checks for strict equality of this vector and v.

Parameters

v

Vector4

Returns

boolean


setAxisAngleFromQuaternion()

setAxisAngleFromQuaternion(q): Vector4

Sets the x, y and z components of this vector to the quaternion’s axis and w to the angle.

Parameters

q

Quaternion

a normalized Quaterion| Quaterion

Returns

Vector4


setAxisAngleFromRotationMatrix()

setAxisAngleFromRotationMatrix(m): Vector4

m a Matrix4 of which the upper left 3x3 matrix is a pure rotation matrix. Sets the x, y and z to the axis of rotation and w to the angle.

Parameters

m

Matrix4

Returns

Vector4


fromArray()

fromArray(array, offset?): Vector4

Sets this vector’s x value to be array[ offset + 0 ], y value to be array[ offset + 1 ] z value to be array[ offset + 2 ] and w value to be array[ offset + 3 ].

Parameters

array

ArrayLike<number>

the source array.

offset?

number

(optional) offset into the array.

Returns

Vector4

Default Value

0.


getNumberCount()

getNumberCount(): number

There are 4 elements in this vector.

Returns

number


toArray()

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

Returns an array [x, y, z, w], or copies x, y, z and w 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[]


fromBufferAttribute()

fromBufferAttribute(attribute, index): Vector4

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

Parameters

attribute

BufferAttribute

the source attribute.

index

number

index in the attribute.

Returns

Vector4