Cesium实战 - 实现大气云层效果

news/2025/3/28 7:59:36/

Cesium实战 - 实现大气云层效果

  • Cesium 实现大气云层效果
    • 主要思路
    • 核心代码
    • 在线示例

Cesium 实现大气云层效果

在实际开发中,一般会有天气的效果,雨雪雾比较常见,相关的博客也很多,但是关于云层的天气效果还是比较少,而官方只有云朵效果,没有云层,本文介绍一下大气云层的效果

本文包括主要思路、核心代码和在线示例三部分。


主要思路

1. 添加矩形带有高度的矩形包裹地球。

实现思路也比较简单:既然是大气云层效果,那么必然是覆盖在地球之上的,开始想着使用球体来实现地球覆盖效果。

但是经过尝试,发现不太合适,需要将球心设置在地心,半径大于地球半径,不合理,于是放弃。

后来相当,地球范围无非是 [-180, -90, 180, 90],那么在地球上覆盖一个矩形(Rectangle)应该就可以。

尝试之后,发现非常完美;大气云层应该有高度,给矩形设置高度即可。

2. 给矩形设置动态效果,使云层动起来。

添加矩形之后,添加图片材质,虽然看起来是云层,但是云层并没有动,看起来不太合理。

于是,通过着色器给图片材质增加了一个动态效果,这样看起来云层是动态的,比较切近实际。

在这里插入图片描述


核心代码

1. 初始化以及参数。

// 初始化地球
const viewer = new Cesium.Viewer("cesiumContainer");
const entities = viewer.entities;// 云层图片
const image = 'https://openlayers.vip/examples/resources/earth_cloud.png';// 云层颜色
const color = new Cesium.Color(1.0, 1.0, 1.0, 1);// 用于计算云层速度
const time = 20;// 图片材质
const imageMaterial = new Cesium.ImageMaterialProperty({image: image
});

2. 创建矩形实体对象。

// 创建矩形实体
const entity = entities.add({rectangle: {coordinates: Cesium.Rectangle.fromDegrees(-180.0, -90.0, 180.0, 90.0),material: imageMaterial,},
});

2. 创建动态效果。

