Vector4
Class representing a 4D vector. A 4D vector is an ordered quadruplet of numbers (labeled x, y, z, and w), which can be used to represent a number of things, such as:
- A point in 4D space.
- A direction and length in 4D space. In three.js the length will always be the Euclidean distance (straight-line distance) from
(0, 0, 0, 0)to(x, y, z, w)and the direction is also measured from(0, 0, 0, 0)towards(x, y, z, w). - Any arbitrary ordered quadruplet of numbers.
There are other things a 4D vector can be used to represent, however these are the most common uses in three.js.
Iterating through a Vector4 instance will yield its components (x, y, z, w)
in the corresponding order.
Code Example
const a = new THREE.Vector4( 0, 1, 0, 0 ); //no arguments; will be initialised
to (0, 0, 0, 1) const b = new THREE.Vector4( ); const d = a.dot( b );
Constructor
Vector4
function Vector4( x: Float, y: Float, z: Float, w: Float ): void;
x - the x value of this vector. Default is 0.
y - the y value of this vector. Default is 0.
z - the z value of this vector. Default is 0.
w - the w value of this vector. Default is 1.
Creates a new Vector4.
Properties
isVector4
isVector4: Boolean;
Read-only flag to check if a given object is of type Vector4.
x
x: Float;
y
y: Float;
z
z: Float;
w
w: Float;
width
width: Float;
Alias for .z.
height
height: Float;
Alias for .w.
Methods
add
function add( v: Vector4 ): this;
Adds v to this vector.
addScalar
function addScalar( s: Float ): this;
Adds the scalar value s to this vector's .x, .y, .z and .w values.
addScaledVector
function addScaledVector( v: Vector4, s: Float ): this;
Adds the multiple of v and s to this vector.
addVectors
function addVectors( a: Vector4, b: Vector4 ): this;
applyMatrix4
function applyMatrix4( m: Matrix4 ): this;
Multiplies this vector by 4 x 4 m.
ceil
function ceil( ): this;
The .x, .y, .z and .w components of this vector are rounded up to the nearest integer value.
clamp
function clamp( min: Vector4, max: Vector4 ): this;
.ector4 - the minimum .x, .y, .z and .w
values.
.ector4 - the maximum .x, .y, .z and .w
values in the desired range
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.
clampLength
function clampLength( min: Float, max: Float ): this;
min - the minimum value the length will be clamped to
max - the maximum value the length will be clamped to
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.
clampScalar
function clampScalar( min: Float, max: Float ): this;
min - the minimum value the components will be clamped to
max - the maximum value the components will be clamped to
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.
clone
function clone( ): Vector4;
Returns a new Vector4 with the same .x, .y, .z and .w values as this one.
copy
function copy( v: Vector4 ): this;
Copies the values of the passed Vector4's .x, .y, .z and .w properties to this Vector4.
divideScalar
function divideScalar( s: Float ): this;
Divides this vector by scalar s.
dot
function dot( v: Vector4 ): Float;
Calculates the dot product of this vector and v.
equals
function equals( v: Vector4 ): Boolean;
Returns true if the components of this vector and v
are strictly equal; false otherwise.
floor
function floor( ): this;
The components of this vector are rounded down to the nearest integer value.
fromArray
function fromArray( array: Array, offset: Integer ): this;
array - the source array.
offset - (optional) offset into the array. Default is 0.
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 ].
fromBufferAttribute
function fromBufferAttribute( attribute: BufferAttribute, index: Integer ):
this;
attribute - the source attribute.
index - index in the attribute.
Sets this vector's .x, .y, .z and .w values from the .ufferAttribute.
getComponent
function getComponent( index: Integer ): Float;
index - 0, 1, 2 or 3.
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.
length
function length( ): Float;
Computes the Euclidean length
(straight-line length) from (0, 0, 0, 0) to (x, y, z, w).
manhattanLength
function manhattanLength( ): Float;
Computes the Manhattan length of this vector.
lengthSq
function lengthSq( ): Float;
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.
lerp
function lerp( v: Vector4, alpha: Float ): this;
v - Vector4 to interpolate
towards.
alpha - interpolation factor, typically in the closed interval [0, 1].
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.
lerpVectors
function lerpVectors( v1: Vector4, v2: Vector4, alpha: Float ): this;
v1 - the starting Vector4.
v2 - Vector4 to interpolate
towards.
alpha - interpolation factor, typically in the closed interval [0, 1].
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.
negate
function negate( ): this;
Inverts this vector - i.e. sets x = -x, y = -y, z = -z and w = -w.
normalize
function normalize( ): this;
Converts this vector to a unit vector - that is, sets it equal to a vector with the same direction as this one, but .length 1.
max
function max( v: Vector4 ): this;
If this vector's x, y, z or w value is less than v's x, y, z or w value, replace that value with the corresponding max value.
min
function min( v: Vector4 ): this;
If this vector's x, y, z or w value is greater than v's x, y, z or w value, replace that value with the corresponding min value.
multiply
function multiply( v: Vector4 ): this;
Multiplies this vector by v.
multiplyScalar
function multiplyScalar( s: Float ): this;
Multiplies this vector by scalar s.
round
function round( ): this;
The components of this vector are rounded to the nearest integer value.
roundToZero
function roundToZero( ): this;
The components of this vector are rounded towards zero (up if negative, down if positive) to an integer value.
set
function set( x: Float, y: Float, z: Float, w: Float ): this;
Sets the .x, .y, .z and .w components of this vector.
setAxisAngleFromQuaternion
function setAxisAngleFromQuaternion( q: Quaternion ): this;
q - a normalized Quaternion
Sets the .x, .y and .z components of this vector to the quaternion's axis and .w to the angle.
setAxisAngleFromRotationMatrix
function setAxisAngleFromRotationMatrix( m: Matrix4 ): this;
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.
setComponent
function setComponent( index: Integer, value: Float ): this;
index - 0, 1, 2 or 3.
value - Float
If index equals 0 set .x to .loat.
If index equals 1 set .y to .loat.
If index equals 2 set .z to .loat.
If index equals 3 set .w to .loat.
setLength
function setLength( l: Float ): this;
Sets this vector to a vector with the same direction as this one, but .length .loat.
setScalar
function setScalar( scalar: Float ): this;
Sets the .x, .y, .z and .w values of this vector both equal to .loat.
setX
function setX( x: Float ): this;
Replaces this vector's .x value with .loat.
setY
function setY( y: Float ): this;
Replaces this vector's .y value with .loat.
setZ
function setZ( z: Float ): this;
Replaces this vector's .z value with .loat.
setW
function setW( w: Float ): this;
Replaces this vector's .w value with .loat.
sub
function sub( v: Vector4 ): this;
Subtracts v from this vector.
subScalar
function subScalar( s: Float ): this;
Subtracts .loat from this vector's .x, .y, .z and .w components.
subVectors
function subVectors( a: Vector4, b: Vector4 ): this;
toArray
function toArray( array: Array, offset: Integer ): Array;
array - (optional) array to store this vector to. If this is not
provided, a new array will be created.
offset - (optional) optional offset into the array.
Returns an array [x, y, z, w], or copies x, y, z and w into the provided array.
random
function random( ): this;
Sets each component of this vector to a pseudo-random value between 0 and
1, excluding 1.