utils/particle/FireEffect.js

import { Cesium } from '../../../namespace'
import { scaleHeight } from '../Coordinate'
import { getStaticFile, randomId } from '../Generate'
/**
 * 火焰粒子类
 * @class
 */
class FireEffect {
  /**
   * 构造函数
   * @param {Object} viewer 地图场景对象
   * @param {Cartesian3} position 火焰粒子位置,笛卡尔坐标
   * @param {String} type 粒子类型:"cone"-锥型,"sphere"-球型,"box"-盒型,"circle"-圆型,
   * @param {Number} size 粒子大小
   */
  constructor(viewer, position, type, size) {
    this.viewer = viewer
    this.viewModel = {
      emissionRate: 5,
      gravity: 0.0, //设置重力参数
      minimumParticleLife: 1,
      maximumParticleLife: 6,
      minimumSpeed: 1.0, //粒子发射的最小速度
      maximumSpeed: 4.0, //粒子发射的最大速度
      startScale: 0.0,
      endScale: 10.0,
      particleSize: size,
    }
    this.emitterModelMatrix = new Cesium.Matrix4()
    this.translation = new Cesium.Cartesian3()
    this.rotation = new Cesium.Quaternion()
    this.hpr = new Cesium.HeadingPitchRoll()
    this.trs = new Cesium.TranslationRotationScale()
    this.scene = this.viewer.scene
    this.particleSystem = ''
    this.position = scaleHeight(position, size / 5, 500)
    this.entity = this.viewer.entities.add({
      //选择粒子放置的坐标
      position: position,
    })
    this.id - randomId()
    this.init(type, size)
  }

  init(type, radians) {
    const _this = this
    this.viewer.clock.shouldAnimate = true
    let particleSystem = this.scene.primitives.add(
      new Cesium.ParticleSystem({
        image: getStaticFile('fire.png'), //生成所需粒子的图片路径
        //粒子在生命周期开始时的颜色
        startColor: new Cesium.Color(1, 1, 1, 1),
        //粒子在生命周期结束时的颜色
        endColor: new Cesium.Color(0.5, 0, 0, 0),
        //粒子在生命周期开始时初始比例
        startScale: _this.viewModel.startScale,
        //粒子在生命周期结束时比例
        endScale: _this.viewModel.endScale,
        //粒子发射的最小速度
        minimumParticleLife: _this.viewModel.minimumParticleLife,
        //粒子发射的最大速度
        maximumParticleLife: _this.viewModel.maximumParticleLife,
        //粒子质量的最小界限
        minimumSpeed: _this.viewModel.minimumSpeed,
        //粒子质量的最大界限
        maximumSpeed: _this.viewModel.maximumSpeed,
        //以像素为单位缩放粒子图像尺寸
        imageSize: new Cesium.Cartesian2(
          _this.viewModel.particleSize,
          _this.viewModel.particleSize
        ),
        //每秒发射的粒子数
        emissionRate: _this.viewModel.emissionRate,
        //粒子系统发射粒子的时间(秒)
        lifetime: 16.0,
        //粒子系统是否应该在完成时循环其爆发
        loop: true,
        //设置粒子的大小是否以米或像素为单位
        sizeInMeters: true,
        //系统的粒子发射器
        emitter: this.getEmitter(type, radians),
      })
    )

    this.particleSystem = particleSystem
    this.preUpdateEvent()
  }
  getEmitter(type, val) {
    //BoxEmitter 盒形发射器,ConeEmitter 锥形发射器,SphereEmitter 球形发射器,CircleEmitter圆形发射器
    const radians = Cesium.Math.toRadians(val)
    const cartesian = Cesium.Cartesian3(val, val, val)
    let emitter
    switch (type) {
      case 'box':
        emitter = new Cesium.BoxEmitter(cartesian)
        break
      case 'sphere':
        emitter = new Cesium.SphereEmitter(radians)
        break
      case 'circle':
        emitter = new Cesium.CircleEmitter(radians)
        break
      default:
        emitter = new Cesium.ConeEmitter(radians)
        break
    }
    return emitter
  }

  //场景渲染事件
  preUpdateEvent() {
    let _this = this
    this.viewer.scene.preUpdate.addEventListener(function (scene, time) {
      //发射器地理位置
      _this.particleSystem.modelMatrix = _this.computeModelMatrix(
        _this.entity,
        time
      )
      //发射器局部位置
      _this.particleSystem.emitterModelMatrix =
        _this.computeEmitterModelMatrix()
      // 将发射器旋转
      if (_this.viewModel.spin) {
        _this.viewModel.heading += 1.0
        _this.viewModel.pitch += 1.0
        _this.viewModel.roll += 1.0
      }
    })
  }

  computeModelMatrix(entity, time) {
    return entity.computeModelMatrix(time, new Cesium.Matrix4())
  }

  computeEmitterModelMatrix() {
    this.hpr = Cesium.HeadingPitchRoll.fromDegrees(0.0, 0.0, 0.0, this.hpr)
    this.trs.translation = Cesium.Cartesian3.fromElements(
      -4.0,
      0.0,
      1.4,
      this.translation
    )
    this.trs.rotation = Cesium.Quaternion.fromHeadingPitchRoll(
      this.hpr,
      this.rotation
    )

    return Cesium.Matrix4.fromTranslationRotationScale(
      this.trs,
      this.emitterModelMatrix
    )
  }

  removeEvent() {
    this.viewer.scene.preUpdate.removeEventListener(this.preUpdateEvent, this)
    this.emitterModelMatrix = undefined
    this.translation = undefined
    this.rotation = undefined
    this.hpr = undefined
    this.trs = undefined
  }

  /**
   * 移除火焰粒子
   */
  remove() {
    ;() => {
      return this.removeEvent()
    } //清除事件
    this.viewer.scene.primitives.remove(this.particleSystem) //删除粒子对象
    this.viewer.entities.remove(this.entity) //删除entity
  }
}

export default FireEffect