// 动态云层效果类
function CloudEffectMaterialProperty() {this._definitionChanged = new Cesium.Event();// 速度this.speed = 200;// 颜色this.color = color;// 图层this._image = image;// 时间this.time = time;// 计算持续时间const durationDefault = 100000;this.duration = 100 / this.speed * durationDefault;this._time = new Date().getTime();}
// 定义属性
Object.defineProperties(CloudEffectMaterialProperty.prototype, {isConstant: {get: function() {return false;}},definitionChanged: {get: function() {return this._definitionChanged;}},color: Cesium.createPropertyDescriptor('color')
});CloudEffectMaterialProperty.prototype.getType = function(time) {return 'CloudEffect';
};
CloudEffectMaterialProperty.prototype.getValue = function(time, result) {if (!Cesium.defined(result)) {result = {};}result.color = Cesium.Property.getValueOrClonedDefault(this._color, time, Cesium.Color.WHITE, result.color);result.time = ((new Date().getTime() - this._time) % this.duration) / this.duration;return result;
};
CloudEffectMaterialProperty.prototype.equals = function(other) {return (this === other ||(other instanceof CloudEffectMaterialProperty &&Cesium.Property.equals(this._color, other._color) &&Cesium.Property.equals(this.speed, other.speed)));
}
;Cesium.Material.CloudEffectType = 'CloudEffect';
Cesium.Material.CloudEffectImage = image;
Cesium.Material.CloudEffectColor = color;
// 着色器代码
Cesium.Material.CloudEffectSource = `
czm_material czm_getMaterial(czm_materialInput materialInput){czm_material material = czm_getDefaultMaterial(materialInput);vec2 st = materialInput.st;vec4 colorImage = texture(image, vec2(fract(st.s + time),st.t));material.alpha = colorImage.a * color.a  ;material.diffuse = color.rgb  ;return material;
}
`;// 添加着色器
Cesium.Material._materialCache.addMaterial(Cesium.Material.CloudEffectType, {fabric: {type: Cesium.Material.CloudEffectType,uniforms: {color: Cesium.Material.CloudEffectColor,image: Cesium.Material.CloudEffectImage,constantSpeed: constantSpeed,time: time},source: Cesium.Material.CloudEffectSource},translucent: function(material) {return true;}
});viewer.zoomTo(viewer.entities);Sandcastle.addToolbarButton("开启动态云层", function () {alert('动态云层!');entity.rectangle.height = 0;entity.rectangle.extrudedHeight = 100000;entity.rectangle.material = new CloudEffectMaterialProperty();
});Sandcastle.addToolbarButton("开启静态云层", function () {alert('静态云层!');entity.rectangle.height = undefined;entity.rectangle.extrudedHeight = undefined;entity.rectangle.material = imageMaterial;
});

在线示例

示例中展示了, 三维地图大气云层效果

Cesium 沙盒示例

在这里插入图片描述


http://www.ppmy.cn/news/115930.html

相关文章

联想小新潮7000黑苹果教程_联想小新潮7000重装系统到固态硬盘教程

进入PE后,打开分区工具,对固态硬盘进行快速分区 设置分区数目和分区的大小,正常分2个盘,系统那个C盘容量配个40个G或者以上,另外一个D盘搞个六七十个G用来安装一些CAD这种吃配置的软件。如果固态硬盘只当系统盘&#x…

[转]D7000跑焦的问题,困扰了我很久,终于解决了。

原贴:http://forum.xitek.com/thread-1276998-1-1-1.html D7000 18-105套头入手1个月,总觉得拍的照片经不起放大,特别是拍摄人像的时候看不清皮肤纹理。通过测焦卡检查焦点的确在中央,但拍出来的照片和LV模式下的照片比总是有点模…

小米路由器r1d刷第三方_小米R1D路由器刷Advacned Tomato V3.4-140固件

小米R1D路由器是第一代小米专业路由器,就是带硬盘的那款,这是适合它刷的tomato固件。本固件主要的特色就是在尽量不改动原版固件的基础上,添加软件中心集成,其逻辑思想和merlin固件比较类似,但是对固件源码的改动更小。…

联想拯救者Y7000加装内存条

记:给联想拯救者Y7000加装内存条 前言: 笔者的笔记本内存是8G的,把PC游戏戒了以后,也明显感觉到内存不够用了。运行个IDEA和几个网页,内存的占用率就在百分之七十左右浮动,搞得都不敢再开其他软件 。于是就…

联想拯救者Y7000p安装ubuntu18.04历险记

[TOC]这里仅仅列出了我装ubuntu18.04时遇到的一些坑以及他的解决办法,并非完整的安装教程。适用于拯救者Y7000P-2019款,仅供参考。 以下为我自己采用的办法,如果有更好的,欢迎一起交流 本文参考了 https://blog.csdn.net/qq_411…

联想拯救者y7000p加内存条_内存、硬盘不够用?手把手教你升级联想拯救者Y7000P...

内存、硬盘不够用?手把手教你升级联想拯救者Y7000P 2019年03月02日 05:15作者:孙斌编辑:孙斌文章出处:泡泡网原创 分享 由于这两年内存价格的高企,主流笔记本的内存容量被锁定在8GB已经有了相当长的时间。作为近几个月…

联想拯救者y7000p加内存条_内存、硬盘不够用?手把手教你升级联想拯救者 Y7000P...

由于这两年内存价格的高企,主流笔记本的内存容量被锁定在 8GB 已经有了相当长的时间。作为近几个月最热门的游戏本产品,联想拯救者 Y7000P 除顶配之外同样使用的是 8GB 内存和 512GB 固态硬盘的配置。所以买到这款机器的玩家多数都会选择进行内存和硬盘的升级。今天我们就以手…

ZYNQ7000引脚-个人理解

文章涉及资料: ① xc7z020clg400pkg.xlsx ② ug865-Zynq-7000-Pkg-Pinout.pdf 一、引脚描述 本文以xc7z020clg400为例进行说明。所有400个引脚如下图所示。 User I/O Pins Configuration Pins Power/Ground Pins PS MIO Pins PS DDR Pins Analog to Digital Con…