@cosmos.gl/graph 2.6.0 → 2.6.2-rc.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.
Files changed (169) hide show
  1. package/dist/config.d.ts +3 -0
  2. package/dist/index.d.ts +48 -6
  3. package/dist/index.js +1346 -1289
  4. package/dist/index.js.map +1 -1
  5. package/dist/index.min.js +15 -15
  6. package/dist/index.min.js.map +1 -1
  7. package/package.json +5 -1
  8. package/.eslintrc +0 -147
  9. package/.github/SECURITY.md +0 -19
  10. package/.github/dco.yml +0 -4
  11. package/.github/workflows/github_pages.yml +0 -54
  12. package/.storybook/main.ts +0 -26
  13. package/.storybook/manager-head.html +0 -1
  14. package/.storybook/manager.ts +0 -14
  15. package/.storybook/preview.ts +0 -29
  16. package/.storybook/style.css +0 -3
  17. package/CHARTER.md +0 -69
  18. package/CODE_OF_CONDUCT.md +0 -178
  19. package/CONTRIBUTING.md +0 -22
  20. package/GOVERNANCE.md +0 -21
  21. package/cosmos-2-0-migration-notes.md +0 -98
  22. package/cosmos_awesome.md +0 -96
  23. package/dist/stories/beginners/basic-set-up/data-gen.d.ts +0 -4
  24. package/dist/stories/beginners/basic-set-up/index.d.ts +0 -5
  25. package/dist/stories/beginners/link-hovering/data-generator.d.ts +0 -19
  26. package/dist/stories/beginners/link-hovering/index.d.ts +0 -5
  27. package/dist/stories/beginners/pinned-points/data-gen.d.ts +0 -5
  28. package/dist/stories/beginners/pinned-points/index.d.ts +0 -5
  29. package/dist/stories/beginners/point-labels/data.d.ts +0 -13
  30. package/dist/stories/beginners/point-labels/index.d.ts +0 -9
  31. package/dist/stories/beginners/point-labels/labels.d.ts +0 -8
  32. package/dist/stories/beginners/quick-start.d.ts +0 -5
  33. package/dist/stories/beginners/remove-points/config.d.ts +0 -2
  34. package/dist/stories/beginners/remove-points/data-gen.d.ts +0 -4
  35. package/dist/stories/beginners/remove-points/index.d.ts +0 -5
  36. package/dist/stories/beginners.stories.d.ts +0 -11
  37. package/dist/stories/clusters/polygon-selection/index.d.ts +0 -6
  38. package/dist/stories/clusters/polygon-selection/polygon.d.ts +0 -20
  39. package/dist/stories/clusters/radial.d.ts +0 -5
  40. package/dist/stories/clusters/with-labels.d.ts +0 -6
  41. package/dist/stories/clusters/worm.d.ts +0 -5
  42. package/dist/stories/clusters.stories.d.ts +0 -9
  43. package/dist/stories/create-cluster-labels.d.ts +0 -4
  44. package/dist/stories/create-cosmos.d.ts +0 -16
  45. package/dist/stories/create-story.d.ts +0 -16
  46. package/dist/stories/experiments/full-mesh.d.ts +0 -5
  47. package/dist/stories/experiments/mesh-with-holes.d.ts +0 -5
  48. package/dist/stories/experiments.stories.d.ts +0 -7
  49. package/dist/stories/generate-mesh-data.d.ts +0 -12
  50. package/dist/stories/geospatial/moscow-metro-stations/index.d.ts +0 -15
  51. package/dist/stories/geospatial/moscow-metro-stations/moscow-metro-coords.d.ts +0 -1
  52. package/dist/stories/geospatial/moscow-metro-stations/point-colors.d.ts +0 -1
  53. package/dist/stories/geospatial.stories.d.ts +0 -6
  54. package/dist/stories/shapes/all-shapes/index.d.ts +0 -5
  55. package/dist/stories/shapes/image-example/index.d.ts +0 -5
  56. package/dist/stories/shapes.stories.d.ts +0 -7
  57. package/logo.svg +0 -3
  58. package/rollup.config.js +0 -70
  59. package/src/config.ts +0 -734
  60. package/src/declaration.d.ts +0 -12
  61. package/src/graph/utils/error-message.ts +0 -23
  62. package/src/helper.ts +0 -74
  63. package/src/index.ts +0 -1635
  64. package/src/modules/Clusters/calculate-centermass.frag +0 -9
  65. package/src/modules/Clusters/calculate-centermass.vert +0 -26
  66. package/src/modules/Clusters/force-cluster.frag +0 -39
  67. package/src/modules/Clusters/index.ts +0 -200
  68. package/src/modules/Drag/index.ts +0 -33
  69. package/src/modules/FPSMonitor/css.ts +0 -53
  70. package/src/modules/FPSMonitor/index.ts +0 -28
  71. package/src/modules/ForceCenter/calculate-centermass.frag +0 -9
  72. package/src/modules/ForceCenter/calculate-centermass.vert +0 -18
  73. package/src/modules/ForceCenter/force-center.frag +0 -27
  74. package/src/modules/ForceCenter/index.ts +0 -104
  75. package/src/modules/ForceGravity/force-gravity.frag +0 -27
  76. package/src/modules/ForceGravity/index.ts +0 -33
  77. package/src/modules/ForceLink/force-spring.ts +0 -73
  78. package/src/modules/ForceLink/index.ts +0 -149
  79. package/src/modules/ForceManyBody/calculate-level.frag +0 -9
  80. package/src/modules/ForceManyBody/calculate-level.vert +0 -25
  81. package/src/modules/ForceManyBody/force-centermass.frag +0 -52
  82. package/src/modules/ForceManyBody/force-level.frag +0 -121
  83. package/src/modules/ForceManyBody/index.ts +0 -223
  84. package/src/modules/ForceManyBody/quadtree-frag-shader.ts +0 -90
  85. package/src/modules/ForceManyBodyQuadtree/calculate-level.frag +0 -9
  86. package/src/modules/ForceManyBodyQuadtree/calculate-level.vert +0 -25
  87. package/src/modules/ForceManyBodyQuadtree/index.ts +0 -157
  88. package/src/modules/ForceManyBodyQuadtree/quadtree-frag-shader.ts +0 -93
  89. package/src/modules/ForceMouse/force-mouse.frag +0 -24
  90. package/src/modules/ForceMouse/index.ts +0 -32
  91. package/src/modules/GraphData/index.ts +0 -384
  92. package/src/modules/Lines/draw-curve-line.frag +0 -46
  93. package/src/modules/Lines/draw-curve-line.vert +0 -194
  94. package/src/modules/Lines/geometry.ts +0 -18
  95. package/src/modules/Lines/hovered-line-index.frag +0 -27
  96. package/src/modules/Lines/hovered-line-index.vert +0 -8
  97. package/src/modules/Lines/index.ts +0 -273
  98. package/src/modules/Points/atlas-utils.ts +0 -137
  99. package/src/modules/Points/drag-point.frag +0 -20
  100. package/src/modules/Points/draw-highlighted.frag +0 -16
  101. package/src/modules/Points/draw-highlighted.vert +0 -86
  102. package/src/modules/Points/draw-points.frag +0 -243
  103. package/src/modules/Points/draw-points.vert +0 -127
  104. package/src/modules/Points/fill-sampled-points.frag +0 -9
  105. package/src/modules/Points/fill-sampled-points.vert +0 -29
  106. package/src/modules/Points/find-hovered-point.frag +0 -9
  107. package/src/modules/Points/find-hovered-point.vert +0 -57
  108. package/src/modules/Points/find-points-on-area-selection.frag +0 -48
  109. package/src/modules/Points/find-points-on-polygon-selection.frag +0 -65
  110. package/src/modules/Points/index.ts +0 -968
  111. package/src/modules/Points/track-positions.frag +0 -18
  112. package/src/modules/Points/update-position.frag +0 -37
  113. package/src/modules/Shared/buffer.ts +0 -37
  114. package/src/modules/Shared/clear.frag +0 -7
  115. package/src/modules/Shared/quad.vert +0 -12
  116. package/src/modules/Store/index.ts +0 -173
  117. package/src/modules/Zoom/index.ts +0 -148
  118. package/src/modules/core-module.ts +0 -28
  119. package/src/stories/1. welcome.mdx +0 -81
  120. package/src/stories/2. configuration.mdx +0 -113
  121. package/src/stories/3. api-reference.mdx +0 -591
  122. package/src/stories/beginners/basic-set-up/data-gen.ts +0 -33
  123. package/src/stories/beginners/basic-set-up/index.ts +0 -163
  124. package/src/stories/beginners/basic-set-up/style.css +0 -35
  125. package/src/stories/beginners/link-hovering/data-generator.ts +0 -198
  126. package/src/stories/beginners/link-hovering/index.ts +0 -61
  127. package/src/stories/beginners/link-hovering/style.css +0 -73
  128. package/src/stories/beginners/pinned-points/data-gen.ts +0 -153
  129. package/src/stories/beginners/pinned-points/index.ts +0 -61
  130. package/src/stories/beginners/point-labels/data.ts +0 -73
  131. package/src/stories/beginners/point-labels/index.ts +0 -65
  132. package/src/stories/beginners/point-labels/labels.ts +0 -46
  133. package/src/stories/beginners/point-labels/style.css +0 -16
  134. package/src/stories/beginners/quick-start.ts +0 -50
  135. package/src/stories/beginners/remove-points/config.ts +0 -25
  136. package/src/stories/beginners/remove-points/data-gen.ts +0 -30
  137. package/src/stories/beginners/remove-points/index.ts +0 -92
  138. package/src/stories/beginners/remove-points/style.css +0 -31
  139. package/src/stories/beginners.stories.ts +0 -131
  140. package/src/stories/clusters/polygon-selection/index.ts +0 -51
  141. package/src/stories/clusters/polygon-selection/polygon.ts +0 -143
  142. package/src/stories/clusters/polygon-selection/style.css +0 -8
  143. package/src/stories/clusters/radial.ts +0 -24
  144. package/src/stories/clusters/with-labels.ts +0 -53
  145. package/src/stories/clusters/worm.ts +0 -40
  146. package/src/stories/clusters.stories.ts +0 -77
  147. package/src/stories/create-cluster-labels.ts +0 -50
  148. package/src/stories/create-cosmos.ts +0 -68
  149. package/src/stories/create-story.ts +0 -51
  150. package/src/stories/experiments/full-mesh.ts +0 -13
  151. package/src/stories/experiments/mesh-with-holes.ts +0 -13
  152. package/src/stories/experiments.stories.ts +0 -43
  153. package/src/stories/generate-mesh-data.ts +0 -125
  154. package/src/stories/geospatial/moscow-metro-stations/index.ts +0 -62
  155. package/src/stories/geospatial/moscow-metro-stations/moscow-metro-coords.ts +0 -1
  156. package/src/stories/geospatial/moscow-metro-stations/point-colors.ts +0 -46
  157. package/src/stories/geospatial/moscow-metro-stations/style.css +0 -30
  158. package/src/stories/geospatial.stories.ts +0 -30
  159. package/src/stories/shapes/all-shapes/index.ts +0 -69
  160. package/src/stories/shapes/image-example/icons/box.png +0 -0
  161. package/src/stories/shapes/image-example/icons/lego.png +0 -0
  162. package/src/stories/shapes/image-example/icons/s.png +0 -0
  163. package/src/stories/shapes/image-example/icons/swift.png +0 -0
  164. package/src/stories/shapes/image-example/icons/toolbox.png +0 -0
  165. package/src/stories/shapes/image-example/index.ts +0 -238
  166. package/src/stories/shapes.stories.ts +0 -37
  167. package/src/variables.ts +0 -68
  168. package/tsconfig.json +0 -41
  169. package/vite.config.ts +0 -54
