当前位置: 首页 > news >正文

Cesium 烟雾效果教程

烟雾效果 ·Smoke Effect· ▶ 在线运行案例

  • 案例合集:三维可视化功能案例(threehub.cn)
  • 开源仓库github地址:https://github.com/z2586300277/three-cesium-examples
  • 400个案例代码:网盘链接

你将学到什么

  • Cesium Entity 高层实体 API

效果说明

本案例演示烟雾效果效果:基于 WebGL 实现「烟雾效果」可视化效果,附完整可运行源码;核心用到 Cesium。建议先打开文首在线案例查看动态画面,再对照下方源码逐步理解。

核心概念

  • Viewer聚合 Scene、Camera、Clock 与渲染循环,是 Cesium 应用入口。
  • Entity面向点线面/模型/标签的高层 API;与 Primitive 相比更适合交互与属性驱动。
  • 阅读下方完整源码时,建议从init/load/animate三条主线入手,再深入 shader 与工具函数。

实现步骤

  • 创建 Viewer,配置地形/影像(若案例需要)并设置初始相机
  • requestAnimationFrame循环中更新状态并 render(Cesium 为viewer.render或自动渲染)
  • 代码要点

    import * as Cesium from 'cesium'

    const box = document.getElementById('box')

    const viewer = new Cesium.Viewer(box, {

    animation: false,//是否创建动画小器件,左下角仪表

    baseLayerPicker: false,//是否显示图层选择器,右上角图层选择按钮

    baseLayer: Cesium.ImageryLayer.fromProviderAsync(Cesium.ArcGisMapServerImageryProvider.fromUrl('https://server.arcgisonline.com/arcgis/rest/services/World_Imagery/MapServer')),

    fullscreenButton: false,//是否显示全屏按钮,右下角全屏选择按钮

    timeline: false,//是否显示时间轴

    infoBox: false,//是否显示信息框

    })

    viewer.scene.debugShowFramesPerSecond = true; viewer.scene.globe.depthTestAgainstTerrain = true;

    //喷雾特效 //烟特效 class smokeEffect { constructor(viewer, obj) { 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: 25.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 = '' this.entity = this.viewer.entities.add({ //选择粒子放置的坐标 position: Cesium.Cartesian3.fromDegrees( obj.lng, obj.lat, 100 ), }); this.init(); }

    init() { const _this = this this.viewer.clock.shouldAnimate = true; this.viewer.scene.globe.depthTestAgainstTerrain = false; // this.viewer.trackedEntity = this.entity; var particleSystem = this.scene.primitives.add( new Cesium.ParticleSystem({ image: FILE_HOST + "images/channels/cloud.png",//生成所需粒子的图片路径 //粒子在生命周期开始时的颜色 startColor: Cesium.Color.BLACK.withAlpha(0.7), //粒子在生命周期结束时的颜色 endColor: Cesium.Color.WHITE.withAlpha(0.3), //粒子在生命周期开始时初始比例 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, //设置粒子的大小是否以米或像素为单位 sizeInMeters: true, //系统的粒子发射器 emitter: new Cesium.CircleEmitter(2.0),//BoxEmitter 盒形发射器,ConeEmitter 锥形发射器,SphereEmitter 球形发射器,CircleEmitter圆形发射器 //回调函数,实现各种喷泉、烟雾效果 updateCallback: (p, dt) => { return this.applyGravity(p, dt); }, }) ); this.particleSystem = particleSystem; this.preUpdateEvent() }

    //场景渲染事件 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 ); }

    applyGravity(p, dt) { var gravityScratch = new Cesium.Cartesian3() var position = p.position Cesium.Cartesian3.normalize(position, gravityScratch) Cesium.Cartesian3.fromElements( 20 * dt, 30 * dt, gravityScratch.y * dt, gravityScratch ); p.velocity = Cesium.Cartesian3.add(p.velocity, gravityScratch, p.velocity) }

    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 }

    }

    new smokeEffect(viewer, { lng: -117.16, lat: 32.71 }) viewer.camera.setView({ destination: Cesium.Cartesian3.fromDegrees(-117.16, 32.71, 300.0) });

    完整源码:GitHub

    小结

    • 本文提供烟雾效果完整 Cesium.js 源码与在线 Demo,建议先运行案例再改 uniform/参数做二次实验
    • 更多 Cesium.js 实战案例见 three-cesium-examples 合集 与 GitHub 开源仓库
http://www.jsqmd.com/news/1083902/

相关文章:

  • SMT编程太慢怎么办?小批量多品种SMT编程怎么破?
  • 1999-2025年上市公司全要素生产率数据+stata代码
  • 解锁QQ音乐加密格式:macOS用户的数字音乐自由指南
  • 按键精灵实现HMAC-SHA512加密:突破自动化脚本加密验证瓶颈
  • 20260625_091712_DeepSeek_Harness团队负责人回应_不招外国人_
  • 如何修复“您的 IP 地址已被封禁”的网络错误?
  • 阴阳师自动化脚本深度解析:如何用AI技术实现百鬼夜行智能撒豆
  • 3分钟掌握DLSS版本管理:告别游戏画质烦恼的智能解决方案
  • 如何快速解密微信聊天记录:完整的微信数据恢复指南
  • Photoshop下载教程Photoshop PS2026 保姆级安装步骤(附安装包)
  • 山东专业网上阅卷公司有哪些
  • STM32 SPI多设备片选解决方案与优化实践
  • 什么是工业级宽带电力载波?和民用载波有何区别?
  • 阴阳师自动化脚本:智能解放双手,告别重复游戏操作
  • [智能体-536]:Dify VS Coze(扣子)全维度对比:技术架构、运维部署、商业化变现、适用场景
  • 10分钟快速上手HS2-HF_Patch:Honey Select 2汉化去码补丁完全指南
  • 阴阳师自动化脚本终极指南:从入门到精通的完整使用手册
  • 1985-2024年各省市区县绿色低碳专利申请与授权量
  • LosslessCut无损视频剪辑:3分钟掌握专业级无损编辑技巧,告别画质损失烦恼
  • 无线感知与分布式LLM:边缘计算下的高效智能决策系统
  • 刘二大人pytorch教程课后作业(03—2)——随机梯度下降
  • 终极文本到图像生成工具:NMKD Stable Diffusion GUI深度解析
  • C++跨平台(三):平台检测与条件编译
  • 北京密云心血管患者的大事件!超导心磁图检查覆盖每位居民!
  • 数据驱动喊了三年,老板还在拍脑袋
  • 山西块状干冰
  • 别再用假数据喂AI了!教你用统一接口采集个人微信聊天,补充真实知识库
  • 如何在IDEA中优雅阅读:Thief-Book插件深度解析
  • 钢梁与钢梁的连接、钢柱与钢柱的连接
  • 移动端HTTPS抓包实战:从原理到工具,攻克证书绑定难题