Hi, can anyone see from the code below why my terrain is not being flattened if it falls within the red polygon?
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<!-- Include the CesiumJS JavaScript and CSS files -->
<script src="https://cesium.com/downloads/cesiumjs/releases/1.119/Build/Cesium/Cesium.js"></script>
<script src="https://cdn.jsdelivr.net/npm/@turf/turf@7.0.0"></script>
<link href="https://cesium.com/downloads/cesiumjs/releases/1.119/Build/Cesium/Widgets/widgets.css" rel="stylesheet">
</head>
<body>
<div id="cesiumContainer"></div>
<script type="text/javascript">
// Your access token can be found at: https://ion.cesium.com/tokens.
// This is the default access token from your ion account
Cesium.Ion.defaultAccessToken = 'REMOVED';
const viewer = new Cesium.Viewer('cesiumContainer', {
terrain: Cesium.Terrain.fromWorldTerrain(),
});
viewer.camera.flyTo({
destination: Cesium.Cartesian3.fromDegrees(-122.4175, 37.655, 400),
orientation: {
heading: Cesium.Math.toRadians(0.0),
pitch: Cesium.Math.toRadians(-15.0),
}
});
const mountainPolygonEntity = viewer.entities.add({
polygon: {
hierarchy: new Cesium.PolygonHierarchy(Cesium.Cartesian3.fromDegreesArray([
-122.5, 37.6,
-122.4, 37.6,
-122.4, 37.7,
-122.5, 37.7
])),
material: Cesium.Color.RED.withAlpha(0.5),
outline: true,
outlineColor: Cesium.Color.BLACK
}
});
const positions = mountainPolygonEntity.polygon.hierarchy.getValue().positions;
const edge1 = Cesium.Cartesian3.subtract(positions[1], positions[0], new Cesium.Cartesian3());
const edge2 = Cesium.Cartesian3.subtract(positions[2], positions[1], new Cesium.Cartesian3());
const normal = Cesium.Cartesian3.normalize(Cesium.Cartesian3.cross(edge1, edge2, new Cesium.Cartesian3()), new Cesium.Cartesian3());
const clippingPlanes = new Cesium.ClippingPlaneCollection({
planes: [new Cesium.Plane(normal, 0)]
});
const terrainProvider = new Cesium.CesiumTerrainProvider({
terrain: Cesium.Terrain.fromWorldTerrain(),
requestVertexNormals: true,
requestWaterMask: true,
hasWaterMask: true,
clippingPlanes: clippingPlanes
});
Promise.resolve(viewer.terrainProvider.readyPromise).then(function () {
const updateHeightPromises = mountainPolygonEntity.polygon.hierarchy.getValue().positions.map(position => {
const cartographic = Cesium.Cartographic.fromCartesian(position);
const longitude = cartographic.longitude;
const latitude = cartographic.latitude;
// Set the height to 0 to flatten the terrain
const flattenedHeight = 0;
return viewer.terrainProvider.requestTileGeometry(new Cesium.Cartographic(longitude, latitude, 0))
.then(function (updatedGeometry) {
console.log(updatedGeometry);
if (updatedGeometry && updatedGeometry.terrainData && updatedGeometry.terrainData.length > 0) {
const terrainHeightSample = updatedGeometry.terrainData[0]; // You might need to determine the index based on the provided geometry
// Update the terrain height in the red polygon area to the flattened height
terrainHeightSample.height = flattenedHeight;
return Cesium.Cartographic.fromRadians(longitude, latitude, flattenedHeight);
}
});
});
Promise.all(updateHeightPromises).then(updatedPositions => {
mountainPolygonEntity.polygon.hierarchy = new Cesium.PolygonHierarchy(updatedPositions);
});
});
</script>
</body>
</html>