"use strict";

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); }
Object.defineProperty(exports, "__esModule", {
  value: true
});
exports["default"] = void 0;
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, _toPropertyKey(descriptor.key), descriptor); } }
function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); Object.defineProperty(Constructor, "prototype", { writable: false }); return Constructor; }
function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
function _toPropertyKey(arg) { var key = _toPrimitive(arg, "string"); return _typeof(key) === "symbol" ? key : String(key); }
function _toPrimitive(input, hint) { if (_typeof(input) !== "object" || input === null) return input; var prim = input[Symbol.toPrimitive]; if (prim !== undefined) { var res = prim.call(input, hint || "default"); if (_typeof(res) !== "object") return res; throw new TypeError("@@toPrimitive must return a primitive value."); } return (hint === "string" ? String : Number)(input); }
var _Cesium = Cesium,
  PostProcessStage = _Cesium.PostProcessStage,
  Color = _Cesium.Color;
;

/** 0-无效果，1-下雪，2-下雨，3-雾霾 */
var Weather = /*#__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;
  }
  _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 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 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 Color(0.8, 0.8, 0.8, 0.5);
      this.remove();
      this.status = 3;
      this.fogStage = new 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;
      }
    }
  }]);
  return Weather;
}();
exports["default"] = Weather;
//# sourceMappingURL=Weather.js.map
