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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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. </code>
  31. <p>
  32. After all files are successfully imported, we can create our composer by passing in an instance of [page:WebGLRenderer].
  33. </p>
  34. <code>
  35. const composer = new EffectComposer( renderer );
  36. </code>
  37. <p>
  38. When using a composer, it's necessary to change the application's animation loop. Instead of calling the render method of
  39. [page:WebGLRenderer], we now use the respective counterpart of [page:EffectComposer].
  40. </p>
  41. <code>
  42. function animate() {
  43. requestAnimationFrame( animate );
  44. composer.render();
  45. }
  46. </code>
  47. <p>
  48. Our composer is now ready so it's possible to configure the chain of post-processing passes. These passes are responsible for creating
  49. the final visual output of the application. They are processed in order of their addition/insertion. In our example, the instance of `RenderPass`
  50. is executed first and then the instance of `GlitchPass`. The last enabled pass in the chain is automatically rendered to the screen. The setup
  51. of the passes looks like so:
  52. </p>
  53. <code>
  54. const renderPass = new RenderPass( scene, camera );
  55. composer.addPass( renderPass );
  56. const glitchPass = new GlitchPass();
  57. composer.addPass( glitchPass );
  58. </code>
  59. <p>
  60. `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,
  61. `GlitchPass` is going to use these image data to apply a wild glitch effect. Check out this [link:https://threejs.org/examples/webgl_postprocessing_glitch live example]
  62. to see it in action.
  63. </p>
  64. <h2>Built-in Passes</h2>
  65. <p>
  66. You can use a wide range of pre-defined post-processing passes provided by the engine. They are located in the
  67. [link:https://github.com/mrdoob/three.js/tree/dev/examples/jsm/postprocessing postprocessing] directory.
  68. </p>
  69. <h2>Custom Passes</h2>
  70. <p>
  71. Sometimes you want to write a custom post-processing shader and include it into the chain of post-processing passes. For this scenario,
  72. you can utilize `ShaderPass`. After importing the file and your custom shader, you can use the following code to setup the pass.
  73. </p>
  74. <code>
  75. import { ShaderPass } from 'three/addons/postprocessing/ShaderPass.js';
  76. import { LuminosityShader } from 'three/addons/shaders/LuminosityShader.js';
  77. // later in your init routine
  78. const luminosityPass = new ShaderPass( LuminosityShader );
  79. composer.addPass( luminosityPass );
  80. </code>
  81. <p>
  82. The repository provides a file called [link:https://github.com/mrdoob/three.js/blob/master/examples/jsm/shaders/CopyShader.js CopyShader] which is a
  83. good starting code for your own custom shader. `CopyShader` just copies the image contents of the [page:EffectComposer]'s read buffer
  84. to its write buffer without applying any effects.
  85. </p>
  86. </body>
  87. </html>