/* 引入Cesium */ // import * as Cesium from 'Cesium'; /** * 光照阴影 */ class SunshineShadow { /** * 默认初始化 * @param {Object} viewer 三维场景 */ constructor(viewer) { if (!viewer) throw new Cesium.DeveloperError('no viewer object!'); this._viewer = viewer; this._stopTime = null; } } /** * 通用对外公开函数 */ Object.assign(SunshineShadow.prototype, /** @lends SunshineShadow.prototype */ { /** * 开启日照分析 * @param {Object} options 具有以下属性: * @param {Number} [options.startTime] 时钟的开始时间 * @param {Number} [options.stopTime] 时钟的停止时间 * @param {Number} [options.speed] 每一个节拍需要多少时间,确定调用Clock#tick时前进了多少时间,负值允许向后前进。 */ startSunShine(options) { options = options || {}; if (!Cesium.defined(options.startTime)) { options.startTime = new Date().getTime(); } if (!Cesium.defined(options.stopTime)) { options.stopTime = new Date().getTime() + 1000; } if (!Cesium.defined(options.speed)) { options.speed = 1600; } if (this._stopTime) { this._viewer.clock.shouldAnimate = true; this._viewer.clock.currentTime = this._stopTime; } else { this._viewer.scene.globe.enableLighting = true; //开启全球光照,开启场景光照 this._viewer.shadows = true; //开启阴影 // this._viewer.terrainShadows = Cesium.ShadowMode.RECEIVE_ONLY; this._viewer.clock.startTime = Cesium.JulianDate.fromDate(new Date(options.startTime)); //时钟的开始时间 this._viewer.clock.currentTime = Cesium.JulianDate.fromDate(new Date(options.startTime)); //当前时间。 this._viewer.clock.stopTime = Cesium.JulianDate.fromDate(new Date(options.stopTime)); //时钟的停止时间。 console.log(this._viewer.clock.currentTime) this._viewer.clock.clockRange = Cesium.ClockRange.LOOP_STOP; // 当我们到达结束时间时循环,确定当达到时钟#startTime或时钟#stopTime时,时钟应该如何表现。 this._viewer.clock.clockStep = Cesium.ClockStep.SYSTEM_CLOCK_MULTIPLIER; //确定对Clock#tick的调用是依赖于帧还是依赖于系统时钟。,确定当达到时钟#startTime或时钟#stopTime时,时钟应该如何表现。 this._viewer.clock.multiplier = options.speed; // 每一个节拍需要多少时间,确定调用Clock#tick时前进了多少时间,负值允许向后前进。 this._viewer.clock.canAnimate = true; //指示Clock#tick是否可以提前时间。例如,如果数据正在被缓冲,这可能是错误的。只有当clock #canAnimate和clock #shouldAnimate都为真时,时钟才会滴答作响。 this._viewer.clock.shouldAnimate = true; // 默认打开动画,指示Clock#tick是否应尝试提前时间。只有当clock #canAnimate和clock #shouldAnimate都为真时,时钟才会滴答作响。 } }, /** * 停止日照分析 */ stopSunShine() { this._stopTime = this._viewer.clock.currentTime; console.log(this._viewer.clock.currentTime) this._viewer.clock.shouldAnimate = false; }, /** * 移除日照分析 */ removeSunShine() { if (this._stopTime) { this._viewer.clock.shouldAnimate = true; this._viewer.clock.currentTime = this._stopTime; console.log(this._viewer.clock.currentTime) } this._viewer.scene.globe.enableLighting = false; this._viewer.shadows = false; this._stopTime = null; // this._viewer.terrainShadows = Cesium.ShadowMode.DISABLED; }, }); /** * 通用对外公开函数 */ Object.assign(SunshineShadow.prototype, /** @lends SunshineShadow.prototype */ { /** * 开启日照分析 * @param {Object} options 具有以下属性: * @param {Number} [options.startTime] 时钟的开始时间 * @param {Number} [options.stopTime] 时钟的停止时间 * @param {Number} [options.speed] 每一个节拍需要多少时间,确定调用Clock#tick时前进了多少时间,负值允许向后前进。 */ start(options) { //左下角动画控件(时间圆盘) if (document.getElementsByClassName("cesium-viewer-animationContainer").length > 0) { document.getElementsByClassName("cesium-viewer-animationContainer")[0].style.display = "block"; } //下方时间线 if (document.getElementsByClassName("cesium-viewer-timelineContainer").length > 0) { document.getElementsByClassName("cesium-viewer-timelineContainer")[0].style.display = "block"; } // this._viewer.animation.container.style.visibility = 'visible'; // this._viewer.timeline.container.style.visibility = 'visible'; // this._viewer.scene.globe.enableLighting = true; //开启全球光照,开启场景光照 this._viewer.shadows = true; //开启阴影 options = options || {}; if (!Cesium.defined(options.startTime)) { options.startTime = new Date(new Date().toLocaleDateString()).getTime(); // 当天凌晨0点 console.log("当天凌晨0点", new Date(new Date().toLocaleDateString())); } if (!Cesium.defined(options.stopTime)) { options.stopTime = new Date(new Date().toLocaleDateString()).getTime() + 24 * 60 * 60 * 1000 - 1; // 当天23点59分59秒 console.log("当天23点59分59秒", new Date(new Date(new Date().toLocaleDateString()).getTime() + 4 * 60 * 60 * 1000)); } if (!Cesium.defined(options.speed)) { options.speed = 1600; } // 起始时间 let start = Cesium.JulianDate.fromDate(new Date(options.startTime)); // 结束时间 let stop = Cesium.JulianDate.fromDate(new Date(options.stopTime)); // 设置时间轴为模拟边界 viewer.timeline.zoomTo(start, stop); //定义clock事件 let clock = viewer.clock; //控制开始和停止 clock.shouldAnimate = false; // 设定clock的起始时间 clock.startTime = start; // 设定clock的终止时间 clock.stopTime = stop; // 设定clock的当前时间 clock.currentTime = start; //表示时间轴达到终点之后的行为 //CLAMPED:达到终止时间后停止。 //LOOP_STOP:达到终止时间后重新循环。 //UNBOUNDED:达到终止时间后继续读秒。时间不仅限于设置的起始日期,可前后拖动(超出该时间轴的范围的可能) clock.clockRange = Cesium.ClockRange.UNBOUNDED; // 时间速率(快速播放与慢速播放),默认1.0,数字越大时间过的越快时间速率 //若设置为86400,表示真实世界经过1秒,而在Cesium中时钟经过1天 clock.multiplier = options.speed; // //时间变化就执行(即使停止时间轴仍然会执行) // clock.onTick.addEventListener(function(clock) { // //可以在这里做一些时间的监听 // }); // //停止时间轴执行 // clock.onStop.addEventListener(function(clock) { // }); }, /** * 移除日照分析 */ remove() { //左下角动画控件(时间圆盘) if (document.getElementsByClassName("cesium-viewer-animationContainer").length > 0) { document.getElementsByClassName("cesium-viewer-animationContainer")[0].style.display = "none"; } //下方时间线 if (document.getElementsByClassName("cesium-viewer-timelineContainer").length > 0) { document.getElementsByClassName("cesium-viewer-timelineContainer")[0].style.display = "none"; } // this._viewer.animation.container.style.visibility = 'hidden'; // this._viewer.timeline.container.style.visibility = 'hidden'; // this._viewer.scene.globe.enableLighting = false; this._viewer.shadows = false; this._viewer.clock.currentTime = Cesium.JulianDate.fromDate(new Date(new Date().getTime())); this._viewer.clock.shouldAnimate = false; }, }); export default SunshineShadow;