-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
Copy pathlayer.js
1012 lines (860 loc) · 32.3 KB
/
layer.js
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
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// 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.
/* eslint-disable react/no-direct-mutation-state */
import {COORDINATE_SYSTEM} from './constants';
import AttributeManager from './attribute/attribute-manager';
import UniformTransitionManager from './uniform-transition-manager';
import {diffProps, validateProps} from '../lifecycle/props';
import {count} from '../utils/count';
import log from '../utils/log';
import debug from '../debug';
import GL from '@luma.gl/constants';
import {withParameters, setParameters} from '@luma.gl/core';
import assert from '../utils/assert';
import memoize from '../utils/memoize';
import {mergeShaders} from '../utils/shader';
import {projectPosition, getWorldPosition} from '../shaderlib/project/project-functions';
import typedArrayManager from '../utils/typed-array-manager';
import Component from '../lifecycle/component';
import LayerState from './layer-state';
import {worldToPixels} from '@math.gl/web-mercator';
import {load} from '@loaders.gl/core';
const TRACE_CHANGE_FLAG = 'layer.changeFlag';
const TRACE_INITIALIZE = 'layer.initialize';
const TRACE_UPDATE = 'layer.update';
const TRACE_FINALIZE = 'layer.finalize';
const TRACE_MATCHED = 'layer.matched';
const MAX_PICKING_COLOR_CACHE_SIZE = 2 ** 24 - 1;
const EMPTY_ARRAY = Object.freeze([]);
// Only compare the same two viewports once
const areViewportsEqual = memoize(({oldViewport, viewport}) => {
return oldViewport.equals(viewport);
});
let pickingColorCache = new Uint8ClampedArray(0);
const defaultProps = {
// data: Special handling for null, see below
data: {type: 'data', value: EMPTY_ARRAY, async: true},
dataComparator: null,
_dataDiff: {type: 'function', value: data => data && data.__diff, compare: false, optional: true},
dataTransform: {type: 'function', value: null, compare: false, optional: true},
onDataLoad: {type: 'function', value: null, compare: false, optional: true},
fetch: {
type: 'function',
value: (url, {propName, layer}) => {
const {resourceManager} = layer.context;
const loadOptions = layer.getLoadOptions();
const {loaders} = layer.props;
let inResourceManager = resourceManager.contains(url);
if (!inResourceManager && !loadOptions) {
// If there is no layer-specific load options, then attempt to cache this resource in the data manager
resourceManager.add({resourceId: url, data: load(url, loaders), persistent: false});
inResourceManager = true;
}
if (inResourceManager) {
return resourceManager.subscribe({
resourceId: url,
onChange: data => layer.internalState.reloadAsyncProp(propName, data),
consumerId: layer.id,
requestId: propName
});
}
return load(url, loaders, loadOptions);
},
compare: false
},
updateTriggers: {}, // Update triggers: a core change detection mechanism in deck.gl
visible: true,
pickable: false,
opacity: {type: 'number', min: 0, max: 1, value: 1},
onHover: {type: 'function', value: null, compare: false, optional: true},
onClick: {type: 'function', value: null, compare: false, optional: true},
onDragStart: {type: 'function', value: null, compare: false, optional: true},
onDrag: {type: 'function', value: null, compare: false, optional: true},
onDragEnd: {type: 'function', value: null, compare: false, optional: true},
coordinateSystem: COORDINATE_SYSTEM.DEFAULT,
coordinateOrigin: {type: 'array', value: [0, 0, 0], compare: true},
modelMatrix: {type: 'array', value: null, compare: true, optional: true},
wrapLongitude: false,
positionFormat: 'XYZ',
colorFormat: 'RGBA',
parameters: {},
transitions: null,
extensions: [],
loaders: {type: 'array', value: [], optional: true, compare: true},
// Offset depth based on layer index to avoid z-fighting.
// Negative values pull layer towards the camera
// https://www.opengl.org/archives/resources/faq/technical/polygonoffset.htm
getPolygonOffset: {
type: 'function',
value: ({layerIndex}) => [0, -layerIndex * 100],
compare: false
},
// Selection/Highlighting
highlightedObjectIndex: -1,
autoHighlight: false,
highlightColor: {type: 'accessor', value: [0, 0, 128, 128]}
};
export default class Layer extends Component {
toString() {
const className = this.constructor.layerName || this.constructor.name;
return `${className}({id: '${this.props.id}'})`;
}
// Public API
// Updates selected state members and marks the object for redraw
setState(updateObject) {
this.setChangeFlags({stateChanged: true});
Object.assign(this.state, updateObject);
this.setNeedsRedraw();
}
// Sets the redraw flag for this layer, will trigger a redraw next animation frame
setNeedsRedraw(redraw = true) {
if (this.internalState) {
this.internalState.needsRedraw = redraw;
}
}
// This layer needs a deep update
setNeedsUpdate() {
this.context.layerManager.setNeedsUpdate(String(this));
this.internalState.needsUpdate = true;
}
// Checks state of attributes and model
getNeedsRedraw(opts = {clearRedrawFlags: false}) {
return this._getNeedsRedraw(opts);
}
// Checks if layer attributes needs updating
needsUpdate() {
// Call subclass lifecycle method
return (
this.internalState.needsUpdate ||
this.hasUniformTransition() ||
this.shouldUpdateState(this._getUpdateParams())
);
// End lifecycle method
}
hasUniformTransition() {
return this.internalState.uniformTransitions.active;
}
get isLoaded() {
return this.internalState && !this.internalState.isAsyncPropLoading();
}
get wrapLongitude() {
return this.props.wrapLongitude;
}
// Returns true if the layer is pickable and visible.
isPickable() {
return this.props.pickable && this.props.visible;
}
// Return an array of models used by this layer, can be overriden by layer subclass
getModels() {
return this.state && (this.state.models || (this.state.model ? [this.state.model] : []));
}
getAttributeManager() {
return this.internalState && this.internalState.attributeManager;
}
// Returns the most recent layer that matched to this state
// (When reacting to an async event, this layer may no longer be the latest)
getCurrentLayer() {
return this.internalState && this.internalState.layer;
}
// Returns the default parse options for async props
getLoadOptions() {
return this.props.loadOptions;
}
// PROJECTION METHODS
// Projects a point with current map state (lat, lon, zoom, pitch, bearing)
// From the current layer's coordinate system to screen
project(xyz) {
const {viewport} = this.context;
const worldPosition = getWorldPosition(xyz, {
viewport,
modelMatrix: this.props.modelMatrix,
coordinateOrigin: this.props.coordinateOrigin,
coordinateSystem: this.props.coordinateSystem
});
const [x, y, z] = worldToPixels(worldPosition, viewport.pixelProjectionMatrix);
return xyz.length === 2 ? [x, y] : [x, y, z];
}
// Note: this does not reverse `project`.
// Always unprojects to the viewport's coordinate system
unproject(xy) {
const {viewport} = this.context;
return viewport.unproject(xy);
}
projectPosition(xyz) {
return projectPosition(xyz, {
viewport: this.context.viewport,
modelMatrix: this.props.modelMatrix,
coordinateOrigin: this.props.coordinateOrigin,
coordinateSystem: this.props.coordinateSystem
});
}
use64bitPositions() {
const {coordinateSystem} = this.props;
return (
coordinateSystem === COORDINATE_SYSTEM.DEFAULT ||
coordinateSystem === COORDINATE_SYSTEM.LNGLAT ||
coordinateSystem === COORDINATE_SYSTEM.CARTESIAN
);
}
// Event handling
onHover(info, pickingEvent) {
if (this.props.onHover) {
return this.props.onHover(info, pickingEvent);
}
return false;
}
onClick(info, pickingEvent) {
if (this.props.onClick) {
return this.props.onClick(info, pickingEvent);
}
return false;
}
// Returns the picking color that doesn't match any subfeature
// Use if some graphics do not belong to any pickable subfeature
// @return {Array} - a black color
nullPickingColor() {
return [0, 0, 0];
}
// Returns the picking color that doesn't match any subfeature
// Use if some graphics do not belong to any pickable subfeature
encodePickingColor(i, target = []) {
target[0] = (i + 1) & 255;
target[1] = ((i + 1) >> 8) & 255;
target[2] = (((i + 1) >> 8) >> 8) & 255;
return target;
}
// Returns the index corresponding to a picking color that doesn't match any subfeature
// @param {Uint8Array} color - color array to be decoded
// @return {Array} - the decoded picking color
decodePickingColor(color) {
assert(color instanceof Uint8Array);
const [i1, i2, i3] = color;
// 1 was added to seperate from no selection
const index = i1 + i2 * 256 + i3 * 65536 - 1;
return index;
}
// //////////////////////////////////////////////////
// LIFECYCLE METHODS, overridden by the layer subclasses
// Called once to set up the initial state
// App can create WebGL resources
initializeState() {
throw new Error(`Layer ${this} has not defined initializeState`);
}
getShaders(shaders) {
for (const extension of this.props.extensions) {
shaders = mergeShaders(shaders, extension.getShaders.call(this, extension));
}
return shaders;
}
// Let's layer control if updateState should be called
shouldUpdateState({oldProps, props, context, changeFlags}) {
return changeFlags.propsOrDataChanged;
}
// Default implementation, all attributes will be invalidated and updated
// when data changes
/* eslint-disable-next-line complexity */
updateState({oldProps, props, context, changeFlags}) {
const attributeManager = this.getAttributeManager();
if (changeFlags.dataChanged && attributeManager) {
const {dataChanged} = changeFlags;
if (Array.isArray(dataChanged)) {
// is partial update
for (const dataRange of dataChanged) {
attributeManager.invalidateAll(dataRange);
}
} else {
attributeManager.invalidateAll();
}
}
const neededPickingBuffer = oldProps.highlightedObjectIndex >= 0 || oldProps.pickable;
const needPickingBuffer = props.highlightedObjectIndex >= 0 || props.pickable;
if (neededPickingBuffer !== needPickingBuffer && attributeManager) {
const {pickingColors, instancePickingColors} = attributeManager.attributes;
const pickingColorsAttribute = pickingColors || instancePickingColors;
if (pickingColorsAttribute) {
if (needPickingBuffer && pickingColorsAttribute.constant) {
pickingColorsAttribute.constant = false;
attributeManager.invalidate(pickingColorsAttribute.id);
}
if (!pickingColorsAttribute.value && !needPickingBuffer) {
pickingColorsAttribute.constant = true;
pickingColorsAttribute.value = [0, 0, 0];
}
}
}
}
// Called once when layer is no longer matched and state will be discarded
// App can destroy WebGL resources here
finalizeState() {
for (const model of this.getModels()) {
model.delete();
}
const attributeManager = this.getAttributeManager();
if (attributeManager) {
attributeManager.finalize();
}
this.context.resourceManager.unsubscribe({consumerId: this.id});
this.internalState.uniformTransitions.clear();
this.internalState.finalize();
}
// If state has a model, draw it with supplied uniforms
draw(opts) {
for (const model of this.getModels()) {
model.draw(opts);
}
}
// called to populate the info object that is passed to the event handler
// @return null to cancel event
getPickingInfo({info, mode}) {
const {index} = info;
if (index >= 0) {
// If props.data is an indexable array, get the object
if (Array.isArray(this.props.data)) {
info.object = this.props.data[index];
}
}
return info;
}
// END LIFECYCLE METHODS
// //////////////////////////////////////////////////
// INTERNAL METHODS
activateViewport(viewport) {
const oldViewport = this.internalState.viewport;
this.internalState.viewport = viewport;
if (!oldViewport || !areViewportsEqual({oldViewport, viewport})) {
this.setChangeFlags({viewportChanged: true});
if (this.isComposite) {
if (this.needsUpdate()) {
// Composite layers may add/remove sublayers on viewport change
// Because we cannot change the layers list during a draw cycle, we don't want to update sublayers right away
// This will not call update immediately, but mark the layerManager as needs update on the next frame
this.setNeedsUpdate();
}
} else {
this._update();
}
}
}
// Default implementation of attribute invalidation, can be redefined
invalidateAttribute(name = 'all', diffReason = '') {
const attributeManager = this.getAttributeManager();
if (!attributeManager) {
return;
}
if (name === 'all') {
attributeManager.invalidateAll();
} else {
attributeManager.invalidate(name);
}
}
updateAttributes(changedAttributes) {
for (const model of this.getModels()) {
this._setModelAttributes(model, changedAttributes);
}
}
// Calls attribute manager to update any WebGL attributes
_updateAttributes(props) {
const attributeManager = this.getAttributeManager();
if (!attributeManager) {
return;
}
// Figure out data length
const numInstances = this.getNumInstances(props);
const startIndices = this.getStartIndices(props);
attributeManager.update({
data: props.data,
numInstances,
startIndices,
props,
transitions: props.transitions,
buffers: props.data.attributes,
context: this,
// Don't worry about non-attribute props
ignoreUnknownAttributes: true
});
const changedAttributes = attributeManager.getChangedAttributes({clearChangedFlags: true});
this.updateAttributes(changedAttributes);
}
// Update attribute transitions. This is called in drawLayer, no model updates required.
_updateAttributeTransition() {
const attributeManager = this.getAttributeManager();
if (attributeManager) {
attributeManager.updateTransition();
}
}
// Update uniform (prop) transitions. This is called in updateState, may result in model updates.
_updateUniformTransition() {
const {uniformTransitions} = this.internalState;
if (uniformTransitions.active) {
// clone props
const propsInTransition = uniformTransitions.update();
const props = Object.create(this.props);
for (const key in propsInTransition) {
Object.defineProperty(props, key, {value: propsInTransition[key]});
}
return props;
}
return this.props;
}
calculateInstancePickingColors(attribute, {numInstances}) {
if (attribute.constant) {
return;
}
// calculateInstancePickingColors always generates the same sequence.
// pickingColorCache saves the largest generated sequence for reuse
const cacheSize = pickingColorCache.length / 3;
if (cacheSize < numInstances) {
if (numInstances > MAX_PICKING_COLOR_CACHE_SIZE) {
log.warn(
'Layer has too many data objects. Picking might not be able to distinguish all objects.'
)();
}
pickingColorCache = typedArrayManager.allocate(pickingColorCache, numInstances, {
size: 3,
copy: true,
maxCount: Math.max(numInstances, MAX_PICKING_COLOR_CACHE_SIZE)
});
// If the attribute is larger than the cache, resize the cache and populate the missing chunk
const newCacheSize = pickingColorCache.length / 3;
const pickingColor = [];
for (let i = cacheSize; i < newCacheSize; i++) {
this.encodePickingColor(i, pickingColor);
pickingColorCache[i * 3 + 0] = pickingColor[0];
pickingColorCache[i * 3 + 1] = pickingColor[1];
pickingColorCache[i * 3 + 2] = pickingColor[2];
}
}
attribute.value = pickingColorCache.subarray(0, numInstances * 3);
}
_setModelAttributes(model, changedAttributes) {
const attributeManager = this.getAttributeManager();
const excludeAttributes = model.userData.excludeAttributes || {};
const shaderAttributes = attributeManager.getShaderAttributes(
changedAttributes,
excludeAttributes
);
model.setAttributes(shaderAttributes);
}
// Sets the picking color at the specified index to null picking color. Used for multi-depth picking.
// This method may be overriden by layer implementations
disablePickingIndex(objectIndex) {
this._disablePickingIndex(objectIndex);
}
_disablePickingIndex(objectIndex) {
const {pickingColors, instancePickingColors} = this.getAttributeManager().attributes;
const colors = pickingColors || instancePickingColors;
const start = colors.getVertexOffset(objectIndex);
const end = colors.getVertexOffset(objectIndex + 1);
// Fill the sub buffer with 0s
colors.buffer.subData({
data: new Uint8Array(end - start),
offset: start // 1 byte per element
});
}
restorePickingColors() {
const {pickingColors, instancePickingColors} = this.getAttributeManager().attributes;
const colors = pickingColors || instancePickingColors;
colors.updateSubBuffer({startOffset: 0});
}
// Deduces numer of instances. Intention is to support:
// - Explicit setting of numInstances
// - Auto-deduction for ES6 containers that define a size member
// - Auto-deduction for Classic Arrays via the built-in length attribute
// - Auto-deduction via arrays
getNumInstances(props) {
props = props || this.props;
// First Check if app has provided an explicit value
if (props.numInstances !== undefined) {
return props.numInstances;
}
// Second check if the layer has set its own value
if (this.state && this.state.numInstances !== undefined) {
return this.state.numInstances;
}
// Use container library to get a count for any ES6 container or object
return count(props.data);
}
// Buffer layout describes how many attribute values are packed for each data object
// The default (null) is one value each object.
// Some data formats (e.g. paths, polygons) have various length. Their buffer layout
// is in the form of [L0, L1, L2, ...]
getStartIndices(props) {
props = props || this.props;
// First Check if startIndices is provided as an explicit value
if (props.startIndices !== undefined) {
return props.startIndices;
}
// Second check if the layer has set its own value
if (this.state && this.state.startIndices) {
return this.state.startIndices;
}
return null;
}
// LAYER MANAGER API
// Should only be called by the deck.gl LayerManager class
// Called by layer manager when a new layer is found
/* eslint-disable max-statements */
_initialize() {
debug(TRACE_INITIALIZE, this);
this._initState();
// Call subclass lifecycle methods
this.initializeState(this.context);
// Initialize extensions
for (const extension of this.props.extensions) {
extension.initializeState.call(this, this.context, extension);
}
// End subclass lifecycle methods
// initializeState callback tends to clear state
this.setChangeFlags({
dataChanged: true,
propsChanged: true,
viewportChanged: true,
extensionsChanged: true
});
this._updateState();
}
// Called by layer manager
// if this layer is new (not matched with an existing layer) oldProps will be empty object
_update() {
// Call subclass lifecycle method
const stateNeedsUpdate = this.needsUpdate();
// End lifecycle method
debug(TRACE_UPDATE, this, stateNeedsUpdate);
if (stateNeedsUpdate) {
this._updateState();
}
}
// Common code for _initialize and _update
_updateState() {
const currentProps = this.props;
const currentViewport = this.context.viewport;
const propsInTransition = this._updateUniformTransition();
this.internalState.propsInTransition = propsInTransition;
// Overwrite this.context.viewport during update to use the last activated viewport on this layer
// In multi-view applications, a layer may only be drawn in one of the views
// Which would make the "active" viewport different from the shared context
this.context.viewport = this.internalState.viewport || currentViewport;
// Overwrite this.props during update to use in-transition prop values
this.props = propsInTransition;
try {
const updateParams = this._getUpdateParams();
const oldModels = this.getModels();
// Safely call subclass lifecycle methods
if (this.context.gl) {
this.updateState(updateParams);
} else {
try {
this.updateState(updateParams);
} catch (error) {
// ignore error if gl context is missing
}
}
// Execute extension updates
for (const extension of this.props.extensions) {
extension.updateState.call(this, updateParams, extension);
}
const modelChanged = this.getModels()[0] !== oldModels[0];
this._updateModules(updateParams, modelChanged);
// End subclass lifecycle methods
if (this.isComposite) {
// Render or update previously rendered sublayers
this._renderLayers(updateParams);
} else {
this.setNeedsRedraw();
// Add any subclass attributes
this._updateAttributes(this.props);
// Note: Automatic instance count update only works for single layers
if (this.state.model) {
this.state.model.setInstanceCount(this.getNumInstances());
}
}
} finally {
// Restore shared context
this.context.viewport = currentViewport;
this.props = currentProps;
this.clearChangeFlags();
this.internalState.needsUpdate = false;
this.internalState.resetOldProps();
}
}
/* eslint-enable max-statements */
// Called by manager when layer is about to be disposed
// Note: not guaranteed to be called on application shutdown
_finalize() {
debug(TRACE_FINALIZE, this);
assert(this.internalState && this.state);
// Call subclass lifecycle method
this.finalizeState(this.context);
// Finalize extensions
for (const extension of this.props.extensions) {
extension.finalizeState.call(this, extension);
}
}
// Calculates uniforms
drawLayer({moduleParameters = null, uniforms = {}, parameters = {}}) {
this._updateAttributeTransition();
const currentProps = this.props;
// Overwrite this.props during redraw to use in-transition prop values
// `internalState.propsInTransition` could be missing if `updateState` failed
this.props = this.internalState.propsInTransition || currentProps;
const {opacity} = this.props;
// apply gamma to opacity to make it visually "linear"
uniforms.opacity = Math.pow(opacity, 1 / 2.2);
try {
// TODO/ib - hack move to luma Model.draw
if (moduleParameters) {
this.setModuleParameters(moduleParameters);
}
// Apply polygon offset to avoid z-fighting
// TODO - move to draw-layers
const {getPolygonOffset} = this.props;
const offsets = (getPolygonOffset && getPolygonOffset(uniforms)) || [0, 0];
setParameters(this.context.gl, {polygonOffset: offsets});
// Call subclass lifecycle method
withParameters(this.context.gl, parameters, () => {
const opts = {moduleParameters, uniforms, parameters, context: this.context};
// extensions
for (const extension of this.props.extensions) {
extension.draw.call(this, opts, extension);
}
this.draw(opts);
});
} finally {
this.props = currentProps;
}
// End lifecycle method
}
// Helper methods
getChangeFlags() {
return this.internalState.changeFlags;
}
// Dirty some change flags, will be handled by updateLayer
/* eslint-disable complexity */
setChangeFlags(flags) {
const {changeFlags} = this.internalState;
/* eslint-disable no-fallthrough, max-depth */
for (const key in flags) {
if (flags[key]) {
let flagChanged = false;
switch (key) {
case 'dataChanged':
// changeFlags.dataChanged may be `false`, a string (reason) or an array of ranges
if (Array.isArray(changeFlags[key])) {
changeFlags[key] = Array.isArray(flags[key])
? changeFlags[key].concat(flags[key])
: flags[key];
flagChanged = true;
}
default:
if (!changeFlags[key]) {
changeFlags[key] = flags[key];
flagChanged = true;
}
}
if (flagChanged) {
debug(TRACE_CHANGE_FLAG, this, key, flags);
}
}
}
/* eslint-enable no-fallthrough, max-depth */
// Update composite flags
const propsOrDataChanged =
changeFlags.dataChanged ||
changeFlags.updateTriggersChanged ||
changeFlags.propsChanged ||
changeFlags.extensionsChanged;
changeFlags.propsOrDataChanged = propsOrDataChanged;
changeFlags.somethingChanged =
propsOrDataChanged || flags.viewportChanged || flags.stateChanged;
}
/* eslint-enable complexity */
// Clear all changeFlags, typically after an update
clearChangeFlags() {
this.internalState.changeFlags = {
// Primary changeFlags, can be strings stating reason for change
dataChanged: false,
propsChanged: false,
updateTriggersChanged: false,
viewportChanged: false,
stateChanged: false,
extensionsChanged: false,
// Derived changeFlags
propsOrDataChanged: false,
somethingChanged: false
};
}
// Compares the layers props with old props from a matched older layer
// and extracts change flags that describe what has change so that state
// can be update correctly with minimal effort
diffProps(newProps, oldProps) {
const changeFlags = diffProps(newProps, oldProps);
// iterate over changedTriggers
if (changeFlags.updateTriggersChanged) {
for (const key in changeFlags.updateTriggersChanged) {
if (changeFlags.updateTriggersChanged[key]) {
this.invalidateAttribute(key);
}
}
}
// trigger uniform transitions
if (changeFlags.transitionsChanged) {
for (const key in changeFlags.transitionsChanged) {
// prop changed and transition is enabled
this.internalState.uniformTransitions.add(
key,
oldProps[key],
newProps[key],
newProps.transitions[key]
);
}
}
return this.setChangeFlags(changeFlags);
}
// Called by layer manager to validate props (in development)
validateProps() {
validateProps(this.props);
}
setModuleParameters(moduleParameters) {
for (const model of this.getModels()) {
model.updateModuleSettings(moduleParameters);
}
}
// PRIVATE METHODS
_updateModules({props, oldProps}, forceUpdate) {
// Picking module parameters
const {autoHighlight, highlightedObjectIndex, highlightColor} = props;
if (
forceUpdate ||
oldProps.autoHighlight !== autoHighlight ||
oldProps.highlightedObjectIndex !== highlightedObjectIndex ||
oldProps.highlightColor !== highlightColor
) {
const parameters = {};
if (!autoHighlight) {
parameters.pickingSelectedColor = null;
}
if (Array.isArray(highlightColor)) {
parameters.pickingHighlightColor = highlightColor;
}
// highlightedObjectIndex will overwrite any settings from auto highlighting.
if (Number.isInteger(highlightedObjectIndex)) {
parameters.pickingSelectedColor =
highlightedObjectIndex >= 0 ? this.encodePickingColor(highlightedObjectIndex) : null;
}
this.setModuleParameters(parameters);
}
}
_getUpdateParams() {
return {
props: this.props,
oldProps: this.internalState.getOldProps(),
context: this.context,
changeFlags: this.internalState.changeFlags
};
}
// Checks state of attributes and model
_getNeedsRedraw(opts) {
// this method may be called by the render loop as soon a the layer
// has been created, so guard against uninitialized state
if (!this.internalState) {
return false;
}
let redraw = false;
redraw = redraw || (this.internalState.needsRedraw && this.id);
this.internalState.needsRedraw = this.internalState.needsRedraw && !opts.clearRedrawFlags;
// TODO - is attribute manager needed? - Model should be enough.
const attributeManager = this.getAttributeManager();
const attributeManagerNeedsRedraw = attributeManager && attributeManager.getNeedsRedraw(opts);
redraw = redraw || attributeManagerNeedsRedraw;
return redraw;
}
// Create new attribute manager
_getAttributeManager() {
return new AttributeManager(this.context.gl, {
id: this.props.id,
stats: this.context.stats,
timeline: this.context.timeline
});
}
_initState() {
assert(!this.internalState && !this.state);
assert(isFinite(this.props.coordinateSystem), `${this.id}: invalid coordinateSystem`);
const attributeManager = this._getAttributeManager();
if (attributeManager) {
// All instanced layers get instancePickingColors attribute by default
// Their shaders can use it to render a picking scene
// TODO - this slightly slows down non instanced layers
attributeManager.addInstanced({
instancePickingColors: {
type: GL.UNSIGNED_BYTE,
size: 3,
noAlloc: true,
update: this.calculateInstancePickingColors
}
});
}
this.internalState = new LayerState({
attributeManager,
layer: this
});
this.clearChangeFlags(); // populate this.internalState.changeFlags
this.state = {};
// for backwards compatibility with older layers
// TODO - remove in next release
/* eslint-disable accessor-pairs */
Object.defineProperty(this.state, 'attributeManager', {
get: () => {
log.deprecated('layer.state.attributeManager', 'layer.getAttributeManager()');
return attributeManager;
}
});
/* eslint-enable accessor-pairs */
this.internalState.layer = this;
this.internalState.uniformTransitions = new UniformTransitionManager(this.context.timeline);
this.internalState.onAsyncPropUpdated = this._onAsyncPropUpdated.bind(this);
// Ensure any async props are updated
this.internalState.setAsyncProps(this.props);
}
// Called by layer manager to transfer state from an old layer
_transferState(oldLayer) {
debug(TRACE_MATCHED, this, this === oldLayer);
const {state, internalState} = oldLayer;
assert(state && internalState);
if (this === oldLayer) {
return;
}
// Move internalState
this.internalState = internalState;
this.internalState.layer = this;
// Move state
this.state = state;
// We keep the state ref on old layers to support async actions
// oldLayer.state = null;
// Ensure any async props are updated
this.internalState.setAsyncProps(this.props);