UNPKG

5.44 kBTypeScriptView Raw
1import { BufferAttribute } from "../core/BufferAttribute.js";
2import { InterleavedBufferAttribute } from "../core/InterleavedBufferAttribute.js";
3import { Euler } from "./Euler.js";
4import { Matrix4 } from "./Matrix4.js";
5import { Vector3, Vector3Like } from "./Vector3.js";
6
7export interface QuaternionLike {
8 readonly x: number;
9 readonly y: number;
10 readonly z: number;
11 readonly w: number;
12}
13
14export type QuaternionTuple = [x: number, y: number, z: number, w: number];
15
16/**
17 * Implementation of a quaternion. This is used for rotating things without incurring in the dreaded gimbal lock issue, amongst other advantages.
18 *
19 * @example
20 * const quaternion = new THREE.Quaternion();
21 * quaternion.setFromAxisAngle( new THREE.Vector3( 0, 1, 0 ), Math.PI / 2 );
22 * const vector = new THREE.Vector3( 1, 0, 0 );
23 * vector.applyQuaternion( quaternion );
24 */
25export class Quaternion {
26 /**
27 * @param x x coordinate
28 * @param y y coordinate
29 * @param z z coordinate
30 * @param w w coordinate
31 */
32 constructor(x?: number, y?: number, z?: number, w?: number);
33
34 /**
35 * @default 0
36 */
37 x: number;
38
39 /**
40 * @default 0
41 */
42 y: number;
43
44 /**
45 * @default 0
46 */
47 z: number;
48
49 /**
50 * @default 1
51 */
52 w: number;
53 readonly isQuaternion: true;
54
55 /**
56 * Sets values of this quaternion.
57 */
58 set(x: number, y: number, z: number, w: number): this;
59
60 /**
61 * Clones this quaternion.
62 */
63 clone(): this;
64
65 /**
66 * Copies values of q to this quaternion.
67 */
68 copy(q: QuaternionLike): this;
69
70 /**
71 * Sets this quaternion from rotation specified by Euler angles.
72 */
73 setFromEuler(euler: Euler, update?: boolean): this;
74
75 /**
76 * Sets this quaternion from rotation specified by axis and angle.
77 * Adapted from http://www.euclideanspace.com/maths/geometry/rotations/conversions/angleToQuaternion/index.htm.
78 * Axis have to be normalized, angle is in radians.
79 */
80 setFromAxisAngle(axis: Vector3Like, angle: number): this;
81
82 /**
83 * Sets this quaternion from rotation component of m. Adapted from http://www.euclideanspace.com/maths/geometry/rotations/conversions/matrixToQuaternion/index.htm.
84 */
85 setFromRotationMatrix(m: Matrix4): this;
86 setFromUnitVectors(vFrom: Vector3, vTo: Vector3Like): this;
87 angleTo(q: Quaternion): number;
88 rotateTowards(q: Quaternion, step: number): this;
89
90 identity(): this;
91
92 /**
93 * Inverts this quaternion.
94 */
95 invert(): this;
96
97 conjugate(): this;
98 dot(v: Quaternion): number;
99 lengthSq(): number;
100
101 /**
102 * Computes length of this quaternion.
103 */
104 length(): number;
105
106 /**
107 * Normalizes this quaternion.
108 */
109 normalize(): this;
110
111 /**
112 * Multiplies this quaternion by b.
113 */
114 multiply(q: Quaternion): this;
115 premultiply(q: Quaternion): this;
116
117 /**
118 * Sets this quaternion to a x b
119 * Adapted from http://www.euclideanspace.com/maths/algebra/realNormedAlgebra/quaternions/code/index.htm.
120 */
121 multiplyQuaternions(a: Quaternion, b: Quaternion): this;
122
123 slerp(qb: Quaternion, t: number): this;
124 slerpQuaternions(qa: Quaternion, qb: Quaternion, t: number): this;
125 equals(v: Quaternion): boolean;
126
127 /**
128 * Sets this quaternion's x, y, z and w value from the provided array or array-like.
129 * @param array the source array or array-like.
130 * @param offset (optional) offset into the array. Default is 0.
131 */
132 fromArray(array: number[] | ArrayLike<number>, offset?: number): this;
133
134 /**
135 * Returns an array [x, y, z, w], or copies x, y, z and w into the provided array.
136 * @param array (optional) array to store the quaternion to. If this is not provided, a new array will be created.
137 * @param offset (optional) optional offset into the array.
138 * @return The created or provided array.
139 */
140 toArray(array?: number[], offset?: number): number[];
141 toArray(array?: QuaternionTuple, offset?: 0): QuaternionTuple;
142
143 /**
144 * Copies x, y, z and w into the provided array-like.
145 * @param array array-like to store the quaternion to.
146 * @param offset (optional) optional offset into the array.
147 * @return The provided array-like.
148 */
149 toArray(array: ArrayLike<number>, offset?: number): ArrayLike<number>;
150
151 /**
152 * This method defines the serialization result of Quaternion.
153 * @return The numerical elements of this quaternion in an array of format [x, y, z, w].
154 */
155 toJSON(): [number, number, number, number];
156
157 /**
158 * Sets x, y, z, w properties of this quaternion from the attribute.
159 * @param attribute the source attribute.
160 * @param index index in the attribute.
161 */
162 fromBufferAttribute(attribute: BufferAttribute | InterleavedBufferAttribute, index: number): this;
163
164 _onChange(callback: () => void): this;
165 _onChangeCallback: () => void;
166
167 static slerpFlat(
168 dst: number[],
169 dstOffset: number,
170 src0: number[],
171 srcOffset: number,
172 src1: number[],
173 stcOffset1: number,
174 t: number,
175 ): void;
176
177 static multiplyQuaternionsFlat(
178 dst: number[],
179 dstOffset: number,
180 src0: number[],
181 srcOffset: number,
182 src1: number[],
183 stcOffset1: number,
184 ): number[];
185
186 random(): this;
187
188 [Symbol.iterator](): Generator<number, void>;
189}