OutputPass.js 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. import {
  2. ColorManagement,
  3. RawShaderMaterial,
  4. UniformsUtils,
  5. LinearToneMapping,
  6. ReinhardToneMapping,
  7. CineonToneMapping,
  8. AgXToneMapping,
  9. ACESFilmicToneMapping,
  10. SRGBTransfer
  11. } from 'three';
  12. import { Pass, FullScreenQuad } from './Pass.js';
  13. import { OutputShader } from '../shaders/OutputShader.js';
  14. class OutputPass extends Pass {
  15. constructor() {
  16. super();
  17. //
  18. const shader = OutputShader;
  19. this.uniforms = UniformsUtils.clone( shader.uniforms );
  20. this.material = new RawShaderMaterial( {
  21. name: shader.name,
  22. uniforms: this.uniforms,
  23. vertexShader: shader.vertexShader,
  24. fragmentShader: shader.fragmentShader
  25. } );
  26. this.fsQuad = new FullScreenQuad( this.material );
  27. // internal cache
  28. this._outputColorSpace = null;
  29. this._toneMapping = null;
  30. }
  31. render( renderer, writeBuffer, readBuffer/*, deltaTime, maskActive */ ) {
  32. this.uniforms[ 'tDiffuse' ].value = readBuffer.texture;
  33. this.uniforms[ 'toneMappingExposure' ].value = renderer.toneMappingExposure;
  34. // rebuild defines if required
  35. if ( this._outputColorSpace !== renderer.outputColorSpace || this._toneMapping !== renderer.toneMapping ) {
  36. this._outputColorSpace = renderer.outputColorSpace;
  37. this._toneMapping = renderer.toneMapping;
  38. this.material.defines = {};
  39. if ( ColorManagement.getTransfer( this._outputColorSpace ) === SRGBTransfer ) this.material.defines.SRGB_TRANSFER = '';
  40. if ( this._toneMapping === LinearToneMapping ) this.material.defines.LINEAR_TONE_MAPPING = '';
  41. else if ( this._toneMapping === ReinhardToneMapping ) this.material.defines.REINHARD_TONE_MAPPING = '';
  42. else if ( this._toneMapping === CineonToneMapping ) this.material.defines.CINEON_TONE_MAPPING = '';
  43. else if ( this._toneMapping === ACESFilmicToneMapping ) this.material.defines.ACES_FILMIC_TONE_MAPPING = '';
  44. else if ( this._toneMapping === AgXToneMapping ) this.material.defines.AGX_TONE_MAPPING = '';
  45. this.material.needsUpdate = true;
  46. }
  47. //
  48. if ( this.renderToScreen === true ) {
  49. renderer.setRenderTarget( null );
  50. this.fsQuad.render( renderer );
  51. } else {
  52. renderer.setRenderTarget( writeBuffer );
  53. if ( this.clear ) renderer.clear( renderer.autoClearColor, renderer.autoClearDepth, renderer.autoClearStencil );
  54. this.fsQuad.render( renderer );
  55. }
  56. }
  57. dispose() {
  58. this.material.dispose();
  59. this.fsQuad.dispose();
  60. }
  61. }
  62. export { OutputPass };