EffectComposer.js 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. ( function () {
  2. class EffectComposer {
  3. constructor( renderer, renderTarget ) {
  4. this.renderer = renderer;
  5. if ( renderTarget === undefined ) {
  6. const parameters = {
  7. minFilter: THREE.LinearFilter,
  8. magFilter: THREE.LinearFilter,
  9. format: THREE.RGBAFormat
  10. };
  11. const size = renderer.getSize( new THREE.Vector2() );
  12. this._pixelRatio = renderer.getPixelRatio();
  13. this._width = size.width;
  14. this._height = size.height;
  15. renderTarget = new THREE.WebGLRenderTarget( this._width * this._pixelRatio, this._height * this._pixelRatio, parameters );
  16. renderTarget.texture.name = 'EffectComposer.rt1';
  17. } else {
  18. this._pixelRatio = 1;
  19. this._width = renderTarget.width;
  20. this._height = renderTarget.height;
  21. }
  22. this.renderTarget1 = renderTarget;
  23. this.renderTarget2 = renderTarget.clone();
  24. this.renderTarget2.texture.name = 'EffectComposer.rt2';
  25. this.writeBuffer = this.renderTarget1;
  26. this.readBuffer = this.renderTarget2;
  27. this.renderToScreen = true;
  28. this.passes = []; // dependencies
  29. if ( THREE.CopyShader === undefined ) {
  30. console.error( 'THREE.EffectComposer relies on THREE.CopyShader' );
  31. }
  32. if ( THREE.ShaderPass === undefined ) {
  33. console.error( 'THREE.EffectComposer relies on THREE.ShaderPass' );
  34. }
  35. this.copyPass = new THREE.ShaderPass( THREE.CopyShader );
  36. this.clock = new THREE.Clock();
  37. }
  38. swapBuffers() {
  39. const tmp = this.readBuffer;
  40. this.readBuffer = this.writeBuffer;
  41. this.writeBuffer = tmp;
  42. }
  43. addPass( pass ) {
  44. this.passes.push( pass );
  45. pass.setSize( this._width * this._pixelRatio, this._height * this._pixelRatio );
  46. }
  47. insertPass( pass, index ) {
  48. this.passes.splice( index, 0, pass );
  49. pass.setSize( this._width * this._pixelRatio, this._height * this._pixelRatio );
  50. }
  51. removePass( pass ) {
  52. const index = this.passes.indexOf( pass );
  53. if ( index !== - 1 ) {
  54. this.passes.splice( index, 1 );
  55. }
  56. }
  57. isLastEnabledPass( passIndex ) {
  58. for ( let i = passIndex + 1; i < this.passes.length; i ++ ) {
  59. if ( this.passes[ i ].enabled ) {
  60. return false;
  61. }
  62. }
  63. return true;
  64. }
  65. render( deltaTime ) {
  66. // deltaTime value is in seconds
  67. if ( deltaTime === undefined ) {
  68. deltaTime = this.clock.getDelta();
  69. }
  70. const currentRenderTarget = this.renderer.getRenderTarget();
  71. let maskActive = false;
  72. for ( let i = 0, il = this.passes.length; i < il; i ++ ) {
  73. const pass = this.passes[ i ];
  74. if ( pass.enabled === false ) continue;
  75. pass.renderToScreen = this.renderToScreen && this.isLastEnabledPass( i );
  76. pass.render( this.renderer, this.writeBuffer, this.readBuffer, deltaTime, maskActive );
  77. if ( pass.needsSwap ) {
  78. if ( maskActive ) {
  79. const context = this.renderer.getContext();
  80. const stencil = this.renderer.state.buffers.stencil; //context.stencilFunc( context.NOTEQUAL, 1, 0xffffffff );
  81. stencil.setFunc( context.NOTEQUAL, 1, 0xffffffff );
  82. this.copyPass.render( this.renderer, this.writeBuffer, this.readBuffer, deltaTime ); //context.stencilFunc( context.EQUAL, 1, 0xffffffff );
  83. stencil.setFunc( context.EQUAL, 1, 0xffffffff );
  84. }
  85. this.swapBuffers();
  86. }
  87. if ( THREE.MaskPass !== undefined ) {
  88. if ( pass instanceof THREE.MaskPass ) {
  89. maskActive = true;
  90. } else if ( pass instanceof THREE.ClearMaskPass ) {
  91. maskActive = false;
  92. }
  93. }
  94. }
  95. this.renderer.setRenderTarget( currentRenderTarget );
  96. }
  97. reset( renderTarget ) {
  98. if ( renderTarget === undefined ) {
  99. const size = this.renderer.getSize( new THREE.Vector2() );
  100. this._pixelRatio = this.renderer.getPixelRatio();
  101. this._width = size.width;
  102. this._height = size.height;
  103. renderTarget = this.renderTarget1.clone();
  104. renderTarget.setSize( this._width * this._pixelRatio, this._height * this._pixelRatio );
  105. }
  106. this.renderTarget1.dispose();
  107. this.renderTarget2.dispose();
  108. this.renderTarget1 = renderTarget;
  109. this.renderTarget2 = renderTarget.clone();
  110. this.writeBuffer = this.renderTarget1;
  111. this.readBuffer = this.renderTarget2;
  112. }
  113. setSize( width, height ) {
  114. this._width = width;
  115. this._height = height;
  116. const effectiveWidth = this._width * this._pixelRatio;
  117. const effectiveHeight = this._height * this._pixelRatio;
  118. this.renderTarget1.setSize( effectiveWidth, effectiveHeight );
  119. this.renderTarget2.setSize( effectiveWidth, effectiveHeight );
  120. for ( let i = 0; i < this.passes.length; i ++ ) {
  121. this.passes[ i ].setSize( effectiveWidth, effectiveHeight );
  122. }
  123. }
  124. setPixelRatio( pixelRatio ) {
  125. this._pixelRatio = pixelRatio;
  126. this.setSize( this._width, this._height );
  127. }
  128. }
  129. class Pass {
  130. constructor() {
  131. // if set to true, the pass is processed by the composer
  132. this.enabled = true; // if set to true, the pass indicates to swap read and write buffer after rendering
  133. this.needsSwap = true; // if set to true, the pass clears its buffer before rendering
  134. this.clear = false; // if set to true, the result of the pass is rendered to screen. This is set automatically by EffectComposer.
  135. this.renderToScreen = false;
  136. }
  137. setSize() {}
  138. render() {
  139. console.error( 'THREE.Pass: .render() must be implemented in derived pass.' );
  140. }
  141. } // Helper for passes that need to fill the viewport with a single quad.
  142. const _camera = new THREE.OrthographicCamera( - 1, 1, 1, - 1, 0, 1 ); // https://github.com/mrdoob/three.js/pull/21358
  143. const _geometry = new THREE.BufferGeometry();
  144. _geometry.setAttribute( 'position', new THREE.Float32BufferAttribute( [ - 1, 3, 0, - 1, - 1, 0, 3, - 1, 0 ], 3 ) );
  145. _geometry.setAttribute( 'uv', new THREE.Float32BufferAttribute( [ 0, 2, 0, 0, 2, 0 ], 2 ) );
  146. class FullScreenQuad {
  147. constructor( material ) {
  148. this._mesh = new THREE.Mesh( _geometry, material );
  149. }
  150. dispose() {
  151. this._mesh.geometry.dispose();
  152. }
  153. render( renderer ) {
  154. renderer.render( this._mesh, _camera );
  155. }
  156. get material() {
  157. return this._mesh.material;
  158. }
  159. set material( value ) {
  160. this._mesh.material = value;
  161. }
  162. }
  163. THREE.EffectComposer = EffectComposer;
  164. THREE.FullScreenQuad = FullScreenQuad;
  165. THREE.Pass = Pass;
  166. } )();