Vector3

Class representing 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:

There are other things a 3D vector can be used to represent, such as momentum vectors and so on, however these are the most common uses in Verge3D.

Example

var a = new v3d.Vector3(0, 1, 0); //no arguments; will be initialised to (0, 0, 0) var b = new v3d.Vector3(); var d = a.distanceTo(b);

Constructor

Vector3(x : Float, y : Float, z : Float)

x - the x value of the vector. Default is 0.
y - the y value of the vector. Default is 0.
z - the z value of the vector. Default is 0.

Creates a new Vector3.

Properties

.isVector3 : Boolean

Used to check whether this or derived classes are Vector3s. Default is true.

You should not change this, as it is used internally for optimisation.

.x : Float

.y : Float

.z : Float

Methods

.add (v : Vector3) : this

Adds v to this vector.

.addScalar (s : Float) : this

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

.addScaledVector (v : Vector3, s : Float) : this

Adds the multiple of v and s to this vector.

.addVectors (a : Vector3, b : Vector3) : this

Sets this vector to a + b.

.applyAxisAngle (axis : Vector3, angle : Float) : this

axis - A normalized Vector3.
angle - An angle in radians.

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

.applyEuler (euler : Euler) : this

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

.applyMatrix3 (m : Matrix3) : this

Multiplies this vector by m

.applyMatrix4 (m : Matrix4) : this

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

.applyQuaternion (quaternion : Quaternion) : this

Applies a Quaternion transform to this vector.

.angleTo (v : Vector3) : Float

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

.ceil () : this

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

.clamp (min : Vector3, max : Vector3) : this

min - the minimum x, y and z values.
max - the maximum x, y and z values in the desired range

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.

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

.clone () : Vector3

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

.copy (v : Vector3) : this

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

.cross (v : Vector3) : this

Sets this vector to cross product of itself and v.

.crossVectors (a : Vector3, b : Vector3) : this

Sets this vector to cross product of a and b.

.distanceTo (v : Vector3) : Float

Computes the distance from this vector to v.

.manhattanDistanceTo (v : Vector3) : Float

Computes the Manhattan distance from this vector to v.

.distanceToSquared (v : Vector3) : Float

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.

.divide (v : Vector3) : this

Divides this vector by v.

.divideScalar (s : Float) : this

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

.dot (v : Vector3) : Float

Calculate the dot product of this vector and v.

.equals (v : Vector3) : Boolean

Checks for strict equality of this vector and v.

.floor () : this

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

.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] and z value to be array[offset + 2].

.fromBufferAttribute (attribute : BufferAttribute, index : Integer) : this

attribute - the source attribute.
index - index in the attribute.

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

.getComponent (index : Integer) : Float

index - 0, 1 or 2.

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

.length () : Float

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

.manhattanLength () : Float

Computes the Manhattan length of this vector.

.lengthSq () : Float

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.

.lerp (v : Vector3, alpha : Float) : this

v - Vector3 to interpolate towards.
alpha - interpolation factor in the closed interval [0, 1].

Linearly interpolate between this vector and v, where alpha is the distance along the line - alpha = 0 will be this vector, and alpha = 1 will be v.

.lerpVectors (v1 : Vector3, v2 : Vector3, alpha : Float) : this

v1 - the starting Vector3.
v2 - Vector3 to interpolate towards.
alpha - interpolation factor in the closed interval [0, 1].

Sets this vector to be the vector linearly interpolated between v1 and v2 where alpha is the distance along the line connecting the two vectors - alpha = 0 will be v1, and alpha = 1 will be v2.

.max (v : Vector3) : this

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.

.min (v : Vector3) : this

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.

.multiply (v : Vector3) : this

Multiplies this vector by v.

.multiplyScalar (s : Float) : this

Multiplies this vector by scalar s.

.multiplyVectors (a : Vector3, b : Vector3) : this

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

.negate () : this

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

.normalize () : this

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

.project (camera : Camera) : this

camera — camera to use in the projection.

Projects the vector with the camera.

.projectOnPlane (planeNormal : Vector3) : this

planeNormal - A vector representing a plane normal.

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

.projectOnVector (Vector3 : Vector3) : this

Projects this vector onto another vector.

.reflect (normal : Vector3) : this

normal - the normal to the reflecting plane

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

.round () : this

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

.roundToZero () : this

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

.set (x : Float, y : Float, z : Float) : this

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

.setComponent (index : Integer, value : Float) : null

index - 0, 1 or 2.
value - Float

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

.setFromCylindrical (c : Cylindrical) : this

Sets this vector from the cylindrical coordinates c.

.setFromCylindricalCoords (radius : Float, theta : Float, y : Float) : this

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

.setFromMatrixColumn (matrix : Matrix4, index : Integer) : this

Sets this vector's x, y and z equal to the column of the matrix specified by the index.

.setFromMatrixPosition (m : Matrix4) : this

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

.setFromMatrixScale (m : Matrix4) : this

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

.setFromSpherical (s : Spherical) : this

Sets this vector from the spherical coordinates s.

.setFromSphericalCoords (radius : Float, phi : Float, theta : Float) : this

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

.setLength (l : Float) : this

Set this vector to the vector with the same direction as this one, but length l.

.setScalar (scalar : Float) : this

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

.setX (x : Float) : this

Replace this vector's x value with x.

.setY (y : Float) : this

Replace this vector's y value with y.

.setZ (z : Float) : this

Replace this vector's z value with z.

.sub (v : Vector3) : this

Subtracts v from this vector.

.subScalar (s : Float) : this

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

.subVectors (a : Vector3, b : Vector3) : this

Sets this vector to a - b.

.toArray (array : Array, offset : Integer) : Array

array - (optional) array to store the 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], or copies x, y and z into the provided array.

.transformDirection (m : Matrix4) : this

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

.unproject (camera : Camera) : this

camera — camera to use in the projection.

Unprojects the vector with the camera's projection matrix.

Source

For more info on how to obtain the source code of this module see this page.