Skip to content

Commit

Permalink
Merge pull request #6104 from hanbollar/zoom-to-tileset
Browse files Browse the repository at this point in the history
Oneliner to Zoom to 3DTileset
  • Loading branch information
ggetz authored Jan 17, 2018
2 parents 6d9f55a + 254d8a4 commit 4440bd9
Show file tree
Hide file tree
Showing 13 changed files with 466 additions and 60 deletions.
12 changes: 5 additions & 7 deletions Apps/Sandcastle/gallery/3D Tiles Adjust Height.html
Original file line number Diff line number Diff line change
Expand Up @@ -60,13 +60,11 @@
url : '../../../Specs/Data/Cesium3DTiles/Tilesets/Tileset'
}));

tileset.readyPromise.then(function() {
var boundingSphere = tileset.boundingSphere;
viewer.camera.viewBoundingSphere(boundingSphere, new Cesium.HeadingPitchRange(0.0, -0.5, boundingSphere.radius * 2));
viewer.camera.lookAtTransform(Cesium.Matrix4.IDENTITY);
}).otherwise(function(error) {
throw(error);
});
tileset.readyPromise.then(function() {
viewer.zoomTo(tileset, new Cesium.HeadingPitchRange(0.0, -0.5, tileset.boundingSphere.radius * 2.0));
}).otherwise(function(error) {
throw(error);
});

Cesium.knockout.getObservable(viewModel, 'height').subscribe(function(height) {
height = Number(height);
Expand Down
4 changes: 1 addition & 3 deletions Apps/Sandcastle/gallery/3D Tiles BIM.html
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,7 @@
}));

tileset.readyPromise.then(function() {
var boundingSphere = tileset.boundingSphere;
viewer.camera.viewBoundingSphere(boundingSphere, new Cesium.HeadingPitchRange(0.5, -0.2, boundingSphere.radius * 4.0));
viewer.camera.lookAtTransform(Cesium.Matrix4.IDENTITY);
viewer.zoomTo(tileset, new Cesium.HeadingPitchRange(0.5, -0.2, tileset.boundingSphere.radius * 4.0));
}).otherwise(function(error) {
throw(error);
});
Expand Down
6 changes: 2 additions & 4 deletions Apps/Sandcastle/gallery/3D Tiles Batch Table Hierarchy.html
Original file line number Diff line number Diff line change
Expand Up @@ -88,10 +88,8 @@
url : '../../../Specs/Data/Cesium3DTiles/Hierarchy/BatchTableHierarchy'
}));

