/* 引入Cesium */ // import * as Cesium from 'Cesium'; /** * 飞行漫游 */ class TrackRoam { /** * 默认初始化 * @param {Object} viewer 三维场景 */ constructor(viewer) { if (!viewer) throw new Cesium.DeveloperError('no viewer object!'); this._viewer = viewer; this._draw3DObj = null; } /** * 飞行漫游路径 * @ignore 忽略注释,注释不生成Doc * @param pathsData */ _startFly(pathsData, options) { var _self = this; options = options || {}; options.time = Cesium.defaultValue(options.time, 360); options.isPathShow = Cesium.defaultValue(options.isPathShow, true); options.height = Cesium.defaultValue(options.height, 5); options.role = Cesium.defaultValue(options.role, 1); this.url = options.modelUrl; this.time = options.time; this.isPathShow = options.isPathShow; this.height = options.height; this.role = options.role; _self.clearFlyPaths(); _self._viewer.camera.setView({ destination: pathsData.position, orientation: pathsData.orientation, }); setTimeout(function() { executeFly3D(); }, 200); function executeFly3D() { if (pathsData && pathsData.geometry) { var positionA = pathsData.geometry.coordinates; /* 转换坐标 */ let positions = positionA.map(point => { return Cesium.Cartesian3.fromDegrees(point.lng, point.lat, point.height); }); //总距离 let distance = []; for (let i = 0; i < positions.length - 1; i++) { let dis = Cesium.Cartesian3.distance(positions[i], positions[i + 1]) distance.push(dis) } //点与点之间间隔时间 let times = [Cesium.JulianDate.fromDate(new Date())] times.push(Cesium.JulianDate.addSeconds(times[0], _self.time, new Cesium.JulianDate())) for (let i = 1; i < positions.length - 1; i++) { let s = Cesium.JulianDate.addSeconds(times[i], _self.time * (distance[i] / distance[0]), new Cesium.JulianDate()) times.push(s) } //一种属性,其值在给定时间内从提供的样本集和指定的插值算法和插值度中插值。 let oriSamples = new Cesium.SampledProperty(Cesium.Cartesian3); oriSamples.addSamples(times, positions); let startTime = times[0]; // 起始时间 let stopTime = times[times.length - 1]; // 结束时间 _self._viewer.clock.startTime = startTime.clone(); // 设置始时钟始时间 _self._viewer.clock.stopTime = stopTime.clone(); // 设置始终停止时间 _self._viewer.clock.currentTime = startTime.clone(); // 设置时钟当前时间 _self._viewer.clock.clockRange = Cesium.ClockRange.LOOP_STOP; //循环执行 _self._viewer.clock.multiplier = 10; // 时间速率,数字越大时间过的越快 // 计算提供的实例之间的天数差。 let timeOfResolution = 6; let samplesNum = Math.floor( Cesium.JulianDate.secondsDifference(stopTime, startTime) / timeOfResolution ); let sampledPositions = []; let sampledTimes = []; for (let i = 0; i < samplesNum + 1; i++) { let sampleTime = Cesium.JulianDate.addSeconds( startTime, i * timeOfResolution, new Cesium.JulianDate() ); let tmpPos = oriSamples.getValue(sampleTime); sampledPositions.push(Cesium.Cartographic.fromCartesian(tmpPos)); sampledTimes.push(sampleTime); } let promise = Cesium.sampleTerrainMostDetailed( _self._viewer.terrainProvider, sampledPositions ).then(() => { let carPositionProperty = new Cesium.SampledPositionProperty(); //高度 for (let i = 0; i < sampledPositions.length; i++) { sampledPositions[i].height = sampledPositions[i].height + _self.height } for (let i = 0; i < samplesNum + 1; i++) { carPositionProperty.addSample( sampledTimes[i], Cesium.Ellipsoid.WGS84.cartographicToCartesian(sampledPositions[i]) ); } var position = carPositionProperty; _self.entityFly = _self._viewer.entities.add({ // 和时间轴关联 availability: new Cesium.TimeIntervalCollection([ new Cesium.TimeInterval({ start: startTime, stop: stopTime, }), ]), // 位置 position: position, //基于位置移动自动计算方向. orientation: new Cesium.VelocityOrientationProperty(position), point: { color: Cesium.Color.RED, outlineColor: Cesium.Color.WHITE, outlineWidth: 2, pixelSize: 10, }, //路径 path: { show: _self.isPathShow, resolution: 1, //设置航线样式,线条颜色,内发光粗细,航线宽度等 material: new Cesium.PolylineGlowMaterialProperty({ glowPower: 0.1, color: Cesium.Color.YELLOW, }), width: 10, }, }); // 0自由模式 1跟随模式 2第一视角 3 第三视角 if (_self.role == 0) { } else if (_self.role == 1) { _self._viewer.trackedEntity = _self.entityFly; } else if (_self.role == 2) { _self._viewer.trackedEntity = _self.entityFly; let exection = function TimeExecution() { if (_self._viewer.clock.shouldAnimate === true) { //获取位置 let center = _self.entity.position.getValue( _self._viewer.clock.currentTime ); //获取偏向角 let orientation = _self.entity.orientation.getValue( _self._viewer.clock.currentTime ) let transform = Cesium.Transforms.eastNorthUpToFixedFrame(center); transform = Cesium.Matrix4.fromRotationTranslation(Cesium.Matrix3.fromQuaternion(orientation), center); _self._viewer.camera.lookAtTransform(transform, new Cesium.Cartesian3(-50, 0, 250)) } }; //监听时间变化 让cesium的时钟方法来监听该方法 _self._viewer.clock.onTick.addEventListener(exection); } else if (_self.role == 3) { _self._viewer.trackedEntity = _self.entityFly; let exection = function TimeExecution() { if (_self._viewer.clock.shouldAnimate === true) { //获取位置 let center = _self.entity.position.getValue( _self._viewer.clock.currentTime ); // 更新相机位置(上帝视角) _self._viewer.camera.lookAt(center, new Cesium.Cartesian3(0, 0, 1000)) } }; //监听时间变化 让cesium的时钟方法来监听该方法 _self._viewer.clock.onTick.addEventListener(exection); } }); } else { return; } } } } /** * 通用对外公开函数 */ Object.assign(TrackRoam.prototype, /** @lends TrackRoam.prototype */ { /** * 设定飞行漫游路线 */ drawFlyPaths(coordinates, options) { //异步函数 return new Promise((resolve, reject) => { let _self = this; //清除飞行路线 _self.clearFlyPaths(); let position = _self._viewer.camera.position; let heading = _self._viewer.camera.heading; let pitch = _self._viewer.camera.pitch; let roll = _self._viewer.camera.roll; var pathsData = { "orientation": { "heading": heading, "pitch": pitch, "roll": roll }, "position": position, "clampToGround": true, //开启贴地 如果有模型则贴模型 "geometry": { "type": "LineString", "coordinates": coordinates } }; _self._draw3DObj = pathsData; resolve(true); }); }, /** * 清空漫游路线 */ clearFlyPaths() { this._draw3DObj = null; this._viewer.trackedEntity = undefined; this._viewer.entities.remove(this.entityFly); //清空飞行路径模型 }, /** * 开始飞行 */ startFly(options,callError) { if (this._draw3DObj) { this._startFly(this._draw3DObj, options); } else { if (callError) callError('漫游路线不存在'); } }, /** * 暂停飞行 */ pauseFly() { var clockViewModel = this._viewer.clockViewModel; if (clockViewModel.shouldAnimate) { clockViewModel.shouldAnimate = false; } else if (this._viewer.clockViewModel.canAnimate) { clockViewModel.shouldAnimate = true; } }, /** * 向前飞行 */ forwardFly() { var clockViewModel = this._viewer.clockViewModel; var multiplier = clockViewModel.multiplier; if (multiplier < 0) { clockViewModel.multiplier = -multiplier; } clockViewModel.shouldAnimate = true; }, /** * 向后飞行 */ backwardsFly() { var clockViewModel = this._viewer.clockViewModel; var multiplier = clockViewModel.multiplier; if (multiplier > 0) { clockViewModel.multiplier = -multiplier; } clockViewModel.shouldAnimate = true; }, /** * 退出飞行 */ outFly() { var start = Cesium.JulianDate.fromDate(new Date()); this._viewer.clock.startTime = start.clone(); var stop = Cesium.JulianDate.addSeconds(start, 300000000, new Cesium.JulianDate()); this._viewer.clock.stopTime = stop.clone(); this.clearFlyPaths(); }, }); export default TrackRoam;