OutputPass.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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. /**
  17. * This pass is responsible for including tone mapping and color space conversion
  18. * into your pass chain. In most cases, this pass should be included at the end
  19. * of each pass chain. If a pass requires sRGB input (e.g. like FXAA), the pass
  20. * must follow `OutputPass` in the pass chain.
  21. *
  22. * The tone mapping and color space settings are extracted from the renderer.
  23. *
  24. * ```js
  25. * const outputPass = new OutputPass();
  26. * composer.addPass( outputPass );
  27. * ```
  28. *
  29. * @augments Pass
  30. * @three_import import { OutputPass } from 'three/addons/postprocessing/OutputPass.js';
  31. */
  32. class OutputPass extends Pass {
  33. /**
  34. * Constructs a new output pass.
  35. */
  36. constructor() {
  37. super();
  38. /**
  39. * This flag indicates that this is an output pass.
  40. *
  41. * @type {boolean}
  42. * @readonly
  43. * @default true
  44. */
  45. this.isOutputPass = true;
  46. /**
  47. * The pass uniforms.
  48. *
  49. * @type {Object}
  50. */
  51. this.uniforms = UniformsUtils.clone( OutputShader.uniforms );
  52. /**
  53. * The pass material.
  54. *
  55. * @type {RawShaderMaterial}
  56. */
  57. this.material = new RawShaderMaterial( {
  58. name: OutputShader.name,
  59. uniforms: this.uniforms,
  60. vertexShader: OutputShader.vertexShader,
  61. fragmentShader: OutputShader.fragmentShader
  62. } );
  63. // internals
  64. this._fsQuad = new FullScreenQuad( this.material );
  65. this._outputColorSpace = null;
  66. this._toneMapping = null;
  67. }
  68. /**
  69. * Performs the output pass.
  70. *
  71. * @param {WebGLRenderer} renderer - The renderer.
  72. * @param {WebGLRenderTarget} writeBuffer - The write buffer. This buffer is intended as the rendering
  73. * destination for the pass.
  74. * @param {WebGLRenderTarget} readBuffer - The read buffer. The pass can access the result from the
  75. * previous pass from this buffer.
  76. * @param {number} deltaTime - The delta time in seconds.
  77. * @param {boolean} maskActive - Whether masking is active or not.
  78. */
  79. render( renderer, writeBuffer, readBuffer/*, deltaTime, maskActive */ ) {
  80. this.uniforms[ 'tDiffuse' ].value = readBuffer.texture;
  81. this.uniforms[ 'toneMappingExposure' ].value = renderer.toneMappingExposure;
  82. // rebuild defines if required
  83. if ( this._outputColorSpace !== renderer.outputColorSpace || this._toneMapping !== renderer.toneMapping ) {
  84. this._outputColorSpace = renderer.outputColorSpace;
  85. this._toneMapping = renderer.toneMapping;
  86. this.material.defines = {};
  87. if ( ColorManagement.getTransfer( this._outputColorSpace ) === SRGBTransfer ) this.material.defines.SRGB_TRANSFER = '';
  88. if ( this._toneMapping === LinearToneMapping ) this.material.defines.LINEAR_TONE_MAPPING = '';
  89. else if ( this._toneMapping === ReinhardToneMapping ) this.material.defines.REINHARD_TONE_MAPPING = '';
  90. else if ( this._toneMapping === CineonToneMapping ) this.material.defines.CINEON_TONE_MAPPING = '';
  91. else if ( this._toneMapping === ACESFilmicToneMapping ) this.material.defines.ACES_FILMIC_TONE_MAPPING = '';
  92. else if ( this._toneMapping === AgXToneMapping ) this.material.defines.AGX_TONE_MAPPING = '';
  93. else if ( this._toneMapping === NeutralToneMapping ) this.material.defines.NEUTRAL_TONE_MAPPING = '';
  94. else if ( this._toneMapping === CustomToneMapping ) this.material.defines.CUSTOM_TONE_MAPPING = '';
  95. this.material.needsUpdate = true;
  96. }
  97. //
  98. if ( this.renderToScreen === true ) {
  99. renderer.setRenderTarget( null );
  100. this._fsQuad.render( renderer );
  101. } else {
  102. renderer.setRenderTarget( writeBuffer );
  103. if ( this.clear ) renderer.clear( renderer.autoClearColor, renderer.autoClearDepth, renderer.autoClearStencil );
  104. this._fsQuad.render( renderer );
  105. }
  106. }
  107. /**
  108. * Frees the GPU-related resources allocated by this instance. Call this
  109. * method whenever the pass is no longer used in your app.
  110. */
  111. dispose() {
  112. this.material.dispose();
  113. this._fsQuad.dispose();
  114. }
  115. }
  116. export { OutputPass };
粤ICP备19079148号