1
0

OutputPass.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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. */
  31. class OutputPass extends Pass {
  32. /**
  33. * Constructs a new output pass.
  34. */
  35. constructor() {
  36. super();
  37. /**
  38. * The pass uniforms.
  39. *
  40. * @type {Object}
  41. */
  42. this.uniforms = UniformsUtils.clone( OutputShader.uniforms );
  43. /**
  44. * The pass material.
  45. *
  46. * @type {RawShaderMaterial}
  47. */
  48. this.material = new RawShaderMaterial( {
  49. name: OutputShader.name,
  50. uniforms: this.uniforms,
  51. vertexShader: OutputShader.vertexShader,
  52. fragmentShader: OutputShader.fragmentShader
  53. } );
  54. // internals
  55. this._fsQuad = new FullScreenQuad( this.material );
  56. this._outputColorSpace = null;
  57. this._toneMapping = null;
  58. }
  59. /**
  60. * Performs the output pass.
  61. *
  62. * @param {WebGLRenderer} renderer - The renderer.
  63. * @param {WebGLRenderTarget} writeBuffer - The write buffer. This buffer is intended as the rendering
  64. * destination for the pass.
  65. * @param {WebGLRenderTarget} readBuffer - The read buffer. The pass can access the result from the
  66. * previous pass from this buffer.
  67. * @param {number} deltaTime - The delta time in seconds.
  68. * @param {boolean} maskActive - Whether masking is active or not.
  69. */
  70. render( renderer, writeBuffer, readBuffer/*, deltaTime, maskActive */ ) {
  71. this.uniforms[ 'tDiffuse' ].value = readBuffer.texture;
  72. this.uniforms[ 'toneMappingExposure' ].value = renderer.toneMappingExposure;
  73. // rebuild defines if required
  74. if ( this._outputColorSpace !== renderer.outputColorSpace || this._toneMapping !== renderer.toneMapping ) {
  75. this._outputColorSpace = renderer.outputColorSpace;
  76. this._toneMapping = renderer.toneMapping;
  77. this.material.defines = {};
  78. if ( ColorManagement.getTransfer( this._outputColorSpace ) === SRGBTransfer ) this.material.defines.SRGB_TRANSFER = '';
  79. if ( this._toneMapping === LinearToneMapping ) this.material.defines.LINEAR_TONE_MAPPING = '';
  80. else if ( this._toneMapping === ReinhardToneMapping ) this.material.defines.REINHARD_TONE_MAPPING = '';
  81. else if ( this._toneMapping === CineonToneMapping ) this.material.defines.CINEON_TONE_MAPPING = '';
  82. else if ( this._toneMapping === ACESFilmicToneMapping ) this.material.defines.ACES_FILMIC_TONE_MAPPING = '';
  83. else if ( this._toneMapping === AgXToneMapping ) this.material.defines.AGX_TONE_MAPPING = '';
  84. else if ( this._toneMapping === NeutralToneMapping ) this.material.defines.NEUTRAL_TONE_MAPPING = '';
  85. else if ( this._toneMapping === CustomToneMapping ) this.material.defines.CUSTOM_TONE_MAPPING = '';
  86. this.material.needsUpdate = true;
  87. }
  88. //
  89. if ( this.renderToScreen === true ) {
  90. renderer.setRenderTarget( null );
  91. this._fsQuad.render( renderer );
  92. } else {
  93. renderer.setRenderTarget( writeBuffer );
  94. if ( this.clear ) renderer.clear( renderer.autoClearColor, renderer.autoClearDepth, renderer.autoClearStencil );
  95. this._fsQuad.render( renderer );
  96. }
  97. }
  98. /**
  99. * Frees the GPU-related resources allocated by this instance. Call this
  100. * method whenever the pass is no longer used in your app.
  101. */
  102. dispose() {
  103. this.material.dispose();
  104. this._fsQuad.dispose();
  105. }
  106. }
  107. export { OutputPass };
粤ICP备19079148号