Particle Fire
Runnable Cesium example: Particle Fire. 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
<!--
示例:用粒子系统堆出火焰
要点:SphereEmitter 向上喷射,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,.92);
border:1px solid #2a313b;border-radius:6px;padding:9px 12px;font-size:12px;
color:#a2a9b6;width:250px}
.hud b{color:#e6e8ec;font-weight:600}
.hud .row{margin-top:7px}
input[type=range]{width:100%}
.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">发射率 <span id="eVal">240</span> 粒/秒</div>
<div class="row"><input id="rate" type="range" min="40" max="800" step="20" value="240"></div>
<div class="row">三处火点共用一套贴图</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 flameTexture() {
var c = document.createElement('canvas');
c.width = c.height = 64;
var ctx = c.getContext('2d');
var g = ctx.createRadialGradient(32, 40, 2, 32, 32, 32);
g.addColorStop(0, 'rgba(255,255,235,1)');
g.addColorStop(0.35, 'rgba(255,196,88,0.95)');
g.addColorStop(0.7, 'rgba(255,110,40,0.45)');
g.addColorStop(1, 'rgba(255,60,20,0)');
ctx.fillStyle = g;
ctx.beginPath();
ctx.arc(32, 32, 32, 0, Math.PI * 2);
ctx.fill();
return c;
}
var TEX = flameTexture();
/* 粒子在局部坐标系里,Z 轴朝上,updateCallback 直接改 velocity.z */
function buoyancy(particle, dt) {
particle.velocity.z += 9 * dt;
particle.velocity.x *= 0.97;
particle.velocity.y *= 0.97;
}
var systems = [];
var SPOTS = [
[116.383, 39.903], [116.391, 39.903], [116.399, 39.903]
];
SPOTS.forEach(function (p) {
var matrix = Cesium.Transforms.eastNorthUpToFixedFrame(
Cesium.Cartesian3.fromDegrees(p[0], p[1], 20)
);
matrix = Cesium.Matrix4.multiply(
matrix, Cesium.Matrix4.fromUniformScale(6), matrix
);
var system = new Cesium.ParticleSystem({
image: TEX,
startColor: Cesium.Color.fromCssColorString('#ffd166').withAlpha(0.95),
endColor: Cesium.Color.fromCssColorString('#ff3d1f').withAlpha(0.0),
startScale: 1.1,
endScale: 0.35,
minimumParticleLife: 0.7,
maximumParticleLife: 1.4,
minimumSpeed: 1.2,
maximumSpeed: 3.4,
imageSize: new Cesium.Cartesian2(46, 46),
emissionRate: 240,
emitter: new Cesium.SphereEmitter(1.4),
modelMatrix: matrix,
lifetime: 32,
updateCallback: buoyancy
});
systems.push(system);
scene.primitives.add(system);
});
/* 承火平台 */
viewer.entities.add({
position: Cesium.Cartesian3.fromDegrees(116.391, 39.903, 4),
box: {
dimensions: new Cesium.Cartesian3(2400, 400, 8),
material: Cesium.Color.fromCssColorString('#243140')
}
});
document.getElementById('rate').addEventListener('input', function () {
var v = parseInt(this.value, 10);
document.getElementById('eVal').textContent = v;
systems.forEach(function (s) { s.emissionRate = v; });
});
viewer.camera.flyTo({
destination: Cesium.Cartesian3.fromDegrees(116.378, 39.893, 700),
orientation: {
heading: Cesium.Math.toRadians(12),
pitch: Cesium.Math.toRadians(-10),
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.