tileset.readyPromise.then(function() {
var boundingSphere = tileset.boundingSphere;
viewer.camera.viewBoundingSphere(boundingSphere, new Cesium.HeadingPitchRange(0.0, -0.3, 0.0));
viewer.camera.lookAtTransform(Cesium.Matrix4.IDENTITY);
viewer.zoomTo(tileset, new Cesium.HeadingPitchRange(0.0, -0.3, 0.0)).otherwise(function(error) {
throw(error);
});

var styles = [];
Expand Down
3 changes: 1 addition & 2 deletions Apps/Sandcastle/gallery/3D Tiles Clipping Planes.html
Original file line number Diff line number Diff line change
Expand Up @@ -129,8 +129,7 @@
var boundingSphere = tileset.boundingSphere;
var radius = boundingSphere.radius;

viewer.camera.viewBoundingSphere(boundingSphere, new Cesium.HeadingPitchRange(0.5, -0.2, radius * 4.0));
viewer.camera.lookAtTransform(Cesium.Matrix4.IDENTITY);
viewer.zoomTo(tileset, new Cesium.HeadingPitchRange(0.5, -0.2, radius * 4.0));

for (var i = 0; i < clippingPlanes.length; ++i) {
var plane = clippingPlanes[i];
Expand Down
6 changes: 2 additions & 4 deletions Apps/Sandcastle/gallery/3D Tiles Formats.html
Original file line number Diff line number Diff line change
Expand Up @@ -111,10 +111,8 @@
inspectorViewModel.tileset = tileset;
scene.primitives.add(tileset);
tileset.readyPromise.then(function(tileset) {
var boundingSphere = tileset.boundingSphere;
var range = Math.max(100.0 - boundingSphere.radius, 0.0); // Set a minimum offset of 100 meters
viewer.camera.viewBoundingSphere(boundingSphere, new Cesium.HeadingPitchRange(0, -2.0, range));
viewer.camera.lookAtTransform(Cesium.Matrix4.IDENTITY);

viewer.zoomTo(tileset, new Cesium.HeadingPitchRange(0, -2.0, Math.max(100.0 - tileset.boundingSphere.radius, 0.0)));

var properties = tileset.properties;
if (Cesium.defined(properties) && Cesium.defined(properties.Height)) {
Expand Down
4 changes: 1 addition & 3 deletions Apps/Sandcastle/gallery/3D Tiles Inspector.html
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,7 @@
}));

tileset.readyPromise.then(function() {
var boundingSphere = tileset.boundingSphere;
viewer.camera.viewBoundingSphere(boundingSphere, new Cesium.HeadingPitchRange(0.0, -0.5, boundingSphere.radius / 4.0));
viewer.camera.lookAtTransform(Cesium.Matrix4.IDENTITY);
viewer.zoomTo(tileset, new Cesium.HeadingPitchRange(0.0, -0.5, tileset.boundingSphere.radius / 4.0));
}).otherwise(function(error) {
throw(error);
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,7 @@
}));

// Move the camera to view the tileset on load.
tileset.readyPromise.then(function() {
var boundingSphere = tileset.boundingSphere;
viewer.camera.viewBoundingSphere(boundingSphere, new Cesium.HeadingPitchRange(0.0, -0.5, boundingSphere.radius));
viewer.camera.lookAtTransform(Cesium.Matrix4.IDENTITY);
}).otherwise(function(error) {
viewer.zoomTo(tileset).otherwise(function(error) {
throw(error);
});

Expand Down
6 changes: 1 addition & 5 deletions Apps/Sandcastle/gallery/3D Tiles Photogrammetry.html
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,7 @@
url : 'https://beta.cesium.com/api/assets/1458?access_token=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJqdGkiOiIxYmJiNTAxOC1lOTg5LTQzN2EtODg1OC0zMWJjM2IxNGNlYmMiLCJpZCI6NDQsImFzc2V0cyI6WzE0NThdLCJpYXQiOjE0OTkyNjM4MjB9.1WKijRa-ILkmG6utrhDWX6rDgasjD7dZv-G5ZyCmkKg'
}));

tileset.readyPromise.then(function() {
var boundingSphere = tileset.boundingSphere;
viewer.camera.viewBoundingSphere(boundingSphere, new Cesium.HeadingPitchRange(0.0, -0.5, boundingSphere.radius));
viewer.camera.lookAtTransform(Cesium.Matrix4.IDENTITY);
}).otherwise(function(error) {
viewer.zoomTo(tileset).otherwise(function(error) {
throw(error);
});
//Sandcastle_End
Expand Down
4 changes: 1 addition & 3 deletions Apps/Sandcastle/gallery/3D Tiles Point Cloud Styling.html
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,7 @@
}));

tileset.readyPromise.then(function() {
var boundingSphere = tileset.boundingSphere;
viewer.camera.viewBoundingSphere(boundingSphere, new Cesium.HeadingPitchRange(0.0, -1.0, 50.0));
viewer.camera.lookAtTransform(Cesium.Matrix4.IDENTITY);
viewer.zoomTo(tileset, new Cesium.HeadingPitchRange(0.0, -1.0, 50.0));
}).otherwise(function(error) {
throw(error);
});
Expand Down
6 changes: 1 addition & 5 deletions Apps/Sandcastle/gallery/3D Tiles Point Cloud.html
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,7 @@
url : 'https://beta.cesium.com/api/assets/1460?access_token=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJqdGkiOiIyMzk2YzJiOS1jZGFmLTRlZmYtYmQ4MS00NTA3NjEwMzViZTkiLCJpZCI6NDQsImFzc2V0cyI6WzE0NjBdLCJpYXQiOjE0OTkyNjQ3NTV9.oWjvN52CRQ-dk3xtvD4e8ZnOHZhoWSpJLlw115mbQJM'
}));

