Particle Weather
Runnable Cesium example: Particle Weather. Open the live demo, read the complete source, and copy the file into your project. No build step or API key.
single HTML file · CDN dependencies onlyinterface may contain Chinese labels
Source
<!--
示例:粒子系统模拟雨雪
要点:ParticleSystem 配 BoxEmitter 挂在相机附近,updateCallback 里按地心方向施加重力
依赖:CesiumJS 1.121 · jsDelivr CDN,粒子贴图由 canvas 生成
-->
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta name="robots" content="noindex,follow">
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>粒子雨雪</title>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/cesium@1.121.0/Build/Cesium/Widgets/widgets.css">
<style>
html,body{height:100%;margin:0}
body{background:#0e1116;color:#e6e8ec;overflow:hidden;
font:13px/1.6 -apple-system,"Segoe UI","PingFang SC",sans-serif}
#c{height:100%}
.hud{position:absolute;left:12px;top:12px;z-index:5;background:rgba(23,28,35,.9);
border:1px solid #2a313b;border-radius:6px;padding:9px 12px;font-size:12px;
color:#a2a9b6;max-width:250px}
.hud b{color:#e6e8ec;font-weight:600}
.hud .row{margin-top:7px}
.hud button{font:inherit;font-size:12px;padding:3px 9px;margin-right:4px;border-radius:4px;
cursor:pointer;border:1px solid #343b46;background:#1d232b;color:#cfd6e2}
.hud button.on{background:#1d3a5c;border-color:#3d6f9e;color:#cfe4ff}
.cesium-viewer-bottom .cesium-widget-credits,.cesium-viewer-animationContainer,
.cesium-viewer-timelineContainer{display:none!important}
</style>
</head>
<body>
<div id="c"></div>
<div class="hud">
<div><b>粒子天气</b></div>
<div class="row">
<button id="rain" class="on">雨</button>
<button id="snow">雪</button>
<button id="off">关闭</button>
</div>
<div class="row" id="info">发射区跟随相机移动</div>
</div>
<script src="https://cdn.jsdelivr.net/npm/cesium@1.121.0/Build/Cesium/Cesium.js"></script>
<script>
(function () {
'use strict';
var viewer = new Cesium.Viewer('c', {
baseLayer: false,
baseLayerPicker: false, geocoder: false, homeButton: false,
sceneModePicker: false, navigationHelpButton: false,
animation: false, timeline: false, fullscreenButton: false,
infoBox: false, selectionIndicator: false
});
var scene = viewer.scene;
scene.globe.baseColor = Cesium.Color.fromCssColorString('#1c2733');
scene.backgroundColor = Cesium.Color.fromCssColorString('#0e1116');
scene.skyBox.show = false;
scene.sun.show = false;
scene.moon.show = false;
if (scene.skyAtmosphere) scene.skyAtmosphere.show = false;
/* ---------- 两张程序生成的粒子贴图 ---------- */
function rainTexture() {
var c = document.createElement('canvas');
c.width = 4; c.height = 24;
var ctx = c.getContext('2d');
var g = ctx.createLinearGradient(0, 0, 0, 24);
g.addColorStop(0, 'rgba(255,255,255,0)');
g.addColorStop(0.6, 'rgba(255,255,255,0.9)');
g.addColorStop(1, 'rgba(255,255,255,0.1)');
ctx.fillStyle = g;
ctx.fillRect(1, 0, 2, 24);
return c;
}
function snowTexture() {
var c = document.createElement('canvas');
c.width = c.height = 16;
var ctx = c.getContext('2d');
var g = ctx.createRadialGradient(8, 8, 0, 8, 8, 8);
g.addColorStop(0, 'rgba(255,255,255,1)');
g.addColorStop(0.5, 'rgba(255,255,255,0.7)');
g.addColorStop(1, 'rgba(255,255,255,0)');
ctx.fillStyle = g;
ctx.fillRect(0, 0, 16, 16);
return c;
}
var RAIN_TEX = rainTexture();
var SNOW_TEX = snowTexture();
var gravity = new Cesium.Cartesian3();
function applyGravity(particle, dt) {
Cesium.Cartesian3.normalize(particle.position, gravity);
Cesium.Cartesian3.multiplyByScalar(gravity, -9.8 * dt, gravity);
particle.velocity = Cesium.Cartesian3.add(particle.velocity, gravity, particle.velocity);
}
var emitterBox = new Cesium.Cartesian3(700, 700, 260);
var rain = new Cesium.ParticleSystem({
image: RAIN_TEX,
startColor: Cesium.Color.fromCssColorString('#8fb7ff').withAlpha(0.55),
endColor: Cesium.Color.fromCssColorString('#8fb7ff').withAlpha(0.05),
minimumParticleLife: 0.9,
maximumParticleLife: 1.4,
minimumSpeed: 26,
maximumSpeed: 38,
imageSize: new Cesium.Cartesian2(2.5, 20),
emissionRate: 900,
emitter: new Cesium.BoxEmitter(emitterBox),
modelMatrix: Cesium.Matrix4.IDENTITY,
lifetime: 60,
updateCallback: applyGravity
});
var snow = new Cesium.ParticleSystem({
image: SNOW_TEX,
startColor: Cesium.Color.WHITE.withAlpha(0.9),
endColor: Cesium.Color.WHITE.withAlpha(0.1),
minimumParticleLife: 4,
maximumParticleLife: 7,
minimumSpeed: 1.5,
maximumSpeed: 4,
imageSize: new Cesium.Cartesian2(8, 8),
emissionRate: 260,
emitter: new Cesium.BoxEmitter(emitterBox),
modelMatrix: Cesium.Matrix4.IDENTITY,
lifetime: 60,
updateCallback: applyGravity
});
snow.show = false;
scene.primitives.add(rain);
scene.primitives.add(snow);
/* 每帧把发射区搬到相机前方,粒子始终填满镜头 */
var forward = new Cesium.Cartesian3();
var center = new Cesium.Cartesian3();
var matrix = new Cesium.Matrix4();
scene.preUpdate.addEventListener(function () {
var cam = viewer.camera;
Cesium.Cartesian3.multiplyByScalar(cam.direction, 220, forward);
Cesium.Cartesian3.add(cam.positionWC, forward, center);
matrix = Cesium.Transforms.eastNorthUpToFixedFrame(center);
rain.modelMatrix = matrix;
snow.modelMatrix = matrix;
});
var btnRain = document.getElementById('rain');
var btnSnow = document.getElementById('snow');
var btnOff = document.getElementById('off');
var info = document.getElementById('info');
function setMode(mode) {
rain.show = mode === 'rain';
snow.show = mode === 'snow';
btnRain.className = mode === 'rain' ? 'on' : '';
btnSnow.className = mode === 'snow' ? 'on' : '';
btnOff.className = mode === 'off' ? 'on' : '';
info.textContent = mode === 'rain' ? '900 粒/秒 · 竖直下落'
: mode === 'snow' ? '260 粒/秒 · 缓慢飘落'
: '粒子已停止发射';
}
btnRain.addEventListener('click', function () { setMode('rain'); });
btnSnow.addEventListener('click', function () { setMode('snow'); });
btnOff.addEventListener('click', function () { setMode('off'); });
viewer.camera.flyTo({
destination: Cesium.Cartesian3.fromDegrees(116.391263, 39.907359, 1500),
orientation: {
heading: Cesium.Math.toRadians(30),
pitch: Cesium.Math.toRadians(-18),
roll: 0
},
duration: 0
});
})();
</script>
</body>
</html>
Language: the code and this page are in English; the full tutorial and API notes for this example are in Chinese. The demo itself is the authoritative version — it runs exactly as shown.