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