flood.js 3.98 KB
import objectManage from './maputils';
export default class flood {
    constructor() {
        this.positions2d = [];
        this.handlerPolygon = new Cesium.DrawHandler(objectManage.viewer, Cesium.DrawMode.Polygon);
        this.handlerPolygon.drawEvt.addEventListener(polygon => {
			var array = [].concat(polygon.object.positions), positions = [];
			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);
				var h=cartographic.height;
				if(positions.indexOf(longitude)===-1&&positions.indexOf(latitude)===-1){
					positions.push(longitude);
					positions.push(latitude);
					positions.push(h);
					this.positions2d.push(longitude);
					this.positions2d.push(latitude);
					this.positions2d.push(1000.0);
				}
			}
			return positions;
		});
    }
    initHyp() {
        var hyp = new Cesium.HypsometricSetting();
        //设置显示模式
        hyp.DisplayMode = Cesium.HypsometricSettingEnum.DisplayMode.FACE;
        //设置线颜色为红色
        hyp._lineColor = new Cesium.Color(1.0, 0.0, 0.0, 1.0);
        //设置最大/最小可见高度
        hyp.MinVisibleValue = 0;
        //设置颜色表的最大/最小key值,表示高度
        hyp.ColorTableMinKey = 1;
        hyp.ColorTableMaxKey = 9000;
        //新建颜色表
        var colorTable = new Cesium.ColorTable();
        var height = 1;
        //每隔200m向颜色表插入一个随机色
        for(var i = 0; i<90; i++){
            height += 200;
            colorTable.insert(height, getRandomColor());
        }
        //返回随机颜色
        function getRandomColor(){
            return new Cesium.Color(Math.random(), Math.random(), Math.random());
        }
        hyp.ColorTable = colorTable;
        hyp.Opacity = 0.8;
        //等高线间隔为200m
        hyp.LineInterval = 200.0;
        return hyp;
    }
    floodCal(entities, positions2d, waterHeight, targetHeight) {
        let entity = entities.add({
            polygon : {
                hierarchy : new Cesium.PolygonHierarchy(Cesium.Cartesian3.fromDegreesArrayHeights(positions2d)),
                perPositionHeight: true,
                extrudedHeight: new Cesium.CallbackProperty(function(){
                    waterHeight+=20;
                    if(waterHeight>targetHeight){
                        waterHeight=targetHeight;//给个最大值
                    }
                    return waterHeight
                }, false),
                material: new Cesium.ImageMaterialProperty({
                    image: './images/waterNormals.jpg',
                    repeat : new Cesium.Cartesian2(1, 1),
                    transparent: true
                })
            }
        });
    }
    floodParse(scene, positions2d, waterHeight, targetHeight) {
        var River1 = new Cesium.Primitive({
            geometryInstances : new Cesium.GeometryInstance({
                geometry :new Cesium.PolygonGeometry({
                    polygonHierarchy : new Cesium.PolygonHierarchy(Cesium.Cartesian3.fromDegreesArrayHeights(positions2d)),
                    extrudedHeight: targetHeight,
                    height: waterHeight,
                    vertexFormat : Cesium.EllipsoidSurfaceAppearance.VERTEX_FORMAT
                })
            }),
            appearance : new Cesium.EllipsoidSurfaceAppearance({
                aboveGround : true,
                material: new Cesium.Material({
                    fabric : {
                        type : 'Water',
                        uniforms : {
                            normalMap:'./images/waterNormals.jpg',
                            frequency: 100.0,
                            animationSpeed: 0.01,
                            amplitude: 10.0
                        }
                    }
                })
            }),
            show : true
        });
        scene.primitives.add(River1);
    }
}