UNPKG

@vtx/cs-map

Version:

React components for Vortex

174 lines (167 loc) 9.11 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports["default"] = void 0; var _cesium = require("cesium"); function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); } function _classCallCheck(a, n) { if (!(a instanceof n)) throw new TypeError("Cannot call a class as a function"); } function _defineProperties(e, r) { for (var t = 0; t < r.length; t++) { var o = r[t]; o.enumerable = o.enumerable || !1, o.configurable = !0, "value" in o && (o.writable = !0), Object.defineProperty(e, _toPropertyKey(o.key), o); } } function _createClass(e, r, t) { return r && _defineProperties(e.prototype, r), t && _defineProperties(e, t), Object.defineProperty(e, "prototype", { writable: !1 }), e; } function _defineProperty(e, r, t) { return (r = _toPropertyKey(r)) in e ? Object.defineProperty(e, r, { value: t, enumerable: !0, configurable: !0, writable: !0 }) : e[r] = t, e; } function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : i + ""; } function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); } /** 0-无效果,1-下雪,2-下雨,3-雾霾 */ var Weather = exports["default"] = /*#__PURE__*/function () { function Weather(viewer) { _classCallCheck(this, Weather); _defineProperty(this, "viewer", void 0); /** 天气状态 */ _defineProperty(this, "status", 0); /** 下雪阶段 */ _defineProperty(this, "snowStage", null); /** 下雨阶段 */ _defineProperty(this, "rainStage", null); /** 雾霾阶段 */ _defineProperty(this, "fogStage", null); this.viewer = viewer; } return _createClass(Weather, [{ key: "snow", value: /** * @name: snow * @Date: 2023-09-01 15:27:58 * @description: 下雪 * @param {number} snowSize 大小 * @param {number} snowSpeed 速度 */ function snow(_ref) { var snowSize = _ref.snowSize, snowSpeed = _ref.snowSpeed; snowSize = snowSize || 0.02; snowSpeed = snowSpeed || 30.0; this.remove(); this.status = 1; this.snowStage = new _cesium.PostProcessStage({ name: 'snow', fragmentShader: "uniform sampler2D colorTexture;\n varying vec2 v_textureCoordinates;\n uniform float snowSpeed;\n uniform float snowSize;\n float snow(vec2 uv,float scale)\n {\n float time=czm_frameNumber/snowSpeed;\n float w=smoothstep(1.,0.,-uv.y*(scale/10.));if(w<.1)return 0.;\n uv+=time/scale;uv.y+=time*2./scale;uv.x+=sin(uv.y+time*.5)/scale;\n uv*=scale;vec2 s=floor(uv),f=fract(uv),p;float k=3.,d;\n 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 k=smoothstep(0.,k,sin(f.x+f.y)*snowSize);\n return k*w;\n }\n void main(void){\n vec2 resolution=czm_viewport.zw;\n vec2 uv=(gl_FragCoord.xy*2.-resolution.xy)/min(resolution.x,resolution.y);\n vec3 finalColor=vec3(0);\n //float c=smoothstep(1.,0.3,clamp(uv.y*.3+.8,0.,.75));\n float c=0.;\n c+=snow(uv,30.)*.0;\n c+=snow(uv,20.)*.0;\n c+=snow(uv,15.)*.0;\n c+=snow(uv,10.);\n c+=snow(uv,8.);\n c+=snow(uv,6.);\n c+=snow(uv,5.);\n finalColor=(vec3(c));\n gl_FragColor=mix(texture2D(colorTexture,v_textureCoordinates),vec4(finalColor,1),.5);\n }\n ", uniforms: { snowSize: snowSize, snowSpeed: snowSpeed } }); this.viewer.scene.postProcessStages.add(this.snowStage); } /** * @name: rain * @Date: 2023-09-01 15:37:01 * @description: 下雨 * @param {number} tiltAngle 倾斜角度 * @param {number} rainSize 大小 * @param {number} rainSpeed 速度 */ }, { key: "rain", value: function rain(_ref2) { var tiltAngle = _ref2.tiltAngle, rainSize = _ref2.rainSize, rainSpeed = _ref2.rainSpeed; tiltAngle = tiltAngle || -0.6; rainSize = rainSize || 0.3; rainSpeed = rainSpeed || 60.0; this.remove(); this.status = 2; this.rainStage = new _cesium.PostProcessStage({ name: 'rain', fragmentShader: "uniform sampler2D colorTexture;\n varying vec2 v_textureCoordinates;\n uniform float tiltAngle;\n uniform float rainSize;\n uniform float rainSpeed;\n float hash(float x) {\n return fract(sin(x * 133.3) * 13.13);\n }\n void main(void) {\n float time = czm_frameNumber / rainSpeed;\n vec2 resolution = czm_viewport.zw;\n vec2 uv = (gl_FragCoord.xy * 2. - resolution.xy) / min(resolution.x, resolution.y);\n vec3 c = vec3(.6, .7, .8);\n float a = tiltAngle;\n float si = sin(a), co = cos(a);\n uv *= mat2(co, -si, si, co);\n uv *= length(uv + vec2(0, 4.9)) * rainSize + 1.;\n float v = 1. - sin(hash(floor(uv.x * 100.)) * 2.);\n float b = clamp(abs(sin(20. * time * v + uv.y * (5. / (2. + v)))) - .95, 0., 1.) * 20.;\n c *= v * b;\n gl_FragColor = mix(texture2D(colorTexture, v_textureCoordinates), vec4(c, 1), .2);\n }\n ", uniforms: { tiltAngle: tiltAngle, rainSize: rainSize, rainSpeed: rainSpeed } }); this.viewer.scene.postProcessStages.add(this.rainStage); } /** * @name: fog * @Date: 2023-09-01 15:37:33 * @description: 雾霾 * @param {number} visibility 可见度 * @param {Color} fogColor 颜色 */ }, { key: "fog", value: function fog(_ref3) { var visibility = _ref3.visibility, fogColor = _ref3.fogColor; visibility = visibility || 0.1; fogColor = fogColor || new _cesium.Color(0.8, 0.8, 0.8, 0.5); this.remove(); this.status = 3; this.fogStage = new _cesium.PostProcessStage({ name: 'fog', fragmentShader: "uniform sampler2D colorTexture;\n uniform sampler2D depthTexture;\n uniform float visibility;\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 = visibility * (depthcolor.r - 0.3) / 0.2; \n if (f < 0.0) f = 0.0; \n else if (f > 1.0) f = 1.0; \n gl_FragColor = mix(origcolor, fogColor, f); \n }\n\n ", uniforms: { visibility: visibility, fogColor: fogColor } }); this.viewer.scene.postProcessStages.add(this.fogStage); } }, { key: "setValue", value: function setValue(value, options) { options = options || {}; this.remove(); // 移除 if (this[value]) { this[value](options); } } /** * @name: remove * @Date: 2023-09-01 16:01:30 * @description: 移除天气 */ }, { key: "remove", value: function remove() { if (this.status == 0) return; switch (this.status) { case 1: this.viewer.scene.postProcessStages.remove(this.snowStage); break; case 2: this.viewer.scene.postProcessStages.remove(this.rainStage); break; case 3: this.viewer.scene.postProcessStages.remove(this.fogStage); break; } this.status = 0; } /** * @name: show * @Date: 2023-09-01 17:38:44 * @description: 控制显示隐藏 * @param {boolean} visible 是否显示 */ }, { key: "show", value: function show(visible) { if (this.status == 0) return; switch (this.status) { case 1: this.snowStage.enabled = visible; break; case 2: this.rainStage.enabled = visible; break; case 3: this.fogStage.enabled = visible; break; } } }]); }(); //# sourceMappingURL=Weather.js.map