OutputPass.js 2.2 KB

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