Three.js 真实火焰教程
真实火焰 ·Real Fire· ▶ 在线运行案例案例合集三维可视化功能案例threehub.cn开源仓库github地址https://github.com/z2586300277/three-cesium-examples400个案例代码:网盘链接你将学到什么ShaderMaterial 自定义着色器实现核心视觉效果OrbitControls 相机轨道交互THREE.Points 粒子点渲染BufferGeometry 自定义顶点/索引数据requestAnimationFrame渲染循环与resize自适应效果说明本案例演示真实火焰效果基于 WebGL 实现「真实火焰」可视化效果附完整可运行源码核心用到 ShaderMaterial、OrbitControls、THREE.Points。建议先打开文首在线案例查看动态画面再对照下方源码逐步理解。核心概念Scene / Camera / WebGLRenderer构成最小渲染闭环大场景可开logarithmicDepthBuffer缓解 Z-fighting。ShaderMaterial通过uniforms 自定义 GLSL 控制逐像素/逐点效果透明粒子常配合depthTest: false。OrbitControls提供轨道旋转/缩放开启enableDamping后需在 animate 中controls.update()。THREE.Points将每个顶点渲染为可控大小的粒子可用自定义 attribute如u_index驱动片元/顶点动画。实现步骤搭建 Scene、PerspectiveCamera、WebGLRenderer挂载 canvas 并处理resize定义 uniforms / onBeforeCompile 或 ShaderMaterial编写 GLSL 与材质参数创建 OrbitControls及 Raycaster 等交互控件若源码包含在requestAnimationFrame循环中更新状态并 renderCesium 为viewer.render或自动渲染代码要点import * as THREE from threeimport { OrbitControls } from three/examples/jsm/controls/OrbitControls.js// 初始化场景 const box document.getElementById(box) const scene new THREE.Scene() const camera new THREE.PerspectiveCamera(50, box.clientWidth / box.clientHeight, 0.1, 1000) camera.position.set(0, 10, 6)// 设置渲染器 const renderer new THREE.WebGLRenderer({ antialias: true, alpha: true, logarithmicDepthBuffer: true }) renderer.setSize(box.clientWidth, box.clientHeight) box.appendChild(renderer.domElement)// 轨道控制器 const controls new OrbitControls(camera, renderer.domElement) controls.enableDamping true/**创建真实火焰效果returns {Object} 包含火焰组和火焰材质的对象*/ function createRealisticFire() { // 火焰配置参数 const fireConfig { particleCount: 1500, // 粒子数量 particleSize: 0.5, // 粒子基础大小 baseHeight: 5, // 火焰高度 baseRadius: 1.2, // 火焰底部半径 colors: { inner: new THREE.Color(0xffff80), // 内焰颜色 - 亮黄色 mid: new THREE.Color(0xff8000), // 中焰颜色 - 橙色 outer: new THREE.Color(0xff4400), // 外焰颜色 - 红色 smoke: new THREE.Color(0x111111) // 烟雾颜色 - 深灰色 }, velocityFactor: 0.6, // 上升速度系数 wiggleFactor: 0.2 // 横向摇摆系数 };// 火焰着色器材质 const fireMaterial new THREE.ShaderMaterial({ uniforms: { time: { value: 0 }, baseColor: { value: fireConfig.colors.inner }, midColor: { value: fireConfig.colors.mid }, tipColor: { value: fireConfig.colors.outer }, smokeColor: { value: fireConfig.colors.smoke } }, vertexShader:attribute float size; attribute float life; attribute float phase; attribute vec3 velocity; uniform float time; varying float vLife; varying float vPhase; void main() { vLife life; vPhase phase; // 计算粒子当前生命周期 float age mod(time phase, 1.0); // 位置随时间变化 vec3 pos position velocity * age; // 添加水平摆动效果随生命周期衰减 float wiggle sin(age20.0 phase10.0)(1.0 - age)0.2; pos.x wiggle; vec4 mvPosition modelViewMatrix * vec4(pos, 1.0); gl_Position projectionMatrix * mvPosition; // 粒子大小随高度和生命周期变化 gl_PointSize size(1.0 - age)(300.0 / -mvPosition.z); }, fragmentShader:uniform vec3 baseColor; uniform vec3 midColor; uniform vec3 tipColor; uniform vec3 smokeColor; varying float vLife; varying float vPhase; void main() { // 计算到粒子中心的距离用于圆形粒子效果 vec2 center gl_PointCoord - 0.5; float dist length(center) * 2.0; // 丢弃边缘像素创建圆形粒子 if (dist 1.0) discard; // 基于生命周期混合颜色 vec3 color; float age vLife; // 颜色过渡: 亮黄 - 橙色 - 红色 - 烟雾色 if (age 0.3) { color mix(baseColor, midColor, age / 0.3); } else if (age 0.8) { color mix(midColor, tipColor, (age - 0.3) / 0.5); } else { color mix(tipColor, smokeColor, (age - 0.8) / 0.2); } // 边缘透明度渐变提高真实感 float alpha (1.0 - dist) * (1.0 - age); gl_FragColor vec4(color, alpha); }, blending: THREE.AdditiveBlending, // 加法混合增强光照效果 depthWrite: false, // 禁用深度写入 transparent: true, // 启用透明 });// 创建粒子几何体 const fireGeometry new THREE.BufferGeometry(); const positions []; const sizes []; const lives []; const phases []; const velocities []; // 生成火焰粒子 for (let i 0; i fireConfig.particleCount; i) { // 在圆形底部随机分布 const radius Math.random() * fireConfig.baseRadius; const theta Math.random()Math.PI2; const x radius * Math.cos(theta); const z radius * Math.sin(theta); const y Math.random() * 0.5; // 略微抬高起始位置 positions.push(x, y, z); // 随机粒子大小 sizes.push(fireConfig.particleSize(0.5 Math.random()0.5)); // 随机生命周期和相位创造自然效果 lives.push(Math.random()); phases.push(Math.random()); // 速度向量 - 主要向上带随机偏移 const speed fireConfig.velocityFactor(0.8 Math.random()0.4); const vx (Math.random() - 0.5) * fireConfig.wiggleFactor; const vy speed * fireConfig.baseHeight; // 主要向上运动 const vz (Math.random() - 0.5) * fireConfig.wiggleFactor; velocities.push(vx, vy, vz); } // 为几何体设置属性 fireGeometry.setAttribute(position, new THREE.Float32BufferAttribute(positions, 3)); fireGeometry.setAttribute(size, new THREE.Float32BufferAttribute(sizes, 1)); fireGeometry.setAttribute(life, new THREE.Float32BufferAttribute(lives, 1)); fireGeometry.setAttribute(phase, new THREE.Float32BufferAttribute(phases, 1)); fireGeometry.setAttribute(velocity, new THREE.Float32BufferAttribute(velocities, 3)); // 创建粒子系统 const fireParticles new THREE.Points(fireGeometry, fireMaterial); // 创建火焰底部的光源 const fireLight new THREE.PointLight(0xff5500, 1, 10); fireLight.position.set(0, 2, 0); // 光源位置稍高于火焰基部 // 创建火焰组包含粒子和光源 const fireGroup new THREE.Group(); fireGroup.add(fireParticles); fireGroup.add(fireLight); return { fireGroup, fireMaterial }; }// 创建火焰并添加到场景 const { fireGroup, fireMaterial } createRealisticFire(); scene.add(fireGroup);// 添加环境光提供基础照明 const ambientLight new THREE.AmbientLight(0x333333); scene.add(ambientLight);// 动画相关 const clock new THREE.Clock();/**动画循环*/ function animate() { requestAnimationFrame(animate); const elapsedTime clock.getElapsedTime(); // 更新火焰的时间参数 fireMaterial.uniforms.time.value elapsedTime; // 使火焰光源强度随时间微微变化模拟火焰闪烁 const fireLight fireGroup.children[1]; fireLight.intensity 1 Math.sin(elapsedTime5)0.2; controls.update(); renderer.render(scene, camera); }// 启动动画 animate();// 窗口大小变化处理 window.addEventListener(resize, () { camera.aspect box.clientWidth / box.clientHeight; camera.updateProjectionMatrix(); renderer.setSize(box.clientWidth, box.clientHeight); });完整源码GitHub小结本文提供真实火焰完整 Three.js 源码与在线 Demo建议先运行案例再改 uniform/参数做二次实验更多 Three.js 实战案例见 three-cesium-examples 合集 与 GitHub 开源仓库

