Three.js 真实火焰教程

发布时间:2026/9/14 4:43:50

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 开源仓库
延伸阅读

更多相关文章

2026/9/10 10:25:29

2026年AI大模型学习指南:从入门到精通

1. 项目概述:为什么需要这份2026年AI大模型学习指南? 过去三年,我亲眼见证了AI大模型技术从实验室走向产业落地的全过程。从GPT-3到今天的多模态大模型,技术迭代速度远超大多数人想象。但令人担忧的是,市面上90%的&quo…

2026/9/13 3:08:55

各个APP批量去除关注情况

1 小红书能正常运行2 vivo能正常运行3 抖音--------用不着,自带4 快手--------用不着,自带5 今日头条----------正在测试

2026/9/14 4:43:38

地铁ACC客流预测系统:Django+LSTM+XGBoost全栈实现

简介:本资源是一套基于Python开发的地铁客流预测系统完整实现,面向交通大数据分析初学者、城市轨道交通领域开发者及高校相关专业师生,解决ACC清分系统下线路级与站点级客流建模、预测与可视化预警的实际问题。压缩包共26个文件,含…

2026/9/14 4:43:38

SpringBoot开发环境搭建与配置指南

1. SpringBoot与JAK环境搭建概述 在Java生态中,SpringBoot已经成为现代应用开发的事实标准框架。而JAK(Java Development Kit)作为Java开发的基石环境,其正确安装与配置是每个Java开发者必须掌握的基础技能。本文将基于Windows平…

2026/9/14 4:38:38

纯前端复刻QQ音乐界面:Web课程设计实战指南

简介:面向前端初学者的QQ音乐界面模仿型Web课程设计资源,适合完成HTMLCSS课程作业、学习页面布局与交互特效的学生参考。压缩包共102个文件,主要包含HTML页面、CSS样式、JavaScript脚本、大量截图与背景音乐,包体约16.16MB&#x…

2026/9/14 2:17:50

拯救者Y7000黑屏故障排查与维修实战指南

1. 项目概述:一台黑屏的拯救者Y7000,到底卡在哪一步? 联想拯救者Y7000系列笔记本,从2018年第一代搭载i5-8300H开始,到后来的i7-9750H、i7-10750H、i5-11400H,再到2023年款的R7-7840HS,它始终是学…

2026/9/14 0:03:22

KCF目标跟踪算法与OTB工程实现:毕业设计实战解析

简介:这是一份基于KCF核相关滤波算法、融合尺度池与抗遮挡处理的目标检测跟踪MATLAB完整源码,主要面向计算机相关专业准备毕业设计、课程设计或期末大作业的学生,也适合需要项目实战练习的初学者。源码在OTB数据集上完成验证,能够…

2026/9/14 0:03:22

语音情感识别实战:Keras实现LSTM、CNN、SVM与MLP多模型对比

简介:面向语音情感识别入门与进阶开发者,这份基于Keras的项目源码完整实现了LSTM、CNN、SVM、MLP四种模型,兼容Python3.8与Keras/TensorFlow2环境。压缩包内含49个文件,大小约70.31MB,主体包括Python脚本、yaml/json配…

2026/9/12 6:29:36

USB Type-C PCB布局分区设计:电源、高速信号与PD协议全攻略

做硬件这行,Type-C接口算是典型的“看着简单,做起来全坑”的东西。光引脚就24个,高低速信号、电源、控制线全部塞在一个小小的连接器里,如果PCB布局不做规划,打样回来基本就是“插上没反应”、“高速掉线”、“静电一打…

2026/9/12 14:32:17

系统编程学习原型如何补齐稳定性边界

系统编程学习原型如何补齐稳定性边界预算有限时&#xff0c;我先优化明显多余的复制&#xff0c;而不是猜测性地换容器。用借用传递只读数据通常就能减少分配&#xff1a; fn parse(line: &str) -> Result<Item, Error> { /* ... */ }用基准确认热点确实在分配&am…

2026/9/13 11:18:28

雨花区哪家财务公司代理记账比较好?

在雨花区&#xff0c;企业处理财税事务常常面临诸多挑战&#xff0c;选择一家靠谱的财务公司至关重要。湖南巨勤财务管理咨询有限公司就是本地正规实体财税服务机构&#xff0c;深耕本地工商财税行业多年&#xff0c;熟悉当地工商局、税务局最新政策与申报流程。主营公司注册、…

还想了解更多?直接咨询顾问

免费诊断 + 免费方案 + 透明报价。

全国咨询热线400-8866-253
免费获取方案
咨询二维码