OutputPass.js 2.6 KB

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