Weather.js 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. /* 引入Cesium */
  2. // import * as Cesium from 'Cesium';
  3. /**
  4. * 天气场景
  5. */
  6. class Weather {
  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. }
  15. /**
  16. * 雨天特效
  17. * @ignore 忽略注释,注释不生成Doc
  18. */
  19. _initRain() {
  20. this.rainStage = new Cesium.PostProcessStage({
  21. name: 'jt_rain',
  22. fragmentShader: this._rain(),
  23. uniforms: {
  24. tiltAngle: () => {
  25. return this.tiltAngle;
  26. },
  27. rainSize: () => {
  28. return this.rainSize;
  29. },
  30. rainSpeed: () => {
  31. return this.rainSpeed;
  32. }
  33. }
  34. });
  35. this._viewer.scene.postProcessStages.add(this.rainStage);
  36. }
  37. /**
  38. * 雪天特效
  39. * @ignore 忽略注释,注释不生成Doc
  40. */
  41. _initSnow() {
  42. this.snowStage = new Cesium.PostProcessStage({
  43. name: 'jt_snow',
  44. fragmentShader: this._snow(),
  45. uniforms: {
  46. snowSize: () => {
  47. return this.snowSize;
  48. },
  49. snowSpeed: () => {
  50. return this.snowSpeed;
  51. }
  52. }
  53. });
  54. this._viewer.scene.postProcessStages.add(this.snowStage);
  55. }
  56. /**
  57. * 雾天特效
  58. * @ignore 忽略注释,注释不生成Doc
  59. */
  60. _initFog() {
  61. this.fogStage = new Cesium.PostProcessStage({
  62. name: 'jt_fog',
  63. fragmentShader: this._fog(),
  64. uniforms: {
  65. visibility: () => {
  66. return this.visibility;
  67. },
  68. fogColor: () => {
  69. return this.color;
  70. }
  71. }
  72. });
  73. this._viewer.scene.postProcessStages.add(this.fogStage);
  74. }
  75. /**
  76. * 雨天粒子效果
  77. * @ignore 忽略注释,注释不生成Doc
  78. */
  79. _rain() {
  80. return "uniform sampler2D colorTexture;\n\
  81. varying vec2 v_textureCoordinates;\n\
  82. uniform float tiltAngle;\n\
  83. uniform float rainSize;\n\
  84. uniform float rainSpeed;\n\
  85. float hash(float x) {\n\
  86. return fract(sin(x * 133.3) * 13.13);\n\
  87. }\n\
  88. void main(void) {\n\
  89. float time = czm_frameNumber / rainSpeed;\n\
  90. vec2 resolution = czm_viewport.zw;\n\
  91. vec2 uv = (gl_FragCoord.xy * 2. - resolution.xy) / min(resolution.x, resolution.y);\n\
  92. vec3 c = vec3(.6, .7, .8);\n\
  93. float a = tiltAngle;\n\
  94. float si = sin(a), co = cos(a);\n\
  95. uv *= mat2(co, -si, si, co);\n\
  96. uv *= length(uv + vec2(0, 4.9)) * rainSize + 1.;\n\
  97. float v = 1. - sin(hash(floor(uv.x * 100.)) * 2.);\n\
  98. float b = clamp(abs(sin(20. * time * v + uv.y * (5. / (2. + v)))) - .95, 0., 1.) * 20.;\n\
  99. c *= v * b;\n\
  100. gl_FragColor = mix(texture2D(colorTexture, v_textureCoordinates), vec4(c, 1), .5);\n\
  101. }\n\
  102. ";
  103. }
  104. /**
  105. * 雪天粒子效果
  106. * @ignore 忽略注释,注释不生成Doc
  107. */
  108. _snow() {
  109. return "uniform sampler2D colorTexture;\n\
  110. varying vec2 v_textureCoordinates;\n\
  111. uniform float snowSpeed;\n\
  112. uniform float snowSize;\n\
  113. float snow(vec2 uv,float scale)\n\
  114. {\n\
  115. float time=czm_frameNumber/snowSpeed;\n\
  116. float w=smoothstep(1.,0.,-uv.y*(scale/10.));if(w<.1)return 0.;\n\
  117. uv+=time/scale;uv.y+=time*2./scale;uv.x+=sin(uv.y+time*.5)/scale;\n\
  118. uv*=scale;vec2 s=floor(uv),f=fract(uv),p;float k=3.,d;\n\
  119. p=.5+.35*sin(11.*fract(sin((s+p+scale)*mat2(7,3,6,5))*5.))-f;d=length(p);k=min(d,k);\n\
  120. k=smoothstep(0.,k,sin(f.x+f.y)*snowSize);\n\
  121. return k*w;\n\
  122. }\n\
  123. void main(void){\n\
  124. vec2 resolution=czm_viewport.zw;\n\
  125. vec2 uv=(gl_FragCoord.xy*2.-resolution.xy)/min(resolution.x,resolution.y);\n\
  126. vec3 finalColor=vec3(0);\n\
  127. //float c=smoothstep(1.,0.3,clamp(uv.y*.3+.8,0.,.75));\n\
  128. float c=0.;\n\
  129. c+=snow(uv,30.)*.0;\n\
  130. c+=snow(uv,20.)*.0;\n\
  131. c+=snow(uv,15.)*.0;\n\
  132. c+=snow(uv,10.);\n\
  133. c+=snow(uv,8.);\n\
  134. c+=snow(uv,6.);\n\
  135. c+=snow(uv,5.);\n\
  136. finalColor=(vec3(c));\n\
  137. gl_FragColor=mix(texture2D(colorTexture,v_textureCoordinates),vec4(finalColor,1),.5);\n\
  138. }\n\
  139. ";
  140. }
  141. /**
  142. * 雾天粒子效果
  143. * @ignore 忽略注释,注释不生成Doc
  144. */
  145. _fog() {
  146. return "uniform sampler2D colorTexture;\n\
  147. uniform sampler2D depthTexture;\n\
  148. uniform float visibility;\n\
  149. uniform vec4 fogColor;\n\
  150. varying vec2 v_textureCoordinates; \n\
  151. void main(void) \n\
  152. { \n\
  153. vec4 origcolor = texture2D(colorTexture, v_textureCoordinates); \n\
  154. float depth = czm_readDepth(depthTexture, v_textureCoordinates); \n\
  155. vec4 depthcolor = texture2D(depthTexture, v_textureCoordinates); \n\
  156. float f = visibility * (depthcolor.r - 0.3) / 0.2; \n\
  157. if (f < 0.0) f = 0.0; \n\
  158. else if (f > 1.0) f = 1.0; \n\
  159. gl_FragColor = mix(origcolor, fogColor, f); \n\
  160. }\n";
  161. }
  162. /**
  163. * 移除雪天效果
  164. * @ignore 忽略注释,注释不生成Doc
  165. */
  166. _removeSnow() {
  167. if (!this._viewer || !this.snowStage) return;
  168. this._viewer.scene.postProcessStages.remove(this.snowStage);
  169. // this.snowStage.destroy();
  170. delete this.snowSize;
  171. delete this.snowSpeed;
  172. }
  173. /**
  174. * 移除雨天效果
  175. * @ignore 忽略注释,注释不生成Doc
  176. */
  177. _removeRain() {
  178. if (!this._viewer || !this.rainStage) return;
  179. this._viewer.scene.postProcessStages.remove(this.rainStage);
  180. // this.rainStage.destroy();
  181. delete this.tiltAngle;
  182. delete this.rainSize;
  183. delete this.rainSpeed;
  184. }
  185. /**
  186. * 移除雾天效果
  187. * @ignore 忽略注释,注释不生成Doc
  188. */
  189. _removeFog() {
  190. if (!this._viewer || !this.fogStage) return;
  191. this._viewer.scene.postProcessStages.remove(this.fogStage);
  192. // this.fogStage.destroy();
  193. delete this.visibility;
  194. delete this.color;
  195. }
  196. }
  197. /**
  198. * 通用对外公开函数
  199. */
  200. Object.assign(Weather.prototype, /** @lends Weather.prototype */ {
  201. /**
  202. * 开启雨天特效
  203. * @param {Object} options 具有以下属性:
  204. * @param {Number} [options.tiltAngle=-0.6] 倾斜角度,负数向右,正数向左
  205. * @param {Number} [options.rainSize=0.3] 雨大小
  206. * @param {Number} [options.rainSpeed=-60.0] 雨速
  207. */
  208. addRainEffect(options) {
  209. options = options || {};
  210. this.tiltAngle = Cesium.defaultValue(options.tiltAngle, -0.6);
  211. this.rainSize = Cesium.defaultValue(options.rainSize, 0.3);
  212. this.rainSpeed = Cesium.defaultValue(options.rainSpeed, 60.0);
  213. this._initRain();
  214. },
  215. /**
  216. * 开启雪天特效
  217. * @param {Object} options 具有以下属性:
  218. * @param {Number} [options.snowSize=0.02] 雪大小,最好小于0.02,默认可不写,
  219. * @param {Number} [options.snowSpeed=60.0] 雪速,默认可不写
  220. */
  221. addSnowEffect(options) {
  222. options = options || {};
  223. this.snowSize = Cesium.defaultValue(options.snowSize, 0.02);
  224. this.snowSpeed = Cesium.defaultValue(options.snowSpeed, 60.0);
  225. this._initSnow();
  226. },
  227. /**
  228. * 开启雾天特效
  229. * @param {Object} options 具有以下属性:
  230. * @param {Number} [options.visibility=-0.1]
  231. * @param {Number} [options.color=0.3]
  232. * @param {Number} [options.show]
  233. */
  234. addFogEffect(options) {
  235. options = options || {};
  236. this.visibility = Cesium.defaultValue(options.visibility, 0.1);
  237. this.color = Cesium.defaultValue(options.color, new Cesium.Color(0.8, 0.8, 0.8, 0.5));
  238. this._show = Cesium.defaultValue(options.show, !0);
  239. this._initFog();
  240. },
  241. /**
  242. * 移除天气特效
  243. */
  244. removeEffect() {
  245. if (this.snowStage) {
  246. this._removeSnow();
  247. }
  248. if (this.rainStage) {
  249. this._removeRain();
  250. }
  251. if (this.fogStage) {
  252. this._removeFog();
  253. }
  254. }
  255. });
  256. export default Weather;