utils/weather/Rain.js

/*
 * 雨效果
 * @Author: jianlei wang
 * @Date: 2024-02-21 16:22:48
 * @Last Modified by: jianlei wang
 * @Last Modified time: 2024-03-28 15:15:43
 */
import { Cesium } from '../../../namespace'
import rainShader from '../shaders/weather/rain.glsl'
/**
 * 雨天气效果
 * @class
 */
class RainEffect {
  /**
   * 构造函数
   * @param {Object} viewer 地图场景对象
   */
  constructor(viewer) {
    this._viewer = viewer
    this._tiltAngle = -0.6 //倾斜角度,负数向右,正数向左
    this._rainSize = 1.0 //雨大小
    this._rainSpeed = 100.0 //雨速
  }

  /**
   * 控制雨天气显示
   * @type {Boolean}
   */
  set show(bool) {
    bool ? this._init() : this._clear()
  }

  _init() {
    this._clear()
    this._rainStage = new Cesium.PostProcessStage({
      name: 'weather_rain',
      fragmentShader: rainShader,
      uniforms: {
        tiltAngle: () => this._tiltAngle,
        rainSize: () => this._rainSize,
        rainSpeed: () => this._rainSpeed,
      },
    })
    this._viewer.scene.postProcessStages.add(this._rainStage)
  }

  _clear() {
    if (!this._viewer || !this._rainStage) return
    this._viewer.scene.postProcessStages.remove(this._rainStage)
  }
  /**
   * 销毁对象
   */
  destroy() {
    this._clear()
    this._rainStage.destroy()
  }
}
export default RainEffect