cesium,实现下雨下雪效果切换
honey们,一起来学习cesium天气效果吧^^
本篇文章实现的是简单的HTML页面在线显示,用其他框架的改一下就可以啦
1、CDN引入
<script src='https://cdn.bootcdn.net/ajax/libs/cesium/1.98.1/Cesium.js'></script>
<link href='https://cdn.bootcdn.net/ajax/libs/cesium/1.98.1/Widgets/widgets.css' rel='stylesheet'>
2、页面显示
设置切换按钮和显示区域
<body><div class="button"><button onclick="rainning()">下雨</button><button onclick="snowwing()">下雪</button><button onclick="clean()">晴天</button></div><div id='container'></div>
</body>
3、容器设置
有需要的控件,可以自行改动为“true”
const viewer = new Cesium.Viewer('container', {// terrainExaggeration: 1.0,fullscreenButton: false,baseLayerPicker: false,selectionIndicator: false,sceneModePicker: false,navigationHelpButton: false,homeButton: false,animation: false, // 时间控制器geocoder: false,timeline: false, //时间线infoBox: false,scene3DOnly: false,vrButton: false,shouldAnimate: true,navigationInstructionsInitiallyVisible: false,imageryProvider: new Cesium.WebMapTileServiceImageryProvider({url: 'http://t{s}.tianditu.gov.cn/img_w/wmts?tk=4befa5ab78e9656c766335d1a007f570',layer: 'img',style: 'default',format: 'tiles',tileMatrixSetID: 'w',subdomains: ['0', '1', '2', '3', '4', '5', '6', '7'],maximumLevel: 18,}),});viewer._cesiumWidget._creditContainer.style.display = 'none';//启用光照viewer.scene.globe.enableLighting = true
4、特效着色器设置
/***********************下雨*************************/
const FS_Rain="uniform sampler2D colorTexture;\n\varying vec2 v_textureCoordinates;\n\\n\float hash(float x){\n\return fract(sin(x*133.3)*13.13);\n\}\n\\n\void main(void){\n\\n\float time = czm_frameNumber / 60.0;\n\vec2 resolution = czm_viewport.zw;\n\\n\vec2 uv=(gl_FragCoord.xy*2.-resolution.xy)/min(resolution.x,resolution.y);\n\vec3 c=vec3(.6,.7,.8);\n\\n\float a=-.4;\n\float si=sin(a),co=cos(a);\n\uv*=mat2(co,-si,si,co);\n\uv*=length(uv+vec2(0,4.9))*.3+1.;\n\\n\float v=1.-sin(hash(floor(uv.x*100.))*2.);\n\float b=clamp(abs(sin(20.*time*v+uv.y*(5./(2.+v))))-.95,0.,1.)*20.;\n\c*=v*b; \n\\n\gl_FragColor = mix(texture2D(colorTexture, v_textureCoordinates), vec4(c,1), 0.5); \n\}\n\
";
/*************************下雪*********************/const FS_Snow="uniform sampler2D colorTexture;\n\varying vec2 v_textureCoordinates;\n\\n\float snow(vec2 uv,float scale)\n\{\n\float time = czm_frameNumber / 60.0;\n\float w=smoothstep(1.,0.,-uv.y*(scale/10.));if(w<.1)return 0.;\n\uv+=time/scale;uv.y+=time*2./scale;uv.x+=sin(uv.y+time*.5)/scale;\n\uv*=scale;vec2 s=floor(uv),f=fract(uv),p;float k=3.,d;\n\p=.5+.35*sin(11.*fract(sin((s+p+scale)*mat2(7,3,6,5))*5.))-f;d=length(p);k=min(d,k);\n\k=smoothstep(0.,k,sin(f.x+f.y)*0.01);\n\return k*w;\n\}\n\
\n\void main(void){\n\vec2 resolution = czm_viewport.zw;\n\vec2 uv=(gl_FragCoord.xy*2.-resolution.xy)/min(resolution.x,resolution.y);\n\vec3 finalColor=vec3(0);\n\float c = 0.0;\n\c+=snow(uv,30.)*.0;\n\c+=snow(uv,20.)*.0;\n\c+=snow(uv,15.)*.0;\n\c+=snow(uv,10.);\n\c+=snow(uv,8.);\n\c+=snow(uv,6.);\n\c+=snow(uv,5.);\n\finalColor=(vec3(c)); \n\gl_FragColor = mix(texture2D(colorTexture, v_textureCoordinates), vec4(finalColor,1), 0.5); \n\
\n\}\n\
";
5、控制按钮
// 清除特效let lastStage=null;let removeStage = () => {lastStage && viewer.scene.postProcessStages.remove(lastStage), lastStage = null}//下雨function rainning(){removeStage()var rain = new Cesium.PostProcessStage({name: 'hi_rain',fragmentShader: FS_Rain});viewer.scene.postProcessStages.add(rain);lastStage=rain}//下雪function snowwing(){removeStage()var snow = new Cesium.PostProcessStage({name: 'hi_snow',fragmentShader: FS_Snow});viewer.scene.postProcessStages.add(snow);lastStage=snow}//晴天function clean(){removeStage()lastStage=null}
完整代码
<!DOCTYPE html>
<html lang='en'><head><meta charset='UTF-8'><meta http-equiv='X-UA-Compatible' content='IE=edge'><meta name='viewport' content='width=device-width, initial-scale=1.0'><script src='https://cdn.bootcdn.net/ajax/libs/cesium/1.98.1/Cesium.js'></script><link href='https://cdn.bootcdn.net/ajax/libs/cesium/1.98.1/Widgets/widgets.css' rel='stylesheet'><title>Document</title><style>html,body,#container {margin: 0;padding: 0;height: 100%;}.button{background:rgb(5,5,5);}</style>
</head><body><div class="button"><button onclick="rainning()">下雨</button><button onclick="snowwing()">下雪</button><button onclick="clean()">晴天</button></div><div id='container'></div>
</body>
<script>const viewer = new Cesium.Viewer('container', {// terrainExaggeration: 1.0,fullscreenButton: false,baseLayerPicker: false,selectionIndicator: false,sceneModePicker: false,navigationHelpButton: false,homeButton: false,animation: false, // 时间控制器geocoder: false,timeline: false, //时间线infoBox: false,scene3DOnly: false,vrButton: false,shouldAnimate: true,navigationInstructionsInitiallyVisible: false,imageryProvider: new Cesium.WebMapTileServiceImageryProvider({url: 'http://t{s}.tianditu.gov.cn/img_w/wmts?tk=4befa5ab78e9656c766335d1a007f570',layer: 'img',style: 'default',format: 'tiles',tileMatrixSetID: 'w',subdomains: ['0', '1', '2', '3', '4', '5', '6', '7'],maximumLevel: 18,}),});viewer._cesiumWidget._creditContainer.style.display = 'none';//启用光照viewer.scene.globe.enableLighting = true/***********************下雨*************************/
const FS_Rain="uniform sampler2D colorTexture;\n\varying vec2 v_textureCoordinates;\n\\n\float hash(float x){\n\return fract(sin(x*133.3)*13.13);\n\}\n\\n\void main(void){\n\\n\float time = czm_frameNumber / 60.0;\n\vec2 resolution = czm_viewport.zw;\n\\n\vec2 uv=(gl_FragCoord.xy*2.-resolution.xy)/min(resolution.x,resolution.y);\n\vec3 c=vec3(.6,.7,.8);\n\\n\float a=-.4;\n\float si=sin(a),co=cos(a);\n\uv*=mat2(co,-si,si,co);\n\uv*=length(uv+vec2(0,4.9))*.3+1.;\n\\n\float v=1.-sin(hash(floor(uv.x*100.))*2.);\n\float b=clamp(abs(sin(20.*time*v+uv.y*(5./(2.+v))))-.95,0.,1.)*20.;\n\c*=v*b; \n\\n\gl_FragColor = mix(texture2D(colorTexture, v_textureCoordinates), vec4(c,1), 0.5); \n\}\n\
";
/*************************下雪*********************/const FS_Snow="uniform sampler2D colorTexture;\n\varying vec2 v_textureCoordinates;\n\\n\float snow(vec2 uv,float scale)\n\{\n\float time = czm_frameNumber / 60.0;\n\float w=smoothstep(1.,0.,-uv.y*(scale/10.));if(w<.1)return 0.;\n\uv+=time/scale;uv.y+=time*2./scale;uv.x+=sin(uv.y+time*.5)/scale;\n\uv*=scale;vec2 s=floor(uv),f=fract(uv),p;float k=3.,d;\n\p=.5+.35*sin(11.*fract(sin((s+p+scale)*mat2(7,3,6,5))*5.))-f;d=length(p);k=min(d,k);\n\k=smoothstep(0.,k,sin(f.x+f.y)*0.01);\n\return k*w;\n\}\n\
\n\void main(void){\n\vec2 resolution = czm_viewport.zw;\n\vec2 uv=(gl_FragCoord.xy*2.-resolution.xy)/min(resolution.x,resolution.y);\n\vec3 finalColor=vec3(0);\n\float c = 0.0;\n\c+=snow(uv,30.)*.0;\n\c+=snow(uv,20.)*.0;\n\c+=snow(uv,15.)*.0;\n\c+=snow(uv,10.);\n\c+=snow(uv,8.);\n\c+=snow(uv,6.);\n\c+=snow(uv,5.);\n\finalColor=(vec3(c)); \n\gl_FragColor = mix(texture2D(colorTexture, v_textureCoordinates), vec4(finalColor,1), 0.5); \n\
\n\}\n\
";// 清除特效let lastStage=null;let removeStage = () => {lastStage && viewer.scene.postProcessStages.remove(lastStage), lastStage = null}//下雨function rainning(){removeStage()var rain = new Cesium.PostProcessStage({name: 'hi_rain',fragmentShader: FS_Rain});viewer.scene.postProcessStages.add(rain);lastStage=rain}//下雪function snowwing(){removeStage()var snow = new Cesium.PostProcessStage({name: 'hi_snow',fragmentShader: FS_Snow});viewer.scene.postProcessStages.add(snow);lastStage=snow}//晴天function clean(){removeStage()lastStage=null}</script></html>
实现效果
着色器设置引用https://blog.csdn.net/qq_40863573/article/details/125327716?spm=1001.2014.3001.5506
cesium,实现下雨下雪效果切换相关推荐
- canvas 让你呼风唤雨,下雨下雪效果
前端如何呼风唤雨 Canvas 等于什么? = html5 =js = 取代flash = 你可以即见即所得向别人装逼!没错 进入 canvas 的权力游戏吧! 起初我创造了canvas . 我 ...
- cesium实现下雨下雪特效
页面效果 使用着色器实例特效 下雨特效着色器 // 下雨特效 rain () {removeStage();var e = new Cesium.PostProcessStage({name: &qu ...
- 20多行js实现canvas雪夜下雪效果
目录 前言 实现canvas雪夜下雪效果 1.设置canvas标签的id为cvs,设置背景颜色为黑色 2.设置body外边距为0 3.通过获取DOM元素获得画板 4.指定二维绘图 5.设置宽高填满页面 ...
- js画布实现下雪效果
之前使用css实现了下雪效果,也对比了前端多种实现动画的方法(链接见文末) 下面使用js和canvas的方法配合requestAnimationFrame实现一个简单的下雪效果,代码十分精简. 代码 ...
- canvas下雪效果(原生js)
效果展示: 源码展示: <!doctype html> <html> <head><meta charset="utf-8">< ...
- Cesium:实现卷帘效果
Cesium:实现卷帘效果 卷帘效果简介 Cesium:卷帘效果实现 Cesium:实现思路 示例代码 卷帘效果简介 先上一个简动画预演一下. 但是如何介绍"卷帘效果"呢? ...
- 默认墨迹天气 下雪效果
WeatherAnimationDemo 项目地址:KitTak/WeatherAnimationDemo 简介:默认墨迹天气 下雪效果 模仿墨迹下雪效果 在布局中直接通过自定义 SurfaceVie ...
- Cesium实现雷达扫描效果
Cesium实现雷达扫描效果 效果: html: <div id="cesiumContainer"></div> <canvas id=" ...
- css3下雪的效果,首页下雪效果
首页下雪效果 Posted on 2019-09-17 14:18 大艾木 阅读(...) 评论(...) 编辑 收藏 下雪效果,转自网络,快去试试吧 :) 原理 通过多张雪花图片,不同速度的移动,达 ...
最新文章
- 【PHP高效搜索专题(1)】sphinxCoreseek的介绍与安装
- 以太坊联合创始人发明了新语言叫板Vyper,主链之后语言将引领新一轮大战?...
- 迪桑特案例拆解:社群运营如何在高端运动服装品牌中发挥价值?
- 由于Item category group customizing 缺失导致的BDOC error
- python替换所有标点符号 正则_python 把标点符号替换为空
- 大咖开讲:一小时学会.NET MVC开发的那些事儿
- 1.原生js封装的获取某一天是当年的第几周方法
- android studio gradle 学习,学习Android Studio里的Gradle
- c ++ stl_通过分配另一个列表的所有元素来创建列表| C ++ STL
- Jenkins-Pipeline 流水线发布
- iOS 用Swipe手势和动画实现循环播放图片
- CHIP-2020 中文医学文本实体关系抽取
- Android Studio开发中的各种开源API
- 建设容器云平台之前不能忽视3个评估,你的企业能得多少分? | 某银行最佳实践分享
- 8月房价上涨城市数量增多 涨幅全线扩大
- 解密新后缀.phobos勒索病毒 防御措施 解密方法how_to_back
- hp台式计算机怎么进bois,hp台式机怎么进bios图解
- iib 9.0.0.2 安装
- BAT bat .bat 脚本,windows下的bat命令
- 使用爱码哥之后的心得
热门文章
- Unity 之 安卓堆栈跟踪和日志工具 (Android Logcat | 符号表解析Bugly捕获)
- 集成easypoi实现excel图片导出
- win8系统设置Ie浏览器Cookie隐私级别的解决教程--win10专业版
- 安装特定版本的keras_Keras安装和配置指南(Windows)
- 运行python脚本后台执行
- python和c语言相通吗_C语言和Python有什么区别呢?
- 印花连衣裙时尚女孩之选
- phpmyadmin实验八
- PHP 的 switch 语法
- java keyset ==_Java中keySet()返回值的排序问题