-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Expand file tree
/
Copy pathcolumn-layer.ts
More file actions
457 lines (402 loc) · 11.4 KB
/
column-layer.ts
File metadata and controls
457 lines (402 loc) · 11.4 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
// deck.gl
// SPDX-License-Identifier: MIT
// Copyright (c) vis.gl contributors
import {
Layer,
project32,
picking,
UNIT,
LayerProps,
LayerDataSource,
UpdateParameters,
Unit,
AccessorFunction,
Position,
Accessor,
Color,
Material,
DefaultProps
} from '@deck.gl/core';
import {gouraudMaterial, phongMaterial} from '@luma.gl/shadertools';
import {Model} from '@luma.gl/engine';
import ColumnGeometry from './column-geometry';
import {columnUniforms, ColumnProps} from './column-layer-uniforms';
import {getColumnLayerWGSL as source} from './column-layer.wgsl';
import vs from './column-layer-vertex.glsl';
import fs from './column-layer-fragment.glsl';
const DEFAULT_COLOR = [0, 0, 0, 255] as const;
const defaultProps: DefaultProps<ColumnLayerProps> = {
diskResolution: {type: 'number', min: 4, value: 20},
vertices: null,
radius: {type: 'number', min: 0, value: 1000},
angle: {type: 'number', value: 0},
offset: {type: 'array', value: [0, 0]},
coverage: {type: 'number', min: 0, max: 1, value: 1},
elevationScale: {type: 'number', min: 0, value: 1},
radiusUnits: 'meters',
lineWidthUnits: 'meters',
lineWidthScale: 1,
lineWidthMinPixels: 0,
lineWidthMaxPixels: Number.MAX_SAFE_INTEGER,
extruded: true,
wireframe: false,
filled: true,
stroked: false,
flatShading: false,
getPosition: {type: 'accessor', value: (x: any) => x.position},
getFillColor: {type: 'accessor', value: DEFAULT_COLOR},
getLineColor: {type: 'accessor', value: DEFAULT_COLOR},
getLineWidth: {type: 'accessor', value: 1},
getElevation: {type: 'accessor', value: 1000},
material: true,
getColor: {deprecatedFor: ['getFillColor', 'getLineColor']}
};
/** All properties supported by ColumnLayer. */
export type ColumnLayerProps<DataT = unknown> = _ColumnLayerProps<DataT> & LayerProps;
/** Properties added by ColumnLayer. */
type _ColumnLayerProps<DataT> = {
data: LayerDataSource<DataT>;
/**
* The number of sides to render the disk as.
* @default 20
*/
diskResolution?: number;
/**
* isk size in units specified by `radiusUnits`.
* @default 1000
*/
radius?: number;
/**
* Disk rotation, counter-clockwise in degrees.
* @default 0
*/
angle?: number;
/**
* Replace the default geometry (regular polygon that fits inside the unit circle) with a custom one.
* @default null
*/
vertices?: Position[] | null;
/**
* Disk offset from the position, relative to the radius.
* @default [0,0]
*/
offset?: Readonly<[number, number]>;
/**
* Radius multiplier, between 0 - 1
* @default 1
*/
coverage?: number;
/**
* Column elevation multiplier.
* @default 1
*/
elevationScale?: number;
/**
* Whether to draw a filled column (solid fill).
* @default true
*/
filled?: boolean;
/**
* Whether to draw an outline around the disks.
* @default false
*/
stroked?: boolean;
/**
* Whether to extrude the columns. If set to `false`, all columns will be rendered as flat polygons.
* @default true
*/
extruded?: boolean;
/**
* Whether to generate a line wireframe of the column.
* @default false
*/
wireframe?: boolean;
/**
* If `true`, the vertical surfaces of the columns use [flat shading](https://en.wikipedia.org/wiki/Shading#Flat_vs._smooth_shading).
* @default false
*/
flatShading?: boolean;
/**
* The units of the radius.
* @default 'meters'
*/
radiusUnits?: Unit;
/**
* The units of the line width.
* @default 'meters'
*/
lineWidthUnits?: Unit;
/**
* The line width multiplier that multiplied to all outlines.
* @default 1
*/
lineWidthScale?: number;
/**
* The minimum outline width in pixels.
* @default 0
*/
lineWidthMinPixels?: number;
/**
* The maximum outline width in pixels.
* @default Number.MAX_SAFE_INTEGER
*/
lineWidthMaxPixels?: number;
/**
* Material settings for lighting effect. Applies if `extruded: true`.
*
* @default true
* @see https://deck.gl/docs/developer-guide/using-lighting
*/
material?: Material;
/**
* Method called to retrieve the position of each column.
* @default object => object.position
*/
getPosition?: AccessorFunction<DataT, Position>;
/**
* @deprecated Use getFilledColor and getLineColor instead
*/
getColor?: Accessor<DataT, Color>;
/**
* Fill collor value or accessor.
* @default [0, 0, 0, 255]
*/
getFillColor?: Accessor<DataT, Color>;
/**
* Line color value or accessor.
*
* @default [0, 0, 0, 255]
*/
getLineColor?: Accessor<DataT, Color>;
/**
* The elevation of each cell in meters.
* @default 1000
*/
getElevation?: Accessor<DataT, number>;
/**
* The width of the outline of the column, in units specified by `lineWidthUnits`.
*
* @default 1
*/
getLineWidth?: Accessor<DataT, number>;
};
/** Render extruded cylinders (tessellated regular polygons) at given coordinates. */
export default class ColumnLayer<DataT = any, ExtraPropsT extends {} = {}> extends Layer<
ExtraPropsT & Required<_ColumnLayerProps<DataT>>
> {
static layerName = 'ColumnLayer';
static defaultProps = defaultProps;
state!: {
fillModel?: Model;
wireframeModel?: Model;
models?: Model[];
fillVertexCount: number;
edgeDistance: number;
};
getShaders() {
const defines: Record<string, any> = {};
const {flatShading} = this.props;
if (flatShading) {
defines.FLAT_SHADING = 1;
}
return super.getShaders({
source: source(flatShading),
vs,
fs,
defines,
modules: [project32, flatShading ? phongMaterial : gouraudMaterial, picking, columnUniforms]
});
}
/**
* DeckGL calls initializeState when GL context is available
* Essentially a deferred constructor
*/
initializeState() {
const attributeManager = this.getAttributeManager()!;
/* eslint-disable max-len */
attributeManager.addInstanced({
instancePositions: {
size: 3,
type: 'float64',
fp64: this.use64bitPositions(),
transition: true,
accessor: 'getPosition'
},
instanceElevations: {
size: 1,
transition: true,
accessor: 'getElevation'
},
instanceFillColors: {
size: this.props.colorFormat.length,
type: 'unorm8',
transition: true,
accessor: 'getFillColor',
defaultValue: DEFAULT_COLOR
},
instanceLineColors: {
size: this.props.colorFormat.length,
type: 'unorm8',
transition: true,
accessor: 'getLineColor',
defaultValue: DEFAULT_COLOR
},
instanceStrokeWidths: {
size: 1,
accessor: 'getLineWidth',
transition: true
}
});
/* eslint-enable max-len */
}
updateState(params: UpdateParameters<this>): void {
super.updateState(params);
const {props, oldProps, changeFlags} = params;
const regenerateModels =
changeFlags.extensionsChanged || props.flatShading !== oldProps.flatShading;
if (regenerateModels) {
this.state.models?.forEach(model => model.destroy());
this.setState(this._getModels());
this.getAttributeManager()!.invalidateAll();
}
const instanceCount = this.getNumInstances();
this.state.fillModel!.setInstanceCount(instanceCount);
this.state.wireframeModel!.setInstanceCount(instanceCount);
if (
regenerateModels ||
props.diskResolution !== oldProps.diskResolution ||
props.vertices !== oldProps.vertices ||
(props.extruded || props.stroked) !== (oldProps.extruded || oldProps.stroked)
) {
this._updateGeometry(props);
}
}
getGeometry(diskResolution: number, vertices: number[] | undefined, hasThinkness: boolean) {
const geometry = new ColumnGeometry({
radius: 1,
height: hasThinkness ? 2 : 0,
vertices,
nradial: diskResolution
});
let meanVertexDistance = 0;
if (vertices) {
for (let i = 0; i < diskResolution; i++) {
const p = vertices[i];
const d = Math.sqrt(p[0] * p[0] + p[1] * p[1]);
meanVertexDistance += d / diskResolution;
}
} else {
meanVertexDistance = 1;
}
this.setState({
edgeDistance: Math.cos(Math.PI / diskResolution) * meanVertexDistance
});
return geometry;
}
protected _getModels() {
const shaders = this.getShaders();
const bufferLayout = this.getAttributeManager()!.getBufferLayouts();
const fillModel = new Model(this.context.device, {
...shaders,
id: `${this.props.id}-fill`,
bufferLayout,
isInstanced: true
});
const wireframeModel = new Model(this.context.device, {
...shaders,
id: `${this.props.id}-wireframe`,
bufferLayout,
isInstanced: true
});
return {
fillModel,
wireframeModel,
models: [wireframeModel, fillModel]
};
}
protected _updateGeometry({diskResolution, vertices, extruded, stroked}) {
const geometry = this.getGeometry(diskResolution, vertices, extruded || stroked);
this.setState({
fillVertexCount: geometry.attributes.POSITION.value.length / 3
});
const fillModel = this.state.fillModel!;
const wireframeModel = this.state.wireframeModel!;
fillModel.setGeometry(geometry);
fillModel.setTopology('triangle-strip');
// Disable indices
fillModel.setIndexBuffer(null);
wireframeModel.setGeometry(geometry);
wireframeModel.setTopology('line-list');
}
draw({uniforms}) {
const {
lineWidthUnits,
lineWidthScale,
lineWidthMinPixels,
lineWidthMaxPixels,
radiusUnits,
elevationScale,
extruded,
filled,
stroked,
wireframe,
offset,
coverage,
radius,
angle
} = this.props;
const fillModel = this.state.fillModel!;
const wireframeModel = this.state.wireframeModel!;
const {fillVertexCount, edgeDistance} = this.state;
const columnProps: Omit<ColumnProps, 'isStroke'> = {
radius,
angle: (angle / 180) * Math.PI,
offset,
extruded,
stroked,
coverage,
elevationScale,
edgeDistance,
radiusUnits: UNIT[radiusUnits],
widthUnits: UNIT[lineWidthUnits],
widthScale: lineWidthScale,
widthMinPixels: lineWidthMinPixels,
widthMaxPixels: lineWidthMaxPixels
};
// When drawing 3d: draw wireframe first so it doesn't get occluded by depth test
if (extruded && wireframe) {
wireframeModel.shaderInputs.setProps({
column: {
...columnProps,
isStroke: true
}
});
wireframeModel.draw(this.context.renderPass);
}
if (filled) {
// model.setProps({isIndexed: false});
fillModel.setVertexCount(fillVertexCount);
fillModel.shaderInputs.setProps({
column: {
...columnProps,
isStroke: false
}
});
fillModel.draw(this.context.renderPass);
}
// When drawing 2d: draw fill before stroke so that the outline is always on top
if (!extruded && stroked) {
// model.setProps({isIndexed: false});
// The width of the stroke is achieved by flattening the side of the cylinder.
// Skip the last 1/3 of the vertices which is the top.
fillModel.setVertexCount((fillVertexCount * 2) / 3);
fillModel.shaderInputs.setProps({
column: {
...columnProps,
isStroke: true
}
});
fillModel.draw(this.context.renderPass);
}
}
}