post-processing.html 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. <!DOCTYPE html><html lang="en"><head>
  2. <meta charset="utf-8">
  3. <title>Post Processing</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 – Post Processing">
  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. <script type="importmap">
  14. {
  15. "imports": {
  16. "three": "../../build/three.module.js"
  17. }
  18. }
  19. </script>
  20. </head>
  21. <body>
  22. <div class="container">
  23. <div class="lesson-title">
  24. <h1>Post Processing</h1>
  25. </div>
  26. <div class="lesson">
  27. <div class="lesson-main">
  28. <p><em>Post processing</em> generally refers to applying some kind of effect or filter to
  29. a 2D image. In the case of THREE.js we have a scene with a bunch of meshes in
  30. it. We render that scene into a 2D image. Normally that image is rendered
  31. directly into the canvas and displayed in the browser but instead we can <a href="rendertargets.html">render
  32. it to a render target</a> and then apply some <em>post
  33. processing</em> effects to the result before drawing it to the canvas. It's called
  34. post processing because it happens after (post) the main scene processing.</p>
  35. <p>Examples of post processing are Instagram like filters,
  36. Photoshop filters, etc...</p>
  37. <p>THREE.js has some example classes to help setup a post processing pipeline. The
  38. way it works is you create an <code class="notranslate" translate="no">EffectComposer</code> and to it you add multiple <code class="notranslate" translate="no">Pass</code>
  39. objects. You then call <code class="notranslate" translate="no">EffectComposer.render</code> and it renders your scene to a
  40. <a href="rendertargets.html">render target</a> and then applies each <code class="notranslate" translate="no">Pass</code>.</p>
  41. <p>Each <code class="notranslate" translate="no">Pass</code> can be some post processing effect like adding a vignette, blurring,
  42. applying a bloom, applying film grain, adjusting the hue, saturation, contrast,
  43. etc... and finally rendering the result to the canvas.</p>
  44. <p>It's a little bit important to understand how <code class="notranslate" translate="no">EffectComposer</code> functions. It
  45. creates two <a href="rendertargets.html">render targets</a>. Let's call them
  46. <strong>rtA</strong> and <strong>rtB</strong>.</p>
  47. <p>Then, you call <code class="notranslate" translate="no">EffectComposer.addPass</code> to add each pass in the order you want
  48. to apply them. The passes are then applied <em>something like</em> this.</p>
  49. <div class="threejs_center"><img src="../resources/images/threejs-postprocessing.svg" style="width: 600px"></div>
  50. <p>First the scene you passed into <code class="notranslate" translate="no">RenderPass</code> is rendered to <strong>rtA</strong>, then
  51. <strong>rtA</strong> is passed to the next pass, whatever it is. That pass uses <strong>rtA</strong> as
  52. input to do whatever it does and writes the results to <strong>rtB</strong>. <strong>rtB</strong> is then
  53. passed to the next pass which uses <strong>rtB</strong> as input and writes back to <strong>rtA</strong>.
  54. This continues through all the passes. </p>
  55. <p>Each <code class="notranslate" translate="no">Pass</code> has 4 basic options</p>
  56. <h2 id="-enabled-"><code class="notranslate" translate="no">enabled</code></h2>
  57. <p>Whether or not to use this pass</p>
  58. <h2 id="-needsswap-"><code class="notranslate" translate="no">needsSwap</code></h2>
  59. <p>Whether or not to swap <code class="notranslate" translate="no">rtA</code> and <code class="notranslate" translate="no">rtB</code> after finishing this pass</p>
  60. <h2 id="-clear-"><code class="notranslate" translate="no">clear</code></h2>
  61. <p>Whether or not to clear before rendering this pass</p>
  62. <h2 id="-rendertoscreen-"><code class="notranslate" translate="no">renderToScreen</code></h2>
  63. <p>Whether or not to render to the canvas instead the current destination render
  64. target. In most use cases you do not set this flag explicitly since the last pass in the pass chain is automatically rendered to screen.</p>
  65. <p>Let's put together a basic example. We'll start with the example from <a href="responsive.html">the
  66. article on responsiveness</a>.</p>
  67. <p>To that first we create an <code class="notranslate" translate="no">EffectComposer</code>.</p>
  68. <pre class="prettyprint showlinemods notranslate lang-js" translate="no">const composer = new EffectComposer(renderer);
  69. </pre>
  70. <p>Then as the first pass we add a <code class="notranslate" translate="no">RenderPass</code> that will render our scene with our
  71. camera into the first render target.</p>
  72. <pre class="prettyprint showlinemods notranslate lang-js" translate="no">composer.addPass(new RenderPass(scene, camera));
  73. </pre>
  74. <p>Next we add a <code class="notranslate" translate="no">BloomPass</code>. A <code class="notranslate" translate="no">BloomPass</code> renders its input to a generally
  75. smaller render target and blurs the result. It then adds that blurred result on
  76. top of the original input. This makes the scene <em>bloom</em></p>
  77. <pre class="prettyprint showlinemods notranslate lang-js" translate="no">const bloomPass = new BloomPass(
  78. 1, // strength
  79. 25, // kernel size
  80. 4, // sigma ?
  81. 256, // blur render target resolution
  82. );
  83. composer.addPass(bloomPass);
  84. </pre>
  85. <p>Next we had a <code class="notranslate" translate="no">FilmPass</code> that draws noise and scanlines on top of its input.</p>
  86. <pre class="prettyprint showlinemods notranslate lang-js" translate="no">const filmPass = new FilmPass(
  87. 0.5, // intensity
  88. false, // grayscale
  89. );
  90. composer.addPass(filmPass);
  91. </pre>
  92. <p>Finally we had a <code class="notranslate" translate="no">OutputPass</code> which performs color space conversion to sRGB and optional tone mapping.
  93. This pass is usually the last pass of the pass chain.
  94. </p>
  95. <pre class="prettyprint showlinemods notranslate lang-js" translate="no">const outputPass = new OutputPass();
  96. composer.addPass(outputPass);
  97. </pre>
  98. <p>To use these classes we need to import a bunch of scripts.</p>
  99. <pre class="prettyprint showlinemods notranslate lang-js" translate="no">import {EffectComposer} from 'three/addons/postprocessing/EffectComposer.js';
  100. import {RenderPass} from 'three/addons/postprocessing/RenderPass.js';
  101. import {BloomPass} from 'three/addons/postprocessing/BloomPass.js';
  102. import {FilmPass} from 'three/addons/postprocessing/FilmPass.js';
  103. import {OutputPass} from 'three/addons/postprocessing/OutputPass.js';
  104. </pre>
  105. <p>For pretty much any post processing <code class="notranslate" translate="no">EffectComposer.js</code>, <code class="notranslate" translate="no">RenderPass.js</code> and <code class="notranslate" translate="no">OutputPass.js</code>
  106. are required.</p>
  107. <p>The last things we need to do are to use <code class="notranslate" translate="no">EffectComposer.render</code> instead of
  108. <a href="/docs/#api/en/renderers/WebGLRenderer.render"><code class="notranslate" translate="no">WebGLRenderer.render</code></a> <em>and</em> to tell the <code class="notranslate" translate="no">EffectComposer</code> to match the size of
  109. the canvas.</p>
  110. <pre class="prettyprint showlinemods notranslate lang-js" translate="no">-function render(now) {
  111. - time *= 0.001;
  112. +let then = 0;
  113. +function render(now) {
  114. + now *= 0.001; // convert to seconds
  115. + const deltaTime = now - then;
  116. + then = now;
  117. if (resizeRendererToDisplaySize(renderer)) {
  118. const canvas = renderer.domElement;
  119. camera.aspect = canvas.clientWidth / canvas.clientHeight;
  120. camera.updateProjectionMatrix();
  121. + composer.setSize(canvas.width, canvas.height);
  122. }
  123. cubes.forEach((cube, ndx) =&gt; {
  124. const speed = 1 + ndx * .1;
  125. - const rot = time * speed;
  126. + const rot = now * speed;
  127. cube.rotation.x = rot;
  128. cube.rotation.y = rot;
  129. });
  130. - renderer.render(scene, camera);
  131. + composer.render(deltaTime);
  132. requestAnimationFrame(render);
  133. }
  134. </pre>
  135. <p><code class="notranslate" translate="no">EffectComposer.render</code> takes a <code class="notranslate" translate="no">deltaTime</code> which is the time in seconds since
  136. the last frame was rendered. It passes this to the various effects in case any
  137. of them are animated. In this case the <code class="notranslate" translate="no">FilmPass</code> is animated.</p>
  138. <p></p><div translate="no" class="threejs_example_container notranslate">
  139. <div><iframe class="threejs_example notranslate" translate="no" style=" " src="/manual/examples/resources/editor.html?url=/manual/examples/postprocessing.html"></iframe></div>
  140. <a class="threejs_center" href="/manual/examples/postprocessing.html" target="_blank">click here to open in a separate window</a>
  141. </div>
  142. <p></p>
  143. <p>To change effect parameters at runtime usually requires setting uniform values.
  144. Let's add a gui to adjust some of the parameters. Figuring out which values you
  145. can easily adjust and how to adjust them requires digging through the code for
  146. that effect.</p>
  147. <p>Looking inside
  148. <a href="https://github.com/mrdoob/three.js/blob/master/examples/jsm/postprocessing/BloomPass.js"><code class="notranslate" translate="no">BloomPass.js</code></a>
  149. I found this line:</p>
  150. <pre class="prettyprint showlinemods notranslate lang-js" translate="no">this.copyUniforms[ "opacity" ].value = strength;
  151. </pre>
  152. <p>So we can set the strength by setting</p>
  153. <pre class="prettyprint showlinemods notranslate lang-js" translate="no">bloomPass.copyUniforms.opacity.value = someValue;
  154. </pre>
  155. <p>Similarly looking in
  156. <a href="https://github.com/mrdoob/three.js/blob/master/examples/jsm/postprocessing/FilmPass.js"><code class="notranslate" translate="no">FilmPass.js</code></a>
  157. I found these lines:</p>
  158. <pre class="prettyprint showlinemods notranslate lang-js" translate="no">this.uniforms.intensity.value = intensity;
  159. this.uniforms.grayscale.value = grayscale;
  160. </pre>
  161. <p>So which makes it pretty clear how to set them.</p>
  162. <p>Let's make a quick GUI to set those values</p>
  163. <pre class="prettyprint showlinemods notranslate lang-js" translate="no">import {GUI} from 'three/addons/libs/lil-gui.module.min.js';
  164. </pre>
  165. <p>and</p>
  166. <pre class="prettyprint showlinemods notranslate lang-js" translate="no">const gui = new GUI();
  167. {
  168. const folder = gui.addFolder('BloomPass');
  169. folder.add(bloomPass.copyUniforms.opacity, 'value', 0, 2).name('strength');
  170. folder.open();
  171. }
  172. {
  173. const folder = gui.addFolder('FilmPass');
  174. folder.add(filmPass.uniforms.grayscale, 'value').name('grayscale');
  175. folder.add(filmPass.uniforms.intensity, 'value', 0, 1).name('intensity');
  176. folder.open();
  177. }
  178. </pre>
  179. <p>and now we can adjust those settings</p>
  180. <p></p><div translate="no" class="threejs_example_container notranslate">
  181. <div><iframe class="threejs_example notranslate" translate="no" style=" " src="/manual/examples/resources/editor.html?url=/manual/examples/postprocessing-gui.html"></iframe></div>
  182. <a class="threejs_center" href="/manual/examples/postprocessing-gui.html" target="_blank">click here to open in a separate window</a>
  183. </div>
  184. <p></p>
  185. <p>That was a small step to making our own effect.</p>
  186. <p>Post processing effects use shaders. Shaders are written in a language called
  187. <a href="https://www.khronos.org/files/opengles_shading_language.pdf">GLSL (Graphics Library Shading Language)</a>. Going
  188. over the entire language is way too large a topic for these articles. A few
  189. resources to get start from would be maybe <a href="https://webglfundamentals.org/webgl/lessons/webgl-shaders-and-glsl.html">this article</a>
  190. and maybe <a href="https://thebookofshaders.com/">the Book of Shaders</a>.</p>
  191. <p>I think an example to get you started would be helpful though so let's make a
  192. simple GLSL post processing shader. We'll make one that lets us multiply the
  193. image by a color.</p>
  194. <p>For post processing THREE.js provides a useful helper called the <code class="notranslate" translate="no">ShaderPass</code>.
  195. It takes an object with info defining a vertex shader, a fragment shader, and
  196. the default inputs. It will handling setting up which texture to read from to
  197. get the previous pass's results and where to render to, either one of the
  198. <code class="notranslate" translate="no">EffectComposer</code>s render target or the canvas.</p>
  199. <p>Here's a simple post processing shader that multiplies the previous pass's
  200. result by a color. </p>
  201. <pre class="prettyprint showlinemods notranslate lang-js" translate="no">const colorShader = {
  202. uniforms: {
  203. tDiffuse: { value: null },
  204. color: { value: new THREE.Color(0x88CCFF) },
  205. },
  206. vertexShader: `
  207. varying vec2 vUv;
  208. void main() {
  209. vUv = uv;
  210. gl_Position = projectionMatrix * modelViewMatrix * vec4(position, 1);
  211. }
  212. `,
  213. fragmentShader: `
  214. varying vec2 vUv;
  215. uniform sampler2D tDiffuse;
  216. uniform vec3 color;
  217. void main() {
  218. vec4 previousPassColor = texture2D(tDiffuse, vUv);
  219. gl_FragColor = vec4(
  220. previousPassColor.rgb * color,
  221. previousPassColor.a);
  222. }
  223. `,
  224. };
  225. </pre>
  226. <p>Above <code class="notranslate" translate="no">tDiffuse</code> is the name that <code class="notranslate" translate="no">ShaderPass</code> uses to pass in the previous
  227. pass's result texture so we pretty much always need that. We then declare
  228. <code class="notranslate" translate="no">color</code> as a THREE.js <a href="/docs/#api/en/math/Color"><code class="notranslate" translate="no">Color</code></a>.</p>
  229. <p>Next we need a vertex shader. For post processing the vertex shader shown here
  230. is pretty much standard and rarely needs to be changed. Without going into too
  231. many details (see articles linked above) the variables <code class="notranslate" translate="no">uv</code>, <code class="notranslate" translate="no">projectionMatrix</code>,
  232. <code class="notranslate" translate="no">modelViewMatrix</code> and <code class="notranslate" translate="no">position</code> are all magically added by THREE.js.</p>
  233. <p>Finally we create a fragment shader. In it we get a pixel color from the
  234. previous pass with this line</p>
  235. <pre class="prettyprint showlinemods notranslate lang-glsl" translate="no">vec4 previousPassColor = texture2D(tDiffuse, vUv);
  236. </pre>
  237. <p>we multiply it by our color and set <code class="notranslate" translate="no">gl_FragColor</code> to the result</p>
  238. <pre class="prettyprint showlinemods notranslate lang-glsl" translate="no">gl_FragColor = vec4(
  239. previousPassColor.rgb * color,
  240. previousPassColor.a);
  241. </pre>
  242. <p>Adding some simple GUI to set the 3 values of the color</p>
  243. <pre class="prettyprint showlinemods notranslate lang-js" translate="no">const gui = new GUI();
  244. gui.add(colorPass.uniforms.color.value, 'r', 0, 4).name('red');
  245. gui.add(colorPass.uniforms.color.value, 'g', 0, 4).name('green');
  246. gui.add(colorPass.uniforms.color.value, 'b', 0, 4).name('blue');
  247. </pre>
  248. <p>Gives us a simple postprocessing effect that multiplies by a color.</p>
  249. <p></p><div translate="no" class="threejs_example_container notranslate">
  250. <div><iframe class="threejs_example notranslate" translate="no" style=" " src="/manual/examples/resources/editor.html?url=/manual/examples/postprocessing-custom.html"></iframe></div>
  251. <a class="threejs_center" href="/manual/examples/postprocessing-custom.html" target="_blank">click here to open in a separate window</a>
  252. </div>
  253. <p></p>
  254. <p>As mentioned about all the details of how to write GLSL and custom shaders is
  255. too much for these articles. If you really want to know how WebGL itself works
  256. then check out <a href="https://webglfundamentals.org">these articles</a>. Another great
  257. resources is just to
  258. <a href="https://github.com/mrdoob/three.js/tree/master/examples/jsm/shaders">read through the existing post processing shaders in the THREE.js repo</a>. Some
  259. are more complicated than others but if you start with the smaller ones you can
  260. hopefully get an idea of how they work.</p>
  261. <p>Most of the post processing effects in the THREE.js repo are unfortunately
  262. undocumented so to use them you'll have to <a href="https://github.com/mrdoob/three.js/tree/master/examples">read through the examples</a> or
  263. <a href="https://github.com/mrdoob/three.js/tree/master/examples/jsm/postprocessing">the code for the effects themselves</a>.
  264. Hopefully these simple example and the article on
  265. <a href="rendertargets.html">render targets</a> provide enough context to get started.</p>
  266. </div>
  267. </div>
  268. </div>
  269. <script src="../resources/prettify.js"></script>
  270. <script src="../resources/lesson.js"></script>
  271. </body></html>