相关新闻

从0到月入10万:用AI自动化搭建私域电商闭环(含5套可复用SOP模板)

从0到月入10万:用AI自动化搭建私域电商闭环(含5套可复用SOP模板)

更多请点击: https://codechina.net 第一章:AI做电商 赚钱方法 人工智能正深度重构电商价值链,从流量获取、商品推荐到客服响应、库存优化,AI已不再是辅助工具,而是直接驱动盈利的核心引擎。商家无需自建大模型&#…

2026/11/10 19:51:04 阅读更多 →
AI论文写作工具评测与宏智树核心优势解析

AI论文写作工具评测与宏智树核心优势解析

1. AI论文写作工具现状与核心痛点当前学术写作领域正经历着前所未有的技术变革,AI辅助写作工具已经从简单的语法检查进化到能够生成完整学术论文的阶段。我测试过市面上主流的9款工具后发现,它们在实际应用中存在三个典型问题:第一是学术规范…

2026/11/10 10:05:55 阅读更多 →
AI技术变革:伦理挑战与风险控制实践

AI技术变革:伦理挑战与风险控制实践

1. 技术变革的双刃剑本质当AlphaGo首次击败人类围棋冠军时,我在现场见证了观众席上凝固的震惊表情。那一刻我突然意识到,我们正站在文明史的奇点上——AI技术已经突破了从工具到伙伴的临界值。但就像核能既能为城市供电也能摧毁文明,AI的指数…

