SunshineShadow.js 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. /* 引入Cesium */
  2. // import * as Cesium from 'Cesium';
  3. /**
  4. * 光照阴影
  5. */
  6. class SunshineShadow {
  7. /**
  8. * 默认初始化
  9. * @param {Object} viewer 三维场景
  10. */
  11. constructor(viewer) {
  12. if (!viewer) throw new Cesium.DeveloperError('no viewer object!');
  13. this._viewer = viewer;
  14. this._stopTime = null;
  15. }
  16. }
  17. /**
  18. * 通用对外公开函数
  19. */
  20. Object.assign(SunshineShadow.prototype, /** @lends SunshineShadow.prototype */ {
  21. /**
  22. * 开启日照分析
  23. * @param {Object} options 具有以下属性:
  24. * @param {Number} [options.startTime] 时钟的开始时间
  25. * @param {Number} [options.stopTime] 时钟的停止时间
  26. * @param {Number} [options.speed] 每一个节拍需要多少时间,确定调用Clock#tick时前进了多少时间,负值允许向后前进。
  27. */
  28. startSunShine(options) {
  29. options = options || {};
  30. if (!Cesium.defined(options.startTime)) {
  31. options.startTime = new Date().getTime();
  32. }
  33. if (!Cesium.defined(options.stopTime)) {
  34. options.stopTime = new Date().getTime() + 1000;
  35. }
  36. if (!Cesium.defined(options.speed)) {
  37. options.speed = 1600;
  38. }
  39. if (this._stopTime) {
  40. this._viewer.clock.shouldAnimate = true;
  41. this._viewer.clock.currentTime = this._stopTime;
  42. } else {
  43. this._viewer.scene.globe.enableLighting = true; //开启全球光照,开启场景光照
  44. this._viewer.shadows = true; //开启阴影
  45. // this._viewer.terrainShadows = Cesium.ShadowMode.RECEIVE_ONLY;
  46. this._viewer.clock.startTime = Cesium.JulianDate.fromDate(new Date(options.startTime)); //时钟的开始时间
  47. this._viewer.clock.currentTime = Cesium.JulianDate.fromDate(new Date(options.startTime)); //当前时间。
  48. this._viewer.clock.stopTime = Cesium.JulianDate.fromDate(new Date(options.stopTime)); //时钟的停止时间。
  49. console.log(this._viewer.clock.currentTime)
  50. this._viewer.clock.clockRange = Cesium.ClockRange.LOOP_STOP; // 当我们到达结束时间时循环,确定当达到时钟#startTime或时钟#stopTime时,时钟应该如何表现。
  51. this._viewer.clock.clockStep = Cesium.ClockStep.SYSTEM_CLOCK_MULTIPLIER; //确定对Clock#tick的调用是依赖于帧还是依赖于系统时钟。,确定当达到时钟#startTime或时钟#stopTime时,时钟应该如何表现。
  52. this._viewer.clock.multiplier = options.speed; // 每一个节拍需要多少时间,确定调用Clock#tick时前进了多少时间,负值允许向后前进。
  53. this._viewer.clock.canAnimate = true; //指示Clock#tick是否可以提前时间。例如,如果数据正在被缓冲,这可能是错误的。只有当clock #canAnimate和clock #shouldAnimate都为真时,时钟才会滴答作响。
  54. this._viewer.clock.shouldAnimate = true; // 默认打开动画,指示Clock#tick是否应尝试提前时间。只有当clock #canAnimate和clock #shouldAnimate都为真时,时钟才会滴答作响。
  55. }
  56. },
  57. /**
  58. * 停止日照分析
  59. */
  60. stopSunShine() {
  61. this._stopTime = this._viewer.clock.currentTime;
  62. console.log(this._viewer.clock.currentTime)
  63. this._viewer.clock.shouldAnimate = false;
  64. },
  65. /**
  66. * 移除日照分析
  67. */
  68. removeSunShine() {
  69. if (this._stopTime) {
  70. this._viewer.clock.shouldAnimate = true;
  71. this._viewer.clock.currentTime = this._stopTime;
  72. console.log(this._viewer.clock.currentTime)
  73. }
  74. this._viewer.scene.globe.enableLighting = false;
  75. this._viewer.shadows = false;
  76. this._stopTime = null;
  77. // this._viewer.terrainShadows = Cesium.ShadowMode.DISABLED;
  78. },
  79. });
  80. /**
  81. * 通用对外公开函数
  82. */
  83. Object.assign(SunshineShadow.prototype, /** @lends SunshineShadow.prototype */ {
  84. /**
  85. * 开启日照分析
  86. * @param {Object} options 具有以下属性:
  87. * @param {Number} [options.startTime] 时钟的开始时间
  88. * @param {Number} [options.stopTime] 时钟的停止时间
  89. * @param {Number} [options.speed] 每一个节拍需要多少时间,确定调用Clock#tick时前进了多少时间,负值允许向后前进。
  90. */
  91. start(options) {
  92. //左下角动画控件(时间圆盘)
  93. if (document.getElementsByClassName("cesium-viewer-animationContainer").length > 0) {
  94. document.getElementsByClassName("cesium-viewer-animationContainer")[0].style.display = "block";
  95. }
  96. //下方时间线
  97. if (document.getElementsByClassName("cesium-viewer-timelineContainer").length > 0) {
  98. document.getElementsByClassName("cesium-viewer-timelineContainer")[0].style.display = "block";
  99. }
  100. // this._viewer.animation.container.style.visibility = 'visible';
  101. // this._viewer.timeline.container.style.visibility = 'visible';
  102. // this._viewer.scene.globe.enableLighting = true; //开启全球光照,开启场景光照
  103. this._viewer.shadows = true; //开启阴影
  104. options = options || {};
  105. if (!Cesium.defined(options.startTime)) {
  106. options.startTime = new Date(new Date().toLocaleDateString()).getTime(); // 当天凌晨0点
  107. console.log("当天凌晨0点", new Date(new Date().toLocaleDateString()));
  108. }
  109. if (!Cesium.defined(options.stopTime)) {
  110. options.stopTime = new Date(new Date().toLocaleDateString()).getTime() + 24 * 60 * 60 * 1000 - 1; // 当天23点59分59秒
  111. console.log("当天23点59分59秒", new Date(new Date(new Date().toLocaleDateString()).getTime() + 4 * 60 * 60 * 1000));
  112. }
  113. if (!Cesium.defined(options.speed)) {
  114. options.speed = 1600;
  115. }
  116. // 起始时间
  117. let start = Cesium.JulianDate.fromDate(new Date(options.startTime));
  118. // 结束时间
  119. let stop = Cesium.JulianDate.fromDate(new Date(options.stopTime));
  120. // 设置时间轴为模拟边界
  121. viewer.timeline.zoomTo(start, stop);
  122. //定义clock事件
  123. let clock = viewer.clock;
  124. //控制开始和停止
  125. clock.shouldAnimate = false;
  126. // 设定clock的起始时间
  127. clock.startTime = start;
  128. // 设定clock的终止时间
  129. clock.stopTime = stop;
  130. // 设定clock的当前时间
  131. clock.currentTime = start;
  132. //表示时间轴达到终点之后的行为
  133. //CLAMPED:达到终止时间后停止。
  134. //LOOP_STOP:达到终止时间后重新循环。
  135. //UNBOUNDED:达到终止时间后继续读秒。时间不仅限于设置的起始日期,可前后拖动(超出该时间轴的范围的可能)
  136. clock.clockRange = Cesium.ClockRange.UNBOUNDED;
  137. // 时间速率(快速播放与慢速播放),默认1.0,数字越大时间过的越快时间速率
  138. //若设置为86400,表示真实世界经过1秒,而在Cesium中时钟经过1天
  139. clock.multiplier = options.speed;
  140. // //时间变化就执行(即使停止时间轴仍然会执行)
  141. // clock.onTick.addEventListener(function(clock) {
  142. // //可以在这里做一些时间的监听
  143. // });
  144. // //停止时间轴执行
  145. // clock.onStop.addEventListener(function(clock) {
  146. // });
  147. },
  148. /**
  149. * 移除日照分析
  150. */
  151. remove() {
  152. //左下角动画控件(时间圆盘)
  153. if (document.getElementsByClassName("cesium-viewer-animationContainer").length > 0) {
  154. document.getElementsByClassName("cesium-viewer-animationContainer")[0].style.display = "none";
  155. }
  156. //下方时间线
  157. if (document.getElementsByClassName("cesium-viewer-timelineContainer").length > 0) {
  158. document.getElementsByClassName("cesium-viewer-timelineContainer")[0].style.display = "none";
  159. }
  160. // this._viewer.animation.container.style.visibility = 'hidden';
  161. // this._viewer.timeline.container.style.visibility = 'hidden';
  162. // this._viewer.scene.globe.enableLighting = false;
  163. this._viewer.shadows = false;
  164. this._viewer.clock.currentTime = Cesium.JulianDate.fromDate(new Date(new Date().getTime()));
  165. this._viewer.clock.shouldAnimate = false;
  166. },
  167. });
  168. export default SunshineShadow;