EffectComposer.js 6.4 KB

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