Skip to content

Commit

Permalink
Merge pull request #11078 from CesiumGS/terrain-provider-config
Browse files Browse the repository at this point in the history
Async event handler for terrain providers
  • Loading branch information
jjhembd authored Feb 14, 2023
2 parents 314b037 + a70e106 commit 5bde3b3
Show file tree
Hide file tree
Showing 78 changed files with 697 additions and 228 deletions.
18 changes: 9 additions & 9 deletions Apps/CesiumViewer/CesiumViewer.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ if (window.CESIUM_BASE_URL === undefined) {

import {
Cartesian3,
createWorldTerrainAsync,
defined,
formatError,
Math as CesiumMath,
Expand All @@ -14,6 +13,7 @@ import {
GeoJsonDataSource,
KmlDataSource,
GpxDataSource,
Terrain,
TileMapServiceImageryProvider,
Viewer,
viewerCesiumInspectorMixin,
Expand Down Expand Up @@ -50,21 +50,21 @@ async function main() {
}

const loadingIndicator = document.getElementById("loadingIndicator");
let viewer;
try {
const hasBaseLayerPicker = !defined(imageryProvider);
const hasBaseLayerPicker = !defined(imageryProvider);

const terrainProvider = await createWorldTerrainAsync({
requestWaterMask: true,
requestVertexNormals: true,
});
const terrain = Terrain.fromWorldTerrain({
requestWaterMask: true,
requestVertexNormals: true,
});

let viewer;
try {
viewer = new Viewer("cesiumContainer", {
imageryProvider: imageryProvider,
baseLayerPicker: hasBaseLayerPicker,
scene3DOnly: endUserOptions.scene3DOnly,
requestRenderMode: true,
terrainProvider: terrainProvider,
terrain: terrain,
});

if (hasBaseLayerPicker) {
Expand Down
2 changes: 1 addition & 1 deletion Apps/Sandcastle/gallery/3D Tiles Feature Picking.html
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
// A simple demo of 3D Tiles feature picking with hover and select behavior
// Building data courtesy of NYC OpenData portal: http://www1.nyc.gov/site/doitt/initiatives/3d-building.page
const viewer = new Cesium.Viewer("cesiumContainer", {
terrainProvider: await Cesium.createWorldTerrainAsync(),
terrain: Cesium.Terrain.fromWorldTerrain(),
});

viewer.scene.globe.depthTestAgainstTerrain = true;
Expand Down
2 changes: 1 addition & 1 deletion Apps/Sandcastle/gallery/3D Tiles Feature Styling.html
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ <h1>Loading...</h1>
// How to use the 3D Tiles Styling language to style individual features, like buildings.
// Styling language specification: https://github.com/CesiumGS/3d-tiles/tree/main/specification/Styling
const viewer = new Cesium.Viewer("cesiumContainer", {
terrainProvider: await Cesium.createWorldTerrainAsync(),
terrain: Cesium.Terrain.fromWorldTerrain(),
});
const handler = new Cesium.ScreenSpaceEventHandler(viewer.scene.canvas);

Expand Down
2 changes: 1 addition & 1 deletion Apps/Sandcastle/gallery/3D Tiles Inspector.html
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
//Sandcastle_Begin
// Building data courtesy of NYC OpenData portal: http://www1.nyc.gov/site/doitt/initiatives/3d-building.page
const viewer = new Cesium.Viewer("cesiumContainer", {
terrainProvider: await Cesium.createWorldTerrainAsync(),
terrain: Cesium.Terrain.fromWorldTerrain(),
});

viewer.scene.globe.depthTestAgainstTerrain = true;
Expand Down
2 changes: 1 addition & 1 deletion Apps/Sandcastle/gallery/3D Tiles Interactivity.html
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@
"use strict";
//Sandcastle_Begin
const viewer = new Cesium.Viewer("cesiumContainer", {
terrainProvider: await Cesium.createWorldTerrainAsync(),
terrain: Cesium.Terrain.fromWorldTerrain(),
});

const scene = viewer.scene;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
const viewer = new Cesium.Viewer("cesiumContainer", {
infoBox: false,
orderIndependentTranslucency: false,
terrainProvider: await Cesium.createWorldTerrainAsync(),
terrain: Cesium.Terrain.fromWorldTerrain(),
});

viewer.clock.currentTime = Cesium.JulianDate.fromIso8601(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
//Sandcastle_Begin
// An example of using a b3dm tileset to classify another b3dm tileset.
const viewer = new Cesium.Viewer("cesiumContainer", {
terrainProvider: await Cesium.createWorldTerrainAsync(),
terrain: Cesium.Terrain.fromWorldTerrain(),
});

// A normal b3dm tileset containing photogrammetry
Expand Down
2 changes: 1 addition & 1 deletion Apps/Sandcastle/gallery/3D Tiles Photogrammetry.html
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
"use strict";
//Sandcastle_Begin
const viewer = new Cesium.Viewer("cesiumContainer", {
terrainProvider: await Cesium.createWorldTerrainAsync(),
terrain: Cesium.Terrain.fromWorldTerrain(),
});

const tileset = new Cesium.Cesium3DTileset({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
//Sandcastle_Begin
// An example showing a point cloud tileset classified by a Geometry tileset.
const viewer = new Cesium.Viewer("cesiumContainer", {
terrainProvider: await Cesium.createWorldTerrainAsync(),
terrain: Cesium.Terrain.fromWorldTerrain(),
});

//Point Cloud by Prof. Peter Allen, Columbia University Robotics Lab. Scanning by Alejandro Troccoli and Matei Ciocarlie.
Expand Down
2 changes: 1 addition & 1 deletion Apps/Sandcastle/gallery/3D Tiles Point Cloud Shading.html
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@
"use strict";
//Sandcastle_Begin
const viewer = new Cesium.Viewer("cesiumContainer", {
terrainProvider: await Cesium.createWorldTerrainAsync(),
terrain: Cesium.Terrain.fromWorldTerrain(),
});

const scene = viewer.scene;
Expand Down
2 changes: 1 addition & 1 deletion Apps/Sandcastle/gallery/3D Tiles Point Cloud.html
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
//Sandcastle_Begin
//Point Cloud by Prof. Peter Allen, Columbia University Robotics Lab. Scanning by Alejandro Troccoli and Matei Ciocarlie.
const viewer = new Cesium.Viewer("cesiumContainer", {
terrainProvider: await Cesium.createWorldTerrainAsync(),
terrain: Cesium.Terrain.fromWorldTerrain(),
});

const tileset = new Cesium.Cesium3DTileset({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
"use strict";
//Sandcastle_Begin
const viewer = new Cesium.Viewer("cesiumContainer", {
terrainProvider: await Cesium.createWorldTerrainAsync(),
terrain: Cesium.Terrain.fromWorldTerrain(),
});

const geocoder = viewer.geocoder.viewModel;
Expand Down
12 changes: 8 additions & 4 deletions Apps/Sandcastle/gallery/ArcGIS Tiled Elevation Terrain.html
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,15 @@
window.startup = async function (Cesium) {
"use strict";
//Sandcastle_Begin
const viewer = new Cesium.Viewer("cesiumContainer", {
terrainProvider: await Cesium.ArcGISTiledElevationTerrainProvider.fromUrl(
const viewer = new Cesium.Viewer("cesiumContainer");

try {
viewer.scene.terrainProvider = await Cesium.ArcGISTiledElevationTerrainProvider.fromUrl(
"https://elevation3d.arcgis.com/arcgis/rest/services/WorldElevation3D/Terrain3D/ImageServer"
),
}); //Sandcastle_End
);
} catch (error) {
window.alert(`Failed to load terrain. ${error}`);
} //Sandcastle_End
};
if (typeof Cesium !== "undefined") {
window.startupCalled = true;
Expand Down
12 changes: 8 additions & 4 deletions Apps/Sandcastle/gallery/ArcticDEM.html
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,17 @@
window.startup = async function (Cesium) {
"use strict";
//Sandcastle_Begin
const viewer = new Cesium.Viewer("cesiumContainer", {
const viewer = new Cesium.Viewer("cesiumContainer");

try {
// High-resolution arctic terrain from the Arctic DEM project (Release 4), tiled and hosted by Cesium ion.
// https://www.pgc.umn.edu/data/arcticdem/
terrainProvider: await Cesium.CesiumTerrainProvider.fromUrl(
viewer.scene.terrainProvider = await Cesium.CesiumTerrainProvider.fromUrl(
Cesium.IonResource.fromAssetId(3956)
),
});
);
} catch (error) {
window.alert(`Failed to load terrain. ${error}`);
}

// Add Alaskan locations
Sandcastle.addDefaultToolbarMenu(
Expand Down
5 changes: 2 additions & 3 deletions Apps/Sandcastle/gallery/Atmosphere.html
Original file line number Diff line number Diff line change
Expand Up @@ -794,10 +794,9 @@
.getObservable(viewModel, "enableTerrain")
.subscribe(async function (newValue) {
if (newValue) {
const provider = await Cesium.createWorldTerrainAsync();
globe.terrainProvider = provider;
scene.setTerrain(Cesium.Terrain.fromWorldTerrain());
} else {
globe.terrainProvider = new Cesium.EllipsoidTerrainProvider();
scene.terrainProvider = new Cesium.EllipsoidTerrainProvider();
}
});
Cesium.knockout
Expand Down
27 changes: 16 additions & 11 deletions Apps/Sandcastle/gallery/Billboards.html
Original file line number Diff line number Diff line change
Expand Up @@ -247,15 +247,24 @@
});
}

let terrainProvider;
async function disableDepthTest() {
Sandcastle.declare(disableDepthTest);

terrainProvider = viewer.terrainProvider;
const worldTerrainProvider = await Cesium.createWorldTerrainAsync();
viewer.terrainProvider = worldTerrainProvider;
viewer.scene.globe.depthTestAgainstTerrain = true;

try {
const worldTerrainProvider = await Cesium.createWorldTerrainAsync();

// Return early in case a different option has been selected in the meantime
if (!viewer.scene.globe.depthTestAgainstTerrain) {
return;
}

viewer.terrainProvider = worldTerrainProvider;
} catch (error) {
window.alert(`Failed to load terrain. ${error}`);
}

viewer.entities.add({
position: Cesium.Cartesian3.fromDegrees(-122.1958, 46.1915),
billboard: {
Expand Down Expand Up @@ -351,15 +360,11 @@
},
]);

Sandcastle.reset = function () {
Sandcastle.reset = async function () {
viewer.camera.flyHome(0);
viewer.entities.removeAll();

if (Cesium.defined(terrainProvider)) {
viewer.terrainProvider = terrainProvider;
terrainProvider = undefined;
viewer.scene.globe.depthTestAgainstTerrain = false;
}
viewer.scene.terrainProvider = new Cesium.EllipsoidTerrainProvider();
viewer.scene.globe.depthTestAgainstTerrain = false;
};
//Sandcastle_End
};
Expand Down
2 changes: 1 addition & 1 deletion Apps/Sandcastle/gallery/CZML Path.html
Original file line number Diff line number Diff line change
Expand Up @@ -7244,7 +7244,7 @@
];

const viewer = new Cesium.Viewer("cesiumContainer", {
terrainProvider: await Cesium.createWorldTerrainAsync(),
terrain: Cesium.Terrain.fromWorldTerrain(),
baseLayerPicker: false,
shouldAnimate: true,
});
Expand Down
2 changes: 1 addition & 1 deletion Apps/Sandcastle/gallery/Cardboard.html
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
//Sandcastle_Begin
const viewer = new Cesium.Viewer("cesiumContainer", {
vrButton: true,
terrainProvider: await Cesium.createWorldTerrainAsync(),
terrain: Cesium.Terrain.fromWorldTerrain(),
});
// Click the VR button in the bottom right of the screen to switch to VR mode.

Expand Down
2 changes: 1 addition & 1 deletion Apps/Sandcastle/gallery/Cartographic Limit Rectangle.html
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
"use strict";
//Sandcastle_Begin
const viewer = new Cesium.Viewer("cesiumContainer", {
terrainProvider: await Cesium.createWorldTerrainAsync(),
terrain: Cesium.Terrain.fromWorldTerrain(),
});

const scene = viewer.scene;
Expand Down
2 changes: 1 addition & 1 deletion Apps/Sandcastle/gallery/Cesium Inspector.html
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
"use strict";
//Sandcastle_Begin
const viewer = new Cesium.Viewer("cesiumContainer", {
terrainProvider: await Cesium.createWorldTerrainAsync(),
terrain: Cesium.Terrain.fromWorldTerrain(),
});

const scene = viewer.scene;
Expand Down
2 changes: 1 addition & 1 deletion Apps/Sandcastle/gallery/Cesium OSM Buildings.html
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
"use strict";
//Sandcastle_Begin
const viewer = new Cesium.Viewer("cesiumContainer", {
terrainProvider: await Cesium.createWorldTerrainAsync(),
terrain: Cesium.Terrain.fromWorldTerrain(),
});

viewer.scene.primitives.add(Cesium.createOsmBuildings());
Expand Down
2 changes: 1 addition & 1 deletion Apps/Sandcastle/gallery/Cesium World Terrain.html
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
//Sandcastle_Begin
// For more information on Cesium World Terrain, see https://cesium.com/platform/cesium-ion/content/cesium-world-terrain/
const viewer = new Cesium.Viewer("cesiumContainer", {
terrainProvider: await Cesium.createWorldTerrainAsync(),
terrain: Cesium.Terrain.fromWorldTerrain(),
}); //Sandcastle_End
};
if (typeof Cesium !== "undefined") {
Expand Down
2 changes: 1 addition & 1 deletion Apps/Sandcastle/gallery/Clamp to 3D Tiles.html
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
"use strict";
//Sandcastle_Begin
const viewer = new Cesium.Viewer("cesiumContainer", {
terrainProvider: await Cesium.createWorldTerrainAsync(),
terrain: Cesium.Terrain.fromWorldTerrain(),
});
const scene = viewer.scene;
const clock = viewer.clock;
Expand Down
34 changes: 24 additions & 10 deletions Apps/Sandcastle/gallery/Clamp to Terrain.html
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,17 @@
window.startup = async function (Cesium) {
"use strict";
//Sandcastle_Begin
const viewer = new Cesium.Viewer("cesiumContainer", {
terrainProvider: await Cesium.createWorldTerrainAsync(),
});
const viewer = new Cesium.Viewer("cesiumContainer");
viewer.scene.globe.depthTestAgainstTerrain = true;

let worldTerrain;
try {
worldTerrain = await Cesium.createWorldTerrainAsync();
viewer.scene.terrainProvider = worldTerrain;
} catch (error) {
window.alert(`There was an error creating world terrain. ${error}`);
}

Sandcastle.addDefaultToolbarMenu(
[
{
Expand Down Expand Up @@ -237,7 +243,14 @@
},
{
text: "Sample line positions and draw with depth test disabled",
onselect: function () {
onselect: async function () {
if (!Cesium.defined(worldTerrain)) {
window.alert(
"Cannot sample terrain. World terrain failed to load."
);
return;
}

const length = 1000;

const startLon = Cesium.Math.toRadians(86.953793);
Expand All @@ -256,12 +269,11 @@
terrainSamplePositions.push(position);
}

Promise.resolve(
Cesium.sampleTerrainMostDetailed(
viewer.terrainProvider,
try {
const samples = await Cesium.sampleTerrainMostDetailed(
worldTerrain,
terrainSamplePositions
)
).then(function (samples) {
);
let offset = 10.0;
for (let i = 0; i < samples.length; ++i) {
samples[i].height += offset;
Expand Down Expand Up @@ -301,7 +313,9 @@
);
viewer.camera.lookAt(target, offset);
viewer.camera.lookAtTransform(Cesium.Matrix4.IDENTITY);
});
} catch (error) {
window.alert(`There was an error sampling terrain ${error}`);
}
},
},
{
Expand Down
2 changes: 1 addition & 1 deletion Apps/Sandcastle/gallery/Classification Types.html
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
"use strict";
//Sandcastle_Begin
const viewer = new Cesium.Viewer("cesiumContainer", {
terrainProvider: await Cesium.createWorldTerrainAsync(),
terrain: Cesium.Terrain.fromWorldTerrain(),
});

const tileset = new Cesium.Cesium3DTileset({
Expand Down
2 changes: 1 addition & 1 deletion Apps/Sandcastle/gallery/Classification.html
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@
"use strict";
//Sandcastle_Begin
const viewer = new Cesium.Viewer("cesiumContainer", {
terrainProvider: await Cesium.createWorldTerrainAsync(),
terrain: Cesium.Terrain.fromWorldTerrain(),
});

const scene = viewer.scene;
Expand Down
Loading

0 comments on commit 5bde3b3

Please sign in to comment.