-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathVec3.ts
463 lines (399 loc) · 12 KB
/
Vec3.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
import { Mat4 } from './Mat4'
import { Quat } from './Quat'
import { Camera } from '../core/camera/Camera'
/**
* Really basic 3D vector class used for vector calculations
* @see https://github.com/mrdoob/three.js/blob/dev/src/math/Vector3.js
* @see http://glmatrix.net/docs/vec3.js.html
*/
export class Vec3 {
/** The type of the {@link Vec3} */
type: string
/** X component of our {@link Vec3} */
private _x: number
/** Y component of our {@link Vec3} */
private _y: number
/** Z component of our {@link Vec3} */
private _z: number
/** function assigned to the {@link onChange} callback */
_onChangeCallback?(): void
/**
* Vec3 constructor
* @param x - X component of our {@link Vec3}
* @param y - Y component of our {@link Vec3}
* @param z - Z component of our {@link Vec3}
*/
constructor(x = 0, y = x, z = x) {
this.type = 'Vec3'
this._x = x
this._y = y
this._z = z
}
/**
* Get the X component of the {@link Vec3}
*/
get x(): number {
return this._x
}
/**
* Set the X component of the {@link Vec3}
* Can trigger {@link onChange} callback
* @param value - X component to set
*/
set x(value: number) {
const changed = value !== this._x
this._x = value
changed && this._onChangeCallback && this._onChangeCallback()
}
/**
* Get the Y component of the {@link Vec3}
*/
get y(): number {
return this._y
}
/**
* Set the Y component of the {@link Vec3}
* Can trigger {@link onChange} callback
* @param value - Y component to set
*/
set y(value: number) {
const changed = value !== this._y
this._y = value
changed && this._onChangeCallback && this._onChangeCallback()
}
/**
* Get the Z component of the {@link Vec3}
*/
get z(): number {
return this._z
}
/**
* Set the Z component of the {@link Vec3}
* Can trigger {@link onChange} callback
* @param value - Z component to set
*/
set z(value: number) {
const changed = value !== this._z
this._z = value
changed && this._onChangeCallback && this._onChangeCallback()
}
/**
* Called when at least one component of the {@link Vec3} has changed
* @param callback - callback to run when at least one component of the {@link Vec3} has changed
* @returns - our {@link Vec3}
*/
onChange(callback: () => void): Vec3 {
if (callback) {
this._onChangeCallback = callback
}
return this
}
/**
* Set the {@link Vec3} from values
* @param x - new X component to set
* @param y - new Y component to set
* @param z - new Z component to set
* @returns - this {@link Vec3} after being set
*/
set(x = 0, y = x, z = x): Vec3 {
this.x = x
this.y = y
this.z = z
return this
}
/**
* Add a {@link Vec3} to this {@link Vec3}
* @param vector - {@link Vec3} to add
* @returns - this {@link Vec3} after addition
*/
add(vector: Vec3 = new Vec3()): Vec3 {
this.x += vector.x
this.y += vector.y
this.z += vector.z
return this
}
/**
* Add a scalar to all the components of this {@link Vec3}
* @param value - number to add
* @returns - this {@link Vec3} after addition
*/
addScalar(value = 0): Vec3 {
this.x += value
this.y += value
this.z += value
return this
}
/**
* Subtract a {@link Vec3} from this {@link Vec3}
* @param vector - {@link Vec3} to subtract
* @returns - this {@link Vec3} after subtraction
*/
sub(vector: Vec3 = new Vec3()): Vec3 {
this.x -= vector.x
this.y -= vector.y
this.z -= vector.z
return this
}
/**
* Subtract a scalar to all the components of this {@link Vec3}
* @param value - number to subtract
* @returns - this {@link Vec3} after subtraction
*/
subScalar(value = 0): Vec3 {
this.x -= value
this.y -= value
this.z -= value
return this
}
/**
* Multiply a {@link Vec3} with this {@link Vec3}
* @param vector - {@link Vec3} to multiply with
* @returns - this {@link Vec3} after multiplication
*/
multiply(vector: Vec3 = new Vec3(1)): Vec3 {
this.x *= vector.x
this.y *= vector.y
this.z *= vector.z
return this
}
/**
* Multiply all components of this {@link Vec3} with a scalar
* @param value - number to multiply with
* @returns - this {@link Vec3} after multiplication
*/
multiplyScalar(value = 1): Vec3 {
this.x *= value
this.y *= value
this.z *= value
return this
}
/**
* Divide a {@link Vec3} with this {@link Vec3}
* @param vector - {@link Vec3} to divide with
* @returns - this {@link Vec3} after division
*/
divide(vector: Vec3 = new Vec3(1)): Vec3 {
this.x /= vector.x
this.y /= vector.y
this.z /= vector.z
return this
}
/**
* Divide all components of this {@link Vec3} with a scalar
* @param value - number to divide with
* @returns - this {@link Vec3} after division
*/
divideScalar(value = 1): Vec3 {
this.x /= value
this.y /= value
this.z /= value
return this
}
/**
* Copy a {@link Vec3} into this {@link Vec3}
* @param vector - {@link Vec3} to copy
* @returns - this {@link Vec3} after copy
*/
copy(vector: Vec3 = new Vec3()): Vec3 {
this.x = vector.x
this.y = vector.y
this.z = vector.z
return this
}
/**
* Clone this {@link Vec3}
* @returns - cloned {@link Vec3}
*/
clone(): Vec3 {
return new Vec3(this.x, this.y, this.z)
}
/**
* Apply max values to this {@link Vec3} components
* @param vector - {@link Vec3} representing max values
* @returns - {@link Vec3} with max values applied
*/
max(vector: Vec3 = new Vec3()): Vec3 {
this.x = Math.max(this.x, vector.x)
this.y = Math.max(this.y, vector.y)
this.z = Math.max(this.z, vector.z)
return this
}
/**
* Apply min values to this {@link Vec3} components
* @param vector - {@link Vec3} representing min values
* @returns - {@link Vec3} with min values applied
*/
min(vector: Vec3 = new Vec3()): Vec3 {
this.x = Math.min(this.x, vector.x)
this.y = Math.min(this.y, vector.y)
this.z = Math.min(this.z, vector.z)
return this
}
/**
* Clamp this {@link Vec3} components by min and max {@link Vec3} vectors
* @param min - minimum {@link Vec3} components to compare with
* @param max - maximum {@link Vec3} components to compare with
* @returns - clamped {@link Vec3}
*/
clamp(min: Vec3 = new Vec3(), max: Vec3 = new Vec3()): Vec3 {
this.x = Math.max(min.x, Math.min(max.x, this.x))
this.y = Math.max(min.y, Math.min(max.y, this.y))
this.z = Math.max(min.z, Math.min(max.z, this.z))
return this
}
/**
* Check if 2 {@link Vec3} are equal
* @param vector - {@link Vec3} to compare
* @returns - whether the {@link Vec3} are equals or not
*/
equals(vector: Vec3 = new Vec3()): boolean {
return this.x === vector.x && this.y === vector.y && this.z === vector.z
}
/**
* Get the square length of this {@link Vec3}
* @returns - square length of this {@link Vec3}
*/
lengthSq(): number {
return this.x * this.x + this.y * this.y + this.z * this.z
}
/**
* Get the length of this {@link Vec3}
* @returns - length of this {@link Vec3}
*/
length(): number {
return Math.sqrt(this.lengthSq())
}
/**
* Normalize this {@link Vec3}
* @returns - normalized {@link Vec3}
*/
normalize(): Vec3 {
// normalize
let len = this.lengthSq()
if (len > 0) {
len = 1 / Math.sqrt(len)
}
this.x *= len
this.y *= len
this.z *= len
return this
}
/**
* Calculate the dot product of 2 {@link Vec3}
* @param vector - {@link Vec3} to use for dot product
* @returns - dot product of the 2 {@link Vec3}
*/
dot(vector: Vec3 = new Vec3()): number {
return this.x * vector.x + this.y * vector.y + this.z * vector.z
}
/**
* Get the cross product of this {@link Vec3} with another {@link Vec3}
* @param vector - {@link Vec3} to use for cross product
* @returns - this {@link Vec3} after cross product
*/
cross(vector: Vec3 = new Vec3()): Vec3 {
return this.crossVectors(this, vector)
}
/**
* Set this {@link Vec3} as the result of the cross product of two {@link Vec3}
* @param a - first {@link Vec3} to use for cross product
* @param b - second {@link Vec3} to use for cross product
* @returns - this {@link Vec3} after cross product
*/
crossVectors(a: Vec3 = new Vec3(), b: Vec3 = new Vec3()): Vec3 {
const ax = a.x,
ay = a.y,
az = a.z
const bx = b.x,
by = b.y,
bz = b.z
this.x = ay * bz - az * by
this.y = az * bx - ax * bz
this.z = ax * by - ay * bx
return this
}
/**
* Calculate the linear interpolation of this {@link Vec3} by given {@link Vec3} and alpha, where alpha is the percent distance along the line
* @param vector - {@link Vec3} to interpolate towards
* @param alpha - interpolation factor in the [0, 1] interval
* @returns - this {@link Vec3} after linear interpolation
*/
lerp(vector: Vec3 = new Vec3(), alpha = 1): Vec3 {
this.x += (vector.x - this.x) * alpha
this.y += (vector.y - this.y) * alpha
this.z += (vector.z - this.z) * alpha
return this
}
/**
* Apply a {@link Mat4 | matrix} to a {@link Vec3}
* Useful to convert a position {@link Vec3} from plane local world to webgl space using projection view matrix for example
* Source code from: http://glmatrix.net/docs/vec3.js.html
* @param matrix - {@link Mat4 | matrix} to use
* @returns - this {@link Vec3} after {@link Mat4 | matrix} application
*/
applyMat4(matrix: Mat4): Vec3 {
const x = this._x,
y = this._y,
z = this._z
const mArray = matrix.elements
let w = mArray[3] * x + mArray[7] * y + mArray[11] * z + mArray[15]
w = w || 1
this.x = (mArray[0] * x + mArray[4] * y + mArray[8] * z + mArray[12]) / w
this.y = (mArray[1] * x + mArray[5] * y + mArray[9] * z + mArray[13]) / w
this.z = (mArray[2] * x + mArray[6] * y + mArray[10] * z + mArray[14]) / w
return this
}
/**
* Apply a {@link Quat | quaternion} (rotation in 3D space) to this {@link Vec3}
* @param quaternion - {@link Quat | quaternion} to use
* @returns - this {@link Vec3} with the transformation applied
*/
applyQuat(quaternion: Quat = new Quat()): Vec3 {
const x = this.x,
y = this.y,
z = this.z
const qx = quaternion.elements[0],
qy = quaternion.elements[1],
qz = quaternion.elements[2],
qw = quaternion.elements[3]
// calculate quat * vector
const ix = qw * x + qy * z - qz * y
const iy = qw * y + qz * x - qx * z
const iz = qw * z + qx * y - qy * x
const iw = -qx * x - qy * y - qz * z
// calculate result * inverse quat
this.x = ix * qw + iw * -qx + iy * -qz - iz * -qy
this.y = iy * qw + iw * -qy + iz * -qx - ix * -qz
this.z = iz * qw + iw * -qz + ix * -qy - iy * -qx
return this
}
/**
* Rotate a {@link Vec3} around and axis by a given angle
* @param axis - normalized {@link Vec3} around which to rotate
* @param angle - angle (in radians) to rotate
* @param quaternion - optional {@link Quat | quaternion} to use for rotation computations
* @returns - this {@link Vec3} with the rotation applied
*/
applyAxisAngle(axis = new Vec3(), angle = 0, quaternion = new Quat()) {
// https://github.com/mrdoob/three.js/blob/master/src/math/Vector3.js#L212
return this.applyQuat(quaternion.setFromAxisAngle(axis, angle))
}
/**
* Project a 3D coordinate {@link Vec3} to a 2D coordinate {@link Vec3}
* @param camera - {@link Camera} to use for projection
* @returns - projected {@link Vec3}
*/
project(camera: Camera): Vec3 {
this.applyMat4(camera.viewMatrix).applyMat4(camera.projectionMatrix)
return this
}
/**
* Unproject a 2D coordinate {@link Vec3} to 3D coordinate {@link Vec3}
* @param camera - {@link Camera} to use for projection
* @returns - unprojected {@link Vec3}
*/
unproject(camera: Camera): Vec3 {
this.applyMat4(camera.projectionMatrix.getInverse()).applyMat4(camera.modelMatrix)
return this
}
}