OutputPass.js 2.2 KB

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