@threenative/physics 0.2.1 → 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,4631 +1,8 @@
1
- import { NavMeshQuery, Crowd, NavMesh, init } from 'recast-navigation';
1
+ import { Raw, NavMeshQuery, Crowd, NavMesh, init } from 'recast-navigation';
2
+ import { Vector3 } from 'three';
2
3
  import { generateSoloNavMesh } from 'recast-navigation/generators';
3
4
 
4
5
  // src/navigation/index.ts
5
-
6
- // ../../node_modules/.pnpm/three@0.185.1/node_modules/three/build/three.core.js
7
- var REVISION = "185";
8
- var WebGLCoordinateSystem = 2e3;
9
- var WebGPUCoordinateSystem = 2001;
10
- var _cache = {};
11
- function enhanceLogMessage(params) {
12
- const message = params[0];
13
- if (typeof message === "string" && message.startsWith("TSL:")) {
14
- const stackTrace = params[1];
15
- if (stackTrace && stackTrace.isStackTrace) {
16
- params[0] += " " + stackTrace.getLocation();
17
- } else {
18
- params[1] = 'Stack trace not available. Enable "THREE.Node.captureStackTrace" to capture stack traces.';
19
- }
20
- }
21
- return params;
22
- }
23
- function warn(...params) {
24
- params = enhanceLogMessage(params);
25
- const message = "THREE." + params.shift();
26
- {
27
- const stackTrace = params[0];
28
- if (stackTrace && stackTrace.isStackTrace) {
29
- console.warn(stackTrace.getError(message));
30
- } else {
31
- console.warn(message, ...params);
32
- }
33
- }
34
- }
35
- function warnOnce(...params) {
36
- const message = params.join(" ");
37
- if (message in _cache) return;
38
- _cache[message] = true;
39
- warn(...params);
40
- }
41
- function clamp(value, min, max) {
42
- return Math.max(min, Math.min(max, value));
43
- }
44
- (class _Vector2 {
45
- static {
46
- _Vector2.prototype.isVector2 = true;
47
- }
48
- /**
49
- * Constructs a new 2D vector.
50
- *
51
- * @param {number} [x=0] - The x value of this vector.
52
- * @param {number} [y=0] - The y value of this vector.
53
- */
54
- constructor(x = 0, y = 0) {
55
- this.x = x;
56
- this.y = y;
57
- }
58
- /**
59
- * Alias for {@link Vector2#x}.
60
- *
61
- * @type {number}
62
- */
63
- get width() {
64
- return this.x;
65
- }
66
- set width(value) {
67
- this.x = value;
68
- }
69
- /**
70
- * Alias for {@link Vector2#y}.
71
- *
72
- * @type {number}
73
- */
74
- get height() {
75
- return this.y;
76
- }
77
- set height(value) {
78
- this.y = value;
79
- }
80
- /**
81
- * Sets the vector components.
82
- *
83
- * @param {number} x - The value of the x component.
84
- * @param {number} y - The value of the y component.
85
- * @return {Vector2} A reference to this vector.
86
- */
87
- set(x, y) {
88
- this.x = x;
89
- this.y = y;
90
- return this;
91
- }
92
- /**
93
- * Sets the vector components to the same value.
94
- *
95
- * @param {number} scalar - The value to set for all vector components.
96
- * @return {Vector2} A reference to this vector.
97
- */
98
- setScalar(scalar) {
99
- this.x = scalar;
100
- this.y = scalar;
101
- return this;
102
- }
103
- /**
104
- * Sets the vector's x component to the given value
105
- *
106
- * @param {number} x - The value to set.
107
- * @return {Vector2} A reference to this vector.
108
- */
109
- setX(x) {
110
- this.x = x;
111
- return this;
112
- }
113
- /**
114
- * Sets the vector's y component to the given value
115
- *
116
- * @param {number} y - The value to set.
117
- * @return {Vector2} A reference to this vector.
118
- */
119
- setY(y) {
120
- this.y = y;
121
- return this;
122
- }
123
- /**
124
- * Allows to set a vector component with an index.
125
- *
126
- * @param {number} index - The component index. `0` equals to x, `1` equals to y.
127
- * @param {number} value - The value to set.
128
- * @return {Vector2} A reference to this vector.
129
- */
130
- setComponent(index, value) {
131
- switch (index) {
132
- case 0:
133
- this.x = value;
134
- break;
135
- case 1:
136
- this.y = value;
137
- break;
138
- default:
139
- throw new Error("THREE.Vector2: index is out of range: " + index);
140
- }
141
- return this;
142
- }
143
- /**
144
- * Returns the value of the vector component which matches the given index.
145
- *
146
- * @param {number} index - The component index. `0` equals to x, `1` equals to y.
147
- * @return {number} A vector component value.
148
- */
149
- getComponent(index) {
150
- switch (index) {
151
- case 0:
152
- return this.x;
153
- case 1:
154
- return this.y;
155
- default:
156
- throw new Error("THREE.Vector2: index is out of range: " + index);
157
- }
158
- }
159
- /**
160
- * Returns a new vector with copied values from this instance.
161
- *
162
- * @return {Vector2} A clone of this instance.
163
- */
164
- clone() {
165
- return new this.constructor(this.x, this.y);
166
- }
167
- /**
168
- * Copies the values of the given vector to this instance.
169
- *
170
- * @param {Vector2} v - The vector to copy.
171
- * @return {Vector2} A reference to this vector.
172
- */
173
- copy(v) {
174
- this.x = v.x;
175
- this.y = v.y;
176
- return this;
177
- }
178
- /**
179
- * Adds the given vector to this instance.
180
- *
181
- * @param {Vector2} v - The vector to add.
182
- * @return {Vector2} A reference to this vector.
183
- */
184
- add(v) {
185
- this.x += v.x;
186
- this.y += v.y;
187
- return this;
188
- }
189
- /**
190
- * Adds the given scalar value to all components of this instance.
191
- *
192
- * @param {number} s - The scalar to add.
193
- * @return {Vector2} A reference to this vector.
194
- */
195
- addScalar(s) {
196
- this.x += s;
197
- this.y += s;
198
- return this;
199
- }
200
- /**
201
- * Adds the given vectors and stores the result in this instance.
202
- *
203
- * @param {Vector2} a - The first vector.
204
- * @param {Vector2} b - The second vector.
205
- * @return {Vector2} A reference to this vector.
206
- */
207
- addVectors(a, b) {
208
- this.x = a.x + b.x;
209
- this.y = a.y + b.y;
210
- return this;
211
- }
212
- /**
213
- * Adds the given vector scaled by the given factor to this instance.
214
- *
215
- * @param {Vector2} v - The vector.
216
- * @param {number} s - The factor that scales `v`.
217
- * @return {Vector2} A reference to this vector.
218
- */
219
- addScaledVector(v, s) {
220
- this.x += v.x * s;
221
- this.y += v.y * s;
222
- return this;
223
- }
224
- /**
225
- * Subtracts the given vector from this instance.
226
- *
227
- * @param {Vector2} v - The vector to subtract.
228
- * @return {Vector2} A reference to this vector.
229
- */
230
- sub(v) {
231
- this.x -= v.x;
232
- this.y -= v.y;
233
- return this;
234
- }
235
- /**
236
- * Subtracts the given scalar value from all components of this instance.
237
- *
238
- * @param {number} s - The scalar to subtract.
239
- * @return {Vector2} A reference to this vector.
240
- */
241
- subScalar(s) {
242
- this.x -= s;
243
- this.y -= s;
244
- return this;
245
- }
246
- /**
247
- * Subtracts the given vectors and stores the result in this instance.
248
- *
249
- * @param {Vector2} a - The first vector.
250
- * @param {Vector2} b - The second vector.
251
- * @return {Vector2} A reference to this vector.
252
- */
253
- subVectors(a, b) {
254
- this.x = a.x - b.x;
255
- this.y = a.y - b.y;
256
- return this;
257
- }
258
- /**
259
- * Multiplies the given vector with this instance.
260
- *
261
- * @param {Vector2} v - The vector to multiply.
262
- * @return {Vector2} A reference to this vector.
263
- */
264
- multiply(v) {
265
- this.x *= v.x;
266
- this.y *= v.y;
267
- return this;
268
- }
269
- /**
270
- * Multiplies the given scalar value with all components of this instance.
271
- *
272
- * @param {number} scalar - The scalar to multiply.
273
- * @return {Vector2} A reference to this vector.
274
- */
275
- multiplyScalar(scalar) {
276
- this.x *= scalar;
277
- this.y *= scalar;
278
- return this;
279
- }
280
- /**
281
- * Divides this instance by the given vector.
282
- *
283
- * @param {Vector2} v - The vector to divide.
284
- * @return {Vector2} A reference to this vector.
285
- */
286
- divide(v) {
287
- this.x /= v.x;
288
- this.y /= v.y;
289
- return this;
290
- }
291
- /**
292
- * Divides this vector by the given scalar.
293
- *
294
- * @param {number} scalar - The scalar to divide.
295
- * @return {Vector2} A reference to this vector.
296
- */
297
- divideScalar(scalar) {
298
- return this.multiplyScalar(1 / scalar);
299
- }
300
- /**
301
- * Multiplies this vector (with an implicit 1 as the 3rd component) by
302
- * the given 3x3 matrix.
303
- *
304
- * @param {Matrix3} m - The matrix to apply.
305
- * @return {Vector2} A reference to this vector.
306
- */
307
- applyMatrix3(m) {
308
- const x = this.x, y = this.y;
309
- const e = m.elements;
310
- this.x = e[0] * x + e[3] * y + e[6];
311
- this.y = e[1] * x + e[4] * y + e[7];
312
- return this;
313
- }
314
- /**
315
- * If this vector's x or y value is greater than the given vector's x or y
316
- * value, replace that value with the corresponding min value.
317
- *
318
- * @param {Vector2} v - The vector.
319
- * @return {Vector2} A reference to this vector.
320
- */
321
- min(v) {
322
- this.x = Math.min(this.x, v.x);
323
- this.y = Math.min(this.y, v.y);
324
- return this;
325
- }
326
- /**
327
- * If this vector's x or y value is less than the given vector's x or y
328
- * value, replace that value with the corresponding max value.
329
- *
330
- * @param {Vector2} v - The vector.
331
- * @return {Vector2} A reference to this vector.
332
- */
333
- max(v) {
334
- this.x = Math.max(this.x, v.x);
335
- this.y = Math.max(this.y, v.y);
336
- return this;
337
- }
338
- /**
339
- * If this vector's x or y value is greater than the max vector's x or y
340
- * value, it is replaced by the corresponding value.
341
- * If this vector's x or y value is less than the min vector's x or y value,
342
- * it is replaced by the corresponding value.
343
- *
344
- * @param {Vector2} min - The minimum x and y values.
345
- * @param {Vector2} max - The maximum x and y values in the desired range.
346
- * @return {Vector2} A reference to this vector.
347
- */
348
- clamp(min, max) {
349
- this.x = clamp(this.x, min.x, max.x);
350
- this.y = clamp(this.y, min.y, max.y);
351
- return this;
352
- }
353
- /**
354
- * If this vector's x or y values are greater than the max value, they are
355
- * replaced by the max value.
356
- * If this vector's x or y values are less than the min value, they are
357
- * replaced by the min value.
358
- *
359
- * @param {number} minVal - The minimum value the components will be clamped to.
360
- * @param {number} maxVal - The maximum value the components will be clamped to.
361
- * @return {Vector2} A reference to this vector.
362
- */
363
- clampScalar(minVal, maxVal) {
364
- this.x = clamp(this.x, minVal, maxVal);
365
- this.y = clamp(this.y, minVal, maxVal);
366
- return this;
367
- }
368
- /**
369
- * If this vector's length is greater than the max value, it is replaced by
370
- * the max value.
371
- * If this vector's length is less than the min value, it is replaced by the
372
- * min value.
373
- *
374
- * @param {number} min - The minimum value the vector length will be clamped to.
375
- * @param {number} max - The maximum value the vector length will be clamped to.
376
- * @return {Vector2} A reference to this vector.
377
- */
378
- clampLength(min, max) {
379
- const length = this.length();
380
- return this.divideScalar(length || 1).multiplyScalar(clamp(length, min, max));
381
- }
382
- /**
383
- * The components of this vector are rounded down to the nearest integer value.
384
- *
385
- * @return {Vector2} A reference to this vector.
386
- */
387
- floor() {
388
- this.x = Math.floor(this.x);
389
- this.y = Math.floor(this.y);
390
- return this;
391
- }
392
- /**
393
- * The components of this vector are rounded up to the nearest integer value.
394
- *
395
- * @return {Vector2} A reference to this vector.
396
- */
397
- ceil() {
398
- this.x = Math.ceil(this.x);
399
- this.y = Math.ceil(this.y);
400
- return this;
401
- }
402
- /**
403
- * The components of this vector are rounded to the nearest integer value
404
- *
405
- * @return {Vector2} A reference to this vector.
406
- */
407
- round() {
408
- this.x = Math.round(this.x);
409
- this.y = Math.round(this.y);
410
- return this;
411
- }
412
- /**
413
- * The components of this vector are rounded towards zero (up if negative,
414
- * down if positive) to an integer value.
415
- *
416
- * @return {Vector2} A reference to this vector.
417
- */
418
- roundToZero() {
419
- this.x = Math.trunc(this.x);
420
- this.y = Math.trunc(this.y);
421
- return this;
422
- }
423
- /**
424
- * Inverts this vector - i.e. sets x = -x and y = -y.
425
- *
426
- * @return {Vector2} A reference to this vector.
427
- */
428
- negate() {
429
- this.x = -this.x;
430
- this.y = -this.y;
431
- return this;
432
- }
433
- /**
434
- * Calculates the dot product of the given vector with this instance.
435
- *
436
- * @param {Vector2} v - The vector to compute the dot product with.
437
- * @return {number} The result of the dot product.
438
- */
439
- dot(v) {
440
- return this.x * v.x + this.y * v.y;
441
- }
442
- /**
443
- * Calculates the cross product of the given vector with this instance.
444
- *
445
- * @param {Vector2} v - The vector to compute the cross product with.
446
- * @return {number} The result of the cross product.
447
- */
448
- cross(v) {
449
- return this.x * v.y - this.y * v.x;
450
- }
451
- /**
452
- * Computes the square of the Euclidean length (straight-line length) from
453
- * (0, 0) to (x, y). If you are comparing the lengths of vectors, you should
454
- * compare the length squared instead as it is slightly more efficient to calculate.
455
- *
456
- * @return {number} The square length of this vector.
457
- */
458
- lengthSq() {
459
- return this.x * this.x + this.y * this.y;
460
- }
461
- /**
462
- * Computes the Euclidean length (straight-line length) from (0, 0) to (x, y).
463
- *
464
- * @return {number} The length of this vector.
465
- */
466
- length() {
467
- return Math.sqrt(this.x * this.x + this.y * this.y);
468
- }
469
- /**
470
- * Computes the Manhattan length of this vector.
471
- *
472
- * @return {number} The length of this vector.
473
- */
474
- manhattanLength() {
475
- return Math.abs(this.x) + Math.abs(this.y);
476
- }
477
- /**
478
- * Converts this vector to a unit vector - that is, sets it equal to a vector
479
- * with the same direction as this one, but with a vector length of `1`.
480
- *
481
- * @return {Vector2} A reference to this vector.
482
- */
483
- normalize() {
484
- return this.divideScalar(this.length() || 1);
485
- }
486
- /**
487
- * Computes the angle in radians of this vector with respect to the positive x-axis.
488
- *
489
- * @return {number} The angle in radians.
490
- */
491
- angle() {
492
- const angle = Math.atan2(-this.y, -this.x) + Math.PI;
493
- return angle;
494
- }
495
- /**
496
- * Returns the angle between the given vector and this instance in radians.
497
- *
498
- * @param {Vector2} v - The vector to compute the angle with.
499
- * @return {number} The angle in radians.
500
- */
501
- angleTo(v) {
502
- const denominator = Math.sqrt(this.lengthSq() * v.lengthSq());
503
- if (denominator === 0) return Math.PI / 2;
504
- const theta = this.dot(v) / denominator;
505
- return Math.acos(clamp(theta, -1, 1));
506
- }
507
- /**
508
- * Computes the distance from the given vector to this instance.
509
- *
510
- * @param {Vector2} v - The vector to compute the distance to.
511
- * @return {number} The distance.
512
- */
513
- distanceTo(v) {
514
- return Math.sqrt(this.distanceToSquared(v));
515
- }
516
- /**
517
- * Computes the squared distance from the given vector to this instance.
518
- * If you are just comparing the distance with another distance, you should compare
519
- * the distance squared instead as it is slightly more efficient to calculate.
520
- *
521
- * @param {Vector2} v - The vector to compute the squared distance to.
522
- * @return {number} The squared distance.
523
- */
524
- distanceToSquared(v) {
525
- const dx = this.x - v.x, dy = this.y - v.y;
526
- return dx * dx + dy * dy;
527
- }
528
- /**
529
- * Computes the Manhattan distance from the given vector to this instance.
530
- *
531
- * @param {Vector2} v - The vector to compute the Manhattan distance to.
532
- * @return {number} The Manhattan distance.
533
- */
534
- manhattanDistanceTo(v) {
535
- return Math.abs(this.x - v.x) + Math.abs(this.y - v.y);
536
- }
537
- /**
538
- * Sets this vector to a vector with the same direction as this one, but
539
- * with the specified length.
540
- *
541
- * @param {number} length - The new length of this vector.
542
- * @return {Vector2} A reference to this vector.
543
- */
544
- setLength(length) {
545
- return this.normalize().multiplyScalar(length);
546
- }
547
- /**
548
- * Linearly interpolates between the given vector and this instance, where
549
- * alpha is the percent distance along the line - alpha = 0 will be this
550
- * vector, and alpha = 1 will be the given one.
551
- *
552
- * @param {Vector2} v - The vector to interpolate towards.
553
- * @param {number} alpha - The interpolation factor, typically in the closed interval `[0, 1]`.
554
- * @return {Vector2} A reference to this vector.
555
- */
556
- lerp(v, alpha) {
557
- this.x += (v.x - this.x) * alpha;
558
- this.y += (v.y - this.y) * alpha;
559
- return this;
560
- }
561
- /**
562
- * Linearly interpolates between the given vectors, where alpha is the percent
563
- * distance along the line - alpha = 0 will be first vector, and alpha = 1 will
564
- * be the second one. The result is stored in this instance.
565
- *
566
- * @param {Vector2} v1 - The first vector.
567
- * @param {Vector2} v2 - The second vector.
568
- * @param {number} alpha - The interpolation factor, typically in the closed interval `[0, 1]`.
569
- * @return {Vector2} A reference to this vector.
570
- */
571
- lerpVectors(v1, v2, alpha) {
572
- this.x = v1.x + (v2.x - v1.x) * alpha;
573
- this.y = v1.y + (v2.y - v1.y) * alpha;
574
- return this;
575
- }
576
- /**
577
- * Returns `true` if this vector is equal with the given one.
578
- *
579
- * @param {Vector2} v - The vector to test for equality.
580
- * @return {boolean} Whether this vector is equal with the given one.
581
- */
582
- equals(v) {
583
- return v.x === this.x && v.y === this.y;
584
- }
585
- /**
586
- * Sets this vector's x value to be `array[ offset ]` and y
587
- * value to be `array[ offset + 1 ]`.
588
- *
589
- * @param {Array<number>} array - An array holding the vector component values.
590
- * @param {number} [offset=0] - The offset into the array.
591
- * @return {Vector2} A reference to this vector.
592
- */
593
- fromArray(array, offset = 0) {
594
- this.x = array[offset];
595
- this.y = array[offset + 1];
596
- return this;
597
- }
598
- /**
599
- * Writes the components of this vector to the given array. If no array is provided,
600
- * the method returns a new instance.
601
- *
602
- * @param {Array<number>} [array=[]] - The target array holding the vector components.
603
- * @param {number} [offset=0] - Index of the first element in the array.
604
- * @return {Array<number>} The vector components.
605
- */
606
- toArray(array = [], offset = 0) {
607
- array[offset] = this.x;
608
- array[offset + 1] = this.y;
609
- return array;
610
- }
611
- /**
612
- * Sets the components of this vector from the given buffer attribute.
613
- *
614
- * @param {BufferAttribute} attribute - The buffer attribute holding vector data.
615
- * @param {number} index - The index into the attribute.
616
- * @return {Vector2} A reference to this vector.
617
- */
618
- fromBufferAttribute(attribute, index) {
619
- this.x = attribute.getX(index);
620
- this.y = attribute.getY(index);
621
- return this;
622
- }
623
- /**
624
- * Rotates this vector around the given center by the given angle.
625
- *
626
- * @param {Vector2} center - The point around which to rotate.
627
- * @param {number} angle - The angle to rotate, in radians.
628
- * @return {Vector2} A reference to this vector.
629
- */
630
- rotateAround(center, angle) {
631
- const c = Math.cos(angle), s = Math.sin(angle);
632
- const x = this.x - center.x;
633
- const y = this.y - center.y;
634
- this.x = x * c - y * s + center.x;
635
- this.y = x * s + y * c + center.y;
636
- return this;
637
- }
638
- /**
639
- * Sets each component of this vector to a pseudo-random value between `0` and
640
- * `1`, excluding `1`.
641
- *
642
- * @return {Vector2} A reference to this vector.
643
- */
644
- random() {
645
- this.x = Math.random();
646
- this.y = Math.random();
647
- return this;
648
- }
649
- *[Symbol.iterator]() {
650
- yield this.x;
651
- yield this.y;
652
- }
653
- });
654
- var Quaternion = class {
655
- /**
656
- * Constructs a new quaternion.
657
- *
658
- * @param {number} [x=0] - The x value of this quaternion.
659
- * @param {number} [y=0] - The y value of this quaternion.
660
- * @param {number} [z=0] - The z value of this quaternion.
661
- * @param {number} [w=1] - The w value of this quaternion.
662
- */
663
- constructor(x = 0, y = 0, z = 0, w = 1) {
664
- this.isQuaternion = true;
665
- this._x = x;
666
- this._y = y;
667
- this._z = z;
668
- this._w = w;
669
- }
670
- /**
671
- * Interpolates between two quaternions via SLERP. This implementation assumes the
672
- * quaternion data are managed in flat arrays.
673
- *
674
- * @param {Array<number>} dst - The destination array.
675
- * @param {number} dstOffset - An offset into the destination array.
676
- * @param {Array<number>} src0 - The source array of the first quaternion.
677
- * @param {number} srcOffset0 - An offset into the first source array.
678
- * @param {Array<number>} src1 - The source array of the second quaternion.
679
- * @param {number} srcOffset1 - An offset into the second source array.
680
- * @param {number} t - The interpolation factor. A value in the range `[0,1]` will interpolate. A value outside the range `[0,1]` will extrapolate.
681
- * @see {@link Quaternion#slerp}
682
- */
683
- static slerpFlat(dst, dstOffset, src0, srcOffset0, src1, srcOffset1, t) {
684
- let x0 = src0[srcOffset0 + 0], y0 = src0[srcOffset0 + 1], z0 = src0[srcOffset0 + 2], w0 = src0[srcOffset0 + 3];
685
- let x1 = src1[srcOffset1 + 0], y1 = src1[srcOffset1 + 1], z1 = src1[srcOffset1 + 2], w1 = src1[srcOffset1 + 3];
686
- if (w0 !== w1 || x0 !== x1 || y0 !== y1 || z0 !== z1) {
687
- let dot = x0 * x1 + y0 * y1 + z0 * z1 + w0 * w1;
688
- if (dot < 0) {
689
- x1 = -x1;
690
- y1 = -y1;
691
- z1 = -z1;
692
- w1 = -w1;
693
- dot = -dot;
694
- }
695
- let s = 1 - t;
696
- if (dot < 0.9995) {
697
- const theta = Math.acos(dot);
698
- const sin = Math.sin(theta);
699
- s = Math.sin(s * theta) / sin;
700
- t = Math.sin(t * theta) / sin;
701
- x0 = x0 * s + x1 * t;
702
- y0 = y0 * s + y1 * t;
703
- z0 = z0 * s + z1 * t;
704
- w0 = w0 * s + w1 * t;
705
- } else {
706
- x0 = x0 * s + x1 * t;
707
- y0 = y0 * s + y1 * t;
708
- z0 = z0 * s + z1 * t;
709
- w0 = w0 * s + w1 * t;
710
- const f = 1 / Math.sqrt(x0 * x0 + y0 * y0 + z0 * z0 + w0 * w0);
711
- x0 *= f;
712
- y0 *= f;
713
- z0 *= f;
714
- w0 *= f;
715
- }
716
- }
717
- dst[dstOffset] = x0;
718
- dst[dstOffset + 1] = y0;
719
- dst[dstOffset + 2] = z0;
720
- dst[dstOffset + 3] = w0;
721
- }
722
- /**
723
- * Multiplies two quaternions. This implementation assumes the quaternion data are managed
724
- * in flat arrays.
725
- *
726
- * @param {Array<number>} dst - The destination array.
727
- * @param {number} dstOffset - An offset into the destination array.
728
- * @param {Array<number>} src0 - The source array of the first quaternion.
729
- * @param {number} srcOffset0 - An offset into the first source array.
730
- * @param {Array<number>} src1 - The source array of the second quaternion.
731
- * @param {number} srcOffset1 - An offset into the second source array.
732
- * @return {Array<number>} The destination array.
733
- * @see {@link Quaternion#multiplyQuaternions}.
734
- */
735
- static multiplyQuaternionsFlat(dst, dstOffset, src0, srcOffset0, src1, srcOffset1) {
736
- const x0 = src0[srcOffset0];
737
- const y0 = src0[srcOffset0 + 1];
738
- const z0 = src0[srcOffset0 + 2];
739
- const w0 = src0[srcOffset0 + 3];
740
- const x1 = src1[srcOffset1];
741
- const y1 = src1[srcOffset1 + 1];
742
- const z1 = src1[srcOffset1 + 2];
743
- const w1 = src1[srcOffset1 + 3];
744
- dst[dstOffset] = x0 * w1 + w0 * x1 + y0 * z1 - z0 * y1;
745
- dst[dstOffset + 1] = y0 * w1 + w0 * y1 + z0 * x1 - x0 * z1;
746
- dst[dstOffset + 2] = z0 * w1 + w0 * z1 + x0 * y1 - y0 * x1;
747
- dst[dstOffset + 3] = w0 * w1 - x0 * x1 - y0 * y1 - z0 * z1;
748
- return dst;
749
- }
750
- /**
751
- * The x value of this quaternion.
752
- *
753
- * @type {number}
754
- * @default 0
755
- */
756
- get x() {
757
- return this._x;
758
- }
759
- set x(value) {
760
- this._x = value;
761
- this._onChangeCallback();
762
- }
763
- /**
764
- * The y value of this quaternion.
765
- *
766
- * @type {number}
767
- * @default 0
768
- */
769
- get y() {
770
- return this._y;
771
- }
772
- set y(value) {
773
- this._y = value;
774
- this._onChangeCallback();
775
- }
776
- /**
777
- * The z value of this quaternion.
778
- *
779
- * @type {number}
780
- * @default 0
781
- */
782
- get z() {
783
- return this._z;
784
- }
785
- set z(value) {
786
- this._z = value;
787
- this._onChangeCallback();
788
- }
789
- /**
790
- * The w value of this quaternion.
791
- *
792
- * @type {number}
793
- * @default 1
794
- */
795
- get w() {
796
- return this._w;
797
- }
798
- set w(value) {
799
- this._w = value;
800
- this._onChangeCallback();
801
- }
802
- /**
803
- * Sets the quaternion components.
804
- *
805
- * @param {number} x - The x value of this quaternion.
806
- * @param {number} y - The y value of this quaternion.
807
- * @param {number} z - The z value of this quaternion.
808
- * @param {number} w - The w value of this quaternion.
809
- * @return {Quaternion} A reference to this quaternion.
810
- */
811
- set(x, y, z, w) {
812
- this._x = x;
813
- this._y = y;
814
- this._z = z;
815
- this._w = w;
816
- this._onChangeCallback();
817
- return this;
818
- }
819
- /**
820
- * Returns a new quaternion with copied values from this instance.
821
- *
822
- * @return {Quaternion} A clone of this instance.
823
- */
824
- clone() {
825
- return new this.constructor(this._x, this._y, this._z, this._w);
826
- }
827
- /**
828
- * Copies the values of the given quaternion to this instance.
829
- *
830
- * @param {Quaternion} quaternion - The quaternion to copy.
831
- * @return {Quaternion} A reference to this quaternion.
832
- */
833
- copy(quaternion) {
834
- this._x = quaternion.x;
835
- this._y = quaternion.y;
836
- this._z = quaternion.z;
837
- this._w = quaternion.w;
838
- this._onChangeCallback();
839
- return this;
840
- }
841
- /**
842
- * Sets this quaternion from the rotation specified by the given
843
- * Euler angles.
844
- *
845
- * @param {Euler} euler - The Euler angles.
846
- * @param {boolean} [update=true] - Whether the internal `onChange` callback should be executed or not.
847
- * @return {Quaternion} A reference to this quaternion.
848
- */
849
- setFromEuler(euler, update = true) {
850
- const x = euler._x, y = euler._y, z = euler._z, order = euler._order;
851
- const cos = Math.cos;
852
- const sin = Math.sin;
853
- const c1 = cos(x / 2);
854
- const c2 = cos(y / 2);
855
- const c3 = cos(z / 2);
856
- const s1 = sin(x / 2);
857
- const s2 = sin(y / 2);
858
- const s3 = sin(z / 2);
859
- switch (order) {
860
- case "XYZ":
861
- this._x = s1 * c2 * c3 + c1 * s2 * s3;
862
- this._y = c1 * s2 * c3 - s1 * c2 * s3;
863
- this._z = c1 * c2 * s3 + s1 * s2 * c3;
864
- this._w = c1 * c2 * c3 - s1 * s2 * s3;
865
- break;
866
- case "YXZ":
867
- this._x = s1 * c2 * c3 + c1 * s2 * s3;
868
- this._y = c1 * s2 * c3 - s1 * c2 * s3;
869
- this._z = c1 * c2 * s3 - s1 * s2 * c3;
870
- this._w = c1 * c2 * c3 + s1 * s2 * s3;
871
- break;
872
- case "ZXY":
873
- this._x = s1 * c2 * c3 - c1 * s2 * s3;
874
- this._y = c1 * s2 * c3 + s1 * c2 * s3;
875
- this._z = c1 * c2 * s3 + s1 * s2 * c3;
876
- this._w = c1 * c2 * c3 - s1 * s2 * s3;
877
- break;
878
- case "ZYX":
879
- this._x = s1 * c2 * c3 - c1 * s2 * s3;
880
- this._y = c1 * s2 * c3 + s1 * c2 * s3;
881
- this._z = c1 * c2 * s3 - s1 * s2 * c3;
882
- this._w = c1 * c2 * c3 + s1 * s2 * s3;
883
- break;
884
- case "YZX":
885
- this._x = s1 * c2 * c3 + c1 * s2 * s3;
886
- this._y = c1 * s2 * c3 + s1 * c2 * s3;
887
- this._z = c1 * c2 * s3 - s1 * s2 * c3;
888
- this._w = c1 * c2 * c3 - s1 * s2 * s3;
889
- break;
890
- case "XZY":
891
- this._x = s1 * c2 * c3 - c1 * s2 * s3;
892
- this._y = c1 * s2 * c3 - s1 * c2 * s3;
893
- this._z = c1 * c2 * s3 + s1 * s2 * c3;
894
- this._w = c1 * c2 * c3 + s1 * s2 * s3;
895
- break;
896
- default:
897
- warn("Quaternion: .setFromEuler() encountered an unknown order: " + order);
898
- }
899
- if (update === true) this._onChangeCallback();
900
- return this;
901
- }
902
- /**
903
- * Sets this quaternion from the given axis and angle.
904
- *
905
- * @param {Vector3} axis - The normalized axis.
906
- * @param {number} angle - The angle in radians.
907
- * @return {Quaternion} A reference to this quaternion.
908
- */
909
- setFromAxisAngle(axis, angle) {
910
- const halfAngle = angle / 2, s = Math.sin(halfAngle);
911
- this._x = axis.x * s;
912
- this._y = axis.y * s;
913
- this._z = axis.z * s;
914
- this._w = Math.cos(halfAngle);
915
- this._onChangeCallback();
916
- return this;
917
- }
918
- /**
919
- * Sets this quaternion from the given rotation matrix.
920
- *
921
- * @param {Matrix4} m - A 4x4 matrix of which the upper 3x3 of matrix is a pure rotation matrix (i.e. unscaled).
922
- * @return {Quaternion} A reference to this quaternion.
923
- */
924
- setFromRotationMatrix(m) {
925
- const te = m.elements, m11 = te[0], m12 = te[4], m13 = te[8], m21 = te[1], m22 = te[5], m23 = te[9], m31 = te[2], m32 = te[6], m33 = te[10], trace = m11 + m22 + m33;
926
- if (trace > 0) {
927
- const s = 0.5 / Math.sqrt(trace + 1);
928
- this._w = 0.25 / s;
929
- this._x = (m32 - m23) * s;
930
- this._y = (m13 - m31) * s;
931
- this._z = (m21 - m12) * s;
932
- } else if (m11 > m22 && m11 > m33) {
933
- const s = 2 * Math.sqrt(1 + m11 - m22 - m33);
934
- this._w = (m32 - m23) / s;
935
- this._x = 0.25 * s;
936
- this._y = (m12 + m21) / s;
937
- this._z = (m13 + m31) / s;
938
- } else if (m22 > m33) {
939
- const s = 2 * Math.sqrt(1 + m22 - m11 - m33);
940
- this._w = (m13 - m31) / s;
941
- this._x = (m12 + m21) / s;
942
- this._y = 0.25 * s;
943
- this._z = (m23 + m32) / s;
944
- } else {
945
- const s = 2 * Math.sqrt(1 + m33 - m11 - m22);
946
- this._w = (m21 - m12) / s;
947
- this._x = (m13 + m31) / s;
948
- this._y = (m23 + m32) / s;
949
- this._z = 0.25 * s;
950
- }
951
- this._onChangeCallback();
952
- return this;
953
- }
954
- /**
955
- * Sets this quaternion to the rotation required to rotate the direction vector
956
- * `vFrom` to the direction vector `vTo`.
957
- *
958
- * @param {Vector3} vFrom - The first (normalized) direction vector.
959
- * @param {Vector3} vTo - The second (normalized) direction vector.
960
- * @return {Quaternion} A reference to this quaternion.
961
- */
962
- setFromUnitVectors(vFrom, vTo) {
963
- let r = vFrom.dot(vTo) + 1;
964
- if (r < 1e-8) {
965
- r = 0;
966
- if (Math.abs(vFrom.x) > Math.abs(vFrom.z)) {
967
- this._x = -vFrom.y;
968
- this._y = vFrom.x;
969
- this._z = 0;
970
- this._w = r;
971
- } else {
972
- this._x = 0;
973
- this._y = -vFrom.z;
974
- this._z = vFrom.y;
975
- this._w = r;
976
- }
977
- } else {
978
- this._x = vFrom.y * vTo.z - vFrom.z * vTo.y;
979
- this._y = vFrom.z * vTo.x - vFrom.x * vTo.z;
980
- this._z = vFrom.x * vTo.y - vFrom.y * vTo.x;
981
- this._w = r;
982
- }
983
- return this.normalize();
984
- }
985
- /**
986
- * Returns the angle between this quaternion and the given one in radians.
987
- *
988
- * @param {Quaternion} q - The quaternion to compute the angle with.
989
- * @return {number} The angle in radians.
990
- */
991
- angleTo(q) {
992
- return 2 * Math.acos(Math.abs(clamp(this.dot(q), -1, 1)));
993
- }
994
- /**
995
- * Rotates this quaternion by a given angular step to the given quaternion.
996
- * The method ensures that the final quaternion will not overshoot `q`.
997
- *
998
- * @param {Quaternion} q - The target quaternion.
999
- * @param {number} step - The angular step in radians.
1000
- * @return {Quaternion} A reference to this quaternion.
1001
- */
1002
- rotateTowards(q, step) {
1003
- const angle = this.angleTo(q);
1004
- if (angle === 0) return this;
1005
- const t = Math.min(1, step / angle);
1006
- this.slerp(q, t);
1007
- return this;
1008
- }
1009
- /**
1010
- * Sets this quaternion to the identity quaternion; that is, to the
1011
- * quaternion that represents "no rotation".
1012
- *
1013
- * @return {Quaternion} A reference to this quaternion.
1014
- */
1015
- identity() {
1016
- return this.set(0, 0, 0, 1);
1017
- }
1018
- /**
1019
- * Inverts this quaternion via {@link Quaternion#conjugate}. The
1020
- * quaternion is assumed to have unit length.
1021
- *
1022
- * @return {Quaternion} A reference to this quaternion.
1023
- */
1024
- invert() {
1025
- return this.conjugate();
1026
- }
1027
- /**
1028
- * Returns the rotational conjugate of this quaternion. The conjugate of a
1029
- * quaternion represents the same rotation in the opposite direction about
1030
- * the rotational axis.
1031
- *
1032
- * @return {Quaternion} A reference to this quaternion.
1033
- */
1034
- conjugate() {
1035
- this._x *= -1;
1036
- this._y *= -1;
1037
- this._z *= -1;
1038
- this._onChangeCallback();
1039
- return this;
1040
- }
1041
- /**
1042
- * Calculates the dot product of this quaternion and the given one.
1043
- *
1044
- * @param {Quaternion} v - The quaternion to compute the dot product with.
1045
- * @return {number} The result of the dot product.
1046
- */
1047
- dot(v) {
1048
- return this._x * v._x + this._y * v._y + this._z * v._z + this._w * v._w;
1049
- }
1050
- /**
1051
- * Computes the squared Euclidean length (straight-line length) of this quaternion,
1052
- * considered as a 4 dimensional vector. This can be useful if you are comparing the
1053
- * lengths of two quaternions, as this is a slightly more efficient calculation than
1054
- * {@link Quaternion#length}.
1055
- *
1056
- * @return {number} The squared Euclidean length.
1057
- */
1058
- lengthSq() {
1059
- return this._x * this._x + this._y * this._y + this._z * this._z + this._w * this._w;
1060
- }
1061
- /**
1062
- * Computes the Euclidean length (straight-line length) of this quaternion,
1063
- * considered as a 4 dimensional vector.
1064
- *
1065
- * @return {number} The Euclidean length.
1066
- */
1067
- length() {
1068
- return Math.sqrt(this._x * this._x + this._y * this._y + this._z * this._z + this._w * this._w);
1069
- }
1070
- /**
1071
- * Normalizes this quaternion - that is, calculated the quaternion that performs
1072
- * the same rotation as this one, but has a length equal to `1`.
1073
- *
1074
- * @return {Quaternion} A reference to this quaternion.
1075
- */
1076
- normalize() {
1077
- let l = this.length();
1078
- if (l === 0) {
1079
- this._x = 0;
1080
- this._y = 0;
1081
- this._z = 0;
1082
- this._w = 1;
1083
- } else {
1084
- l = 1 / l;
1085
- this._x = this._x * l;
1086
- this._y = this._y * l;
1087
- this._z = this._z * l;
1088
- this._w = this._w * l;
1089
- }
1090
- this._onChangeCallback();
1091
- return this;
1092
- }
1093
- /**
1094
- * Multiplies this quaternion by the given one.
1095
- *
1096
- * @param {Quaternion} q - The quaternion.
1097
- * @return {Quaternion} A reference to this quaternion.
1098
- */
1099
- multiply(q) {
1100
- return this.multiplyQuaternions(this, q);
1101
- }
1102
- /**
1103
- * Pre-multiplies this quaternion by the given one.
1104
- *
1105
- * @param {Quaternion} q - The quaternion.
1106
- * @return {Quaternion} A reference to this quaternion.
1107
- */
1108
- premultiply(q) {
1109
- return this.multiplyQuaternions(q, this);
1110
- }
1111
- /**
1112
- * Multiplies the given quaternions and stores the result in this instance.
1113
- *
1114
- * @param {Quaternion} a - The first quaternion.
1115
- * @param {Quaternion} b - The second quaternion.
1116
- * @return {Quaternion} A reference to this quaternion.
1117
- */
1118
- multiplyQuaternions(a, b) {
1119
- const qax = a._x, qay = a._y, qaz = a._z, qaw = a._w;
1120
- const qbx = b._x, qby = b._y, qbz = b._z, qbw = b._w;
1121
- this._x = qax * qbw + qaw * qbx + qay * qbz - qaz * qby;
1122
- this._y = qay * qbw + qaw * qby + qaz * qbx - qax * qbz;
1123
- this._z = qaz * qbw + qaw * qbz + qax * qby - qay * qbx;
1124
- this._w = qaw * qbw - qax * qbx - qay * qby - qaz * qbz;
1125
- this._onChangeCallback();
1126
- return this;
1127
- }
1128
- /**
1129
- * Performs a spherical linear interpolation between this quaternion and the target quaternion.
1130
- *
1131
- * @param {Quaternion} qb - The target quaternion.
1132
- * @param {number} t - The interpolation factor. A value in the range `[0,1]` will interpolate. A value outside the range `[0,1]` will extrapolate.
1133
- * @return {Quaternion} A reference to this quaternion.
1134
- */
1135
- slerp(qb, t) {
1136
- let x = qb._x, y = qb._y, z = qb._z, w = qb._w;
1137
- let dot = this.dot(qb);
1138
- if (dot < 0) {
1139
- x = -x;
1140
- y = -y;
1141
- z = -z;
1142
- w = -w;
1143
- dot = -dot;
1144
- }
1145
- let s = 1 - t;
1146
- if (dot < 0.9995) {
1147
- const theta = Math.acos(dot);
1148
- const sin = Math.sin(theta);
1149
- s = Math.sin(s * theta) / sin;
1150
- t = Math.sin(t * theta) / sin;
1151
- this._x = this._x * s + x * t;
1152
- this._y = this._y * s + y * t;
1153
- this._z = this._z * s + z * t;
1154
- this._w = this._w * s + w * t;
1155
- this._onChangeCallback();
1156
- } else {
1157
- this._x = this._x * s + x * t;
1158
- this._y = this._y * s + y * t;
1159
- this._z = this._z * s + z * t;
1160
- this._w = this._w * s + w * t;
1161
- this.normalize();
1162
- }
1163
- return this;
1164
- }
1165
- /**
1166
- * Performs a spherical linear interpolation between the given quaternions
1167
- * and stores the result in this quaternion.
1168
- *
1169
- * @param {Quaternion} qa - The source quaternion.
1170
- * @param {Quaternion} qb - The target quaternion.
1171
- * @param {number} t - The interpolation factor in the closed interval `[0, 1]`.
1172
- * @return {Quaternion} A reference to this quaternion.
1173
- */
1174
- slerpQuaternions(qa, qb, t) {
1175
- return this.copy(qa).slerp(qb, t);
1176
- }
1177
- /**
1178
- * Sets this quaternion to a uniformly random, normalized quaternion.
1179
- *
1180
- * @return {Quaternion} A reference to this quaternion.
1181
- */
1182
- random() {
1183
- const theta1 = 2 * Math.PI * Math.random();
1184
- const theta2 = 2 * Math.PI * Math.random();
1185
- const x0 = Math.random();
1186
- const r1 = Math.sqrt(1 - x0);
1187
- const r2 = Math.sqrt(x0);
1188
- return this.set(
1189
- r1 * Math.sin(theta1),
1190
- r1 * Math.cos(theta1),
1191
- r2 * Math.sin(theta2),
1192
- r2 * Math.cos(theta2)
1193
- );
1194
- }
1195
- /**
1196
- * Returns `true` if this quaternion is equal with the given one.
1197
- *
1198
- * @param {Quaternion} quaternion - The quaternion to test for equality.
1199
- * @return {boolean} Whether this quaternion is equal with the given one.
1200
- */
1201
- equals(quaternion) {
1202
- return quaternion._x === this._x && quaternion._y === this._y && quaternion._z === this._z && quaternion._w === this._w;
1203
- }
1204
- /**
1205
- * Sets this quaternion's components from the given array.
1206
- *
1207
- * @param {Array<number>} array - An array holding the quaternion component values.
1208
- * @param {number} [offset=0] - The offset into the array.
1209
- * @return {Quaternion} A reference to this quaternion.
1210
- */
1211
- fromArray(array, offset = 0) {
1212
- this._x = array[offset];
1213
- this._y = array[offset + 1];
1214
- this._z = array[offset + 2];
1215
- this._w = array[offset + 3];
1216
- this._onChangeCallback();
1217
- return this;
1218
- }
1219
- /**
1220
- * Writes the components of this quaternion to the given array. If no array is provided,
1221
- * the method returns a new instance.
1222
- *
1223
- * @param {Array<number>} [array=[]] - The target array holding the quaternion components.
1224
- * @param {number} [offset=0] - Index of the first element in the array.
1225
- * @return {Array<number>} The quaternion components.
1226
- */
1227
- toArray(array = [], offset = 0) {
1228
- array[offset] = this._x;
1229
- array[offset + 1] = this._y;
1230
- array[offset + 2] = this._z;
1231
- array[offset + 3] = this._w;
1232
- return array;
1233
- }
1234
- /**
1235
- * Sets the components of this quaternion from the given buffer attribute.
1236
- *
1237
- * @param {BufferAttribute} attribute - The buffer attribute holding quaternion data.
1238
- * @param {number} index - The index into the attribute.
1239
- * @return {Quaternion} A reference to this quaternion.
1240
- */
1241
- fromBufferAttribute(attribute, index) {
1242
- this._x = attribute.getX(index);
1243
- this._y = attribute.getY(index);
1244
- this._z = attribute.getZ(index);
1245
- this._w = attribute.getW(index);
1246
- this._onChangeCallback();
1247
- return this;
1248
- }
1249
- /**
1250
- * This methods defines the serialization result of this class. Returns the
1251
- * numerical elements of this quaternion in an array of format `[x, y, z, w]`.
1252
- *
1253
- * @return {Array<number>} The serialized quaternion.
1254
- */
1255
- toJSON() {
1256
- return this.toArray();
1257
- }
1258
- _onChange(callback) {
1259
- this._onChangeCallback = callback;
1260
- return this;
1261
- }
1262
- _onChangeCallback() {
1263
- }
1264
- *[Symbol.iterator]() {
1265
- yield this._x;
1266
- yield this._y;
1267
- yield this._z;
1268
- yield this._w;
1269
- }
1270
- };
1271
- var Vector3 = class _Vector3 {
1272
- static {
1273
- _Vector3.prototype.isVector3 = true;
1274
- }
1275
- /**
1276
- * Constructs a new 3D vector.
1277
- *
1278
- * @param {number} [x=0] - The x value of this vector.
1279
- * @param {number} [y=0] - The y value of this vector.
1280
- * @param {number} [z=0] - The z value of this vector.
1281
- */
1282
- constructor(x = 0, y = 0, z = 0) {
1283
- this.x = x;
1284
- this.y = y;
1285
- this.z = z;
1286
- }
1287
- /**
1288
- * Sets the vector components.
1289
- *
1290
- * @param {number} x - The value of the x component.
1291
- * @param {number} y - The value of the y component.
1292
- * @param {number} z - The value of the z component.
1293
- * @return {Vector3} A reference to this vector.
1294
- */
1295
- set(x, y, z) {
1296
- if (z === void 0) z = this.z;
1297
- this.x = x;
1298
- this.y = y;
1299
- this.z = z;
1300
- return this;
1301
- }
1302
- /**
1303
- * Sets the vector components to the same value.
1304
- *
1305
- * @param {number} scalar - The value to set for all vector components.
1306
- * @return {Vector3} A reference to this vector.
1307
- */
1308
- setScalar(scalar) {
1309
- this.x = scalar;
1310
- this.y = scalar;
1311
- this.z = scalar;
1312
- return this;
1313
- }
1314
- /**
1315
- * Sets the vector's x component to the given value.
1316
- *
1317
- * @param {number} x - The value to set.
1318
- * @return {Vector3} A reference to this vector.
1319
- */
1320
- setX(x) {
1321
- this.x = x;
1322
- return this;
1323
- }
1324
- /**
1325
- * Sets the vector's y component to the given value.
1326
- *
1327
- * @param {number} y - The value to set.
1328
- * @return {Vector3} A reference to this vector.
1329
- */
1330
- setY(y) {
1331
- this.y = y;
1332
- return this;
1333
- }
1334
- /**
1335
- * Sets the vector's z component to the given value.
1336
- *
1337
- * @param {number} z - The value to set.
1338
- * @return {Vector3} A reference to this vector.
1339
- */
1340
- setZ(z) {
1341
- this.z = z;
1342
- return this;
1343
- }
1344
- /**
1345
- * Allows to set a vector component with an index.
1346
- *
1347
- * @param {number} index - The component index. `0` equals to x, `1` equals to y, `2` equals to z.
1348
- * @param {number} value - The value to set.
1349
- * @return {Vector3} A reference to this vector.
1350
- */
1351
- setComponent(index, value) {
1352
- switch (index) {
1353
- case 0:
1354
- this.x = value;
1355
- break;
1356
- case 1:
1357
- this.y = value;
1358
- break;
1359
- case 2:
1360
- this.z = value;
1361
- break;
1362
- default:
1363
- throw new Error("THREE.Vector3: index is out of range: " + index);
1364
- }
1365
- return this;
1366
- }
1367
- /**
1368
- * Returns the value of the vector component which matches the given index.
1369
- *
1370
- * @param {number} index - The component index. `0` equals to x, `1` equals to y, `2` equals to z.
1371
- * @return {number} A vector component value.
1372
- */
1373
- getComponent(index) {
1374
- switch (index) {
1375
- case 0:
1376
- return this.x;
1377
- case 1:
1378
- return this.y;
1379
- case 2:
1380
- return this.z;
1381
- default:
1382
- throw new Error("THREE.Vector3: index is out of range: " + index);
1383
- }
1384
- }
1385
- /**
1386
- * Returns a new vector with copied values from this instance.
1387
- *
1388
- * @return {Vector3} A clone of this instance.
1389
- */
1390
- clone() {
1391
- return new this.constructor(this.x, this.y, this.z);
1392
- }
1393
- /**
1394
- * Copies the values of the given vector to this instance.
1395
- *
1396
- * @param {Vector3} v - The vector to copy.
1397
- * @return {Vector3} A reference to this vector.
1398
- */
1399
- copy(v) {
1400
- this.x = v.x;
1401
- this.y = v.y;
1402
- this.z = v.z;
1403
- return this;
1404
- }
1405
- /**
1406
- * Adds the given vector to this instance.
1407
- *
1408
- * @param {Vector3} v - The vector to add.
1409
- * @return {Vector3} A reference to this vector.
1410
- */
1411
- add(v) {
1412
- this.x += v.x;
1413
- this.y += v.y;
1414
- this.z += v.z;
1415
- return this;
1416
- }
1417
- /**
1418
- * Adds the given scalar value to all components of this instance.
1419
- *
1420
- * @param {number} s - The scalar to add.
1421
- * @return {Vector3} A reference to this vector.
1422
- */
1423
- addScalar(s) {
1424
- this.x += s;
1425
- this.y += s;
1426
- this.z += s;
1427
- return this;
1428
- }
1429
- /**
1430
- * Adds the given vectors and stores the result in this instance.
1431
- *
1432
- * @param {Vector3} a - The first vector.
1433
- * @param {Vector3} b - The second vector.
1434
- * @return {Vector3} A reference to this vector.
1435
- */
1436
- addVectors(a, b) {
1437
- this.x = a.x + b.x;
1438
- this.y = a.y + b.y;
1439
- this.z = a.z + b.z;
1440
- return this;
1441
- }
1442
- /**
1443
- * Adds the given vector scaled by the given factor to this instance.
1444
- *
1445
- * @param {Vector3|Vector4} v - The vector.
1446
- * @param {number} s - The factor that scales `v`.
1447
- * @return {Vector3} A reference to this vector.
1448
- */
1449
- addScaledVector(v, s) {
1450
- this.x += v.x * s;
1451
- this.y += v.y * s;
1452
- this.z += v.z * s;
1453
- return this;
1454
- }
1455
- /**
1456
- * Subtracts the given vector from this instance.
1457
- *
1458
- * @param {Vector3} v - The vector to subtract.
1459
- * @return {Vector3} A reference to this vector.
1460
- */
1461
- sub(v) {
1462
- this.x -= v.x;
1463
- this.y -= v.y;
1464
- this.z -= v.z;
1465
- return this;
1466
- }
1467
- /**
1468
- * Subtracts the given scalar value from all components of this instance.
1469
- *
1470
- * @param {number} s - The scalar to subtract.
1471
- * @return {Vector3} A reference to this vector.
1472
- */
1473
- subScalar(s) {
1474
- this.x -= s;
1475
- this.y -= s;
1476
- this.z -= s;
1477
- return this;
1478
- }
1479
- /**
1480
- * Subtracts the given vectors and stores the result in this instance.
1481
- *
1482
- * @param {Vector3} a - The first vector.
1483
- * @param {Vector3} b - The second vector.
1484
- * @return {Vector3} A reference to this vector.
1485
- */
1486
- subVectors(a, b) {
1487
- this.x = a.x - b.x;
1488
- this.y = a.y - b.y;
1489
- this.z = a.z - b.z;
1490
- return this;
1491
- }
1492
- /**
1493
- * Multiplies the given vector with this instance.
1494
- *
1495
- * @param {Vector3} v - The vector to multiply.
1496
- * @return {Vector3} A reference to this vector.
1497
- */
1498
- multiply(v) {
1499
- this.x *= v.x;
1500
- this.y *= v.y;
1501
- this.z *= v.z;
1502
- return this;
1503
- }
1504
- /**
1505
- * Multiplies the given scalar value with all components of this instance.
1506
- *
1507
- * @param {number} scalar - The scalar to multiply.
1508
- * @return {Vector3} A reference to this vector.
1509
- */
1510
- multiplyScalar(scalar) {
1511
- this.x *= scalar;
1512
- this.y *= scalar;
1513
- this.z *= scalar;
1514
- return this;
1515
- }
1516
- /**
1517
- * Multiplies the given vectors and stores the result in this instance.
1518
- *
1519
- * @param {Vector3} a - The first vector.
1520
- * @param {Vector3} b - The second vector.
1521
- * @return {Vector3} A reference to this vector.
1522
- */
1523
- multiplyVectors(a, b) {
1524
- this.x = a.x * b.x;
1525
- this.y = a.y * b.y;
1526
- this.z = a.z * b.z;
1527
- return this;
1528
- }
1529
- /**
1530
- * Applies the given Euler rotation to this vector.
1531
- *
1532
- * @param {Euler} euler - The Euler angles.
1533
- * @return {Vector3} A reference to this vector.
1534
- */
1535
- applyEuler(euler) {
1536
- return this.applyQuaternion(_quaternion$5.setFromEuler(euler));
1537
- }
1538
- /**
1539
- * Applies a rotation specified by an axis and an angle to this vector.
1540
- *
1541
- * @param {Vector3} axis - A normalized vector representing the rotation axis.
1542
- * @param {number} angle - The angle in radians.
1543
- * @return {Vector3} A reference to this vector.
1544
- */
1545
- applyAxisAngle(axis, angle) {
1546
- return this.applyQuaternion(_quaternion$5.setFromAxisAngle(axis, angle));
1547
- }
1548
- /**
1549
- * Multiplies this vector with the given 3x3 matrix.
1550
- *
1551
- * @param {Matrix3} m - The 3x3 matrix.
1552
- * @return {Vector3} A reference to this vector.
1553
- */
1554
- applyMatrix3(m) {
1555
- const x = this.x, y = this.y, z = this.z;
1556
- const e = m.elements;
1557
- this.x = e[0] * x + e[3] * y + e[6] * z;
1558
- this.y = e[1] * x + e[4] * y + e[7] * z;
1559
- this.z = e[2] * x + e[5] * y + e[8] * z;
1560
- return this;
1561
- }
1562
- /**
1563
- * Multiplies this vector by the given normal matrix and normalizes
1564
- * the result.
1565
- *
1566
- * @param {Matrix3} m - The normal matrix.
1567
- * @return {Vector3} A reference to this vector.
1568
- */
1569
- applyNormalMatrix(m) {
1570
- return this.applyMatrix3(m).normalize();
1571
- }
1572
- /**
1573
- * Multiplies this vector (with an implicit 1 in the 4th dimension) by m, and
1574
- * divides by perspective.
1575
- *
1576
- * @param {Matrix4} m - The matrix to apply.
1577
- * @return {Vector3} A reference to this vector.
1578
- */
1579
- applyMatrix4(m) {
1580
- const x = this.x, y = this.y, z = this.z;
1581
- const e = m.elements;
1582
- const w = 1 / (e[3] * x + e[7] * y + e[11] * z + e[15]);
1583
- this.x = (e[0] * x + e[4] * y + e[8] * z + e[12]) * w;
1584
- this.y = (e[1] * x + e[5] * y + e[9] * z + e[13]) * w;
1585
- this.z = (e[2] * x + e[6] * y + e[10] * z + e[14]) * w;
1586
- return this;
1587
- }
1588
- /**
1589
- * Applies the given Quaternion to this vector.
1590
- *
1591
- * @param {Quaternion} q - The Quaternion.
1592
- * @return {Vector3} A reference to this vector.
1593
- */
1594
- applyQuaternion(q) {
1595
- const vx = this.x, vy = this.y, vz = this.z;
1596
- const qx = q.x, qy = q.y, qz = q.z, qw = q.w;
1597
- const tx = 2 * (qy * vz - qz * vy);
1598
- const ty = 2 * (qz * vx - qx * vz);
1599
- const tz = 2 * (qx * vy - qy * vx);
1600
- this.x = vx + qw * tx + qy * tz - qz * ty;
1601
- this.y = vy + qw * ty + qz * tx - qx * tz;
1602
- this.z = vz + qw * tz + qx * ty - qy * tx;
1603
- return this;
1604
- }
1605
- /**
1606
- * Projects this vector from world space into the camera's normalized
1607
- * device coordinate (NDC) space.
1608
- *
1609
- * @param {Camera} camera - The camera.
1610
- * @return {Vector3} A reference to this vector.
1611
- */
1612
- project(camera) {
1613
- return this.applyMatrix4(camera.matrixWorldInverse).applyMatrix4(camera.projectionMatrix);
1614
- }
1615
- /**
1616
- * Unprojects this vector from the camera's normalized device coordinate (NDC)
1617
- * space into world space.
1618
- *
1619
- * @param {Camera} camera - The camera.
1620
- * @return {Vector3} A reference to this vector.
1621
- */
1622
- unproject(camera) {
1623
- return this.applyMatrix4(camera.projectionMatrixInverse).applyMatrix4(camera.matrixWorld);
1624
- }
1625
- /**
1626
- * Transforms the direction of this vector by a matrix (the upper left 3 x 3
1627
- * subset of the given 4x4 matrix and then normalizes the result.
1628
- *
1629
- * @param {Matrix4} m - The matrix.
1630
- * @return {Vector3} A reference to this vector.
1631
- */
1632
- transformDirection(m) {
1633
- const x = this.x, y = this.y, z = this.z;
1634
- const e = m.elements;
1635
- this.x = e[0] * x + e[4] * y + e[8] * z;
1636
- this.y = e[1] * x + e[5] * y + e[9] * z;
1637
- this.z = e[2] * x + e[6] * y + e[10] * z;
1638
- return this.normalize();
1639
- }
1640
- /**
1641
- * Divides this instance by the given vector.
1642
- *
1643
- * @param {Vector3} v - The vector to divide.
1644
- * @return {Vector3} A reference to this vector.
1645
- */
1646
- divide(v) {
1647
- this.x /= v.x;
1648
- this.y /= v.y;
1649
- this.z /= v.z;
1650
- return this;
1651
- }
1652
- /**
1653
- * Divides this vector by the given scalar.
1654
- *
1655
- * @param {number} scalar - The scalar to divide.
1656
- * @return {Vector3} A reference to this vector.
1657
- */
1658
- divideScalar(scalar) {
1659
- return this.multiplyScalar(1 / scalar);
1660
- }
1661
- /**
1662
- * If this vector's x, y or z value is greater than the given vector's x, y or z
1663
- * value, replace that value with the corresponding min value.
1664
- *
1665
- * @param {Vector3} v - The vector.
1666
- * @return {Vector3} A reference to this vector.
1667
- */
1668
- min(v) {
1669
- this.x = Math.min(this.x, v.x);
1670
- this.y = Math.min(this.y, v.y);
1671
- this.z = Math.min(this.z, v.z);
1672
- return this;
1673
- }
1674
- /**
1675
- * If this vector's x, y or z value is less than the given vector's x, y or z
1676
- * value, replace that value with the corresponding max value.
1677
- *
1678
- * @param {Vector3} v - The vector.
1679
- * @return {Vector3} A reference to this vector.
1680
- */
1681
- max(v) {
1682
- this.x = Math.max(this.x, v.x);
1683
- this.y = Math.max(this.y, v.y);
1684
- this.z = Math.max(this.z, v.z);
1685
- return this;
1686
- }
1687
- /**
1688
- * If this vector's x, y or z value is greater than the max vector's x, y or z
1689
- * value, it is replaced by the corresponding value.
1690
- * If this vector's x, y or z value is less than the min vector's x, y or z value,
1691
- * it is replaced by the corresponding value.
1692
- *
1693
- * @param {Vector3} min - The minimum x, y and z values.
1694
- * @param {Vector3} max - The maximum x, y and z values in the desired range.
1695
- * @return {Vector3} A reference to this vector.
1696
- */
1697
- clamp(min, max) {
1698
- this.x = clamp(this.x, min.x, max.x);
1699
- this.y = clamp(this.y, min.y, max.y);
1700
- this.z = clamp(this.z, min.z, max.z);
1701
- return this;
1702
- }
1703
- /**
1704
- * If this vector's x, y or z values are greater than the max value, they are
1705
- * replaced by the max value.
1706
- * If this vector's x, y or z values are less than the min value, they are
1707
- * replaced by the min value.
1708
- *
1709
- * @param {number} minVal - The minimum value the components will be clamped to.
1710
- * @param {number} maxVal - The maximum value the components will be clamped to.
1711
- * @return {Vector3} A reference to this vector.
1712
- */
1713
- clampScalar(minVal, maxVal) {
1714
- this.x = clamp(this.x, minVal, maxVal);
1715
- this.y = clamp(this.y, minVal, maxVal);
1716
- this.z = clamp(this.z, minVal, maxVal);
1717
- return this;
1718
- }
1719
- /**
1720
- * If this vector's length is greater than the max value, it is replaced by
1721
- * the max value.
1722
- * If this vector's length is less than the min value, it is replaced by the
1723
- * min value.
1724
- *
1725
- * @param {number} min - The minimum value the vector length will be clamped to.
1726
- * @param {number} max - The maximum value the vector length will be clamped to.
1727
- * @return {Vector3} A reference to this vector.
1728
- */
1729
- clampLength(min, max) {
1730
- const length = this.length();
1731
- return this.divideScalar(length || 1).multiplyScalar(clamp(length, min, max));
1732
- }
1733
- /**
1734
- * The components of this vector are rounded down to the nearest integer value.
1735
- *
1736
- * @return {Vector3} A reference to this vector.
1737
- */
1738
- floor() {
1739
- this.x = Math.floor(this.x);
1740
- this.y = Math.floor(this.y);
1741
- this.z = Math.floor(this.z);
1742
- return this;
1743
- }
1744
- /**
1745
- * The components of this vector are rounded up to the nearest integer value.
1746
- *
1747
- * @return {Vector3} A reference to this vector.
1748
- */
1749
- ceil() {
1750
- this.x = Math.ceil(this.x);
1751
- this.y = Math.ceil(this.y);
1752
- this.z = Math.ceil(this.z);
1753
- return this;
1754
- }
1755
- /**
1756
- * The components of this vector are rounded to the nearest integer value
1757
- *
1758
- * @return {Vector3} A reference to this vector.
1759
- */
1760
- round() {
1761
- this.x = Math.round(this.x);
1762
- this.y = Math.round(this.y);
1763
- this.z = Math.round(this.z);
1764
- return this;
1765
- }
1766
- /**
1767
- * The components of this vector are rounded towards zero (up if negative,
1768
- * down if positive) to an integer value.
1769
- *
1770
- * @return {Vector3} A reference to this vector.
1771
- */
1772
- roundToZero() {
1773
- this.x = Math.trunc(this.x);
1774
- this.y = Math.trunc(this.y);
1775
- this.z = Math.trunc(this.z);
1776
- return this;
1777
- }
1778
- /**
1779
- * Inverts this vector - i.e. sets x = -x, y = -y and z = -z.
1780
- *
1781
- * @return {Vector3} A reference to this vector.
1782
- */
1783
- negate() {
1784
- this.x = -this.x;
1785
- this.y = -this.y;
1786
- this.z = -this.z;
1787
- return this;
1788
- }
1789
- /**
1790
- * Calculates the dot product of the given vector with this instance.
1791
- *
1792
- * @param {Vector3} v - The vector to compute the dot product with.
1793
- * @return {number} The result of the dot product.
1794
- */
1795
- dot(v) {
1796
- return this.x * v.x + this.y * v.y + this.z * v.z;
1797
- }
1798
- /**
1799
- * Computes the square of the Euclidean length (straight-line length) from
1800
- * (0, 0, 0) to (x, y, z). If you are comparing the lengths of vectors, you should
1801
- * compare the length squared instead as it is slightly more efficient to calculate.
1802
- *
1803
- * @return {number} The square length of this vector.
1804
- */
1805
- lengthSq() {
1806
- return this.x * this.x + this.y * this.y + this.z * this.z;
1807
- }
1808
- /**
1809
- * Computes the Euclidean length (straight-line length) from (0, 0, 0) to (x, y, z).
1810
- *
1811
- * @return {number} The length of this vector.
1812
- */
1813
- length() {
1814
- return Math.sqrt(this.x * this.x + this.y * this.y + this.z * this.z);
1815
- }
1816
- /**
1817
- * Computes the Manhattan length of this vector.
1818
- *
1819
- * @return {number} The length of this vector.
1820
- */
1821
- manhattanLength() {
1822
- return Math.abs(this.x) + Math.abs(this.y) + Math.abs(this.z);
1823
- }
1824
- /**
1825
- * Converts this vector to a unit vector - that is, sets it equal to a vector
1826
- * with the same direction as this one, but with a vector length of `1`.
1827
- *
1828
- * @return {Vector3} A reference to this vector.
1829
- */
1830
- normalize() {
1831
- return this.divideScalar(this.length() || 1);
1832
- }
1833
- /**
1834
- * Sets this vector to a vector with the same direction as this one, but
1835
- * with the specified length.
1836
- *
1837
- * @param {number} length - The new length of this vector.
1838
- * @return {Vector3} A reference to this vector.
1839
- */
1840
- setLength(length) {
1841
- return this.normalize().multiplyScalar(length);
1842
- }
1843
- /**
1844
- * Linearly interpolates between the given vector and this instance, where
1845
- * alpha is the percent distance along the line - alpha = 0 will be this
1846
- * vector, and alpha = 1 will be the given one.
1847
- *
1848
- * @param {Vector3} v - The vector to interpolate towards.
1849
- * @param {number} alpha - The interpolation factor, typically in the closed interval `[0, 1]`.
1850
- * @return {Vector3} A reference to this vector.
1851
- */
1852
- lerp(v, alpha) {
1853
- this.x += (v.x - this.x) * alpha;
1854
- this.y += (v.y - this.y) * alpha;
1855
- this.z += (v.z - this.z) * alpha;
1856
- return this;
1857
- }
1858
- /**
1859
- * Linearly interpolates between the given vectors, where alpha is the percent
1860
- * distance along the line - alpha = 0 will be first vector, and alpha = 1 will
1861
- * be the second one. The result is stored in this instance.
1862
- *
1863
- * @param {Vector3} v1 - The first vector.
1864
- * @param {Vector3} v2 - The second vector.
1865
- * @param {number} alpha - The interpolation factor, typically in the closed interval `[0, 1]`.
1866
- * @return {Vector3} A reference to this vector.
1867
- */
1868
- lerpVectors(v1, v2, alpha) {
1869
- this.x = v1.x + (v2.x - v1.x) * alpha;
1870
- this.y = v1.y + (v2.y - v1.y) * alpha;
1871
- this.z = v1.z + (v2.z - v1.z) * alpha;
1872
- return this;
1873
- }
1874
- /**
1875
- * Calculates the cross product of the given vector with this instance.
1876
- *
1877
- * @param {Vector3} v - The vector to compute the cross product with.
1878
- * @return {Vector3} The result of the cross product.
1879
- */
1880
- cross(v) {
1881
- return this.crossVectors(this, v);
1882
- }
1883
- /**
1884
- * Calculates the cross product of the given vectors and stores the result
1885
- * in this instance.
1886
- *
1887
- * @param {Vector3} a - The first vector.
1888
- * @param {Vector3} b - The second vector.
1889
- * @return {Vector3} A reference to this vector.
1890
- */
1891
- crossVectors(a, b) {
1892
- const ax = a.x, ay = a.y, az = a.z;
1893
- const bx = b.x, by = b.y, bz = b.z;
1894
- this.x = ay * bz - az * by;
1895
- this.y = az * bx - ax * bz;
1896
- this.z = ax * by - ay * bx;
1897
- return this;
1898
- }
1899
- /**
1900
- * Projects this vector onto the given one.
1901
- *
1902
- * @param {Vector3} v - The vector to project to.
1903
- * @return {Vector3} A reference to this vector.
1904
- */
1905
- projectOnVector(v) {
1906
- const denominator = v.lengthSq();
1907
- if (denominator === 0) return this.set(0, 0, 0);
1908
- const scalar = v.dot(this) / denominator;
1909
- return this.copy(v).multiplyScalar(scalar);
1910
- }
1911
- /**
1912
- * Projects this vector onto a plane by subtracting this
1913
- * vector projected onto the plane's normal from this vector.
1914
- *
1915
- * @param {Vector3} planeNormal - The plane normal.
1916
- * @return {Vector3} A reference to this vector.
1917
- */
1918
- projectOnPlane(planeNormal) {
1919
- _vector$c.copy(this).projectOnVector(planeNormal);
1920
- return this.sub(_vector$c);
1921
- }
1922
- /**
1923
- * Reflects this vector off a plane orthogonal to the given normal vector.
1924
- *
1925
- * @param {Vector3} normal - The (normalized) normal vector.
1926
- * @return {Vector3} A reference to this vector.
1927
- */
1928
- reflect(normal) {
1929
- return this.sub(_vector$c.copy(normal).multiplyScalar(2 * this.dot(normal)));
1930
- }
1931
- /**
1932
- * Returns the angle between the given vector and this instance in radians.
1933
- *
1934
- * @param {Vector3} v - The vector to compute the angle with.
1935
- * @return {number} The angle in radians.
1936
- */
1937
- angleTo(v) {
1938
- const denominator = Math.sqrt(this.lengthSq() * v.lengthSq());
1939
- if (denominator === 0) return Math.PI / 2;
1940
- const theta = this.dot(v) / denominator;
1941
- return Math.acos(clamp(theta, -1, 1));
1942
- }
1943
- /**
1944
- * Computes the distance from the given vector to this instance.
1945
- *
1946
- * @param {Vector3} v - The vector to compute the distance to.
1947
- * @return {number} The distance.
1948
- */
1949
- distanceTo(v) {
1950
- return Math.sqrt(this.distanceToSquared(v));
1951
- }
1952
- /**
1953
- * Computes the squared distance from the given vector to this instance.
1954
- * If you are just comparing the distance with another distance, you should compare
1955
- * the distance squared instead as it is slightly more efficient to calculate.
1956
- *
1957
- * @param {Vector3} v - The vector to compute the squared distance to.
1958
- * @return {number} The squared distance.
1959
- */
1960
- distanceToSquared(v) {
1961
- const dx = this.x - v.x, dy = this.y - v.y, dz = this.z - v.z;
1962
- return dx * dx + dy * dy + dz * dz;
1963
- }
1964
- /**
1965
- * Computes the Manhattan distance from the given vector to this instance.
1966
- *
1967
- * @param {Vector3} v - The vector to compute the Manhattan distance to.
1968
- * @return {number} The Manhattan distance.
1969
- */
1970
- manhattanDistanceTo(v) {
1971
- return Math.abs(this.x - v.x) + Math.abs(this.y - v.y) + Math.abs(this.z - v.z);
1972
- }
1973
- /**
1974
- * Sets the vector components from the given spherical coordinates.
1975
- *
1976
- * @param {Spherical} s - The spherical coordinates.
1977
- * @return {Vector3} A reference to this vector.
1978
- */
1979
- setFromSpherical(s) {
1980
- return this.setFromSphericalCoords(s.radius, s.phi, s.theta);
1981
- }
1982
- /**
1983
- * Sets the vector components from the given spherical coordinates.
1984
- *
1985
- * @param {number} radius - The radius.
1986
- * @param {number} phi - The phi angle in radians.
1987
- * @param {number} theta - The theta angle in radians.
1988
- * @return {Vector3} A reference to this vector.
1989
- */
1990
- setFromSphericalCoords(radius, phi, theta) {
1991
- const sinPhiRadius = Math.sin(phi) * radius;
1992
- this.x = sinPhiRadius * Math.sin(theta);
1993
- this.y = Math.cos(phi) * radius;
1994
- this.z = sinPhiRadius * Math.cos(theta);
1995
- return this;
1996
- }
1997
- /**
1998
- * Sets the vector components from the given cylindrical coordinates.
1999
- *
2000
- * @param {Cylindrical} c - The cylindrical coordinates.
2001
- * @return {Vector3} A reference to this vector.
2002
- */
2003
- setFromCylindrical(c) {
2004
- return this.setFromCylindricalCoords(c.radius, c.theta, c.y);
2005
- }
2006
- /**
2007
- * Sets the vector components from the given cylindrical coordinates.
2008
- *
2009
- * @param {number} radius - The radius.
2010
- * @param {number} theta - The theta angle in radians.
2011
- * @param {number} y - The y value.
2012
- * @return {Vector3} A reference to this vector.
2013
- */
2014
- setFromCylindricalCoords(radius, theta, y) {
2015
- this.x = radius * Math.sin(theta);
2016
- this.y = y;
2017
- this.z = radius * Math.cos(theta);
2018
- return this;
2019
- }
2020
- /**
2021
- * Sets the vector components to the position elements of the
2022
- * given transformation matrix.
2023
- *
2024
- * @param {Matrix4} m - The 4x4 matrix.
2025
- * @return {Vector3} A reference to this vector.
2026
- */
2027
- setFromMatrixPosition(m) {
2028
- const e = m.elements;
2029
- this.x = e[12];
2030
- this.y = e[13];
2031
- this.z = e[14];
2032
- return this;
2033
- }
2034
- /**
2035
- * Sets the vector components to the scale elements of the
2036
- * given transformation matrix.
2037
- *
2038
- * @param {Matrix4} m - The 4x4 matrix.
2039
- * @return {Vector3} A reference to this vector.
2040
- */
2041
- setFromMatrixScale(m) {
2042
- const sx = this.setFromMatrixColumn(m, 0).length();
2043
- const sy = this.setFromMatrixColumn(m, 1).length();
2044
- const sz = this.setFromMatrixColumn(m, 2).length();
2045
- this.x = sx;
2046
- this.y = sy;
2047
- this.z = sz;
2048
- return this;
2049
- }
2050
- /**
2051
- * Sets the vector components from the specified matrix column.
2052
- *
2053
- * @param {Matrix4} m - The 4x4 matrix.
2054
- * @param {number} index - The column index.
2055
- * @return {Vector3} A reference to this vector.
2056
- */
2057
- setFromMatrixColumn(m, index) {
2058
- return this.fromArray(m.elements, index * 4);
2059
- }
2060
- /**
2061
- * Sets the vector components from the specified matrix column.
2062
- *
2063
- * @param {Matrix3} m - The 3x3 matrix.
2064
- * @param {number} index - The column index.
2065
- * @return {Vector3} A reference to this vector.
2066
- */
2067
- setFromMatrix3Column(m, index) {
2068
- return this.fromArray(m.elements, index * 3);
2069
- }
2070
- /**
2071
- * Sets the vector components from the given Euler angles.
2072
- *
2073
- * @param {Euler} e - The Euler angles to set.
2074
- * @return {Vector3} A reference to this vector.
2075
- */
2076
- setFromEuler(e) {
2077
- this.x = e._x;
2078
- this.y = e._y;
2079
- this.z = e._z;
2080
- return this;
2081
- }
2082
- /**
2083
- * Sets the vector components from the RGB components of the
2084
- * given color.
2085
- *
2086
- * @param {Color} c - The color to set.
2087
- * @return {Vector3} A reference to this vector.
2088
- */
2089
- setFromColor(c) {
2090
- this.x = c.r;
2091
- this.y = c.g;
2092
- this.z = c.b;
2093
- return this;
2094
- }
2095
- /**
2096
- * Returns `true` if this vector is equal with the given one.
2097
- *
2098
- * @param {Vector3} v - The vector to test for equality.
2099
- * @return {boolean} Whether this vector is equal with the given one.
2100
- */
2101
- equals(v) {
2102
- return v.x === this.x && v.y === this.y && v.z === this.z;
2103
- }
2104
- /**
2105
- * Sets this vector's x value to be `array[ offset ]`, y value to be `array[ offset + 1 ]`
2106
- * and z value to be `array[ offset + 2 ]`.
2107
- *
2108
- * @param {Array<number>} array - An array holding the vector component values.
2109
- * @param {number} [offset=0] - The offset into the array.
2110
- * @return {Vector3} A reference to this vector.
2111
- */
2112
- fromArray(array, offset = 0) {
2113
- this.x = array[offset];
2114
- this.y = array[offset + 1];
2115
- this.z = array[offset + 2];
2116
- return this;
2117
- }
2118
- /**
2119
- * Writes the components of this vector to the given array. If no array is provided,
2120
- * the method returns a new instance.
2121
- *
2122
- * @param {Array<number>} [array=[]] - The target array holding the vector components.
2123
- * @param {number} [offset=0] - Index of the first element in the array.
2124
- * @return {Array<number>} The vector components.
2125
- */
2126
- toArray(array = [], offset = 0) {
2127
- array[offset] = this.x;
2128
- array[offset + 1] = this.y;
2129
- array[offset + 2] = this.z;
2130
- return array;
2131
- }
2132
- /**
2133
- * Sets the components of this vector from the given buffer attribute.
2134
- *
2135
- * @param {BufferAttribute} attribute - The buffer attribute holding vector data.
2136
- * @param {number} index - The index into the attribute.
2137
- * @return {Vector3} A reference to this vector.
2138
- */
2139
- fromBufferAttribute(attribute, index) {
2140
- this.x = attribute.getX(index);
2141
- this.y = attribute.getY(index);
2142
- this.z = attribute.getZ(index);
2143
- return this;
2144
- }
2145
- /**
2146
- * Sets each component of this vector to a pseudo-random value between `0` and
2147
- * `1`, excluding `1`.
2148
- *
2149
- * @return {Vector3} A reference to this vector.
2150
- */
2151
- random() {
2152
- this.x = Math.random();
2153
- this.y = Math.random();
2154
- this.z = Math.random();
2155
- return this;
2156
- }
2157
- /**
2158
- * Sets this vector to a uniformly random point on a unit sphere.
2159
- *
2160
- * @return {Vector3} A reference to this vector.
2161
- */
2162
- randomDirection() {
2163
- const theta = Math.random() * Math.PI * 2;
2164
- const u = Math.random() * 2 - 1;
2165
- const c = Math.sqrt(1 - u * u);
2166
- this.x = c * Math.cos(theta);
2167
- this.y = u;
2168
- this.z = c * Math.sin(theta);
2169
- return this;
2170
- }
2171
- *[Symbol.iterator]() {
2172
- yield this.x;
2173
- yield this.y;
2174
- yield this.z;
2175
- }
2176
- };
2177
- var _vector$c = /* @__PURE__ */ new Vector3();
2178
- var _quaternion$5 = /* @__PURE__ */ new Quaternion();
2179
- var Matrix3 = class _Matrix3 {
2180
- static {
2181
- _Matrix3.prototype.isMatrix3 = true;
2182
- }
2183
- /**
2184
- * Constructs a new 3x3 matrix. The arguments are supposed to be
2185
- * in row-major order. If no arguments are provided, the constructor
2186
- * initializes the matrix as an identity matrix.
2187
- *
2188
- * @param {number} [n11] - 1-1 matrix element.
2189
- * @param {number} [n12] - 1-2 matrix element.
2190
- * @param {number} [n13] - 1-3 matrix element.
2191
- * @param {number} [n21] - 2-1 matrix element.
2192
- * @param {number} [n22] - 2-2 matrix element.
2193
- * @param {number} [n23] - 2-3 matrix element.
2194
- * @param {number} [n31] - 3-1 matrix element.
2195
- * @param {number} [n32] - 3-2 matrix element.
2196
- * @param {number} [n33] - 3-3 matrix element.
2197
- */
2198
- constructor(n11, n12, n13, n21, n22, n23, n31, n32, n33) {
2199
- this.elements = [
2200
- 1,
2201
- 0,
2202
- 0,
2203
- 0,
2204
- 1,
2205
- 0,
2206
- 0,
2207
- 0,
2208
- 1
2209
- ];
2210
- if (n11 !== void 0) {
2211
- this.set(n11, n12, n13, n21, n22, n23, n31, n32, n33);
2212
- }
2213
- }
2214
- /**
2215
- * Sets the elements of the matrix.The arguments are supposed to be
2216
- * in row-major order.
2217
- *
2218
- * @param {number} [n11] - 1-1 matrix element.
2219
- * @param {number} [n12] - 1-2 matrix element.
2220
- * @param {number} [n13] - 1-3 matrix element.
2221
- * @param {number} [n21] - 2-1 matrix element.
2222
- * @param {number} [n22] - 2-2 matrix element.
2223
- * @param {number} [n23] - 2-3 matrix element.
2224
- * @param {number} [n31] - 3-1 matrix element.
2225
- * @param {number} [n32] - 3-2 matrix element.
2226
- * @param {number} [n33] - 3-3 matrix element.
2227
- * @return {Matrix3} A reference to this matrix.
2228
- */
2229
- set(n11, n12, n13, n21, n22, n23, n31, n32, n33) {
2230
- const te = this.elements;
2231
- te[0] = n11;
2232
- te[1] = n21;
2233
- te[2] = n31;
2234
- te[3] = n12;
2235
- te[4] = n22;
2236
- te[5] = n32;
2237
- te[6] = n13;
2238
- te[7] = n23;
2239
- te[8] = n33;
2240
- return this;
2241
- }
2242
- /**
2243
- * Sets this matrix to the 3x3 identity matrix.
2244
- *
2245
- * @return {Matrix3} A reference to this matrix.
2246
- */
2247
- identity() {
2248
- this.set(
2249
- 1,
2250
- 0,
2251
- 0,
2252
- 0,
2253
- 1,
2254
- 0,
2255
- 0,
2256
- 0,
2257
- 1
2258
- );
2259
- return this;
2260
- }
2261
- /**
2262
- * Copies the values of the given matrix to this instance.
2263
- *
2264
- * @param {Matrix3} m - The matrix to copy.
2265
- * @return {Matrix3} A reference to this matrix.
2266
- */
2267
- copy(m) {
2268
- const te = this.elements;
2269
- const me = m.elements;
2270
- te[0] = me[0];
2271
- te[1] = me[1];
2272
- te[2] = me[2];
2273
- te[3] = me[3];
2274
- te[4] = me[4];
2275
- te[5] = me[5];
2276
- te[6] = me[6];
2277
- te[7] = me[7];
2278
- te[8] = me[8];
2279
- return this;
2280
- }
2281
- /**
2282
- * Extracts the basis of this matrix into the three axis vectors provided.
2283
- *
2284
- * @param {Vector3} xAxis - The basis's x axis.
2285
- * @param {Vector3} yAxis - The basis's y axis.
2286
- * @param {Vector3} zAxis - The basis's z axis.
2287
- * @return {Matrix3} A reference to this matrix.
2288
- */
2289
- extractBasis(xAxis, yAxis, zAxis) {
2290
- xAxis.setFromMatrix3Column(this, 0);
2291
- yAxis.setFromMatrix3Column(this, 1);
2292
- zAxis.setFromMatrix3Column(this, 2);
2293
- return this;
2294
- }
2295
- /**
2296
- * Set this matrix to the upper 3x3 matrix of the given 4x4 matrix.
2297
- *
2298
- * @param {Matrix4} m - The 4x4 matrix.
2299
- * @return {Matrix3} A reference to this matrix.
2300
- */
2301
- setFromMatrix4(m) {
2302
- const me = m.elements;
2303
- this.set(
2304
- me[0],
2305
- me[4],
2306
- me[8],
2307
- me[1],
2308
- me[5],
2309
- me[9],
2310
- me[2],
2311
- me[6],
2312
- me[10]
2313
- );
2314
- return this;
2315
- }
2316
- /**
2317
- * Post-multiplies this matrix by the given 3x3 matrix.
2318
- *
2319
- * @param {Matrix3} m - The matrix to multiply with.
2320
- * @return {Matrix3} A reference to this matrix.
2321
- */
2322
- multiply(m) {
2323
- return this.multiplyMatrices(this, m);
2324
- }
2325
- /**
2326
- * Pre-multiplies this matrix by the given 3x3 matrix.
2327
- *
2328
- * @param {Matrix3} m - The matrix to multiply with.
2329
- * @return {Matrix3} A reference to this matrix.
2330
- */
2331
- premultiply(m) {
2332
- return this.multiplyMatrices(m, this);
2333
- }
2334
- /**
2335
- * Multiples the given 3x3 matrices and stores the result
2336
- * in this matrix.
2337
- *
2338
- * @param {Matrix3} a - The first matrix.
2339
- * @param {Matrix3} b - The second matrix.
2340
- * @return {Matrix3} A reference to this matrix.
2341
- */
2342
- multiplyMatrices(a, b) {
2343
- const ae = a.elements;
2344
- const be = b.elements;
2345
- const te = this.elements;
2346
- const a11 = ae[0], a12 = ae[3], a13 = ae[6];
2347
- const a21 = ae[1], a22 = ae[4], a23 = ae[7];
2348
- const a31 = ae[2], a32 = ae[5], a33 = ae[8];
2349
- const b11 = be[0], b12 = be[3], b13 = be[6];
2350
- const b21 = be[1], b22 = be[4], b23 = be[7];
2351
- const b31 = be[2], b32 = be[5], b33 = be[8];
2352
- te[0] = a11 * b11 + a12 * b21 + a13 * b31;
2353
- te[3] = a11 * b12 + a12 * b22 + a13 * b32;
2354
- te[6] = a11 * b13 + a12 * b23 + a13 * b33;
2355
- te[1] = a21 * b11 + a22 * b21 + a23 * b31;
2356
- te[4] = a21 * b12 + a22 * b22 + a23 * b32;
2357
- te[7] = a21 * b13 + a22 * b23 + a23 * b33;
2358
- te[2] = a31 * b11 + a32 * b21 + a33 * b31;
2359
- te[5] = a31 * b12 + a32 * b22 + a33 * b32;
2360
- te[8] = a31 * b13 + a32 * b23 + a33 * b33;
2361
- return this;
2362
- }
2363
- /**
2364
- * Multiplies every component of the matrix by the given scalar.
2365
- *
2366
- * @param {number} s - The scalar.
2367
- * @return {Matrix3} A reference to this matrix.
2368
- */
2369
- multiplyScalar(s) {
2370
- const te = this.elements;
2371
- te[0] *= s;
2372
- te[3] *= s;
2373
- te[6] *= s;
2374
- te[1] *= s;
2375
- te[4] *= s;
2376
- te[7] *= s;
2377
- te[2] *= s;
2378
- te[5] *= s;
2379
- te[8] *= s;
2380
- return this;
2381
- }
2382
- /**
2383
- * Computes and returns the determinant of this matrix.
2384
- *
2385
- * @return {number} The determinant.
2386
- */
2387
- determinant() {
2388
- const te = this.elements;
2389
- const a = te[0], b = te[1], c = te[2], d = te[3], e = te[4], f = te[5], g = te[6], h = te[7], i = te[8];
2390
- return a * e * i - a * f * h - b * d * i + b * f * g + c * d * h - c * e * g;
2391
- }
2392
- /**
2393
- * Inverts this matrix, using the [analytic method](https://en.wikipedia.org/wiki/Invertible_matrix#Analytic_solution).
2394
- * You can not invert with a determinant of zero. If you attempt this, the method produces
2395
- * a zero matrix instead.
2396
- *
2397
- * @return {Matrix3} A reference to this matrix.
2398
- */
2399
- invert() {
2400
- const te = this.elements, n11 = te[0], n21 = te[1], n31 = te[2], n12 = te[3], n22 = te[4], n32 = te[5], n13 = te[6], n23 = te[7], n33 = te[8], t11 = n33 * n22 - n32 * n23, t12 = n32 * n13 - n33 * n12, t13 = n23 * n12 - n22 * n13, det = n11 * t11 + n21 * t12 + n31 * t13;
2401
- if (det === 0) return this.set(0, 0, 0, 0, 0, 0, 0, 0, 0);
2402
- const detInv = 1 / det;
2403
- te[0] = t11 * detInv;
2404
- te[1] = (n31 * n23 - n33 * n21) * detInv;
2405
- te[2] = (n32 * n21 - n31 * n22) * detInv;
2406
- te[3] = t12 * detInv;
2407
- te[4] = (n33 * n11 - n31 * n13) * detInv;
2408
- te[5] = (n31 * n12 - n32 * n11) * detInv;
2409
- te[6] = t13 * detInv;
2410
- te[7] = (n21 * n13 - n23 * n11) * detInv;
2411
- te[8] = (n22 * n11 - n21 * n12) * detInv;
2412
- return this;
2413
- }
2414
- /**
2415
- * Transposes this matrix in place.
2416
- *
2417
- * @return {Matrix3} A reference to this matrix.
2418
- */
2419
- transpose() {
2420
- let tmp;
2421
- const m = this.elements;
2422
- tmp = m[1];
2423
- m[1] = m[3];
2424
- m[3] = tmp;
2425
- tmp = m[2];
2426
- m[2] = m[6];
2427
- m[6] = tmp;
2428
- tmp = m[5];
2429
- m[5] = m[7];
2430
- m[7] = tmp;
2431
- return this;
2432
- }
2433
- /**
2434
- * Computes the normal matrix which is the inverse transpose of the upper
2435
- * left 3x3 portion of the given 4x4 matrix.
2436
- *
2437
- * @param {Matrix4} matrix4 - The 4x4 matrix.
2438
- * @return {Matrix3} A reference to this matrix.
2439
- */
2440
- getNormalMatrix(matrix4) {
2441
- return this.setFromMatrix4(matrix4).invert().transpose();
2442
- }
2443
- /**
2444
- * Transposes this matrix into the supplied array, and returns itself unchanged.
2445
- *
2446
- * @param {Array<number>} r - An array to store the transposed matrix elements.
2447
- * @return {Matrix3} A reference to this matrix.
2448
- */
2449
- transposeIntoArray(r) {
2450
- const m = this.elements;
2451
- r[0] = m[0];
2452
- r[1] = m[3];
2453
- r[2] = m[6];
2454
- r[3] = m[1];
2455
- r[4] = m[4];
2456
- r[5] = m[7];
2457
- r[6] = m[2];
2458
- r[7] = m[5];
2459
- r[8] = m[8];
2460
- return this;
2461
- }
2462
- /**
2463
- * Sets the UV transform matrix from offset, repeat, rotation, and center.
2464
- *
2465
- * @param {number} tx - Offset x.
2466
- * @param {number} ty - Offset y.
2467
- * @param {number} sx - Repeat x.
2468
- * @param {number} sy - Repeat y.
2469
- * @param {number} rotation - Rotation, in radians. Positive values rotate counterclockwise.
2470
- * @param {number} cx - Center x of rotation.
2471
- * @param {number} cy - Center y of rotation
2472
- * @return {Matrix3} A reference to this matrix.
2473
- */
2474
- setUvTransform(tx, ty, sx, sy, rotation, cx, cy) {
2475
- const c = Math.cos(rotation);
2476
- const s = Math.sin(rotation);
2477
- this.set(
2478
- sx * c,
2479
- sx * s,
2480
- -sx * (c * cx + s * cy) + cx + tx,
2481
- -sy * s,
2482
- sy * c,
2483
- -sy * (-s * cx + c * cy) + cy + ty,
2484
- 0,
2485
- 0,
2486
- 1
2487
- );
2488
- return this;
2489
- }
2490
- /**
2491
- * Scales this matrix with the given scalar values.
2492
- *
2493
- * @deprecated
2494
- * @param {number} sx - The amount to scale in the X axis.
2495
- * @param {number} sy - The amount to scale in the Y axis.
2496
- * @return {Matrix3} A reference to this matrix.
2497
- */
2498
- scale(sx, sy) {
2499
- warnOnce("Matrix3: .scale() is deprecated. Use .makeScale() instead.");
2500
- this.premultiply(_m3.makeScale(sx, sy));
2501
- return this;
2502
- }
2503
- /**
2504
- * Rotates this matrix by the given angle.
2505
- *
2506
- * @deprecated
2507
- * @param {number} theta - The rotation in radians.
2508
- * @return {Matrix3} A reference to this matrix.
2509
- */
2510
- rotate(theta) {
2511
- warnOnce("Matrix3: .rotate() is deprecated. Use .makeRotation() instead.");
2512
- this.premultiply(_m3.makeRotation(-theta));
2513
- return this;
2514
- }
2515
- /**
2516
- * Translates this matrix by the given scalar values.
2517
- *
2518
- * @deprecated
2519
- * @param {number} tx - The amount to translate in the X axis.
2520
- * @param {number} ty - The amount to translate in the Y axis.
2521
- * @return {Matrix3} A reference to this matrix.
2522
- */
2523
- translate(tx, ty) {
2524
- warnOnce("Matrix3: .translate() is deprecated. Use .makeTranslation() instead.");
2525
- this.premultiply(_m3.makeTranslation(tx, ty));
2526
- return this;
2527
- }
2528
- // for 2D Transforms
2529
- /**
2530
- * Sets this matrix as a 2D translation transform.
2531
- *
2532
- * @param {number|Vector2} x - The amount to translate in the X axis or alternatively a translation vector.
2533
- * @param {number} y - The amount to translate in the Y axis.
2534
- * @return {Matrix3} A reference to this matrix.
2535
- */
2536
- makeTranslation(x, y) {
2537
- if (x.isVector2) {
2538
- this.set(
2539
- 1,
2540
- 0,
2541
- x.x,
2542
- 0,
2543
- 1,
2544
- x.y,
2545
- 0,
2546
- 0,
2547
- 1
2548
- );
2549
- } else {
2550
- this.set(
2551
- 1,
2552
- 0,
2553
- x,
2554
- 0,
2555
- 1,
2556
- y,
2557
- 0,
2558
- 0,
2559
- 1
2560
- );
2561
- }
2562
- return this;
2563
- }
2564
- /**
2565
- * Sets this matrix as a 2D rotational transformation.
2566
- *
2567
- * @param {number} theta - The rotation in radians.
2568
- * @return {Matrix3} A reference to this matrix.
2569
- */
2570
- makeRotation(theta) {
2571
- const c = Math.cos(theta);
2572
- const s = Math.sin(theta);
2573
- this.set(
2574
- c,
2575
- -s,
2576
- 0,
2577
- s,
2578
- c,
2579
- 0,
2580
- 0,
2581
- 0,
2582
- 1
2583
- );
2584
- return this;
2585
- }
2586
- /**
2587
- * Sets this matrix as a 2D scale transform.
2588
- *
2589
- * @param {number} x - The amount to scale in the X axis.
2590
- * @param {number} y - The amount to scale in the Y axis.
2591
- * @return {Matrix3} A reference to this matrix.
2592
- */
2593
- makeScale(x, y) {
2594
- this.set(
2595
- x,
2596
- 0,
2597
- 0,
2598
- 0,
2599
- y,
2600
- 0,
2601
- 0,
2602
- 0,
2603
- 1
2604
- );
2605
- return this;
2606
- }
2607
- /**
2608
- * Returns `true` if this matrix is equal with the given one.
2609
- *
2610
- * @param {Matrix3} matrix - The matrix to test for equality.
2611
- * @return {boolean} Whether this matrix is equal with the given one.
2612
- */
2613
- equals(matrix) {
2614
- const te = this.elements;
2615
- const me = matrix.elements;
2616
- for (let i = 0; i < 9; i++) {
2617
- if (te[i] !== me[i]) return false;
2618
- }
2619
- return true;
2620
- }
2621
- /**
2622
- * Sets the elements of the matrix from the given array.
2623
- *
2624
- * @param {Array<number>} array - The matrix elements in column-major order.
2625
- * @param {number} [offset=0] - Index of the first element in the array.
2626
- * @return {Matrix3} A reference to this matrix.
2627
- */
2628
- fromArray(array, offset = 0) {
2629
- for (let i = 0; i < 9; i++) {
2630
- this.elements[i] = array[i + offset];
2631
- }
2632
- return this;
2633
- }
2634
- /**
2635
- * Writes the elements of this matrix to the given array. If no array is provided,
2636
- * the method returns a new instance.
2637
- *
2638
- * @param {Array<number>} [array=[]] - The target array holding the matrix elements in column-major order.
2639
- * @param {number} [offset=0] - Index of the first element in the array.
2640
- * @return {Array<number>} The matrix elements in column-major order.
2641
- */
2642
- toArray(array = [], offset = 0) {
2643
- const te = this.elements;
2644
- array[offset] = te[0];
2645
- array[offset + 1] = te[1];
2646
- array[offset + 2] = te[2];
2647
- array[offset + 3] = te[3];
2648
- array[offset + 4] = te[4];
2649
- array[offset + 5] = te[5];
2650
- array[offset + 6] = te[6];
2651
- array[offset + 7] = te[7];
2652
- array[offset + 8] = te[8];
2653
- return array;
2654
- }
2655
- /**
2656
- * Returns a matrix with copied values from this instance.
2657
- *
2658
- * @return {Matrix3} A clone of this instance.
2659
- */
2660
- clone() {
2661
- return new this.constructor().fromArray(this.elements);
2662
- }
2663
- };
2664
- var _m3 = /* @__PURE__ */ new Matrix3();
2665
- (class _Vector4 {
2666
- static {
2667
- _Vector4.prototype.isVector4 = true;
2668
- }
2669
- /**
2670
- * Constructs a new 4D vector.
2671
- *
2672
- * @param {number} [x=0] - The x value of this vector.
2673
- * @param {number} [y=0] - The y value of this vector.
2674
- * @param {number} [z=0] - The z value of this vector.
2675
- * @param {number} [w=1] - The w value of this vector.
2676
- */
2677
- constructor(x = 0, y = 0, z = 0, w = 1) {
2678
- this.x = x;
2679
- this.y = y;
2680
- this.z = z;
2681
- this.w = w;
2682
- }
2683
- /**
2684
- * Alias for {@link Vector4#z}.
2685
- *
2686
- * @type {number}
2687
- */
2688
- get width() {
2689
- return this.z;
2690
- }
2691
- set width(value) {
2692
- this.z = value;
2693
- }
2694
- /**
2695
- * Alias for {@link Vector4#w}.
2696
- *
2697
- * @type {number}
2698
- */
2699
- get height() {
2700
- return this.w;
2701
- }
2702
- set height(value) {
2703
- this.w = value;
2704
- }
2705
- /**
2706
- * Sets the vector components.
2707
- *
2708
- * @param {number} x - The value of the x component.
2709
- * @param {number} y - The value of the y component.
2710
- * @param {number} z - The value of the z component.
2711
- * @param {number} w - The value of the w component.
2712
- * @return {Vector4} A reference to this vector.
2713
- */
2714
- set(x, y, z, w) {
2715
- this.x = x;
2716
- this.y = y;
2717
- this.z = z;
2718
- this.w = w;
2719
- return this;
2720
- }
2721
- /**
2722
- * Sets the vector components to the same value.
2723
- *
2724
- * @param {number} scalar - The value to set for all vector components.
2725
- * @return {Vector4} A reference to this vector.
2726
- */
2727
- setScalar(scalar) {
2728
- this.x = scalar;
2729
- this.y = scalar;
2730
- this.z = scalar;
2731
- this.w = scalar;
2732
- return this;
2733
- }
2734
- /**
2735
- * Sets the vector's x component to the given value
2736
- *
2737
- * @param {number} x - The value to set.
2738
- * @return {Vector4} A reference to this vector.
2739
- */
2740
- setX(x) {
2741
- this.x = x;
2742
- return this;
2743
- }
2744
- /**
2745
- * Sets the vector's y component to the given value
2746
- *
2747
- * @param {number} y - The value to set.
2748
- * @return {Vector4} A reference to this vector.
2749
- */
2750
- setY(y) {
2751
- this.y = y;
2752
- return this;
2753
- }
2754
- /**
2755
- * Sets the vector's z component to the given value
2756
- *
2757
- * @param {number} z - The value to set.
2758
- * @return {Vector4} A reference to this vector.
2759
- */
2760
- setZ(z) {
2761
- this.z = z;
2762
- return this;
2763
- }
2764
- /**
2765
- * Sets the vector's w component to the given value
2766
- *
2767
- * @param {number} w - The value to set.
2768
- * @return {Vector4} A reference to this vector.
2769
- */
2770
- setW(w) {
2771
- this.w = w;
2772
- return this;
2773
- }
2774
- /**
2775
- * Allows to set a vector component with an index.
2776
- *
2777
- * @param {number} index - The component index. `0` equals to x, `1` equals to y,
2778
- * `2` equals to z, `3` equals to w.
2779
- * @param {number} value - The value to set.
2780
- * @return {Vector4} A reference to this vector.
2781
- */
2782
- setComponent(index, value) {
2783
- switch (index) {
2784
- case 0:
2785
- this.x = value;
2786
- break;
2787
- case 1:
2788
- this.y = value;
2789
- break;
2790
- case 2:
2791
- this.z = value;
2792
- break;
2793
- case 3:
2794
- this.w = value;
2795
- break;
2796
- default:
2797
- throw new Error("THREE.Vector4: index is out of range: " + index);
2798
- }
2799
- return this;
2800
- }
2801
- /**
2802
- * Returns the value of the vector component which matches the given index.
2803
- *
2804
- * @param {number} index - The component index. `0` equals to x, `1` equals to y,
2805
- * `2` equals to z, `3` equals to w.
2806
- * @return {number} A vector component value.
2807
- */
2808
- getComponent(index) {
2809
- switch (index) {
2810
- case 0:
2811
- return this.x;
2812
- case 1:
2813
- return this.y;
2814
- case 2:
2815
- return this.z;
2816
- case 3:
2817
- return this.w;
2818
- default:
2819
- throw new Error("THREE.Vector4: index is out of range: " + index);
2820
- }
2821
- }
2822
- /**
2823
- * Returns a new vector with copied values from this instance.
2824
- *
2825
- * @return {Vector4} A clone of this instance.
2826
- */
2827
- clone() {
2828
- return new this.constructor(this.x, this.y, this.z, this.w);
2829
- }
2830
- /**
2831
- * Copies the values of the given vector to this instance.
2832
- *
2833
- * @param {Vector3|Vector4} v - The vector to copy.
2834
- * @return {Vector4} A reference to this vector.
2835
- */
2836
- copy(v) {
2837
- this.x = v.x;
2838
- this.y = v.y;
2839
- this.z = v.z;
2840
- this.w = v.w !== void 0 ? v.w : 1;
2841
- return this;
2842
- }
2843
- /**
2844
- * Adds the given vector to this instance.
2845
- *
2846
- * @param {Vector4} v - The vector to add.
2847
- * @return {Vector4} A reference to this vector.
2848
- */
2849
- add(v) {
2850
- this.x += v.x;
2851
- this.y += v.y;
2852
- this.z += v.z;
2853
- this.w += v.w;
2854
- return this;
2855
- }
2856
- /**
2857
- * Adds the given scalar value to all components of this instance.
2858
- *
2859
- * @param {number} s - The scalar to add.
2860
- * @return {Vector4} A reference to this vector.
2861
- */
2862
- addScalar(s) {
2863
- this.x += s;
2864
- this.y += s;
2865
- this.z += s;
2866
- this.w += s;
2867
- return this;
2868
- }
2869
- /**
2870
- * Adds the given vectors and stores the result in this instance.
2871
- *
2872
- * @param {Vector4} a - The first vector.
2873
- * @param {Vector4} b - The second vector.
2874
- * @return {Vector4} A reference to this vector.
2875
- */
2876
- addVectors(a, b) {
2877
- this.x = a.x + b.x;
2878
- this.y = a.y + b.y;
2879
- this.z = a.z + b.z;
2880
- this.w = a.w + b.w;
2881
- return this;
2882
- }
2883
- /**
2884
- * Adds the given vector scaled by the given factor to this instance.
2885
- *
2886
- * @param {Vector4} v - The vector.
2887
- * @param {number} s - The factor that scales `v`.
2888
- * @return {Vector4} A reference to this vector.
2889
- */
2890
- addScaledVector(v, s) {
2891
- this.x += v.x * s;
2892
- this.y += v.y * s;
2893
- this.z += v.z * s;
2894
- this.w += v.w * s;
2895
- return this;
2896
- }
2897
- /**
2898
- * Subtracts the given vector from this instance.
2899
- *
2900
- * @param {Vector4} v - The vector to subtract.
2901
- * @return {Vector4} A reference to this vector.
2902
- */
2903
- sub(v) {
2904
- this.x -= v.x;
2905
- this.y -= v.y;
2906
- this.z -= v.z;
2907
- this.w -= v.w;
2908
- return this;
2909
- }
2910
- /**
2911
- * Subtracts the given scalar value from all components of this instance.
2912
- *
2913
- * @param {number} s - The scalar to subtract.
2914
- * @return {Vector4} A reference to this vector.
2915
- */
2916
- subScalar(s) {
2917
- this.x -= s;
2918
- this.y -= s;
2919
- this.z -= s;
2920
- this.w -= s;
2921
- return this;
2922
- }
2923
- /**
2924
- * Subtracts the given vectors and stores the result in this instance.
2925
- *
2926
- * @param {Vector4} a - The first vector.
2927
- * @param {Vector4} b - The second vector.
2928
- * @return {Vector4} A reference to this vector.
2929
- */
2930
- subVectors(a, b) {
2931
- this.x = a.x - b.x;
2932
- this.y = a.y - b.y;
2933
- this.z = a.z - b.z;
2934
- this.w = a.w - b.w;
2935
- return this;
2936
- }
2937
- /**
2938
- * Multiplies the given vector with this instance.
2939
- *
2940
- * @param {Vector4} v - The vector to multiply.
2941
- * @return {Vector4} A reference to this vector.
2942
- */
2943
- multiply(v) {
2944
- this.x *= v.x;
2945
- this.y *= v.y;
2946
- this.z *= v.z;
2947
- this.w *= v.w;
2948
- return this;
2949
- }
2950
- /**
2951
- * Multiplies the given scalar value with all components of this instance.
2952
- *
2953
- * @param {number} scalar - The scalar to multiply.
2954
- * @return {Vector4} A reference to this vector.
2955
- */
2956
- multiplyScalar(scalar) {
2957
- this.x *= scalar;
2958
- this.y *= scalar;
2959
- this.z *= scalar;
2960
- this.w *= scalar;
2961
- return this;
2962
- }
2963
- /**
2964
- * Multiplies this vector with the given 4x4 matrix.
2965
- *
2966
- * @param {Matrix4} m - The 4x4 matrix.
2967
- * @return {Vector4} A reference to this vector.
2968
- */
2969
- applyMatrix4(m) {
2970
- const x = this.x, y = this.y, z = this.z, w = this.w;
2971
- const e = m.elements;
2972
- this.x = e[0] * x + e[4] * y + e[8] * z + e[12] * w;
2973
- this.y = e[1] * x + e[5] * y + e[9] * z + e[13] * w;
2974
- this.z = e[2] * x + e[6] * y + e[10] * z + e[14] * w;
2975
- this.w = e[3] * x + e[7] * y + e[11] * z + e[15] * w;
2976
- return this;
2977
- }
2978
- /**
2979
- * Divides this instance by the given vector.
2980
- *
2981
- * @param {Vector4} v - The vector to divide.
2982
- * @return {Vector4} A reference to this vector.
2983
- */
2984
- divide(v) {
2985
- this.x /= v.x;
2986
- this.y /= v.y;
2987
- this.z /= v.z;
2988
- this.w /= v.w;
2989
- return this;
2990
- }
2991
- /**
2992
- * Divides this vector by the given scalar.
2993
- *
2994
- * @param {number} scalar - The scalar to divide.
2995
- * @return {Vector4} A reference to this vector.
2996
- */
2997
- divideScalar(scalar) {
2998
- return this.multiplyScalar(1 / scalar);
2999
- }
3000
- /**
3001
- * Sets the x, y and z components of this
3002
- * vector to the quaternion's axis and w to the angle.
3003
- *
3004
- * @param {Quaternion} q - The Quaternion to set.
3005
- * @return {Vector4} A reference to this vector.
3006
- */
3007
- setAxisAngleFromQuaternion(q) {
3008
- this.w = 2 * Math.acos(q.w);
3009
- const s = Math.sqrt(1 - q.w * q.w);
3010
- if (s < 1e-4) {
3011
- this.x = 1;
3012
- this.y = 0;
3013
- this.z = 0;
3014
- } else {
3015
- this.x = q.x / s;
3016
- this.y = q.y / s;
3017
- this.z = q.z / s;
3018
- }
3019
- return this;
3020
- }
3021
- /**
3022
- * Sets the x, y and z components of this
3023
- * vector to the axis of rotation and w to the angle.
3024
- *
3025
- * @param {Matrix4} m - A 4x4 matrix of which the upper left 3x3 matrix is a pure rotation matrix.
3026
- * @return {Vector4} A reference to this vector.
3027
- */
3028
- setAxisAngleFromRotationMatrix(m) {
3029
- let angle, x, y, z;
3030
- const epsilon = 0.01, epsilon2 = 0.1, te = m.elements, m11 = te[0], m12 = te[4], m13 = te[8], m21 = te[1], m22 = te[5], m23 = te[9], m31 = te[2], m32 = te[6], m33 = te[10];
3031
- if (Math.abs(m12 - m21) < epsilon && Math.abs(m13 - m31) < epsilon && Math.abs(m23 - m32) < epsilon) {
3032
- if (Math.abs(m12 + m21) < epsilon2 && Math.abs(m13 + m31) < epsilon2 && Math.abs(m23 + m32) < epsilon2 && Math.abs(m11 + m22 + m33 - 3) < epsilon2) {
3033
- this.set(1, 0, 0, 0);
3034
- return this;
3035
- }
3036
- angle = Math.PI;
3037
- const xx = (m11 + 1) / 2;
3038
- const yy = (m22 + 1) / 2;
3039
- const zz = (m33 + 1) / 2;
3040
- const xy = (m12 + m21) / 4;
3041
- const xz = (m13 + m31) / 4;
3042
- const yz = (m23 + m32) / 4;
3043
- if (xx > yy && xx > zz) {
3044
- if (xx < epsilon) {
3045
- x = 0;
3046
- y = 0.707106781;
3047
- z = 0.707106781;
3048
- } else {
3049
- x = Math.sqrt(xx);
3050
- y = xy / x;
3051
- z = xz / x;
3052
- }
3053
- } else if (yy > zz) {
3054
- if (yy < epsilon) {
3055
- x = 0.707106781;
3056
- y = 0;
3057
- z = 0.707106781;
3058
- } else {
3059
- y = Math.sqrt(yy);
3060
- x = xy / y;
3061
- z = yz / y;
3062
- }
3063
- } else {
3064
- if (zz < epsilon) {
3065
- x = 0.707106781;
3066
- y = 0.707106781;
3067
- z = 0;
3068
- } else {
3069
- z = Math.sqrt(zz);
3070
- x = xz / z;
3071
- y = yz / z;
3072
- }
3073
- }
3074
- this.set(x, y, z, angle);
3075
- return this;
3076
- }
3077
- let s = Math.sqrt((m32 - m23) * (m32 - m23) + (m13 - m31) * (m13 - m31) + (m21 - m12) * (m21 - m12));
3078
- if (Math.abs(s) < 1e-3) s = 1;
3079
- this.x = (m32 - m23) / s;
3080
- this.y = (m13 - m31) / s;
3081
- this.z = (m21 - m12) / s;
3082
- this.w = Math.acos((m11 + m22 + m33 - 1) / 2);
3083
- return this;
3084
- }
3085
- /**
3086
- * Sets the vector components to the position elements of the
3087
- * given transformation matrix.
3088
- *
3089
- * @param {Matrix4} m - The 4x4 matrix.
3090
- * @return {Vector4} A reference to this vector.
3091
- */
3092
- setFromMatrixPosition(m) {
3093
- const e = m.elements;
3094
- this.x = e[12];
3095
- this.y = e[13];
3096
- this.z = e[14];
3097
- this.w = e[15];
3098
- return this;
3099
- }
3100
- /**
3101
- * If this vector's x, y, z or w value is greater than the given vector's x, y, z or w
3102
- * value, replace that value with the corresponding min value.
3103
- *
3104
- * @param {Vector4} v - The vector.
3105
- * @return {Vector4} A reference to this vector.
3106
- */
3107
- min(v) {
3108
- this.x = Math.min(this.x, v.x);
3109
- this.y = Math.min(this.y, v.y);
3110
- this.z = Math.min(this.z, v.z);
3111
- this.w = Math.min(this.w, v.w);
3112
- return this;
3113
- }
3114
- /**
3115
- * If this vector's x, y, z or w value is less than the given vector's x, y, z or w
3116
- * value, replace that value with the corresponding max value.
3117
- *
3118
- * @param {Vector4} v - The vector.
3119
- * @return {Vector4} A reference to this vector.
3120
- */
3121
- max(v) {
3122
- this.x = Math.max(this.x, v.x);
3123
- this.y = Math.max(this.y, v.y);
3124
- this.z = Math.max(this.z, v.z);
3125
- this.w = Math.max(this.w, v.w);
3126
- return this;
3127
- }
3128
- /**
3129
- * If this vector's x, y, z or w value is greater than the max vector's x, y, z or w
3130
- * value, it is replaced by the corresponding value.
3131
- * If this vector's x, y, z or w value is less than the min vector's x, y, z or w value,
3132
- * it is replaced by the corresponding value.
3133
- *
3134
- * @param {Vector4} min - The minimum x, y and z values.
3135
- * @param {Vector4} max - The maximum x, y and z values in the desired range.
3136
- * @return {Vector4} A reference to this vector.
3137
- */
3138
- clamp(min, max) {
3139
- this.x = clamp(this.x, min.x, max.x);
3140
- this.y = clamp(this.y, min.y, max.y);
3141
- this.z = clamp(this.z, min.z, max.z);
3142
- this.w = clamp(this.w, min.w, max.w);
3143
- return this;
3144
- }
3145
- /**
3146
- * If this vector's x, y, z or w values are greater than the max value, they are
3147
- * replaced by the max value.
3148
- * If this vector's x, y, z or w values are less than the min value, they are
3149
- * replaced by the min value.
3150
- *
3151
- * @param {number} minVal - The minimum value the components will be clamped to.
3152
- * @param {number} maxVal - The maximum value the components will be clamped to.
3153
- * @return {Vector4} A reference to this vector.
3154
- */
3155
- clampScalar(minVal, maxVal) {
3156
- this.x = clamp(this.x, minVal, maxVal);
3157
- this.y = clamp(this.y, minVal, maxVal);
3158
- this.z = clamp(this.z, minVal, maxVal);
3159
- this.w = clamp(this.w, minVal, maxVal);
3160
- return this;
3161
- }
3162
- /**
3163
- * If this vector's length is greater than the max value, it is replaced by
3164
- * the max value.
3165
- * If this vector's length is less than the min value, it is replaced by the
3166
- * min value.
3167
- *
3168
- * @param {number} min - The minimum value the vector length will be clamped to.
3169
- * @param {number} max - The maximum value the vector length will be clamped to.
3170
- * @return {Vector4} A reference to this vector.
3171
- */
3172
- clampLength(min, max) {
3173
- const length = this.length();
3174
- return this.divideScalar(length || 1).multiplyScalar(clamp(length, min, max));
3175
- }
3176
- /**
3177
- * The components of this vector are rounded down to the nearest integer value.
3178
- *
3179
- * @return {Vector4} A reference to this vector.
3180
- */
3181
- floor() {
3182
- this.x = Math.floor(this.x);
3183
- this.y = Math.floor(this.y);
3184
- this.z = Math.floor(this.z);
3185
- this.w = Math.floor(this.w);
3186
- return this;
3187
- }
3188
- /**
3189
- * The components of this vector are rounded up to the nearest integer value.
3190
- *
3191
- * @return {Vector4} A reference to this vector.
3192
- */
3193
- ceil() {
3194
- this.x = Math.ceil(this.x);
3195
- this.y = Math.ceil(this.y);
3196
- this.z = Math.ceil(this.z);
3197
- this.w = Math.ceil(this.w);
3198
- return this;
3199
- }
3200
- /**
3201
- * The components of this vector are rounded to the nearest integer value
3202
- *
3203
- * @return {Vector4} A reference to this vector.
3204
- */
3205
- round() {
3206
- this.x = Math.round(this.x);
3207
- this.y = Math.round(this.y);
3208
- this.z = Math.round(this.z);
3209
- this.w = Math.round(this.w);
3210
- return this;
3211
- }
3212
- /**
3213
- * The components of this vector are rounded towards zero (up if negative,
3214
- * down if positive) to an integer value.
3215
- *
3216
- * @return {Vector4} A reference to this vector.
3217
- */
3218
- roundToZero() {
3219
- this.x = Math.trunc(this.x);
3220
- this.y = Math.trunc(this.y);
3221
- this.z = Math.trunc(this.z);
3222
- this.w = Math.trunc(this.w);
3223
- return this;
3224
- }
3225
- /**
3226
- * Inverts this vector - i.e. sets x = -x, y = -y, z = -z, w = -w.
3227
- *
3228
- * @return {Vector4} A reference to this vector.
3229
- */
3230
- negate() {
3231
- this.x = -this.x;
3232
- this.y = -this.y;
3233
- this.z = -this.z;
3234
- this.w = -this.w;
3235
- return this;
3236
- }
3237
- /**
3238
- * Calculates the dot product of the given vector with this instance.
3239
- *
3240
- * @param {Vector4} v - The vector to compute the dot product with.
3241
- * @return {number} The result of the dot product.
3242
- */
3243
- dot(v) {
3244
- return this.x * v.x + this.y * v.y + this.z * v.z + this.w * v.w;
3245
- }
3246
- /**
3247
- * Computes the square of the Euclidean length (straight-line length) from
3248
- * (0, 0, 0, 0) to (x, y, z, w). If you are comparing the lengths of vectors, you should
3249
- * compare the length squared instead as it is slightly more efficient to calculate.
3250
- *
3251
- * @return {number} The square length of this vector.
3252
- */
3253
- lengthSq() {
3254
- return this.x * this.x + this.y * this.y + this.z * this.z + this.w * this.w;
3255
- }
3256
- /**
3257
- * Computes the Euclidean length (straight-line length) from (0, 0, 0, 0) to (x, y, z, w).
3258
- *
3259
- * @return {number} The length of this vector.
3260
- */
3261
- length() {
3262
- return Math.sqrt(this.x * this.x + this.y * this.y + this.z * this.z + this.w * this.w);
3263
- }
3264
- /**
3265
- * Computes the Manhattan length of this vector.
3266
- *
3267
- * @return {number} The length of this vector.
3268
- */
3269
- manhattanLength() {
3270
- return Math.abs(this.x) + Math.abs(this.y) + Math.abs(this.z) + Math.abs(this.w);
3271
- }
3272
- /**
3273
- * Converts this vector to a unit vector - that is, sets it equal to a vector
3274
- * with the same direction as this one, but with a vector length of `1`.
3275
- *
3276
- * @return {Vector4} A reference to this vector.
3277
- */
3278
- normalize() {
3279
- return this.divideScalar(this.length() || 1);
3280
- }
3281
- /**
3282
- * Sets this vector to a vector with the same direction as this one, but
3283
- * with the specified length.
3284
- *
3285
- * @param {number} length - The new length of this vector.
3286
- * @return {Vector4} A reference to this vector.
3287
- */
3288
- setLength(length) {
3289
- return this.normalize().multiplyScalar(length);
3290
- }
3291
- /**
3292
- * Linearly interpolates between the given vector and this instance, where
3293
- * alpha is the percent distance along the line - alpha = 0 will be this
3294
- * vector, and alpha = 1 will be the given one.
3295
- *
3296
- * @param {Vector4} v - The vector to interpolate towards.
3297
- * @param {number} alpha - The interpolation factor, typically in the closed interval `[0, 1]`.
3298
- * @return {Vector4} A reference to this vector.
3299
- */
3300
- lerp(v, alpha) {
3301
- this.x += (v.x - this.x) * alpha;
3302
- this.y += (v.y - this.y) * alpha;
3303
- this.z += (v.z - this.z) * alpha;
3304
- this.w += (v.w - this.w) * alpha;
3305
- return this;
3306
- }
3307
- /**
3308
- * Linearly interpolates between the given vectors, where alpha is the percent
3309
- * distance along the line - alpha = 0 will be first vector, and alpha = 1 will
3310
- * be the second one. The result is stored in this instance.
3311
- *
3312
- * @param {Vector4} v1 - The first vector.
3313
- * @param {Vector4} v2 - The second vector.
3314
- * @param {number} alpha - The interpolation factor, typically in the closed interval `[0, 1]`.
3315
- * @return {Vector4} A reference to this vector.
3316
- */
3317
- lerpVectors(v1, v2, alpha) {
3318
- this.x = v1.x + (v2.x - v1.x) * alpha;
3319
- this.y = v1.y + (v2.y - v1.y) * alpha;
3320
- this.z = v1.z + (v2.z - v1.z) * alpha;
3321
- this.w = v1.w + (v2.w - v1.w) * alpha;
3322
- return this;
3323
- }
3324
- /**
3325
- * Returns `true` if this vector is equal with the given one.
3326
- *
3327
- * @param {Vector4} v - The vector to test for equality.
3328
- * @return {boolean} Whether this vector is equal with the given one.
3329
- */
3330
- equals(v) {
3331
- return v.x === this.x && v.y === this.y && v.z === this.z && v.w === this.w;
3332
- }
3333
- /**
3334
- * Sets this vector's x value to be `array[ offset ]`, y value to be `array[ offset + 1 ]`,
3335
- * z value to be `array[ offset + 2 ]`, w value to be `array[ offset + 3 ]`.
3336
- *
3337
- * @param {Array<number>} array - An array holding the vector component values.
3338
- * @param {number} [offset=0] - The offset into the array.
3339
- * @return {Vector4} A reference to this vector.
3340
- */
3341
- fromArray(array, offset = 0) {
3342
- this.x = array[offset];
3343
- this.y = array[offset + 1];
3344
- this.z = array[offset + 2];
3345
- this.w = array[offset + 3];
3346
- return this;
3347
- }
3348
- /**
3349
- * Writes the components of this vector to the given array. If no array is provided,
3350
- * the method returns a new instance.
3351
- *
3352
- * @param {Array<number>} [array=[]] - The target array holding the vector components.
3353
- * @param {number} [offset=0] - Index of the first element in the array.
3354
- * @return {Array<number>} The vector components.
3355
- */
3356
- toArray(array = [], offset = 0) {
3357
- array[offset] = this.x;
3358
- array[offset + 1] = this.y;
3359
- array[offset + 2] = this.z;
3360
- array[offset + 3] = this.w;
3361
- return array;
3362
- }
3363
- /**
3364
- * Sets the components of this vector from the given buffer attribute.
3365
- *
3366
- * @param {BufferAttribute} attribute - The buffer attribute holding vector data.
3367
- * @param {number} index - The index into the attribute.
3368
- * @return {Vector4} A reference to this vector.
3369
- */
3370
- fromBufferAttribute(attribute, index) {
3371
- this.x = attribute.getX(index);
3372
- this.y = attribute.getY(index);
3373
- this.z = attribute.getZ(index);
3374
- this.w = attribute.getW(index);
3375
- return this;
3376
- }
3377
- /**
3378
- * Sets each component of this vector to a pseudo-random value between `0` and
3379
- * `1`, excluding `1`.
3380
- *
3381
- * @return {Vector4} A reference to this vector.
3382
- */
3383
- random() {
3384
- this.x = Math.random();
3385
- this.y = Math.random();
3386
- this.z = Math.random();
3387
- this.w = Math.random();
3388
- return this;
3389
- }
3390
- *[Symbol.iterator]() {
3391
- yield this.x;
3392
- yield this.y;
3393
- yield this.z;
3394
- yield this.w;
3395
- }
3396
- });
3397
- var Matrix4 = class _Matrix4 {
3398
- static {
3399
- _Matrix4.prototype.isMatrix4 = true;
3400
- }
3401
- /**
3402
- * Constructs a new 4x4 matrix. The arguments are supposed to be
3403
- * in row-major order. If no arguments are provided, the constructor
3404
- * initializes the matrix as an identity matrix.
3405
- *
3406
- * @param {number} [n11] - 1-1 matrix element.
3407
- * @param {number} [n12] - 1-2 matrix element.
3408
- * @param {number} [n13] - 1-3 matrix element.
3409
- * @param {number} [n14] - 1-4 matrix element.
3410
- * @param {number} [n21] - 2-1 matrix element.
3411
- * @param {number} [n22] - 2-2 matrix element.
3412
- * @param {number} [n23] - 2-3 matrix element.
3413
- * @param {number} [n24] - 2-4 matrix element.
3414
- * @param {number} [n31] - 3-1 matrix element.
3415
- * @param {number} [n32] - 3-2 matrix element.
3416
- * @param {number} [n33] - 3-3 matrix element.
3417
- * @param {number} [n34] - 3-4 matrix element.
3418
- * @param {number} [n41] - 4-1 matrix element.
3419
- * @param {number} [n42] - 4-2 matrix element.
3420
- * @param {number} [n43] - 4-3 matrix element.
3421
- * @param {number} [n44] - 4-4 matrix element.
3422
- */
3423
- constructor(n11, n12, n13, n14, n21, n22, n23, n24, n31, n32, n33, n34, n41, n42, n43, n44) {
3424
- this.elements = [
3425
- 1,
3426
- 0,
3427
- 0,
3428
- 0,
3429
- 0,
3430
- 1,
3431
- 0,
3432
- 0,
3433
- 0,
3434
- 0,
3435
- 1,
3436
- 0,
3437
- 0,
3438
- 0,
3439
- 0,
3440
- 1
3441
- ];
3442
- if (n11 !== void 0) {
3443
- this.set(n11, n12, n13, n14, n21, n22, n23, n24, n31, n32, n33, n34, n41, n42, n43, n44);
3444
- }
3445
- }
3446
- /**
3447
- * Sets the elements of the matrix.The arguments are supposed to be
3448
- * in row-major order.
3449
- *
3450
- * @param {number} [n11] - 1-1 matrix element.
3451
- * @param {number} [n12] - 1-2 matrix element.
3452
- * @param {number} [n13] - 1-3 matrix element.
3453
- * @param {number} [n14] - 1-4 matrix element.
3454
- * @param {number} [n21] - 2-1 matrix element.
3455
- * @param {number} [n22] - 2-2 matrix element.
3456
- * @param {number} [n23] - 2-3 matrix element.
3457
- * @param {number} [n24] - 2-4 matrix element.
3458
- * @param {number} [n31] - 3-1 matrix element.
3459
- * @param {number} [n32] - 3-2 matrix element.
3460
- * @param {number} [n33] - 3-3 matrix element.
3461
- * @param {number} [n34] - 3-4 matrix element.
3462
- * @param {number} [n41] - 4-1 matrix element.
3463
- * @param {number} [n42] - 4-2 matrix element.
3464
- * @param {number} [n43] - 4-3 matrix element.
3465
- * @param {number} [n44] - 4-4 matrix element.
3466
- * @return {Matrix4} A reference to this matrix.
3467
- */
3468
- set(n11, n12, n13, n14, n21, n22, n23, n24, n31, n32, n33, n34, n41, n42, n43, n44) {
3469
- const te = this.elements;
3470
- te[0] = n11;
3471
- te[4] = n12;
3472
- te[8] = n13;
3473
- te[12] = n14;
3474
- te[1] = n21;
3475
- te[5] = n22;
3476
- te[9] = n23;
3477
- te[13] = n24;
3478
- te[2] = n31;
3479
- te[6] = n32;
3480
- te[10] = n33;
3481
- te[14] = n34;
3482
- te[3] = n41;
3483
- te[7] = n42;
3484
- te[11] = n43;
3485
- te[15] = n44;
3486
- return this;
3487
- }
3488
- /**
3489
- * Sets this matrix to the 4x4 identity matrix.
3490
- *
3491
- * @return {Matrix4} A reference to this matrix.
3492
- */
3493
- identity() {
3494
- this.set(
3495
- 1,
3496
- 0,
3497
- 0,
3498
- 0,
3499
- 0,
3500
- 1,
3501
- 0,
3502
- 0,
3503
- 0,
3504
- 0,
3505
- 1,
3506
- 0,
3507
- 0,
3508
- 0,
3509
- 0,
3510
- 1
3511
- );
3512
- return this;
3513
- }
3514
- /**
3515
- * Returns a matrix with copied values from this instance.
3516
- *
3517
- * @return {Matrix4} A clone of this instance.
3518
- */
3519
- clone() {
3520
- return new _Matrix4().fromArray(this.elements);
3521
- }
3522
- /**
3523
- * Copies the values of the given matrix to this instance.
3524
- *
3525
- * @param {Matrix4} m - The matrix to copy.
3526
- * @return {Matrix4} A reference to this matrix.
3527
- */
3528
- copy(m) {
3529
- const te = this.elements;
3530
- const me = m.elements;
3531
- te[0] = me[0];
3532
- te[1] = me[1];
3533
- te[2] = me[2];
3534
- te[3] = me[3];
3535
- te[4] = me[4];
3536
- te[5] = me[5];
3537
- te[6] = me[6];
3538
- te[7] = me[7];
3539
- te[8] = me[8];
3540
- te[9] = me[9];
3541
- te[10] = me[10];
3542
- te[11] = me[11];
3543
- te[12] = me[12];
3544
- te[13] = me[13];
3545
- te[14] = me[14];
3546
- te[15] = me[15];
3547
- return this;
3548
- }
3549
- /**
3550
- * Copies the translation component of the given matrix
3551
- * into this matrix's translation component.
3552
- *
3553
- * @param {Matrix4} m - The matrix to copy the translation component.
3554
- * @return {Matrix4} A reference to this matrix.
3555
- */
3556
- copyPosition(m) {
3557
- const te = this.elements, me = m.elements;
3558
- te[12] = me[12];
3559
- te[13] = me[13];
3560
- te[14] = me[14];
3561
- return this;
3562
- }
3563
- /**
3564
- * Set the upper 3x3 elements of this matrix to the values of given 3x3 matrix.
3565
- *
3566
- * @param {Matrix3} m - The 3x3 matrix.
3567
- * @return {Matrix4} A reference to this matrix.
3568
- */
3569
- setFromMatrix3(m) {
3570
- const me = m.elements;
3571
- this.set(
3572
- me[0],
3573
- me[3],
3574
- me[6],
3575
- 0,
3576
- me[1],
3577
- me[4],
3578
- me[7],
3579
- 0,
3580
- me[2],
3581
- me[5],
3582
- me[8],
3583
- 0,
3584
- 0,
3585
- 0,
3586
- 0,
3587
- 1
3588
- );
3589
- return this;
3590
- }
3591
- /**
3592
- * Extracts the basis of this matrix into the three axis vectors provided.
3593
- *
3594
- * @param {Vector3} xAxis - The basis's x axis.
3595
- * @param {Vector3} yAxis - The basis's y axis.
3596
- * @param {Vector3} zAxis - The basis's z axis.
3597
- * @return {Matrix4} A reference to this matrix.
3598
- */
3599
- extractBasis(xAxis, yAxis, zAxis) {
3600
- if (this.determinantAffine() === 0) {
3601
- xAxis.set(1, 0, 0);
3602
- yAxis.set(0, 1, 0);
3603
- zAxis.set(0, 0, 1);
3604
- return this;
3605
- }
3606
- xAxis.setFromMatrixColumn(this, 0);
3607
- yAxis.setFromMatrixColumn(this, 1);
3608
- zAxis.setFromMatrixColumn(this, 2);
3609
- return this;
3610
- }
3611
- /**
3612
- * Sets the given basis vectors to this matrix.
3613
- *
3614
- * @param {Vector3} xAxis - The basis's x axis.
3615
- * @param {Vector3} yAxis - The basis's y axis.
3616
- * @param {Vector3} zAxis - The basis's z axis.
3617
- * @return {Matrix4} A reference to this matrix.
3618
- */
3619
- makeBasis(xAxis, yAxis, zAxis) {
3620
- this.set(
3621
- xAxis.x,
3622
- yAxis.x,
3623
- zAxis.x,
3624
- 0,
3625
- xAxis.y,
3626
- yAxis.y,
3627
- zAxis.y,
3628
- 0,
3629
- xAxis.z,
3630
- yAxis.z,
3631
- zAxis.z,
3632
- 0,
3633
- 0,
3634
- 0,
3635
- 0,
3636
- 1
3637
- );
3638
- return this;
3639
- }
3640
- /**
3641
- * Extracts the rotation component of the given matrix
3642
- * into this matrix's rotation component.
3643
- *
3644
- * Note: This method does not support reflection matrices.
3645
- *
3646
- * @param {Matrix4} m - The matrix.
3647
- * @return {Matrix4} A reference to this matrix.
3648
- */
3649
- extractRotation(m) {
3650
- if (m.determinantAffine() === 0) {
3651
- return this.identity();
3652
- }
3653
- const te = this.elements;
3654
- const me = m.elements;
3655
- const scaleX = 1 / _v1$7.setFromMatrixColumn(m, 0).length();
3656
- const scaleY = 1 / _v1$7.setFromMatrixColumn(m, 1).length();
3657
- const scaleZ = 1 / _v1$7.setFromMatrixColumn(m, 2).length();
3658
- te[0] = me[0] * scaleX;
3659
- te[1] = me[1] * scaleX;
3660
- te[2] = me[2] * scaleX;
3661
- te[3] = 0;
3662
- te[4] = me[4] * scaleY;
3663
- te[5] = me[5] * scaleY;
3664
- te[6] = me[6] * scaleY;
3665
- te[7] = 0;
3666
- te[8] = me[8] * scaleZ;
3667
- te[9] = me[9] * scaleZ;
3668
- te[10] = me[10] * scaleZ;
3669
- te[11] = 0;
3670
- te[12] = 0;
3671
- te[13] = 0;
3672
- te[14] = 0;
3673
- te[15] = 1;
3674
- return this;
3675
- }
3676
- /**
3677
- * Sets the rotation component (the upper left 3x3 matrix) of this matrix to
3678
- * the rotation specified by the given Euler angles. The rest of
3679
- * the matrix is set to the identity. Depending on the {@link Euler#order},
3680
- * there are six possible outcomes. See [this page](https://en.wikipedia.org/wiki/Euler_angles#Rotation_matrix)
3681
- * for a complete list.
3682
- *
3683
- * @param {Euler} euler - The Euler angles.
3684
- * @return {Matrix4} A reference to this matrix.
3685
- */
3686
- makeRotationFromEuler(euler) {
3687
- const te = this.elements;
3688
- const x = euler.x, y = euler.y, z = euler.z;
3689
- const a = Math.cos(x), b = Math.sin(x);
3690
- const c = Math.cos(y), d = Math.sin(y);
3691
- const e = Math.cos(z), f = Math.sin(z);
3692
- if (euler.order === "XYZ") {
3693
- const ae = a * e, af = a * f, be = b * e, bf = b * f;
3694
- te[0] = c * e;
3695
- te[4] = -c * f;
3696
- te[8] = d;
3697
- te[1] = af + be * d;
3698
- te[5] = ae - bf * d;
3699
- te[9] = -b * c;
3700
- te[2] = bf - ae * d;
3701
- te[6] = be + af * d;
3702
- te[10] = a * c;
3703
- } else if (euler.order === "YXZ") {
3704
- const ce = c * e, cf = c * f, de = d * e, df = d * f;
3705
- te[0] = ce + df * b;
3706
- te[4] = de * b - cf;
3707
- te[8] = a * d;
3708
- te[1] = a * f;
3709
- te[5] = a * e;
3710
- te[9] = -b;
3711
- te[2] = cf * b - de;
3712
- te[6] = df + ce * b;
3713
- te[10] = a * c;
3714
- } else if (euler.order === "ZXY") {
3715
- const ce = c * e, cf = c * f, de = d * e, df = d * f;
3716
- te[0] = ce - df * b;
3717
- te[4] = -a * f;
3718
- te[8] = de + cf * b;
3719
- te[1] = cf + de * b;
3720
- te[5] = a * e;
3721
- te[9] = df - ce * b;
3722
- te[2] = -a * d;
3723
- te[6] = b;
3724
- te[10] = a * c;
3725
- } else if (euler.order === "ZYX") {
3726
- const ae = a * e, af = a * f, be = b * e, bf = b * f;
3727
- te[0] = c * e;
3728
- te[4] = be * d - af;
3729
- te[8] = ae * d + bf;
3730
- te[1] = c * f;
3731
- te[5] = bf * d + ae;
3732
- te[9] = af * d - be;
3733
- te[2] = -d;
3734
- te[6] = b * c;
3735
- te[10] = a * c;
3736
- } else if (euler.order === "YZX") {
3737
- const ac = a * c, ad = a * d, bc = b * c, bd = b * d;
3738
- te[0] = c * e;
3739
- te[4] = bd - ac * f;
3740
- te[8] = bc * f + ad;
3741
- te[1] = f;
3742
- te[5] = a * e;
3743
- te[9] = -b * e;
3744
- te[2] = -d * e;
3745
- te[6] = ad * f + bc;
3746
- te[10] = ac - bd * f;
3747
- } else if (euler.order === "XZY") {
3748
- const ac = a * c, ad = a * d, bc = b * c, bd = b * d;
3749
- te[0] = c * e;
3750
- te[4] = -f;
3751
- te[8] = d * e;
3752
- te[1] = ac * f + bd;
3753
- te[5] = a * e;
3754
- te[9] = ad * f - bc;
3755
- te[2] = bc * f - ad;
3756
- te[6] = b * e;
3757
- te[10] = bd * f + ac;
3758
- }
3759
- te[3] = 0;
3760
- te[7] = 0;
3761
- te[11] = 0;
3762
- te[12] = 0;
3763
- te[13] = 0;
3764
- te[14] = 0;
3765
- te[15] = 1;
3766
- return this;
3767
- }
3768
- /**
3769
- * Sets the rotation component of this matrix to the rotation specified by
3770
- * the given Quaternion as outlined [here](https://en.wikipedia.org/wiki/Rotation_matrix#Quaternion)
3771
- * The rest of the matrix is set to the identity.
3772
- *
3773
- * @param {Quaternion} q - The Quaternion.
3774
- * @return {Matrix4} A reference to this matrix.
3775
- */
3776
- makeRotationFromQuaternion(q) {
3777
- return this.compose(_zero, q, _one);
3778
- }
3779
- /**
3780
- * Sets the rotation component of the transformation matrix, looking from `eye` towards
3781
- * `target`, and oriented by the up-direction.
3782
- *
3783
- * @param {Vector3} eye - The eye vector.
3784
- * @param {Vector3} target - The target vector.
3785
- * @param {Vector3} up - The up vector.
3786
- * @return {Matrix4} A reference to this matrix.
3787
- */
3788
- lookAt(eye, target, up) {
3789
- const te = this.elements;
3790
- _z.subVectors(eye, target);
3791
- if (_z.lengthSq() === 0) {
3792
- _z.z = 1;
3793
- }
3794
- _z.normalize();
3795
- _x.crossVectors(up, _z);
3796
- if (_x.lengthSq() === 0) {
3797
- if (Math.abs(up.z) === 1) {
3798
- _z.x += 1e-4;
3799
- } else {
3800
- _z.z += 1e-4;
3801
- }
3802
- _z.normalize();
3803
- _x.crossVectors(up, _z);
3804
- }
3805
- _x.normalize();
3806
- _y.crossVectors(_z, _x);
3807
- te[0] = _x.x;
3808
- te[4] = _y.x;
3809
- te[8] = _z.x;
3810
- te[1] = _x.y;
3811
- te[5] = _y.y;
3812
- te[9] = _z.y;
3813
- te[2] = _x.z;
3814
- te[6] = _y.z;
3815
- te[10] = _z.z;
3816
- return this;
3817
- }
3818
- /**
3819
- * Post-multiplies this matrix by the given 4x4 matrix.
3820
- *
3821
- * @param {Matrix4} m - The matrix to multiply with.
3822
- * @return {Matrix4} A reference to this matrix.
3823
- */
3824
- multiply(m) {
3825
- return this.multiplyMatrices(this, m);
3826
- }
3827
- /**
3828
- * Pre-multiplies this matrix by the given 4x4 matrix.
3829
- *
3830
- * @param {Matrix4} m - The matrix to multiply with.
3831
- * @return {Matrix4} A reference to this matrix.
3832
- */
3833
- premultiply(m) {
3834
- return this.multiplyMatrices(m, this);
3835
- }
3836
- /**
3837
- * Multiples the given 4x4 matrices and stores the result
3838
- * in this matrix.
3839
- *
3840
- * @param {Matrix4} a - The first matrix.
3841
- * @param {Matrix4} b - The second matrix.
3842
- * @return {Matrix4} A reference to this matrix.
3843
- */
3844
- multiplyMatrices(a, b) {
3845
- const ae = a.elements;
3846
- const be = b.elements;
3847
- const te = this.elements;
3848
- const a11 = ae[0], a12 = ae[4], a13 = ae[8], a14 = ae[12];
3849
- const a21 = ae[1], a22 = ae[5], a23 = ae[9], a24 = ae[13];
3850
- const a31 = ae[2], a32 = ae[6], a33 = ae[10], a34 = ae[14];
3851
- const a41 = ae[3], a42 = ae[7], a43 = ae[11], a44 = ae[15];
3852
- const b11 = be[0], b12 = be[4], b13 = be[8], b14 = be[12];
3853
- const b21 = be[1], b22 = be[5], b23 = be[9], b24 = be[13];
3854
- const b31 = be[2], b32 = be[6], b33 = be[10], b34 = be[14];
3855
- const b41 = be[3], b42 = be[7], b43 = be[11], b44 = be[15];
3856
- te[0] = a11 * b11 + a12 * b21 + a13 * b31 + a14 * b41;
3857
- te[4] = a11 * b12 + a12 * b22 + a13 * b32 + a14 * b42;
3858
- te[8] = a11 * b13 + a12 * b23 + a13 * b33 + a14 * b43;
3859
- te[12] = a11 * b14 + a12 * b24 + a13 * b34 + a14 * b44;
3860
- te[1] = a21 * b11 + a22 * b21 + a23 * b31 + a24 * b41;
3861
- te[5] = a21 * b12 + a22 * b22 + a23 * b32 + a24 * b42;
3862
- te[9] = a21 * b13 + a22 * b23 + a23 * b33 + a24 * b43;
3863
- te[13] = a21 * b14 + a22 * b24 + a23 * b34 + a24 * b44;
3864
- te[2] = a31 * b11 + a32 * b21 + a33 * b31 + a34 * b41;
3865
- te[6] = a31 * b12 + a32 * b22 + a33 * b32 + a34 * b42;
3866
- te[10] = a31 * b13 + a32 * b23 + a33 * b33 + a34 * b43;
3867
- te[14] = a31 * b14 + a32 * b24 + a33 * b34 + a34 * b44;
3868
- te[3] = a41 * b11 + a42 * b21 + a43 * b31 + a44 * b41;
3869
- te[7] = a41 * b12 + a42 * b22 + a43 * b32 + a44 * b42;
3870
- te[11] = a41 * b13 + a42 * b23 + a43 * b33 + a44 * b43;
3871
- te[15] = a41 * b14 + a42 * b24 + a43 * b34 + a44 * b44;
3872
- return this;
3873
- }
3874
- /**
3875
- * Multiplies every component of the matrix by the given scalar.
3876
- *
3877
- * @param {number} s - The scalar.
3878
- * @return {Matrix4} A reference to this matrix.
3879
- */
3880
- multiplyScalar(s) {
3881
- const te = this.elements;
3882
- te[0] *= s;
3883
- te[4] *= s;
3884
- te[8] *= s;
3885
- te[12] *= s;
3886
- te[1] *= s;
3887
- te[5] *= s;
3888
- te[9] *= s;
3889
- te[13] *= s;
3890
- te[2] *= s;
3891
- te[6] *= s;
3892
- te[10] *= s;
3893
- te[14] *= s;
3894
- te[3] *= s;
3895
- te[7] *= s;
3896
- te[11] *= s;
3897
- te[15] *= s;
3898
- return this;
3899
- }
3900
- /**
3901
- * Computes and returns the determinant of this matrix.
3902
- *
3903
- * Based on the method outlined [here](http://www.euclideanspace.com/maths/algebra/matrix/functions/inverse/fourD/index.html).
3904
- *
3905
- * @return {number} The determinant.
3906
- */
3907
- determinant() {
3908
- const te = this.elements;
3909
- const n11 = te[0], n12 = te[4], n13 = te[8], n14 = te[12];
3910
- const n21 = te[1], n22 = te[5], n23 = te[9], n24 = te[13];
3911
- const n31 = te[2], n32 = te[6], n33 = te[10], n34 = te[14];
3912
- const n41 = te[3], n42 = te[7], n43 = te[11], n44 = te[15];
3913
- const t11 = n23 * n34 - n24 * n33;
3914
- const t12 = n22 * n34 - n24 * n32;
3915
- const t13 = n22 * n33 - n23 * n32;
3916
- const t21 = n21 * n34 - n24 * n31;
3917
- const t22 = n21 * n33 - n23 * n31;
3918
- const t23 = n21 * n32 - n22 * n31;
3919
- return n11 * (n42 * t11 - n43 * t12 + n44 * t13) - n12 * (n41 * t11 - n43 * t21 + n44 * t22) + n13 * (n41 * t12 - n42 * t21 + n44 * t23) - n14 * (n41 * t13 - n42 * t22 + n43 * t23);
3920
- }
3921
- /**
3922
- * Computes and returns the determinant of the 4x4 matrix, but assumes the
3923
- * matrix is affine, saving some computations.
3924
- *
3925
- * For affine matrices (like an object's world matrix), this value equals the
3926
- * full 4x4 {@link Matrix4#determinant} but is cheaper to compute.
3927
- *
3928
- * Assumes the bottom row is [0, 0, 0, 1].
3929
- *
3930
- * @return {number} The determinant of the matrix.
3931
- */
3932
- determinantAffine() {
3933
- const te = this.elements;
3934
- const n11 = te[0], n12 = te[4], n13 = te[8];
3935
- const n21 = te[1], n22 = te[5], n23 = te[9];
3936
- const n31 = te[2], n32 = te[6], n33 = te[10];
3937
- return n11 * (n22 * n33 - n23 * n32) - n12 * (n21 * n33 - n23 * n31) + n13 * (n21 * n32 - n22 * n31);
3938
- }
3939
- /**
3940
- * Transposes this matrix in place.
3941
- *
3942
- * @return {Matrix4} A reference to this matrix.
3943
- */
3944
- transpose() {
3945
- const te = this.elements;
3946
- let tmp;
3947
- tmp = te[1];
3948
- te[1] = te[4];
3949
- te[4] = tmp;
3950
- tmp = te[2];
3951
- te[2] = te[8];
3952
- te[8] = tmp;
3953
- tmp = te[6];
3954
- te[6] = te[9];
3955
- te[9] = tmp;
3956
- tmp = te[3];
3957
- te[3] = te[12];
3958
- te[12] = tmp;
3959
- tmp = te[7];
3960
- te[7] = te[13];
3961
- te[13] = tmp;
3962
- tmp = te[11];
3963
- te[11] = te[14];
3964
- te[14] = tmp;
3965
- return this;
3966
- }
3967
- /**
3968
- * Sets the position component for this matrix from the given vector,
3969
- * without affecting the rest of the matrix.
3970
- *
3971
- * @param {number|Vector3} x - The x component of the vector or alternatively the vector object.
3972
- * @param {number} y - The y component of the vector.
3973
- * @param {number} z - The z component of the vector.
3974
- * @return {Matrix4} A reference to this matrix.
3975
- */
3976
- setPosition(x, y, z) {
3977
- const te = this.elements;
3978
- if (x.isVector3) {
3979
- te[12] = x.x;
3980
- te[13] = x.y;
3981
- te[14] = x.z;
3982
- } else {
3983
- te[12] = x;
3984
- te[13] = y;
3985
- te[14] = z;
3986
- }
3987
- return this;
3988
- }
3989
- /**
3990
- * Inverts this matrix, using the [analytic method](https://en.wikipedia.org/wiki/Invertible_matrix#Analytic_solution).
3991
- * You can not invert with a determinant of zero. If you attempt this, the method produces
3992
- * a zero matrix instead.
3993
- *
3994
- * @return {Matrix4} A reference to this matrix.
3995
- */
3996
- invert() {
3997
- const te = this.elements, n11 = te[0], n21 = te[1], n31 = te[2], n41 = te[3], n12 = te[4], n22 = te[5], n32 = te[6], n42 = te[7], n13 = te[8], n23 = te[9], n33 = te[10], n43 = te[11], n14 = te[12], n24 = te[13], n34 = te[14], n44 = te[15], t1 = n11 * n22 - n21 * n12, t2 = n11 * n32 - n31 * n12, t3 = n11 * n42 - n41 * n12, t4 = n21 * n32 - n31 * n22, t5 = n21 * n42 - n41 * n22, t6 = n31 * n42 - n41 * n32, t7 = n13 * n24 - n23 * n14, t8 = n13 * n34 - n33 * n14, t9 = n13 * n44 - n43 * n14, t10 = n23 * n34 - n33 * n24, t11 = n23 * n44 - n43 * n24, t12 = n33 * n44 - n43 * n34;
3998
- const det = t1 * t12 - t2 * t11 + t3 * t10 + t4 * t9 - t5 * t8 + t6 * t7;
3999
- if (det === 0) return this.set(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
4000
- const detInv = 1 / det;
4001
- te[0] = (n22 * t12 - n32 * t11 + n42 * t10) * detInv;
4002
- te[1] = (n31 * t11 - n21 * t12 - n41 * t10) * detInv;
4003
- te[2] = (n24 * t6 - n34 * t5 + n44 * t4) * detInv;
4004
- te[3] = (n33 * t5 - n23 * t6 - n43 * t4) * detInv;
4005
- te[4] = (n32 * t9 - n12 * t12 - n42 * t8) * detInv;
4006
- te[5] = (n11 * t12 - n31 * t9 + n41 * t8) * detInv;
4007
- te[6] = (n34 * t3 - n14 * t6 - n44 * t2) * detInv;
4008
- te[7] = (n13 * t6 - n33 * t3 + n43 * t2) * detInv;
4009
- te[8] = (n12 * t11 - n22 * t9 + n42 * t7) * detInv;
4010
- te[9] = (n21 * t9 - n11 * t11 - n41 * t7) * detInv;
4011
- te[10] = (n14 * t5 - n24 * t3 + n44 * t1) * detInv;
4012
- te[11] = (n23 * t3 - n13 * t5 - n43 * t1) * detInv;
4013
- te[12] = (n22 * t8 - n12 * t10 - n32 * t7) * detInv;
4014
- te[13] = (n11 * t10 - n21 * t8 + n31 * t7) * detInv;
4015
- te[14] = (n24 * t2 - n14 * t4 - n34 * t1) * detInv;
4016
- te[15] = (n13 * t4 - n23 * t2 + n33 * t1) * detInv;
4017
- return this;
4018
- }
4019
- /**
4020
- * Multiplies the columns of this matrix by the given vector.
4021
- *
4022
- * @param {Vector3} v - The scale vector.
4023
- * @return {Matrix4} A reference to this matrix.
4024
- */
4025
- scale(v) {
4026
- const te = this.elements;
4027
- const x = v.x, y = v.y, z = v.z;
4028
- te[0] *= x;
4029
- te[4] *= y;
4030
- te[8] *= z;
4031
- te[1] *= x;
4032
- te[5] *= y;
4033
- te[9] *= z;
4034
- te[2] *= x;
4035
- te[6] *= y;
4036
- te[10] *= z;
4037
- te[3] *= x;
4038
- te[7] *= y;
4039
- te[11] *= z;
4040
- return this;
4041
- }
4042
- /**
4043
- * Gets the maximum scale value of the three axes.
4044
- *
4045
- * @return {number} The maximum scale.
4046
- */
4047
- getMaxScaleOnAxis() {
4048
- const te = this.elements;
4049
- const scaleXSq = te[0] * te[0] + te[1] * te[1] + te[2] * te[2];
4050
- const scaleYSq = te[4] * te[4] + te[5] * te[5] + te[6] * te[6];
4051
- const scaleZSq = te[8] * te[8] + te[9] * te[9] + te[10] * te[10];
4052
- return Math.sqrt(Math.max(scaleXSq, scaleYSq, scaleZSq));
4053
- }
4054
- /**
4055
- * Sets this matrix as a translation transform from the given vector.
4056
- *
4057
- * @param {number|Vector3} x - The amount to translate in the X axis or alternatively a translation vector.
4058
- * @param {number} y - The amount to translate in the Y axis.
4059
- * @param {number} z - The amount to translate in the z axis.
4060
- * @return {Matrix4} A reference to this matrix.
4061
- */
4062
- makeTranslation(x, y, z) {
4063
- if (x.isVector3) {
4064
- this.set(
4065
- 1,
4066
- 0,
4067
- 0,
4068
- x.x,
4069
- 0,
4070
- 1,
4071
- 0,
4072
- x.y,
4073
- 0,
4074
- 0,
4075
- 1,
4076
- x.z,
4077
- 0,
4078
- 0,
4079
- 0,
4080
- 1
4081
- );
4082
- } else {
4083
- this.set(
4084
- 1,
4085
- 0,
4086
- 0,
4087
- x,
4088
- 0,
4089
- 1,
4090
- 0,
4091
- y,
4092
- 0,
4093
- 0,
4094
- 1,
4095
- z,
4096
- 0,
4097
- 0,
4098
- 0,
4099
- 1
4100
- );
4101
- }
4102
- return this;
4103
- }
4104
- /**
4105
- * Sets this matrix as a rotational transformation around the X axis by
4106
- * the given angle.
4107
- *
4108
- * @param {number} theta - The rotation in radians.
4109
- * @return {Matrix4} A reference to this matrix.
4110
- */
4111
- makeRotationX(theta) {
4112
- const c = Math.cos(theta), s = Math.sin(theta);
4113
- this.set(
4114
- 1,
4115
- 0,
4116
- 0,
4117
- 0,
4118
- 0,
4119
- c,
4120
- -s,
4121
- 0,
4122
- 0,
4123
- s,
4124
- c,
4125
- 0,
4126
- 0,
4127
- 0,
4128
- 0,
4129
- 1
4130
- );
4131
- return this;
4132
- }
4133
- /**
4134
- * Sets this matrix as a rotational transformation around the Y axis by
4135
- * the given angle.
4136
- *
4137
- * @param {number} theta - The rotation in radians.
4138
- * @return {Matrix4} A reference to this matrix.
4139
- */
4140
- makeRotationY(theta) {
4141
- const c = Math.cos(theta), s = Math.sin(theta);
4142
- this.set(
4143
- c,
4144
- 0,
4145
- s,
4146
- 0,
4147
- 0,
4148
- 1,
4149
- 0,
4150
- 0,
4151
- -s,
4152
- 0,
4153
- c,
4154
- 0,
4155
- 0,
4156
- 0,
4157
- 0,
4158
- 1
4159
- );
4160
- return this;
4161
- }
4162
- /**
4163
- * Sets this matrix as a rotational transformation around the Z axis by
4164
- * the given angle.
4165
- *
4166
- * @param {number} theta - The rotation in radians.
4167
- * @return {Matrix4} A reference to this matrix.
4168
- */
4169
- makeRotationZ(theta) {
4170
- const c = Math.cos(theta), s = Math.sin(theta);
4171
- this.set(
4172
- c,
4173
- -s,
4174
- 0,
4175
- 0,
4176
- s,
4177
- c,
4178
- 0,
4179
- 0,
4180
- 0,
4181
- 0,
4182
- 1,
4183
- 0,
4184
- 0,
4185
- 0,
4186
- 0,
4187
- 1
4188
- );
4189
- return this;
4190
- }
4191
- /**
4192
- * Sets this matrix as a rotational transformation around the given axis by
4193
- * the given angle.
4194
- *
4195
- * This is a somewhat controversial but mathematically sound alternative to
4196
- * rotating via Quaternions. See the discussion [here](https://www.gamedev.net/articles/programming/math-and-physics/do-we-really-need-quaternions-r1199).
4197
- *
4198
- * @param {Vector3} axis - The normalized rotation axis.
4199
- * @param {number} angle - The rotation in radians.
4200
- * @return {Matrix4} A reference to this matrix.
4201
- */
4202
- makeRotationAxis(axis, angle) {
4203
- const c = Math.cos(angle);
4204
- const s = Math.sin(angle);
4205
- const t = 1 - c;
4206
- const x = axis.x, y = axis.y, z = axis.z;
4207
- const tx = t * x, ty = t * y;
4208
- this.set(
4209
- tx * x + c,
4210
- tx * y - s * z,
4211
- tx * z + s * y,
4212
- 0,
4213
- tx * y + s * z,
4214
- ty * y + c,
4215
- ty * z - s * x,
4216
- 0,
4217
- tx * z - s * y,
4218
- ty * z + s * x,
4219
- t * z * z + c,
4220
- 0,
4221
- 0,
4222
- 0,
4223
- 0,
4224
- 1
4225
- );
4226
- return this;
4227
- }
4228
- /**
4229
- * Sets this matrix as a scale transformation.
4230
- *
4231
- * @param {number} x - The amount to scale in the X axis.
4232
- * @param {number} y - The amount to scale in the Y axis.
4233
- * @param {number} z - The amount to scale in the Z axis.
4234
- * @return {Matrix4} A reference to this matrix.
4235
- */
4236
- makeScale(x, y, z) {
4237
- this.set(
4238
- x,
4239
- 0,
4240
- 0,
4241
- 0,
4242
- 0,
4243
- y,
4244
- 0,
4245
- 0,
4246
- 0,
4247
- 0,
4248
- z,
4249
- 0,
4250
- 0,
4251
- 0,
4252
- 0,
4253
- 1
4254
- );
4255
- return this;
4256
- }
4257
- /**
4258
- * Sets this matrix as a shear transformation.
4259
- *
4260
- * @param {number} xy - The amount to shear X by Y.
4261
- * @param {number} xz - The amount to shear X by Z.
4262
- * @param {number} yx - The amount to shear Y by X.
4263
- * @param {number} yz - The amount to shear Y by Z.
4264
- * @param {number} zx - The amount to shear Z by X.
4265
- * @param {number} zy - The amount to shear Z by Y.
4266
- * @return {Matrix4} A reference to this matrix.
4267
- */
4268
- makeShear(xy, xz, yx, yz, zx, zy) {
4269
- this.set(
4270
- 1,
4271
- yx,
4272
- zx,
4273
- 0,
4274
- xy,
4275
- 1,
4276
- zy,
4277
- 0,
4278
- xz,
4279
- yz,
4280
- 1,
4281
- 0,
4282
- 0,
4283
- 0,
4284
- 0,
4285
- 1
4286
- );
4287
- return this;
4288
- }
4289
- /**
4290
- * Sets this matrix to the transformation composed of the given position,
4291
- * rotation (Quaternion) and scale.
4292
- *
4293
- * @param {Vector3} position - The position vector.
4294
- * @param {Quaternion} quaternion - The rotation as a Quaternion.
4295
- * @param {Vector3} scale - The scale vector.
4296
- * @return {Matrix4} A reference to this matrix.
4297
- */
4298
- compose(position, quaternion, scale) {
4299
- const te = this.elements;
4300
- const x = quaternion._x, y = quaternion._y, z = quaternion._z, w = quaternion._w;
4301
- const x2 = x + x, y2 = y + y, z2 = z + z;
4302
- const xx = x * x2, xy = x * y2, xz = x * z2;
4303
- const yy = y * y2, yz = y * z2, zz = z * z2;
4304
- const wx = w * x2, wy = w * y2, wz = w * z2;
4305
- const sx = scale.x, sy = scale.y, sz = scale.z;
4306
- te[0] = (1 - (yy + zz)) * sx;
4307
- te[1] = (xy + wz) * sx;
4308
- te[2] = (xz - wy) * sx;
4309
- te[3] = 0;
4310
- te[4] = (xy - wz) * sy;
4311
- te[5] = (1 - (xx + zz)) * sy;
4312
- te[6] = (yz + wx) * sy;
4313
- te[7] = 0;
4314
- te[8] = (xz + wy) * sz;
4315
- te[9] = (yz - wx) * sz;
4316
- te[10] = (1 - (xx + yy)) * sz;
4317
- te[11] = 0;
4318
- te[12] = position.x;
4319
- te[13] = position.y;
4320
- te[14] = position.z;
4321
- te[15] = 1;
4322
- return this;
4323
- }
4324
- /**
4325
- * Decomposes this matrix into its position, rotation and scale components
4326
- * and provides the result in the given objects.
4327
- *
4328
- * Note: Not all matrices are decomposable in this way. For example, if an
4329
- * object has a non-uniformly scaled parent, then the object's world matrix
4330
- * may not be decomposable, and this method may not be appropriate.
4331
- *
4332
- * @param {Vector3} position - The position vector.
4333
- * @param {Quaternion} quaternion - The rotation as a Quaternion.
4334
- * @param {Vector3} scale - The scale vector.
4335
- * @return {Matrix4} A reference to this matrix.
4336
- */
4337
- decompose(position, quaternion, scale) {
4338
- const te = this.elements;
4339
- position.x = te[12];
4340
- position.y = te[13];
4341
- position.z = te[14];
4342
- const det = this.determinantAffine();
4343
- if (det === 0) {
4344
- scale.set(1, 1, 1);
4345
- quaternion.identity();
4346
- return this;
4347
- }
4348
- let sx = _v1$7.set(te[0], te[1], te[2]).length();
4349
- const sy = _v1$7.set(te[4], te[5], te[6]).length();
4350
- const sz = _v1$7.set(te[8], te[9], te[10]).length();
4351
- if (det < 0) sx = -sx;
4352
- _m1$2.copy(this);
4353
- const invSX = 1 / sx;
4354
- const invSY = 1 / sy;
4355
- const invSZ = 1 / sz;
4356
- _m1$2.elements[0] *= invSX;
4357
- _m1$2.elements[1] *= invSX;
4358
- _m1$2.elements[2] *= invSX;
4359
- _m1$2.elements[4] *= invSY;
4360
- _m1$2.elements[5] *= invSY;
4361
- _m1$2.elements[6] *= invSY;
4362
- _m1$2.elements[8] *= invSZ;
4363
- _m1$2.elements[9] *= invSZ;
4364
- _m1$2.elements[10] *= invSZ;
4365
- quaternion.setFromRotationMatrix(_m1$2);
4366
- scale.x = sx;
4367
- scale.y = sy;
4368
- scale.z = sz;
4369
- return this;
4370
- }
4371
- /**
4372
- * Creates a perspective projection matrix. This is used internally by
4373
- * {@link PerspectiveCamera#updateProjectionMatrix}.
4374
-
4375
- * @param {number} left - Left boundary of the viewing frustum at the near plane.
4376
- * @param {number} right - Right boundary of the viewing frustum at the near plane.
4377
- * @param {number} top - Top boundary of the viewing frustum at the near plane.
4378
- * @param {number} bottom - Bottom boundary of the viewing frustum at the near plane.
4379
- * @param {number} near - The distance from the camera to the near plane.
4380
- * @param {number} far - The distance from the camera to the far plane.
4381
- * @param {(WebGLCoordinateSystem|WebGPUCoordinateSystem)} [coordinateSystem=WebGLCoordinateSystem] - The coordinate system.
4382
- * @param {boolean} [reversedDepth=false] - Whether to use a reversed depth.
4383
- * @return {Matrix4} A reference to this matrix.
4384
- */
4385
- makePerspective(left, right, top, bottom, near, far, coordinateSystem = WebGLCoordinateSystem, reversedDepth = false) {
4386
- const te = this.elements;
4387
- const x = 2 * near / (right - left);
4388
- const y = 2 * near / (top - bottom);
4389
- const a = (right + left) / (right - left);
4390
- const b = (top + bottom) / (top - bottom);
4391
- let c, d;
4392
- if (reversedDepth) {
4393
- c = near / (far - near);
4394
- d = far * near / (far - near);
4395
- } else {
4396
- if (coordinateSystem === WebGLCoordinateSystem) {
4397
- c = -(far + near) / (far - near);
4398
- d = -2 * far * near / (far - near);
4399
- } else if (coordinateSystem === WebGPUCoordinateSystem) {
4400
- c = -far / (far - near);
4401
- d = -far * near / (far - near);
4402
- } else {
4403
- throw new Error("THREE.Matrix4.makePerspective(): Invalid coordinate system: " + coordinateSystem);
4404
- }
4405
- }
4406
- te[0] = x;
4407
- te[4] = 0;
4408
- te[8] = a;
4409
- te[12] = 0;
4410
- te[1] = 0;
4411
- te[5] = y;
4412
- te[9] = b;
4413
- te[13] = 0;
4414
- te[2] = 0;
4415
- te[6] = 0;
4416
- te[10] = c;
4417
- te[14] = d;
4418
- te[3] = 0;
4419
- te[7] = 0;
4420
- te[11] = -1;
4421
- te[15] = 0;
4422
- return this;
4423
- }
4424
- /**
4425
- * Creates a orthographic projection matrix. This is used internally by
4426
- * {@link OrthographicCamera#updateProjectionMatrix}.
4427
-
4428
- * @param {number} left - Left boundary of the viewing frustum at the near plane.
4429
- * @param {number} right - Right boundary of the viewing frustum at the near plane.
4430
- * @param {number} top - Top boundary of the viewing frustum at the near plane.
4431
- * @param {number} bottom - Bottom boundary of the viewing frustum at the near plane.
4432
- * @param {number} near - The distance from the camera to the near plane.
4433
- * @param {number} far - The distance from the camera to the far plane.
4434
- * @param {(WebGLCoordinateSystem|WebGPUCoordinateSystem)} [coordinateSystem=WebGLCoordinateSystem] - The coordinate system.
4435
- * @param {boolean} [reversedDepth=false] - Whether to use a reversed depth.
4436
- * @return {Matrix4} A reference to this matrix.
4437
- */
4438
- makeOrthographic(left, right, top, bottom, near, far, coordinateSystem = WebGLCoordinateSystem, reversedDepth = false) {
4439
- const te = this.elements;
4440
- const x = 2 / (right - left);
4441
- const y = 2 / (top - bottom);
4442
- const a = -(right + left) / (right - left);
4443
- const b = -(top + bottom) / (top - bottom);
4444
- let c, d;
4445
- if (reversedDepth) {
4446
- c = 1 / (far - near);
4447
- d = far / (far - near);
4448
- } else {
4449
- if (coordinateSystem === WebGLCoordinateSystem) {
4450
- c = -2 / (far - near);
4451
- d = -(far + near) / (far - near);
4452
- } else if (coordinateSystem === WebGPUCoordinateSystem) {
4453
- c = -1 / (far - near);
4454
- d = -near / (far - near);
4455
- } else {
4456
- throw new Error("THREE.Matrix4.makeOrthographic(): Invalid coordinate system: " + coordinateSystem);
4457
- }
4458
- }
4459
- te[0] = x;
4460
- te[4] = 0;
4461
- te[8] = 0;
4462
- te[12] = a;
4463
- te[1] = 0;
4464
- te[5] = y;
4465
- te[9] = 0;
4466
- te[13] = b;
4467
- te[2] = 0;
4468
- te[6] = 0;
4469
- te[10] = c;
4470
- te[14] = d;
4471
- te[3] = 0;
4472
- te[7] = 0;
4473
- te[11] = 0;
4474
- te[15] = 1;
4475
- return this;
4476
- }
4477
- /**
4478
- * Returns `true` if this matrix is equal with the given one.
4479
- *
4480
- * @param {Matrix4} matrix - The matrix to test for equality.
4481
- * @return {boolean} Whether this matrix is equal with the given one.
4482
- */
4483
- equals(matrix) {
4484
- const te = this.elements;
4485
- const me = matrix.elements;
4486
- for (let i = 0; i < 16; i++) {
4487
- if (te[i] !== me[i]) return false;
4488
- }
4489
- return true;
4490
- }
4491
- /**
4492
- * Sets the elements of the matrix from the given array.
4493
- *
4494
- * @param {Array<number>} array - The matrix elements in column-major order.
4495
- * @param {number} [offset=0] - Index of the first element in the array.
4496
- * @return {Matrix4} A reference to this matrix.
4497
- */
4498
- fromArray(array, offset = 0) {
4499
- for (let i = 0; i < 16; i++) {
4500
- this.elements[i] = array[i + offset];
4501
- }
4502
- return this;
4503
- }
4504
- /**
4505
- * Writes the elements of this matrix to the given array. If no array is provided,
4506
- * the method returns a new instance.
4507
- *
4508
- * @param {Array<number>} [array=[]] - The target array holding the matrix elements in column-major order.
4509
- * @param {number} [offset=0] - Index of the first element in the array.
4510
- * @return {Array<number>} The matrix elements in column-major order.
4511
- */
4512
- toArray(array = [], offset = 0) {
4513
- const te = this.elements;
4514
- array[offset] = te[0];
4515
- array[offset + 1] = te[1];
4516
- array[offset + 2] = te[2];
4517
- array[offset + 3] = te[3];
4518
- array[offset + 4] = te[4];
4519
- array[offset + 5] = te[5];
4520
- array[offset + 6] = te[6];
4521
- array[offset + 7] = te[7];
4522
- array[offset + 8] = te[8];
4523
- array[offset + 9] = te[9];
4524
- array[offset + 10] = te[10];
4525
- array[offset + 11] = te[11];
4526
- array[offset + 12] = te[12];
4527
- array[offset + 13] = te[13];
4528
- array[offset + 14] = te[14];
4529
- array[offset + 15] = te[15];
4530
- return array;
4531
- }
4532
- };
4533
- var _v1$7 = /* @__PURE__ */ new Vector3();
4534
- var _m1$2 = /* @__PURE__ */ new Matrix4();
4535
- var _zero = /* @__PURE__ */ new Vector3(0, 0, 0);
4536
- var _one = /* @__PURE__ */ new Vector3(1, 1, 1);
4537
- var _x = /* @__PURE__ */ new Vector3();
4538
- var _y = /* @__PURE__ */ new Vector3();
4539
- var _z = /* @__PURE__ */ new Vector3();
4540
- (class _Matrix2 {
4541
- static {
4542
- _Matrix2.prototype.isMatrix2 = true;
4543
- }
4544
- /**
4545
- * Constructs a new 2x2 matrix. The arguments are supposed to be
4546
- * in row-major order. If no arguments are provided, the constructor
4547
- * initializes the matrix as an identity matrix.
4548
- *
4549
- * @param {number} [n11] - 1-1 matrix element.
4550
- * @param {number} [n12] - 1-2 matrix element.
4551
- * @param {number} [n21] - 2-1 matrix element.
4552
- * @param {number} [n22] - 2-2 matrix element.
4553
- */
4554
- constructor(n11, n12, n21, n22) {
4555
- this.elements = [
4556
- 1,
4557
- 0,
4558
- 0,
4559
- 1
4560
- ];
4561
- if (n11 !== void 0) {
4562
- this.set(n11, n12, n21, n22);
4563
- }
4564
- }
4565
- /**
4566
- * Sets this matrix to the 2x2 identity matrix.
4567
- *
4568
- * @return {Matrix2} A reference to this matrix.
4569
- */
4570
- identity() {
4571
- this.set(
4572
- 1,
4573
- 0,
4574
- 0,
4575
- 1
4576
- );
4577
- return this;
4578
- }
4579
- /**
4580
- * Sets the elements of the matrix from the given array.
4581
- *
4582
- * @param {Array<number>} array - The matrix elements in column-major order.
4583
- * @param {number} [offset=0] - Index of the first element in the array.
4584
- * @return {Matrix2} A reference to this matrix.
4585
- */
4586
- fromArray(array, offset = 0) {
4587
- for (let i = 0; i < 4; i++) {
4588
- this.elements[i] = array[i + offset];
4589
- }
4590
- return this;
4591
- }
4592
- /**
4593
- * Sets the elements of the matrix.The arguments are supposed to be
4594
- * in row-major order.
4595
- *
4596
- * @param {number} n11 - 1-1 matrix element.
4597
- * @param {number} n12 - 1-2 matrix element.
4598
- * @param {number} n21 - 2-1 matrix element.
4599
- * @param {number} n22 - 2-2 matrix element.
4600
- * @return {Matrix2} A reference to this matrix.
4601
- */
4602
- set(n11, n12, n21, n22) {
4603
- const te = this.elements;
4604
- te[0] = n11;
4605
- te[2] = n12;
4606
- te[1] = n21;
4607
- te[3] = n22;
4608
- return this;
4609
- }
4610
- });
4611
- if (typeof __THREE_DEVTOOLS__ !== "undefined") {
4612
- __THREE_DEVTOOLS__.dispatchEvent(new CustomEvent("register", { detail: {
4613
- revision: REVISION
4614
- } }));
4615
- }
4616
- if (typeof window !== "undefined") {
4617
- if (window.__THREE__) {
4618
- warn("WARNING: Multiple instances of Three.js being imported.");
4619
- } else {
4620
- window.__THREE__ = REVISION;
4621
- }
4622
- }
4623
- var _m$1 = /* @__PURE__ */ new Matrix3();
4624
- _m$1.set(-1, 0, 0, 0, 1, 0, 0, 0, 1);
4625
- var _m = /* @__PURE__ */ new Matrix3();
4626
- _m.set(-1, 0, 0, 0, 1, 0, 0, 0, 1);
4627
-
4628
- // src/navigation/NavigationAgent3D.ts
4629
6
  var MAX_CROWD_AGENTS = 64;
4630
7
  var MAX_CROWD_AGENT_RADIUS = 2;
4631
8
  function finitePositive(name, value) {
@@ -4633,8 +10,20 @@ function finitePositive(name, value) {
4633
10
  throw new Error(`NavigationAgent3D ${name} must be finite and positive.`);
4634
11
  return value;
4635
12
  }
4636
- function toNavigationVector(value) {
4637
- return { x: value.x, y: value.y, z: value.z };
13
+ function finitePositionComponents(x, y, z) {
14
+ return Number.isFinite(x) && Number.isFinite(y) && Number.isFinite(z);
15
+ }
16
+ function toNavigationVector(value, target) {
17
+ target.x = value.x;
18
+ target.y = value.y;
19
+ target.z = value.z;
20
+ return target;
21
+ }
22
+ function toNavigationArray(value, target) {
23
+ target[0] = value.x;
24
+ target[1] = value.y;
25
+ target[2] = value.z;
26
+ return target;
4638
27
  }
4639
28
  function distanceSquared(a, b) {
4640
29
  return (a.x - b.x) ** 2 + (a.y - b.y) ** 2 + (a.z - b.z) ** 2;
@@ -4664,6 +53,7 @@ var NavigationAgent3D = class {
4664
53
  targetDesiredDistance;
4665
54
  #avoidanceEnabled;
4666
55
  #target;
56
+ #targetRevision = 0;
4667
57
  #path = [];
4668
58
  #pathIndex = 0;
4669
59
  #finished = false;
@@ -4674,6 +64,16 @@ var NavigationAgent3D = class {
4674
64
  targetReached: /* @__PURE__ */ new Set()
4675
65
  };
4676
66
  #crowdAgent;
67
+ #objectRecord = { x: 0, y: 0, z: 0 };
68
+ #targetRecord = { x: 0, y: 0, z: 0 };
69
+ #avoidanceRecord = { x: 0, y: 0, z: 0 };
70
+ #crowdPositionArray = [0, 0, 0];
71
+ #crowdTargetArray = [0, 0, 0];
72
+ #crowdHalfExtentsArray = [0, 0, 0];
73
+ #crowdNearestPointArray = [0, 0, 0];
74
+ #crowdNearestRef;
75
+ #crowdNearestPoint;
76
+ #crowdPointOverPoly;
4677
77
  constructor(options) {
4678
78
  if (options.navigation === void 0)
4679
79
  throw new Error("NavigationAgent3D requires a navigation context.");
@@ -4692,6 +92,9 @@ var NavigationAgent3D = class {
4692
92
  "targetDesiredDistance",
4693
93
  options.targetDesiredDistance ?? 0.45
4694
94
  );
95
+ this.#crowdNearestRef = new Raw.UnsignedIntRef();
96
+ this.#crowdNearestPoint = new Raw.Vec3();
97
+ this.#crowdPointOverPoly = new Raw.BoolRef();
4695
98
  this.#avoidanceEnabled = options.avoidanceEnabled ?? true;
4696
99
  this.#crowdAgent = void 0;
4697
100
  if (this.#avoidanceEnabled) this.#enableAvoidance();
@@ -4715,46 +118,83 @@ var NavigationAgent3D = class {
4715
118
  }
4716
119
  setTargetPosition(position) {
4717
120
  if (this.#disposed) throw new Error("NavigationAgent3D cannot target after dispose.");
4718
- if (![position.x, position.y, position.z].every(Number.isFinite))
121
+ const x = position.x;
122
+ const y = position.y;
123
+ const z = position.z;
124
+ if (!finitePositionComponents(x, y, z))
4719
125
  throw new Error("NavigationAgent3D target position must be finite.");
4720
- this.#target = new Vector3(position.x, position.y, position.z);
126
+ this.#target ??= new Vector3();
127
+ this.#target.set(x, y, z);
128
+ this.#targetRevision += 1;
4721
129
  this.#pathIndex = 0;
4722
130
  this.#finished = false;
4723
- if (this.isTargetReachable()) {
4724
- const result = this.navigation.query.computePath(
4725
- toNavigationVector(this.object.position),
4726
- toNavigationVector(position)
4727
- );
4728
- this.#path = result.success ? result.path.map((point) => new Vector3(point.x, point.y, point.z)) : [];
4729
- if (this.#path.length > 0) this.#crowdAgent?.requestMoveTarget(toNavigationVector(position));
4730
- } else {
131
+ const target = this.#target;
132
+ const start = this.navigation.query.findClosestPoint(
133
+ toNavigationVector(this.object.position, this.#objectRecord)
134
+ );
135
+ const end = this.navigation.query.findClosestPoint(
136
+ toNavigationVector(target, this.#targetRecord)
137
+ );
138
+ if (!this.#hasEnabledRegion() || !start.success || !end.success) {
4731
139
  this.#path = [];
4732
140
  this.#crowdAgent?.resetMoveTarget();
141
+ } else if (start.polyRef === end.polyRef) {
142
+ this.#storeComputedPath(target);
143
+ if (!navigationPointMatchesTarget(end.point, target, this.targetDesiredDistance, this.height)) {
144
+ this.#crowdAgent?.resetMoveTarget();
145
+ } else if (this.#path.length > 0) {
146
+ this.#requestCrowdMoveTarget(target);
147
+ }
148
+ } else {
149
+ const reachable = this.#storeComputedPath(target);
150
+ if (!reachable) {
151
+ this.#crowdAgent?.resetMoveTarget();
152
+ } else if (this.#path.length > 0) {
153
+ this.#requestCrowdMoveTarget(target);
154
+ }
4733
155
  }
4734
156
  for (const handler of this.#listeners.pathChanged) handler();
4735
157
  }
4736
- getNextPathPosition() {
158
+ getNextPathPosition(target = new Vector3()) {
4737
159
  if (this.#target === void 0)
4738
160
  throw new Error("NavigationAgent3D.getNextPathPosition requires a target position.");
4739
- if (!this.#hasEnabledRegion()) return this.object.position.clone();
4740
- const avoidance = this.#crowdAgent?.desiredVelocityObstacleAdjusted();
4741
- if (this.#path.length === 0) return this.object.position.clone();
161
+ if (!this.#hasEnabledRegion()) return target.copy(this.object.position);
162
+ const avoidance = this.#readAvoidance();
163
+ if (this.#path.length === 0) return target.copy(this.object.position);
4742
164
  if (avoidance !== void 0 && Math.hypot(avoidance.x, avoidance.z) > 1e-3) {
4743
- const next = this.object.position.clone();
4744
- next.x += avoidance.x;
4745
- next.z += avoidance.z;
4746
- return next;
165
+ target.copy(this.object.position);
166
+ target.x += avoidance.x;
167
+ target.z += avoidance.z;
168
+ return target;
4747
169
  }
4748
- return this.#path[this.#pathIndex]?.clone() ?? this.object.position.clone();
170
+ return target.copy(this.#path[this.#pathIndex] ?? this.object.position);
4749
171
  }
4750
172
  isNavigationFinished() {
4751
173
  return this.#target !== void 0 && this.#finished;
4752
174
  }
175
+ /**
176
+ * Computes the path from the current object position to `target` once, stores it, and reports
177
+ * whether its final waypoint lands within tolerance. The one place both `setTargetPosition`
178
+ * and reachability judgments get their path from.
179
+ */
180
+ #storeComputedPath(target) {
181
+ const result = this.navigation.query.computePath(
182
+ toNavigationVector(this.object.position, this.#objectRecord),
183
+ toNavigationVector(target, this.#targetRecord)
184
+ );
185
+ this.#path = result.success ? result.path.map((point) => new Vector3(point.x, point.y, point.z)) : [];
186
+ const final = this.#path.at(-1);
187
+ return result.success && final !== void 0 && navigationPointMatchesTarget(final, target, this.targetDesiredDistance, this.height);
188
+ }
4753
189
  isTargetReachable(position) {
4754
190
  const target = position ?? this.#target;
4755
191
  if (target === void 0) return false;
4756
- const start = this.navigation.query.findClosestPoint(toNavigationVector(this.object.position));
4757
- const end = this.navigation.query.findClosestPoint(toNavigationVector(target));
192
+ const start = this.navigation.query.findClosestPoint(
193
+ toNavigationVector(this.object.position, this.#objectRecord)
194
+ );
195
+ const end = this.navigation.query.findClosestPoint(
196
+ toNavigationVector(target, this.#targetRecord)
197
+ );
4758
198
  if (!this.#hasEnabledRegion() || !start.success || !end.success) return false;
4759
199
  if (start.polyRef === end.polyRef)
4760
200
  return navigationPointMatchesTarget(
@@ -4764,8 +204,8 @@ var NavigationAgent3D = class {
4764
204
  this.height
4765
205
  );
4766
206
  const path = this.navigation.query.computePath(
4767
- toNavigationVector(this.object.position),
4768
- toNavigationVector(target)
207
+ toNavigationVector(this.object.position, this.#objectRecord),
208
+ toNavigationVector(target, this.#targetRecord)
4769
209
  );
4770
210
  const final = path.path.at(-1);
4771
211
  return path.success && final !== void 0 && navigationPointMatchesTarget(final, target, this.targetDesiredDistance, this.height);
@@ -4776,11 +216,28 @@ var NavigationAgent3D = class {
4776
216
  distanceToTarget() {
4777
217
  return this.#target === void 0 ? Number.POSITIVE_INFINITY : Math.sqrt(distanceSquared(this.object.position, this.#target));
4778
218
  }
219
+ // Teleport forces Recast to re-localise the agent on the navmesh, so it is paid only when the
220
+ // object actually moved since the last sync; NaN starts make the first sync always localise.
221
+ #lastSyncX = Number.NaN;
222
+ #lastSyncY = Number.NaN;
223
+ #lastSyncZ = Number.NaN;
224
+ #lastRequestedTargetRevision = -1;
4779
225
  syncCrowd() {
4780
226
  if (this.#disposed || this.#crowdAgent === void 0) return;
4781
- this.#crowdAgent.teleport(toNavigationVector(this.object.position));
4782
- if (this.#target !== void 0 && this.#path.length > 0)
4783
- this.#crowdAgent.requestMoveTarget(toNavigationVector(this.#target));
227
+ const { x, y, z } = this.object.position;
228
+ const moved = x !== this.#lastSyncX || y !== this.#lastSyncY || z !== this.#lastSyncZ;
229
+ const retargeted = this.#target !== void 0 && this.#targetRevision !== this.#lastRequestedTargetRevision && this.#path.length > 0;
230
+ if (!moved && !retargeted) return;
231
+ this.#lastSyncX = x;
232
+ this.#lastSyncY = y;
233
+ this.#lastSyncZ = z;
234
+ if (moved) {
235
+ this.#teleportCrowdAgent();
236
+ }
237
+ if (this.#target !== void 0 && this.#path.length > 0) {
238
+ this.#lastRequestedTargetRevision = this.#targetRevision;
239
+ this.#requestCrowdMoveTarget(this.#target);
240
+ }
4784
241
  }
4785
242
  advance() {
4786
243
  if (this.#disposed || this.#target === void 0 || this.#path.length === 0 || !this.#hasEnabledRegion())
@@ -4805,32 +262,94 @@ var NavigationAgent3D = class {
4805
262
  for (const listeners of Object.values(this.#listeners)) listeners.clear();
4806
263
  this.#path = [];
4807
264
  this.#target = void 0;
265
+ Raw.destroy(this.#crowdNearestRef);
266
+ Raw.destroy(this.#crowdNearestPoint);
267
+ Raw.destroy(this.#crowdPointOverPoly);
4808
268
  }
4809
269
  #enableAvoidance() {
4810
270
  if (this.#crowdAgent !== void 0) return;
4811
271
  const crowd = crowdFor(this.navigation, this.radius);
4812
- this.#crowdAgent = crowd.addAgent(toNavigationVector(this.object.position), {
4813
- collisionQueryRange: 2.5,
4814
- height: this.height,
4815
- maxAcceleration: 20,
4816
- maxSpeed: this.maxSpeed,
4817
- obstacleAvoidanceType: 0,
4818
- pathOptimizationRange: 0,
4819
- radius: this.radius,
4820
- separationWeight: 2,
4821
- updateFlags: 7,
4822
- userData: this
4823
- });
272
+ this.#crowdAgent = crowd.addAgent(
273
+ toNavigationVector(this.object.position, this.#objectRecord),
274
+ {
275
+ collisionQueryRange: 2.5,
276
+ height: this.height,
277
+ maxAcceleration: 20,
278
+ maxSpeed: this.maxSpeed,
279
+ obstacleAvoidanceType: 0,
280
+ pathOptimizationRange: 0,
281
+ radius: this.radius,
282
+ separationWeight: 2,
283
+ updateFlags: 7,
284
+ userData: this
285
+ }
286
+ );
4824
287
  }
4825
288
  #disableAvoidance() {
4826
289
  if (this.#crowdAgent === void 0) return;
4827
290
  this.navigation.crowd?.removeAgent(this.#crowdAgent);
4828
291
  this.#crowdAgent = void 0;
4829
292
  }
293
+ #teleportCrowdAgent() {
294
+ const crowdAgent = this.#crowdAgent;
295
+ if (crowdAgent === void 0) return;
296
+ const { crowd } = crowdAgent;
297
+ const query = crowd.navMeshQuery;
298
+ Raw.CrowdUtils.agentTeleport(
299
+ crowd.raw,
300
+ crowdAgent.agentIndex,
301
+ toNavigationArray(this.object.position, this.#crowdPositionArray),
302
+ toNavigationArray(query.defaultQueryHalfExtents, this.#crowdHalfExtentsArray),
303
+ query.defaultFilter.raw
304
+ );
305
+ crowdAgent.interpolatedPosition.x = this.object.position.x;
306
+ crowdAgent.interpolatedPosition.y = this.object.position.y;
307
+ crowdAgent.interpolatedPosition.z = this.object.position.z;
308
+ }
309
+ #requestCrowdMoveTarget(target) {
310
+ const crowdAgent = this.#crowdAgent;
311
+ if (crowdAgent === void 0) return;
312
+ const { crowd } = crowdAgent;
313
+ const query = crowd.navMeshQuery;
314
+ const targetArray = toNavigationArray(target, this.#crowdTargetArray);
315
+ const halfExtentsArray = toNavigationArray(
316
+ query.defaultQueryHalfExtents,
317
+ this.#crowdHalfExtentsArray
318
+ );
319
+ this.#crowdNearestRef.value = 0;
320
+ this.#crowdNearestPoint.x = 0;
321
+ this.#crowdNearestPoint.y = 0;
322
+ this.#crowdNearestPoint.z = 0;
323
+ this.#crowdPointOverPoly.value = false;
324
+ query.raw.findNearestPoly(
325
+ targetArray,
326
+ halfExtentsArray,
327
+ query.defaultFilter.raw,
328
+ this.#crowdNearestRef,
329
+ this.#crowdNearestPoint,
330
+ this.#crowdPointOverPoly
331
+ );
332
+ this.#crowdNearestPointArray[0] = this.#crowdNearestPoint.x;
333
+ this.#crowdNearestPointArray[1] = this.#crowdNearestPoint.y;
334
+ this.#crowdNearestPointArray[2] = this.#crowdNearestPoint.z;
335
+ crowd.raw.requestMoveTarget(
336
+ crowdAgent.agentIndex,
337
+ this.#crowdNearestRef.value,
338
+ this.#crowdNearestPointArray
339
+ );
340
+ }
4830
341
  #hasEnabledRegion() {
4831
342
  for (const region of this.navigation.regions) if (region.enabled) return true;
4832
343
  return false;
4833
344
  }
345
+ #readAvoidance() {
346
+ const raw = this.#crowdAgent?.raw;
347
+ if (raw === void 0) return void 0;
348
+ this.#avoidanceRecord.x = raw.get_nvel(0);
349
+ this.#avoidanceRecord.y = raw.get_nvel(1);
350
+ this.#avoidanceRecord.z = raw.get_nvel(2);
351
+ return this.#avoidanceRecord;
352
+ }
4834
353
  };
4835
354
  var MAX_CROWD_AGENTS2 = 64;
4836
355
  var MAX_CROWD_AGENT_RADIUS2 = 2;
@@ -4879,10 +398,21 @@ var NavigationObstacle3D = class {
4879
398
  if (value) this.#enableAvoidance();
4880
399
  else this.#disableAvoidance();
4881
400
  }
401
+ // Teleport forces Recast to re-localise on the navmesh; a stationary obstacle pays it once,
402
+ // not per frame. NaN starts make the first sync always localise.
403
+ #lastSyncX = Number.NaN;
404
+ #lastSyncY = Number.NaN;
405
+ #lastSyncZ = Number.NaN;
4882
406
  syncCrowd() {
4883
407
  if (this.#disposed || this.crowdAgent === void 0) return;
4884
- this.crowdAgent.teleport(toNavigationVector2(this.object.position));
4885
- this.crowdAgent.requestMoveVelocity({ x: 0, y: 0, z: 0 });
408
+ const { x, y, z } = this.object.position;
409
+ if (x !== this.#lastSyncX || y !== this.#lastSyncY || z !== this.#lastSyncZ) {
410
+ this.#lastSyncX = x;
411
+ this.#lastSyncY = y;
412
+ this.#lastSyncZ = z;
413
+ this.crowdAgent.teleport(toNavigationVector2(this.object.position));
414
+ this.crowdAgent.requestMoveVelocity({ x: 0, y: 0, z: 0 });
415
+ }
4886
416
  }
4887
417
  dispose() {
4888
418
  if (this.#disposed) return;
@@ -5032,7 +562,10 @@ var NavigationRegion3D = class {
5032
562
  this.navigation.navMesh = result.navMesh;
5033
563
  this.navigationMesh = result.navMesh;
5034
564
  previousQuery.destroy();
5035
- previousMesh.destroy();
565
+ const supersededElsewhere = [...this.navigation.regions].some(
566
+ (region) => region !== this && region.navigationMesh === previousMesh
567
+ );
568
+ if (!supersededElsewhere) previousMesh.destroy();
5036
569
  return result.navMesh;
5037
570
  }
5038
571
  dispose() {
@@ -5119,15 +652,5 @@ function recast() {
5119
652
  }
5120
653
  };
5121
654
  }
5122
- /*! Bundled license information:
5123
-
5124
- three/build/three.core.js:
5125
- three/build/three.module.js:
5126
- (**
5127
- * @license
5128
- * Copyright 2010-2026 Three.js Authors
5129
- * SPDX-License-Identifier: MIT
5130
- *)
5131
- */
5132
655
 
5133
656
  export { NavigationAgent3D, NavigationObstacle3D, NavigationRegion3D, recast };