webgpu-postprocessing.html 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. <!DOCTYPE html><html lang="zh"><head>
  2. <meta charset="utf-8">
  3. <title>WebGPURenderer 后处理</title>
  4. <meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
  5. <meta name="twitter:card" content="summary_large_image">
  6. <meta name="twitter:site" content="@threejs">
  7. <meta name="twitter:title" content="Three.js - WebGPURenderer 后处理">
  8. <meta property="og:image" content="https://threejs.org/files/share.png">
  9. <link rel="shortcut icon" href="../../files/favicon_white.ico" media="(prefers-color-scheme: dark)">
  10. <link rel="shortcut icon" href="../../files/favicon.ico" media="(prefers-color-scheme: light)">
  11. <link rel="stylesheet" href="../resources/lesson.css">
  12. <link rel="stylesheet" href="../resources/lang.css">
  13. <link rel="stylesheet" href="/manual/zh/lang.css">
  14. <script type="importmap">
  15. {
  16. "imports": {
  17. "three": "../../build/three.module.js"
  18. }
  19. }
  20. </script>
  21. </head>
  22. <body>
  23. <div class="container">
  24. <div class="lesson-title">
  25. <h1>WebGPURenderer 后处理</h1>
  26. </div>
  27. <div class="lesson">
  28. <div class="lesson-main">
  29. <p>
  30. `WebGPURenderer` 提供了全新的后处理组件。本文介绍其工作方式和基础用法。
  31. </p>
  32. <h2>概述</h2>
  33. <p>
  34. 旧版 `WebGLRenderer` 的后处理在设计上存在一些限制。由于渲染器支持不足,
  35. 使用 MRT(多渲染目标)较繁琐,同时缺少自动 pass/effect 合并机制,
  36. 难以优化整体性能。
  37. </p>
  38. <p>
  39. 新版 `WebGPURenderer` 后处理栈从一开始就面向这些需求设计。
  40. </p>
  41. <ul>
  42. <li>
  43. 内置完整 MRT 支持。
  44. </li>
  45. <li>
  46. 系统会在可能时自动合并效果,减少总渲染 pass 数量。
  47. </li>
  48. <li>
  49. 效果链通过节点组合表达,可更灵活地搭建后处理流程。
  50. </li>
  51. </ul>
  52. <p>
  53. 下面来看如何在 three.js 应用中接入这套后处理系统。
  54. </p>
  55. <h2>基础用法</h2>
  56. <p>
  57. 请先阅读 <a href="webgpurenderer">WebGPURenderer</a> 指南并正确配置导入。
  58. 然后按如下方式创建渲染管线模块实例:
  59. </p>
  60. <pre class="prettyprint showlinemods notranslate lang-js" translate="no">
  61. const renderPipeline = new THREE.RenderPipeline( renderer );
  62. </pre>
  63. <p>
  64. `RenderPipeline` 用于替代旧的 `EffectComposer`。
  65. 要确保最终输出来自该模块,需要把动画循环改成如下形式:
  66. </p>
  67. <pre class="prettyprint showlinemods notranslate lang-js" translate="no">
  68. - renderer.render( scene, camera );
  69. + renderPipeline.render();
  70. </pre>
  71. <p>
  72. 大多数后处理流程都会先创建一个所谓的场景 pass(scene pass),也称 beauty pass,
  73. 作为原始渲染图像,然后再叠加 Bloom、景深、SSR 等效果。
  74. 先从 TSL 命名空间导入 `pass()` 并创建该 pass。
  75. </p>
  76. <pre class="prettyprint showlinemods notranslate lang-js" translate="no">
  77. import { pass } from 'three/tsl';
  78. // 在你的初始化流程中
  79. const scenePass = pass( scene, camera );
  80. </pre>
  81. <p>
  82. 节点系统的核心思想是:把材质或后处理效果表示为节点组合。
  83. 例如要实现 DotScreen 与 RGB Shift,只需创建对应效果节点并串联。
  84. </p>
  85. <pre class="prettyprint showlinemods notranslate lang-js" translate="no">
  86. import { pass } from 'three/tsl';
  87. + import { dotScreen } from 'three/addons/tsl/display/DotScreenNode.js';
  88. + import { rgbShift } from 'three/addons/tsl/display/RGBShiftNode.js';
  89. // 在你的初始化流程中
  90. const scenePass = pass( scene, camera );
  91. + const dotScreenPass = dotScreen( scenePass );
  92. + const rgbShiftPass = rgbShift( dotScreenPass );
  93. </pre>
  94. <p>
  95. 完成后,把最终节点赋给 `RenderPipeline` 即可。
  96. </p>
  97. <pre class="prettyprint showlinemods notranslate lang-js" translate="no">
  98. renderPipeline.outputNode = rgbShiftPass;
  99. </pre>
  100. <h2>色调映射与色彩空间</h2>
  101. <p>
  102. 使用后处理时,色调映射与色彩空间转换会在效果链末尾自动执行。
  103. 但某些场景你可能希望完全控制执行时机与顺序。
  104. 例如使用 `FXAANode` 做 FXAA,或用 `Lut3DNode` 做调色时,
  105. 可以关闭自动处理,并通过 `renderOutput()` 手动应用。
  106. </p>
  107. <pre class="prettyprint showlinemods notranslate lang-js" translate="no">
  108. import { pass, renderOutput } from 'three/tsl';
  109. import { fxaa } from 'three/addons/tsl/display/FXAANode.js';
  110. // 在你的初始化流程中
  111. const renderPipeline = new THREE.RenderPipeline( renderer );
  112. renderPipeline.outputColorTransform = false; // 禁用默认输出色彩变换
  113. const scenePass = pass( scene, camera );
  114. const outputPass = renderOutput( scenePass ); // 在这里应用色调映射和色彩空间转换
  115. // FXAA 必须在 sRGB 色彩空间中计算
  116. const fxaaPass = fxaa( outputPass );
  117. renderPipeline.outputNode = fxaaPass;
  118. </pre>
  119. <p>
  120. `renderOutput()` 不是强制的,你也可以按需求自行实现色调映射与色彩空间转换。
  121. </p>
  122. <h2>MRT(多渲染目标)</h2>
  123. <p>
  124. 新后处理栈内置 MRT,对高级效果非常关键。MRT 允许你在一次渲染 pass 中产生多个输出。
  125. 例如使用 TRAA 时,你可以按下述配置准备抗锯齿输入。
  126. </p>
  127. <pre class="prettyprint showlinemods notranslate lang-js" translate="no">
  128. import { pass, mrt, output, velocity } from 'three/tsl';
  129. // 在你的初始化流程中
  130. const scenePass = pass( scene, camera );
  131. scenePass.setMRT( mrt( {
  132. output: output,
  133. velocity: velocity
  134. } ) );
  135. </pre>
  136. <p>
  137. 传给 `mrt()` 的配置对象用于描述该 pass 的各个输出。
  138. 本例中我们保存默认输出(场景主图)和速度信息,用于 TRAA。
  139. 如果还需要深度,一般无需额外作为 MRT 输出配置;
  140. 在默认输出 pass 中按需请求即可获取。
  141. 若后续效果需要这些结果,可把它们作为纹理节点读取。
  142. </p>
  143. <pre class="prettyprint showlinemods notranslate lang-js" translate="no">
  144. import { traa } from 'three/addons/tsl/display/TRAANode.js';
  145. // 在你的初始化流程中
  146. const scenePassColor = scenePass.getTextureNode( 'output' );
  147. const scenePassDepth = scenePass.getTextureNode( 'depth' );
  148. const scenePassVelocity = scenePass.getTextureNode( 'velocity' );
  149. const traaPass = traa( scenePassColor, scenePassDepth, scenePassVelocity, camera );
  150. renderPipeline.outputNode = traaPass;
  151. </pre>
  152. <p>
  153. MRT 配置取决于你的具体方案。你可以使用 `output`、`velocity`、`normalView`、
  154. `emissive` 等 TSL 对象,把片元数据写入不同 attachment。
  155. 在复杂 MRT 流程中,为提升性能并避免显存压力,必须做好数据打包与格式优化。
  156. 默认 attachment 精度是 RGBA16(Half-Float),并非所有数据都需要这么高。
  157. 例如下方把 `diffuseColor` 改为 RGBA8,可将带宽和内存占用减半。
  158. </p>
  159. <pre class="prettyprint showlinemods notranslate lang-js" translate="no">
  160. const diffuseTexture = scenePass.getTexture( 'diffuseColor' );
  161. diffuseTexture.type = THREE.UnsignedByteType;
  162. </pre>
  163. <p>
  164. 下方 SSR(屏幕空间反射)示例把默认 FP16 法线转换为 RGBA8 颜色,
  165. 并将金属度/粗糙度打包到单个 attachment。配合 `sample()` TSL 函数可实现自定义解包,
  166. 本例中会把颜色还原为归一化方向向量。
  167. </p>
  168. <pre class="prettyprint showlinemods notranslate lang-js" translate="no">
  169. scenePass.setMRT( mrt( {
  170. output: output,
  171. normal: directionToColor( normalView ),
  172. metalrough: vec2( metalness, roughness )
  173. } ) );
  174. // 使用 RGBA8 替代 RGBA16
  175. const normalTexture = scenePass.getTexture( 'normal' );
  176. normalTexture.type = THREE.UnsignedByteType;
  177. const metalRoughTexture = scenePass.getTexture( 'metalrough' );
  178. metalRoughTexture.type = THREE.UnsignedByteType;
  179. // 自定义解包。后续效果里请使用得到的 "sceneNormal"
  180. // 来替代 "scenePassNormal"
  181. const sceneNormal = sample( ( uv ) => {
  182. return colorToDirection( scenePassNormal.sample( uv ) );
  183. } );
  184. </pre>
  185. <p>
  186. 后续还会继续增强打包/解包能力,提供更多 MRT 数据组织方式。
  187. 目前建议参考
  188. <a href="https://threejs.org/examples/?q=webgpu%20postprocessing" target="_blank">官方示例</a>,
  189. 了解现有效果和配置模式。
  190. </p>
  191. </div>
  192. </div>
  193. </div>
  194. <script src="../resources/prettify.js"></script>
  195. <script src="../resources/lesson.js"></script>
  196. </body>
  197. </html>
粤ICP备19079148号