@cosmos.gl/deck-layers 3.5.0-beta.2 → 3.5.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/README.md CHANGED
@@ -10,7 +10,7 @@ Versions are released in lockstep with `@cosmos.gl/graph` — install matching v
10
10
  ## Install
11
11
 
12
12
  ```bash
13
- npm install @cosmos.gl/deck-layers @cosmos.gl/graph @deck.gl/core @luma.gl/core @luma.gl/engine
13
+ npm install @cosmos.gl/deck-layers @cosmos.gl/graph @deck.gl/core@~9.3.0 @luma.gl/core@~9.3.0 @luma.gl/engine@~9.3.0 @luma.gl/shadertools@~9.3.0 @luma.gl/webgl@~9.3.0
14
14
  ```
15
15
 
16
16
  `@cosmos.gl/graph`, `@deck.gl/core` and `@luma.gl/*` are peer dependencies: the whole
@@ -71,6 +71,12 @@ new CosmosGraphLayer({
71
71
  simulation at a low alpha so the graph responds around the moving point. View panning
72
72
  is suppressed only while a point is grabbed.
73
73
  - **Colors** follow the deck.gl convention: RGBA channels in 0..255.
74
+ - **Binary styling**: in binary mode, `points.attributes` and `links.attributes` take typed
75
+ arrays keyed by the accessor they replace — `getPointColor`, `getPointSize`, `getLinkColor`,
76
+ `getLinkWidth` — and deck uploads them as they are. Links then take the form
77
+ `{ pairs, attributes }`; a bare pair array is `{ pairs }`. A new `points` or `links` object over
78
+ the same positions or pairs restyles in place: the simulation is reloaded only when the
79
+ positions or the pairs themselves change.
74
80
  - **Simulation control**: pass forces and callbacks through `simulationConfig`
75
81
  (`GraphSimulationConfig` from `@cosmos.gl/graph`); take the wheel through
76
82
  `onSimulationCreated`.
@@ -78,50 +84,64 @@ new CosmosGraphLayer({
78
84
  Live examples: the *Integrations* section of the
79
85
  [cosmos.gl Storybook](https://cosmosgl.github.io/graph).
80
86
 
81
- ## The app-owned tier: primitive layers
87
+ ## Bring your own simulation
82
88
 
83
- `CosmosPointsLayer` and `CosmosLinksLayer` are the pure renderers underneath the
84
- composite, for full control: own the `GraphSimulation`, decide when it steps, share one
85
- simulation across visualizations, or compose your own layers between points and links.
86
- Anything exposing `getPointPositionTexture()` (the `PositionTextureSource` type)
87
- can feed them.
89
+ Pass a `GraphSimulation` you created through `simulation` when the application needs to
90
+ own it: create it before the layer exists, keep it across layer removals, or drive it
91
+ from elsewhere in the app. It must run on deck's device. The layer steps it from deck's
92
+ timeline and renders it, but never configures or destroys it. `simulationConfig` and
93
+ `onSimulationCreated` apply only to a simulation the layer creates.
94
+
95
+ `points` decides who loads the data. Leave it out and the layer draws whatever the
96
+ simulation holds and follows its changes: load the simulation yourself, before or after
97
+ the layer exists, and the layout survives removing and re-adding the layer. Give the
98
+ layer `points` (and `links`) and it loads them into the simulation, over whatever it
99
+ held, once the device check has passed.
88
100
 
89
101
  ```js
90
102
  let deck
91
103
  const devicePromise = new Promise((resolve) => {
92
- deck = new Deck({ /* … */, _animate: true, onDeviceInitialized: resolve, layers: [] })
104
+ deck = new Deck({ /* … */, onDeviceInitialized: resolve, layers: [] })
93
105
  })
94
106
 
95
- const simulation = new GraphSimulation(config, devicePromise) // deck's device, never destroyed by cosmos
107
+ // deck's device, never destroyed by cosmos; the application loads it
108
+ const simulation = new GraphSimulation(config, devicePromise)
96
109
  simulation.setPointPositions(positions)
97
110
  simulation.setLinks(links)
98
111
  simulation.applyData()
99
- await simulation.ready
100
112
 
101
113
  deck.setProps({
102
- onBeforeRender: () => { if (simulation.isSimulationRunning) simulation.step() },
103
- layers: [
104
- new CosmosLinksLayer({
105
- id: 'links',
106
- graph: simulation,
107
- // the cosmos pair array read as two interleaved binary attributes — no copy
108
- data: {
109
- length: links.length / 2,
110
- attributes: {
111
- getLinkSource: { value: links, size: 1, stride: 8 },
112
- getLinkTarget: { value: links, size: 1, offset: 4, stride: 8 },
113
- },
114
- },
115
- }),
116
- new CosmosPointsLayer({ id: 'points', graph: simulation, data: { length: pointCount } }),
117
- ],
114
+ // no `points`: the layer draws the simulation as loaded
115
+ layers: [new CosmosGraphLayer({ id: 'graph', simulation, pickable: true })],
118
116
  })
119
117
 
118
+ // Take over stepping: pause it, step it yourself, then ask deck to repaint
119
+ simulation.pause()
120
+ simulation.step()
121
+ deck.redraw()
122
+
120
123
  // Teardown order matters: the device belongs to deck.
121
124
  simulation.destroy()
122
125
  deck.finalize()
123
126
  ```
124
127
 
128
+ Several layers can draw one simulation, say a main view and a differently styled
129
+ minimap: it steps once per frame however many layers draw it, as long as at most one of
130
+ them carries `points`. Loading new data never reheats: call `simulation.start()` after
131
+ `applyData()` when the simulation had settled.
132
+
133
+ ## Your own renderer: `PositionTextureSource`
134
+
135
+ The layer's points and links are internal sublayers that `texelFetch` the live position
136
+ texture. To draw it some other way (your own deck layer, a Three.js material, a MapLibre
137
+ custom layer), type your input against `PositionTextureSource` from `@cosmos.gl/graph`.
138
+ `Graph` and `GraphSimulation` both implement it. `getPointPositionTexture()` returns a
139
+ `PointPositionTexture`: point `i` lives at texel `(i % textureSize, floor(i / textureSize))`
140
+ as `[x, y, i, unused]`, an absent point reads as NaN, and the handle changes as the
141
+ simulation ping-pongs, so re-fetch it whenever `version` changes. The sublayer sources
142
+ ([points](src/cosmos-points-layer.ts), [links](src/cosmos-links-layer.ts)) are a worked
143
+ reference.
144
+
125
145
  ## The universal fallback: CPU readback
126
146
 
127
147
  When you need stock deck layers (attribute transitions, extensions, text at positions),
@@ -1,33 +1,61 @@
1
- import { CompositeLayer, Accessor, Color, DefaultProps, GetPickingInfoParams, Layer, LayerContext, LayerProps, PickingInfo, Unit, UpdateParameters } from '@deck.gl/core';
1
+ import { CompositeLayer, Accessor, BinaryAttribute, Color, DefaultProps, GetPickingInfoParams, Layer, LayerContext, LayerProps, PickingInfo, Unit, UpdateParameters } from '@deck.gl/core';
2
2
  import { GraphSimulation, GraphSimulationConfig } from '@cosmos.gl/graph';
3
+ /**
4
+ * Binary styling channels for binary points, keyed by the accessor each one
5
+ * replaces: deck uploads the typed array as the instanced attribute instead of
6
+ * calling the accessor per point. Colors are RGBA bytes (0..255), 4 per point;
7
+ * sizes are one diameter per point, in `pointSizeUnits`.
8
+ */
9
+ export type CosmosPointAttributes = {
10
+ getPointColor?: BinaryAttribute;
11
+ getPointSize?: BinaryAttribute;
12
+ };
3
13
  /**
4
14
  * Points input: an array to run accessors over (picking returns the original
5
15
  * objects), or the cosmos-native binary form — a point count plus optional
6
- * `[x0, y0, x1, y1, …]` initial positions. Points without given positions are
7
- * seeded randomly inside the simulation space.
16
+ * `[x0, y0, x1, y1, …]` initial positions and binary styling channels. Points
17
+ * without given positions are seeded randomly inside the simulation space.
8
18
  */
9
19
  export type CosmosGraphPoints<PointDataT> = readonly PointDataT[] | {
10
20
  length: number;
11
21
  initialPositions?: Float32Array;
12
- /** Optional binary styling channels, keyed by accessor name (`getPointColor`, `getPointSize`). */
13
- attributes?: Record<string, unknown>;
22
+ attributes?: CosmosPointAttributes;
23
+ };
24
+ /**
25
+ * Binary styling channels for binary links, keyed by the accessor each one
26
+ * replaces. Colors are RGBA bytes (0..255), 4 per link; widths are one per
27
+ * link, in `linkWidthUnits`.
28
+ */
29
+ export type CosmosLinkAttributes = {
30
+ getLinkColor?: BinaryAttribute;
31
+ getLinkWidth?: BinaryAttribute;
14
32
  };
15
33
  /**
16
- * Links input: an array to run accessors over, or the cosmos-native
17
- * `[source0, target0, source1, target1, …]` array of point indices.
18
- * An array link whose source or target does not resolve to a point is dropped
19
- * from both the simulation and the rendering, with one warning per data change.
34
+ * Links input: an array to run accessors over, the cosmos-native
35
+ * `[source0, target0, source1, target1, …]` array of point indices, or that
36
+ * array as `pairs` with binary styling channels beside it. An array link whose
37
+ * source or target does not resolve to a point is dropped from both the
38
+ * simulation and the rendering, with one warning per data change; a pair that
39
+ * names no point is skipped by both and keeps its index.
20
40
  */
21
- export type CosmosGraphLinks<LinkDataT> = readonly LinkDataT[] | Float32Array;
41
+ export type CosmosGraphLinks<LinkDataT> = readonly LinkDataT[] | Float32Array | {
42
+ pairs: Float32Array;
43
+ attributes?: CosmosLinkAttributes;
44
+ };
22
45
  /** Picking info from a CosmosGraphLayer: which kind of element was hit. */
23
46
  export type CosmosGraphPickingInfo = PickingInfo & {
24
47
  elementType?: 'point' | 'link';
25
48
  };
26
49
  export type CosmosGraphLayerProps<PointDataT = unknown, LinkDataT = unknown> = CosmosGraphLayerOwnProps<PointDataT, LinkDataT> & LayerProps;
27
50
  type CosmosGraphLayerOwnProps<PointDataT, LinkDataT> = {
28
- /** One entry per point — an array of your objects, or `{ length, initialPositions? }`. */
29
- points: CosmosGraphPoints<PointDataT>;
30
- /** One entry per link — an array of your objects, or a flat point-index pair array. */
51
+ /**
52
+ * One entry per point — an array of your objects, or `{ length, initialPositions?, attributes? }`.
53
+ * The layer loads them, with `links`, into the simulation. Omit it to draw the data the
54
+ * simulation already holds instead: the layer then writes nothing into the simulation and
55
+ * follows its point count and links — the mode for an application-loaded `simulation`.
56
+ */
57
+ points?: CosmosGraphPoints<PointDataT> | null;
58
+ /** One entry per link — an array of your objects, or a flat point-index pair array. Ignored without `points`. */
31
59
  links?: CosmosGraphLinks<LinkDataT> | null;
32
60
  /**
33
61
  * Stable point id accessor. When provided (with array points), link
@@ -102,11 +130,32 @@ type CosmosGraphLayerOwnProps<PointDataT, LinkDataT> = {
102
130
  onPointDrag?: ((info: CosmosGraphPickingInfo) => void) | null;
103
131
  /** Called when a point drag ends. */
104
132
  onPointDragEnd?: ((info: CosmosGraphPickingInfo) => void) | null;
105
- /** Simulation configuration, passed through to `GraphSimulation` (forces, spaceSize, callbacks). */
133
+ /**
134
+ * An application-owned `GraphSimulation` to render instead of creating one.
135
+ * It must run on deck's device — construct it with the device from deck's
136
+ * `onDeviceInitialized`. The layer steps it while it runs (`pause()` it to
137
+ * take over stepping, then `deck.redraw()` after each manual `step()`) and
138
+ * renders it; it never configures or destroys it — `simulationConfig` and
139
+ * `onSimulationCreated` apply only to a layer-created simulation, and
140
+ * teardown is the application's, before `deck.finalize()`.
141
+ *
142
+ * `points` decides who loads the data. Given, the layer loads `points` and
143
+ * `links` into the simulation once the device check has passed, over whatever
144
+ * it held. Omitted, the layer draws the simulation's own data and follows its
145
+ * changes. Several layers may draw one simulation — it steps once per frame
146
+ * however many draw it — as long as at most one of them loads data.
147
+ */
148
+ simulation?: GraphSimulation | null;
149
+ /**
150
+ * Simulation configuration, passed through to the layer-created
151
+ * `GraphSimulation` (forces, spaceSize, callbacks). Ignored when
152
+ * `simulation` is provided.
153
+ */
106
154
  simulationConfig?: GraphSimulationConfig;
107
155
  /**
108
- * Called once with the layer-owned `GraphSimulation` — the escape hatch for
109
- * advanced control (pause, pinning, sparse position writes).
156
+ * Called once with the layer-created `GraphSimulation` — the escape hatch for
157
+ * advanced control (pause, pinning, sparse position writes). Not called for
158
+ * a provided `simulation`.
110
159
  */
111
160
  onSimulationCreated?: ((simulation: GraphSimulation) => void) | null;
112
161
  };
@@ -114,33 +163,57 @@ type CosmosGraphLayerOwnProps<PointDataT, LinkDataT> = {
114
163
  type DragGestureEvent = {
115
164
  stopImmediatePropagation?: () => void;
116
165
  };
166
+ /** The endpoint channels of the links sublayer, read straight from the cosmos pair array. */
167
+ type LinkEndpointAttributes = {
168
+ getLinkSource: BinaryAttribute;
169
+ getLinkTarget: BinaryAttribute;
170
+ };
171
+ type BinaryLinksData = {
172
+ length: number;
173
+ attributes: LinkEndpointAttributes & CosmosLinkAttributes;
174
+ };
117
175
  /**
118
- * The batteries-included cosmos.gl graph layer: give it points and links, and
119
- * it owns the rest. The layer creates a `GraphSimulation` on deck's device,
120
- * ingests the data, advances the simulation once per animation frame from
121
- * deck's shared timeline while it runs (deck goes idle when it settles — no
122
- * `_animate` required), renders through `CosmosLinksLayer` + `CosmosPointsLayer`
123
- * (positions stay GPU-resident), and destroys the simulation when the layer is
124
- * removed. Picking reports `elementType: 'point' | 'link'` alongside the index
125
- * and, for array data, the original object.
176
+ * The cosmos.gl graph layer: give it points and links, and it handles the
177
+ * rest. The layer creates a `GraphSimulation` on deck's device (or renders the
178
+ * application's, via `simulation`), loads the data — or, without `points`,
179
+ * follows the data the simulation holds — advances the simulation once per
180
+ * animation frame from deck's shared timeline while it runs (deck goes idle
181
+ * when it settles — no `_animate` required), renders through its internal
182
+ * links and points sublayers (positions stay GPU-resident), and destroys a
183
+ * simulation it created when the layer is removed. Picking reports
184
+ * `elementType: 'point' | 'link'` alongside the index and, for array data, the
185
+ * original object.
126
186
  */
127
187
  export declare class CosmosGraphLayer<PointDataT = unknown, LinkDataT = unknown> extends CompositeLayer<Required<CosmosGraphLayerOwnProps<PointDataT, LinkDataT>>> {
128
188
  static layerName: string;
129
189
  static defaultProps: DefaultProps<CosmosGraphLayerProps<unknown, unknown>>;
130
190
  state: {
131
191
  simulation?: GraphSimulation;
192
+ /** The `simulation` prop this state was built from; `null` when the layer created its own. */
193
+ providedSimulation: GraphSimulation | null;
132
194
  isReady: boolean;
195
+ /** `points` changed before the simulation was ready: load them once it is. */
196
+ isIngestPending: boolean;
133
197
  pointCount: number;
134
198
  pointsData: readonly PointDataT[] | {
135
199
  length: number;
136
- attributes?: Record<string, unknown>;
200
+ attributes?: CosmosPointAttributes;
137
201
  };
138
- linksData: readonly LinkDataT[] | {
139
- length: number;
140
- attributes: Record<string, unknown>;
141
- } | null;
202
+ linksData: readonly LinkDataT[] | BinaryLinksData | null;
142
203
  /** Array links only: the resolved `[source, target]` point-index pairs, one per entry of `linksData`. */
143
204
  linkIndices: Float32Array | null;
205
+ /**
206
+ * Load mode: what the simulation holds, so a channel reloads only when its own
207
+ * input changes and a restyle leaves the layout alone. `loadedPositions` is the
208
+ * `initialPositions` array, `SEEDED`, or the points array; `loadedPairs` the pair
209
+ * array sent last; `idToIndex` the id map of the loaded points.
210
+ */
211
+ hasLoaded: boolean;
212
+ loadedPositions?: unknown;
213
+ loadedPairs?: Float32Array | null;
214
+ idToIndex?: Map<string | number, number> | null;
215
+ /** Without `points`: the simulation's links array the render data was built from. */
216
+ followedLinks?: Float32Array | null;
144
217
  draggedPointIndex: number | null;
145
218
  animationHandle?: number;
146
219
  };
@@ -161,8 +234,27 @@ export declare class CosmosGraphLayer<PointDataT = unknown, LinkDataT = unknown>
161
234
  * and clear the other, or hovering point N would also tint link N.
162
235
  */
163
236
  protected _updateAutoHighlight(info: PickingInfo): void;
237
+ /** Adopts the provided simulation, or creates one on deck's device when `provided` is `null`. */
238
+ private _attachSimulation;
239
+ /** Destroys a layer-created simulation; a provided one is left to the application. */
240
+ private _releaseSimulation;
241
+ /** The simulation's space size — authoritative for a provided simulation too. */
242
+ private _spaceSize;
164
243
  private _onTimelineTick;
244
+ /**
245
+ * Without `points`: builds the render data from what the simulation holds —
246
+ * its point count and links — and rebuilds it when either changes. Runs every
247
+ * tick, so it only compares until something differs.
248
+ */
249
+ private _syncFromSimulation;
165
250
  private _dragCoordinate;
251
+ /**
252
+ * Load mode: puts the layer's `points` and `links` into the simulation and
253
+ * builds the render data from them. Each channel is sent only when its own
254
+ * input changed — a new `points` or `links` object with the same positions
255
+ * or pairs restyles in place and leaves the layout alone. A changed id map
256
+ * re-resolves array links without touching the positions.
257
+ */
166
258
  private _updateSimulationData;
167
259
  }
168
260
  export {};
@@ -5,14 +5,16 @@ type CosmosLinksBindingProps = {
5
5
  type CosmosLinksUniformProps = {
6
6
  pointsTextureSize: number;
7
7
  widthUnits: number;
8
+ pointCount: number;
8
9
  };
9
10
  export type CosmosLinksProps = CosmosLinksBindingProps & CosmosLinksUniformProps;
10
11
  export declare const cosmosLinksUniforms: {
11
12
  readonly name: "cosmosLinks";
12
- readonly vs: "layout(std140) uniform cosmosLinksUniforms {\n float pointsTextureSize;\n highp int widthUnits;\n} cosmosLinks;\n";
13
+ readonly vs: "layout(std140) uniform cosmosLinksUniforms {\n float pointsTextureSize;\n highp int widthUnits;\n float pointCount;\n} cosmosLinks;\n";
13
14
  readonly uniformTypes: {
14
15
  readonly pointsTextureSize: "f32";
15
16
  readonly widthUnits: "i32";
17
+ readonly pointCount: "f32";
16
18
  };
17
19
  };
18
20
  export {};
@@ -1,6 +1,6 @@
1
1
  import { Layer, Accessor, Color, DefaultProps, LayerDataSource, LayerProps, Unit, UpdateParameters } from '@deck.gl/core';
2
2
  import { Model } from '@luma.gl/engine';
3
- import { PositionTextureSource } from './types';
3
+ import { PositionTextureSource } from '@cosmos.gl/graph';
4
4
  export type CosmosLinksLayerProps<DataT = unknown> = CosmosLinksLayerOwnProps<DataT> & LayerProps;
5
5
  type CosmosLinksLayerOwnProps<DataT> = {
6
6
  /**
@@ -47,6 +47,11 @@ type CosmosLinksLayerOwnProps<DataT> = {
47
47
  * updates. Endpoint indices, color and width are ordinary deck instanced
48
48
  * attributes, and picking works out of the box: the instance index is the
49
49
  * link index.
50
+ *
51
+ * Internal: the links sublayer of `CosmosGraphLayer`, not a package export —
52
+ * the composite resolves array links to point indices before they reach this
53
+ * layer, and a pair that names no point collapses in the shader, keeping its
54
+ * index.
50
55
  */
51
56
  export declare class CosmosLinksLayer<DataT = unknown> extends Layer<Required<CosmosLinksLayerOwnProps<DataT>>> {
52
57
  static layerName: string;
@@ -1,6 +1,6 @@
1
1
  import { Layer, Accessor, Color, DefaultProps, LayerDataSource, LayerProps, Unit, UpdateParameters } from '@deck.gl/core';
2
2
  import { Model } from '@luma.gl/engine';
3
- import { PositionTextureSource } from './types';
3
+ import { PositionTextureSource } from '@cosmos.gl/graph';
4
4
  export type CosmosPointsLayerProps<DataT = unknown> = CosmosPointsLayerOwnProps<DataT> & LayerProps;
5
5
  type CosmosPointsLayerOwnProps<DataT> = {
6
6
  /**
@@ -33,6 +33,9 @@ type CosmosPointsLayerOwnProps<DataT> = {
33
33
  * attribute, no CPU copy, no per-frame attribute updates. Color and size are
34
34
  * ordinary deck instanced attributes, and picking works out of the box: the
35
35
  * instance index is the point index.
36
+ *
37
+ * Internal: the points sublayer of `CosmosGraphLayer`, not a package export —
38
+ * the composite keeps `data` aligned with the simulation's point index space.
36
39
  */
37
40
  export declare class CosmosPointsLayer<DataT = unknown> extends Layer<Required<CosmosPointsLayerOwnProps<DataT>>> {
38
41
  static layerName: string;
package/dist/index.d.ts CHANGED
@@ -1,11 +1,8 @@
1
1
  /**
2
- * deck.gl layers that render a cosmos.gl simulation with **zero position
3
- * readback**: each draw samples the live GPU position texture exposed by
4
- * `graph.getPointPositionTexture()` with `texelFetch`, so point coordinates
5
- * never leave the GPU. The layers share deck.gl's device with the cosmos.gl
6
- * simulation, which the application owns and steps.
2
+ * A deck.gl layer that renders a cosmos.gl simulation with **zero position
3
+ * readback**: each draw samples the live GPU position texture with
4
+ * `texelFetch`, so point coordinates never leave the GPU. The simulation runs
5
+ * on deck.gl's own device — created and owned by the layer, or supplied by the
6
+ * application through the `simulation` prop.
7
7
  */
8
- export { CosmosGraphLayer, type CosmosGraphLayerProps, type CosmosGraphPoints, type CosmosGraphLinks, type CosmosGraphPickingInfo, } from './cosmos-graph-layer';
9
- export { CosmosPointsLayer, type CosmosPointsLayerProps } from './cosmos-points-layer';
10
- export { CosmosLinksLayer, type CosmosLinksLayerProps } from './cosmos-links-layer';
11
- export type { PositionTextureSource } from './types';
8
+ export { CosmosGraphLayer, type CosmosGraphLayerProps, type CosmosGraphPoints, type CosmosPointAttributes, type CosmosGraphLinks, type CosmosLinkAttributes, type CosmosGraphPickingInfo, } from './cosmos-graph-layer';