@@ -1,149 +0,0 @@
1
- import regl from 'regl'
2
- import { CoreModule } from '@/graph/modules/core-module'
3
- import { forceFrag } from '@/graph/modules/ForceLink/force-spring'
4
- import { createQuadBuffer } from '@/graph/modules/Shared/buffer'
5
- import updateVert from '@/graph/modules/Shared/quad.vert'
6
-
7
- export enum LinkDirection {
8
- OUTGOING = 'outgoing',
9
- INCOMING = 'incoming'
10
- }
11
-
12
- export class ForceLink extends CoreModule {
13
- private linkFirstIndicesAndAmountFbo: regl.Framebuffer2D | undefined
14
- private indicesFbo: regl.Framebuffer2D | undefined
15
- private biasAndStrengthFbo: regl.Framebuffer2D | undefined
16
- private randomDistanceFbo: regl.Framebuffer2D | undefined
17
- private linkFirstIndicesAndAmount: Float32Array = new Float32Array()
18
- private indices: Float32Array = new Float32Array()
19
- private maxPointDegree = 0
20
- private runCommand: regl.DrawCommand | undefined
21
- private linkFirstIndicesAndAmountTexture: regl.Texture2D | undefined
22
- private indicesTexture: regl.Texture2D | undefined
23
- private biasAndStrengthTexture: regl.Texture2D | undefined
24
- private randomDistanceTexture: regl.Texture2D | undefined
25
-
26
- public create (direction: LinkDirection): void {
27
- const { reglInstance, store: { pointsTextureSize, linksTextureSize }, data } = this
28
- if (!pointsTextureSize || !linksTextureSize) return
29
- this.linkFirstIndicesAndAmount = new Float32Array(pointsTextureSize * pointsTextureSize * 4)
30
- this.indices = new Float32Array(linksTextureSize * linksTextureSize * 4)
31
- const linkBiasAndStrengthState = new Float32Array(linksTextureSize * linksTextureSize * 4)
32
- const linkDistanceState = new Float32Array(linksTextureSize * linksTextureSize * 4)
33
-
34
- const grouped = direction === LinkDirection.INCOMING ? data.sourceIndexToTargetIndices : data.targetIndexToSourceIndices
35
- this.maxPointDegree = 0
36
- let linkIndex = 0
37
- grouped?.forEach((connectedPointIndices, pointIndex) => {
38
- if (connectedPointIndices) {
39
- this.linkFirstIndicesAndAmount[pointIndex * 4 + 0] = linkIndex % linksTextureSize
40
- this.linkFirstIndicesAndAmount[pointIndex * 4 + 1] = Math.floor(linkIndex / linksTextureSize)
41
- this.linkFirstIndicesAndAmount[pointIndex * 4 + 2] = connectedPointIndices.length ?? 0
42
-
43
- connectedPointIndices.forEach(([connectedPointIndex, initialLinkIndex]) => {
44
- this.indices[linkIndex * 4 + 0] = connectedPointIndex % pointsTextureSize
45
- this.indices[linkIndex * 4 + 1] = Math.floor(connectedPointIndex / pointsTextureSize)
46
- const degree = data.degree?.[connectedPointIndex] ?? 0
47
- const connectedDegree = data.degree?.[pointIndex] ?? 0
48
- const degreeSum = degree + connectedDegree
49
- // Prevent division by zero
50
- const bias = degreeSum !== 0 ? degree / degreeSum : 0.5
51
- const minDegree = Math.min(degree, connectedDegree)
52
- // Prevent division by zero
53
- let strength = data.linkStrength?.[initialLinkIndex] ?? (1 / Math.max(minDegree, 1))
54
- strength = Math.sqrt(strength)
55
- linkBiasAndStrengthState[linkIndex * 4 + 0] = bias
56
- linkBiasAndStrengthState[linkIndex * 4 + 1] = strength
57
- linkDistanceState[linkIndex * 4] = this.store.getRandomFloat(0, 1)
58
-
59
- linkIndex += 1
60
- })
61
-
62
- this.maxPointDegree = Math.max(this.maxPointDegree, connectedPointIndices.length ?? 0)
63
- }
64
- })
65
-
66
- if (!this.linkFirstIndicesAndAmountTexture) this.linkFirstIndicesAndAmountTexture = reglInstance.texture()
67
- this.linkFirstIndicesAndAmountTexture({
68
- data: this.linkFirstIndicesAndAmount,
69
- shape: [pointsTextureSize, pointsTextureSize, 4],
70
- type: 'float',
71
- })
72
- if (!this.linkFirstIndicesAndAmountFbo) this.linkFirstIndicesAndAmountFbo = reglInstance.framebuffer()
73
- this.linkFirstIndicesAndAmountFbo({
74
- color: this.linkFirstIndicesAndAmountTexture,
75
- depth: false,
76
- stencil: false,
77
- })
78
-
79
- if (!this.indicesTexture) this.indicesTexture = reglInstance.texture()
80
- this.indicesTexture({
81
- data: this.indices,
82
- shape: [linksTextureSize, linksTextureSize, 4],
83
- type: 'float',
84
- })
85
- if (!this.indicesFbo) this.indicesFbo = reglInstance.framebuffer()
86
- this.indicesFbo({
87
- color: this.indicesTexture,
88
- depth: false,
89
- stencil: false,
90
- })
91
-
92
- if (!this.biasAndStrengthTexture) this.biasAndStrengthTexture = reglInstance.texture()
93
- this.biasAndStrengthTexture({
94
- data: linkBiasAndStrengthState,
95
- shape: [linksTextureSize, linksTextureSize, 4],
96
- type: 'float',
97
- })
98
- if (!this.biasAndStrengthFbo) this.biasAndStrengthFbo = reglInstance.framebuffer()
99
- this.biasAndStrengthFbo({
100
- color: this.biasAndStrengthTexture,
101
- depth: false,
102
- stencil: false,
103
- })
104
-
105
- if (!this.randomDistanceTexture) this.randomDistanceTexture = reglInstance.texture()
106
- this.randomDistanceTexture({
107
- data: linkDistanceState,
108
- shape: [linksTextureSize, linksTextureSize, 4],
109
- type: 'float',
110
- })
111
- if (!this.randomDistanceFbo) this.randomDistanceFbo = reglInstance.framebuffer()
112
- this.randomDistanceFbo({
113
- color: this.randomDistanceTexture,
114
- depth: false,
115
- stencil: false,
116
- })
117
- }
118
-
119
- public initPrograms (): void {
120
- const { reglInstance, config, store, points } = this
121
- if (!this.runCommand) {
122
- this.runCommand = reglInstance({
123
- frag: () => forceFrag(this.maxPointDegree),
124
- vert: updateVert,
125
- framebuffer: () => points?.velocityFbo as regl.Framebuffer2D,
126
- primitive: 'triangle strip',
127
- count: 4,
128
- attributes: { vertexCoord: createQuadBuffer(reglInstance) },
129
- uniforms: {
130
- positionsTexture: () => points?.previousPositionFbo,
131
- linkSpring: () => config.simulationLinkSpring,
132
- linkDistance: () => config.simulationLinkDistance,
133
- linkDistRandomVariationRange: () => config.simulationLinkDistRandomVariationRange,
134
- linkInfoTexture: () => this.linkFirstIndicesAndAmountFbo,
135
- linkIndicesTexture: () => this.indicesFbo,
136
- linkPropertiesTexture: () => this.biasAndStrengthFbo,
137
- linkRandomDistanceTexture: () => this.randomDistanceFbo,
138
- pointsTextureSize: () => store.pointsTextureSize,
139
- linksTextureSize: () => store.linksTextureSize,
140
- alpha: () => store.alpha,
141
- },
142
- })
143
- }
144
- }
145
-
146
- public run (): void {
147
- this.runCommand?.()
148
- }
149
- }
@@ -1,9 +0,0 @@
1
- #ifdef GL_ES
2
- precision highp float;
3
- #endif
4
-
5
- varying vec4 rgba;
6
-
7
- void main() {
8
- gl_FragColor = rgba;
9
- }
@@ -1,25 +0,0 @@
1
- #ifdef GL_ES
2
- precision highp float;
3
- #endif
4
-
5
- uniform sampler2D positionsTexture;
6
- uniform float pointsTextureSize;
7
- uniform float levelTextureSize;
8
- uniform float cellSize;
9
-
10
- attribute vec2 pointIndices;
11
-
12
- varying vec4 rgba;
13
-
14
- void main() {
15
- vec4 pointPosition = texture2D(positionsTexture, pointIndices / pointsTextureSize);
16
- rgba = vec4(pointPosition.rg, 1.0, 0.0);
17
-
18
- float n = floor(pointPosition.x / cellSize);
19
- float m = floor(pointPosition.y / cellSize);
20
-
21
- vec2 levelPosition = 2.0 * (vec2(n, m) + 0.5) / levelTextureSize - 1.0;
22
-
23
- gl_Position = vec4(levelPosition, 0.0, 1.0);
24
- gl_PointSize = 1.0;
25
- }
@@ -1,52 +0,0 @@
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
- #ifdef GL_ES
5
- precision highp float;
6
- #endif
7
-
8
- uniform sampler2D positionsTexture;
9
- uniform sampler2D levelFbo;
10
- uniform sampler2D randomValues;
11
-
12
- uniform float levelTextureSize;
13
- uniform float repulsion;
14
- uniform float alpha;
15
-
16
- varying vec2 textureCoords;
17
-
18
- // Calculate the additional velocity based on the center of mass
19
- vec2 calculateAdditionalVelocity (vec2 ij, vec2 pp) {
20
- vec2 add = vec2(0.0);
21
- vec4 centermass = texture2D(levelFbo, ij);
22
- if (centermass.r > 0.0 && centermass.g > 0.0 && centermass.b > 0.0) {
23
- vec2 centermassPosition = vec2(centermass.rg / centermass.b);
24
- vec2 distVector = pp - centermassPosition;
25
- float l = dot(distVector, distVector);
26
- float dist = sqrt(l);
27
- if (l > 0.0) {
28
- float angle = atan(distVector.y, distVector.x);
29
- float c = alpha * repulsion * centermass.b;
30
-
31
- float distanceMin2 = 1.0;
32
- if (l < distanceMin2) l = sqrt(distanceMin2 * l);
33
- float addV = c / sqrt(l);
34
- add = addV * vec2(cos(angle), sin(angle));
35
- }
36
- }
37
- return add;
38
- }
39
-
40
- void main() {
41
- vec4 pointPosition = texture2D(positionsTexture, textureCoords);
42
- vec4 random = texture2D(randomValues, textureCoords);
43
-
44
- vec4 velocity = vec4(0.0);
45
-
46
- // Calculate additional velocity based on the point position
47
- velocity.xy += calculateAdditionalVelocity(pointPosition.xy / levelTextureSize, pointPosition.xy);
48
- // Apply random factor to the velocity
49
- velocity.xy += velocity.xy * random.rg;
50
-
51
- gl_FragColor = velocity;
52
- }
@@ -1,121 +0,0 @@
1
- // The fragment shader calculates the velocity of a point based on its position
2
- // and the positions of other points in different levels of a spatial hierarchy.
3
-
4
- #ifdef GL_ES
5
- precision highp float;
6
- #endif
7
-
8
- uniform sampler2D positionsTexture;
9
- uniform sampler2D levelFbo;
10
-
11
- uniform float level;
12
- uniform float levels;
13
- uniform float levelTextureSize;
14
- uniform float repulsion;
15
- uniform float alpha;
16
- uniform float spaceSize;
17
- uniform float theta;
18
-
19
- varying vec2 textureCoords;
20
-
21
- const float MAX_LEVELS_NUM = 14.0;
22
-
23
- vec2 calculateAdditionalVelocity (vec2 ij, vec2 pp) {
24
- vec2 add = vec2(0.0);
25
- vec4 centermass = texture2D(levelFbo, ij);
26
- if (centermass.r > 0.0 && centermass.g > 0.0 && centermass.b > 0.0) {
27
- vec2 centermassPosition = vec2(centermass.rg / centermass.b);
28
- vec2 distVector = pp - centermassPosition;
29
- float l = dot(distVector, distVector);
30
- float dist = sqrt(l);
31
- if (l > 0.0) {
32
- float c = alpha * repulsion * centermass.b;
33
-
34
- float distanceMin2 = 1.0;
35
- if (l < distanceMin2) l = sqrt(distanceMin2 * l);
36
- float addV = c / sqrt(l);
37
- add = addV * normalize(distVector);
38
- }
39
- }
40
- return add;
41
- }
42
-
43
- void main() {
44
- vec4 pointPosition = texture2D(positionsTexture, textureCoords);
45
- float x = pointPosition.x;
46
- float y = pointPosition.y;
47
-
48
- float left = 0.0;
49
- float top = 0.0;
50
- float right = spaceSize;
51
- float bottom = spaceSize;
52
-
53
- float n_left = 0.0;
54
- float n_top = 0.0;
55
- float n_right = 0.0;
56
- float n_bottom = 0.0;
57
-
58
- float cellSize = 0.0;
59
-
60
- // Iterate over levels to adjust the boundaries based on the current level
61
- for (float i = 0.0; i < MAX_LEVELS_NUM; i += 1.0) {
62
- if (i <= level) {
63
- left += cellSize * n_left;
64
- top += cellSize * n_top;
65
- right -= cellSize * n_right;
66
- bottom -= cellSize * n_bottom;
67
-
68
- cellSize = pow(2.0 , levels - i - 1.0);
69
-
70
- float dist_left = x - left;
71
- n_left = max(0.0, floor(dist_left / cellSize - theta));
72
-
73
- float dist_top = y - top;
74
- n_top = max(0.0, floor(dist_top / cellSize - theta));
75
-
76
- float dist_right = right - x;
77
- n_right = max(0.0, floor(dist_right / cellSize - theta));
78
-
79
- float dist_bottom = bottom - y;
80
- n_bottom = max(0.0, floor(dist_bottom / cellSize - theta));
81
-
82
- }
83
- }
84
-
85
- vec4 velocity = vec4(vec2(0.0), 1.0, 0.0);
86
-
87
- // Calculate the additional velocity based on neighboring cells
88
- for (float i = 0.0; i < 12.0; i += 1.0) {
89
- for (float j = 0.0; j < 4.0; j += 1.0) {
90
- float n = left + cellSize * j;
91
- float m = top + cellSize * n_top + cellSize * i;
92
-
93
- if (n < (left + n_left * cellSize) && m < bottom) {
94
- velocity.xy += calculateAdditionalVelocity(vec2(n / cellSize, m / cellSize) / levelTextureSize, pointPosition.xy);
95
- }
96
-
97
- n = left + cellSize * i;
98
- m = top + cellSize * j;
99
-
100
- if (n < (right - n_right * cellSize) && m < (top + n_top * cellSize)) {
101
- velocity.xy += calculateAdditionalVelocity(vec2(n / cellSize, m / cellSize) / levelTextureSize, pointPosition.xy);
102
- }
103
-
104
- n = right - n_right * cellSize + cellSize * j;
105
- m = top + cellSize * i;
106
-
107
- if (n < right && m < (bottom - n_bottom * cellSize)) {
108
- velocity.xy += calculateAdditionalVelocity(vec2(n / cellSize, m / cellSize) / levelTextureSize, pointPosition.xy);
109
- }
110
-
111
- n = left + n_left * cellSize + cellSize * i;
112
- m = bottom - n_bottom * cellSize + cellSize * j;
113
-
114
- if (n < right && m < bottom) {
115
- velocity.xy += calculateAdditionalVelocity(vec2(n / cellSize, m / cellSize) / levelTextureSize, pointPosition.xy);
116
- }
117
- }
118
- }
119
-
120
- gl_FragColor = velocity;
121
- }
@@ -1,223 +0,0 @@
1
- import regl from 'regl'
2
- import { CoreModule } from '@/graph/modules/core-module'
3
- import calculateLevelFrag from '@/graph/modules/ForceManyBody/calculate-level.frag'
4
- import calculateLevelVert from '@/graph/modules/ForceManyBody/calculate-level.vert'
5
- import forceFrag from '@/graph/modules/ForceManyBody/force-level.frag'
6
- import forceCenterFrag from '@/graph/modules/ForceManyBody/force-centermass.frag'
7
- import { createIndexesForBuffer, createQuadBuffer } from '@/graph/modules/Shared/buffer'
8
- import clearFrag from '@/graph/modules/Shared/clear.frag'
9
- import updateVert from '@/graph/modules/Shared/quad.vert'
10
-
11
- export class ForceManyBody extends CoreModule {
12
- private randomValuesFbo: regl.Framebuffer2D | undefined
13
- private levelsFbos = new Map<string, regl.Framebuffer2D>()
14
- private clearLevelsCommand: regl.DrawCommand | undefined
15
- private clearVelocityCommand: regl.DrawCommand | undefined
16
- private calculateLevelsCommand: regl.DrawCommand | undefined
17
- private forceCommand: regl.DrawCommand | undefined
18
- private forceFromItsOwnCentermassCommand: regl.DrawCommand | undefined
19
- private quadtreeLevels = 0
20
- private randomValuesTexture: regl.Texture2D | undefined
21
- private pointIndices: regl.Buffer | undefined
22
-
23
- public create (): void {
24
- const { reglInstance, store } = this
25
- if (!store.pointsTextureSize) return
26
- this.quadtreeLevels = Math.log2(store.adjustedSpaceSize)
27
- for (let i = 0; i < this.quadtreeLevels; i += 1) {
28
- const levelTextureSize = Math.pow(2, i + 1)
29
- if (!this.levelsFbos.has(`level[${i}]`)) {
30
- this.levelsFbos.set(`level[${i}]`, reglInstance.framebuffer())
31
- }
32
- const fbo = this.levelsFbos.get(`level[${i}]`)
33
- if (fbo) {
34
- fbo({
35
- shape: [levelTextureSize, levelTextureSize],
36
- colorType: 'float',
37
- depth: false,
38
- stencil: false,
39
- })
40
- }
41
- }
42
- // Create random number to prevent point to stick together in one coordinate
43
- const randomValuesState = new Float32Array(store.pointsTextureSize * store.pointsTextureSize * 4)
44
- for (let i = 0; i < store.pointsTextureSize * store.pointsTextureSize; ++i) {
45
- randomValuesState[i * 4] = store.getRandomFloat(-1, 1) * 0.00001
46
- randomValuesState[i * 4 + 1] = store.getRandomFloat(-1, 1) * 0.00001
47
- }
48
-
49
- if (!this.randomValuesTexture) this.randomValuesTexture = reglInstance.texture()
50
- this.randomValuesTexture({
51
- data: randomValuesState,
52
- shape: [store.pointsTextureSize, store.pointsTextureSize, 4],
53
- type: 'float',
54
- })
55
- if (!this.randomValuesFbo) this.randomValuesFbo = reglInstance.framebuffer()
56
- this.randomValuesFbo({
57
- color: this.randomValuesTexture,
58
- depth: false,
59
- stencil: false,
60
- })
61
-
62
- if (!this.pointIndices) this.pointIndices = reglInstance.buffer(0)
63
- this.pointIndices(createIndexesForBuffer(store.pointsTextureSize))
64
- }
65
-
66
- public initPrograms (): void {
67
- const { reglInstance, config, store, data, points } = this
68
- if (!this.clearLevelsCommand) {
69
- this.clearLevelsCommand = reglInstance({
70
- frag: clearFrag,
71
- vert: updateVert,
72
- framebuffer: (_: regl.DefaultContext, props: { levelFbo: regl.Framebuffer2D }) => props.levelFbo,
73
- primitive: 'triangle strip',
74
- count: 4,
75
- attributes: { vertexCoord: createQuadBuffer(reglInstance) },
76
- })
77
- }
78
- if (!this.calculateLevelsCommand) {
79
- this.calculateLevelsCommand = reglInstance({
80
- frag: calculateLevelFrag,
81
- vert: calculateLevelVert,
82
- framebuffer: (_: regl.DefaultContext, props: { levelFbo: regl.Framebuffer2D; levelTextureSize: number; cellSize: number }) => props.levelFbo,
83
- primitive: 'points',
84
- count: () => data.pointsNumber ?? 0,
85
- attributes: {
86
- pointIndices: {
87
- buffer: this.pointIndices,
88
- size: 2,
89
- },
90
- },
91
- uniforms: {
92
- positionsTexture: () => points?.previousPositionFbo,
93
- pointsTextureSize: () => store.pointsTextureSize,
94
- levelTextureSize: (_: regl.DefaultContext, props: { levelTextureSize: number }) => props.levelTextureSize,
95
- cellSize: (_: regl.DefaultContext, props: { cellSize: number }) => props.cellSize,
96
- },
97
- blend: {
98
- enable: true,
99
- func: {
100
- src: 'one',
101
- dst: 'one',
102
- },
103
- equation: {
104
- rgb: 'add',
105
- alpha: 'add',
106
- },
107
- },
108
- depth: { enable: false, mask: false },
109
- stencil: { enable: false },
110
- })
111
- }
112
-
113
- if (!this.forceCommand) {
114
- this.forceCommand = reglInstance({
115
- frag: forceFrag,
116
- vert: updateVert,
117
- framebuffer: () => points?.velocityFbo as regl.Framebuffer2D,
118
- primitive: 'triangle strip',
119
- count: 4,
120
- attributes: { vertexCoord: createQuadBuffer(reglInstance) },
121
- uniforms: {
122
- positionsTexture: () => points?.previousPositionFbo,
123
- level: (_, props: { levelFbo: regl.Framebuffer2D; levelTextureSize: number; level: number }) => props.level,
124
- levels: this.quadtreeLevels,
125
- levelFbo: (_, props) => props.levelFbo,
126
- levelTextureSize: (_, props) => props.levelTextureSize,
127
- alpha: () => store.alpha,
128
- repulsion: () => config.simulationRepulsion,
129
- spaceSize: () => store.adjustedSpaceSize,
130
- theta: () => config.simulationRepulsionTheta,
131
- },
132
- blend: {
133
- enable: true,
134
- func: {
135
- src: 'one',
136
- dst: 'one',
137
- },
138
- equation: {
139
- rgb: 'add',
140
- alpha: 'add',
141
- },
142
- },
143
- depth: { enable: false, mask: false },
144
- stencil: { enable: false },
145
- })
146
- }
147
-
148
- if (!this.forceFromItsOwnCentermassCommand) {
149
- this.forceFromItsOwnCentermassCommand = reglInstance({
150
- frag: forceCenterFrag,
151
- vert: updateVert,
152
- framebuffer: () => points?.velocityFbo as regl.Framebuffer2D,
153
- primitive: 'triangle strip',
154
- count: 4,
155
- attributes: { vertexCoord: createQuadBuffer(reglInstance) },
156
- uniforms: {
157
- positionsTexture: () => points?.previousPositionFbo,
158
- randomValues: () => this.randomValuesFbo,
159
- levelFbo: (_, props: { levelFbo: regl.Framebuffer2D; levelTextureSize: number }) => props.levelFbo,
160
- levelTextureSize: (_, props) => props.levelTextureSize,
161
- alpha: () => store.alpha,
162
- repulsion: () => config.simulationRepulsion,
163
- spaceSize: () => store.adjustedSpaceSize,
164
- },
165
- blend: {
166
- enable: true,
167
- func: {
168
- src: 'one',
169
- dst: 'one',
170
- },
171
- equation: {
172
- rgb: 'add',
173
- alpha: 'add',
174
- },
175
- },
176
- depth: { enable: false, mask: false },
177
- stencil: { enable: false },
178
- })
179
- }
180
-
181
- if (!this.clearVelocityCommand) {
182
- this.clearVelocityCommand = reglInstance({
183
- frag: clearFrag,
184
- vert: updateVert,
185
- framebuffer: () => points?.velocityFbo as regl.Framebuffer2D,
186
- primitive: 'triangle strip',
187
- count: 4,
188
- attributes: { vertexCoord: createQuadBuffer(reglInstance) },
189
- })
190
- }
191
- }
192
-
193
- public run (): void {
194
- const { store } = this
195
- for (let i = 0; i < this.quadtreeLevels; i += 1) {
196
- this.clearLevelsCommand?.({ levelFbo: this.levelsFbos.get(`level[${i}]`) })
197
- const levelTextureSize = Math.pow(2, i + 1)
198
- const cellSize = store.adjustedSpaceSize / levelTextureSize
199
- this.calculateLevelsCommand?.({
200
- levelFbo: this.levelsFbos.get(`level[${i}]`),
201
- levelTextureSize,
202
- cellSize,
203
- })
204
- }
205
- this.clearVelocityCommand?.()
206
- for (let i = 0; i < this.quadtreeLevels; i += 1) {
207
- const levelTextureSize = Math.pow(2, i + 1)
208
- this.forceCommand?.({
209
- levelFbo: this.levelsFbos.get(`level[${i}]`),
210
- levelTextureSize,
211
- level: i,
212
- })
213
-
214
- if (i === this.quadtreeLevels - 1) {
215
- this.forceFromItsOwnCentermassCommand?.({
216
- levelFbo: this.levelsFbos.get(`level[${i}]`),
217
- levelTextureSize,
218
- level: i,
219
- })
220
- }
221
- }
222
- }
223
- }
@@ -1,90 +0,0 @@
1
- export function forceFrag (startLevel: number, maxLevels: number): string {
2
- startLevel = Math.min(startLevel, maxLevels)
3
- const delta = maxLevels - startLevel
4
- const calcAdd = `
5
- float dist = sqrt(l);
6
- if (dist > 0.0) {
7
- float c = alpha * repulsion * centermass.b;
8
- addVelocity += calcAdd(vec2(x, y), l, c);
9
- addVelocity += addVelocity * random.rg;
10
- }
11
- `
12
- function quad (level: number): string {
13
- if (level >= maxLevels) {
14
- return calcAdd
15
- } else {
16
- const groupSize = Math.pow(2, level + 1)
17
-
18
- const iEnding = new Array(level + 1 - delta).fill(0).map((_, l) => `pow(2.0, ${level - (l + delta)}.0) * i${l + delta}`).join('+')
19
- const jEnding = new Array(level + 1 - delta).fill(0).map((_, l) => `pow(2.0, ${level - (l + delta)}.0) * j${l + delta}`).join('+')
20
-
21
- return `
22
- for (float ij${level} = 0.0; ij${level} < 4.0; ij${level} += 1.0) {
23
- float i${level} = 0.0;
24
- float j${level} = 0.0;
25
- if (ij${level} == 1.0 || ij${level} == 3.0) i${level} = 1.0;
26
- if (ij${level} == 2.0 || ij${level} == 3.0) j${level} = 1.0;
27
- float i = pow(2.0, ${startLevel}.0) * n / width${level + 1} + ${iEnding};
28
- float j = pow(2.0, ${startLevel}.0) * m / width${level + 1} + ${jEnding};
29
- float groupPosX = (i + 0.5) / ${groupSize}.0;
30
- float groupPosY = (j + 0.5) / ${groupSize}.0;
31
-
32
- vec4 centermass = texture2D(level[${level}], vec2(groupPosX, groupPosY));
33
- if (centermass.r > 0.0 && centermass.g > 0.0 && centermass.b > 0.0) {
34
- float x = centermass.r / centermass.b - pointPosition.r;
35
- float y = centermass.g / centermass.b - pointPosition.g;
36
- float l = x * x + y * y;
37
- if ((width${level + 1} * width${level + 1}) / theta < l) {
38
- ${calcAdd}
39
- } else {
40
- ${quad(level + 1)}
41
- }
42
- }
43
- }
44
- `
45
- }
46
- }
47
- return `
48
- #ifdef GL_ES
49
- precision highp float;
50
- #endif
51
-
52
- uniform sampler2D positionsTexture;
53
- uniform sampler2D randomValues;
54
- uniform float spaceSize;
55
- uniform float repulsion;
56
- uniform float theta;
57
- uniform float alpha;
58
- uniform sampler2D level[${maxLevels}];
59
- varying vec2 textureCoords;
60
-
61
- vec2 calcAdd(vec2 xy, float l, float c) {
62
- float distanceMin2 = 1.0;
63
- if (l < distanceMin2) l = sqrt(distanceMin2 * l);
64
- float add = c / l;
65
- return add * xy;
66
- }
67
-
68
- void main() {
69
- vec4 pointPosition = texture2D(positionsTexture, textureCoords);
70
- vec4 random = texture2D(randomValues, textureCoords);
71
-
72
- float width0 = spaceSize;
73
-
74
- vec2 velocity = vec2(0.0);
75
- vec2 addVelocity = vec2(0.0);
76
-
77
- ${new Array(maxLevels).fill(0).map((_, i) => `float width${i + 1} = width${i} / 2.0;`).join('\n')}
78
-
79
- for (float n = 0.0; n < pow(2.0, ${delta}.0); n += 1.0) {
80
- for (float m = 0.0; m < pow(2.0, ${delta}.0); m += 1.0) {
81
- ${quad(delta)}
82
- }
83
- }
84
-
85
- velocity -= addVelocity;
86
-
87
- gl_FragColor = vec4(velocity, 0.0, 0.0);
88
- }
89
- `
90
- }
@@ -1,9 +0,0 @@
1
- #ifdef GL_ES
2
- precision highp float;
3
- #endif
4
-
5
- varying vec4 rgba;
6
-
7
- void main() {
8
- gl_FragColor = rgba;
9
- }