1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180
| import * as Cesium from "cesium";
export default class WeatherChange { constructor(viewer) { this.viewer = viewer; }
status = 0;
snowStage = null;
rainStage = null;
fogStage = null;
sunnyStage = null;
remove() { if (this.status == 0) return; this.viewer.shadowMap.darkness = 0.0; this.viewer.shadows = false; let collection = this.viewer.scene.postProcessStages; switch (this.status) { case 4: collection.remove(this.snowStage); case 3: collection.remove(this.rainStage); case 5: collection.remove(this.fogStage); } this.status = 0; }
snow(vsnow = 60) { console.log("下雪咯~") this.remove() this.viewer.shadowMap.darkness = 0.9; let collection = this.viewer.scene.postProcessStages; this.snowStage = new Cesium.PostProcessStage({ name: 'czm_snow', fragmentShader: ` uniform sampler2D colorTexture; varying vec2 v_textureCoordinates; uniform float vsnow; float snow(vec2 uv,float scale) { float time = czm_frameNumber / vsnow; float w=smoothstep(1.,0.,-uv.y*(scale/10.));if(w<.1)return 0.; uv+=time/scale;uv.y+=time*2./scale;uv.x+=sin(uv.y+time*.5)/scale; uv*=scale;vec2 s=floor(uv),f=fract(uv),p;float k=3.,d; p=.5+.35*sin(11.*fract(sin((s+p+scale)*mat2(7,3,6,5))*5.))-f;d=length(p);k=min(d,k); k=smoothstep(0.,k,sin(f.x+f.y)*0.01); return k*w; } void main(void){ vec2 resolution = czm_viewport.zw; vec2 uv=(gl_FragCoord.xy*2.-resolution.xy)/min(resolution.x,resolution.y); vec3 finalColor=vec3(0); float c = 0.0; c+=snow(uv,30.)*.0; c+=snow(uv,20.)*.0; c+=snow(uv,15.)*.0; c+=snow(uv,10.); c+=snow(uv,8.); c+=snow(uv,6.); c+=snow(uv,5.); finalColor=(vec3(c)); gl_FragColor = mix(texture2D(colorTexture, v_textureCoordinates), vec4(finalColor,1), 0.5); } `, uniforms: { vsnow: vsnow } }); this.status = 4; collection.add(this.snowStage); }
fog(fogColor = [0.8, 0.8, 0.8, 0.5], vfog = 0.5) { console.log("起雾咯~") this.remove() this.viewer.shadowMap.darkness = 0.9; let collection = this.viewer.scene.postProcessStages; this.fogStage = new Cesium.PostProcessStage({ name: 'czm_fog', fragmentShader: `uniform sampler2D colorTexture;\n\ uniform sampler2D depthTexture;\n\ uniform float vfog;\n\ uniform vec4 fogColor;\n\ varying vec2 v_textureCoordinates; \n\ void main(void) \n\ { \n\ vec4 origcolor = texture2D(colorTexture, v_textureCoordinates); \n\ float depth = czm_readDepth(depthTexture, v_textureCoordinates); \n\ vec4 depthcolor = texture2D(depthTexture, v_textureCoordinates); \n\ float f = vfog * (depthcolor.r - 0.3) / 0.2; \n\ if (f < 0.0) f = 0.3; \n\ else if (f > 1.0) f = 0.8; \n\ gl_FragColor = mix(origcolor, fogColor, f); \n\ }\n`, uniforms: { vfog: vfog, fogColor: function () { return new Cesium.Color(fogColor[0], fogColor[1], fogColor[2], fogColor[3]) }, } }); this.status = 5; collection.add(this.fogStage); }
rain() { console.log("下雨咯~") this.remove() this.viewer.shadowMap.darkness = 0.9; let collection = this.viewer.scene.postProcessStages; this.rainStage = new Cesium.PostProcessStage({ name: 'czm_rain', fragmentShader: ` uniform sampler2D colorTexture;//输入的场景渲染照片 varying vec2 v_textureCoordinates; uniform float vrain; float hash(float x){ return fract(sin(x*133.3)*13.13); } void main(void){ float time = czm_frameNumber / vrain; vec2 resolution = czm_viewport.zw; vec2 uv=(gl_FragCoord.xy*2.-resolution.xy)/min(resolution.x,resolution.y); vec3 c=vec3(.6,.7,.8); float a=0.4; float si=sin(a),co=cos(a); uv*=mat2(co,-si,si,co); uv*=length(uv+vec2(0,4.9))*.3+1.; float v=1.-sin(hash(floor(uv.x*100.))*2.); float b=clamp(abs(sin(20.*time*v+uv.y*(5./(2.+v))))-.95,0.,1.)*20.; c*=v*b; //屏幕上雨的颜色 gl_FragColor = mix(texture2D(colorTexture, v_textureCoordinates), vec4(c,1), 0.5); //将雨和三维场景融合 } `, uniforms: { vrain: function () { return 30 } } }); this.status = 3; collection.add(this.rainStage); }
sun(darkness = 0.6) { console.log("大晴天~") this.remove() this.viewer.shadows = true; this.viewer.shadowMap.enabled = true; this.viewer.shadowMap.size = 2048 * 2; this.viewer.shadowMap.darkness = darkness; this.viewer.shadowMap.softShadows = true; this.viewer.shadowMap.maximumDistance = 10000.0; this.viewer.clock.currentTime = Cesium.JulianDate.fromDate(new Date()); this.status = 1; } }
|