-
Notifications
You must be signed in to change notification settings - Fork 386
Expand file tree
/
Copy pathtraverseFunctions.js
More file actions
493 lines (308 loc) · 13.1 KB
/
traverseFunctions.js
File metadata and controls
493 lines (308 loc) · 13.1 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
import { LOADED, FAILED } from '../constants.js';
const viewErrorTarget = {
inView: false,
error: Infinity,
distanceFromCamera: Infinity,
};
// flag guiding the behavior of the traversal to load the siblings at the root of the
// tileset or not. The spec seems to indicate "true" when using REPLACE define but
// Cesium's behavior is "false".
// See CesiumGS/3d-tiles#776
const LOAD_ROOT_SIBLINGS = true;
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 ) {
return Boolean( tile.traversal );
}
function areChildrenProcessed( tile ) {
const children = tile.children;
return children.length === 0 || isProcessed( children[ children.length - 1 ] );
}
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 ) {
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;
// 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 ) {
renderer.ensureChildrenArePreprocessed( tile );
resetFrameState( tile, renderer );
markUsed( tile, renderer, cacheOnly );
// 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 traverses to the next tiles with unloaded renderable content to load them
function recursivelyLoadNextRenderableTiles( tile, renderer ) {
renderer.ensureChildrenArePreprocessed( tile );
// exit the recursion if the tile hasn't been used this frame
if ( isUsedThisFrame( tile, renderer.frameCount ) ) {
// queue this tile to download content
if ( tile.internal.hasContent ) {
renderer.queueTileForDownload( tile );
}
if ( areChildrenProcessed( tile ) ) {
// queue any used child tiles
const children = tile.children;
for ( let i = 0, l = children.length; i < l; i ++ ) {
recursivelyLoadNextRenderableTiles( children[ i ], renderer );
}
}
}
}
// Mark a tile as being used by current view
function markUsed( tile, renderer, cacheOnly = false ) {
if ( tile.traversal.used ) {
return;
}
if ( ! cacheOnly ) {
tile.traversal.used = true;
renderer.stats.used ++;
}
renderer.markTileUsed( tile );
if ( tile.traversal.inFrustum === true ) {
renderer.stats.inFrustum ++;
}
}
// 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;
}
// 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.
renderer.ensureChildrenArePreprocessed( tile );
resetFrameState( tile, renderer );
if ( ! tile.traversal.inFrustum ) {
return;
}
if ( ! canTraverse( tile, renderer ) ) {
markUsed( tile, renderer );
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;
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
markUsed( tile, renderer );
// If this is a tile that needs children loaded to refine then recursively load child
// tiles until error is met
if ( tile.refine === 'REPLACE' && ( anyChildrenUsed && tile.internal.depth !== 0 || LOAD_ROOT_SIBLINGS ) ) {
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 );
}
if ( ! anyChildrenUsed ) {
tile.traversal.isLeaf = true;
} else {
let allChildrenReady = true;
for ( let i = 0, l = children.length; i < l; i ++ ) {
const c = children[ i ];
markUsedSetLeaves( c, renderer );
if ( isUsedThisFrame( c, frameCount ) ) {
// Compute whether this child is _allowed_ to display by checking the geometric error relative to the parent tile to avoid holes.
// If the child's geometric error is less than or equal to the parent's (or it has unrenderable content), we should NOT display the child to avoid holes.
// Only display the child if its geometric error is greater than the parent's and it has renderable content.
// Note that this behavior is undocumented in the 3d tiles specification and tilesets designed to take advantage of it may not work as expected
// in other rendering systems.
// See issue NASA-AMMOS/3DTilesRendererJS#1304
const childCanDisplay = ! canUnconditionallyRefine( c );
// Consider a child to be ready to be displayed if
// - the children's children have been loaded
// - the tile content has loaded
// - the tile is completely empty - ie has no children and no content
// - the child tileset has tried to load but failed
let isChildReady =
! c.internal.hasContent ||
( c.internal.hasRenderableContent && isDownloadFinished( c.internal.loadingState ) ) ||
( c.internal.hasUnrenderableContent && c.internal.loadingState === FAILED );
// Consider this child ready if it can be displayed and is ready for display or all of it's children ready to be displayed
isChildReady = ( childCanDisplay && isChildReady ) || c.traversal.allChildrenReady;
allChildrenReady = allChildrenReady && isChildReady;
}
}
tile.traversal.allChildrenReady = allChildrenReady;
}
}
// TODO: revisit implementation
// Skip past tiles we consider unrenderable because they are outside the error threshold.
function markVisibleTiles( tile, renderer ) {
const stats = renderer.stats;
if ( ! isUsedThisFrame( tile, renderer.frameCount ) ) {
return;
}
// Request the tile contents or mark it as visible if we've found a leaf.
if ( tile.traversal.isLeaf ) {
if ( tile.internal.loadingState === LOADED || ! tile.internal.hasContent ) {
if ( tile.traversal.inFrustum ) {
tile.traversal.visible = true;
stats.visible ++;
}
tile.traversal.active = true;
stats.active ++;
} else if ( tile.internal.hasContent ) {
renderer.queueTileForDownload( tile );
}
return;
}
const children = tile.children;
const hasContent = tile.internal.hasContent;
const loadedContent = isDownloadFinished( tile.internal.loadingState ) && hasContent;
const errorRequirement = ( renderer.errorTarget + 1 ) * renderer.errorThreshold;
const meetsSSE = tile.traversal.error <= errorRequirement;
const isAdditiveRefine = tile.refine === 'ADD';
// TODO: the "meetsSSE" field can be removed when the "errorThreshold" field has been removed
// 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
const allChildrenReady = tile.traversal.allChildrenReady || ( tile.internal.depth === 0 && ! LOAD_ROOT_SIBLINGS );
// If we've met the SSE requirements and we can load content then fire a fetch.
if ( hasContent && ( meetsSSE || isAdditiveRefine ) ) {
renderer.queueTileForDownload( tile );
}
// By this time only tiles that meet the screen space error requirements will be traversed. Only mark this
// as visible if it's been loaded and not all children have loaded yet or it's an additive tile, meaning it needs
// to display in addition to the children.
// Skip the tile entirely if there's no content to load
if ( meetsSSE && loadedContent && ! allChildrenReady || loadedContent && isAdditiveRefine ) {
if ( tile.traversal.inFrustum ) {
tile.traversal.visible = true;
stats.visible ++;
}
tile.traversal.active = true;
stats.active ++;
}
// If we're additive then don't stop the traversal here because it doesn't matter whether the children load in
// at the same rate.
if ( ! isAdditiveRefine && meetsSSE && ! allChildrenReady ) {
// load the child content if we've found that we've been loaded so we can move down to the next tile
// layer when the data has loaded.
for ( let i = 0, l = children.length; i < l; i ++ ) {
const c = children[ i ];
if ( isUsedThisFrame( c, renderer.frameCount ) ) {
recursivelyLoadNextRenderableTiles( c, renderer );
}
}
} else {
for ( let i = 0, l = children.length; i < l; i ++ ) {
markVisibleTiles( children[ i ], renderer );
}
}
}
// Final traverse to toggle tile visibility.
function toggleTiles( tile, renderer ) {
const isUsed = isUsedThisFrame( tile, renderer.frameCount );
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.
// Fire for tiles with loaded renderable content, or for empty tiles (no content at all).
if ( ( tile.internal.hasRenderableContent && tile.internal.loadingState === LOADED ) || ! tile.internal.hasContent ) {
if ( tile.traversal.wasSetActive !== setActive ) {
renderer.invokeOnePlugin( plugin => plugin.setTileActive && plugin.setTileActive( tile, setActive ) );
}
if ( tile.traversal.wasSetVisible !== setVisible ) {
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 );
}