| 1 | import { |
| 2 | Color |
| 3 | } from 'three'; |
| 4 | import { Pass } from './Pass.js'; |
| 5 | |
| 6 | class RenderPass extends Pass { |
| 7 | |
| 8 | constructor( scene, camera, overrideMaterial = null, clearColor = null, clearAlpha = null ) { |
| 9 | |
| 10 | super(); |
| 11 | |
| 12 | this.scene = scene; |
| 13 | this.camera = camera; |
| 14 | |
| 15 | this.overrideMaterial = overrideMaterial; |
| 16 | |
| 17 | this.clearColor = clearColor; |
| 18 | this.clearAlpha = clearAlpha; |
| 19 | |
| 20 | this.clear = true; |
| 21 | this.clearDepth = false; |
| 22 | this.needsSwap = false; |
| 23 | this._oldClearColor = new Color(); |
| 24 | |
| 25 | } |
| 26 | |
| 27 | render( renderer, writeBuffer, readBuffer /*, deltaTime, maskActive */ ) { |
| 28 | |
| 29 | const oldAutoClear = renderer.autoClear; |
| 30 | renderer.autoClear = false; |
| 31 | |
| 32 | let oldClearAlpha, oldOverrideMaterial; |
| 33 | |
| 34 | if ( this.overrideMaterial !== null ) { |
| 35 | |
| 36 | oldOverrideMaterial = this.scene.overrideMaterial; |
| 37 | |
| 38 | this.scene.overrideMaterial = this.overrideMaterial; |
| 39 | |
| 40 | } |
| 41 | |
| 42 | if ( this.clearColor !== null ) { |
| 43 | |
| 44 | renderer.getClearColor( this._oldClearColor ); |
| 45 | renderer.setClearColor( this.clearColor ); |
| 46 | |
| 47 | } |
| 48 | |
| 49 | if ( this.clearAlpha !== null ) { |
| 50 | |
| 51 | oldClearAlpha = renderer.getClearAlpha(); |
| 52 | renderer.setClearAlpha( this.clearAlpha ); |
| 53 | |
| 54 | } |
| 55 | |
| 56 | if ( this.clearDepth == true ) { |
| 57 | |
| 58 | renderer.clearDepth(); |
| 59 | |
| 60 | } |
| 61 | |
| 62 | renderer.setRenderTarget( this.renderToScreen ? null : readBuffer ); |
| 63 | |
| 64 | if ( this.clear === true ) { |
| 65 | |
| 66 | // TODO: Avoid using autoClear properties, see https://github.com/mrdoob/three.js/pull/15571#issuecomment-465669600 |
| 67 | renderer.clear( renderer.autoClearColor, renderer.autoClearDepth, renderer.autoClearStencil ); |
| 68 | |
| 69 | } |
| 70 | |
| 71 | renderer.render( this.scene, this.camera ); |
| 72 | |
| 73 | // restore |
| 74 | |
| 75 | if ( this.clearColor !== null ) { |
| 76 | |
| 77 | renderer.setClearColor( this._oldClearColor ); |
| 78 | |
| 79 | } |
| 80 | |
| 81 | if ( this.clearAlpha !== null ) { |
| 82 | |
| 83 | renderer.setClearAlpha( oldClearAlpha ); |
| 84 | |
| 85 | } |
| 86 | |
| 87 | if ( this.overrideMaterial !== null ) { |
| 88 | |
| 89 | this.scene.overrideMaterial = oldOverrideMaterial; |
| 90 | |
| 91 | } |
| 92 | |
| 93 | renderer.autoClear = oldAutoClear; |
| 94 | |
| 95 | } |
| 96 | |
| 97 | } |
| 98 | |
| 99 | export { RenderPass }; |