引言
在 3D 可视化与游戏化界面里,”发光”是提升质感最直接的手段:霓虹灯牌、能量护盾、粒子光晕都依赖辉光(Bloom)。Three.js 本身只负责把几何体渲染成一张普通图像,真正的辉光来自后处理(Post-Processing)管线——在渲染结果之上再做一次屏幕级图像处理。本文用一个可运行的例子,带你从零搭建辉光效果,并解释它为什么不能直接挂在 WebGLRenderer 上。
原理
后处理的核心是把”渲染一帧”拆成多道工序。Three.js 用 EffectComposer 充当流水线调度器:
RenderPass 先按常规方式把场景画进一个离屏缓冲区(而不是直接画到屏幕);
- 后续的 Pass(如
UnrealBloomPass)读取上一道工序的输出,做模糊、叠加等操作,再把结果交给下一道;
- 最后一道 Pass 把像素真正写到屏幕上。
UnrealBloomPass 的实现是经典的”亮度提取 + 多级高斯模糊 + 叠加”:它先按阈值(threshold)抠出发光区域,再用不同半径做几层模糊,最后把模糊结果按强度(strength)加回原图。因此参数调得好,发光物体会”溢光”,非发光物体保持干净。
代码实现
下面是一份可直接保存为 .html 打开运行的示例(使用 importmap 引入 three 模块):
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
| <!DOCTYPE html> <html lang="zh-CN"> <head> <meta charset="UTF-8" /> <title>Three.js Bloom Demo</title> <style> body { margin: 0; } #app { width: 100vw; height: 100vh; } </style> </head> <body> <div id="app"></div> <script type="importmap"> { "imports": { "three": "https://unpkg.com/three@0.160.0/build/three.module.js", "three/addons/": "https://unpkg.com/three@0.160.0/examples/jsm/" } } </script> <script type="module"> import * as THREE from 'three'; import { OrbitControls } from 'three/addons/controls/OrbitControls.js'; import { EffectComposer } from 'three/addons/postprocessing/EffectComposer.js'; import { RenderPass } from 'three/addons/postprocessing/RenderPass.js'; import { UnrealBloomPass } from 'three/addons/postprocessing/UnrealBloomPass.js';
const app = document.getElementById('app'); const scene = new THREE.Scene(); scene.background = new THREE.Color(0x0b0b12);
const camera = new THREE.PerspectiveCamera(45, innerWidth / innerHeight, 0.1, 100); camera.position.set(0, 2, 6);
const renderer = new THREE.WebGLRenderer({ antialias: true }); renderer.setSize(innerWidth, innerHeight); renderer.setPixelRatio(Math.min(devicePixelRatio, 2)); app.appendChild(renderer.domElement);
const controls = new OrbitControls(camera, renderer.domElement); controls.enableDamping = true;
scene.add(new THREE.AmbientLight(0xffffff, 0.4)); const dir = new THREE.DirectionalLight(0xffffff, 0.8); dir.position.set(3, 5, 4); scene.add(dir);
const glowMat = new THREE.MeshStandardMaterial({ color: 0x2266ff, emissive: 0x2266ff, emissiveIntensity: 2.5, roughness: 0.3 }); const glowSphere = new THREE.Mesh(new THREE.SphereGeometry(0.8, 32, 32), glowMat); glowSphere.position.x = -1.6; scene.add(glowSphere);
const normalMat = new THREE.MeshStandardMaterial({ color: 0x888888, roughness: 0.8 }); const normalSphere = new THREE.Mesh(new THREE.SphereGeometry(0.8, 32, 32), normalMat); normalSphere.position.x = 1.6; scene.add(normalSphere);
const composer = new EffectComposer(renderer); composer.addPass(new RenderPass(scene, camera)); const bloom = new UnrealBloomPass( new THREE.Vector2(innerWidth, innerHeight), 0.9, 0.4, 0.85 ); composer.addPass(bloom);
addEventListener('resize', () => { camera.aspect = innerWidth / innerHeight; camera.updateProjectionMatrix(); renderer.setSize(innerWidth, innerHeight); composer.setSize(innerWidth, innerHeight); });
function animate() { requestAnimationFrame(animate); glowSphere.rotation.y += 0.01; controls.update(); composer.render(); } animate(); </script> </body> </html>
|
常见坑
- 用了 composer 还调用
renderer.render。后处理接管了最终输出,必须调用 composer.render(),否则屏幕上会是一片黑或只有未处理的画面。
- 抗锯齿失效。
EffectComposer 默认用 WebGLRenderTarget,会丢掉 renderer 的 antialias。解决:给 composer 传带 samples 参数的 render target,或在管线末端叠加 OutputPass(较新版本 three 支持 MSAA 离屏缓冲)。
- 辉光”糊”住整个画面。通常是 threshold 设得太低(比如 0),让所有像素都发光。把 threshold 提到 0.8 以上,并只对需要发光的物体调高
emissiveIntensity。
- 性能骤降。bloom 要做多遍全屏模糊,分辨率越高越吃 GPU。用
setPixelRatio 限制到 2,并在移动端适当下调 strength / radius。
- 颜色发灰。新版 three 默认输出色彩空间为 sRGB,bloom 管线末端应加
OutputPass 做色彩空间与色调映射转换,否则高光偏灰。
小结
辉光不是材质属性,而是屏幕后处理;记住”RenderPass 打底、UnrealBloomPass 提取高亮、composer.render 收尾”这个三段式,就能给任意 Three.js 场景加上干净的发光。调参时优先动 threshold 控制”谁发光”,再动 strength 控制”发多亮”,最后用 radius 柔化边缘。把它和之前的射线拾取、实例化渲染组合,你的可视化项目就能既有交互又有质感。