当前位置: 首页 > news >正文

第五部分-后期特效与着色器——25. 内置特效

25. 内置特效

1. 概述

Three.js 提供了多种内置后期特效,包括泛光(Bloom)、景深(Depth of Field)、轮廓描边(Outline)、颜色校正等。这些特效可以组合使用,创造出丰富的视觉效果。

┌─────────────────────────────────────────────────────────────┐ │ 内置特效类型 │ ├─────────────────────────────────────────────────────────────┤ │ │ │ 泛光效果 (UnrealBloomPass) │ │ ├── 模拟高光溢出 │ │ ├── 增强亮部区域 │ │ └── 参数:强度、半径、阈值 │ │ │ │ 景深效果 (BokehPass) │ │ ├── 模拟相机焦距 │ │ ├── 背景模糊 │ │ └── 参数:焦距、光圈、焦点距离 │ │ │ │ 轮廓描边 (OutlinePass) │ │ ├── 物体边缘高亮 │ │ ├── 选择特定物体 │ │ └── 参数:边缘颜色、强度 │ │ │ │ 颜色校正 (Afterimage) │ │ ├── 残影效果 │ │ ├── 运动模糊 │ │ └── 参数:衰减、阻尼 │ │ │ └─────────────────────────────────────────────────────────────┘

2. 泛光效果(UnrealBloomPass)

泛光效果模拟高亮区域的发光效果,常用于科幻、梦幻风格。

2.1 安装和导入

import{EffectComposer}from'three/examples/jsm/postprocessing/EffectComposer.js';import{RenderPass}from'three/examples/jsm/postprocessing/RenderPass.js';import{UnrealBloomPass}from'three/examples/jsm/postprocessing/UnrealBloomPass.js';

2.2 创建泛光特效

// 创建合成器constcomposer=newEffectComposer(renderer);// 渲染通道constrenderPass=newRenderPass(scene,camera);composer.addPass(renderPass);// 泛光通道constbloomPass=newUnrealBloomPass(newTHREE.Vector2(window.innerWidth,window.innerHeight),1.5,0.4,0.85);bloomPass.threshold=0.1;// 亮度阈值(高于此值的像素会发光)bloomPass.strength=1.2;// 发光强度bloomPass.radius=0.5;// 发光半径composer.addPass(bloomPass);

2.3 参数详解

参数说明范围默认值
threshold亮度阈值,只有高于此值的像素产生泛光0-10
strength泛光强度0-31
radius泛光半径0-10.5

3. 景深效果(BokehPass)

景深效果模拟相机镜头对焦,使背景或前景模糊。

3.1 创建景深特效

import{BokehPass}from'three/examples/jsm/postprocessing/BokehPass.js';// 需要深度材质constdepthMaterial=newTHREE.MeshDepthMaterial();constbokehPass=newBokehPass(scene,camera,{focus:1.0,// 焦点距离aperture:0.025,// 光圈大小maxblur:0.01,// 最大模糊量width:window.innerWidth,height:window.innerHeight});composer.addPass(bokehPass);

3.2 参数详解

参数说明
focus焦点距离(相机到焦点的距离)
aperture光圈大小(影响模糊程度)
maxblur最大模糊半径

4. 轮廓描边(OutlinePass)

轮廓描边用于高亮显示选中的物体。

4.1 创建轮廓描边

import{OutlinePass}from'three/examples/jsm/postprocessing/OutlinePass.js';constoutlinePass=newOutlinePass(newTHREE.Vector2(window.innerWidth,window.innerHeight),scene,camera);outlinePass.edgeStrength=3.0;// 边缘强度outlinePass.edgeGlow=0.5;// 边缘发光outlinePass.edgeThickness=1.0;// 边缘厚度outlinePass.pulsePeriod=2;// 脉冲周期outlinePass.visibleEdgeColor.set(0xff0000);// 可见边缘颜色outlinePass.hiddenEdgeColor.set(0x00ff00);// 隐藏边缘颜色// 设置需要描边的物体outlinePass.selectedObjects=[sphere,cube];composer.addPass(outlinePass);

4.2 参数详解

参数说明
edgeStrength边缘强度
edgeGlow边缘发光效果
edgeThickness边缘厚度
pulsePeriod脉冲周期(0=无脉冲)
visibleEdgeColor可见边缘颜色
hiddenEdgeColor隐藏边缘颜色
selectedObjects需要描边的物体数组

5. 完整示例

