Skip to content

Commit

Permalink
Merge pull request #4405 from AnalyticalGraphicsInc/all-visible-event
Browse files Browse the repository at this point in the history
3D Tiles - Add allVisibleTilesLoaded event to Cesium3DTileset
  • Loading branch information
pjcozzi authored Jan 11, 2017
2 parents 112f718 + d15728d commit 558ea1c
Show file tree
Hide file tree
Showing 4 changed files with 147 additions and 51 deletions.
4 changes: 4 additions & 0 deletions Apps/Sandcastle/gallery/3D Tiles.html
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,10 @@
//console.log('Loading: requests: ' + numberOfPendingRequests + ', processing: ' + numberProcessing);
});

tileset.allTilesLoaded.addEventListener(function() {
//console.log('All tiles are loaded');
});

tileset.tileUnload.addEventListener(function(tile) {
//console.log('Tile unloaded.')
});
Expand Down
84 changes: 71 additions & 13 deletions Source/Scene/Cesium3DTileset.js
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,7 @@ define([
visited : 0,
numberOfCommands : 0,
// Loading stats
numberOfAttemptedRequests : 0,
numberOfPendingRequests : 0,
numberProcessing : 0,
numberContentReady : 0, // Number of tiles with content loaded, does not include empty tiles
Expand All @@ -295,6 +296,8 @@ define([
lastPick : new Cesium3DTilesetStatistics()
};

this._tilesLoaded = false;

/**
* This property is for debugging only; it is not optimized for production use.
* <p>
Expand Down Expand Up @@ -385,6 +388,25 @@ define([
*/
this.loadProgress = new Event();

/**
* The event fired to indicate that all tiles that meet the screen space error this frame are loaded. The tileset
* is completely loaded for this view.
* <p>
* This event is fired at the end of the frame after the scene is rendered.
* </p>
*
* @type {Event}
* @default new Event()
*
* @example
* city.allTilesLoaded.addEventListener(function() {
* console.log('All tiles are loaded');
* });
*
* @see Cesium3DTileset#tilesLoaded
*/
this.allTilesLoaded = new Event();

/**
* The event fired to indicate that a tile's content was unloaded from the cache.
* <p>
Expand Down Expand Up @@ -436,7 +458,6 @@ define([
this._readyPromise = when.defer();

var that = this;

this.loadTileset(tilesetUrl).then(function(data) {
var tilesetJson = data.tilesetJson;
that._asset = tilesetJson.asset;
Expand All @@ -450,15 +471,16 @@ define([
}

function Cesium3DTilesetStatistics() {
this.selected = -1;
this.visited = -1;
this.numberOfCommands = -1;
this.numberOfPendingRequests = -1;
this.numberProcessing = -1;
this.numberContentReady = -1;
this.numberTotal = -1;
this.numberOfTilesStyled = -1;
this.numberOfFeaturesStyled = -1;
this.selected = 0;
this.visited = 0;
this.numberOfCommands = 0;
this.numberOfAttemptedRequests = 0;
this.numberOfPendingRequests = 0;
this.numberProcessing = 0;
this.numberContentReady = 0;
this.numberTotal = 0;
this.numberOfTilesStyled = 0;
this.numberOfFeaturesStyled = 0;
}

defineProperties(Cesium3DTileset.prototype, {
Expand Down Expand Up @@ -569,6 +591,25 @@ define([
}
},

/**
* When <code>true</code>, all tiles that meet the screen space error this frame are loaded. The tileset is
* completely loaded for this view.
*
* @memberof Cesium3DTileset.prototype
*
* @type {Boolean}
* @readonly
*
* @default false
*
* @see Cesium3DTileset#allTilesLoaded
*/
tilesLoaded : {
get : function() {
return this._tilesLoaded;
}
},

/**
* The url to a tileset.json file or to a directory containing a tileset.json file.
*
Expand Down Expand Up @@ -1063,13 +1104,16 @@ define([

tile.requestContent();

var stats = tileset._statistics;

if (!tile.contentUnloaded) {
var stats = tileset._statistics;
++stats.numberOfPendingRequests;

var removeFunction = removeFromProcessingQueue(tileset, tile);
tile.content.contentReadyToProcessPromise.then(addToProcessingQueue(tileset, tile)).otherwise(removeFunction);
tile.content.readyPromise.then(removeFunction).otherwise(removeFunction);
} else {
++stats.numberOfAttemptedRequests;
}
}

Expand Down Expand Up @@ -1479,6 +1523,7 @@ define([
var stats = tileset._statistics;
stats.visited = 0;
stats.numberOfCommands = 0;
stats.numberOfAttemptedRequests = 0;
stats.numberOfTilesStyled = 0;
stats.numberOfFeaturesStyled = 0;
}
Expand All @@ -1495,6 +1540,7 @@ define([
(last.visited !== stats.visited ||
last.numberOfCommands !== stats.numberOfCommands ||
last.selected !== tileset._selectedTiles.length ||
last.numberOfAttemptedRequests !== stats.numberOfAttemptedRequests ||
last.numberOfPendingRequests !== stats.numberOfPendingRequests ||
last.numberProcessing !== stats.numberProcessing ||
last.numberContentReady !== stats.numberContentReady ||
Expand Down Expand Up @@ -1524,6 +1570,7 @@ define([

// --- Cache/loading stats
' | Requests: ' + stats.numberOfPendingRequests +
', Attempted: ' + stats.numberOfAttemptedRequests +
', Processing: ' + stats.numberProcessing +
', Content Ready: ' + stats.numberContentReady +
// Total number of tiles includes tiles without content, so "Ready" may never reach
Expand All @@ -1541,6 +1588,7 @@ define([
last.visited = stats.visited;
last.numberOfCommands = stats.numberOfCommands;
last.selected = tileset._selectedTiles.length;
last.numberOfAttemptedRequests = stats.numberOfAttemptedRequests;
last.numberOfPendingRequests = stats.numberOfPendingRequests;
last.numberProcessing = stats.numberProcessing;
last.numberContentReady = stats.numberContentReady;
Expand Down Expand Up @@ -1623,11 +1671,21 @@ define([
var lastNumberOfPendingRequest = stats.lastColor.numberOfPendingRequests;
var lastNumberProcessing = stats.lastColor.numberProcessing;

if ((numberOfPendingRequests !== lastNumberOfPendingRequest) || (numberProcessing !== lastNumberProcessing)) {
var progressChanged = (numberOfPendingRequests !== lastNumberOfPendingRequest) || (numberProcessing !== lastNumberProcessing);

if (progressChanged) {
frameState.afterRender.push(function() {
tileset.loadProgress.raiseEvent(numberOfPendingRequests, numberProcessing);
});
}

tileset._tilesLoaded = (stats.numberOfPendingRequests === 0) && (stats.numberProcessing === 0) && (stats.numberOfAttemptedRequests === 0);

if (progressChanged && tileset._tilesLoaded) {
frameState.afterRender.push(function() {
tileset.allTilesLoaded.raiseEvent();
});
}
}

///////////////////////////////////////////////////////////////////////////
Expand Down Expand Up @@ -1667,7 +1725,7 @@ define([
*/
Cesium3DTileset.prototype.update = function(frameState) {
// TODO: Support 2D and CV
if (!this.show || !defined(this._root) || (frameState.mode !== SceneMode.SCENE3D)) {
if (!this.show || !this.ready || (frameState.mode !== SceneMode.SCENE3D)) {
return;
}

Expand Down
22 changes: 14 additions & 8 deletions Specs/Cesium3DTilesTester.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,11 +83,21 @@ define([
expectRender(scene, tileset);
};

Cesium3DTilesTester.waitForPendingRequests = function(scene, tileset) {
Cesium3DTilesTester.waitForTilesLoaded = function(scene, tileset) {
return pollToPromise(function() {
scene.renderForSpecs();
var stats = tileset._statistics;
return ((stats.numberOfPendingRequests === 0) && (stats.numberProcessing === 0));
return tileset.tilesLoaded;
}).then(function() {
return tileset;
});
};

Cesium3DTilesTester.waitForReady = function(scene, tileset) {
return pollToPromise(function() {
scene.renderForSpecs();
return tileset.ready;
}).then(function() {
return tileset;
});
};

Expand All @@ -97,11 +107,7 @@ define([
url : url
}));

return tileset.readyPromise.then(function() {
return Cesium3DTilesTester.waitForPendingRequests(scene, tileset).then(function() {
return tileset;
});
});
return Cesium3DTilesTester.waitForTilesLoaded(scene, tileset);
};

Cesium3DTilesTester.loadTileExpectError = function(scene, arrayBuffer, type) {
Expand Down
Loading

0 comments on commit 558ea1c

Please sign in to comment.