OutputPass.js 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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. name: shader.name,
  21. uniforms: this.uniforms,
  22. vertexShader: shader.vertexShader,
  23. fragmentShader: shader.fragmentShader
  24. } );
  25. this.fsQuad = new FullScreenQuad( this.material );
  26. // internal cache
  27. this._outputColorSpace = null;
  28. this._toneMapping = null;
  29. }
  30. render( renderer, writeBuffer, readBuffer/*, deltaTime, maskActive */ ) {
  31. this.uniforms[ 'tDiffuse' ].value = readBuffer.texture;
  32. this.uniforms[ 'toneMappingExposure' ].value = renderer.toneMappingExposure;
  33. // rebuild defines if required
  34. if ( this._outputColorSpace !== renderer.outputColorSpace || this._toneMapping !== renderer.toneMapping ) {
  35. this._outputColorSpace = renderer.outputColorSpace;
  36. this._toneMapping = renderer.toneMapping;
  37. this.material.defines = {};
  38. if ( ColorManagement.getTransfer( this._outputColorSpace ) === SRGBTransfer ) this.material.defines.SRGB_TRANSFER = '';
  39. if ( this._toneMapping === LinearToneMapping ) this.material.defines.LINEAR_TONE_MAPPING = '';
  40. else if ( this._toneMapping === ReinhardToneMapping ) this.material.defines.REINHARD_TONE_MAPPING = '';
  41. else if ( this._toneMapping === CineonToneMapping ) this.material.defines.CINEON_TONE_MAPPING = '';
  42. else if ( this._toneMapping === ACESFilmicToneMapping ) this.material.defines.ACES_FILMIC_TONE_MAPPING = '';
  43. this.material.needsUpdate = true;
  44. }
  45. //
  46. if ( this.renderToScreen === true ) {
  47. renderer.setRenderTarget( null );
  48. this.fsQuad.render( renderer );
  49. } else {
  50. renderer.setRenderTarget( writeBuffer );
  51. if ( this.clear ) renderer.clear( renderer.autoClearColor, renderer.autoClearDepth, renderer.autoClearStencil );
  52. this.fsQuad.render( renderer );
  53. }
  54. }
  55. dispose() {
  56. this.material.dispose();
  57. this.fsQuad.dispose();
  58. }
  59. }
  60. export { OutputPass };