-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy paththree-viewport-gizmo.js
1651 lines (1514 loc) · 56.1 KB
/
three-viewport-gizmo.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
var oe = Object.defineProperty;
var re = (s, e, t) => e in s ? oe(s, e, { enumerable: !0, configurable: !0, writable: !0, value: t }) : s[e] = t;
var m = (s, e, t) => re(s, typeof e != "symbol" ? e + "" : e, t);
import { Vector3 as U, Vector2 as Q, Raycaster as ae, Object3D as ft, Color as Vt, CanvasTexture as ce, RepeatWrapping as zt, SRGBColorSpace as le, BufferGeometry as Xt, BufferAttribute as nt, Sprite as bt, SpriteMaterial as St, Mesh as V, MeshBasicMaterial as at, BackSide as de, SphereGeometry as ue, InstancedBufferGeometry as he, Float32BufferAttribute as Bt, InstancedInterleavedBuffer as pt, InterleavedBufferAttribute as N, WireframeGeometry as fe, Box3 as xt, Sphere as Zt, ShaderMaterial as pe, ShaderLib as it, UniformsUtils as $t, UniformsLib as st, Vector4 as X, Line3 as me, Matrix4 as Yt, MathUtils as ge, Clock as ye, Quaternion as ct, Scene as _e, OrthographicCamera as ve, PerspectiveCamera as we, Spherical as be } from "three";
const Qt = (s, e) => {
const [t, n] = e.split("-");
return Object.assign(s.style, {
left: n === "left" ? "0" : n === "center" ? "50%" : "",
right: n === "right" ? "0" : "",
top: t === "top" ? "0" : t === "bottom" ? "" : "50%",
bottom: t === "bottom" ? "0" : "",
transform: `${n === "center" ? "translateX(-50%)" : ""} ${t === "center" ? "translateY(-50%)" : ""}`
}), e;
}, Se = ({
placement: s,
size: e,
offset: t,
id: n,
className: i
}) => {
const o = document.createElement("div"), { top: a, left: l, right: d, bottom: h } = t;
return Object.assign(o.style, {
id: n,
position: "absolute",
zIndex: "1000",
height: `${e}px`,
width: `${e}px`,
margin: `${a}px ${d}px ${h}px ${l}px`,
borderRadius: "100%"
}), Qt(o, s), n && (o.id = n), i && (o.className = i), o;
}, xe = (s) => {
const e = typeof s == "string" ? document.querySelector(s) : s;
if (!e) throw Error("Invalid DOM element");
return e;
};
function mt(s, e, t) {
return Math.max(e, Math.min(t, s));
}
const Ee = [
["x", 0, 3],
["y", 1, 4],
["z", 2, 5]
], Ct = /* @__PURE__ */ new U();
function Ot({ isSphere: s }, e, t) {
s && (Ct.set(0, 0, 1).applyQuaternion(t.quaternion), Ee.forEach(([n, i, o]) => {
const a = Ct[n];
let l = e[i], d = l.userData.opacity;
l.material.opacity = mt(a >= 0 ? d : d / 2, 0, 1), l = e[o], d = l.userData.opacity, l.material.opacity = mt(a >= 0 ? d / 2 : d, 0, 1);
}));
}
const Ae = (s, e, t = 10) => Math.abs(s.clientX - e.x) < t && Math.abs(s.clientY - e.y) < t, Dt = /* @__PURE__ */ new ae(), Pt = /* @__PURE__ */ new Q(), Gt = (s, e, t, n) => {
Pt.set(
(s.clientX - e.left) / e.width * 2 - 1,
-((s.clientY - e.top) / e.height) * 2 + 1
), Dt.setFromCamera(Pt, t);
const i = Dt.intersectObjects(
n,
!1
), o = i.length ? i[0] : null;
return !o || !o.object.visible ? null : o;
}, lt = 1e-6, Me = 2 * Math.PI, Jt = ["x", "y", "z"], Y = [...Jt, "nx", "ny", "nz"], Te = ["x", "z", "y", "nx", "nz", "ny"], Ue = ["z", "x", "y", "nz", "nx", "ny"], gt = "Right", ot = "Top", yt = "Front", _t = "Left", rt = "Bottom", vt = "Back", Le = [
gt,
ot,
yt,
_t,
rt,
vt
].map((s) => s.toLocaleLowerCase()), Kt = 1.3, Rt = (s, e = !0) => {
const { material: t, userData: n } = s, { color: i, opacity: o } = e ? n.hover : n;
t.color.set(i), t.opacity = o;
}, k = (s) => JSON.parse(JSON.stringify(s)), ze = (s) => {
const e = s.type || "sphere", t = e === "sphere", n = s.resolution || t ? 64 : 128, i = ft.DEFAULT_UP, o = i.z === 1, a = i.x === 1, { container: l } = s;
s.container = void 0, s = JSON.parse(JSON.stringify(s)), s.container = l;
const d = o ? Te : a ? Ue : Y;
Le.forEach((c, f) => {
s[c] && (s[d[f]] = s[c]);
});
const h = {
enabled: !0,
color: 16777215,
opacity: 1,
scale: 0.7,
labelColor: 2236962,
line: !1,
border: {
size: 0,
color: 14540253
},
hover: {
color: t ? 16777215 : 9688043,
labelColor: 2236962,
opacity: 1,
scale: 0.7,
border: {
size: 0,
color: 14540253
}
}
}, r = {
line: !1,
scale: t ? 0.45 : 0.7,
hover: {
scale: t ? 0.5 : 0.7
}
}, u = {
type: e,
container: document.body,
size: 128,
placement: "top-right",
resolution: n,
lineWidth: 4,
radius: t ? 1 : 0.2,
smoothness: 18,
animated: !0,
speed: 1,
background: {
enabled: !0,
color: t ? 16777215 : 14739180,
opacity: t ? 0 : 1,
hover: {
color: t ? 16777215 : 14739180,
opacity: t ? 0.2 : 1
}
},
font: {
family: "sans-serif",
weight: 900
},
offset: {
top: 10,
left: 10,
bottom: 10,
right: 10
},
corners: {
enabled: !t,
color: t ? 15915362 : 16777215,
opacity: 1,
scale: t ? 0.15 : 0.2,
radius: 1,
smoothness: 18,
hover: {
color: t ? 16777215 : 9688043,
opacity: 1,
scale: t ? 0.2 : 0.225
}
},
edges: {
enabled: !t,
color: t ? 15915362 : 16777215,
opacity: t ? 1 : 0,
radius: t ? 1 : 0.125,
smoothness: 18,
scale: t ? 0.15 : 1,
hover: {
color: t ? 16777215 : 9688043,
opacity: 1,
scale: t ? 0.2 : 1
}
},
x: {
...k(h),
...t ? { label: "X", color: 16725587, line: !0 } : { label: a ? ot : gt }
},
y: {
...k(h),
...t ? { label: "Y", color: 9100032, line: !0 } : { label: o || a ? yt : ot }
},
z: {
...k(h),
...t ? { label: "Z", color: 2920447, line: !0 } : {
label: o ? ot : a ? gt : yt
}
},
nx: {
...k(r),
label: t ? "" : a ? rt : _t
},
ny: {
...k(r),
label: t ? "" : o || a ? vt : rt
},
nz: {
...k(r),
label: t ? "" : o ? rt : a ? _t : vt
}
};
return wt(s, u), Jt.forEach(
(c) => wt(
s[`n${c}`],
k(s[c])
)
), { ...s, isSphere: t };
};
function wt(s, ...e) {
if (s instanceof HTMLElement || typeof s != "object" || s === null)
return s;
for (const t of e)
for (const n in t)
n !== "container" && n in t && (s[n] === void 0 ? s[n] = t[n] : typeof t[n] == "object" && !Array.isArray(t[n]) && (s[n] = wt(
s[n] || {},
t[n]
)));
return s;
}
const Be = (s, e = 2) => {
const t = new Vt(), n = e * 2, { isSphere: i, resolution: o, radius: a, font: l, corners: d, edges: h } = s, r = Y.map((p) => ({ ...s[p], radius: a }));
i && d.enabled && r.push(d), i && h.enabled && r.push(h);
const u = document.createElement("canvas"), c = u.getContext("2d");
u.width = o * 2 + n * 2, u.height = o * r.length + n * r.length;
const [f, y] = Z(r, o, l);
r.forEach(
({
radius: p,
label: x,
color: I,
labelColor: _,
border: b,
hover: {
color: F,
labelColor: B,
border: C
}
}, G) => {
const R = o * G + G * n + e;
L(
e,
R,
e,
o,
p,
x,
b,
I,
_
), L(
o + e * 3,
R,
e,
o,
p,
x,
C ?? b,
F ?? I,
B ?? _
);
}
);
const v = r.length, g = e / (o * 2), S = e / (o * 6), w = 1 / v, E = new ce(u);
return E.repeat.set(0.5 - 2 * g, w - 2 * S), E.offset.set(g, 1 - S), Object.assign(E, {
colorSpace: le,
wrapS: zt,
wrapT: zt,
userData: {
offsetX: g,
offsetY: S,
cellHeight: w
}
}), E;
function L(p, x, I, _, b, F, B, C, G) {
if (b = b * (_ / 2), C != null && C !== "" && (R(), c.fillStyle = t.set(C).getStyle(), c.fill()), B && B.size) {
const W = B.size * _ / 2;
p += W, x += W, _ -= B.size * _, b = Math.max(0, b - W), R(), c.strokeStyle = t.set(B.color).getStyle(), c.lineWidth = B.size * _, c.stroke();
}
F && z(
c,
p + _ / 2,
x + (_ + I) / 2,
F,
t.set(G).getStyle()
);
function R() {
c.beginPath(), c.moveTo(p + b, x), c.lineTo(p + _ - b, x), c.arcTo(p + _, x, p + _, x + b, b), c.lineTo(p + _, x + _ - b), c.arcTo(p + _, x + _, p + _ - b, x + _, b), c.lineTo(p + b, x + _), c.arcTo(p, x + _, p, x + _ - b, b), c.lineTo(p, x + b), c.arcTo(p, x, p + b, x, b), c.closePath();
}
}
function Z(p, x, I) {
const b = [...p].sort((J, se) => {
var Ut, Lt;
return (((Ut = J.label) == null ? void 0 : Ut.length) || 0) - (((Lt = se.label) == null ? void 0 : Lt.length) || 0);
}).pop().label, { family: F, weight: B } = I, C = i ? Math.sqrt(Math.pow(x * 0.7, 2) / 2) : x;
let G = C, R = 0, W = 0;
do {
c.font = `${B} ${G}px ${F}`;
const J = c.measureText(b);
R = J.width, W = J.fontBoundingBoxDescent, G--;
} while (R > C && G > 0);
const Tt = C / W, ne = Math.min(C / R, Tt), ie = Math.floor(G * ne);
return [`${B} ${ie}px ${F}`, Tt];
}
function z(p, x, I, _, b) {
p.font = f, p.textAlign = "center", p.textBaseline = "middle", p.fillStyle = b, p.fillText(_, x, I + (i ? y : 0));
}
}, Ce = (s, e) => s.offset.x = (e ? 0.5 : 0) + s.userData.offsetX, Et = (s, e) => {
const {
offset: t,
userData: { offsetY: n, cellHeight: i }
} = s;
t.y = 1 - (e + 1) * i + n;
};
function At(s, e, t = 2, n = 2) {
const i = t / 2 - s, o = n / 2 - s, a = s / t, l = (t - s) / t, d = s / n, h = (n - s) / n, r = [i, o, 0, -i, o, 0, -i, -o, 0, i, -o, 0], u = [l, h, a, h, a, d, l, d], c = [
3 * (e + 1) + 3,
3 * (e + 1) + 4,
e + 4,
e + 5,
2 * (e + 1) + 4,
2,
1,
2 * (e + 1) + 3,
3,
4 * (e + 1) + 3,
4,
0
], f = [0, 1, 2, 0, 2, 3, 4, 5, 6, 4, 6, 7, 8, 9, 10, 8, 10, 11].map(
(z) => c[z]
);
let y, v, g, S, w, E, L, Z;
for (let z = 0; z < 4; z++) {
S = z < 1 || z > 2 ? i : -i, w = z < 2 ? o : -o, E = z < 1 || z > 2 ? l : a, L = z < 2 ? h : d;
for (let p = 0; p <= e; p++)
y = Math.PI / 2 * (z + p / e), v = Math.cos(y), g = Math.sin(y), r.push(S + s * v, w + s * g, 0), u.push(E + a * v, L + d * g), p < e && (Z = (e + 1) * z + p + 4, f.push(z, Z, Z + 1));
}
return new Xt().setIndex(new nt(new Uint32Array(f), 1)).setAttribute(
"position",
new nt(new Float32Array(r), 3)
).setAttribute("uv", new nt(new Float32Array(u), 2));
}
const Oe = (s, e) => {
const t = new U(), { isSphere: n, radius: i, smoothness: o } = s, a = At(i, o);
return Y.map((l, d) => {
const h = d < 3, r = Y[d], u = d ? e.clone() : e;
Et(u, d);
const { enabled: c, scale: f, opacity: y, hover: v } = s[r], g = {
map: u,
opacity: y,
transparent: !0
}, S = n ? new bt(new St(g)) : new V(a, new at(g)), w = h ? r : r[1];
return S.position[w] = (h ? 1 : -1) * (n ? Kt : 1), n || S.lookAt(t.copy(S.position).multiplyScalar(1.7)), S.scale.setScalar(f), S.renderOrder = 1, S.visible = c, S.userData = {
scale: f,
opacity: y,
hover: v
}, S;
});
}, De = (s, e) => {
const { isSphere: t, corners: n } = s;
if (!n.enabled) return [];
const { color: i, opacity: o, scale: a, radius: l, smoothness: d, hover: h } = n, r = t ? null : At(l, d), u = {
transparent: !0,
opacity: o
}, c = [
1,
1,
1,
-1,
1,
1,
1,
-1,
1,
-1,
-1,
1,
1,
1,
-1,
-1,
1,
-1,
1,
-1,
-1,
-1,
-1,
-1
].map((y) => y * 0.85), f = new U();
return Array(c.length / 3).fill(0).map((y, v) => {
if (t) {
const w = e.clone();
Et(w, 6), u.map = w;
} else
u.color = i;
const g = t ? new bt(new St(u)) : new V(r, new at(u)), S = v * 3;
return g.position.set(c[S], c[S + 1], c[S + 2]), t && g.position.normalize().multiplyScalar(1.7), g.scale.setScalar(a), g.lookAt(f.copy(g.position).multiplyScalar(2)), g.renderOrder = 1, g.userData = {
color: i,
opacity: o,
scale: a,
hover: h
}, g;
});
}, Pe = (s, e, t) => {
const { isSphere: n, edges: i } = s;
if (!i.enabled) return [];
const { color: o, opacity: a, scale: l, hover: d, radius: h, smoothness: r } = i, u = n ? null : At(h, r, 1.2, 0.25), c = {
transparent: !0,
opacity: a
}, f = [
0,
1,
1,
0,
-1,
1,
1,
0,
1,
-1,
0,
1,
0,
1,
-1,
0,
-1,
-1,
1,
0,
-1,
-1,
0,
-1,
1,
1,
0,
1,
-1,
0,
-1,
1,
0,
-1,
-1,
0
].map((g) => g * 0.925), y = new U(), v = new U(0, 1, 0);
return Array(f.length / 3).fill(0).map((g, S) => {
if (n) {
const L = e.clone();
Et(L, t), c.map = L;
} else
c.color = o;
const w = n ? new bt(new St(c)) : new V(u, new at(c)), E = S * 3;
return w.position.set(f[E], f[E + 1], f[E + 2]), n && w.position.normalize().multiplyScalar(1.7), w.scale.setScalar(l), w.up.copy(v), w.lookAt(y.copy(w.position).multiplyScalar(2)), !n && !w.position.y && (w.rotation.z = Math.PI / 2), w.renderOrder = 1, w.userData = {
color: o,
opacity: a,
scale: l,
hover: d
}, w;
});
};
function Ge(s, e = !1) {
const t = s[0].index !== null, n = new Set(Object.keys(s[0].attributes)), i = new Set(Object.keys(s[0].morphAttributes)), o = {}, a = {}, l = s[0].morphTargetsRelative, d = new Xt();
let h = 0;
for (let r = 0; r < s.length; ++r) {
const u = s[r];
let c = 0;
if (t !== (u.index !== null))
return console.error("THREE.BufferGeometryUtils: .mergeGeometries() failed with geometry at index " + r + ". All geometries must have compatible attributes; make sure index attribute exists among all geometries, or in none of them."), null;
for (const f in u.attributes) {
if (!n.has(f))
return console.error("THREE.BufferGeometryUtils: .mergeGeometries() failed with geometry at index " + r + '. All geometries must have compatible attributes; make sure "' + f + '" attribute exists among all geometries, or in none of them.'), null;
o[f] === void 0 && (o[f] = []), o[f].push(u.attributes[f]), c++;
}
if (c !== n.size)
return console.error("THREE.BufferGeometryUtils: .mergeGeometries() failed with geometry at index " + r + ". Make sure all geometries have the same number of attributes."), null;
if (l !== u.morphTargetsRelative)
return console.error("THREE.BufferGeometryUtils: .mergeGeometries() failed with geometry at index " + r + ". .morphTargetsRelative must be consistent throughout all geometries."), null;
for (const f in u.morphAttributes) {
if (!i.has(f))
return console.error("THREE.BufferGeometryUtils: .mergeGeometries() failed with geometry at index " + r + ". .morphAttributes must be consistent throughout all geometries."), null;
a[f] === void 0 && (a[f] = []), a[f].push(u.morphAttributes[f]);
}
if (e) {
let f;
if (t)
f = u.index.count;
else if (u.attributes.position !== void 0)
f = u.attributes.position.count;
else
return console.error("THREE.BufferGeometryUtils: .mergeGeometries() failed with geometry at index " + r + ". The geometry must have either an index or a position attribute"), null;
d.addGroup(h, f, r), h += f;
}
}
if (t) {
let r = 0;
const u = [];
for (let c = 0; c < s.length; ++c) {
const f = s[c].index;
for (let y = 0; y < f.count; ++y)
u.push(f.getX(y) + r);
r += s[c].attributes.position.count;
}
d.setIndex(u);
}
for (const r in o) {
const u = It(o[r]);
if (!u)
return console.error("THREE.BufferGeometryUtils: .mergeGeometries() failed while trying to merge the " + r + " attribute."), null;
d.setAttribute(r, u);
}
for (const r in a) {
const u = a[r][0].length;
if (u === 0) break;
d.morphAttributes = d.morphAttributes || {}, d.morphAttributes[r] = [];
for (let c = 0; c < u; ++c) {
const f = [];
for (let v = 0; v < a[r].length; ++v)
f.push(a[r][v][c]);
const y = It(f);
if (!y)
return console.error("THREE.BufferGeometryUtils: .mergeGeometries() failed while trying to merge the " + r + " morphAttribute."), null;
d.morphAttributes[r].push(y);
}
}
return d;
}
function It(s) {
let e, t, n, i = -1, o = 0;
for (let h = 0; h < s.length; ++h) {
const r = s[h];
if (e === void 0 && (e = r.array.constructor), e !== r.array.constructor)
return console.error("THREE.BufferGeometryUtils: .mergeAttributes() failed. BufferAttribute.array must be of consistent array types across matching attributes."), null;
if (t === void 0 && (t = r.itemSize), t !== r.itemSize)
return console.error("THREE.BufferGeometryUtils: .mergeAttributes() failed. BufferAttribute.itemSize must be consistent across matching attributes."), null;
if (n === void 0 && (n = r.normalized), n !== r.normalized)
return console.error("THREE.BufferGeometryUtils: .mergeAttributes() failed. BufferAttribute.normalized must be consistent across matching attributes."), null;
if (i === -1 && (i = r.gpuType), i !== r.gpuType)
return console.error("THREE.BufferGeometryUtils: .mergeAttributes() failed. BufferAttribute.gpuType must be consistent across matching attributes."), null;
o += r.count * t;
}
const a = new e(o), l = new nt(a, t, n);
let d = 0;
for (let h = 0; h < s.length; ++h) {
const r = s[h];
if (r.isInterleavedBufferAttribute) {
const u = d / t;
for (let c = 0, f = r.count; c < f; c++)
for (let y = 0; y < t; y++) {
const v = r.getComponent(c, y);
l.setComponent(c + u, y, v);
}
} else
a.set(r.array, d);
d += r.count * t;
}
return i !== void 0 && (l.gpuType = i), l;
}
const Re = (s, e) => {
const {
isSphere: t,
background: { enabled: n, color: i, opacity: o, hover: a }
} = e;
let l;
const d = new at({
color: i,
side: de,
opacity: o,
transparent: !0,
depthWrite: !1
});
if (!n) return null;
if (t)
l = new V(
new ue(1.8, 64, 64),
d
);
else {
let h;
s.forEach((r) => {
const u = r.scale.x;
r.scale.setScalar(0.9), r.updateMatrix();
const c = r.geometry.clone();
c.applyMatrix4(r.matrix), h = h ? Ge([h, c]) : c, r.scale.setScalar(u);
}), l = new V(h, d);
}
return l.userData = {
color: i,
opacity: o,
hover: a
}, l;
}, Ft = new xt(), K = new U();
class te extends he {
constructor() {
super(), this.isLineSegmentsGeometry = !0, this.type = "LineSegmentsGeometry";
const e = [-1, 2, 0, 1, 2, 0, -1, 1, 0, 1, 1, 0, -1, 0, 0, 1, 0, 0, -1, -1, 0, 1, -1, 0], t = [-1, 2, 1, 2, -1, 1, 1, 1, -1, -1, 1, -1, -1, -2, 1, -2], n = [0, 2, 1, 2, 3, 1, 2, 4, 3, 4, 5, 3, 4, 6, 5, 6, 7, 5];
this.setIndex(n), this.setAttribute("position", new Bt(e, 3)), this.setAttribute("uv", new Bt(t, 2));
}
applyMatrix4(e) {
const t = this.attributes.instanceStart, n = this.attributes.instanceEnd;
return t !== void 0 && (t.applyMatrix4(e), n.applyMatrix4(e), t.needsUpdate = !0), this.boundingBox !== null && this.computeBoundingBox(), this.boundingSphere !== null && this.computeBoundingSphere(), this;
}
setPositions(e) {
let t;
e instanceof Float32Array ? t = e : Array.isArray(e) && (t = new Float32Array(e));
const n = new pt(t, 6, 1);
return this.setAttribute("instanceStart", new N(n, 3, 0)), this.setAttribute("instanceEnd", new N(n, 3, 3)), this.instanceCount = this.attributes.instanceStart.count, this.computeBoundingBox(), this.computeBoundingSphere(), this;
}
setColors(e) {
let t;
e instanceof Float32Array ? t = e : Array.isArray(e) && (t = new Float32Array(e));
const n = new pt(t, 6, 1);
return this.setAttribute("instanceColorStart", new N(n, 3, 0)), this.setAttribute("instanceColorEnd", new N(n, 3, 3)), this;
}
fromWireframeGeometry(e) {
return this.setPositions(e.attributes.position.array), this;
}
fromEdgesGeometry(e) {
return this.setPositions(e.attributes.position.array), this;
}
fromMesh(e) {
return this.fromWireframeGeometry(new fe(e.geometry)), this;
}
fromLineSegments(e) {
const t = e.geometry;
return this.setPositions(t.attributes.position.array), this;
}
computeBoundingBox() {
this.boundingBox === null && (this.boundingBox = new xt());
const e = this.attributes.instanceStart, t = this.attributes.instanceEnd;
e !== void 0 && t !== void 0 && (this.boundingBox.setFromBufferAttribute(e), Ft.setFromBufferAttribute(t), this.boundingBox.union(Ft));
}
computeBoundingSphere() {
this.boundingSphere === null && (this.boundingSphere = new Zt()), this.boundingBox === null && this.computeBoundingBox();
const e = this.attributes.instanceStart, t = this.attributes.instanceEnd;
if (e !== void 0 && t !== void 0) {
const n = this.boundingSphere.center;
this.boundingBox.getCenter(n);
let i = 0;
for (let o = 0, a = e.count; o < a; o++)
K.fromBufferAttribute(e, o), i = Math.max(i, n.distanceToSquared(K)), K.fromBufferAttribute(t, o), i = Math.max(i, n.distanceToSquared(K));
this.boundingSphere.radius = Math.sqrt(i), isNaN(this.boundingSphere.radius) && console.error("THREE.LineSegmentsGeometry.computeBoundingSphere(): Computed radius is NaN. The instanced position data is likely to have NaN values.", this);
}
}
toJSON() {
}
applyMatrix(e) {
return console.warn("THREE.LineSegmentsGeometry: applyMatrix() has been renamed to applyMatrix4()."), this.applyMatrix4(e);
}
}
st.line = {
worldUnits: { value: 1 },
linewidth: { value: 1 },
resolution: { value: new Q(1, 1) },
dashOffset: { value: 0 },
dashScale: { value: 1 },
dashSize: { value: 1 },
gapSize: { value: 1 }
// todo FIX - maybe change to totalSize
};
it.line = {
uniforms: $t.merge([
st.common,
st.fog,
st.line
]),
vertexShader: (
/* glsl */
`
#include <common>
#include <color_pars_vertex>
#include <fog_pars_vertex>
#include <logdepthbuf_pars_vertex>
#include <clipping_planes_pars_vertex>
uniform float linewidth;
uniform vec2 resolution;
attribute vec3 instanceStart;
attribute vec3 instanceEnd;
attribute vec3 instanceColorStart;
attribute vec3 instanceColorEnd;
#ifdef WORLD_UNITS
varying vec4 worldPos;
varying vec3 worldStart;
varying vec3 worldEnd;
#ifdef USE_DASH
varying vec2 vUv;
#endif
#else
varying vec2 vUv;
#endif
#ifdef USE_DASH
uniform float dashScale;
attribute float instanceDistanceStart;
attribute float instanceDistanceEnd;
varying float vLineDistance;
#endif
void trimSegment( const in vec4 start, inout vec4 end ) {
// trim end segment so it terminates between the camera plane and the near plane
// conservative estimate of the near plane
float a = projectionMatrix[ 2 ][ 2 ]; // 3nd entry in 3th column
float b = projectionMatrix[ 3 ][ 2 ]; // 3nd entry in 4th column
float nearEstimate = - 0.5 * b / a;
float alpha = ( nearEstimate - start.z ) / ( end.z - start.z );
end.xyz = mix( start.xyz, end.xyz, alpha );
}
void main() {
#ifdef USE_COLOR
vColor.xyz = ( position.y < 0.5 ) ? instanceColorStart : instanceColorEnd;
#endif
#ifdef USE_DASH
vLineDistance = ( position.y < 0.5 ) ? dashScale * instanceDistanceStart : dashScale * instanceDistanceEnd;
vUv = uv;
#endif
float aspect = resolution.x / resolution.y;
// camera space
vec4 start = modelViewMatrix * vec4( instanceStart, 1.0 );
vec4 end = modelViewMatrix * vec4( instanceEnd, 1.0 );
#ifdef WORLD_UNITS
worldStart = start.xyz;
worldEnd = end.xyz;
#else
vUv = uv;
#endif
// special case for perspective projection, and segments that terminate either in, or behind, the camera plane
// clearly the gpu firmware has a way of addressing this issue when projecting into ndc space
// but we need to perform ndc-space calculations in the shader, so we must address this issue directly
// perhaps there is a more elegant solution -- WestLangley
bool perspective = ( projectionMatrix[ 2 ][ 3 ] == - 1.0 ); // 4th entry in the 3rd column
if ( perspective ) {
if ( start.z < 0.0 && end.z >= 0.0 ) {
trimSegment( start, end );
} else if ( end.z < 0.0 && start.z >= 0.0 ) {
trimSegment( end, start );
}
}
// clip space
vec4 clipStart = projectionMatrix * start;
vec4 clipEnd = projectionMatrix * end;
// ndc space
vec3 ndcStart = clipStart.xyz / clipStart.w;
vec3 ndcEnd = clipEnd.xyz / clipEnd.w;
// direction
vec2 dir = ndcEnd.xy - ndcStart.xy;
// account for clip-space aspect ratio
dir.x *= aspect;
dir = normalize( dir );
#ifdef WORLD_UNITS
vec3 worldDir = normalize( end.xyz - start.xyz );
vec3 tmpFwd = normalize( mix( start.xyz, end.xyz, 0.5 ) );
vec3 worldUp = normalize( cross( worldDir, tmpFwd ) );
vec3 worldFwd = cross( worldDir, worldUp );
worldPos = position.y < 0.5 ? start: end;
// height offset
float hw = linewidth * 0.5;
worldPos.xyz += position.x < 0.0 ? hw * worldUp : - hw * worldUp;
// don't extend the line if we're rendering dashes because we
// won't be rendering the endcaps
#ifndef USE_DASH
// cap extension
worldPos.xyz += position.y < 0.5 ? - hw * worldDir : hw * worldDir;
// add width to the box
worldPos.xyz += worldFwd * hw;
// endcaps
if ( position.y > 1.0 || position.y < 0.0 ) {
worldPos.xyz -= worldFwd * 2.0 * hw;
}
#endif
// project the worldpos
vec4 clip = projectionMatrix * worldPos;
// shift the depth of the projected points so the line
// segments overlap neatly
vec3 clipPose = ( position.y < 0.5 ) ? ndcStart : ndcEnd;
clip.z = clipPose.z * clip.w;
#else
vec2 offset = vec2( dir.y, - dir.x );
// undo aspect ratio adjustment
dir.x /= aspect;
offset.x /= aspect;
// sign flip
if ( position.x < 0.0 ) offset *= - 1.0;
// endcaps
if ( position.y < 0.0 ) {
offset += - dir;
} else if ( position.y > 1.0 ) {
offset += dir;
}
// adjust for linewidth
offset *= linewidth;
// adjust for clip-space to screen-space conversion // maybe resolution should be based on viewport ...
offset /= resolution.y;
// select end
vec4 clip = ( position.y < 0.5 ) ? clipStart : clipEnd;
// back to clip space
offset *= clip.w;
clip.xy += offset;
#endif
gl_Position = clip;
vec4 mvPosition = ( position.y < 0.5 ) ? start : end; // this is an approximation
#include <logdepthbuf_vertex>
#include <clipping_planes_vertex>
#include <fog_vertex>
}
`
),
fragmentShader: (
/* glsl */
`
uniform vec3 diffuse;
uniform float opacity;
uniform float linewidth;
#ifdef USE_DASH
uniform float dashOffset;
uniform float dashSize;
uniform float gapSize;
#endif
varying float vLineDistance;
#ifdef WORLD_UNITS
varying vec4 worldPos;
varying vec3 worldStart;
varying vec3 worldEnd;
#ifdef USE_DASH
varying vec2 vUv;
#endif
#else
varying vec2 vUv;
#endif
#include <common>
#include <color_pars_fragment>
#include <fog_pars_fragment>
#include <logdepthbuf_pars_fragment>
#include <clipping_planes_pars_fragment>
vec2 closestLineToLine(vec3 p1, vec3 p2, vec3 p3, vec3 p4) {
float mua;
float mub;
vec3 p13 = p1 - p3;
vec3 p43 = p4 - p3;
vec3 p21 = p2 - p1;
float d1343 = dot( p13, p43 );
float d4321 = dot( p43, p21 );
float d1321 = dot( p13, p21 );
float d4343 = dot( p43, p43 );
float d2121 = dot( p21, p21 );
float denom = d2121 * d4343 - d4321 * d4321;
float numer = d1343 * d4321 - d1321 * d4343;
mua = numer / denom;
mua = clamp( mua, 0.0, 1.0 );
mub = ( d1343 + d4321 * ( mua ) ) / d4343;
mub = clamp( mub, 0.0, 1.0 );
return vec2( mua, mub );
}
void main() {
#include <clipping_planes_fragment>
#ifdef USE_DASH
if ( vUv.y < - 1.0 || vUv.y > 1.0 ) discard; // discard endcaps
if ( mod( vLineDistance + dashOffset, dashSize + gapSize ) > dashSize ) discard; // todo - FIX
#endif
float alpha = opacity;
#ifdef WORLD_UNITS
// Find the closest points on the view ray and the line segment
vec3 rayEnd = normalize( worldPos.xyz ) * 1e5;
vec3 lineDir = worldEnd - worldStart;
vec2 params = closestLineToLine( worldStart, worldEnd, vec3( 0.0, 0.0, 0.0 ), rayEnd );
vec3 p1 = worldStart + lineDir * params.x;
vec3 p2 = rayEnd * params.y;
vec3 delta = p1 - p2;
float len = length( delta );
float norm = len / linewidth;
#ifndef USE_DASH
#ifdef USE_ALPHA_TO_COVERAGE
float dnorm = fwidth( norm );
alpha = 1.0 - smoothstep( 0.5 - dnorm, 0.5 + dnorm, norm );
#else
if ( norm > 0.5 ) {
discard;
}
#endif
#endif
#else
#ifdef USE_ALPHA_TO_COVERAGE
// artifacts appear on some hardware if a derivative is taken within a conditional
float a = vUv.x;
float b = ( vUv.y > 0.0 ) ? vUv.y - 1.0 : vUv.y + 1.0;
float len2 = a * a + b * b;
float dlen = fwidth( len2 );
if ( abs( vUv.y ) > 1.0 ) {
alpha = 1.0 - smoothstep( 1.0 - dlen, 1.0 + dlen, len2 );
}