Capítulo 370 de 859

Chapter 370: Vector3

Core Idea

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:

  • A point in 3D space.
  • A direction and length in 3D space. In three.js the length will always be the Euclidean distance(straight-line distance) from (0, 0, 0) to (x, y, z) and the direction is also measured from (0, 0, 0) towards (x, y, z).
  • 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, however these are the most common uses in three.js.

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

Key Concepts

  • x : number: The x value of this vector.
  • y : number: The y value of this vector.
  • z : number: The z value of this vector.

Code Examples

const a = new THREE.Vector3( 0, 1, 0 );
//no arguments; will be initialised to (0, 0, 0)
const b = new THREE.Vector3( );
const d = a.distanceTo( b );
  • What it demonstrates: Typical usage of Vector3.

Reference Tables

Constructor

Signature
new Vector3( x : number, y : number, z : number )

Properties

PropertyDescription
.x : numberThe x value of this vector.
.y : numberThe y value of this vector.
.z : numberThe z value of this vector.

Methods

MethodDescription
.add( v : Vector3 ) : Vector3Adds the given vector to this instance.
.addScalar( s : number ) : Vector3Adds the given scalar value to all components of this instance.
`.addScaledVector( v : Vector3Vector4, s : number ) : Vector3`
.addVectors( a : Vector3, b : Vector3 ) : Vector3Adds the given vectors and stores the result in this instance.
.angleTo( v : Vector3 ) : numberReturns the angle between the given vector and this instance in radians.
.applyAxisAngle( axis : Vector3, angle : number ) : Vector3Applies a rotation specified by an axis and an angle to this vector.
.applyEuler( euler : Euler ) : Vector3Applies the given Euler rotation to this vector.
.applyMatrix3( m : Matrix3 ) : Vector3Multiplies this vector with the given 3x3 matrix.
.applyMatrix4( m : Matrix4 ) : Vector3Multiplies this vector (with an implicit 1 in the 4th dimension) by m, and divides by perspective.
.applyNormalMatrix( m : Matrix3 ) : Vector3Multiplies this vector by the given normal matrix and normalizes the result.
.applyQuaternion( q : Quaternion ) : Vector3Applies the given Quaternion to this vector.
.ceil() : Vector3The components of this vector are rounded up to the nearest integer value.
.clamp( min : Vector3, max : Vector3 ) : Vector3If 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 …
.clampLength( min : number, max : number ) : Vector3If 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( minVal : number, maxVal : number ) : Vector3If 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() : Vector3Returns a new vector with copied values from this instance.
.copy( v : Vector3 ) : Vector3Copies the values of the given vector to this instance.
.cross( v : Vector3 ) : Vector3Calculates the cross product of the given vector with this instance.
.crossVectors( a : Vector3, b : Vector3 ) : Vector3Calculates the cross product of the given vectors and stores the result in this instance.
.distanceTo( v : Vector3 ) : numberComputes the distance from the given vector to this instance.
.distanceToSquared( v : Vector3 ) : numberComputes the squared distance from the given vector to this instance. If you are just comparing the distance with another distance, you should compare the distance squared instead as it is slightly m…
.divide( v : Vector3 ) : Vector3Divides this instance by the given vector.
.divideScalar( scalar : number ) : Vector3Divides this vector by the given scalar.
.dot( v : Vector3 ) : numberCalculates the dot product of the given vector with this instance.
.equals( v : Vector3 ) : booleanReturns true if this vector is equal with the given one.
.floor() : Vector3The components of this vector are rounded down to the nearest integer value.
.fromArray( array : Array.<number>, offset : number ) : Vector3Sets this vector's x value to be array[ offset ], y value to be array[ offset + 1 ] and z value to be array[ offset + 2 ].
.fromBufferAttribute( attribute : BufferAttribute, index : number ) : Vector3Sets the components of this vector from the given buffer attribute.
.getComponent( index : number ) : numberReturns the value of the vector component which matches the given index.
.length() : numberComputes the Euclidean length (straight-line length) from (0, 0, 0) to (x, y, z).
.lengthSq() : numberComputes 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 sli…
.lerp( v : Vector3, alpha : number ) : Vector3Linearly interpolates between the given vector and this instance, where alpha is the percent distance along the line - alpha = 0 will be this vector, and alpha = 1 will be the given one.
.lerpVectors( v1 : Vector3, v2 : Vector3, alpha : number ) : Vector3Linearly interpolates between the given vectors, where alpha is the percent distance along the line - alpha = 0 will be first vector, and alpha = 1 will be the second one. The result is stored in thi…
.manhattanDistanceTo( v : Vector3 ) : numberComputes the Manhattan distance from the given vector to this instance.
.manhattanLength() : numberComputes the Manhattan length of this vector.
.max( v : Vector3 ) : Vector3If this vector's x, y or z value is less than the given vector's x, y or z value, replace that value with the corresponding max value.
.min( v : Vector3 ) : Vector3If this vector's x, y or z value is greater than the given vector's x, y or z value, replace that value with the corresponding min value.
.multiply( v : Vector3 ) : Vector3Multiplies the given vector with this instance.
.multiplyScalar( scalar : number ) : Vector3Multiplies the given scalar value with all components of this instance.
.multiplyVectors( a : Vector3, b : Vector3 ) : Vector3Multiplies the given vectors and stores the result in this instance.
.negate() : Vector3Inverts this vector - i.e. sets x = -x, y = -y and z = -z.
.normalize() : Vector3Converts this vector to a unit vector - that is, sets it equal to a vector with the same direction as this one, but with a vector length of 1.
.project( camera : Camera ) : Vector3Projects this vector from world space into the camera's normalized device coordinate (NDC) space.
.projectOnPlane( planeNormal : Vector3 ) : Vector3Projects this vector onto a plane by subtracting this vector projected onto the plane's normal from this vector.
.projectOnVector( v : Vector3 ) : Vector3Projects this vector onto the given one.
.random() : Vector3Sets each component of this vector to a pseudo-random value between 0 and 1, excluding 1.
.randomDirection() : Vector3Sets this vector to a uniformly random point on a unit sphere.
.reflect( normal : Vector3 ) : Vector3Reflects this vector off a plane orthogonal to the given normal vector.
.round() : Vector3The components of this vector are rounded to the nearest integer value
.roundToZero() : Vector3The components of this vector are rounded towards zero (up if negative, down if positive) to an integer value.
.set( x : number, y : number, z : number ) : Vector3Sets the vector components.
.setComponent( index : number, value : number ) : Vector3Allows to set a vector component with an index.
.setFromColor( c : Color ) : Vector3Sets the vector components from the RGB components of the given color.
.setFromCylindrical( c : Cylindrical ) : Vector3Sets the vector components from the given cylindrical coordinates.
.setFromCylindricalCoords( radius : number, theta : number, y : number ) : Vector3Sets the vector components from the given cylindrical coordinates.
.setFromEuler( e : Euler ) : Vector3Sets the vector components from the given Euler angles.
.setFromMatrix3Column( m : Matrix3, index : number ) : Vector3Sets the vector components from the specified matrix column.
.setFromMatrixColumn( m : Matrix4, index : number ) : Vector3Sets the vector components from the specified matrix column.
.setFromMatrixPosition( m : Matrix4 ) : Vector3Sets the vector components to the position elements of the given transformation matrix.
.setFromMatrixScale( m : Matrix4 ) : Vector3Sets the vector components to the scale elements of the given transformation matrix.
.setFromSpherical( s : Spherical ) : Vector3Sets the vector components from the given spherical coordinates.
.setFromSphericalCoords( radius : number, phi : number, theta : number ) : Vector3Sets the vector components from the given spherical coordinates.
.setLength( length : number ) : Vector3Sets this vector to a vector with the same direction as this one, but with the specified length.
.setScalar( scalar : number ) : Vector3Sets the vector components to the same value.
.setX( x : number ) : Vector3Sets the vector's x component to the given value.
.setY( y : number ) : Vector3Sets the vector's y component to the given value.
.setZ( z : number ) : Vector3Sets the vector's z component to the given value.
.sub( v : Vector3 ) : Vector3Subtracts the given vector from this instance.
.subScalar( s : number ) : Vector3Subtracts the given scalar value from all components of this instance.
.subVectors( a : Vector3, b : Vector3 ) : Vector3Subtracts the given vectors and stores the result in this instance.
.toArray( array : Array.<number>, offset : number ) : Array.<number>Writes the components of this vector to the given array. If no array is provided, the method returns a new instance.
.transformDirection( m : Matrix4 ) : Vector3Transforms the direction of this vector by a matrix (the upper left 3 x 3 subset of the given 4x4 matrix and then normalizes the result.
.unproject( camera : Camera ) : Vector3Unprojects this vector from the camera's normalized device coordinate (NDC) space into world space.

Key Takeaways

  1. Most relevant properties: x, y, z.
  2. Key methods: add, addScalar, addScaledVector, addVectors.

Connects To