2
0

How-to-use-post-processing.html 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="utf-8">
  5. <base href="../../../" />
  6. <script src="page.js"></script>
  7. <link type="text/css" rel="stylesheet" href="page.css" />
  8. </head>
  9. <body>
  10. <h1>How to use post-processing</h1>
  11. <p>
  12. Many three.js applications render their 3D objects directly to the screen. Sometimes, however, you want to apply one or more graphical
  13. effects like Depth-Of-Field, Bloom, Film Grain or various types of Anti-aliasing. Post-processing is a widely used approach
  14. to implement such effects. First, the scene is rendered to a render target which represents a buffer in the video card's memory.
  15. In the next step one or more post-processing passes apply filters and effects to the image buffer before it is eventually rendered to
  16. the screen.
  17. </p>
  18. <p>
  19. three.js provides a complete post-processing solution via [page:EffectComposer] to implement such a workflow.
  20. </p>
  21. <h2>Workflow</h2>
  22. <p>
  23. The first step in the process is to import all necessary files from the examples directory. The guide assumes you are using the official
  24. [link:https://www.npmjs.com/package/three npm package] of three.js. For our basic demo in this guide we need the following files.
  25. </p>
  26. <code>
  27. import { EffectComposer } from 'three/addons/postprocessing/EffectComposer.js';
  28. import { RenderPass } from 'three/addons/postprocessing/RenderPass.js';
  29. import { GlitchPass } from 'three/addons/postprocessing/GlitchPass.js';
  30. import { OutputPass } from 'three/addons/postprocessing/OutputPass.js';
  31. </code>
  32. <p>
  33. After all files are successfully imported, we can create our composer by passing in an instance of [page:WebGLRenderer].
  34. </p>
  35. <code>
  36. const composer = new EffectComposer( renderer );
  37. </code>
  38. <p>
  39. When using a composer, it's necessary to change the application's animation loop. Instead of calling the render method of
  40. [page:WebGLRenderer], we now use the respective counterpart of [page:EffectComposer].
  41. </p>
  42. <code>
  43. function animate() {
  44. requestAnimationFrame( animate );
  45. composer.render();
  46. }
  47. </code>
  48. <p>
  49. Our composer is now ready so it's possible to configure the chain of post-processing passes. These passes are responsible for creating
  50. the final visual output of the application. They are processed in order of their addition/insertion. In our example, the instance of `RenderPass`
  51. is executed first, then the instance of `GlitchPass` and finally `OutputPass`. The last enabled pass in the chain is automatically rendered to the screen.
  52. The setup of the passes looks like so:
  53. </p>
  54. <code>
  55. const renderPass = new RenderPass( scene, camera );
  56. composer.addPass( renderPass );
  57. const glitchPass = new GlitchPass();
  58. composer.addPass( glitchPass );
  59. const outputPass = new OutputPass();
  60. composer.addPass( outputPass );
  61. </code>
  62. <p>
  63. `RenderPass` is normally placed at the beginning of the chain in order to provide the rendered scene as an input for the next post-processing step. In our case,
  64. `GlitchPass` is going to use these image data to apply a wild glitch effect. `OutputPass` is usually the last pass in the chain which performs sRGB color space conversion and tone mapping.
  65. Check out this [link:https://threejs.org/examples/webgl_postprocessing_glitch live example] to see it in action.
  66. </p>
  67. <h2>Built-in Passes</h2>
  68. <p>
  69. You can use a wide range of pre-defined post-processing passes provided by the engine. They are located in the
  70. [link:https://github.com/mrdoob/three.js/tree/dev/examples/jsm/postprocessing postprocessing] directory.
  71. </p>
  72. <h2>Custom Passes</h2>
  73. <p>
  74. Sometimes you want to write a custom post-processing shader and include it into the chain of post-processing passes. For this scenario,
  75. you can utilize `ShaderPass`. After importing the file and your custom shader, you can use the following code to setup the pass.
  76. </p>
  77. <code>
  78. import { ShaderPass } from 'three/addons/postprocessing/ShaderPass.js';
  79. import { LuminosityShader } from 'three/addons/shaders/LuminosityShader.js';
  80. // later in your init routine
  81. const luminosityPass = new ShaderPass( LuminosityShader );
  82. composer.addPass( luminosityPass );
  83. </code>
  84. <p>
  85. The repository provides a file called [link:https://github.com/mrdoob/three.js/blob/master/examples/jsm/shaders/CopyShader.js CopyShader] which is a
  86. good starting code for your own custom shader. `CopyShader` just copies the image contents of the [page:EffectComposer]'s read buffer
  87. to its write buffer without applying any effects.
  88. </p>
  89. </body>
  90. </html>