-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathstage.ts
More file actions
288 lines (250 loc) · 7.29 KB
/
stage.ts
File metadata and controls
288 lines (250 loc) · 7.29 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
declare const graph: HTMLDivElement
import {applyLetterbox} from './letterbox'
import ForceGraph3D, { ForceGraph3DInstance } from '3d-force-graph'
import * as THREE from 'three'
import SpriteText from 'three-spritetext'
import {purple, yellow, red, silver, blue, green, indigo, black, RGBA} from './colors'
import { UnrealBloomPass } from 'three/examples/jsm/postprocessing/UnrealBloomPass'
import { sleep } from './time';
type ForceGraph3D = ForceGraph3DInstance & {
isAutoZooming?: number,
highlighted?: Set<string>
highlight?(id: string): ForceGraph3D
lowlight?(id: string): ForceGraph3D
clearHighlights?(): ForceGraph3D
recenter?(): ForceGraph3D
updateHighlight?(): ForceGraph3D
setTextSizeForDistance?(distance?: number, frames?: number): ForceGraph3D
}
export const Graph: ForceGraph3D = ForceGraph3D({ controlType: 'orbit' })(graph)
Graph.renderer().setPixelRatio(window.devicePixelRatio)
const graphColors = {
agitprop: red,
obscura: blue,
unified: purple,
}
type SpriteTextExt = SpriteText & {
baseColor?: RGBA
baseTextHeight?: number
}
function textObject(node: any) {
const graph = node.graph || 'default'
const palette = graphColors[graph] || yellow
// use a sphere as a drag handle
const obj = new THREE.Mesh(
new THREE.SphereGeometry(10),
new THREE.MeshBasicMaterial({ depthWrite: false, transparent: true, opacity: 0 })
);
// add text sprite as child
const sprite: SpriteTextExt = new SpriteText(node.label);
sprite.fontFace = 'Source Sans Pro';
sprite.fontWeight = '600'
sprite.padding = 1
sprite.material.depthWrite = false
sprite.material.blending = THREE.AdditiveBlending
let color
if (node.isRoot) {
color = palette.lightest
sprite.baseTextHeight = sprite.textHeight = 12
} else if (node.isField) {
color = node.isScalar ? palette.dark : palette.base
sprite.baseTextHeight = sprite.textHeight = 8
sprite.fontWeight = '700'
} else {
color = palette.light
sprite.baseTextHeight = sprite.textHeight = 10
}
sprite.color = color.a(1).toString()
sprite.baseColor = color
obj.add(sprite)
node.three = obj
node.sprite = sprite
return obj
}
Graph
.forceEngine('d3')
.numDimensions(3)
.nodeThreeObject(textObject)
.linkWidth((link: any) =>
Graph.highlighted?.has(link.id)
? 5
: 0.5
)
.linkColor(blue.light.toString())
.showNavInfo(false)
.linkDirectionalArrowLength(6)
.linkDirectionalArrowRelPos(0.5)
.linkDirectionalArrowColor('#ff00ff')
.linkDirectionalParticleColor(blue.lightest.three as any)
.linkDirectionalParticles((link: any) =>
Graph.highlighted && Graph.highlighted.has(link.id)
? 10
: 0
)
.linkDirectionalParticleWidth(1.5)
.linkDirectionalParticleSpeed(0.005)
.cameraPosition({ x: 0, y: 0, z: 500 })
.enableNavigationControls(true)
.showNavInfo(false)
Graph.updateHighlight = function() {
this.linkDirectionalParticles(this.linkDirectionalParticles())
const {nodes} = this.graphData()
if (!this.highlighted) {
for (const {sprite} of nodes) {
if (!sprite) continue
sprite.color = sprite.baseColor.a(1).toString()
}
return
}
for (const {id, sprite} of nodes) {
if (!sprite) continue
sprite.color = this.highlighted.has(id)
? sprite.baseColor.a(1).toString()
: sprite.baseColor.a(0.2).toString()
}
return this
}
Graph.highlight = function(id: string) {
const hls = this.highlighted ? this.highlighted : (this.highlighted = new Set)
hls.add(id)
this.updateHighlight()
return this
}
Graph.lowlight = function(id: string) {
if (!this.highlighted) return
this.highlighted.delete(id)
if (!this.highlighted.size) delete this.highlighted
this.updateHighlight()
return this
}
Graph.clearHighlights = function() {
delete this.highlighted
this.updateHighlight()
return this
}
function addBloom(graph: ForceGraph3D) {
const bloomPass = new (UnrealBloomPass as any)()
bloomPass.strength = 1;
bloomPass.radius = 1;
bloomPass.threshold = 0.1;
graph.postProcessingComposer().addPass(bloomPass);
return graph
}
addBloom(Graph)
const SLOW = 1
const Y = new THREE.Vector3(0, 1, 0)
export function orbit(graph = Graph, point = {x: 0, y: 0, z: 0}) {
return t => {
if (graph.isAutoZooming) return
let angle = t * SLOW
let distance = graph.camera().position.length()
graph.cameraPosition({
x: (point.x || 0) + distance * Math.sin(angle),
y: point.y || 0,
z: (point.z || 0) + distance * Math.cos(angle),
})
}
}
function spinCam(graph = Graph, speed = SLOW) {
return t => {
let angle = t * speed
graph.cameraPosition({
x: 0, y: 0, z: 100,
}, {
x: Math.sin(angle),
y: 0,
z: Math.cos(angle) + 100,
})
}
}
Graph.onNodeClick(console.log)
Graph.d3Force('charge')(-10)
;(Graph.d3Force('link') as any).distance((link: any) => link.rel === 'origin' ? 300 : 30)
// Graph.d3Force('center')(5)
applyLetterbox([16, 9], box => {
Graph.width(box.width)
Graph.height(box.height)
const {splode} = window as any
if (splode) {
splode.width(box.width)
splode.height(box.height)
}
});
const ztf = Graph.zoomToFit
Graph.isAutoZooming = 0
Graph.zoomToFit = function(...args: Parameters<ForceGraph3DInstance["zoomToFit"]>) {
++this.isAutoZooming
ztf.apply(this, args)
setTimeout(() => {
this.setTextSizeForDistance()
--this.isAutoZooming
}, args[0])
return this
}
Graph.setTextSizeForDistance = function(distance=this.camera().position.length(), frames=10) {
const items: any[] = []
const {nodes} = this.graphData()
for (const {sprite} of nodes) {
if (!sprite) continue
const height = Math.max(sprite.baseTextHeight, sprite.baseTextHeight * distance / 800)
const delta = height - sprite.textHeight
if (Math.abs(delta) > 0.001) items.push({sprite, delta: delta / frames})
}
let count = 0
resize()
return this
function resize() {
if (count++ < frames) requestAnimationFrame(resize)
for (const {sprite, delta} of items) {
sprite.textHeight += delta
}
}
}
async function doRecenter(graph: ForceGraph3D, ms = 600) {
graph.zoomToFit(ms)
await sleep(1200)
const dist = graph.camera().position.length()
++graph.isAutoZooming
try {
graph.cameraPosition({ x: 0, y: 0, z: dist }, { x: 0, y: 0, z: 0 }, 1000)
await sleep(1000)
graph.zoomToFit(ms)
await sleep(1000)
} finally {
--graph.isAutoZooming
}
}
Graph.recenter = function(ms = 600) {
// setTimeout(() => {
// this.zoomToFit(ms)
// setTimeout(() => {
// const dist = this.camera().position.length()
// ++this.isAutoZooming
// this.cameraPosition({ x: 0, y: 0, z: dist }, { x: 0, y: 0, z: 0 }, 1000)
// setTimeout(() => {
// --this.isAutoZooming
// }, 1000)
// }, ms)
// }, 1500)
doRecenter(this, ms)
return this
}
import blocks from './blocks.json'
function embiggen(graphData: { nodes: any[], links: any[] }) {
let tag = 'x'
const nodes = graphData.nodes.map(n => ({
...n, id: tag + n.id
}))
const links = graphData.links.map(l => ({
...l,
source: tag + l.source,
target: tag + l.target,
}))
return {
nodes: graphData.nodes.concat(nodes),
links: graphData.links.concat(links)
}
}
Object.assign(window as any, {
ForceGraph3D, THREE, orbit, Graph, blocks: embiggen(embiggen(blocks)), addBloom, spinCam
})