poly-extrude 0.1.0 → 0.3.0

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