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

Cesium 智慧城市着色器教程

智慧城市着色器 ·SmartCity· ▶ 在线运行案例

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

你将学到什么

  • Cesium3DTileset 加载 3D Tiles 倾斜摄影
  • 3D Tiles 流式 LOD 场景
  • requestAnimationFrame渲染循环与resize自适应

效果说明

本案例演示智慧城市着色器效果:加载倾斜摄影或人工 3D Tiles 白膜并自动定位相机;核心用到 Cesium3DTileset、3D。建议先打开文首在线案例查看动态画面,再对照下方源码逐步理解。

核心概念

  • Viewer聚合 Scene、Camera、Clock 与渲染循环,是 Cesium 应用入口。
  • Cesium3DTileset流式加载 LOD 瓦片,适合城市倾斜摄影;常用viewer.zoomTo(tileset)viewBoundingSphere定位。
  • 阅读下方完整源码时,建议从init/load/animate三条主线入手,再深入 shader 与工具函数。

实现步骤

  • 创建 Viewer,配置地形/影像(若案例需要)并设置初始相机
  • 异步加载模型 / 3D Tiles / GeoJSON 等资源并加入 scene 或 entities
  • 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,//是否显示信息框

    })

    const tileset = await Cesium.Cesium3DTileset.fromUrl(https://g2657.github.io/gz-city/tileset.json)

    viewer.scene.primitives.add(tileset)

    viewer.camera.viewBoundingSphere(tileset.boundingSphere, new Cesium.HeadingPitchRange(0, -0.1, tileset.boundingSphere.radius * 0.5))

    class SweepShader extends Cesium.CustomShader {

    constructor(opt = {}) { const { sweepColor = new Cesium.Color.fromCssColorString('green'), mixColor1 = new Cesium.Color.fromCssColorString('red'), mixColor2 = new Cesium.Color.fromCssColorString('white') } = opt;

    super({ vertexShaderText:void vertexMain(VertexInput vsInput, inout czm_modelVertexOutput vsOutput) { // 注意这里的uv,详情看本系列第一篇文章 v_uv = vec2(vsInput.attributes.positionMC.z / 80., vsInput.attributes.positionMC.z / 250.); }, fragmentShaderText:float random(vec2 st) {

    return fract(sin(dot(st.xy, vec2(12.9898, 78.233))) * 43758.5453123); } void fragmentMain(FragmentInput fsInput, inout czm_modelMaterial material) { vec3 color = vec3(0); vec3 originColor = mix(u_mix_color1, u_mix_color2, v_uv.y); float t = fract(u_time2.)2.; vec2 absUv = abs(v_uv - t); vec2 st = v_uv * 15.; vec2 ipos = floor(st + u_time * 5.); float r = random(ipos)+.2; float d = clamp(distance(0., absUv.y) / .2, 0., 1.); float diffuse = dot(czm_sunDirectionEC, fsInput.attributes.normalEC); diffuse = clamp(-diffuse, 0., .45); color += originColor; color = mix(u_sweep_colorr + u_sweep_color.8, color, d); material.diffuse = color; material.emissive = vec3(diffuse) * (1. - d); }, uniforms: { u_sweep_color: { value: sweepColor, type: Cesium.UniformType.VEC3 }, u_mix_color1: { value: mixColor1, type: Cesium.UniformType.VEC3 }, u_mix_color2: { value: mixColor2, type: Cesium.UniformType.VEC3 }, u_time: { value: 0, // initial value type: Cesium.UniformType.FLOAT },

    }, varyings: { v_selectedColor: Cesium.VaryingType.VEC3, v_uv: Cesium.VaryingType.VEC2, }, })

    this.sweepColor = sweepColor this.mixColor1 = mixColor1 this.mixColor2 = mixColor2

    const task = (t) => { this.setUniform("u_time", t * .0001) requestAnimationFrame(task) } requestAnimationFrame(task) } }

    tileset.customShader = new SweepShader()

    完整源码:GitHub

    小结

    • 本文提供智慧城市着色器完整 Cesium.js 源码与在线 Demo,建议先运行案例再改 uniform/参数做二次实验
    • 更多 Cesium.js 实战案例见 three-cesium-examples 合集 与 GitHub 开源仓库
http://www.cnnetsun.cn/news/3293281.html

相关文章:

  • 项目开发计划 V1.0 编制实战:3个核心模板与5步滚动式迭代法
  • AI个性化学习系统架构与实现:从知识图谱到推荐算法
  • LR(0) 分析表构造实战:Python 实现 5 步自动生成算法(附完整代码)
  • STM32数字控制DC-DC升压转换系统设计与优化
  • Neo4j AuraDB 与 Docker 社区版部署:3 种方案成本与性能实测
  • 如何高效使用openeuler/integration-test?10分钟上手核心测试用例
  • KASandbox核心组件深度解析:从CRI shim到编排服务的完整架构
  • 现代文件上传系统架构深度解析:WebUploader技术实现与性能优化
  • CDMA 直接序列扩频实验箱实战:m序列与Gold序列扩频增益32对比实测
  • CentOS7 容器化安装 zabbix7.0服务
  • 小说下载器终极指南:200+网站一键离线阅读完整方案
  • Go Modules 使用指南:从入门到精通
  • 终极图像标注工具选择指南:从LabelImg到现代标注平台的完整迁移路径
  • FGO自动化革命:从零到精通的Fate/Grand Automata实战指南
  • 音乐解密工具终极方案:一键解锁加密音频实现跨设备播放
  • PCIe 3.0/4.0/5.0 Block Alignment 三阶段详解:从 EIEOS 检测到 Locked 状态转换
  • ASMR资源自动下载器:3步构建你的专属音频收藏库
  • 终极指南:使用KMS_VL_ALL_AIO智能激活脚本免费激活Windows和Office
  • 复盘 SFLSOJ 20260707 coach 原题 CSU1913 一条龙送礼物
  • RUFI洗衣精华怎么选
  • 电力通信现场作业 4 类安全措施实操指南:基于《安规》的 20+ 关键条款解读
  • 工业负载控制:智能高侧开关与微控制器的实战应用
  • 3人团队如何跑赢AI算力战?Cloud架构选型踩坑实录
  • 如何用嘎嘎降AI处理生物医学论文:生物医学毕业论文降AI4.8元知网达标完整教程
  • 心理学论文降AI工具免费推荐:2026年心理学毕业论文降AI免费4.8元亲测完整指南
  • 3步极速部署:TradingAgents-CN智能投资分析平台的完整实战指南
  • 【软考设计模式】策略模式:算法族的封装与动态切换精讲
  • 腾讯Hy3大模型实战:MoE架构解析与完整部署指南
  • C语言中的for循环,看这一篇就够了!
  • 终极指南:如何用GBFR Logs伤害统计工具优化《碧蓝幻想:Relink》团队输出