-
Notifications
You must be signed in to change notification settings - Fork 319
Expand file tree
/
Copy pathLayeredMaterialNodeProcessing.js
More file actions
258 lines (222 loc) · 9.83 KB
/
Copy pathLayeredMaterialNodeProcessing.js
File metadata and controls
258 lines (222 loc) · 9.83 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
import { chooseNextLevelToFetch } from 'Layer/LayerUpdateStrategy';
import LayerUpdateState from 'Layer/LayerUpdateState';
import handlingError from 'Process/handlerNodeError';
function materialCommandQueuePriorityFunction(material) {
// We know that 'node' is visible because commands can only be
// issued for visible nodes.
// TODO: need priorization of displayed nodes
// Then prefer displayed node over non-displayed one
return material.visible ? 100 : 10;
}
function refinementCommandCancellationFn(cmd) {
if (!cmd.requester.parent || !cmd.requester.material) {
return true;
}
// Cancel the command if the tile already has a better texture.
// This is only needed for elevation layers, because we may have several
// concurrent layers but we can only use one texture.
if (cmd.layer.isElevationLayer && cmd.requester.material.getElevationTile() &&
cmd.targetLevel <= cmd.requester.material.getElevationTile().level) {
return true;
}
// Cancel the command if the layer was removed between command scheduling and command execution
if (!cmd.requester.layerUpdateState[cmd.layer.id]) {
// || !cmd.layer.source._featuresCaches[cmd.layer.crs]) {
return true;
}
return !cmd.requester.material.visible;
}
function buildCommand(view, layer, extentsSource, extentsDestination, requester) {
return {
view,
layer,
extentsSource,
extentsDestination,
requester,
priority: materialCommandQueuePriorityFunction(requester.material),
earlyDropFunction: refinementCommandCancellationFn,
partialLoading: true,
};
}
function computePitchs(textures, extentsDestination) {
return extentsDestination
.map((ext, i) => (ext.offsetToParent(textures[i].extent)));
}
export function updateLayeredMaterialNodeImagery(context, layer, node, parent) {
const material = node.material;
if (!parent || !material) {
return;
}
const extentsDestination = node.getExtentsByProjection(layer.crs);
const zoom = extentsDestination[0].zoom;
if (zoom > layer.zoom.max || zoom < layer.zoom.min) {
return;
}
let nodeLayer = material.getTile(layer.id);
// Initialisation
if (node.layerUpdateState[layer.id] === undefined) {
node.layerUpdateState[layer.id] = new LayerUpdateState();
if (!layer.source.extentInsideLimit(node.extent, zoom)) {
// we also need to check that tile's parent doesn't have a texture for this layer,
// because even if this tile is outside of the layer, it could inherit it's
// parent texture
if (!layer.noTextureParentOutsideLimit &&
parent.material &&
parent.material.getTile &&
parent.material.getTile(layer.id)) {
// ok, we're going to inherit our parent's texture
} else {
node.layerUpdateState[layer.id].noMoreUpdatePossible();
return;
}
}
if (!nodeLayer) {
// Create new raster node
nodeLayer = layer.setupRasterNode(node);
// Init the node by parent
const parentLayer = parent.material?.getTile(layer.id);
nodeLayer.initFromParent(parentLayer, extentsDestination);
}
// Proposed new process, two separate processes:
// * FIRST PASS: initNodeXXXFromParent and get out of the function
// * SECOND PASS: Fetch best texture
// The two-step allows you to filter out unnecessary requests
// Indeed in the second pass, their state (not visible or not displayed) can block them to fetch
if (nodeLayer.level >= layer.source.zoom.min) {
context.view.notifyChange(node, false);
return;
}
}
// Node is hidden, no need to update it
if (!material.visible) {
return;
}
// An update is pending / or impossible -> abort
if (!layer.visible || !node.layerUpdateState[layer.id].canTryUpdate()) {
return;
}
if (nodeLayer.level >= extentsDestination[0].zoom) {
// default decision method
node.layerUpdateState[layer.id].noMoreUpdatePossible();
return;
}
// is fetching data from this layer disabled?
if (layer.frozen) {
return;
}
const failureParams = node.layerUpdateState[layer.id].failureParams;
const destinationLevel = extentsDestination[0].zoom || node.level;
const targetLevel = chooseNextLevelToFetch(layer.updateStrategy.type, node, destinationLevel, nodeLayer.level, layer, failureParams);
if ((!layer.source.isVectorSource && targetLevel <= nodeLayer.level) || targetLevel > destinationLevel) {
if (failureParams.lowestLevelError != Infinity) {
// this is the highest level found in case of error.
node.layerUpdateState[layer.id].noMoreUpdatePossible();
}
return;
} else if (!layer.source.extentInsideLimit(node.extent, targetLevel)) {
node.layerUpdateState[layer.id].noData({ targetLevel });
context.view.notifyChange(node, false);
return;
}
const extentsSource = extentsDestination.map(e => e.tiledExtentParent(targetLevel));
node.layerUpdateState[layer.id].newTry();
const command = buildCommand(context.view, layer, extentsSource, extentsDestination, node);
return context.scheduler.execute(command).then(
(results) => {
// Does nothing if the layer has been removed while command was being or waiting to be executed
if (!node.layerUpdateState[layer.id]) {
return;
}
const textures = results.map((texture, index) => (texture != null ? texture :
{ isTexture: false, extent: extentsDestination[index] }));
// TODO: Handle error : result is undefined in provider. throw error
const pitchs = computePitchs(textures, extentsDestination);
nodeLayer.setTextures(textures, pitchs);
node.layerUpdateState[layer.id].success();
},
err => handlingError(err, node, layer, targetLevel, context.view));
}
export function updateLayeredMaterialNodeElevation(context, layer, node, parent) {
const material = node.material;
if (!parent || !material) {
return;
}
// TODO: we need either
// - compound or exclusive layers
// - support for multiple elevation layers
// Elevation is currently handled differently from color layers.
// This is caused by a LayeredMaterial limitation: only 1 elevation texture
// can be used (where a tile can have N textures x M layers)
const extentsDestination = node.getExtentsByProjection(layer.crs);
const zoom = extentsDestination[0].zoom;
if (zoom > layer.zoom.max || zoom < layer.zoom.min) {
return;
}
// Init elevation layer, and inherit from parent if possible
let nodeLayer = material.getElevationTile();
if (!nodeLayer) {
nodeLayer = layer.setupRasterNode(node);
}
if (node.layerUpdateState[layer.id] === undefined) {
node.layerUpdateState[layer.id] = new LayerUpdateState();
const parentLayer = parent.material?.getTile(layer.id);
nodeLayer.initFromParent(parentLayer, extentsDestination);
if (nodeLayer.level >= layer.source.zoom.min) {
context.view.notifyChange(node, false);
return;
}
}
// Possible conditions to *not* update the elevation texture
if (layer.frozen ||
!material.visible ||
!node.layerUpdateState[layer.id].canTryUpdate()) {
return;
}
const failureParams = node.layerUpdateState[layer.id].failureParams;
const targetLevel = chooseNextLevelToFetch(layer.updateStrategy.type, node, extentsDestination[0].zoom, nodeLayer.level, layer, failureParams);
if (targetLevel <= nodeLayer.level || targetLevel > extentsDestination[0].zoom) {
node.layerUpdateState[layer.id].noMoreUpdatePossible();
return;
} else if (!layer.source.extentInsideLimit(node.extent, targetLevel)) {
node.layerUpdateState[layer.id].noData({ targetLevel });
context.view.notifyChange(node, false);
return;
}
const extentsSource = extentsDestination.map(e => e.tiledExtentParent(targetLevel));
node.layerUpdateState[layer.id].newTry();
const command = buildCommand(context.view, layer, extentsSource, extentsDestination, node);
return context.scheduler.execute(command).then(
(results) => {
// Does nothing if the layer has been removed while command was being or waiting to be executed
if (!node.layerUpdateState[layer.id]) {
return;
}
// Do not apply the new texture if its level is < than the current
// one. This is only needed for elevation layers, because we may
// have several concurrent layers but we can only use one texture.
if (targetLevel <= nodeLayer.level) {
node.layerUpdateState[layer.id].noMoreUpdatePossible();
return;
}
const pitchs = computePitchs(results, extentsDestination);
nodeLayer.setTextures(results, pitchs);
node.layerUpdateState[layer.id].success();
},
err => handlingError(err, node, layer, targetLevel, context.view));
}
export function removeLayeredMaterialNodeTile(tileId) {
/**
* @param {TileMesh} node - The node to udpate.
*/
return function removeLayeredMaterialNodeTile(node) {
if (node.material?.removeTile) {
if (node.material.elevationTile !== undefined) {
node.setBBoxZ({ min: 0, max: 0 });
}
node.material.removeTile(tileId);
}
if (node.layerUpdateState && node.layerUpdateState[tileId]) {
delete node.layerUpdateState[tileId];
}
};
}