Cesium自定义多边形挖地形
注意:绘制的多边形时必须是逆时针方向,而且不能有重复点
注意:绘制的凸多边形时必须是逆时针方向,而且不能有重复点(是凸多边形!!!!!!!)
效果:
代码
var points = [
new Cesium.Cartesian3(-1715292.6999753984, 4993153.157628936, 3566663.752912529),
new Cesium.Cartesian3(-1715285.8150713604, 4993167.072601330,3566647.6921528564),
new Cesium.Cartesian3(-1715286.5985765400, 4993181.309761941, 3566627.519787549),
new Cesium.Cartesian3(-1715299.0249209427, 4993191.177501195, 3566607.861264360),
new Cesium.Cartesian3(-1715349.5762367432, 4993176.675656664, 3566603.878289345),
new Cesium.Cartesian3(-1715375.5538853381, 4993159.990032478, 3566614.671147202),
new Cesium.Cartesian3(-1715370.1945772346, 4993141.041835706, 3566643.580587877),
new Cesium.Cartesian3(-1715359.7019716015, 4993131.063945592, 3566662.468046927),
new Cesium.Cartesian3(-1715321.9141253997, 4993137.762602262, 3566671.205164391)
];
var pointsLength = points.length;
var clippingPlanes = []; // 存储ClippingPlane集合
for (var i = 0; i < pointsLength; ++i) {
var nextIndex = (i + 1) % pointsLength;
var midpoint = Cesium.Cartesian3.add(points[i], points[nextIndex], new Cesium.Cartesian3());
midpoint = Cesium.Cartesian3.multiplyByScalar(midpoint, 0.5, midpoint);
var up = Cesium.Cartesian3.normalize(midpoint, new Cesium.Cartesian3());
var right = Cesium.Cartesian3.subtract(points[nextIndex], midpoint, new Cesium.Cartesian3());
right = Cesium.Cartesian3.normalize(right, right);
var normal = Cesium.Cartesian3.cross(right, up, new Cesium.Cartesian3());
normal = Cesium.Cartesian3.normalize(normal, normal);
var originCenteredPlane = new Cesium.Plane(normal, 0.0);
var distance = Cesium.Plane.getPointDistance(originCenteredPlane, midpoint);
clippingPlanes.push(new Cesium.ClippingPlane(normal, distance));
}
viewer.scene.globe.clippingPlanes = new Cesium.ClippingPlaneCollection({
planes:clippingPlanes,
edgeWidth: 1.0,
edgeColor: Cesium.Color.WHITE.withAlpha(0.7),
});