Repository navigation
Expand file tree
/
Copy pathGraphEngine.ts
More file actions
1238 lines (1128 loc) · 46 KB
/
Copy pathGraphEngine.ts
File metadata and controls
1238 lines (1128 loc) · 46 KB
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
import * as THREE from 'three';
import { OrbitControls } from 'three/examples/jsm/controls/OrbitControls.js';
import { ForceLayout } from './ForceLayout';
import { colorForGroup, getGroups } from './themes';
import type { GraphData, GraphNode, Theme } from './types';
export interface GraphEngineCallbacks {
onHover?: (node: GraphNode | null) => void;
onSelect?: (node: GraphNode | null) => void;
}
const PARTICLES_PER_LINK = 2;
// Points trailing each spark, drawn smaller and fainter to form a comet tail.
const SPARK_TRAIL = 4;
const TOP_LABELS = 14;
const NODE_DIM = 0.12;
const LINK_DIM = 0.06;
const LINK_FOCUS = 2.4;
const CURSOR_RADIUS_PX = 110;
// Nodes this close to the pointer are left alone so they can be hovered and grabbed.
const CURSOR_CALM_PX = 36;
const STEP = 1 / 60;
const POINT_VERT = /* glsl */ `
attribute vec3 aColor;
attribute float aSize;
attribute float aAlpha;
uniform float uScale;
varying vec3 vColor;
varying float vAlpha;
void main() {
vec4 mv = modelViewMatrix * vec4(position, 1.0);
gl_Position = projectionMatrix * mv;
gl_PointSize = aSize * uScale / max(-mv.z, 0.001);
vColor = aColor;
vAlpha = aAlpha;
}`;
const POINT_FRAG = /* glsl */ `
uniform float uSoftness;
// Fraction of the sprite radius covered by the node; the glow starts there so it isn't hidden behind the sphere.
uniform float uInner;
varying vec3 vColor;
varying float vAlpha;
void main() {
float d = length(gl_PointCoord - 0.5) * 2.0;
if (d > 1.0) discard;
float t = clamp((d - uInner) / (1.0 - uInner), 0.0, 1.0);
gl_FragColor = vec4(vColor, pow(1.0 - t, uSoftness) * vAlpha);
#include <colorspace_fragment>
}`;
// A spark: white-hot core inside a coloured glow, like a tiny version of the orb nodes.
const SPARK_FRAG = /* glsl */ `
uniform float uCore;
varying vec3 vColor;
varying float vAlpha;
void main() {
float d = length(gl_PointCoord - 0.5) * 2.0;
if (d > 1.0) discard;
float core = smoothstep(0.22, 0.0, d);
float glow = pow(1.0 - d, 2.6);
vec3 col = mix(vColor, vec3(1.0), core * uCore);
gl_FragColor = vec4(col, (core + glow * 0.65) * vAlpha);
#include <colorspace_fragment>
}`;
const LINE_VERT = /* glsl */ `
attribute vec3 aColor;
attribute float aAlpha;
varying vec3 vColor;
varying float vAlpha;
void main() {
vColor = aColor;
vAlpha = aAlpha;
gl_Position = projectionMatrix * modelViewMatrix * vec4(position, 1.0);
}`;
const LINE_FRAG = /* glsl */ `
varying vec3 vColor;
varying float vAlpha;
void main() {
gl_FragColor = vec4(vColor, vAlpha);
#include <colorspace_fragment>
}`;
const easeInOutCubic = (t: number) => (t < 0.5 ? 4 * t * t * t : 1 - Math.pow(-2 * t + 2, 3) / 2);
const damp = (a: number, b: number, lambda: number, dt: number) => a + (b - a) * (1 - Math.exp(-lambda * dt));
function dynamicAttr(array: Float32Array, size: number): THREE.BufferAttribute {
return new THREE.BufferAttribute(array, size).setUsage(THREE.DynamicDrawUsage);
}
interface CameraTween {
fromPos: THREE.Vector3;
fromTarget: THREE.Vector3;
toPos: THREE.Vector3;
toTarget: THREE.Vector3;
start: number;
duration: number;
}
export class GraphEngine {
private readonly container: HTMLElement;
private readonly renderer: THREE.WebGLRenderer;
private readonly scene = new THREE.Scene();
private readonly camera: THREE.PerspectiveCamera;
private readonly controls: OrbitControls;
private readonly labelLayer: HTMLDivElement;
private readonly graphGroup = new THREE.Group();
private readonly envGroup = new THREE.Group();
private readonly camLight = new THREE.DirectionalLight(0xffffff, 1);
private readonly raycaster = new THREE.Raycaster();
private readonly resizeObserver: ResizeObserver;
private theme: Theme;
private readonly cb: GraphEngineCallbacks;
private readonly reducedMotion = window.matchMedia?.('(prefers-reduced-motion: reduce)').matches ?? false;
private raf = 0;
private time = 0;
private lastTs = performance.now();
private stepAcc = 0;
private width = 1;
private height = 1;
private pointScale = 1;
// Graph state
private nodes: GraphNode[] = [];
private idToIndex = new Map<string, number>();
private linkIdx = new Int32Array(0);
private neighbors: number[][] = [];
private groupOf: string[] = [];
private layout: ForceLayout | null = null;
private vpos = new Float32Array(0);
private radius = new Float32Array(0);
private colors: THREE.Color[] = [];
private scaleCur = new Float32Array(0);
private scaleTarget = new Float32Array(0);
private emphCur = new Float32Array(0);
private emphTarget = new Float32Array(0);
private linkEmphCur = new Float32Array(0);
private linkEmphTarget = new Float32Array(0);
private focusMask = new Uint8Array(0);
private particlePhase = new Float32Array(0);
private labelAlpha = new Float32Array(0);
private labelShown = new Uint8Array(0);
private labelTarget = new Float32Array(0);
private labelPriority = new Float32Array(0);
private labelX = new Float32Array(0);
private labelY = new Float32Array(0);
private labelZ = new Float32Array(0);
private labelW = new Float32Array(0);
private readonly labelCand: number[] = [];
private readonly labelBoxes: number[] = [];
private labelEls: HTMLDivElement[] = [];
private topNodes = new Set<number>();
private needsFit = false;
private pendingFocus = -1;
manualClock = false;
// Visuals
private nodeMesh: THREE.InstancedMesh | null = null;
private halo: THREE.Points | null = null;
private lines: THREE.LineSegments | null = null;
private particles: THREE.Points | null = null;
private selRing: THREE.Group | null = null;
private stars: THREE.Points | null = null;
private grid: THREE.GridHelper | null = null;
private ripples: { mesh: THREE.Mesh; life: number; idx: number }[] = [];
private readonly rippleGeo = new THREE.RingGeometry(0.92, 1, 64);
private fogColor = new THREE.Color();
// Interaction
private hovered = -1;
private selected = -1;
private groupHighlight: string | null = null;
private readonly pointer = {
x: 0, y: 0, vx: 0, vy: 0, ndc: new THREE.Vector2(),
inside: false, down: false, moved: false, downX: 0, downY: 0,
};
private dragIndex = -1;
private dragging = false;
private readonly dragPlane = new THREE.Plane();
private orbiting = false;
private lastInteraction = 0;
private impulses: { at: number; idx: number; x: number; y: number; z: number }[] = [];
private tween: CameraTween | null = null;
private starTilt = new THREE.Vector2();
private readonly v1 = new THREE.Vector3();
private readonly v2 = new THREE.Vector3();
private readonly right = new THREE.Vector3();
private readonly up = new THREE.Vector3();
private readonly mat = new THREE.Matrix4();
private readonly quat = new THREE.Quaternion();
private readonly euler = new THREE.Euler();
private readonly scl = new THREE.Vector3();
private readonly col = new THREE.Color();
constructor(container: HTMLElement, theme: Theme, cb: GraphEngineCallbacks = {}) {
this.container = container;
this.theme = theme;
this.cb = cb;
this.renderer = new THREE.WebGLRenderer({ antialias: true, alpha: true, powerPreference: 'high-performance' });
this.renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
this.renderer.setClearColor(0x000000, 0);
this.renderer.outputColorSpace = THREE.SRGBColorSpace;
this.renderer.domElement.className = 'kg-canvas';
container.appendChild(this.renderer.domElement);
this.labelLayer = document.createElement('div');
this.labelLayer.className = 'kg-labels';
container.appendChild(this.labelLayer);
this.camera = new THREE.PerspectiveCamera(50, 1, 0.5, 9000);
this.camera.position.set(0, 80, 600);
this.camLight.position.set(0.4, 0.6, 1);
this.camera.add(this.camLight);
this.scene.add(this.camera, this.graphGroup, this.envGroup);
// Registered before OrbitControls so node presses can disable orbiting first.
const el = this.renderer.domElement;
el.addEventListener('pointerdown', this.onPointerDown);
el.addEventListener('pointermove', this.onPointerMove);
el.addEventListener('pointerup', this.onPointerUp);
el.addEventListener('pointerleave', this.onPointerLeave);
this.controls = new OrbitControls(this.camera, el);
this.controls.enableDamping = true;
this.controls.dampingFactor = 0.07;
this.controls.rotateSpeed = 0.55;
this.controls.zoomSpeed = 0.9;
this.controls.panSpeed = 0.7;
this.controls.minDistance = 15;
this.controls.maxDistance = 4000;
this.controls.zoomToCursor = true;
this.controls.addEventListener('start', () => {
this.orbiting = true;
this.tween = null;
this.lastInteraction = this.time;
});
this.controls.addEventListener('end', () => {
this.orbiting = false;
this.lastInteraction = this.time;
});
this.resizeObserver = new ResizeObserver(() => this.resize());
this.resizeObserver.observe(container);
this.resize();
this.applyEnvironment();
this.raf = requestAnimationFrame(this.frame);
}
// ---------------------------------------------------------------- public API
setData(data: GraphData): void {
this.nodes = data.nodes;
this.idToIndex = new Map(this.nodes.map((n, i) => [n.id, i]));
const n = this.nodes.length;
const pairs: Array<[number, number]> = [];
for (const l of data.links) {
const s = this.idToIndex.get(l.source);
const t = this.idToIndex.get(l.target);
if (s === undefined || t === undefined || s === t) continue;
pairs.push([s, t]);
}
const m = pairs.length;
this.linkIdx = new Int32Array(m * 2);
this.neighbors = Array.from({ length: n }, () => []);
pairs.forEach(([s, t], k) => {
this.linkIdx[k * 2] = s;
this.linkIdx[k * 2 + 1] = t;
this.neighbors[s].push(t);
this.neighbors[t].push(s);
});
this.groupOf = this.nodes.map((nd) => nd.group ?? 'default');
this.radius = new Float32Array(n);
for (let i = 0; i < n; i++) {
const size = this.nodes[i].size;
this.radius[i] =
typeof size === 'number' && size > 0 ? size : Math.min(10, 2.6 + 1.6 * Math.sqrt(this.neighbors[i].length));
}
this.topNodes = new Set(
[...Array(n).keys()].sort((a, b) => this.neighbors[b].length - this.neighbors[a].length).slice(0, TOP_LABELS),
);
this.layout = new ForceLayout(n, pairs);
this.vpos = new Float32Array(this.layout.pos);
this.scaleCur = new Float32Array(n).fill(0.01);
this.scaleTarget = new Float32Array(n).fill(1);
this.emphCur = new Float32Array(n).fill(1);
this.emphTarget = new Float32Array(n).fill(1);
this.linkEmphCur = new Float32Array(m).fill(0);
this.linkEmphTarget = new Float32Array(m).fill(1);
this.focusMask = new Uint8Array(n);
this.particlePhase = new Float32Array(m * PARTICLES_PER_LINK).map(() => Math.random());
this.labelAlpha = new Float32Array(n);
this.labelShown = new Uint8Array(n);
this.labelTarget = new Float32Array(n);
this.labelPriority = new Float32Array(n);
this.labelX = new Float32Array(n);
this.labelY = new Float32Array(n);
this.labelZ = new Float32Array(n);
this.labelW = new Float32Array(n);
this.labelLayer.replaceChildren();
this.labelEls = this.nodes.map((nd) => {
const el = document.createElement('div');
el.className = 'kg-label';
el.textContent = nd.label ?? nd.id;
el.style.display = 'none';
this.labelLayer.appendChild(el);
return el;
});
this.hovered = -1;
this.selected = -1;
this.groupHighlight = null;
this.impulses = [];
this.clearRipples();
this.buildVisuals();
this.refreshFocus();
const est = 14 * Math.cbrt(Math.max(n, 1)) * 2.4 + 40;
this.tween = null;
this.controls.target.set(0, 0, 0);
this.camera.position.set(est * 0.4, est * 0.5, est * 3.2);
this.needsFit = true;
}
setTheme(theme: Theme): void {
if (theme.id === this.theme.id) return;
this.theme = theme;
this.applyEnvironment();
this.buildVisuals();
}
focusNode(id: string): void {
const idx = this.idToIndex.get(id);
if (idx === undefined) return;
this.needsFit = false;
this.pendingFocus = -1;
this.select(idx, true);
}
/** Focuses once the initial layout has cooled, instead of the default fit-to-view. */
focusNodeWhenSettled(id: string): void {
const idx = this.idToIndex.get(id);
if (idx !== undefined) this.pendingFocus = idx;
}
clearSelection(): void {
this.select(-1, false);
}
highlightGroup(group: string | null): void {
this.groupHighlight = group;
this.refreshFocus();
}
reheat(): void {
if (!this.layout) return;
const p = this.layout.pos;
for (let i = 0; i < this.nodes.length; i++) {
const s = 2.5 + Math.random() * 2;
this.layout.impulse(i, (Math.random() - 0.5) * s * 2, (Math.random() - 0.5) * s * 2, (Math.random() - 0.5) * s * 2);
this.layout.impulse(i, p[i * 3] * 0.02, p[i * 3 + 1] * 0.02, p[i * 3 + 2] * 0.02);
}
this.layout.reheat(0.5);
}
fitView(duration = 1200): void {
const n = this.nodes.length;
if (!n) return;
const c = new THREE.Vector3();
for (let i = 0; i < n; i++) c.x += this.vpos[i * 3], c.y += this.vpos[i * 3 + 1], c.z += this.vpos[i * 3 + 2];
c.divideScalar(n);
const dists: number[] = [];
for (let i = 0; i < n; i++) {
dists.push(this.v1.set(this.vpos[i * 3], this.vpos[i * 3 + 1], this.vpos[i * 3 + 2]).distanceTo(c));
}
dists.sort((a, b) => a - b);
const r = Math.max(30, dists[Math.floor((n - 1) * 0.98)] + 10);
const vFov = THREE.MathUtils.degToRad(this.camera.fov) / 2;
const hFov = Math.atan(Math.tan(vFov) * this.camera.aspect);
const dist = (r / Math.sin(Math.min(vFov, hFov))) * 1.02;
const dir = this.v2.copy(this.camera.position).sub(this.controls.target).normalize();
this.flyTo(c, c.clone().addScaledVector(dir, dist), duration);
if (this.grid) this.grid.position.y = c.y - r * 1.15;
}
dispose(): void {
cancelAnimationFrame(this.raf);
const el = this.renderer.domElement;
el.removeEventListener('pointerdown', this.onPointerDown);
el.removeEventListener('pointermove', this.onPointerMove);
el.removeEventListener('pointerup', this.onPointerUp);
el.removeEventListener('pointerleave', this.onPointerLeave);
this.resizeObserver.disconnect();
this.controls.dispose();
this.disposeObject(this.graphGroup);
this.disposeObject(this.envGroup);
this.rippleGeo.dispose();
this.renderer.dispose();
el.remove();
this.labelLayer.remove();
}
// ---------------------------------------------------------------- scene building
private applyEnvironment(): void {
const t = this.theme;
this.disposeObject(this.envGroup);
this.stars = null;
this.grid = null;
this.fogColor.set(t.fog.color);
this.scene.fog = new THREE.FogExp2(this.fogColor, t.fog.density);
this.camLight.intensity = t.lights.key;
this.envGroup.add(new THREE.HemisphereLight(t.lights.sky, t.lights.ground, t.lights.ambient * 2));
this.envGroup.add(new THREE.AmbientLight(0xffffff, t.lights.ambient));
if (t.stars.enabled) {
const count = 2200;
const pos = new Float32Array(count * 3);
for (let i = 0; i < count; i++) {
const r = 1800 + Math.random() * 2400;
const u = Math.random() * 2 - 1;
const th = Math.random() * Math.PI * 2;
const s = Math.sqrt(1 - u * u);
pos.set([r * s * Math.cos(th), r * u, r * s * Math.sin(th)], i * 3);
}
const geo = new THREE.BufferGeometry();
geo.setAttribute('position', new THREE.BufferAttribute(pos, 3));
const mat = new THREE.PointsMaterial({
color: t.stars.color, size: 1.6, sizeAttenuation: false, transparent: true,
opacity: 0.75, depthWrite: false, fog: false,
});
this.stars = new THREE.Points(geo, mat);
this.envGroup.add(this.stars);
}
if (t.grid.enabled) {
const grid = new THREE.GridHelper(4000, 80, t.grid.color, t.grid.color);
const mats = Array.isArray(grid.material) ? grid.material : [grid.material];
for (const m of mats) {
m.transparent = true;
m.opacity = t.grid.opacity;
m.depthWrite = false;
}
grid.position.y = -220;
this.grid = grid;
this.envGroup.add(grid);
}
}
private buildVisuals(): void {
this.disposeObject(this.graphGroup);
this.clearRipples();
this.nodeMesh = this.halo = this.lines = this.particles = this.selRing = null;
const t = this.theme;
const n = this.nodes.length;
const m = this.linkIdx.length / 2;
if (!n) return;
const groups = getGroups(this.nodes);
this.colors = this.nodes.map((nd) =>
nd.color ? new THREE.Color(nd.color) : colorForGroup(t, groups.indexOf(nd.group ?? 'default')),
);
const blending = t.blending === 'additive' ? THREE.AdditiveBlending : THREE.NormalBlending;
// Nodes
const geo =
t.node.shape === 'icosahedron'
? new THREE.IcosahedronGeometry(1, 0)
: t.node.shape === 'octahedron'
? new THREE.OctahedronGeometry(1, 0)
: new THREE.SphereGeometry(1, 32, 20);
const mat = new THREE.MeshStandardMaterial({
color: 0xffffff, roughness: t.node.roughness, metalness: t.node.metalness, flatShading: t.node.flatShading,
});
const emissive = t.node.emissive;
const orb = t.node.shape === 'sphere' ? t.node.orb : undefined;
mat.onBeforeCompile = (shader) => {
shader.fragmentShader = shader.fragmentShader.replace(
'#include <emissivemap_fragment>',
`#include <emissivemap_fragment>\n#ifdef USE_COLOR\n totalEmissiveRadiance += vColor.rgb * ${emissive.toFixed(3)};\n#endif`,
);
if (orb) {
shader.fragmentShader = shader.fragmentShader.replace(
'#include <opaque_fragment>',
/* glsl */ `#ifdef USE_COLOR
{
float ndv = saturate(dot(normalize(normal), normalize(vViewPosition)));
vec3 orb = vColor.rgb;
float peak = max(max(orb.r, orb.g), orb.b);
// Pale core, scaled by the node's own brightness so dimmed nodes stay dim.
vec3 coreCol = mix(orb, vec3(1.0), 0.7) * (0.3 + 0.9 * peak);
outgoingLight = mix(outgoingLight, max(outgoingLight, coreCol), ${orb.core.toFixed(3)} * pow(ndv, 2.5));
float rim = pow(1.0 - ndv, 3.0);
float edge = smoothstep(0.22, 0.04, ndv);
outgoingLight += (orb * 1.3 + vec3(0.3) * peak) * ${orb.rim.toFixed(3)} * (0.55 * rim + 0.45 * edge);
}
#endif
#include <opaque_fragment>`,
);
}
};
mat.customProgramCacheKey = () => `kg-node-${emissive}-${orb ? `${orb.core}-${orb.rim}` : 'flat'}`;
this.nodeMesh = new THREE.InstancedMesh(geo, mat, n);
this.nodeMesh.instanceMatrix.setUsage(THREE.DynamicDrawUsage);
for (let i = 0; i < n; i++) this.nodeMesh.setColorAt(i, this.colors[i]);
this.nodeMesh.frustumCulled = false;
this.graphGroup.add(this.nodeMesh);
// Links
const lgeo = new THREE.BufferGeometry();
lgeo.setAttribute('position', dynamicAttr(new Float32Array(m * 6), 3));
lgeo.setAttribute('aColor', dynamicAttr(new Float32Array(m * 6), 3));
lgeo.setAttribute('aAlpha', dynamicAttr(new Float32Array(m * 2), 1));
this.lines = new THREE.LineSegments(
lgeo,
new THREE.ShaderMaterial({
vertexShader: LINE_VERT, fragmentShader: LINE_FRAG, transparent: true, depthWrite: false, blending,
}),
);
this.lines.frustumCulled = false;
this.lines.renderOrder = 1;
this.graphGroup.add(this.lines);
if (t.halo.enabled) {
this.halo = this.makePoints(n, 2.4, blending, Math.min(0.8, 2 / t.halo.size));
this.halo.renderOrder = 2;
this.graphGroup.add(this.halo);
}
if (t.particles.enabled && m > 0) {
this.particles = this.makePoints(m * PARTICLES_PER_LINK * (SPARK_TRAIL + 1), 1, blending, 0, SPARK_FRAG, {
uCore: { value: t.blending === 'additive' ? 0.85 : 0.25 },
});
this.particles.renderOrder = 3;
this.graphGroup.add(this.particles);
}
// Selection ring: a thin full ring plus a rotating arc
const ringMat = new THREE.MeshBasicMaterial({
color: t.ui.accent, transparent: true, opacity: 0.9, depthWrite: false, side: THREE.DoubleSide,
});
const ring = new THREE.Group();
ring.add(new THREE.Mesh(new THREE.RingGeometry(1.45, 1.52, 96), ringMat));
const arc = new THREE.Mesh(new THREE.RingGeometry(1.68, 1.8, 96, 1, 0, Math.PI * 1.25), ringMat);
arc.name = 'arc';
ring.add(arc);
ring.visible = false;
ring.renderOrder = 4;
this.selRing = ring;
this.graphGroup.add(ring);
}
private makePoints(
count: number,
softness: number,
blending: THREE.Blending,
inner = 0,
fragmentShader = POINT_FRAG,
extraUniforms: Record<string, THREE.IUniform> = {},
): THREE.Points {
const geo = new THREE.BufferGeometry();
geo.setAttribute('position', dynamicAttr(new Float32Array(count * 3), 3));
geo.setAttribute('aColor', dynamicAttr(new Float32Array(count * 3), 3));
geo.setAttribute('aSize', dynamicAttr(new Float32Array(count), 1));
geo.setAttribute('aAlpha', dynamicAttr(new Float32Array(count), 1));
const mat = new THREE.ShaderMaterial({
uniforms: {
uScale: { value: this.pointScale },
uSoftness: { value: softness },
uInner: { value: inner },
...extraUniforms,
},
vertexShader: POINT_VERT, fragmentShader, transparent: true, depthWrite: false, blending,
});
const pts = new THREE.Points(geo, mat);
pts.frustumCulled = false;
return pts;
}
private disposeObject(root: THREE.Object3D): void {
for (const child of [...root.children]) {
child.traverse((o) => {
const mesh = o as THREE.Mesh;
mesh.geometry?.dispose();
const mat = mesh.material as THREE.Material | THREE.Material[] | undefined;
if (Array.isArray(mat)) mat.forEach((x) => x.dispose());
else mat?.dispose();
});
root.remove(child);
}
}
private clearRipples(): void {
for (const r of this.ripples) {
(r.mesh.material as THREE.Material).dispose();
r.mesh.removeFromParent();
}
this.ripples = [];
}
private resize(): void {
this.width = Math.max(1, this.container.clientWidth);
this.height = Math.max(1, this.container.clientHeight);
this.renderer.setSize(this.width, this.height);
this.camera.aspect = this.width / this.height;
this.camera.updateProjectionMatrix();
this.pointScale =
(this.height * 0.5 * this.renderer.getPixelRatio()) / Math.tan(THREE.MathUtils.degToRad(this.camera.fov) / 2);
for (const p of [this.halo, this.particles]) {
if (p) (p.material as THREE.ShaderMaterial).uniforms.uScale.value = this.pointScale;
}
}
// ---------------------------------------------------------------- selection & focus
private select(idx: number, fly: boolean): void {
this.selected = idx;
this.refreshFocus();
if (idx >= 0) {
this.spawnRipple(idx);
this.shockwave(idx);
this.scaleCur[idx] *= 0.55;
if (fly) this.flyToNode(idx);
}
this.cb.onSelect?.(idx >= 0 ? this.nodes[idx] : null);
}
private setHover(idx: number): void {
if (idx === this.hovered) return;
this.hovered = idx;
this.renderer.domElement.style.cursor = idx >= 0 ? 'pointer' : '';
this.refreshFocus();
this.cb.onHover?.(idx >= 0 ? this.nodes[idx] : null);
}
private refreshFocus(): void {
const n = this.nodes.length;
const focus = this.hovered >= 0 ? this.hovered : this.selected;
const mask = this.focusMask;
mask.fill(0);
if (focus >= 0) {
mask[focus] = 2;
for (const j of this.neighbors[focus]) mask[j] = Math.max(mask[j], 1);
} else if (this.groupHighlight !== null) {
for (let i = 0; i < n; i++) if (this.groupOf[i] === this.groupHighlight) mask[i] = 1;
}
const active = focus >= 0 || this.groupHighlight !== null;
for (let i = 0; i < n; i++) {
this.emphTarget[i] = !active || mask[i] ? 1 : NODE_DIM;
let s = active ? (mask[i] ? 1.12 : 0.88) : 1;
if (i === this.selected) {
s = 1.35;
this.emphTarget[i] = Math.max(this.emphTarget[i], 0.7);
}
if (i === this.hovered) s = 1.55;
this.scaleTarget[i] = s;
}
const m = this.linkIdx.length / 2;
for (let k = 0; k < m; k++) {
const s = this.linkIdx[k * 2];
const t = this.linkIdx[k * 2 + 1];
if (focus >= 0) this.linkEmphTarget[k] = s === focus || t === focus ? LINK_FOCUS : LINK_DIM;
else if (this.groupHighlight !== null) this.linkEmphTarget[k] = mask[s] && mask[t] ? LINK_FOCUS * 0.7 : LINK_DIM;
else this.linkEmphTarget[k] = 1;
}
}
private flyToNode(idx: number): void {
const p = new THREE.Vector3(this.vpos[idx * 3], this.vpos[idx * 3 + 1], this.vpos[idx * 3 + 2]);
let reach = 0;
for (const j of this.neighbors[idx]) {
reach = Math.max(reach, this.v1.set(this.vpos[j * 3], this.vpos[j * 3 + 1], this.vpos[j * 3 + 2]).distanceTo(p));
}
const dist = THREE.MathUtils.clamp(reach * 2.6, 70, 700);
const dir = this.v2.copy(this.camera.position).sub(this.controls.target).normalize();
this.flyTo(p, p.clone().addScaledVector(dir, dist), 1300);
}
private flyTo(target: THREE.Vector3, position: THREE.Vector3, duration: number): void {
this.tween = {
fromPos: this.camera.position.clone(),
fromTarget: this.controls.target.clone(),
toPos: position.clone(),
toTarget: target.clone(),
start: this.time,
duration: duration / 1000,
};
}
private spawnRipple(idx: number): void {
const t = this.theme;
const mat = new THREE.MeshBasicMaterial({
color: this.colors[idx], transparent: true, opacity: 0.9, depthWrite: false, side: THREE.DoubleSide,
blending: t.blending === 'additive' ? THREE.AdditiveBlending : THREE.NormalBlending,
});
const mesh = new THREE.Mesh(this.rippleGeo, mat);
mesh.renderOrder = 5;
this.graphGroup.add(mesh);
this.ripples.push({ mesh, life: 0, idx });
}
/** Pushes nodes outward in a wave that travels hop-by-hop through the graph. */
private shockwave(origin: number): void {
if (this.reducedMotion) return;
const hops = new Int32Array(this.nodes.length).fill(-1);
hops[origin] = 0;
const queue = [origin];
const ox = this.vpos[origin * 3], oy = this.vpos[origin * 3 + 1], oz = this.vpos[origin * 3 + 2];
for (let q = 0; q < queue.length; q++) {
const i = queue[q];
if (hops[i] >= 5) continue;
for (const j of this.neighbors[i]) {
if (hops[j] >= 0) continue;
hops[j] = hops[i] + 1;
queue.push(j);
let dx = this.vpos[j * 3] - ox, dy = this.vpos[j * 3 + 1] - oy, dz = this.vpos[j * 3 + 2] - oz;
const len = Math.hypot(dx, dy, dz) || 1;
const mag = 2.6 / hops[j];
dx = (dx / len) * mag;
dy = (dy / len) * mag;
dz = (dz / len) * mag;
this.impulses.push({ at: this.time + hops[j] * 0.075, idx: j, x: dx, y: dy, z: dz });
}
}
}
// ---------------------------------------------------------------- pointer
private updatePointer(e: PointerEvent): void {
const rect = this.renderer.domElement.getBoundingClientRect();
this.pointer.x = e.clientX - rect.left;
this.pointer.y = e.clientY - rect.top;
this.pointer.ndc.set((this.pointer.x / rect.width) * 2 - 1, -(this.pointer.y / rect.height) * 2 + 1);
}
private onPointerDown = (e: PointerEvent): void => {
this.updatePointer(e);
this.lastInteraction = this.time;
const p = this.pointer;
p.down = true;
p.moved = false;
p.downX = e.clientX;
p.downY = e.clientY;
if (e.button !== 0) return;
const idx = this.pick();
if (idx < 0) return;
this.dragIndex = idx;
this.controls.enabled = false;
this.camera.getWorldDirection(this.v1);
this.dragPlane.setFromNormalAndCoplanarPoint(
this.v1,
this.v2.set(this.vpos[idx * 3], this.vpos[idx * 3 + 1], this.vpos[idx * 3 + 2]),
);
this.renderer.domElement.setPointerCapture(e.pointerId);
};
private onPointerMove = (e: PointerEvent): void => {
const p = this.pointer;
const px = p.x, py = p.y;
this.updatePointer(e);
if (p.inside) {
p.vx += p.x - px;
p.vy += p.y - py;
}
p.inside = true;
this.lastInteraction = this.time;
if (p.down && Math.hypot(e.clientX - p.downX, e.clientY - p.downY) > 4) p.moved = true;
if (this.dragIndex >= 0 && p.moved && this.layout) {
if (!this.dragging) {
this.dragging = true;
this.layout.alphaTarget = 0.12;
this.layout.reheat(0.15);
this.renderer.domElement.style.cursor = 'grabbing';
}
this.raycaster.setFromCamera(p.ndc, this.camera);
if (this.raycaster.ray.intersectPlane(this.dragPlane, this.v1)) {
this.layout.pin(this.dragIndex, this.v1.x, this.v1.y, this.v1.z);
}
}
};
private onPointerUp = (e: PointerEvent): void => {
const p = this.pointer;
if (!p.down) return;
p.down = false;
if (this.dragIndex >= 0) {
const idx = this.dragIndex;
this.dragIndex = -1;
this.controls.enabled = true;
if (this.renderer.domElement.hasPointerCapture(e.pointerId)) {
this.renderer.domElement.releasePointerCapture(e.pointerId);
}
if (this.dragging) {
this.dragging = false;
this.layout?.unpin();
if (this.layout) this.layout.alphaTarget = 0;
this.renderer.domElement.style.cursor = this.hovered >= 0 ? 'pointer' : '';
} else {
this.select(idx, true);
}
} else if (!p.moved && e.button === 0 && this.selected >= 0) {
this.select(-1, false);
}
};
private onPointerLeave = (): void => {
if (this.dragIndex >= 0) return;
this.pointer.inside = false;
this.setHover(-1);
};
/** Screen-space picking: forgiving for small nodes and robust to moving geometry. */
private pick(): number {
const n = this.nodes.length;
const f = (this.height * 0.5) / Math.tan(THREE.MathUtils.degToRad(this.camera.fov) / 2);
const inv = this.camera.matrixWorldInverse;
let best = -1;
let bestDepth = Infinity;
for (let i = 0; i < n; i++) {
const v = this.v1.set(this.vpos[i * 3], this.vpos[i * 3 + 1], this.vpos[i * 3 + 2]).applyMatrix4(inv);
const depth = -v.z;
if (depth < 1) continue;
const sx = (v.x * f) / depth + this.width / 2;
const sy = this.height / 2 - (v.y * f) / depth;
const pr = (this.radius[i] * this.scaleCur[i] * f) / depth;
const d = Math.hypot(sx - this.pointer.x, sy - this.pointer.y);
if (d <= Math.max(pr, 6) + 6 && depth < bestDepth) {
best = i;
bestDepth = depth;
}
}
return best;
}
/** Cursor acts as a soft force field: nodes part around it and get dragged along its wake. */
private applyCursorField(): void {
const p = this.pointer;
const mvx = p.vx, mvy = p.vy;
p.vx *= 0.75;
p.vy *= 0.75;
if (!p.inside || this.orbiting || this.dragIndex >= 0 || !this.layout || this.reducedMotion) return;
this.raycaster.setFromCamera(p.ndc, this.camera);
const { origin: o, direction: d } = this.raycaster.ray;
const pxPerDepth = (2 * Math.tan(THREE.MathUtils.degToRad(this.camera.fov) / 2)) / this.height;
this.right.setFromMatrixColumn(this.camera.matrixWorld, 0);
this.up.setFromMatrixColumn(this.camera.matrixWorld, 1);
const wx = this.right.x * mvx - this.up.x * mvy;
const wy = this.right.y * mvx - this.up.y * mvy;
const wz = this.right.z * mvx - this.up.z * mvy;
// While a node is under the pointer, keep everything still so it is easy to click or drag.
if (this.hovered >= 0) return;
for (let i = 0; i < this.nodes.length; i++) {
const px = this.vpos[i * 3], py = this.vpos[i * 3 + 1], pz = this.vpos[i * 3 + 2];
const t = (px - o.x) * d.x + (py - o.y) * d.y + (pz - o.z) * d.z;
if (t <= 0) continue;
const ox = px - (o.x + d.x * t), oy = py - (o.y + d.y * t), oz = pz - (o.z + d.z * t);
const dist = Math.hypot(ox, oy, oz);
const wpp = t * pxPerDepth;
const R = CURSOR_RADIUS_PX * wpp;
const calm = CURSOR_CALM_PX * wpp + this.radius[i] * this.scaleCur[i];
if (dist >= R || dist <= calm) continue;
// Force ramps up from the calm edge and fades out toward R: a soft ring, not a hard shove.
const u = (dist - calm) / (R - calm);
const f = Math.min(1, u / 0.3) * (1 - u) ** 2;
const push = (0.3 * wpp * f) / dist;
const wake = 0.035 * wpp * f;
this.layout.impulse(i, ox * push + wx * wake, oy * push + wy * wake, oz * push + wz * wake);
}
}
// ---------------------------------------------------------------- frame loop
private frame = (ts: number): void => {
this.raf = requestAnimationFrame(this.frame);
const dt = Math.min(Math.max((ts - this.lastTs) / 1000, 0), 0.05);
this.lastTs = ts;
if (!this.manualClock) this.tick(dt);
};
/** Advances the animation by exact 1/30 s frames; used with `manualClock` for frame-perfect recordings. */
advance(frames = 1): void {
for (let i = 0; i < frames; i++) this.tick(1 / 30);
}
private tick(dt: number): void {
this.time += dt;
if (this.layout && this.nodes.length) this.simulate(dt);
this.updateCamera(dt);
this.updateVisuals(dt);
this.renderer.render(this.scene, this.camera);
this.updateLabels(dt);
}
private simulate(dt: number): void {
const layout = this.layout!;
if (this.pointer.inside && !this.orbiting && this.dragIndex < 0) this.setHover(this.pick());
this.applyCursorField();
if (this.impulses.length) {
const due = this.impulses.filter((im) => im.at <= this.time);
if (due.length) {
for (const im of due) layout.impulse(im.idx, im.x, im.y, im.z);
this.impulses = this.impulses.filter((im) => im.at > this.time);
}
}
this.stepAcc += dt;
let steps = 0;
while (this.stepAcc >= STEP && steps < 3) {
layout.step();
this.stepAcc -= STEP;
steps++;
}
if (steps === 3) this.stepAcc = 0;
// Gentle idle drift so the graph always feels alive.
const p = layout.pos;
const tm = this.time;
const amp = this.reducedMotion ? 0 : 0.7;
for (let i = 0; i < this.nodes.length; i++) {
const o = i * 3;
this.vpos[o] = p[o] + amp * Math.sin(tm * 0.6 + i * 1.7);
this.vpos[o + 1] = p[o + 1] + amp * Math.cos(tm * 0.5 + i * 2.3);
this.vpos[o + 2] = p[o + 2] + amp * Math.sin(tm * 0.45 + i * 0.9);
}
if (this.needsFit && layout.alpha < 0.05) {
this.needsFit = false;
if (this.pendingFocus >= 0) this.select(this.pendingFocus, true);
else this.fitView(1800);
this.pendingFocus = -1;
}
}
private updateCamera(dt: number): void {
const tw = this.tween;
if (tw) {
const k = Math.min(1, (this.time - tw.start) / tw.duration);
const e = easeInOutCubic(k);
this.camera.position.lerpVectors(tw.fromPos, tw.toPos, e);
this.controls.target.lerpVectors(tw.fromTarget, tw.toTarget, e);
if (k >= 1) this.tween = null;
}
const idle = this.time - this.lastInteraction > 5 && this.selected < 0 && !tw && !this.reducedMotion;
this.controls.autoRotate = true;
this.controls.autoRotateSpeed = damp(this.controls.autoRotateSpeed, idle ? 0.35 : 0, 2, dt);
this.controls.update(dt);
// Keep depth cueing consistent regardless of graph size / zoom level.
const fog = this.scene.fog as THREE.FogExp2 | null;
if (fog) {
const camDist = this.camera.position.distanceTo(this.controls.target);
fog.density = (this.theme.fog.density * 350) / Math.max(350, camDist);
}
if (this.stars) {
this.starTilt.x = damp(this.starTilt.x, this.pointer.ndc.y * 0.04, 2, dt);
this.starTilt.y = damp(this.starTilt.y, -this.pointer.ndc.x * 0.04, 2, dt);
this.stars.rotation.set(this.starTilt.x, this.time * 0.004 + this.starTilt.y, 0);
}
}
private updateVisuals(dt: number): void {
const n = this.nodes.length;
if (!n || !this.nodeMesh) return;
const t = this.theme;
const k = 1 - Math.exp(-dt * 9);
const rotate = t.node.shape !== 'sphere';
const haloGeo = this.halo?.geometry;
const hPos = haloGeo?.getAttribute('position').array as Float32Array | undefined;
const hCol = haloGeo?.getAttribute('aColor').array as Float32Array | undefined;
const hSize = haloGeo?.getAttribute('aSize').array as Float32Array | undefined;
const hAlpha = haloGeo?.getAttribute('aAlpha').array as Float32Array | undefined;
for (let i = 0; i < n; i++) {
this.scaleCur[i] += (this.scaleTarget[i] - this.scaleCur[i]) * k;
this.emphCur[i] += (this.emphTarget[i] - this.emphCur[i]) * k;
let r = this.radius[i] * this.scaleCur[i];
if (i === this.selected) r *= 1 + 0.05 * Math.sin(this.time * 4);
const o = i * 3;
const x = this.vpos[o], y = this.vpos[o + 1], z = this.vpos[o + 2];