Capítulo 363 de 859

Chapter 363: Quaternion

Core Idea

Class for representing a Quaternion. Quaternions are used in three.js to represent rotations.

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

Note that three.js expects Quaternions to be normalized.

Key Concepts

  • isQuaternion : boolean (readonly): This flag can be used for type testing.
  • w : number: The w value of this quaternion.
  • x : number: The x value of this quaternion.
  • y : number: The y value of this quaternion.
  • z : number: The z value of this quaternion.

Code Examples

const quaternion = new THREE.Quaternion();
quaternion.setFromAxisAngle( new THREE.Vector3( 0, 1, 0 ), Math.PI / 2 );
const vector = new THREE.Vector3( 1, 0, 0 );
vector.applyQuaternion( quaternion );
  • What it demonstrates: Typical usage of Quaternion.

Reference Tables

Constructor

Signature
new Quaternion( x : number, y : number, z : number, w : number )

Properties

PropertyDescription
.isQuaternion : boolean (readonly)This flag can be used for type testing.
.w : numberThe w value of this quaternion.
.x : numberThe x value of this quaternion.
.y : numberThe y value of this quaternion.
.z : numberThe z value of this quaternion.

Methods

MethodDescription
.angleTo( q : Quaternion ) : numberReturns the angle between this quaternion and the given one in radians.
.clone() : QuaternionReturns a new quaternion with copied values from this instance.
.conjugate() : QuaternionReturns the rotational conjugate of this quaternion. The conjugate of a quaternion represents the same rotation in the opposite direction about the rotational axis.
.copy( quaternion : Quaternion ) : QuaternionCopies the values of the given quaternion to this instance.
.dot( v : Quaternion ) : numberCalculates the dot product of this quaternion and the given one.
.equals( quaternion : Quaternion ) : booleanReturns true if this quaternion is equal with the given one.
.fromArray( array : Array.<number>, offset : number ) : QuaternionSets this quaternion's components from the given array.
.fromBufferAttribute( attribute : BufferAttribute, index : number ) : QuaternionSets the components of this quaternion from the given buffer attribute.
.identity() : QuaternionSets this quaternion to the identity quaternion; that is, to the quaternion that represents "no rotation".
.invert() : QuaternionInverts this quaternion via Quaternion#conjugate. The quaternion is assumed to have unit length.
.length() : numberComputes the Euclidean length (straight-line length) of this quaternion, considered as a 4 dimensional vector.
.lengthSq() : numberComputes the squared Euclidean length (straight-line length) of this quaternion, considered as a 4 dimensional vector. This can be useful if you are comparing the lengths of two quaternions, as this …
.multiply( q : Quaternion ) : QuaternionMultiplies this quaternion by the given one.
.multiplyQuaternions( a : Quaternion, b : Quaternion ) : QuaternionMultiplies the given quaternions and stores the result in this instance.
.normalize() : QuaternionNormalizes this quaternion - that is, calculated the quaternion that performs the same rotation as this one, but has a length equal to 1.
.premultiply( q : Quaternion ) : QuaternionPre-multiplies this quaternion by the given one.
.random() : QuaternionSets this quaternion to a uniformly random, normalized quaternion.
.rotateTowards( q : Quaternion, step : number ) : QuaternionRotates this quaternion by a given angular step to the given quaternion. The method ensures that the final quaternion will not overshoot q.
.set( x : number, y : number, z : number, w : number ) : QuaternionSets the quaternion components.
.setFromAxisAngle( axis : Vector3, angle : number ) : QuaternionSets this quaternion from the given axis and angle.
.setFromEuler( euler : Euler, update : boolean ) : QuaternionSets this quaternion from the rotation specified by the given Euler angles.
.setFromRotationMatrix( m : Matrix4 ) : QuaternionSets this quaternion from the given rotation matrix.
.setFromUnitVectors( vFrom : Vector3, vTo : Vector3 ) : QuaternionSets this quaternion to the rotation required to rotate the direction vector vFrom to the direction vector vTo.
.slerp( qb : Quaternion, t : number ) : QuaternionPerforms a spherical linear interpolation between this quaternion and the target quaternion.
.slerpQuaternions( qa : Quaternion, qb : Quaternion, t : number ) : QuaternionPerforms a spherical linear interpolation between the given quaternions and stores the result in this quaternion.
.toArray( array : Array.<number>, offset : number ) : Array.<number>Writes the components of this quaternion to the given array. If no array is provided, the method returns a new instance.
.toJSON() : Array.<number>This methods defines the serialization result of this class. Returns the numerical elements of this quaternion in an array of format [x, y, z, w].

Key Takeaways

  1. Most relevant properties: isQuaternion, w, x, y.
  2. Key methods: angleTo, clone, conjugate, copy.

Connects To