-
Notifications
You must be signed in to change notification settings - Fork 266
/
traverseFunctions.js
449 lines (292 loc) · 11.1 KB
/
traverseFunctions.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
import { LOADED, FAILED } from './constants.js';
function isDownloadFinished( value ) {
return value === LOADED || value === FAILED;
}
// Checks whether this tile was last used on the given frame.
function isUsedThisFrame( tile, frameCount ) {
return tile.__lastFrameVisited === frameCount && tile.__used;
}
// Resets the frame frame information for the given tile
function resetFrameState( tile, frameCount ) {
if ( tile.__lastFrameVisited !== frameCount ) {
tile.__lastFrameVisited = frameCount;
tile.__used = false;
tile.__inFrustum = false;
tile.__isLeaf = false;
tile.__visible = false;
tile.__active = false;
tile.__error = Infinity;
tile.__distanceFromCamera = Infinity;
tile.__childrenWereVisible = false;
tile.__allChildrenLoaded = false;
}
}
// Recursively mark tiles used down to the next tile with content
function recursivelyMarkUsed( tile, frameCount, lruCache, renderer ) {
renderer.ensureChildrenArePreprocessed( tile );
resetFrameState( tile, frameCount );
tile.__used = true;
lruCache.markUsed( tile );
if ( tile.__contentEmpty ) {
const children = tile.children;
for ( let i = 0, l = children.length; i < l; i ++ ) {
recursivelyMarkUsed( children[ i ], frameCount, lruCache, renderer );
}
}
}
function recursivelyLoadTiles( tile, depthFromRenderedParent, renderer ) {
renderer.ensureChildrenArePreprocessed( tile );
// Try to load any external tile set children if the external tile set has loaded.
const doTraverse =
tile.__contentEmpty && (
! tile.__externalTileSet ||
isDownloadFinished( tile.__loadingState )
);
if ( doTraverse ) {
const children = tile.children;
for ( let i = 0, l = children.length; i < l; i ++ ) {
// don't increment depth to rendered parent here because we should treat
// the next layer of rendered children as just a single depth away for the
// sake of sorting.
const child = children[ i ];
child.__depthFromRenderedParent = depthFromRenderedParent;
recursivelyLoadTiles( child, depthFromRenderedParent, renderer );
}
} else {
renderer.requestTileContents( tile );
}
}
// Helper function for recursively traversing a tile set. If `beforeCb` returns `true` then the
// traversal will end early.
export function traverseSet( tile, beforeCb = null, afterCb = null, parent = null, depth = 0 ) {
if ( beforeCb && beforeCb( tile, parent, depth ) ) {
if ( afterCb ) {
afterCb( tile, parent, depth );
}
return;
}
const children = tile.children;
for ( let i = 0, l = children.length; i < l; i ++ ) {
traverseSet( children[ i ], beforeCb, afterCb, tile, depth + 1 );
}
if ( afterCb ) {
afterCb( tile, parent, depth );
}
}
// Determine which tiles are within the camera frustum.
// TODO: this is marking items as used in the lrucache, which means some data is
// being kept around that isn't being used -- is that okay?
export function determineFrustumSet( 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 );
const stats = renderer.stats;
const frameCount = renderer.frameCount;
const errorTarget = renderer.errorTarget;
const maxDepth = renderer.maxDepth;
const loadSiblings = renderer.loadSiblings;
const lruCache = renderer.lruCache;
const stopAtEmptyTiles = renderer.stopAtEmptyTiles;
resetFrameState( tile, frameCount );
// Early out if this tile is not within view.
const inFrustum = renderer.tileInView( tile );
if ( inFrustum === false ) {
return false;
}
tile.__used = true;
lruCache.markUsed( tile );
tile.__inFrustum = true;
stats.inFrustum ++;
// Early out if this tile has less error than we're targeting but don't stop
// at an external tile set.
if ( ( stopAtEmptyTiles || ! tile.__contentEmpty ) && ! tile.__externalTileSet ) {
// compute the _error and __distanceFromCamera fields
renderer.calculateError( tile );
const error = tile.__error;
if ( error <= errorTarget ) {
return true;
}
// Early out if we've reached the maximum allowed depth.
if ( renderer.maxDepth > 0 && tile.__depth + 1 >= maxDepth ) {
return true;
}
}
// Traverse children and see if any children are in view.
let anyChildrenUsed = false;
const children = tile.children;
for ( let i = 0, l = children.length; i < l; i ++ ) {
const c = children[ i ];
const r = determineFrustumSet( c, renderer );
anyChildrenUsed = anyChildrenUsed || r;
}
// If there are children within view and we are loading siblings then mark
// all sibling tiles as used, as well.
if ( anyChildrenUsed && loadSiblings ) {
for ( let i = 0, l = children.length; i < l; i ++ ) {
const c = children[ i ];
recursivelyMarkUsed( c, frameCount, lruCache, renderer );
}
}
return true;
}
// Traverse and mark the tiles that are at the leaf nodes of the "used" tree.
export function markUsedSetLeaves( tile, renderer ) {
const stats = renderer.stats;
const frameCount = renderer.frameCount;
if ( ! isUsedThisFrame( tile, frameCount ) ) {
return;
}
stats.used ++;
// 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 ) {
// TODO: This isn't necessarily right because it's possible that a parent tile is considered in the
// frustum while the child tiles are not, making them unused. If all children have loaded and were properly
// considered to be in the used set then we shouldn't set ourselves to a leaf here.
tile.__isLeaf = true;
} else {
let childrenWereVisible = false;
let allChildrenLoaded = true;
for ( let i = 0, l = children.length; i < l; i ++ ) {
const c = children[ i ];
markUsedSetLeaves( c, renderer );
childrenWereVisible = childrenWereVisible || c.__wasSetVisible || c.__childrenWereVisible;
if ( isUsedThisFrame( c, frameCount ) ) {
// consider a child to be loaded 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
const childLoaded =
c.__allChildrenLoaded ||
( ! c.__contentEmpty && isDownloadFinished( c.__loadingState ) ) ||
( ! c.__externalTileSet && c.__contentEmpty && c.children.length === 0 ) ||
( c.__externalTileSet && c.__loadingState === FAILED );
allChildrenLoaded = allChildrenLoaded && childLoaded;
}
}
tile.__childrenWereVisible = childrenWereVisible;
tile.__allChildrenLoaded = allChildrenLoaded;
}
}
// Skip past tiles we consider unrenderable because they are outside the error threshold.
export function skipTraversal( tile, renderer ) {
const stats = renderer.stats;
const frameCount = renderer.frameCount;
if ( ! isUsedThisFrame( tile, frameCount ) ) {
return;
}
const parent = tile.parent;
const parentDepthToParent = parent ? parent.__depthFromRenderedParent : - 1;
tile.__depthFromRenderedParent = parentDepthToParent;
// Request the tile contents or mark it as visible if we've found a leaf.
const lruCache = renderer.lruCache;
if ( tile.__isLeaf ) {
tile.__depthFromRenderedParent ++;
if ( tile.__loadingState === LOADED ) {
if ( tile.__inFrustum ) {
tile.__visible = true;
stats.visible ++;
}
tile.__active = true;
stats.active ++;
} else if ( ! lruCache.isFull() && ( ! tile.__contentEmpty || tile.__externalTileSet ) ) {
renderer.requestTileContents( tile );
}
return;
}
const errorRequirement = ( renderer.errorTarget + 1 ) * renderer.errorThreshold;
const meetsSSE = tile.__error <= errorRequirement;
const includeTile = meetsSSE || tile.refine === 'ADD';
const hasModel = ! tile.__contentEmpty;
const hasContent = hasModel || tile.__externalTileSet;
const loadedContent = isDownloadFinished( tile.__loadingState ) && hasContent;
const childrenWereVisible = tile.__childrenWereVisible;
const children = tile.children;
const allChildrenHaveContent = tile.__allChildrenLoaded;
// Increment the relative depth of the node to the nearest rendered parent if it has content
// and is being rendered.
if ( includeTile && hasModel ) {
tile.__depthFromRenderedParent ++;
}
// If we've met the SSE requirements and we can load content then fire a fetch.
if ( includeTile && ! loadedContent && ! lruCache.isFull() && hasContent ) {
renderer.requestTileContents( tile );
}
// Only mark this tile as visible if it meets the screen space error requirements, has loaded content, not
// all children have loaded yet, and if no children were visible last frame. We want to keep children visible
// that _were_ visible to avoid a pop in level of detail as the camera moves around and parent / sibling tiles
// load in.
// Skip the tile entirely if there's no content to load
if (
( meetsSSE && ! allChildrenHaveContent && ! childrenWereVisible && loadedContent )
|| ( tile.refine === 'ADD' && loadedContent )
) {
if ( tile.__inFrustum ) {
tile.__visible = true;
stats.visible ++;
}
tile.__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 ( tile.refine !== 'ADD' && meetsSSE && ! allChildrenHaveContent && loadedContent ) {
// 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, frameCount ) && ! lruCache.isFull() ) {
c.__depthFromRenderedParent = tile.__depthFromRenderedParent + 1;
recursivelyLoadTiles( c, c.__depthFromRenderedParent, renderer );
}
}
} else {
for ( let i = 0, l = children.length; i < l; i ++ ) {
const c = children[ i ];
if ( isUsedThisFrame( c, frameCount ) ) {
skipTraversal( c, renderer );
}
}
}
}
// Final traverse to toggle tile visibility.
export function toggleTiles( tile, renderer ) {
const frameCount = renderer.frameCount;
const isUsed = isUsedThisFrame( tile, frameCount );
if ( isUsed || tile.__usedLastFrame ) {
let setActive = false;
let setVisible = false;
if ( isUsed ) {
// enable visibility if active due to shadows
setActive = tile.__active;
if ( renderer.displayActiveTiles ) {
setVisible = tile.__active || tile.__visible;
} else {
setVisible = tile.__visible;
}
}
// If the active or visible state changed then call the functions.
if ( ! tile.__contentEmpty && tile.__loadingState === LOADED ) {
if ( tile.__wasSetActive !== setActive ) {
renderer.setTileActive( tile, setActive );
}
if ( tile.__wasSetVisible !== setVisible ) {
renderer.setTileVisible( tile, setVisible );
}
}
tile.__wasSetActive = setActive;
tile.__wasSetVisible = setVisible;
tile.__usedLastFrame = isUsed;
const children = tile.children;
for ( let i = 0, l = children.length; i < l; i ++ ) {
const c = children[ i ];
toggleTiles( c, renderer );
}
}
}