# 四元数（Quaternion）

## 代码示例

``` const quaternion = new v3d.Quaternion(); quaternion.setFromAxisAngle(new v3d.Vector3(0, 1, 0), Math.PI / 2); const vector = new v3d.Vector3(1, 0, 0); vector.applyQuaternion(quaternion); ```

x - x 坐标
y - y 坐标
z - z 坐标
w - w 坐标

## 方法

v - 用于进行比较的四元数。

### # .fromArray (array : Array, offset : Integer) : Quaternion

array - 用于构造四元数的形如(x, y, z, w)的数组。
offset - （可选）数组的偏移量。（译者注：使用数组中从第offset元素算起的四个元素）

### # .normalize () : Quaternion

Normalizes（归一化）四元数 —— 即计算与该四元数具有相同旋转、但长度为1的四元数。

### # .premultiply (q : Quaternion) : Quaternion

Pre-multiplies this quaternion by q.

### # .rotateTowards (q : Quaternion, step : Float) : Quaternion

q - The target quaternion.
step - The angular step in radians.

Rotates this quaternion by a given angular step to the defined quaternion q. The method ensures that the final quaternion will not overshoot q.

### # .slerp (qb : Quaternion, t : Float) : Quaternion

qb - The other quaternion rotation
t - interpolation factor in the closed interval [0, 1].

Handles the spherical linear interpolation between quaternions. t represents the amount of rotation between this quaternion (where t is 0) and qb (where t is 1). This quaternion is set to the result. Also see the static version of the slerp below. ``` // rotate a mesh towards a target quaternion mesh.quaternion.slerp(endQuaternion, 0.01); ```

m的旋转分量中来设置该四元数。

### # .setFromUnitVectors (vFrom : Vector3, vTo : Vector3) : Quaternion

Sets this quaternion to the rotation required to rotate direction vector vFrom to direction vector vTo.
vFrom and vTo are assumed to be normalized.

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

array - （可选）存储该四元数的数组。若未指定该参数，则将创建一个新数组。
offset - （可选）若指定了该值，结果将会被拷贝到该 Array

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

attribute - 源 attribute。
index - attribute 中的索引。

attribute 中设置该四元数的xyzw属性。

## 静态方法

### # .slerp (qStart : Quaternion, qEnd : Quaternion, qTarget : Quaternion, t : Float) : Quaternion

qStart - The starting quaternion (where t is 0)
qEnd - The ending quaternion (where t is 1)
qTarget - The target quaternion that gets set with the result
t - interpolation factor in the closed interval [0, 1].

Unlike the normal method, the static version of slerp sets a target quaternion to the result of the slerp operation. ``` // Code setup const startQuaternion = new v3d.Quaternion().set(0, 0, 0, 1).normalize(); const endQuaternion = new v3d.Quaternion().set(1, 1, 1, 1).normalize(); let t = 0; // Update a mesh's rotation in the loop t = (t + 0.01) % 1; // constant angular momentum v3d.Quaternion.slerp(startQuaternion, endQuaternion, mesh.quaternion, t); ```

### # .slerpFlat (dst : Array, dstOffset : Integer, src0 : Array, srcOffset0 : Integer, src1 : Array, srcOffset1 : Integer, t : Float) : null

dst - The output array.
dstOffset - An offset into the output array.
src0 - The source array of the starting quaternion.
srcOffset0 - An offset into the array src0.
src1 - The source array of the target quatnerion.
srcOffset1 - An offset into the array src1.
t - Normalized interpolation factor (between 0 and 1).

Like the static slerp method above, but operates directly on flat arrays of numbers.