import*asTHREEfrom'three';import{OrbitControls}from'three/examples/jsm/controls/OrbitControls.js';import{EffectComposer}from'three/examples/jsm/postprocessing/EffectComposer.js';import{RenderPass}from'three/examples/jsm/postprocessing/RenderPass.js';import{UnrealBloomPass}from'three/examples/jsm/postprocessing/UnrealBloomPass.js';import{OutlinePass}from'three/examples/jsm/postprocessing/OutlinePass.js';import{EffectPass}from'three/examples/jsm/postprocessing/EffectPass.js';import{AfterimageEffect}from'three/examples/jsm/effects/AfterimageEffect.js';constscene=newTHREE.Scene();scene.background=newTHREE.Color(0x000000);constcamera=newTHREE.PerspectiveCamera(45,window.innerWidth/window.innerHeight,0.1,1000);camera.position.set(5,4,8);camera.lookAt(0,0,0);constrenderer=newTHREE.WebGLRenderer({antialias:true});renderer.setSize(window.innerWidth,window.innerHeight);renderer.toneMapping=THREE.ReinhardToneMapping;document.body.appendChild(renderer.domElement);constcontrols=newOrbitControls(camera,renderer.domElement);controls.enableDamping=true;// 光源constambientLight=newTHREE.AmbientLight(0x404040,0.3);scene.add(ambientLight);constpointLight=newTHREE.PointLight(0xffffff,1);pointLight.position.set(2,3,4);scene.add(pointLight);// 创建物体constgeometry=newTHREE.SphereGeometry(0.8,64,64);constmaterial=newTHREE.MeshStandardMaterial({color:0xff6600,metalness:0.5,roughness:0.3,emissive:0x442200});constsphere=newTHREE.Mesh(geometry,material);sphere.castShadow=true;scene.add(sphere);constboxGeometry=newTHREE.BoxGeometry(1,1,1);constboxMaterial=newTHREE.MeshStandardMaterial({color:0x44aa88,metalness:0.5,roughness:0.3,emissive:0x004422});constcube=newTHREE.Mesh(boxGeometry,boxMaterial);cube.position.set(1.5,0,1.5);cube.castShadow=true;scene.add(cube);consttorusGeometry=newTHREE.TorusKnotGeometry(0.6,0.2,100,16);consttorusMaterial=newTHREE.MeshStandardMaterial({color:0x88aaff,metalness:0.7,roughness:0.2,emissive:0x004466});consttorus=newTHREE.Mesh(torusGeometry,torusMaterial);torus.position.set(-1.5,0,-1.5);torus.castShadow=true;scene.add(torus);// 地面constplaneGeometry=newTHREE.PlaneGeometry(10,10);constplaneMaterial=newTHREE.MeshStandardMaterial({color:0x224466,side:THREE.DoubleSide});constplane=newTHREE.Mesh(planeGeometry,planeMaterial);plane.rotation.x=-Math.PI/2;plane.position.y=-1;plane.receiveShadow=true;scene.add(plane);// 创建 EffectComposerconstcomposer=newEffectComposer(renderer);// 渲染通道constrenderPass=newRenderPass(scene,camera);composer.addPass(renderPass);// 泛光效果constbloomPass=newUnrealBloomPass(newTHREE.Vector2(window.innerWidth,window.innerHeight),1.5,0.4,0.85);bloomPass.threshold=0.1;bloomPass.strength=0.8;bloomPass.radius=0.5;composer.addPass(bloomPass);// 轮廓描边constoutlinePass=newOutlinePass(newTHREE.Vector2(window.innerWidth,window.innerHeight),scene,camera);outlinePass.edgeStrength=3;outlinePass.edgeGlow=0.5;outlinePass.edgeThickness=1;outlinePass.pulsePeriod=2;outlinePass.visibleEdgeColor.set(0xff6600);outlinePass.selectedObjects=[sphere,cube,torus];composer.addPass(outlinePass);// GUI 控制importGUIfrom'lil-gui';constgui=newGUI();constbloomFolder=gui.addFolder('泛光控制');bloomFolder.add(bloomPass,'threshold',0,1).name('亮度阈值');bloomFolder.add(bloomPass,'strength',0,2).name('强度');bloomFolder.add(bloomPass,'radius',0,1).name('半径');bloomFolder.open();constoutlineFolder=gui.addFolder('轮廓控制');outlineFolder.add(outlinePass,'edgeStrength',0,5).name('边缘强度');outlineFolder.add(outlinePass,'edgeGlow',0,1).name('边缘发光');outlineFolder.add(outlinePass,'edgeThickness',0,3).name('边缘厚度');outlineFolder.add(outlinePass,'pulsePeriod',0,5).name('脉冲周期');outlineFolder.open();// 动画lettime=0;functionanimate(){requestAnimationFrame(animate);time+=0.01;sphere.rotation.y+=0.01;cube.rotation.x+=0.01;cube.rotation.y+=0.01;torus.rotation.x+=0.01;torus.rotation.y+=0.02;// 动态光源pointLight.position.x=2+Math.sin(time)*1.5;pointLight.position.z=4+Math.cos(time)*1.5;controls.update();composer.render();}animate();window.addEventListener('resize',onWindowResize,false);functiononWindowResize(){camera.aspect=window.innerWidth/window.innerHeight;camera.updateProjectionMatrix();composer.setSize(window.innerWidth,window.innerHeight);}

