| 1 | import { |
| 2 | Clock, |
| 3 | HalfFloatType, |
| 4 | NoBlending, |
| 5 | Vector2, |
| 6 | WebGLRenderTarget |
| 7 | } from 'three'; |
| 8 | import { CopyShader } from '../shaders/CopyShader.js'; |
| 9 | import { ShaderPass } from './ShaderPass.js'; |
| 10 | import { MaskPass } from './MaskPass.js'; |
| 11 | import { ClearMaskPass } from './MaskPass.js'; |
| 12 | |
| 13 | class EffectComposer { |
| 14 | |
| 15 | constructor( renderer, renderTarget ) { |
| 16 | |
| 17 | this.renderer = renderer; |
| 18 | |
| 19 | this._pixelRatio = renderer.getPixelRatio(); |
| 20 | |
| 21 | if ( renderTarget === undefined ) { |
| 22 | |
| 23 | const size = renderer.getSize( new Vector2() ); |
| 24 | this._width = size.width; |
| 25 | this._height = size.height; |
| 26 | |
| 27 | renderTarget = new WebGLRenderTarget( this._width * this._pixelRatio, this._height * this._pixelRatio, { type: HalfFloatType } ); |
| 28 | renderTarget.texture.name = 'EffectComposer.rt1'; |
| 29 | |
| 30 | } else { |
| 31 | |
| 32 | this._width = renderTarget.width; |
| 33 | this._height = renderTarget.height; |
| 34 | |
| 35 | } |
| 36 | |
| 37 | this.renderTarget1 = renderTarget; |
| 38 | this.renderTarget2 = renderTarget.clone(); |
| 39 | this.renderTarget2.texture.name = 'EffectComposer.rt2'; |
| 40 | |
| 41 | this.writeBuffer = this.renderTarget1; |
| 42 | this.readBuffer = this.renderTarget2; |
| 43 | |
| 44 | this.renderToScreen = true; |
| 45 | |
| 46 | this.passes = []; |
| 47 | |
| 48 | this.copyPass = new ShaderPass( CopyShader ); |
| 49 | this.copyPass.material.blending = NoBlending; |
| 50 | |
| 51 | this.clock = new Clock(); |
| 52 | |
| 53 | } |
| 54 | |
| 55 | swapBuffers() { |
| 56 | |
| 57 | const tmp = this.readBuffer; |
| 58 | this.readBuffer = this.writeBuffer; |
| 59 | this.writeBuffer = tmp; |
| 60 | |
| 61 | } |
| 62 | |
| 63 | addPass( pass ) { |
| 64 | |
| 65 | this.passes.push( pass ); |
| 66 | pass.setSize( this._width * this._pixelRatio, this._height * this._pixelRatio ); |
| 67 | |
| 68 | } |
| 69 | |
| 70 | insertPass( pass, index ) { |
| 71 | |
| 72 | this.passes.splice( index, 0, pass ); |
| 73 | pass.setSize( this._width * this._pixelRatio, this._height * this._pixelRatio ); |
| 74 | |
| 75 | } |
| 76 | |
| 77 | removePass( pass ) { |
| 78 | |
| 79 | const index = this.passes.indexOf( pass ); |
| 80 | |
| 81 | if ( index !== - 1 ) { |
| 82 | |
| 83 | this.passes.splice( index, 1 ); |
| 84 | |
| 85 | } |
| 86 | |
| 87 | } |
| 88 | |
| 89 | isLastEnabledPass( passIndex ) { |
| 90 | |
| 91 | for ( let i = passIndex + 1; i < this.passes.length; i ++ ) { |
| 92 | |
| 93 | if ( this.passes[ i ].enabled ) { |
| 94 | |
| 95 | return false; |
| 96 | |
| 97 | } |
| 98 | |
| 99 | } |
| 100 | |
| 101 | return true; |
| 102 | |
| 103 | } |
| 104 | |
| 105 | render( deltaTime ) { |
| 106 | |
| 107 | // deltaTime value is in seconds |
| 108 | |
| 109 | if ( deltaTime === undefined ) { |
| 110 | |
| 111 | deltaTime = this.clock.getDelta(); |
| 112 | |
| 113 | } |
| 114 | |
| 115 | const currentRenderTarget = this.renderer.getRenderTarget(); |
| 116 | |
| 117 | let maskActive = false; |
| 118 | |
| 119 | for ( let i = 0, il = this.passes.length; i < il; i ++ ) { |
| 120 | |
| 121 | const pass = this.passes[ i ]; |
| 122 | |
| 123 | if ( pass.enabled === false ) continue; |
| 124 | |
| 125 | pass.renderToScreen = ( this.renderToScreen && this.isLastEnabledPass( i ) ); |
| 126 | pass.render( this.renderer, this.writeBuffer, this.readBuffer, deltaTime, maskActive ); |
| 127 | |
| 128 | if ( pass.needsSwap ) { |
| 129 | |
| 130 | if ( maskActive ) { |
| 131 | |
| 132 | const context = this.renderer.getContext(); |
| 133 | const stencil = this.renderer.state.buffers.stencil; |
| 134 | |
| 135 | //context.stencilFunc( context.NOTEQUAL, 1, 0xffffffff ); |
| 136 | stencil.setFunc( context.NOTEQUAL, 1, 0xffffffff ); |
| 137 | |
| 138 | this.copyPass.render( this.renderer, this.writeBuffer, this.readBuffer, deltaTime ); |
| 139 | |
| 140 | //context.stencilFunc( context.EQUAL, 1, 0xffffffff ); |
| 141 | stencil.setFunc( context.EQUAL, 1, 0xffffffff ); |
| 142 | |
| 143 | } |
| 144 | |
| 145 | this.swapBuffers(); |
| 146 | |
| 147 | } |
| 148 | |
| 149 | if ( MaskPass !== undefined ) { |
| 150 | |
| 151 | if ( pass instanceof MaskPass ) { |
| 152 | |
| 153 | maskActive = true; |
| 154 | |
| 155 | } else if ( pass instanceof ClearMaskPass ) { |
| 156 | |
| 157 | maskActive = false; |
| 158 | |
| 159 | } |
| 160 | |
| 161 | } |
| 162 | |
| 163 | } |
| 164 | |
| 165 | this.renderer.setRenderTarget( currentRenderTarget ); |
| 166 | |
| 167 | } |
| 168 | |
| 169 | reset( renderTarget ) { |
| 170 | |
| 171 | if ( renderTarget === undefined ) { |
| 172 | |
| 173 | const size = this.renderer.getSize( new Vector2() ); |
| 174 | this._pixelRatio = this.renderer.getPixelRatio(); |
| 175 | this._width = size.width; |
| 176 | this._height = size.height; |
| 177 | |
| 178 | renderTarget = this.renderTarget1.clone(); |
| 179 | renderTarget.setSize( this._width * this._pixelRatio, this._height * this._pixelRatio ); |
| 180 | |
| 181 | } |
| 182 | |
| 183 | this.renderTarget1.dispose(); |
| 184 | this.renderTarget2.dispose(); |
| 185 | this.renderTarget1 = renderTarget; |
| 186 | this.renderTarget2 = renderTarget.clone(); |
| 187 | |
| 188 | this.writeBuffer = this.renderTarget1; |
| 189 | this.readBuffer = this.renderTarget2; |
| 190 | |
| 191 | } |
| 192 | |
| 193 | setSize( width, height ) { |
| 194 | |
| 195 | this._width = width; |
| 196 | this._height = height; |
| 197 | |
| 198 | const effectiveWidth = this._width * this._pixelRatio; |
| 199 | const effectiveHeight = this._height * this._pixelRatio; |
| 200 | |
| 201 | this.renderTarget1.setSize( effectiveWidth, effectiveHeight ); |
| 202 | this.renderTarget2.setSize( effectiveWidth, effectiveHeight ); |
| 203 | |
| 204 | for ( let i = 0; i < this.passes.length; i ++ ) { |
| 205 | |
| 206 | this.passes[ i ].setSize( effectiveWidth, effectiveHeight ); |
| 207 | |
| 208 | } |
| 209 | |
| 210 | } |
| 211 | |
| 212 | setPixelRatio( pixelRatio ) { |
| 213 | |
| 214 | this._pixelRatio = pixelRatio; |
| 215 | |
| 216 | this.setSize( this._width, this._height ); |
| 217 | |
| 218 | } |
| 219 | |
| 220 | dispose() { |
| 221 | |
| 222 | this.renderTarget1.dispose(); |
| 223 | this.renderTarget2.dispose(); |
| 224 | |
| 225 | this.copyPass.dispose(); |
| 226 | |
| 227 | } |
| 228 | |
| 229 | } |
| 230 | |
| 231 | export { EffectComposer }; |