Cesium火焰特效

class FireEffect {
  constructor(viewer) {
    this.viewer = viewer;
    this.viewModel = {
      emissionRate: 300,
      gravity: 0.0, //设置重力参数
      minimumParticleLife: 1,
      maximumParticleLife: 2,
      minimumSpeed: 10.0, //粒子发射的最小速度
      maximumSpeed: 200.0, //粒子发射的最大速度
      startScale: 0.0,
      endScale: 2.0,
      particleSize: 50.0,
    };
    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 = null;
    this.entity = this.viewer.entities.add({
      //选择粒子放置的坐标
      position: Cesium.Cartesian3.fromDegrees(114.287848, 38.027024, 280.0),
    });
    this.init();
  }

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

  //场景渲染事件
  preUpdateEvent() {
    this.viewer.scene.preUpdate.addEventListener((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(
      0.0,
      0.0,
      0.0,
      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
  }
}

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容