OutputPass.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. import {
  2. ColorManagement,
  3. RawShaderMaterial,
  4. UniformsUtils,
  5. LinearToneMapping,
  6. ReinhardToneMapping,
  7. CineonToneMapping,
  8. AgXToneMapping,
  9. ACESFilmicToneMapping,
  10. NeutralToneMapping,
  11. CustomToneMapping,
  12. SRGBTransfer
  13. } from 'three';
  14. import { Pass, FullScreenQuad } from './Pass.js';
  15. import { OutputShader } from '../shaders/OutputShader.js';
  16. /**
  17. * This pass is responsible for including tone mapping and color space conversion
  18. * into your pass chain. In most cases, this pass should be included at the end
  19. * of each pass chain. If a pass requires sRGB input (e.g. like FXAA), the pass
  20. * must follow `OutputPass` in the pass chain.
  21. *
  22. * The tone mapping and color space settings are extracted from the renderer.
  23. *
  24. * ```js
  25. * const outputPass = new OutputPass();
  26. * composer.addPass( outputPass );
  27. * ```
  28. *
  29. * @augments Pass
  30. * @three_import import { OutputPass } from 'three/addons/postprocessing/OutputPass.js';
  31. */
  32. class OutputPass extends Pass {
  33. /**
  34. * Constructs a new output pass.
  35. */
  36. constructor() {
  37. super();
  38. /**
  39. * The pass uniforms.
  40. *
  41. * @type {Object}
  42. */
  43. this.uniforms = UniformsUtils.clone( OutputShader.uniforms );
  44. /**
  45. * The pass material.
  46. *
  47. * @type {RawShaderMaterial}
  48. */
  49. this.material = new RawShaderMaterial( {
  50. name: OutputShader.name,
  51. uniforms: this.uniforms,
  52. vertexShader: OutputShader.vertexShader,
  53. fragmentShader: OutputShader.fragmentShader
  54. } );
  55. // internals
  56. this._fsQuad = new FullScreenQuad( this.material );
  57. this._outputColorSpace = null;
  58. this._toneMapping = null;
  59. }
  60. /**
  61. * Performs the output pass.
  62. *
  63. * @param {WebGLRenderer} renderer - The renderer.
  64. * @param {WebGLRenderTarget} writeBuffer - The write buffer. This buffer is intended as the rendering
  65. * destination for the pass.
  66. * @param {WebGLRenderTarget} readBuffer - The read buffer. The pass can access the result from the
  67. * previous pass from this buffer.
  68. * @param {number} deltaTime - The delta time in seconds.
  69. * @param {boolean} maskActive - Whether masking is active or not.
  70. */
  71. render( renderer, writeBuffer, readBuffer/*, deltaTime, maskActive */ ) {
  72. this.uniforms[ 'tDiffuse' ].value = readBuffer.texture;
  73. this.uniforms[ 'toneMappingExposure' ].value = renderer.toneMappingExposure;
  74. // rebuild defines if required
  75. if ( this._outputColorSpace !== renderer.outputColorSpace || this._toneMapping !== renderer.toneMapping ) {
  76. this._outputColorSpace = renderer.outputColorSpace;
  77. this._toneMapping = renderer.toneMapping;
  78. this.material.defines = {};
  79. if ( ColorManagement.getTransfer( this._outputColorSpace ) === SRGBTransfer ) this.material.defines.SRGB_TRANSFER = '';
  80. if ( this._toneMapping === LinearToneMapping ) this.material.defines.LINEAR_TONE_MAPPING = '';
  81. else if ( this._toneMapping === ReinhardToneMapping ) this.material.defines.REINHARD_TONE_MAPPING = '';
  82. else if ( this._toneMapping === CineonToneMapping ) this.material.defines.CINEON_TONE_MAPPING = '';
  83. else if ( this._toneMapping === ACESFilmicToneMapping ) this.material.defines.ACES_FILMIC_TONE_MAPPING = '';
  84. else if ( this._toneMapping === AgXToneMapping ) this.material.defines.AGX_TONE_MAPPING = '';
  85. else if ( this._toneMapping === NeutralToneMapping ) this.material.defines.NEUTRAL_TONE_MAPPING = '';
  86. else if ( this._toneMapping === CustomToneMapping ) this.material.defines.CUSTOM_TONE_MAPPING = '';
  87. this.material.needsUpdate = true;
  88. }
  89. //
  90. if ( this.renderToScreen === true ) {
  91. renderer.setRenderTarget( null );
  92. this._fsQuad.render( renderer );
  93. } else {
  94. renderer.setRenderTarget( writeBuffer );
  95. if ( this.clear ) renderer.clear( renderer.autoClearColor, renderer.autoClearDepth, renderer.autoClearStencil );
  96. this._fsQuad.render( renderer );
  97. }
  98. }
  99. /**
  100. * Frees the GPU-related resources allocated by this instance. Call this
  101. * method whenever the pass is no longer used in your app.
  102. */
  103. dispose() {
  104. this.material.dispose();
  105. this._fsQuad.dispose();
  106. }
  107. }
  108. export { OutputPass };