-
Notifications
You must be signed in to change notification settings - Fork 384
Expand file tree
/
Copy pathoptimizedTraverseFunctions.js
More file actions
577 lines (354 loc) · 14.9 KB
/
optimizedTraverseFunctions.js
File metadata and controls
577 lines (354 loc) · 14.9 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
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
import { LOADED, FAILED } from '../constants.js';
const viewErrorTarget = {
inView: false,
error: Infinity,
distanceFromCamera: Infinity,
};
function isDownloadFinished( value ) {
return value === LOADED || value === FAILED;
}
// Checks whether this tile was last used on the given frame.
function isUsedThisFrame( tile, frameCount ) {
return isProcessed( tile ) && tile.traversal.lastFrameVisited === frameCount && tile.traversal.used;
}
function isProcessed( tile ) {
// TODO: here "traversal" is used to determine whether a tile has been initialized. We should
// move these to tiles utils
return Boolean( tile.traversal );
}
// Checks whether all children have been processed and are ready to traverse
function areChildrenProcessed( tile ) {
const { children } = tile;
const childrenReady = children.length === 0 || isProcessed( children[ children.length - 1 ] );
const contentReady = ! tile.internal.hasUnrenderableContent || isDownloadFinished( tile.internal.loadingState );
return childrenReady && contentReady;
}
// Checks whether we can stop at this tile for rendering or not
function canUnconditionallyRefine( tile ) {
return tile.internal.hasUnrenderableContent || ( tile.parent && tile.parent.geometricError < tile.geometricError );
}
// Resets the frame information for the given tile
function resetFrameState( tile, renderer ) {
renderer.ensureChildrenArePreprocessed( tile );
if ( tile.traversal.lastFrameVisited !== renderer.frameCount ) {
tile.traversal.lastFrameVisited = renderer.frameCount;
tile.traversal.used = false;
tile.traversal.inFrustum = false;
tile.traversal.isLeaf = false;
tile.traversal.visible = false;
tile.traversal.active = false;
tile.traversal.error = Infinity;
tile.traversal.distanceFromCamera = Infinity;
tile.traversal.allChildrenReady = false;
tile.traversal.allChildrenLoaded = false;
tile.traversal.kicked = false;
tile.traversal.allUsedChildrenProcessed = false;
// update tile frustum and error state
renderer.calculateTileViewErrorWithPlugin( tile, viewErrorTarget );
tile.traversal.inFrustum = viewErrorTarget.inView;
tile.traversal.error = viewErrorTarget.error;
tile.traversal.distanceFromCamera = viewErrorTarget.distanceFromCamera;
}
}
// Recursively mark tiles used down to the next layer, skipping external tilesets
function recursivelyMarkUsed( tile, renderer, cacheOnly = false ) {
resetFrameState( tile, renderer );
if ( cacheOnly ) {
renderer.markTileUsed( tile );
} else {
markUsed( tile );
}
// don't traverse if the children have not been processed, yet but tileset content
// should be considered to be "replaced" by the loaded children so await that here.
if ( canUnconditionallyRefine( tile ) && areChildrenProcessed( tile ) ) {
const children = tile.children;
for ( let i = 0, l = children.length; i < l; i ++ ) {
recursivelyMarkUsed( children[ i ], renderer, cacheOnly );
}
}
}
// Recursively mark tiles used down to the next layer, skipping external tilesets
function recursivelyMarkPreviouslyUsed( tile, renderer ) {
resetFrameState( tile, renderer );
if ( tile.traversal.usedLastFrame ) {
markUsed( tile, renderer );
if ( tile.traversal.wasSetActive ) {
tile.traversal.active = true;
}
if ( ! tile.traversal.active || canUnconditionallyRefine( tile ) ) {
// don't traverse if the children have not been processed, yet but tileset content
// should be considered to be "replaced" by the loaded children so await that here.
if ( areChildrenProcessed( tile ) ) {
const children = tile.children;
for ( let i = 0, l = children.length; i < l; i ++ ) {
recursivelyMarkPreviouslyUsed( children[ i ], renderer );
}
}
}
}
}
// Mark a tile as being used by current view
function markUsed( tile ) {
tile.traversal.used = true;
}
// Returns whether the tile can be traversed to the next layer of children by checking the tile metrics
function canTraverse( tile, renderer ) {
// If we've met the error requirements then don't load further - if an external tileset is encountered,
// though, then continue to refine.
if ( tile.traversal.error <= renderer.errorTarget && ! canUnconditionallyRefine( tile ) ) {
return false;
}
// Early out if we've reached the maximum allowed depth.
if ( renderer.maxDepth > 0 && tile.internal.depth + 1 >= renderer.maxDepth ) {
return false;
}
// Early out if the children haven't been processed, yet
if ( ! areChildrenProcessed( tile ) ) {
return false;
}
return true;
}
// Marks "active" children as "kicked" so they are still loaded but not rendered yet
function kickActiveChildren( tile, renderer ) {
const { frameCount } = renderer;
const { children } = tile;
for ( let i = 0, l = children.length; i < l; i ++ ) {
const c = children[ i ];
if ( isUsedThisFrame( c, frameCount ) ) {
if ( c.traversal.active ) {
c.traversal.kicked = true;
c.traversal.active = false;
}
kickActiveChildren( c, renderer );
}
}
}
// Checks whether this tile is ready to be stopped at for rendering
function isChildReady( tile ) {
return ! canUnconditionallyRefine( tile ) && ( ! tile.internal.hasContent || isDownloadFinished( tile.internal.loadingState ) );
}
// Determine which tiles are used by the renderer given the current camera configuration
function markUsedTiles( tile, renderer ) {
// determine frustum set is run first so we can ensure the preprocessing of all the necessary
// child tiles has happened here.
resetFrameState( tile, renderer );
if ( ! tile.traversal.inFrustum ) {
return;
}
if ( ! canTraverse( tile, renderer ) ) {
markUsed( tile );
return;
}
// Traverse children and see if any children are in view.
let anyChildrenUsed = false;
let anyChildrenInFrustum = false;
const children = tile.children;
for ( let i = 0, l = children.length; i < l; i ++ ) {
const c = children[ i ];
markUsedTiles( c, renderer );
anyChildrenUsed = anyChildrenUsed || isUsedThisFrame( c, renderer.frameCount );
anyChildrenInFrustum = anyChildrenInFrustum || c.traversal.inFrustum;
}
// If none of the children are visible in the frustum then there should be no reason to display this tile. We still mark
// this tile and all children as "used" only in the cache (but not loaded) so they are not disposed, causing an oscillation
// / flicker in the content.
if ( tile.refine === 'REPLACE' && ! anyChildrenInFrustum && children.length !== 0 ) {
tile.traversal.inFrustum = false;
renderer.markTileUsed( tile );
for ( let i = 0, l = children.length; i < l; i ++ ) {
recursivelyMarkUsed( children[ i ], renderer, true );
}
return;
}
// wait until after the above condition to mark the traversed tile as used or not
// and then mark any of the sibling child tiles as used
markUsed( tile );
if ( tile.refine === 'REPLACE' && anyChildrenUsed && renderer.loadSiblings ) {
for ( let i = 0, l = children.length; i < l; i ++ ) {
recursivelyMarkUsed( children[ i ], renderer );
}
}
}
// Traverse and mark the tiles that are at the leaf nodes of the "used" tree.
function markUsedSetLeaves( tile, renderer ) {
const frameCount = renderer.frameCount;
if ( ! isUsedThisFrame( tile, frameCount ) ) {
return;
}
// This tile is a leaf if none of the children had been used.
const children = tile.children;
let anyChildrenUsed = false;
for ( let i = 0, l = children.length; i < l; i ++ ) {
const c = children[ i ];
anyChildrenUsed = anyChildrenUsed || isUsedThisFrame( c, frameCount );
}
// Traversal
if ( ! anyChildrenUsed ) {
tile.traversal.isLeaf = true;
} else {
for ( let i = 0, l = children.length; i < l; i ++ ) {
markUsedSetLeaves( children[ i ], renderer );
}
// compute content-readiness so markVisibleTiles can stop traversal at this tile when
// loadParents is enabled and children haven't finished loading their content yet.
// only computed when anyChildrenUsed — leaf tiles retain allChildrenLoaded = false
// (from resetFrameState) so parents correctly see them as not yet loaded.
let allChildrenLoaded = true;
for ( let i = 0, l = children.length; i < l; i ++ ) {
const c = children[ i ];
if ( isUsedThisFrame( c, frameCount ) ) {
const childCanDisplay = ! canUnconditionallyRefine( c );
const childContentReady = ! c.internal.hasContent || isDownloadFinished( c.internal.loadingState );
const childIsReady = ( childCanDisplay && childContentReady ) || c.traversal.allChildrenLoaded;
if ( ! childIsReady ) {
allChildrenLoaded = false;
}
}
}
tile.traversal.allChildrenLoaded = allChildrenLoaded;
}
// save whether any children are processed
let allUsedChildrenProcessed = true;
for ( let i = 0, l = children.length; i < l; i ++ ) {
const c = children[ i ];
if ( isUsedThisFrame( c, renderer.frameCount ) && ! c.traversal.allUsedChildrenProcessed ) {
allUsedChildrenProcessed = false;
}
}
tile.traversal.allUsedChildrenProcessed = allUsedChildrenProcessed && areChildrenProcessed( tile );
}
// TODO: revisit implementation
// Skip past tiles we consider unrenderable because they are outside the error threshold.
function markVisibleTiles( tile, renderer ) {
if ( ! isUsedThisFrame( tile, renderer.frameCount ) ) {
return;
}
const hasContent = tile.internal.hasContent;
const loadedContent = isDownloadFinished( tile.internal.loadingState ) && hasContent;
const children = tile.children;
// When loading parent tiles as fallbacks: if children aren't content-ready yet, mark this tile
// as a leaf so it is displayed as a placeholder while children load (mirrors legacy behavior).
// allChildrenLoaded was computed bottom-up in markUsedSetLeaves so it can be checked before recursing.
if ( renderer.loadParents && ! tile.traversal.allChildrenLoaded && ! canUnconditionallyRefine( tile ) ) {
tile.traversal.isLeaf = true;
}
if ( tile.traversal.isLeaf ) {
// if we're allowed to stop at this tile then mark it as active and allow any previously active tiles to
// continue to be displayed
if ( ! canUnconditionallyRefine( tile ) ) {
tile.traversal.active = true;
if ( areChildrenProcessed( tile ) && ( ! tile.internal.hasContent || ! isDownloadFinished( tile.internal.loadingState ) ) ) {
for ( let i = 0, l = children.length; i < l; i ++ ) {
recursivelyMarkPreviouslyUsed( children[ i ], renderer );
}
}
}
return;
}
// Don't wait for all children tiles to load if this tileset has empty tiles at the root in order
// to match Cesium's behavior
let allChildrenReady = children.length > 0;
for ( let i = 0, l = children.length; i < l; i ++ ) {
const c = children[ i ];
markVisibleTiles( c, renderer );
if ( isUsedThisFrame( c, renderer.frameCount ) ) {
const childIsReady = c.traversal.active && isChildReady( c );
if ( ! childIsReady && ! c.traversal.allChildrenReady ) {
allChildrenReady = false;
}
}
}
tile.traversal.allChildrenReady = allChildrenReady;
// If we find that the subsequent children are not ready such that this tile gap can be filled then
// mark all lower tiles as non active and prepare this one to be displayed if possible
const thisTileIsVisible = tile.traversal.active && isChildReady( tile );
if ( ! canUnconditionallyRefine( tile ) && ! allChildrenReady && ! thisTileIsVisible ) {
if ( tile.traversal.wasSetActive && ( loadedContent || ! tile.internal.hasContent ) ) {
tile.traversal.active = true;
kickActiveChildren( tile, renderer );
}
}
}
// Final traverse to toggle tile visibility.
function toggleTiles( tile, renderer ) {
const isUsed = isUsedThisFrame( tile, renderer.frameCount );
if ( isUsed ) {
// any internal tileset and additive tile must be marked as active and loaded
if ( tile.internal.hasUnrenderableContent || tile.internal.hasRenderableContent && tile.refine === 'ADD' ) {
tile.traversal.active = true;
}
// queue any tiles to load that we need to, and unmark any unloaded or non visible tiles as "active"
// TODO: it may be more simple to track a separate variable than "active" here
if ( ( tile.traversal.active || tile.traversal.kicked ) && tile.internal.hasContent ) {
renderer.markTileUsed( tile );
if ( tile.internal.hasUnrenderableContent || tile.traversal.allUsedChildrenProcessed ) {
renderer.queueTileForDownload( tile );
}
if ( tile.internal.loadingState !== LOADED ) {
tile.traversal.active = false;
}
} else {
tile.traversal.active = false;
}
// when loading parent tiles as fallbacks, keep all used tiles downloaded
// regardless of active state so they are available to display while children load
if ( renderer.loadParents && tile.internal.hasContent ) {
renderer.markTileUsed( tile );
renderer.queueTileForDownload( tile );
}
// keep tiles with virtual children retained in the LRU cache so the content is
// available to regenerate virtual children if the overlay configuration changes.
if ( tile.internal.virtualChildCount > 0 && tile.internal.hasContent ) {
renderer.markTileUsed( tile );
}
// if the tile is loaded and in frustum we can mark it as visible
tile.traversal.visible = tile.internal.hasRenderableContent && tile.traversal.active && tile.traversal.inFrustum && tile.internal.loadingState === LOADED;
renderer.stats.used ++;
if ( tile.traversal.inFrustum ) {
renderer.stats.inFrustum ++;
}
}
if ( isUsed || isProcessed( tile ) && tile.traversal?.usedLastFrame ) {
let setActive = false;
let setVisible = false;
if ( isUsed ) {
// enable visibility if active due to shadows
setActive = tile.traversal.active;
if ( renderer.displayActiveTiles ) {
setVisible = tile.traversal.active || tile.traversal.visible;
} else {
setVisible = tile.traversal.visible;
}
} else {
// if the tile was used last frame but not this one then there's potential for the tile
// to not have been visited during the traversal, meaning it hasn't been reset and has
// stale values. This ensures the values are not stale.
resetFrameState( tile, renderer );
}
// If the active or visible state changed then call the functions.
if ( tile.internal.hasRenderableContent && tile.internal.loadingState === LOADED ) {
if ( tile.traversal.wasSetActive !== setActive ) {
renderer.stats.active += setActive ? 1 : - 1;
renderer.invokeOnePlugin( plugin => plugin.setTileActive && plugin.setTileActive( tile, setActive ) );
}
if ( tile.traversal.wasSetVisible !== setVisible ) {
renderer.stats.visible += setVisible ? 1 : - 1;
renderer.invokeOnePlugin( plugin => plugin.setTileVisible && plugin.setTileVisible( tile, setVisible ) );
}
}
tile.traversal.wasSetActive = setActive;
tile.traversal.wasSetVisible = setVisible;
tile.traversal.usedLastFrame = isUsed;
const children = tile.children;
for ( let i = 0, l = children.length; i < l; i ++ ) {
const c = children[ i ];
toggleTiles( c, renderer );
}
}
}
export function runTraversal( tile, renderer ) {
markUsedTiles( tile, renderer );
markUsedSetLeaves( tile, renderer );
markVisibleTiles( tile, renderer );
toggleTiles( tile, renderer );
}