UNPKG

5.72 kBTypeScriptView Raw
1import { BufferAttribute } from "../core/BufferAttribute.js";
2import { Matrix4 } from "./Matrix4.js";
3import { QuaternionLike } from "./Quaternion.js";
4
5export type Vector4Tuple = [number, number, number, number];
6
7export interface Vector4Like {
8 readonly x: number;
9 readonly y: number;
10 readonly z: number;
11 readonly w: number;
12}
13
14/**
15 * 4D vector.
16 */
17export class Vector4 {
18 constructor(x?: number, y?: number, z?: number, w?: number);
19
20 /**
21 * @default 0
22 */
23 x: number;
24
25 /**
26 * @default 0
27 */
28 y: number;
29
30 /**
31 * @default 0
32 */
33 z: number;
34
35 /**
36 * @default 0
37 */
38 w: number;
39
40 width: number;
41 height: number;
42 readonly isVector4: true;
43
44 /**
45 * Sets value of this vector.
46 */
47 set(x: number, y: number, z: number, w: number): this;
48
49 /**
50 * Sets all values of this vector.
51 */
52 setScalar(scalar: number): this;
53
54 /**
55 * Sets X component of this vector.
56 */
57 setX(x: number): this;
58
59 /**
60 * Sets Y component of this vector.
61 */
62 setY(y: number): this;
63
64 /**
65 * Sets Z component of this vector.
66 */
67 setZ(z: number): this;
68
69 /**
70 * Sets w component of this vector.
71 */
72 setW(w: number): this;
73
74 setComponent(index: number, value: number): this;
75
76 getComponent(index: number): number;
77
78 /**
79 * Clones this vector.
80 */
81 clone(): this;
82
83 /**
84 * Copies value of v to this vector.
85 */
86 copy(v: Vector4Like): this;
87
88 /**
89 * Adds v to this vector.
90 */
91 add(v: Vector4Like): this;
92
93 addScalar(scalar: number): this;
94
95 /**
96 * Sets this vector to a + b.
97 */
98 addVectors(a: Vector4Like, b: Vector4Like): this;
99
100 addScaledVector(v: Vector4Like, s: number): this;
101 /**
102 * Subtracts v from this vector.
103 */
104 sub(v: Vector4Like): this;
105
106 subScalar(s: number): this;
107
108 /**
109 * Sets this vector to a - b.
110 */
111 subVectors(a: Vector4Like, b: Vector4Like): this;
112
113 multiply(v: Vector4Like): this;
114
115 /**
116 * Multiplies this vector by scalar s.
117 */
118 multiplyScalar(s: number): this;
119
120 applyMatrix4(m: Matrix4): this;
121
122 divide(v: Vector4Like): this;
123
124 /**
125 * Divides this vector by scalar s.
126 * Set vector to ( 0, 0, 0 ) if s == 0.
127 */
128 divideScalar(s: number): this;
129
130 /**
131 * http://www.euclideanspace.com/maths/geometry/rotations/conversions/quaternionToAngle/index.htm
132 * @param q is assumed to be normalized
133 */
134 setAxisAngleFromQuaternion(q: QuaternionLike): this;
135
136 /**
137 * http://www.euclideanspace.com/maths/geometry/rotations/conversions/matrixToAngle/index.htm
138 * @param m assumes the upper 3x3 of m is a pure rotation matrix (i.e, unscaled)
139 */
140 setAxisAngleFromRotationMatrix(m: Matrix4): this;
141
142 /**
143 * Sets this vector to the position elements of the
144 * [transformation matrix]{@link https://en.wikipedia.org/wiki/Transformation_matrix} m.
145 */
146 setFromMatrixPosition(m: Matrix4): this;
147
148 min(v: Vector4Like): this;
149 max(v: Vector4Like): this;
150 clamp(min: Vector4Like, max: Vector4Like): this;
151 clampScalar(min: number, max: number): this;
152 floor(): this;
153 ceil(): this;
154 round(): this;
155 roundToZero(): this;
156
157 /**
158 * Inverts this vector.
159 */
160 negate(): this;
161
162 /**
163 * Computes dot product of this vector and v.
164 */
165 dot(v: Vector4Like): number;
166
167 /**
168 * Computes squared length of this vector.
169 */
170 lengthSq(): number;
171
172 /**
173 * Computes length of this vector.
174 */
175 length(): number;
176
177 /**
178 * Computes the Manhattan length of this vector.
179 *
180 * see {@link http://en.wikipedia.org/wiki/Taxicab_geometry|Wikipedia: Taxicab Geometry}
181 */
182 manhattanLength(): number;
183
184 /**
185 * Normalizes this vector.
186 */
187 normalize(): this;
188
189 /**
190 * Normalizes this vector and multiplies it by l.
191 */
192 setLength(length: number): this;
193
194 /**
195 * Linearly interpolate between this vector and v with alpha factor.
196 */
197 lerp(v: Vector4Like, alpha: number): this;
198
199 lerpVectors(v1: Vector4Like, v2: Vector4Like, alpha: number): this;
200
201 /**
202 * Checks for strict equality of this vector and v.
203 */
204 equals(v: Vector4Like): boolean;
205
206 /**
207 * Sets this vector's x, y, z and w value from the provided array or array-like.
208 * @param array the source array or array-like.
209 * @param offset (optional) offset into the array. Default is 0.
210 */
211 fromArray(array: number[] | ArrayLike<number>, offset?: number): this;
212
213 /**
214 * Returns an array [x, y, z, w], or copies x, y, z and w into the provided array.
215 * @param array (optional) array to store the vector to. If this is not provided, a new array will be created.
216 * @param offset (optional) optional offset into the array.
217 * @return The created or provided array.
218 */
219 toArray(array?: number[], offset?: number): number[];
220 toArray(array?: Vector4Tuple, offset?: 0): Vector4Tuple;
221
222 /**
223 * Copies x, y, z and w into the provided array-like.
224 * @param array array-like to store the vector to.
225 * @param offset (optional) optional offset into the array-like.
226 * @return The provided array-like.
227 */
228 toArray(array: ArrayLike<number>, offset?: number): ArrayLike<number>;
229
230 fromBufferAttribute(attribute: BufferAttribute, index: number): this;
231
232 /**
233 * Sets this vector's x, y, z and w from Math.random
234 */
235 random(): this;
236
237 /**
238 * Iterating through a Vector4 instance will yield its components (x, y, z, w) in the corresponding order.
239 */
240 [Symbol.iterator](): Iterator<number>;
241}