xl-public-utils 1.0.11 → 1.0.13

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.
@@ -0,0 +1,617 @@
1
+ import * as MathUtils from './MathUtils.js';
2
+ import { Quaternion } from './Quaternion.js';
3
+
4
+ class Vector3 {
5
+ constructor(x = 0, y = 0, z = 0) {
6
+ this.isVector3 = true;
7
+
8
+ this.x = x;
9
+ this.y = y;
10
+ this.z = z;
11
+ }
12
+
13
+ set(x, y, z) {
14
+ if (z === undefined) z = this.z; // sprite.scale.set(x,y)
15
+
16
+ this.x = x;
17
+ this.y = y;
18
+ this.z = z;
19
+
20
+ return this;
21
+ }
22
+
23
+ setScalar(scalar) {
24
+ this.x = scalar;
25
+ this.y = scalar;
26
+ this.z = scalar;
27
+
28
+ return this;
29
+ }
30
+
31
+ setX(x) {
32
+ this.x = x;
33
+
34
+ return this;
35
+ }
36
+
37
+ setY(y) {
38
+ this.y = y;
39
+
40
+ return this;
41
+ }
42
+
43
+ setZ(z) {
44
+ this.z = z;
45
+
46
+ return this;
47
+ }
48
+
49
+ setComponent(index, value) {
50
+ switch (index) {
51
+ case 0:
52
+ this.x = value;
53
+ break;
54
+ case 1:
55
+ this.y = value;
56
+ break;
57
+ case 2:
58
+ this.z = value;
59
+ break;
60
+ default:
61
+ throw new Error('index is out of range: ' + index);
62
+ }
63
+
64
+ return this;
65
+ }
66
+
67
+ getComponent(index) {
68
+ switch (index) {
69
+ case 0:
70
+ return this.x;
71
+ case 1:
72
+ return this.y;
73
+ case 2:
74
+ return this.z;
75
+ default:
76
+ throw new Error('index is out of range: ' + index);
77
+ }
78
+ }
79
+
80
+ clone() {
81
+ return new this.constructor(this.x, this.y, this.z);
82
+ }
83
+
84
+ copy(v) {
85
+ this.x = v.x;
86
+ this.y = v.y;
87
+ this.z = v.z;
88
+
89
+ return this;
90
+ }
91
+
92
+ add(v, w) {
93
+ if (w !== undefined) {
94
+ console.warn('THREE.Vector3: .add() now only accepts one argument. Use .addVectors( a, b ) instead.');
95
+ return this.addVectors(v, w);
96
+ }
97
+
98
+ this.x += v.x;
99
+ this.y += v.y;
100
+ this.z += v.z;
101
+
102
+ return this;
103
+ }
104
+
105
+ addScalar(s) {
106
+ this.x += s;
107
+ this.y += s;
108
+ this.z += s;
109
+
110
+ return this;
111
+ }
112
+
113
+ addVectors(a, b) {
114
+ this.x = a.x + b.x;
115
+ this.y = a.y + b.y;
116
+ this.z = a.z + b.z;
117
+
118
+ return this;
119
+ }
120
+
121
+ addScaledVector(v, s) {
122
+ this.x += v.x * s;
123
+ this.y += v.y * s;
124
+ this.z += v.z * s;
125
+
126
+ return this;
127
+ }
128
+
129
+ sub(v, w) {
130
+ if (w !== undefined) {
131
+ console.warn('THREE.Vector3: .sub() now only accepts one argument. Use .subVectors( a, b ) instead.');
132
+ return this.subVectors(v, w);
133
+ }
134
+
135
+ this.x -= v.x;
136
+ this.y -= v.y;
137
+ this.z -= v.z;
138
+
139
+ return this;
140
+ }
141
+
142
+ subScalar(s) {
143
+ this.x -= s;
144
+ this.y -= s;
145
+ this.z -= s;
146
+
147
+ return this;
148
+ }
149
+
150
+ subVectors(a, b) {
151
+ this.x = a.x - b.x;
152
+ this.y = a.y - b.y;
153
+ this.z = a.z - b.z;
154
+
155
+ return this;
156
+ }
157
+
158
+ multiply(v, w) {
159
+ if (w !== undefined) {
160
+ console.warn('THREE.Vector3: .multiply() now only accepts one argument. Use .multiplyVectors( a, b ) instead.');
161
+ return this.multiplyVectors(v, w);
162
+ }
163
+
164
+ this.x *= v.x;
165
+ this.y *= v.y;
166
+ this.z *= v.z;
167
+
168
+ return this;
169
+ }
170
+
171
+ multiplyScalar(scalar) {
172
+ this.x *= scalar;
173
+ this.y *= scalar;
174
+ this.z *= scalar;
175
+
176
+ return this;
177
+ }
178
+
179
+ multiplyVectors(a, b) {
180
+ this.x = a.x * b.x;
181
+ this.y = a.y * b.y;
182
+ this.z = a.z * b.z;
183
+
184
+ return this;
185
+ }
186
+
187
+ applyEuler(euler) {
188
+ if (!(euler && euler.isEuler)) {
189
+ console.error('THREE.Vector3: .applyEuler() now expects an Euler rotation rather than a Vector3 and order.');
190
+ }
191
+
192
+ return this.applyQuaternion(_quaternion.setFromEuler(euler));
193
+ }
194
+
195
+ applyAxisAngle(axis, angle) {
196
+ return this.applyQuaternion(_quaternion.setFromAxisAngle(axis, angle));
197
+ }
198
+
199
+ applyMatrix3(m) {
200
+ const x = this.x,
201
+ y = this.y,
202
+ z = this.z;
203
+ const e = m.elements;
204
+
205
+ this.x = e[0] * x + e[3] * y + e[6] * z;
206
+ this.y = e[1] * x + e[4] * y + e[7] * z;
207
+ this.z = e[2] * x + e[5] * y + e[8] * z;
208
+
209
+ return this;
210
+ }
211
+
212
+ applyNormalMatrix(m) {
213
+ return this.applyMatrix3(m).normalize();
214
+ }
215
+
216
+ applyMatrix4(m) {
217
+ const x = this.x,
218
+ y = this.y,
219
+ z = this.z;
220
+ const e = m.elements;
221
+
222
+ const w = 1 / (e[3] * x + e[7] * y + e[11] * z + e[15]);
223
+
224
+ this.x = (e[0] * x + e[4] * y + e[8] * z + e[12]) * w;
225
+ this.y = (e[1] * x + e[5] * y + e[9] * z + e[13]) * w;
226
+ this.z = (e[2] * x + e[6] * y + e[10] * z + e[14]) * w;
227
+
228
+ return this;
229
+ }
230
+
231
+ applyQuaternion(q) {
232
+ const x = this.x,
233
+ y = this.y,
234
+ z = this.z;
235
+ const qx = q.x,
236
+ qy = q.y,
237
+ qz = q.z,
238
+ qw = q.w;
239
+
240
+ // calculate quat * vector
241
+
242
+ const ix = qw * x + qy * z - qz * y;
243
+ const iy = qw * y + qz * x - qx * z;
244
+ const iz = qw * z + qx * y - qy * x;
245
+ const iw = -qx * x - qy * y - qz * z;
246
+
247
+ // calculate result * inverse quat
248
+
249
+ this.x = ix * qw + iw * -qx + iy * -qz - iz * -qy;
250
+ this.y = iy * qw + iw * -qy + iz * -qx - ix * -qz;
251
+ this.z = iz * qw + iw * -qz + ix * -qy - iy * -qx;
252
+
253
+ return this;
254
+ }
255
+
256
+ project(camera) {
257
+ return this.applyMatrix4(camera.matrixWorldInverse).applyMatrix4(camera.projectionMatrix);
258
+ }
259
+
260
+ unproject(camera) {
261
+ return this.applyMatrix4(camera.projectionMatrixInverse).applyMatrix4(camera.matrixWorld);
262
+ }
263
+
264
+ transformDirection(m) {
265
+ // input: THREE.Matrix4 affine matrix
266
+ // vector interpreted as a direction
267
+
268
+ const x = this.x,
269
+ y = this.y,
270
+ z = this.z;
271
+ const e = m.elements;
272
+
273
+ this.x = e[0] * x + e[4] * y + e[8] * z;
274
+ this.y = e[1] * x + e[5] * y + e[9] * z;
275
+ this.z = e[2] * x + e[6] * y + e[10] * z;
276
+
277
+ return this.normalize();
278
+ }
279
+
280
+ divide(v) {
281
+ this.x /= v.x;
282
+ this.y /= v.y;
283
+ this.z /= v.z;
284
+
285
+ return this;
286
+ }
287
+
288
+ divideScalar(scalar) {
289
+ return this.multiplyScalar(1 / scalar);
290
+ }
291
+
292
+ min(v) {
293
+ this.x = Math.min(this.x, v.x);
294
+ this.y = Math.min(this.y, v.y);
295
+ this.z = Math.min(this.z, v.z);
296
+
297
+ return this;
298
+ }
299
+
300
+ max(v) {
301
+ this.x = Math.max(this.x, v.x);
302
+ this.y = Math.max(this.y, v.y);
303
+ this.z = Math.max(this.z, v.z);
304
+
305
+ return this;
306
+ }
307
+
308
+ clamp(min, max) {
309
+ // assumes min < max, componentwise
310
+
311
+ this.x = Math.max(min.x, Math.min(max.x, this.x));
312
+ this.y = Math.max(min.y, Math.min(max.y, this.y));
313
+ this.z = Math.max(min.z, Math.min(max.z, this.z));
314
+
315
+ return this;
316
+ }
317
+
318
+ clampScalar(minVal, maxVal) {
319
+ this.x = Math.max(minVal, Math.min(maxVal, this.x));
320
+ this.y = Math.max(minVal, Math.min(maxVal, this.y));
321
+ this.z = Math.max(minVal, Math.min(maxVal, this.z));
322
+
323
+ return this;
324
+ }
325
+
326
+ clampLength(min, max) {
327
+ const length = this.length();
328
+
329
+ return this.divideScalar(length || 1).multiplyScalar(Math.max(min, Math.min(max, length)));
330
+ }
331
+
332
+ floor() {
333
+ this.x = Math.floor(this.x);
334
+ this.y = Math.floor(this.y);
335
+ this.z = Math.floor(this.z);
336
+
337
+ return this;
338
+ }
339
+
340
+ ceil() {
341
+ this.x = Math.ceil(this.x);
342
+ this.y = Math.ceil(this.y);
343
+ this.z = Math.ceil(this.z);
344
+
345
+ return this;
346
+ }
347
+
348
+ round() {
349
+ this.x = Math.round(this.x);
350
+ this.y = Math.round(this.y);
351
+ this.z = Math.round(this.z);
352
+
353
+ return this;
354
+ }
355
+
356
+ roundToZero() {
357
+ this.x = this.x < 0 ? Math.ceil(this.x) : Math.floor(this.x);
358
+ this.y = this.y < 0 ? Math.ceil(this.y) : Math.floor(this.y);
359
+ this.z = this.z < 0 ? Math.ceil(this.z) : Math.floor(this.z);
360
+
361
+ return this;
362
+ }
363
+
364
+ negate() {
365
+ this.x = -this.x;
366
+ this.y = -this.y;
367
+ this.z = -this.z;
368
+
369
+ return this;
370
+ }
371
+
372
+ dot(v) {
373
+ return this.x * v.x + this.y * v.y + this.z * v.z;
374
+ }
375
+
376
+ // TODO lengthSquared?
377
+
378
+ lengthSq() {
379
+ return this.x * this.x + this.y * this.y + this.z * this.z;
380
+ }
381
+
382
+ length() {
383
+ return Math.sqrt(this.x * this.x + this.y * this.y + this.z * this.z);
384
+ }
385
+
386
+ manhattanLength() {
387
+ return Math.abs(this.x) + Math.abs(this.y) + Math.abs(this.z);
388
+ }
389
+
390
+ normalize() {
391
+ return this.divideScalar(this.length() || 1);
392
+ }
393
+
394
+ setLength(length) {
395
+ return this.normalize().multiplyScalar(length);
396
+ }
397
+
398
+ lerp(v, alpha) {
399
+ this.x += (v.x - this.x) * alpha;
400
+ this.y += (v.y - this.y) * alpha;
401
+ this.z += (v.z - this.z) * alpha;
402
+
403
+ return this;
404
+ }
405
+
406
+ lerpVectors(v1, v2, alpha) {
407
+ this.x = v1.x + (v2.x - v1.x) * alpha;
408
+ this.y = v1.y + (v2.y - v1.y) * alpha;
409
+ this.z = v1.z + (v2.z - v1.z) * alpha;
410
+
411
+ return this;
412
+ }
413
+
414
+ cross(v, w) {
415
+ if (w !== undefined) {
416
+ console.warn('THREE.Vector3: .cross() now only accepts one argument. Use .crossVectors( a, b ) instead.');
417
+ return this.crossVectors(v, w);
418
+ }
419
+
420
+ return this.crossVectors(this, v);
421
+ }
422
+
423
+ crossVectors(a, b) {
424
+ const ax = a.x,
425
+ ay = a.y,
426
+ az = a.z;
427
+ const bx = b.x,
428
+ by = b.y,
429
+ bz = b.z;
430
+
431
+ this.x = ay * bz - az * by;
432
+ this.y = az * bx - ax * bz;
433
+ this.z = ax * by - ay * bx;
434
+
435
+ return this;
436
+ }
437
+
438
+ projectOnVector(v) {
439
+ const denominator = v.lengthSq();
440
+
441
+ if (denominator === 0) return this.set(0, 0, 0);
442
+
443
+ const scalar = v.dot(this) / denominator;
444
+
445
+ return this.copy(v).multiplyScalar(scalar);
446
+ }
447
+
448
+ projectOnPlane(planeNormal) {
449
+ _vector.copy(this).projectOnVector(planeNormal);
450
+
451
+ return this.sub(_vector);
452
+ }
453
+
454
+ reflect(normal) {
455
+ // reflect incident vector off plane orthogonal to normal
456
+ // normal is assumed to have unit length
457
+
458
+ return this.sub(_vector.copy(normal).multiplyScalar(2 * this.dot(normal)));
459
+ }
460
+
461
+ angleTo(v) {
462
+ const denominator = Math.sqrt(this.lengthSq() * v.lengthSq());
463
+
464
+ if (denominator === 0) return Math.PI / 2;
465
+
466
+ const theta = this.dot(v) / denominator;
467
+
468
+ // clamp, to handle numerical problems
469
+
470
+ return Math.acos(MathUtils.clamp(theta, -1, 1));
471
+ }
472
+
473
+ distanceTo(v) {
474
+ return Math.sqrt(this.distanceToSquared(v));
475
+ }
476
+
477
+ distanceToSquared(v) {
478
+ const dx = this.x - v.x,
479
+ dy = this.y - v.y,
480
+ dz = this.z - v.z;
481
+
482
+ return dx * dx + dy * dy + dz * dz;
483
+ }
484
+
485
+ manhattanDistanceTo(v) {
486
+ return Math.abs(this.x - v.x) + Math.abs(this.y - v.y) + Math.abs(this.z - v.z);
487
+ }
488
+
489
+ setFromSpherical(s) {
490
+ return this.setFromSphericalCoords(s.radius, s.phi, s.theta);
491
+ }
492
+
493
+ setFromSphericalCoords(radius, phi, theta) {
494
+ const sinPhiRadius = Math.sin(phi) * radius;
495
+
496
+ this.x = sinPhiRadius * Math.sin(theta);
497
+ this.y = Math.cos(phi) * radius;
498
+ this.z = sinPhiRadius * Math.cos(theta);
499
+
500
+ return this;
501
+ }
502
+
503
+ setFromCylindrical(c) {
504
+ return this.setFromCylindricalCoords(c.radius, c.theta, c.y);
505
+ }
506
+
507
+ setFromCylindricalCoords(radius, theta, y) {
508
+ this.x = radius * Math.sin(theta);
509
+ this.y = y;
510
+ this.z = radius * Math.cos(theta);
511
+
512
+ return this;
513
+ }
514
+
515
+ setFromMatrixPosition(m) {
516
+ const e = m.elements;
517
+
518
+ this.x = e[12];
519
+ this.y = e[13];
520
+ this.z = e[14];
521
+
522
+ return this;
523
+ }
524
+
525
+ setFromMatrixScale(m) {
526
+ const sx = this.setFromMatrixColumn(m, 0).length();
527
+ const sy = this.setFromMatrixColumn(m, 1).length();
528
+ const sz = this.setFromMatrixColumn(m, 2).length();
529
+
530
+ this.x = sx;
531
+ this.y = sy;
532
+ this.z = sz;
533
+
534
+ return this;
535
+ }
536
+
537
+ setFromMatrixColumn(m, index) {
538
+ return this.fromArray(m.elements, index * 4);
539
+ }
540
+
541
+ setFromMatrix3Column(m, index) {
542
+ return this.fromArray(m.elements, index * 3);
543
+ }
544
+
545
+ setFromEuler(e) {
546
+ this.x = e._x;
547
+ this.y = e._y;
548
+ this.z = e._z;
549
+
550
+ return this;
551
+ }
552
+
553
+ equals(v) {
554
+ return v.x === this.x && v.y === this.y && v.z === this.z;
555
+ }
556
+
557
+ fromArray(array, offset = 0) {
558
+ this.x = array[offset];
559
+ this.y = array[offset + 1];
560
+ this.z = array[offset + 2];
561
+
562
+ return this;
563
+ }
564
+
565
+ toArray(array = [], offset = 0) {
566
+ array[offset] = this.x;
567
+ array[offset + 1] = this.y;
568
+ array[offset + 2] = this.z;
569
+
570
+ return array;
571
+ }
572
+
573
+ fromBufferAttribute(attribute, index, offset) {
574
+ if (offset !== undefined) {
575
+ console.warn('THREE.Vector3: offset has been removed from .fromBufferAttribute().');
576
+ }
577
+
578
+ this.x = attribute.getX(index);
579
+ this.y = attribute.getY(index);
580
+ this.z = attribute.getZ(index);
581
+
582
+ return this;
583
+ }
584
+
585
+ random() {
586
+ this.x = Math.random();
587
+ this.y = Math.random();
588
+ this.z = Math.random();
589
+
590
+ return this;
591
+ }
592
+
593
+ randomDirection() {
594
+ // Derived from https://mathworld.wolfram.com/SpherePointPicking.html
595
+
596
+ const u = (Math.random() - 0.5) * 2;
597
+ const t = Math.random() * Math.PI * 2;
598
+ const f = Math.sqrt(1 - u ** 2);
599
+
600
+ this.x = f * Math.cos(t);
601
+ this.y = f * Math.sin(t);
602
+ this.z = u;
603
+
604
+ return this;
605
+ }
606
+
607
+ *[Symbol.iterator]() {
608
+ yield this.x;
609
+ yield this.y;
610
+ yield this.z;
611
+ }
612
+ }
613
+
614
+ const _vector = /*@__PURE__*/ new Vector3();
615
+ const _quaternion = /*@__PURE__*/ new Quaternion();
616
+
617
+ export { Vector3 };