-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
deck-picker.ts
567 lines (503 loc) · 16.5 KB
/
deck-picker.ts
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
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
// Copyright (c) 2015 - 2017 Uber Technologies, Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
import type {Device} from '@luma.gl/core';
import PickLayersPass, {PickingColorDecoder} from '../passes/pick-layers-pass';
import {getClosestObject, getUniqueObjects, PickedPixel} from './picking/query-object';
import {
processPickInfo,
getLayerPickingInfo,
getEmptyPickingInfo,
PickingInfo
} from './picking/pick-info';
import type {Framebuffer} from '@luma.gl/core';
import type {FilterContext, Rect} from '../passes/layers-pass';
import type Layer from './layer';
import type {Effect} from './effect';
import type View from '../views/view';
import type Viewport from '../viewports/viewport';
export type PickByPointOptions = {
x: number;
y: number;
radius?: number;
depth?: number;
mode?: string;
unproject3D?: boolean;
};
export type PickByRectOptions = {
x: number;
y: number;
width?: number;
height?: number;
mode?: string;
maxObjects?: number | null;
};
type PickOperationContext = {
layers: Layer[];
views: Record<string, View>;
viewports: Viewport[];
onViewportActive: (viewport: Viewport) => void;
effects: Effect[];
};
/** Manages picking in a Deck context */
export default class DeckPicker {
device: Device;
pickingFBO?: Framebuffer;
depthFBO?: Framebuffer;
pickLayersPass: PickLayersPass;
layerFilter?: (context: FilterContext) => boolean;
/** Identifiers of the previously picked object, for callback tracking and auto highlight */
lastPickedInfo: {
index: number;
layerId: string | null;
info: PickingInfo | null;
};
_pickable: boolean = true;
constructor(device: Device) {
this.device = device;
this.pickLayersPass = new PickLayersPass(device);
this.lastPickedInfo = {
index: -1,
layerId: null,
info: null
};
}
setProps(props: any): void {
if ('layerFilter' in props) {
this.layerFilter = props.layerFilter;
}
if ('_pickable' in props) {
this._pickable = props._pickable;
}
}
finalize() {
if (this.pickingFBO) {
this.pickingFBO.destroy();
}
if (this.depthFBO) {
this.depthFBO.destroy();
}
}
/** Pick the closest info at given coordinate */
pickObject(opts: PickByPointOptions & PickOperationContext) {
return this._pickClosestObject(opts);
}
/** Get all unique infos within a bounding box */
pickObjects(opts: PickByRectOptions & PickOperationContext) {
return this._pickVisibleObjects(opts);
}
// Returns a new picking info object by assuming the last picked object is still picked
getLastPickedObject({x, y, layers, viewports}, lastPickedInfo = this.lastPickedInfo.info) {
const lastPickedLayerId = lastPickedInfo && lastPickedInfo.layer && lastPickedInfo.layer.id;
const lastPickedViewportId =
lastPickedInfo && lastPickedInfo.viewport && lastPickedInfo.viewport.id;
const layer = lastPickedLayerId ? layers.find(l => l.id === lastPickedLayerId) : null;
const viewport =
(lastPickedViewportId && viewports.find(v => v.id === lastPickedViewportId)) || viewports[0];
const coordinate = viewport && viewport.unproject([x - viewport.x, y - viewport.y]);
const info = {
x,
y,
viewport,
coordinate,
layer
};
return {...lastPickedInfo, ...info};
}
// Private
/** Ensures that picking framebuffer exists and matches the canvas size */
_resizeBuffer() {
// Create a frame buffer if not already available
if (!this.pickingFBO) {
this.pickingFBO = this.device.createFramebuffer({
colorAttachments: ['rgba8unorm'],
depthStencilAttachment: 'depth16unorm'
});
if (this.device.isTextureFormatRenderable('rgba32float')) {
const depthFBO = this.device.createFramebuffer({
colorAttachments: ['rgba32float'],
depthStencilAttachment: 'depth16unorm'
});
this.depthFBO = depthFBO;
}
}
// Resize it to current canvas size (this is a noop if size hasn't changed)
// @ts-expect-error
const gl = this.device.gl as WebGL2RenderingContext;
this.pickingFBO?.resize({width: gl.canvas.width, height: gl.canvas.height});
this.depthFBO?.resize({width: gl.canvas.width, height: gl.canvas.height});
}
/** Preliminary filtering of the layers list. Skid picking pass if no layer is pickable. */
_getPickable(layers: Layer[]): Layer[] | null {
if (this._pickable === false) {
return null;
}
const pickableLayers = layers.filter(
layer => this.pickLayersPass.shouldDrawLayer(layer) && !layer.isComposite
);
return pickableLayers.length ? pickableLayers : null;
}
/** Pick the closest object at the given coordinate */
// eslint-disable-next-line max-statements,complexity
_pickClosestObject({
layers,
views,
viewports,
x,
y,
radius = 0,
depth = 1,
mode = 'query',
unproject3D,
onViewportActive,
effects
}: PickByPointOptions & PickOperationContext): {
result: PickingInfo[];
emptyInfo: PickingInfo;
} {
// @ts-expect-error TODO - assuming WebGL context
const pixelRatio = this.device.canvasContext.cssToDeviceRatio();
const pickableLayers = this._getPickable(layers);
if (!pickableLayers || viewports.length === 0) {
return {
result: [],
emptyInfo: getEmptyPickingInfo({viewports, x, y, pixelRatio})
};
}
this._resizeBuffer();
// Convert from canvas top-left to WebGL bottom-left coordinates
// Top-left coordinates [x, y] to bottom-left coordinates [deviceX, deviceY]
// And compensate for pixelRatio
// @ts-expect-error TODO - assuming WebGL context
const devicePixelRange = this.device.canvasContext.cssToDevicePixels([x, y], true);
const devicePixel = [
devicePixelRange.x + Math.floor(devicePixelRange.width / 2),
devicePixelRange.y + Math.floor(devicePixelRange.height / 2)
];
const deviceRadius = Math.round(radius * pixelRatio);
const {width, height} = this.pickingFBO as Framebuffer;
const deviceRect = this._getPickingRect({
deviceX: devicePixel[0],
deviceY: devicePixel[1],
deviceRadius,
deviceWidth: width,
deviceHeight: height
});
const cullRect: Rect = {
x: x - radius,
y: y - radius,
width: radius * 2 + 1,
height: radius * 2 + 1
};
let infos: Map<string | null, PickingInfo>;
const result: PickingInfo[] = [];
const affectedLayers = new Set<Layer>();
for (let i = 0; i < depth; i++) {
let pickInfo: PickedPixel;
if (deviceRect) {
const pickedResult = this._drawAndSample({
layers: pickableLayers,
views,
viewports,
onViewportActive,
deviceRect,
cullRect,
effects,
pass: `picking:${mode}`
});
pickInfo = getClosestObject({
...pickedResult,
deviceX: devicePixel[0],
deviceY: devicePixel[1],
deviceRadius,
deviceRect
});
} else {
pickInfo = {
pickedColor: null,
pickedObjectIndex: -1
};
}
let z;
if (pickInfo.pickedLayer && unproject3D && this.depthFBO) {
const {pickedColors: pickedColors2} = this._drawAndSample(
{
layers: [pickInfo.pickedLayer],
views,
viewports,
onViewportActive,
deviceRect: {
x: pickInfo.pickedX as number,
y: pickInfo.pickedY as number,
width: 1,
height: 1
},
cullRect,
effects,
pass: `picking:${mode}:z`
},
true
);
// picked value is in common space (pixels) from the camera target (viewport.position)
// convert it to meters from the ground
if (pickedColors2[3]) {
z = pickedColors2[0];
}
}
// Only exclude if we need to run picking again.
// We need to run picking again if an object is detected AND
// we have not exhausted the requested depth.
if (pickInfo.pickedLayer && i + 1 < depth) {
affectedLayers.add(pickInfo.pickedLayer);
pickInfo.pickedLayer.disablePickingIndex(pickInfo.pickedObjectIndex);
}
// This logic needs to run even if no object is picked.
infos = processPickInfo({
pickInfo,
lastPickedInfo: this.lastPickedInfo,
mode,
layers: pickableLayers,
viewports,
x,
y,
z,
pixelRatio
});
for (const info of infos.values()) {
if (info.layer) {
result.push(info);
}
}
// If no object is picked stop.
if (!pickInfo.pickedColor) {
break;
}
}
// reset only affected buffers
for (const layer of affectedLayers) {
layer.restorePickingColors();
}
return {result, emptyInfo: infos!.get(null) as PickingInfo};
}
/** Pick all objects within the given bounding box */
_pickVisibleObjects({
layers,
views,
viewports,
x,
y,
width = 1,
height = 1,
mode = 'query',
maxObjects = null,
onViewportActive,
effects
}: PickByRectOptions & PickOperationContext): PickingInfo[] {
const pickableLayers = this._getPickable(layers);
if (!pickableLayers || viewports.length === 0) {
return [];
}
this._resizeBuffer();
// Convert from canvas top-left to WebGL bottom-left coordinates
// And compensate for pixelRatio
// @ts-expect-error TODO - assuming WebGL context
const pixelRatio = this.device.canvasContext.cssToDeviceRatio();
// @ts-expect-error TODO - assuming WebGL context
const leftTop = this.device.canvasContext.cssToDevicePixels([x, y], true);
// take left and top (y inverted in device pixels) from start location
const deviceLeft = leftTop.x;
const deviceTop = leftTop.y + leftTop.height;
// take right and bottom (y inverted in device pixels) from end location
// @ts-expect-error TODO - assuming WebGL context
const rightBottom = this.device.canvasContext.cssToDevicePixels([x + width, y + height], true);
const deviceRight = rightBottom.x + rightBottom.width;
const deviceBottom = rightBottom.y;
const deviceRect = {
x: deviceLeft,
y: deviceBottom,
// deviceTop and deviceRight represent the first pixel outside the desired rect
width: deviceRight - deviceLeft,
height: deviceTop - deviceBottom
};
const pickedResult = this._drawAndSample({
layers: pickableLayers,
views,
viewports,
onViewportActive,
deviceRect,
cullRect: {x, y, width, height},
effects,
pass: `picking:${mode}`
});
const pickInfos = getUniqueObjects(pickedResult);
// `getUniqueObjects` dedup by picked color
// However different picked color may be linked to the same picked object, e.g. stroke and fill of the same polygon
// picked from different sub layers of a GeoJsonLayer
// Here after resolving the picked index with `layer.getPickingInfo`, we need to dedup again by unique picked objects
const uniquePickedObjects = new Map<string, Set<unknown>>();
const uniqueInfos: PickingInfo[] = [];
const limitMaxObjects = Number.isFinite(maxObjects);
for (let i = 0; i < pickInfos.length; i++) {
if (limitMaxObjects && uniqueInfos.length >= maxObjects!) {
break;
}
const pickInfo = pickInfos[i];
let info: PickingInfo = {
color: pickInfo.pickedColor,
layer: null,
index: pickInfo.pickedObjectIndex,
picked: true,
x,
y,
pixelRatio
};
info = getLayerPickingInfo({layer: pickInfo.pickedLayer as Layer, info, mode});
// info.layer is always populated because it's a picked pixel
const pickedLayerId = info.layer!.id;
if (!uniquePickedObjects.has(pickedLayerId)) {
uniquePickedObjects.set(pickedLayerId, new Set<unknown>());
}
const uniqueObjectsInLayer = uniquePickedObjects.get(pickedLayerId) as Set<unknown>;
// info.object may be null if the layer is using non-iterable data.
// Fall back to using index as identifier.
const pickedObjectKey = info.object ?? info.index;
if (!uniqueObjectsInLayer.has(pickedObjectKey)) {
uniqueObjectsInLayer.add(pickedObjectKey);
uniqueInfos.push(info);
}
}
return uniqueInfos;
}
/** Renders layers into the picking buffer with picking colors and read the pixels. */
_drawAndSample(params: {
deviceRect: Rect;
pass: string;
layers: Layer[];
views: Record<string, View>;
viewports: Viewport[];
onViewportActive: (viewport: Viewport) => void;
cullRect?: Rect;
effects: Effect[];
}): {
pickedColors: Uint8Array;
decodePickingColor: PickingColorDecoder;
};
/** Renders layers into the picking buffer with encoded z values and read the pixels. */
_drawAndSample(
params: {
deviceRect: Rect;
pass: string;
layers: Layer[];
views: Record<string, View>;
viewports: Viewport[];
onViewportActive: (viewport: Viewport) => void;
cullRect?: Rect;
effects: Effect[];
},
pickZ: true
): {
pickedColors: Float32Array;
decodePickingColor: null;
};
_drawAndSample(
{
layers,
views,
viewports,
onViewportActive,
deviceRect,
cullRect,
effects,
pass
}: {
deviceRect: Rect;
pass: string;
layers: Layer[];
views: Record<string, View>;
viewports: Viewport[];
onViewportActive: (viewport: Viewport) => void;
cullRect?: Rect;
effects: Effect[];
},
pickZ: boolean = false
): {
pickedColors: Uint8Array | Float32Array;
decodePickingColor: PickingColorDecoder | null;
} {
const pickingFBO = pickZ ? this.depthFBO : this.pickingFBO;
const opts = {
layers,
layerFilter: this.layerFilter,
views,
viewports,
onViewportActive,
pickingFBO,
deviceRect,
cullRect,
effects,
pass,
pickZ,
preRenderStats: {}
};
for (const effect of effects) {
if (effect.useInPicking) {
opts.preRenderStats[effect.id] = effect.preRender(opts);
}
}
const {decodePickingColor} = this.pickLayersPass.render(opts);
// Read from an already rendered picking buffer
// Returns an Uint8ClampedArray of picked pixels
const {x, y, width, height} = deviceRect;
const pickedColors = new (pickZ ? Float32Array : Uint8Array)(width * height * 4);
this.device.readPixelsToArrayWebGL(pickingFBO as Framebuffer, {
sourceX: x,
sourceY: y,
sourceWidth: width,
sourceHeight: height,
target: pickedColors
});
return {pickedColors, decodePickingColor};
}
// Calculate a picking rect centered on deviceX and deviceY and clipped to device
// Returns null if pixel is outside of device
_getPickingRect({
deviceX,
deviceY,
deviceRadius,
deviceWidth,
deviceHeight
}: {
deviceX: number;
deviceY: number;
deviceRadius: number;
deviceWidth: number;
deviceHeight: number;
}): Rect | null {
// Create a box of size `radius * 2 + 1` centered at [deviceX, deviceY]
const x = Math.max(0, deviceX - deviceRadius);
const y = Math.max(0, deviceY - deviceRadius);
const width = Math.min(deviceWidth, deviceX + deviceRadius + 1) - x;
const height = Math.min(deviceHeight, deviceY + deviceRadius + 1) - y;
// x, y out of bounds.
if (width <= 0 || height <= 0) {
return null;
}
return {x, y, width, height};
}
}