6. 特效对比

特效用途性能
UnrealBloomPass泛光/发光中等
BokehPass景深较高
OutlinePass轮廓描边中等
AfterimageEffect残影
GlitchPass故障效果
FilmPass胶片效果

7. 总结

特效类用途主要参数
UnrealBloomPass泛光threshold, strength, radius
BokehPass景深focus, aperture, maxblur
OutlinePass轮廓edgeStrength, edgeGlow, edgeThickness
AfterimageEffect残影damp, shader

http://www.jsqmd.com/news/747678/

相关文章:

  • 2026现阶段,浙江企业团建为何首选“包吃包住”?深度解析与高口碑目的地推荐 - 2026年企业推荐榜
  • Sunshine:5分钟搭建个人游戏串流服务器,让任何设备都能畅玩PC游戏
  • Hugging Face lerobot:机器人学习的开源利器与应用实践
  • 2025届毕业生推荐的AI学术方案横评
  • 论文自动转视频技术:Paper2Video框架解析与应用
  • 终极星露谷物语模组合集指南:15个必备SMAPI模组提升游戏体验
  • MOREBENCH:大语言模型道德推理能力评估新基准
  • Java实现Llama 3本地推理:轻量级引擎设计与企业级集成实践
  • 物理引擎如何提升AI舞蹈动作的自然度
  • Tracecat:AI原生安全自动化平台架构解析与实战指南
  • 2026年AI真人剧人才培训**指南:如何选择高通过率的机构 - 2026年企业推荐榜
  • BM25算法解析:信息检索的核心排序技术
  • 别再手动K帧了!Blender 3.6自动关键帧与插值技巧,让你的动画丝滑又高效
  • 网盘直链下载助手LinkSwift:八大网盘免费获取真实下载链接的终极解决方案
  • 别再让电机发烫!STM32 FOC开环标定零电角度的安全操作指南
  • PDPS镜像对象保姆级教程:从单个零件到整站布局,5分钟搞定对称模型
  • 50.YOLOv8 工业级全流程实战(CUDA118):训练 + 推理 + ONNX 导出 + TensorRT 加速 + Flask 部署,全套可复制源码 + 避坑指南
  • 揭秘NBTExplorer:专业级Minecraft数据可视化编辑实战指南
  • 别再让大图拖慢你的网站了!用Docker Compose一键部署imgproxy,给MinIO图片服务加个‘瘦身’插件
  • 大语言模型评估:静态测试与生成式方法对比
  • 当理想撞上现实:我是如何用‘断臂求生’策略,拆分硬件创业团队并重启项目的
  • 2026年现阶段山西塑胶地板优质服务商联系与选择全解析 - 2026年企业推荐榜
  • 本地化AI伴侣Amica:私有部署、角色定制与全流程实战指南
  • 别再只懂console.log了!Node.js process模块的7个实战用法,从环境变量到内存监控
  • 在 Hermes Agent 项目中集成 Taotoken 作为自定义模型源
  • 2026萧山考试提分服务标杆名录:慈溪考试提分、新昌考试提分、杭州市区考试提分、柯桥考试提分、桐乡考试提分、桐庐考试提分选择指南 - 优质品牌商家
  • 从金融核心系统到IoT边缘设备:Python数据库适配的7层抽象模型(附架构图与可复用Adapter基类)
  • MedCLIPSeg:基于CLIP的医学图像小样本分割技术
  • RAGFlow 系列教程 第十课:LLM 抽象层 -- 统一模型接口
  • 机器翻译质量评估与优化实战指南