news 2026/7/11 20:12:32

Cesium 智慧城市着色器教程

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
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 开源仓库
版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/7/11 20:11:39

项目开发计划 V1.0 编制实战:3个核心模板与5步滚动式迭代法

项目开发计划 V1.0 编制实战:3个核心模板与5步滚动式迭代法在项目管理领域,最常听到的抱怨莫过于"计划赶不上变化"。但有趣的是,越是经验丰富的项目经理,越会在项目启动时花大量时间打磨开发计划。这看似矛盾的现象背后…

作者头像 李华
网站建设 2026/7/11 20:11:06

AI个性化学习系统架构与实现:从知识图谱到推荐算法

当"AI私立学校"开始向美国富裕家庭推销个性化学习方案时,很多人第一反应是:这不过是又一个打着AI旗号的高端教育产品。但真正值得关注的是,这种模式背后反映出的技术趋势——AI正在从辅助工具转向教育核心流程的重构者。传统教育面…

作者头像 李华
网站建设 2026/7/11 20:10:55

LR(0) 分析表构造实战:Python 实现 5 步自动生成算法(附完整代码)

LR(0)分析表构造实战:Python实现5步自动生成算法(附完整代码)1. 理解LR(0)分析的核心概念在编译原理中,语法分析是编译器前端的关键环节。LR(0)分析法作为一种自底向上的分析方法,因其强大的分析能力和相对简单的构造逻…

作者头像 李华
网站建设 2026/7/11 20:10:55

STM32数字控制DC-DC升压转换系统设计与优化

1. 项目概述与核心器件选型这个项目本质上是一个数字控制的DC-DC升压转换系统,核心是通过STM32微控制器动态调节TPS61170升压芯片的工作状态。我在工业电源设计中多次采用这种架构,它的优势在于既能保留专用电源芯片的高效特性,又能通过MCU实…

作者头像 李华
网站建设 2026/7/11 20:10:40

Neo4j AuraDB 与 Docker 社区版部署:3 种方案成本与性能实测

Neo4j AuraDB 与 Docker 社区版部署:3 种方案成本与性能实测 在当今数据驱动的商业环境中,图形数据库正成为处理复杂关联数据的首选工具。作为全球领先的图形数据库解决方案,Neo4j 提供了多种部署选项以满足不同场景需求。本文将深入对比 Neo…

作者头像 李华
网站建设 2026/7/11 20:10:00

如何高效使用openeuler/integration-test?10分钟上手核心测试用例

如何高效使用openeuler/integration-test?10分钟上手核心测试用例 【免费下载链接】integration-test The repo contains test suits for system integration test 项目地址: https://gitcode.com/openeuler/integration-test 前往项目官网免费下载&#xff…

作者头像 李华