-
Notifications
You must be signed in to change notification settings - Fork 386
Expand file tree
/
Copy pathTilesFadePlugin.js
More file actions
530 lines (334 loc) · 12.8 KB
/
TilesFadePlugin.js
File metadata and controls
530 lines (334 loc) · 12.8 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
import { Matrix4, Vector3, Quaternion } from 'three';
import { FadeManager } from './FadeManager.js';
import { FadeMaterialManager } from './FadeMaterialManager.js';
import { FadeBatchedMesh } from './FadeBatchedMesh.js';
const HAS_POPPED_IN = Symbol( 'HAS_POPPED_IN' );
const _fromPos = /* @__PURE__ */ new Vector3();
const _toPos = /* @__PURE__ */ new Vector3();
const _fromQuat = /* @__PURE__ */ new Quaternion();
const _toQuat = /* @__PURE__ */ new Quaternion();
const _scale = /* @__PURE__ */ new Vector3();
function onUpdateBefore() {
const fadeManager = this._fadeManager;
const tiles = this.tiles;
// store the tiles renderer state before the tiles update so we can check
// whether fading started or stopped completely
this._fadingBefore = fadeManager.fadeCount;
this._displayActiveTiles = tiles.displayActiveTiles;
// we need to display all active tiles in this case so we don't fade tiles in
// when moving from off screen
tiles.displayActiveTiles = true;
}
function onUpdateAfter() {
const fadeManager = this._fadeManager;
const fadeMaterialManager = this._fadeMaterialManager;
const displayActiveTiles = this._displayActiveTiles;
const fadingBefore = this._fadingBefore;
const prevCameraTransforms = this._prevCameraTransforms;
const { tiles, maximumFadeOutTiles, batchedMesh } = this;
const { cameras } = tiles;
// reset the active tiles flag
tiles.displayActiveTiles = displayActiveTiles;
// update fade step
fadeManager.update();
// fire an event
const fadingAfter = fadeManager.fadeCount;
if ( fadingBefore !== 0 && fadingAfter !== 0 ) {
tiles.dispatchEvent( { type: 'fade-change' } );
tiles.dispatchEvent( { type: 'needs-render' } );
}
// update the visibility of tiles based on visibility since we must use
// the active tiles for rendering fade
if ( ! displayActiveTiles ) {
tiles.visibleTiles.forEach( t => {
// if a tile is fading out then it may not be traversed and thus will not have
// the frustum flag set correctly.
const scene = t.engineData.scene;
if ( scene ) {
scene.visible = t.traversal.inFrustum;
}
this.forEachBatchIds( t, ( id, batchedMesh, plugin ) => {
batchedMesh.setVisibleAt( id, t.traversal.inFrustum );
plugin.batchedMesh.setVisibleAt( id, t.traversal.inFrustum );
} );
} );
}
if ( maximumFadeOutTiles < this._fadingOutCount ) {
// determine whether all the rendering cameras are moving
// quickly so we can adjust how tiles fade accordingly
let isMovingFast = true;
cameras.forEach( camera => {
if ( ! prevCameraTransforms.has( camera ) ) {
return;
}
const currMatrix = camera.matrixWorld;
const prevMatrix = prevCameraTransforms.get( camera );
currMatrix.decompose( _toPos, _toQuat, _scale );
prevMatrix.decompose( _fromPos, _fromQuat, _scale );
const angleTo = _toQuat.angleTo( _fromQuat );
const positionTo = _toPos.distanceTo( _fromPos );
// if rotation is moving > 0.25 radians per frame or position is moving > 0.1 units
// then we are considering the camera to be moving too fast to notice a faster / abrupt fade
isMovingFast = isMovingFast && ( angleTo > 0.25 || positionTo > 0.1 );
} );
if ( isMovingFast ) {
fadeManager.completeAllFades();
}
}
// track the camera movement so we can use it for next frame
cameras.forEach( camera => {
prevCameraTransforms.get( camera ).copy( camera.matrixWorld );
} );
// update the fade state for each tile
fadeManager.forEachObject( ( tile, { fadeIn, fadeOut } ) => {
// prevent faded tiles from being unloaded
const scene = tile.engineData.scene;
const isFadingOut = fadeManager.isFadingOut( tile );
tiles.markTileUsed( tile );
if ( scene ) {
fadeMaterialManager.setFade( scene, fadeIn, fadeOut );
if ( isFadingOut ) {
scene.visible = true;
}
}
// fade the tiles and toggle the visibility appropriately
this.forEachBatchIds( tile, ( id, batchedMesh, plugin ) => {
batchedMesh.setFadeAt( id, fadeIn, fadeOut );
batchedMesh.setVisibleAt( id, true );
plugin.batchedMesh.setVisibleAt( id, false );
} );
} );
// update the batched mesh fields
if ( batchedMesh ) {
const material = tiles.getPluginByName( 'BATCHED_TILES_PLUGIN' ).batchedMesh.material;
batchedMesh.material.map = material.map;
}
}
/**
* Plugin that overrides material shaders to fade tile geometry in and out as tile LODs
* change, preventing pop-in. Dispatches `fade-change`, `fade-start`, and `fade-end`
* events on the `TilesRenderer` during animation — use these when doing on-demand
* rendering. Works alongside `BatchedTilesPlugin` when present.
* @param {Object} [options]
* @param {number} [options.fadeDuration=250] Time in milliseconds for a tile to fully fade in or out.
* @param {number} [options.maximumFadeOutTiles=50] Maximum simultaneous fade-out tiles. If exceeded, tiles pop instead of fading.
* @param {boolean} [options.fadeRootTiles=false] Whether root-level tiles fade in on their first appearance.
*/
export class TilesFadePlugin {
get fadeDuration() {
return this._fadeManager.duration;
}
set fadeDuration( value ) {
this._fadeManager.duration = Number( value );
}
get fadingTiles() {
return this._fadeManager.fadeCount;
}
constructor( options ) {
options = {
maximumFadeOutTiles: 50,
fadeRootTiles: false,
fadeDuration: 250,
...options,
};
this.name = 'FADE_TILES_PLUGIN';
this.priority = - 2;
this.tiles = null;
this.batchedMesh = null;
this._quickFadeTiles = new Set();
this._fadeManager = new FadeManager();
this._fadeMaterialManager = new FadeMaterialManager();
this._prevCameraTransforms = null;
this._fadingOutCount = 0;
this.maximumFadeOutTiles = options.maximumFadeOutTiles;
this.fadeRootTiles = options.fadeRootTiles;
this.fadeDuration = options.fadeDuration;
}
init( tiles ) {
// event callback initialization
this._onLoadModel = ( { scene } )=> {
// initialize all the scene materials to fade
this._fadeMaterialManager.prepareScene( scene );
};
this._onDisposeModel = ( { tile, scene } ) => {
if ( this.tiles.visibleTiles.has( tile ) ) {
// mark the parent as needing to fade in quickly to accommodate the children disappearing.
// this can happen when a tile is forcefully disposed or removed from the lru cache while visible.
this._quickFadeTiles.add( tile.parent );
}
// delete the fade info from the managers on disposal of model
this._fadeManager.deleteObject( tile );
this._fadeMaterialManager.deleteScene( scene );
};
this._onAddCamera = ( { camera } ) => {
// track the camera transform
this._prevCameraTransforms.set( camera, new Matrix4() );
};
this._onDeleteCamera = ( { camera } )=> {
// remove the camera transform
this._prevCameraTransforms.delete( camera );
};
this._onTileVisibilityChange = ( { tile, visible } ) => {
// this function gets fired _after_ all set visible callbacks including the batched meshes
// revert the scene and fade to the initial state when toggling
const scene = tile.engineData.scene;
if ( scene ) {
scene.visible = true;
}
this.forEachBatchIds( tile, ( id, batchedMesh, plugin ) => {
batchedMesh.setFadeAt( id, 0, 0 );
batchedMesh.setVisibleAt( id, false );
plugin.batchedMesh.setVisibleAt( id, false );
} );
};
this._onUpdateBefore = () => {
onUpdateBefore.call( this );
};
this._onUpdateAfter = () => {
onUpdateAfter.call( this );
};
tiles.addEventListener( 'load-model', this._onLoadModel );
tiles.addEventListener( 'dispose-model', this._onDisposeModel );
tiles.addEventListener( 'add-camera', this._onAddCamera );
tiles.addEventListener( 'delete-camera', this._onDeleteCamera );
tiles.addEventListener( 'update-before', this._onUpdateBefore );
tiles.addEventListener( 'update-after', this._onUpdateAfter );
tiles.addEventListener( 'tile-visibility-change', this._onTileVisibilityChange );
// initialize fade manager
const fadeManager = this._fadeManager;
fadeManager.onFadeSetStart = () => {
tiles.dispatchEvent( { type: 'fade-start' } );
tiles.dispatchEvent( { type: 'needs-render' } );
};
fadeManager.onFadeSetComplete = () => {
tiles.dispatchEvent( { type: 'fade-end' } );
tiles.dispatchEvent( { type: 'needs-render' } );
};
fadeManager.onFadeComplete = ( tile, visible ) => {
// mark the fade as finished and reset the fade parameters
this._fadeMaterialManager.setFade( tile.engineData.scene, 0, 0 );
this.forEachBatchIds( tile, ( id, batchedMesh, plugin ) => {
batchedMesh.setFadeAt( id, 0, 0 );
batchedMesh.setVisibleAt( id, false );
plugin.batchedMesh.setVisibleAt( id, visible );
} );
if ( ! visible ) {
// now that the tile is hidden we can run the built-in setTileVisible function for the tile
tiles.invokeOnePlugin( plugin => plugin !== this && plugin.setTileVisible && plugin.setTileVisible( tile, false ) );
this._fadingOutCount --;
}
};
// initialize the state based on what's already present
const prevCameraTransforms = new Map();
tiles.cameras.forEach( camera => {
prevCameraTransforms.set( camera, new Matrix4() );
} );
tiles.forEachLoadedModel( ( scene, tile ) => {
this._onLoadModel( { scene } );
} );
this.tiles = tiles;
this._fadeManager = fadeManager;
this._prevCameraTransforms = prevCameraTransforms;
}
// initializes the batched mesh if it needs to be, dispose if it it's no longer needed
initBatchedMesh() {
const otherBatchedMesh = this.tiles.getPluginByName( 'BATCHED_TILES_PLUGIN' )?.batchedMesh;
if ( otherBatchedMesh ) {
if ( this.batchedMesh === null ) {
this._onBatchedMeshDispose = () => {
this.batchedMesh.dispose();
this.batchedMesh.removeFromParent();
this.batchedMesh = null;
otherBatchedMesh.removeEventListener( 'dispose', this._onBatchedMeshDispose );
};
const material = otherBatchedMesh.material.clone();
material.onBeforeCompile = otherBatchedMesh.material.onBeforeCompile;
this.batchedMesh = new FadeBatchedMesh( otherBatchedMesh, material );
this.tiles.group.add( this.batchedMesh );
}
} else {
if ( this.batchedMesh !== null ) {
this._onBatchedMeshDispose();
this._onBatchedMeshDispose = null;
}
}
}
// callback for fading to prevent tiles from being removed until the fade effect has completed
setTileVisible( tile, visible ) {
if ( ! tile.engineData.scene ) {
return false;
}
const fadeManager = this._fadeManager;
// track the fade state
const wasFading = fadeManager.isFading( tile );
if ( fadeManager.isFadingOut( tile ) ) {
this._fadingOutCount --;
}
// trigger any necessary fades
if ( ! visible ) {
this._fadingOutCount ++;
fadeManager.fadeOut( tile );
} else {
// if this is a root renderable tile and this is the first time rendering in
// then pop it in
const isRootRenderableTile = tile.internal.depthFromRenderedParent === 1;
if ( isRootRenderableTile ) {
if ( tile[ HAS_POPPED_IN ] || this.fadeRootTiles ) {
this._fadeManager.fadeIn( tile );
}
tile[ HAS_POPPED_IN ] = true;
} else {
this._fadeManager.fadeIn( tile );
}
}
// if a tile needs to be jumped in then complete the fade here
if ( this._quickFadeTiles.has( tile ) ) {
this._fadeManager.completeFade( tile );
this._quickFadeTiles.delete( tile );
}
// if a tile was already fading then it's already marked as visible and in the scene
if ( wasFading ) {
return true;
}
// cancel the visibility change trigger because we're fading and will call this after
// fade completes.
const isFading = this._fadeManager.isFading( tile );
if ( ! visible && isFading ) {
return true;
}
return false;
}
dispose() {
const tiles = this.tiles;
this._fadeManager.completeAllFades();
if ( this.batchedMesh !== null ) {
this._onBatchedMeshDispose();
}
tiles.removeEventListener( 'load-model', this._onLoadModel );
tiles.removeEventListener( 'dispose-model', this._onDisposeModel );
tiles.removeEventListener( 'add-camera', this._onAddCamera );
tiles.removeEventListener( 'delete-camera', this._onDeleteCamera );
tiles.removeEventListener( 'update-before', this._onUpdateBefore );
tiles.removeEventListener( 'update-after', this._onUpdateAfter );
tiles.removeEventListener( 'tile-visibility-change', this._onTileVisibilityChange );
tiles.forEachLoadedModel( ( scene, tile ) => {
this._fadeManager.deleteObject( tile );
if ( scene ) {
scene.visible = true; // TODO
}
} );
}
// helper for iterating over the batch ids for a given tile
forEachBatchIds( tile, cb ) {
this.initBatchedMesh();
if ( this.batchedMesh ) {
const batchedPlugin = this.tiles.getPluginByName( 'BATCHED_TILES_PLUGIN' );
const instanceIds = batchedPlugin.getTileBatchIds( tile );
if ( instanceIds ) {
instanceIds.forEach( id => {
cb( id, this.batchedMesh, batchedPlugin );
} );
}
}
}
}