tileset.readyPromise.then(function() {
var boundingSphere = tileset.boundingSphere;
viewer.camera.viewBoundingSphere(boundingSphere, new Cesium.HeadingPitchRange(0.0, -0.5, boundingSphere.radius));
viewer.camera.lookAtTransform(Cesium.Matrix4.IDENTITY);
}).otherwise(function(error) {
viewer.zoomTo(tileset).otherwise(function(error) {
throw(error);
});
//Sandcastle_End
Expand Down
3 changes: 2 additions & 1 deletion CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,10 @@ Change Log
* Only one node is supported.
* Only one mesh per node is supported.
* Only one primitive per mesh is supported.
* Updated documentation links to reflect new locations on cesiumjs.org and cesium.com.
* Updated 'Viewer.zoomTo' and 'Viewer.flyTo' to take in Cesium3DTilesets as a target and updated sandcastle 3DTileset examples to reflect this change
* Fixed a glTF animation bug that caused certain animations to jitter. [#5740](https://github.com/AnalyticalGraphicsInc/cesium/pull/5740)
* Fixed a bug when creating billboard and model entities without a globe. [#6109](https://github.com/AnalyticalGraphicsInc/cesium/pull/6109)
* Updated documentation links to reflect new locations on cesiumjs.org and cesium.com.
* Added support for vertex shader uniforms when `tileset.colorBlendMode` is `MIX` or `REPLACE`. [#5874](https://github.com/AnalyticalGraphicsInc/cesium/pull/5874)
* Fixed applying a translucent style to a point cloud tileset. [#6113](https://github.com/AnalyticalGraphicsInc/cesium/pull/6113)

Expand Down
86 changes: 70 additions & 16 deletions Source/Widgets/Viewer/Viewer.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
define([
'../../Core/BoundingSphere',
'../../Core/Cartesian3',
'../../Core/Check',
'../../Core/Clock',
'../../Core/defaultValue',
'../../Core/defined',
Expand All @@ -9,6 +10,7 @@ define([
'../../Core/DeveloperError',
'../../Core/Event',
'../../Core/EventHelper',
'../../Core/HeadingPitchRange',
'../../Core/isArray',
'../../Core/Matrix4',
'../../Core/Rectangle',
Expand All @@ -20,6 +22,7 @@ define([
'../../DataSources/Entity',
'../../DataSources/EntityView',
'../../DataSources/Property',
'../../Scene/Cesium3DTileset',
'../../Scene/ImageryLayer',
'../../Scene/SceneMode',
'../../ThirdParty/knockout',
Expand All @@ -46,6 +49,7 @@ define([
], function(
BoundingSphere,
Cartesian3,
Check,
Clock,
defaultValue,
defined,
Expand All @@ -54,6 +58,7 @@ define([
DeveloperError,
Event,
EventHelper,
HeadingPitchRange,
isArray,
Matrix4,
Rectangle,
Expand All @@ -65,6 +70,7 @@ define([
Entity,
EntityView,
Property,
Cesium3DTileset,
ImageryLayer,
SceneMode,
knockout,
Expand Down Expand Up @@ -205,13 +211,13 @@ define([
if (defined(homeButton)) {
homeButton.container.style.visibility = visibility;
}
if(defined(sceneModePicker)) {
if (defined(sceneModePicker)) {
sceneModePicker.container.style.visibility = visibility;
}
if (defined(projectionPicker)) {
projectionPicker.container.style.visibility = visibility;
}
if(defined(baseLayerPicker)) {
if (defined(baseLayerPicker)) {
baseLayerPicker.container.style.visibility = visibility;
}
if (defined(animation)) {
Expand Down Expand Up @@ -356,7 +362,7 @@ define([
options = defaultValue(options, defaultValue.EMPTY_OBJECT);

var createBaseLayerPicker = (!defined(options.globe) || options.globe !== false) &&
(!defined(options.baseLayerPicker) || options.baseLayerPicker !== false);
(!defined(options.baseLayerPicker) || options.baseLayerPicker !== false);

//>>includeStart('debug', pragmas.debug);
// If using BaseLayerPicker, imageryProvider is an invalid option
Expand Down Expand Up @@ -431,7 +437,7 @@ Either specify options.terrainProvider instead or set options.baseLayerPicker to
targetFrameRate : options.targetFrameRate,
showRenderLoopErrors : options.showRenderLoopErrors,
creditContainer : defined(options.creditContainer) ? options.creditContainer : bottomContainer,
creditViewport: options.creditViewport,
creditViewport : options.creditViewport,
scene3DOnly : scene3DOnly,
terrainExaggeration : options.terrainExaggeration,
shadows : options.shadows,
Expand Down Expand Up @@ -491,7 +497,7 @@ Either specify options.terrainProvider instead or set options.baseLayerPicker to
toolbar.appendChild(geocoderContainer);
geocoder = new Geocoder({
container : geocoderContainer,
geocoderServices: defined(options.geocoder) ? (isArray(options.geocoder) ? options.geocoder : [options.geocoder]) : undefined,
geocoderServices : defined(options.geocoder) ? (isArray(options.geocoder) ? options.geocoder : [options.geocoder]) : undefined,
scene : cesiumWidget.scene
});
// Subscribe to search so that we can clear the trackedEntity when it is clicked.
Expand Down Expand Up @@ -1725,12 +1731,15 @@ Either specify options.terrainProvider instead or set options.baseLayerPicker to
* target will be the range. The heading will be determined from the offset. If the heading cannot be
* determined from the offset, the heading will be north.</p>
*
* @param {Entity|Entity[]|EntityCollection|DataSource|ImageryLayer|Promise.<Entity|Entity[]|EntityCollection|DataSource|ImageryLayer>} target The entity, array of entities, entity collection, data source or imagery layer to view. You can also pass a promise that resolves to one of the previously mentioned types.
* @param {Entity|Entity[]|EntityCollection|DataSource|ImageryLayer|Cesium3DTileset|Promise.<Entity|Entity[]|EntityCollection|DataSource|ImageryLayer|Cesium3DTileset>} target The entity, array of entities, entity collection, data source, Cesium#DTileset, or imagery layer to view. You can also pass a promise that resolves to one of the previously mentioned types.
* @param {HeadingPitchRange} [offset] The offset from the center of the entity in the local east-north-up reference frame.
* @returns {Promise.<Boolean>} A Promise that resolves to true if the zoom was successful or false if the entity is not currently visualized in the scene or the zoom was cancelled.
* @returns {Promise.<Boolean>} A Promise that resolves to true if the zoom was successful or false if the target is not currently visualized in the scene or the zoom was cancelled.
*/
Viewer.prototype.zoomTo = function(target, offset) {
return zoomToOrFly(this, target, offset, false);
var options = {
offset : offset
};
return zoomToOrFly(this, target, options, false);
};

/**
Expand All @@ -1748,12 +1757,12 @@ Either specify options.terrainProvider instead or set options.baseLayerPicker to
* target will be the range. The heading will be determined from the offset. If the heading cannot be
* determined from the offset, the heading will be north.</p>
*
* @param {Entity|Entity[]|EntityCollection|DataSource|ImageryLayer|Promise.<Entity|Entity[]|EntityCollection|DataSource|ImageryLayer>} target The entity, array of entities, entity collection, data source or imagery layer to view. You can also pass a promise that resolves to one of the previously mentioned types.
* @param {Entity|Entity[]|EntityCollection|DataSource|ImageryLayer|Cesium3DTileset|Promise.<Entity|Entity[]|EntityCollection|DataSource|ImageryLayer|Cesium3DTileset>} target The entity, array of entities, entity collection, data source, Cesium3DTileset, or imagery layer to view. You can also pass a promise that resolves to one of the previously mentioned types.
* @param {Object} [options] Object with the following properties:
* @param {Number} [options.duration=3.0] The duration of the flight in seconds.
* @param {Number} [options.maximumHeight] The maximum height at the peak of the flight.
* @param {HeadingPitchRange} [options.offset] The offset from the target in the local east-north-up reference frame centered at the target.
* @returns {Promise.<Boolean>} A Promise that resolves to true if the flight was successful or false if the entity is not currently visualized in the scene or the flight was cancelled.
* @returns {Promise.<Boolean>} A Promise that resolves to true if the flight was successful or false if the target is not currently visualized in the scene or the flight was cancelled. //TODO: Cleanup entity mentions
*/
Viewer.prototype.flyTo = function(target, options) {
return zoomToOrFly(this, target, options, true);
Expand Down Expand Up @@ -1794,6 +1803,12 @@ Either specify options.terrainProvider instead or set options.baseLayerPicker to
return;
}

//If the zoom target is a Cesium3DTileset
if (zoomTarget instanceof Cesium3DTileset) {
that._zoomTarget = zoomTarget;
return;
}

//If the zoom target is a data source, and it's in the middle of loading, wait for it to finish loading.
if (zoomTarget.isLoading && defined(zoomTarget.loadingEvent)) {
var removeEvent = zoomTarget.loadingEvent.addEventListener(function() {
Expand Down Expand Up @@ -1821,6 +1836,7 @@ Either specify options.terrainProvider instead or set options.baseLayerPicker to
zoomTarget = zoomTarget.entities.values;
}

//Zoom target is already an array, just copy it and return.
if (isArray(zoomTarget)) {
that._zoomTarget = zoomTarget.slice(0);
} else {
Expand Down Expand Up @@ -1855,20 +1871,56 @@ Either specify options.terrainProvider instead or set options.baseLayerPicker to
};

function updateZoomTarget(viewer) {
var entities = viewer._zoomTarget;
if (!defined(entities) || viewer.scene.mode === SceneMode.MORPHING) {
var target = viewer._zoomTarget;
if (!defined(target) || viewer.scene.mode === SceneMode.MORPHING) {
return;
}

var scene = viewer.scene;
var camera = scene.camera;
var zoomPromise = viewer._zoomPromise;
var zoomOptions = defaultValue(viewer._zoomOptions, {});
var options;

// If zoomTarget was Cesium3DTileset
if (target instanceof Cesium3DTileset) {
return target.readyPromise.then(function() {
var boundingSphere = target.boundingSphere;
// if offset was originally undefined then give it base value instead of empty object
if (!defined(zoomOptions.offset)) {
zoomOptions.offset = new HeadingPitchRange(0.0, -0.5, boundingSphere.radius);
}

options = {
offset : zoomOptions.offset,
duration : zoomOptions.duration,
maximumHeight : zoomOptions.maximumHeight,
complete : function() {
zoomPromise.resolve(true);
},
cancel : function() {
zoomPromise.resolve(false);
}
};

if (viewer._zoomIsFlight) {
camera.flyToBoundingSphere(target.boundingSphere, options);
} else {
camera.viewBoundingSphere(boundingSphere, zoomOptions.offset);
camera.lookAtTransform(Matrix4.IDENTITY);

// finish the promise
zoomPromise.resolve(true);
}

clearZoom(viewer);
});
}

//If zoomTarget was an ImageryLayer
if (entities instanceof Rectangle) {
var options = {
destination : entities,
if (target instanceof Rectangle) {
options = {
destination : target,
duration : zoomOptions.duration,
maximumHeight : zoomOptions.maximumHeight,
complete : function() {
Expand All @@ -1889,6 +1941,8 @@ Either specify options.terrainProvider instead or set options.baseLayerPicker to
return;
}

var entities = target;

var boundingSpheres = [];
for (var i = 0, len = entities.length; i < len; i++) {
var state = viewer._dataSourceDisplay.getBoundingSphere(entities[i], false, boundingSphereScratch);
Expand All @@ -1911,7 +1965,7 @@ Either specify options.terrainProvider instead or set options.baseLayerPicker to
var boundingSphere = BoundingSphere.fromBoundingSpheres(boundingSpheres);

if (!viewer._zoomIsFlight) {
camera.viewBoundingSphere(boundingSphere, viewer._zoomOptions);
camera.viewBoundingSphere(boundingSphere, viewer._zoomOptions.offset);
camera.lookAtTransform(Matrix4.IDENTITY);
clearZoom(viewer);
zoomPromise.resolve(true);
Expand Down
Loading

0 comments on commit 4440bd9

Please sign in to comment.