2026/11/2 5:18:43 阅读更多 →

最新新闻

Electron桌面应用开发实战:从环境搭建到打包发布全流程

Electron桌面应用开发实战:从环境搭建到打包发布全流程

做前端这些年,我大部分时间都在跟浏览器标签页打交道,直到第一次把桌面应用跑起来,才发现原来自己一直被限制在地址栏和刷新按钮围起来的框框里。Electron这个词,最开始是在某段时间频繁出现在同事的讨论里,一个用HTML…

2026/11/13 5:13:25 阅读更多 →
滑盖智能锁T109深度解析:结构原理、安装调试与故障排查

滑盖智能锁T109深度解析:结构原理、安装调试与故障排查

简介:亚太天能T109滑盖智能锁产品说明书是一份PDF格式的官方技术文档,主要面向智能锁安装人员、终端用户及售后维护者,帮助快速掌握产品安装、配置与常见故障处理。全包仅1个PDF文件,大小4.37MB,内容结构清晰&#xff…

2026/11/13 5:13:25 阅读更多 →
PMSM弱磁控制原理与Simulink建模:高速AGV驱动系统设计指南

PMSM弱磁控制原理与Simulink建模:高速AGV驱动系统设计指南

上个月我在调试一台高速AGV的驱动系统,整车要求空载跑到8m/s,电机转速已经拉到额定点,驱动器的输出电压也顶到了母线电压极限附近。再往上踩速度指令,转速纹丝不动,三相电流反而开始震荡。那会儿我的第一反应就是&…

2026/11/13 5:13:25 阅读更多 →
C++核心知识点:从内存管理到STL的实战指南

C++核心知识点:从内存管理到STL的实战指南

C 这门语言我前前后后折腾了十几年,用它写过嵌入式驱动、做过游戏服务端,也搭过基础组件库。每次看到新人一头扎进"C 核心知识点"这个搜索词,我其实挺感慨的——这门语言的难点从来不是语法多,而是知识点之间的耦合太深…

2026/11/12 18:22:26 阅读更多 →
T109滑盖智能锁怎么装?安装调试与故障排查全攻略

T109滑盖智能锁怎么装?安装调试与故障排查全攻略

简介:亚太天能T109滑盖智能锁产品说明书以PDF格式发布,适合智能锁用户、安装人员与售后维修人员阅读。内容围绕T109系列指纹密码刷卡锁展开,先介绍前面板、后面板、锁体、机械钥匙、电池盒盖等部件名称与功能,再给出完整安装步骤&…

2026/11/12 18:22:25 阅读更多 →
LLM知识降熵:从信息垄断到理解权下放

LLM知识降熵:从信息垄断到理解权下放

你有没有过这种经历:翻开源码注释、论文摘要或者一份专业报告,每个字都认识,连在一起却像天书。想找人聊聊,身边要么没人懂,要么懂的懒得跟你解释。后来我试着把同一段内容丢给一个大语言模型,几秒钟之后&a…

2026/11/13 5:13:25 阅读更多 →

日新闻

周新闻

月新闻