OutputPass.js 2.5 KB

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