heightControl.js 2.4 KB
import objectManage from './maputils';
export default class heightControl {
    constructor(opt) {
        this.handlerPolygon = new Cesium.DrawHandler(objectManage.viewer, Cesium.DrawMode.Polygon);
        this.building = objectManage.viewer.scene.layers.find("build1");
        this.building1 = objectManage.viewer.scene.layers.find("build2");
        this.arr = [];
        var i = 0;
        while(i < 2000) this.arr.push(i++);
        this.handlerPolygon.drawEvt.addEventListener(result => {
            var array = [].concat(result.object.positions), positions = [];
            var polygon = result.object;
            if(!polygon) return;
            polygon.show = false;
            this.handlerPolygon.polyline.show = false;
			for(var i = 0, len = array.length; i < len; i++){
				var cartographic = Cesium.Cartographic.fromCartesian(array[i]);
				var longitude = Cesium.Math.toDegrees(cartographic.longitude);
				var latitude = Cesium.Math.toDegrees(cartographic.latitude);
				if(positions.indexOf(longitude)===-1&&positions.indexOf(latitude)===-1){
					positions.push(longitude);
					positions.push(latitude);
				}
            }
            this.addPolygon(Object.assign(opt, {p: positions}));
            this.splice(opt.h);
		});
    }
    addPolygon(opt) {
        this.polygon = objectManage.viewer.entities.add({
            id: 'polygonA',
            polygon: {
                hierarchy: Cesium.Cartesian3.fromDegreesArray(opt.p),
                height: opt.h,
                material: new Cesium.Color(1, 1, 0.20, 0.5),
                outline: true,
                outlineColor: Cesium.Color.RED
            }
        }).polygon;
    }
    splice(h) {
        this.building.clipLineColor = Cesium.Color.WHITE.withAlpha(0.0);
        this.building1.setObjsColor(this.arr, Cesium.Color.DARKORANGE.withAlpha(0.5));
        this.building.setCustomClipBox({
            dimensions: new Cesium.Cartesian3(5000, 5000, h * 2),
            position: Cesium.Cartesian3.fromDegrees(116.44391163897568, 39.922128887755996, h / h - 1),
            clipMode: "clip_behind_any_plane"
        });
        this.building1.setCustomClipBox({
            dimensions: new Cesium.Cartesian3(5000, 5000, h * 2),
            position: Cesium.Cartesian3.fromDegrees(116.44391163897568, 39.922128887755996, h / h - 1),
            clipMode: "clip_behind_all_plane"
        });
    }
    remove() {
        objectManage.viewer.entities.removeAll();
    }
}