@cosmos.gl/graph 2.6.2-rc.0 → 2.7.0-beta.0
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.
- package/.eslintrc +147 -0
- package/.github/SECURITY.md +13 -0
- package/.github/dco.yml +4 -0
- package/.github/workflows/github_pages.yml +54 -0
- package/.storybook/main.ts +26 -0
- package/.storybook/manager-head.html +1 -0
- package/.storybook/manager.ts +14 -0
- package/.storybook/preview.ts +29 -0
- package/.storybook/style.css +3 -0
- package/CHARTER.md +69 -0
- package/CODE_OF_CONDUCT.md +178 -0
- package/CONTRIBUTING.md +22 -0
- package/GOVERNANCE.md +21 -0
- package/cosmos-2-0-migration-notes.md +98 -0
- package/cosmos_awesome.md +96 -0
- package/dist/config.d.ts +5 -9
- package/dist/graph/utils/error-message.d.ts +1 -1
- package/dist/helper.d.ts +39 -2
- package/dist/index-FUIgayhu.js +19827 -0
- package/dist/index-FUIgayhu.js.map +1 -0
- package/dist/index.d.ts +17 -64
- package/dist/index.js +14 -14654
- package/dist/index.js.map +1 -1
- package/dist/index.min.js +1062 -475
- package/dist/index.min.js.map +1 -1
- package/dist/modules/Clusters/index.d.ts +11 -3
- package/dist/modules/ForceCenter/index.d.ts +10 -3
- package/dist/modules/ForceGravity/index.d.ts +5 -1
- package/dist/modules/ForceLink/index.d.ts +8 -5
- package/dist/modules/ForceManyBody/index.d.ts +16 -7
- package/dist/modules/ForceMouse/index.d.ts +5 -1
- package/dist/modules/GraphData/index.d.ts +0 -1
- package/dist/modules/Lines/index.d.ts +11 -5
- package/dist/modules/Points/index.d.ts +31 -13
- package/dist/modules/Store/index.d.ts +93 -0
- package/dist/modules/core-module.d.ts +3 -3
- package/dist/stories/beginners/basic-set-up/data-gen.d.ts +4 -0
- package/dist/stories/beginners/basic-set-up/index.d.ts +6 -0
- package/dist/stories/beginners/link-hovering/data-generator.d.ts +19 -0
- package/dist/stories/beginners/link-hovering/index.d.ts +6 -0
- package/dist/stories/beginners/point-labels/data.d.ts +13 -0
- package/dist/stories/beginners/point-labels/index.d.ts +10 -0
- package/dist/stories/beginners/point-labels/labels.d.ts +8 -0
- package/dist/stories/beginners/quick-start.d.ts +6 -0
- package/dist/stories/beginners/remove-points/config.d.ts +2 -0
- package/dist/stories/beginners/remove-points/data-gen.d.ts +4 -0
- package/dist/stories/beginners/remove-points/index.d.ts +6 -0
- package/dist/stories/beginners.stories.d.ts +10 -0
- package/dist/stories/clusters/polygon-selection/index.d.ts +6 -0
- package/dist/stories/clusters/polygon-selection/polygon.d.ts +20 -0
- package/dist/stories/clusters/radial.d.ts +6 -0
- package/dist/stories/clusters/with-labels.d.ts +6 -0
- package/dist/stories/clusters/worm.d.ts +6 -0
- package/dist/stories/clusters.stories.d.ts +9 -0
- package/dist/stories/create-cluster-labels.d.ts +4 -0
- package/dist/stories/create-cosmos.d.ts +17 -0
- package/dist/stories/create-story.d.ts +16 -0
- package/dist/stories/experiments/full-mesh.d.ts +6 -0
- package/dist/stories/experiments/mesh-with-holes.d.ts +6 -0
- package/dist/stories/experiments.stories.d.ts +7 -0
- package/dist/stories/generate-mesh-data.d.ts +12 -0
- package/dist/stories/geospatial/moscow-metro-stations/index.d.ts +16 -0
- package/dist/stories/geospatial/moscow-metro-stations/moscow-metro-coords.d.ts +1 -0
- package/dist/stories/geospatial/moscow-metro-stations/point-colors.d.ts +1 -0
- package/dist/stories/geospatial.stories.d.ts +6 -0
- package/dist/stories/shapes/all-shapes/index.d.ts +6 -0
- package/dist/stories/shapes/image-example/index.d.ts +6 -0
- package/dist/stories/shapes.stories.d.ts +7 -0
- package/dist/stories/test-luma-migration.d.ts +6 -0
- package/dist/stories/test.stories.d.ts +6 -0
- package/dist/webgl-device-B9ewDj5L.js +3923 -0
- package/dist/webgl-device-B9ewDj5L.js.map +1 -0
- package/logo.svg +3 -0
- package/package.json +5 -7
- package/rollup.config.js +70 -0
- package/src/config.ts +728 -0
- package/src/declaration.d.ts +12 -0
- package/src/graph/utils/error-message.ts +23 -0
- package/src/helper.ts +113 -0
- package/src/index.ts +1769 -0
- package/src/modules/Clusters/calculate-centermass.frag +12 -0
- package/src/modules/Clusters/calculate-centermass.vert +38 -0
- package/src/modules/Clusters/force-cluster.frag +55 -0
- package/src/modules/Clusters/index.ts +578 -0
- package/src/modules/Drag/index.ts +33 -0
- package/src/modules/FPSMonitor/css.ts +53 -0
- package/src/modules/FPSMonitor/index.ts +28 -0
- package/src/modules/ForceCenter/calculate-centermass.frag +9 -0
- package/src/modules/ForceCenter/calculate-centermass.vert +26 -0
- package/src/modules/ForceCenter/force-center.frag +37 -0
- package/src/modules/ForceCenter/index.ts +284 -0
- package/src/modules/ForceGravity/force-gravity.frag +40 -0
- package/src/modules/ForceGravity/index.ts +107 -0
- package/src/modules/ForceLink/force-spring.ts +89 -0
- package/src/modules/ForceLink/index.ts +293 -0
- package/src/modules/ForceManyBody/calculate-level.frag +9 -0
- package/src/modules/ForceManyBody/calculate-level.vert +37 -0
- package/src/modules/ForceManyBody/force-centermass.frag +61 -0
- package/src/modules/ForceManyBody/force-level.frag +138 -0
- package/src/modules/ForceManyBody/index.ts +525 -0
- package/src/modules/ForceManyBody/quadtree-frag-shader.ts +89 -0
- package/src/modules/ForceManyBodyQuadtree/calculate-level.frag +9 -0
- package/src/modules/ForceManyBodyQuadtree/calculate-level.vert +25 -0
- package/src/modules/ForceManyBodyQuadtree/index.ts +157 -0
- package/src/modules/ForceManyBodyQuadtree/quadtree-frag-shader.ts +93 -0
- package/src/modules/ForceMouse/force-mouse.frag +35 -0
- package/src/modules/ForceMouse/index.ts +102 -0
- package/src/modules/GraphData/index.ts +383 -0
- package/src/modules/Lines/draw-curve-line.frag +59 -0
- package/src/modules/Lines/draw-curve-line.vert +248 -0
- package/src/modules/Lines/geometry.ts +18 -0
- package/src/modules/Lines/hovered-line-index.frag +43 -0
- package/src/modules/Lines/hovered-line-index.vert +13 -0
- package/src/modules/Lines/index.ts +661 -0
- package/src/modules/Points/atlas-utils.ts +137 -0
- package/src/modules/Points/drag-point.frag +34 -0
- package/src/modules/Points/draw-highlighted.frag +44 -0
- package/src/modules/Points/draw-highlighted.vert +145 -0
- package/src/modules/Points/draw-points.frag +259 -0
- package/src/modules/Points/draw-points.vert +203 -0
- package/src/modules/Points/fill-sampled-points.frag +12 -0
- package/src/modules/Points/fill-sampled-points.vert +51 -0
- package/src/modules/Points/find-hovered-point.frag +15 -0
- package/src/modules/Points/find-hovered-point.vert +90 -0
- package/src/modules/Points/find-points-on-area-selection.frag +88 -0
- package/src/modules/Points/find-points-on-polygon-selection.frag +89 -0
- package/src/modules/Points/index.ts +2292 -0
- package/src/modules/Points/track-positions.frag +30 -0
- package/src/modules/Points/update-position.frag +39 -0
- package/src/modules/Shared/buffer.ts +39 -0
- package/src/modules/Shared/clear.frag +10 -0
- package/src/modules/Shared/quad.vert +13 -0
- package/src/modules/Store/index.ts +283 -0
- package/src/modules/Zoom/index.ts +148 -0
- package/src/modules/core-module.ts +28 -0
- package/src/stories/1. welcome.mdx +75 -0
- package/src/stories/2. configuration.mdx +111 -0
- package/src/stories/3. api-reference.mdx +591 -0
- package/src/stories/beginners/basic-set-up/data-gen.ts +33 -0
- package/src/stories/beginners/basic-set-up/index.ts +167 -0
- package/src/stories/beginners/basic-set-up/style.css +35 -0
- package/src/stories/beginners/link-hovering/data-generator.ts +198 -0
- package/src/stories/beginners/link-hovering/index.ts +65 -0
- package/src/stories/beginners/link-hovering/style.css +73 -0
- package/src/stories/beginners/point-labels/data.ts +73 -0
- package/src/stories/beginners/point-labels/index.ts +69 -0
- package/src/stories/beginners/point-labels/labels.ts +46 -0
- package/src/stories/beginners/point-labels/style.css +16 -0
- package/src/stories/beginners/quick-start.ts +54 -0
- package/src/stories/beginners/remove-points/config.ts +25 -0
- package/src/stories/beginners/remove-points/data-gen.ts +30 -0
- package/src/stories/beginners/remove-points/index.ts +96 -0
- package/src/stories/beginners/remove-points/style.css +31 -0
- package/src/stories/beginners.stories.ts +130 -0
- package/src/stories/clusters/polygon-selection/index.ts +52 -0
- package/src/stories/clusters/polygon-selection/polygon.ts +143 -0
- package/src/stories/clusters/polygon-selection/style.css +8 -0
- package/src/stories/clusters/radial.ts +24 -0
- package/src/stories/clusters/with-labels.ts +54 -0
- package/src/stories/clusters/worm.ts +40 -0
- package/src/stories/clusters.stories.ts +77 -0
- package/src/stories/create-cluster-labels.ts +50 -0
- package/src/stories/create-cosmos.ts +72 -0
- package/src/stories/create-story.ts +51 -0
- package/src/stories/experiments/full-mesh.ts +13 -0
- package/src/stories/experiments/mesh-with-holes.ts +13 -0
- package/src/stories/experiments.stories.ts +43 -0
- package/src/stories/generate-mesh-data.ts +125 -0
- package/src/stories/geospatial/moscow-metro-stations/index.ts +66 -0
- package/src/stories/geospatial/moscow-metro-stations/moscow-metro-coords.ts +1 -0
- package/src/stories/geospatial/moscow-metro-stations/point-colors.ts +46 -0
- package/src/stories/geospatial/moscow-metro-stations/style.css +30 -0
- package/src/stories/geospatial.stories.ts +30 -0
- package/src/stories/shapes/all-shapes/index.ts +73 -0
- package/src/stories/shapes/image-example/icons/box.png +0 -0
- package/src/stories/shapes/image-example/icons/lego.png +0 -0
- package/src/stories/shapes/image-example/icons/s.png +0 -0
- package/src/stories/shapes/image-example/icons/swift.png +0 -0
- package/src/stories/shapes/image-example/icons/toolbox.png +0 -0
- package/src/stories/shapes/image-example/index.ts +246 -0
- package/src/stories/shapes.stories.ts +37 -0
- package/src/stories/test-luma-migration.ts +195 -0
- package/src/stories/test.stories.ts +25 -0
- package/src/variables.ts +68 -0
- package/tsconfig.json +41 -0
- package/vite.config.ts +52 -0
|
@@ -0,0 +1,157 @@
|
|
|
1
|
+
import regl from 'regl'
|
|
2
|
+
import { CoreModule } from '@/graph/modules/core-module'
|
|
3
|
+
import calculateLevelFrag from '@/graph/modules/ForceManyBody/calculate-level.frag?raw'
|
|
4
|
+
import calculateLevelVert from '@/graph/modules/ForceManyBody/calculate-level.vert?raw'
|
|
5
|
+
import { forceFrag } from '@/graph/modules/ForceManyBody/quadtree-frag-shader'
|
|
6
|
+
import { createIndexesForBuffer, createQuadBuffer } from '@/graph/modules/Shared/buffer'
|
|
7
|
+
import clearFrag from '@/graph/modules/Shared/clear.frag?raw'
|
|
8
|
+
import updateVert from '@/graph/modules/Shared/quad.vert?raw'
|
|
9
|
+
|
|
10
|
+
export class ForceManyBodyQuadtree extends CoreModule {
|
|
11
|
+
private randomValuesFbo: regl.Framebuffer2D | undefined
|
|
12
|
+
private levelsTextures = new Map<string, regl.Texture2D>()
|
|
13
|
+
private levelsFbos = new Map<string, regl.Framebuffer2D>()
|
|
14
|
+
private clearLevelsCommand: regl.DrawCommand | undefined
|
|
15
|
+
private calculateLevelsCommand: regl.DrawCommand | undefined
|
|
16
|
+
private quadtreeCommand: regl.DrawCommand | undefined
|
|
17
|
+
private quadtreeLevels = 0
|
|
18
|
+
private randomValuesTexture: regl.Texture2D | undefined
|
|
19
|
+
private pointIndices: regl.Buffer | undefined
|
|
20
|
+
|
|
21
|
+
public create (): void {
|
|
22
|
+
const { reglInstance, store } = this
|
|
23
|
+
if (!store.pointsTextureSize) return
|
|
24
|
+
this.quadtreeLevels = Math.log2(store.adjustedSpaceSize)
|
|
25
|
+
for (let i = 0; i < this.quadtreeLevels; i += 1) {
|
|
26
|
+
const levelTextureSize = Math.pow(2, i + 1)
|
|
27
|
+
const levelKey = `level[${i}]`
|
|
28
|
+
if (!this.levelsTextures.has(levelKey)) {
|
|
29
|
+
this.levelsTextures.set(levelKey, reglInstance.texture())
|
|
30
|
+
}
|
|
31
|
+
const texture = this.levelsTextures.get(levelKey)
|
|
32
|
+
if (texture) {
|
|
33
|
+
texture({
|
|
34
|
+
data: new Float32Array(levelTextureSize * levelTextureSize * 4),
|
|
35
|
+
shape: [levelTextureSize, levelTextureSize, 4],
|
|
36
|
+
type: 'float',
|
|
37
|
+
})
|
|
38
|
+
}
|
|
39
|
+
if (!this.levelsFbos.has(levelKey)) {
|
|
40
|
+
this.levelsFbos.set(levelKey, reglInstance.framebuffer())
|
|
41
|
+
}
|
|
42
|
+
const fbo = this.levelsFbos.get(levelKey)
|
|
43
|
+
if (fbo) {
|
|
44
|
+
fbo({
|
|
45
|
+
color: this.levelsTextures.get(levelKey),
|
|
46
|
+
depth: false,
|
|
47
|
+
stencil: false,
|
|
48
|
+
})
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
// Create random number to prevent point to stick together in one coordinate
|
|
53
|
+
const randomValuesState = new Float32Array(store.pointsTextureSize * store.pointsTextureSize * 4)
|
|
54
|
+
for (let i = 0; i < store.pointsTextureSize * store.pointsTextureSize; ++i) {
|
|
55
|
+
randomValuesState[i * 4] = store.getRandomFloat(-1, 1) * 0.00001
|
|
56
|
+
randomValuesState[i * 4 + 1] = store.getRandomFloat(-1, 1) * 0.00001
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
if (!this.randomValuesTexture) this.randomValuesTexture = reglInstance.texture()
|
|
60
|
+
this.randomValuesTexture({
|
|
61
|
+
data: randomValuesState,
|
|
62
|
+
shape: [store.pointsTextureSize, store.pointsTextureSize, 4],
|
|
63
|
+
type: 'float',
|
|
64
|
+
})
|
|
65
|
+
if (!this.randomValuesFbo) this.randomValuesFbo = reglInstance.framebuffer()
|
|
66
|
+
this.randomValuesFbo({
|
|
67
|
+
color: this.randomValuesTexture,
|
|
68
|
+
depth: false,
|
|
69
|
+
stencil: false,
|
|
70
|
+
})
|
|
71
|
+
|
|
72
|
+
if (!this.pointIndices) this.pointIndices = reglInstance.buffer(0)
|
|
73
|
+
this.pointIndices(createIndexesForBuffer(store.pointsTextureSize))
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
public initPrograms (): void {
|
|
77
|
+
const { reglInstance, config, store, data, points } = this
|
|
78
|
+
if (!this.clearLevelsCommand) {
|
|
79
|
+
this.clearLevelsCommand = reglInstance({
|
|
80
|
+
frag: clearFrag,
|
|
81
|
+
vert: updateVert,
|
|
82
|
+
framebuffer: (_: regl.DefaultContext, props: { levelFbo: regl.Framebuffer2D }) => props.levelFbo,
|
|
83
|
+
primitive: 'triangle strip',
|
|
84
|
+
count: 4,
|
|
85
|
+
attributes: { vertexCoord: createQuadBuffer(reglInstance) },
|
|
86
|
+
})
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
if (!this.calculateLevelsCommand) {
|
|
90
|
+
this.calculateLevelsCommand = reglInstance({
|
|
91
|
+
frag: calculateLevelFrag,
|
|
92
|
+
vert: calculateLevelVert,
|
|
93
|
+
framebuffer: (_: regl.DefaultContext, props: { levelFbo: regl.Framebuffer2D; levelTextureSize: number; cellSize: number }) => props.levelFbo,
|
|
94
|
+
primitive: 'points',
|
|
95
|
+
count: () => data.pointsNumber ?? 0,
|
|
96
|
+
attributes: {
|
|
97
|
+
pointIndices: {
|
|
98
|
+
buffer: this.pointIndices,
|
|
99
|
+
size: 2,
|
|
100
|
+
},
|
|
101
|
+
},
|
|
102
|
+
uniforms: {
|
|
103
|
+
positionsTexture: () => points?.previousPositionFbo,
|
|
104
|
+
pointsTextureSize: () => store.pointsTextureSize,
|
|
105
|
+
levelTextureSize: (_: regl.DefaultContext, props: { levelTextureSize: number }) => props.levelTextureSize,
|
|
106
|
+
cellSize: (_: regl.DefaultContext, props: { cellSize: number }) => props.cellSize,
|
|
107
|
+
},
|
|
108
|
+
blend: {
|
|
109
|
+
enable: true,
|
|
110
|
+
func: {
|
|
111
|
+
src: 'one',
|
|
112
|
+
dst: 'one',
|
|
113
|
+
},
|
|
114
|
+
equation: {
|
|
115
|
+
rgb: 'add',
|
|
116
|
+
alpha: 'add',
|
|
117
|
+
},
|
|
118
|
+
},
|
|
119
|
+
depth: { enable: false, mask: false },
|
|
120
|
+
stencil: { enable: false },
|
|
121
|
+
})
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
this.quadtreeCommand = reglInstance({
|
|
125
|
+
frag: forceFrag(config.simulationRepulsionQuadtreeLevels ?? this.quadtreeLevels, this.quadtreeLevels),
|
|
126
|
+
vert: updateVert,
|
|
127
|
+
framebuffer: () => points?.velocityFbo as regl.Framebuffer2D,
|
|
128
|
+
primitive: 'triangle strip',
|
|
129
|
+
count: 4,
|
|
130
|
+
attributes: { vertexCoord: createQuadBuffer(reglInstance) },
|
|
131
|
+
uniforms: {
|
|
132
|
+
positionsTexture: () => points?.previousPositionFbo,
|
|
133
|
+
randomValues: () => this.randomValuesFbo,
|
|
134
|
+
spaceSize: () => store.adjustedSpaceSize,
|
|
135
|
+
repulsion: () => config.simulationRepulsion,
|
|
136
|
+
theta: () => config.simulationRepulsionTheta,
|
|
137
|
+
alpha: () => store.alpha,
|
|
138
|
+
...Object.fromEntries(this.levelsFbos),
|
|
139
|
+
},
|
|
140
|
+
})
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
public run (): void {
|
|
144
|
+
const { store } = this
|
|
145
|
+
for (let i = 0; i < this.quadtreeLevels; i += 1) {
|
|
146
|
+
this.clearLevelsCommand?.({ levelFbo: this.levelsFbos.get(`level[${i}]`) })
|
|
147
|
+
const levelTextureSize = Math.pow(2, i + 1)
|
|
148
|
+
const cellSize = store.adjustedSpaceSize / levelTextureSize
|
|
149
|
+
this.calculateLevelsCommand?.({
|
|
150
|
+
levelFbo: this.levelsFbos.get(`level[${i}]`),
|
|
151
|
+
levelTextureSize,
|
|
152
|
+
cellSize,
|
|
153
|
+
})
|
|
154
|
+
}
|
|
155
|
+
this.quadtreeCommand?.()
|
|
156
|
+
}
|
|
157
|
+
}
|
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
/* The fragment shader calculates the velocity of a point based on its position,
|
|
2
|
+
* the positions of other points in a spatial hierarchy, and random factors.
|
|
3
|
+
**/
|
|
4
|
+
export function forceFrag (startLevel: number, maxLevels: number): string {
|
|
5
|
+
startLevel = Math.min(startLevel, maxLevels)
|
|
6
|
+
const delta = maxLevels - startLevel
|
|
7
|
+
const calculateVelocityAdjustment = `
|
|
8
|
+
float dist = sqrt(l);
|
|
9
|
+
if (dist > 0.0) {
|
|
10
|
+
float c = alpha * repulsion * centermass.b;
|
|
11
|
+
addVelocity += calculateAdditionalVelocity(vec2(x, y), l, c);
|
|
12
|
+
addVelocity += addVelocity * random.rg;
|
|
13
|
+
}
|
|
14
|
+
`
|
|
15
|
+
function quad (level: number): string {
|
|
16
|
+
if (level >= maxLevels) {
|
|
17
|
+
return calculateVelocityAdjustment
|
|
18
|
+
} else {
|
|
19
|
+
const groupSize = Math.pow(2, level + 1)
|
|
20
|
+
|
|
21
|
+
const iEnding = new Array(level + 1 - delta).fill(0).map((_, l) => `pow(2.0, ${level - (l + delta)}.0) * i${l + delta}`).join('+')
|
|
22
|
+
const jEnding = new Array(level + 1 - delta).fill(0).map((_, l) => `pow(2.0, ${level - (l + delta)}.0) * j${l + delta}`).join('+')
|
|
23
|
+
|
|
24
|
+
return `
|
|
25
|
+
for (float ij${level} = 0.0; ij${level} < 4.0; ij${level} += 1.0) {
|
|
26
|
+
float i${level} = 0.0;
|
|
27
|
+
float j${level} = 0.0;
|
|
28
|
+
if (ij${level} == 1.0 || ij${level} == 3.0) i${level} = 1.0;
|
|
29
|
+
if (ij${level} == 2.0 || ij${level} == 3.0) j${level} = 1.0;
|
|
30
|
+
float i = pow(2.0, ${startLevel}.0) * n / width${level + 1} + ${iEnding};
|
|
31
|
+
float j = pow(2.0, ${startLevel}.0) * m / width${level + 1} + ${jEnding};
|
|
32
|
+
float groupPosX = (i + 0.5) / ${groupSize}.0;
|
|
33
|
+
float groupPosY = (j + 0.5) / ${groupSize}.0;
|
|
34
|
+
|
|
35
|
+
vec4 centermass = texture2D(level[${level}], vec2(groupPosX, groupPosY));
|
|
36
|
+
if (centermass.r > 0.0 && centermass.g > 0.0 && centermass.b > 0.0) {
|
|
37
|
+
float x = centermass.r / centermass.b - pointPosition.r;
|
|
38
|
+
float y = centermass.g / centermass.b - pointPosition.g;
|
|
39
|
+
float l = x * x + y * y;
|
|
40
|
+
if ((width${level + 1} * width${level + 1}) / theta < l) {
|
|
41
|
+
${calculateVelocityAdjustment}
|
|
42
|
+
} else {
|
|
43
|
+
${quad(level + 1)}
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
`
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
return `
|
|
51
|
+
#ifdef GL_ES
|
|
52
|
+
precision highp float;
|
|
53
|
+
#endif
|
|
54
|
+
|
|
55
|
+
uniform sampler2D positionsTexture;
|
|
56
|
+
uniform sampler2D randomValues;
|
|
57
|
+
uniform float spaceSize;
|
|
58
|
+
uniform float repulsion;
|
|
59
|
+
uniform float theta;
|
|
60
|
+
uniform float alpha;
|
|
61
|
+
uniform sampler2D level[${maxLevels}];
|
|
62
|
+
varying vec2 textureCoords;
|
|
63
|
+
|
|
64
|
+
vec2 calculateAdditionalVelocity(vec2 xy, float l, float c) {
|
|
65
|
+
float distanceMin2 = 1.0;
|
|
66
|
+
if (l < distanceMin2) l = sqrt(distanceMin2 * l);
|
|
67
|
+
float add = c / l;
|
|
68
|
+
return add * xy;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
void main() {
|
|
72
|
+
vec4 pointPosition = texture2D(positionsTexture, textureCoords);
|
|
73
|
+
vec4 random = texture2D(randomValues, textureCoords);
|
|
74
|
+
|
|
75
|
+
float width0 = spaceSize;
|
|
76
|
+
|
|
77
|
+
vec2 velocity = vec2(0.0);
|
|
78
|
+
vec2 addVelocity = vec2(0.0);
|
|
79
|
+
|
|
80
|
+
${new Array(maxLevels).fill(0).map((_, i) => `float width${i + 1} = width${i} / 2.0;`).join('\n')}
|
|
81
|
+
|
|
82
|
+
for (float n = 0.0; n < pow(2.0, ${delta}.0); n += 1.0) {
|
|
83
|
+
for (float m = 0.0; m < pow(2.0, ${delta}.0); m += 1.0) {
|
|
84
|
+
${quad(delta)}
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
velocity -= addVelocity;
|
|
89
|
+
|
|
90
|
+
gl_FragColor = vec4(velocity, 0.0, 0.0);
|
|
91
|
+
}
|
|
92
|
+
`
|
|
93
|
+
}
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
#version 300 es
|
|
2
|
+
precision highp float;
|
|
3
|
+
|
|
4
|
+
uniform sampler2D positionsTexture;
|
|
5
|
+
|
|
6
|
+
#ifdef USE_UNIFORM_BUFFERS
|
|
7
|
+
layout(std140) uniform forceMouseUniforms {
|
|
8
|
+
float repulsion;
|
|
9
|
+
vec2 mousePos;
|
|
10
|
+
} forceMouse;
|
|
11
|
+
|
|
12
|
+
#define repulsion forceMouse.repulsion
|
|
13
|
+
#define mousePos forceMouse.mousePos
|
|
14
|
+
#else
|
|
15
|
+
uniform float repulsion;
|
|
16
|
+
uniform vec2 mousePos;
|
|
17
|
+
#endif
|
|
18
|
+
|
|
19
|
+
in vec2 textureCoords;
|
|
20
|
+
out vec4 fragColor;
|
|
21
|
+
|
|
22
|
+
void main() {
|
|
23
|
+
vec4 pointPosition = texture(positionsTexture, textureCoords);
|
|
24
|
+
vec4 velocity = vec4(0.0);
|
|
25
|
+
vec2 mouse = mousePos;
|
|
26
|
+
// Move particles away from the mouse position using a repulsive force
|
|
27
|
+
vec2 distVector = mouse - pointPosition.rg;
|
|
28
|
+
float dist = sqrt(dot(distVector, distVector));
|
|
29
|
+
dist = max(dist, 10.0);
|
|
30
|
+
float angle = atan(distVector.y, distVector.x);
|
|
31
|
+
float addV = 100.0 * repulsion / (dist * dist);
|
|
32
|
+
velocity.rg -= addV * vec2(cos(angle), sin(angle));
|
|
33
|
+
|
|
34
|
+
fragColor = velocity;
|
|
35
|
+
}
|
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
import { Buffer, RenderPass, UniformStore } from '@luma.gl/core'
|
|
2
|
+
import { Model } from '@luma.gl/engine'
|
|
3
|
+
import { CoreModule } from '@/graph/modules/core-module'
|
|
4
|
+
import forceFrag from '@/graph/modules/ForceMouse/force-mouse.frag?raw'
|
|
5
|
+
import updateVert from '@/graph/modules/Shared/quad.vert?raw'
|
|
6
|
+
|
|
7
|
+
export class ForceMouse extends CoreModule {
|
|
8
|
+
private runCommand: Model | undefined
|
|
9
|
+
private vertexCoordBuffer: Buffer | undefined
|
|
10
|
+
private uniformStore: UniformStore<{
|
|
11
|
+
forceMouseUniforms: {
|
|
12
|
+
repulsion: number;
|
|
13
|
+
mousePos: [number, number];
|
|
14
|
+
};
|
|
15
|
+
}> | undefined
|
|
16
|
+
|
|
17
|
+
public initPrograms (): void {
|
|
18
|
+
const { device, points } = this
|
|
19
|
+
if (!points) return
|
|
20
|
+
|
|
21
|
+
if (!this.vertexCoordBuffer || this.vertexCoordBuffer.destroyed) {
|
|
22
|
+
this.vertexCoordBuffer = device.createBuffer({
|
|
23
|
+
data: new Float32Array([-1, -1, 1, -1, -1, 1, 1, 1]),
|
|
24
|
+
})
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
if (!this.uniformStore) {
|
|
28
|
+
this.uniformStore = new UniformStore({
|
|
29
|
+
forceMouseUniforms: {
|
|
30
|
+
uniformTypes: {
|
|
31
|
+
repulsion: 'f32',
|
|
32
|
+
mousePos: 'vec2<f32>',
|
|
33
|
+
},
|
|
34
|
+
},
|
|
35
|
+
})
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
if (!this.runCommand) {
|
|
39
|
+
this.runCommand = new Model(device, {
|
|
40
|
+
fs: forceFrag,
|
|
41
|
+
vs: updateVert,
|
|
42
|
+
topology: 'triangle-strip',
|
|
43
|
+
vertexCount: 4,
|
|
44
|
+
attributes: {
|
|
45
|
+
vertexCoord: this.vertexCoordBuffer,
|
|
46
|
+
},
|
|
47
|
+
bufferLayout: [
|
|
48
|
+
{ name: 'vertexCoord', format: 'float32x2' },
|
|
49
|
+
],
|
|
50
|
+
defines: {
|
|
51
|
+
USE_UNIFORM_BUFFERS: true,
|
|
52
|
+
},
|
|
53
|
+
bindings: {
|
|
54
|
+
forceMouseUniforms: this.uniformStore.getManagedUniformBuffer(device, 'forceMouseUniforms'),
|
|
55
|
+
positionsTexture: points.previousPositionTexture!,
|
|
56
|
+
},
|
|
57
|
+
parameters: {
|
|
58
|
+
depthWriteEnabled: false,
|
|
59
|
+
depthCompare: 'always',
|
|
60
|
+
},
|
|
61
|
+
})
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
public run (renderPass?: RenderPass): void {
|
|
66
|
+
const { device, points, store } = this
|
|
67
|
+
if (!points || !this.runCommand || !this.uniformStore) return
|
|
68
|
+
if (!points.previousPositionTexture || points.previousPositionTexture.destroyed) return
|
|
69
|
+
if (!renderPass && (!points.velocityFbo || points.velocityFbo.destroyed)) return
|
|
70
|
+
|
|
71
|
+
this.uniformStore.setUniforms({
|
|
72
|
+
forceMouseUniforms: {
|
|
73
|
+
repulsion: this.config.simulationRepulsionFromMouse ?? 0,
|
|
74
|
+
mousePos: [store.mousePosition[0] ?? 0, store.mousePosition[1] ?? 0],
|
|
75
|
+
},
|
|
76
|
+
})
|
|
77
|
+
|
|
78
|
+
this.runCommand.setBindings({
|
|
79
|
+
forceMouseUniforms: this.uniformStore.getManagedUniformBuffer(device, 'forceMouseUniforms'),
|
|
80
|
+
positionsTexture: points.previousPositionTexture!,
|
|
81
|
+
})
|
|
82
|
+
|
|
83
|
+
const pass = renderPass ?? device.beginRenderPass({
|
|
84
|
+
framebuffer: points.velocityFbo,
|
|
85
|
+
})
|
|
86
|
+
|
|
87
|
+
this.runCommand.draw(pass)
|
|
88
|
+
|
|
89
|
+
if (!renderPass) pass.end()
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
public destroy (): void {
|
|
93
|
+
this.uniformStore?.destroy()
|
|
94
|
+
this.uniformStore = undefined
|
|
95
|
+
|
|
96
|
+
this.runCommand?.destroy()
|
|
97
|
+
this.runCommand = undefined
|
|
98
|
+
|
|
99
|
+
if (this.vertexCoordBuffer && !this.vertexCoordBuffer.destroyed) this.vertexCoordBuffer.destroy()
|
|
100
|
+
this.vertexCoordBuffer = undefined
|
|
101
|
+
}
|
|
102
|
+
}
|