CesiumJS【Pro】- #3 万家灯火

本文介绍如何利用Cesium 1.87版本后的自定义Shader功能,为3DTileset创建万家灯火的视觉效果。通过Cesium.createOsmBuildings建立三维建筑,并借助自定义纹理实现动态灯光变化。展望中提到可以结合时间控制灯光开关,增加霓虹灯效果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

万家灯火

1 说在前面

从 Cesium 1.87开始,支持了自定义Shader,可以为3DTileset 编写自己的Shader脚本代码了。在此之前,要修改的样式,只能通过属性值,简单的修改颜色(和传统gis的条件专题图一样,比如高度大于100米的建筑物表面赋予红色)别无新意。

本文通过Cesium.CustomShader,简单实现一个万家灯火的效果。

2 实现的功能

  • 自定义Shader</
以下是一个简单的万家灯火背景特效的JS代码: ```html <!DOCTYPE html> <html> <head> <title>万家灯火背景特效</title> <style type="text/css"> body { margin: 0; padding: 0; height: 100vh; background-color: #000; overflow: hidden; } canvas { position: absolute; top: 0; left: 0; z-index: -1; } </style> </head> <body> <canvas id="myCanvas"></canvas> <script type="text/javascript"> window.onload = function() { var canvas = document.getElementById(&#39;myCanvas&#39;); canvas.width = window.innerWidth; canvas.height = window.innerHeight; var ctx = canvas.getContext(&#39;2d&#39;); var particles = []; function Particle() { this.x = canvas.width * Math.random(); this.y = canvas.height * Math.random(); this.vx = (Math.random() - 0.5) * 5; this.vy = (Math.random() - 0.5) * 5; this.radius = 5; } Particle.prototype.draw = function() { ctx.beginPath(); ctx.arc(this.x, this.y, this.radius, 0, Math.PI * 2); ctx.fillStyle = &#39;rgba(255, 255, 255, 0.8)&#39;; ctx.fill(); }; Particle.prototype.update = function() { this.x += this.vx; this.y += this.vy; if (this.x < 0 || this.x > canvas.width) { this.vx *= -1; } if (this.y < 0 || this.y > canvas.height) { this.vy *= -1; } }; for (var i = 0; i < 1000; i++) { particles.push(new Particle()); } function loop() { ctx.clearRect(0, 0, canvas.width, canvas.height); for (var i = 0; i < particles.length; i++) { particles[i].draw(); particles[i].update(); } requestAnimationFrame(loop); } loop(); }; </script> </body> </html> ``` 该代码会在页面背景中生成许多随机移动的白色圆点,效果类似于万家灯火。可以通过调整代码中的参数来改变特效的效果,例如圆点数量、运动速度、颜色透明度等。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

满天飞飞

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值