EffectComposer.js 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  1. console.warn( "THREE.EffectComposer: As part of the transition to ES6 Modules, the files in 'examples/js' were deprecated in May 2020 (r117) and will be deleted in December 2020 (r124). You can find more information about developing using ES6 Modules in https://threejs.org/docs/#manual/en/introduction/Installation." );
  2. THREE.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 = [];
  28. // 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. Object.assign( THREE.EffectComposer.prototype, {
  39. swapBuffers: function () {
  40. var tmp = this.readBuffer;
  41. this.readBuffer = this.writeBuffer;
  42. this.writeBuffer = tmp;
  43. },
  44. addPass: function ( pass ) {
  45. this.passes.push( pass );
  46. pass.setSize( this._width * this._pixelRatio, this._height * this._pixelRatio );
  47. },
  48. insertPass: function ( pass, index ) {
  49. this.passes.splice( index, 0, pass );
  50. pass.setSize( this._width * this._pixelRatio, this._height * this._pixelRatio );
  51. },
  52. isLastEnabledPass: function ( passIndex ) {
  53. for ( var i = passIndex + 1; i < this.passes.length; i ++ ) {
  54. if ( this.passes[ i ].enabled ) {
  55. return false;
  56. }
  57. }
  58. return true;
  59. },
  60. render: function ( deltaTime ) {
  61. // deltaTime value is in seconds
  62. if ( deltaTime === undefined ) {
  63. deltaTime = this.clock.getDelta();
  64. }
  65. var currentRenderTarget = this.renderer.getRenderTarget();
  66. var maskActive = false;
  67. var pass, i, il = this.passes.length;
  68. for ( i = 0; i < il; i ++ ) {
  69. pass = this.passes[ i ];
  70. if ( pass.enabled === false ) continue;
  71. pass.renderToScreen = ( this.renderToScreen && this.isLastEnabledPass( i ) );
  72. pass.render( this.renderer, this.writeBuffer, this.readBuffer, deltaTime, maskActive );
  73. if ( pass.needsSwap ) {
  74. if ( maskActive ) {
  75. var context = this.renderer.getContext();
  76. var stencil = this.renderer.state.buffers.stencil;
  77. //context.stencilFunc( context.NOTEQUAL, 1, 0xffffffff );
  78. stencil.setFunc( context.NOTEQUAL, 1, 0xffffffff );
  79. this.copyPass.render( this.renderer, this.writeBuffer, this.readBuffer, deltaTime );
  80. //context.stencilFunc( context.EQUAL, 1, 0xffffffff );
  81. stencil.setFunc( context.EQUAL, 1, 0xffffffff );
  82. }
  83. this.swapBuffers();
  84. }
  85. if ( THREE.MaskPass !== undefined ) {
  86. if ( pass instanceof THREE.MaskPass ) {
  87. maskActive = true;
  88. } else if ( pass instanceof THREE.ClearMaskPass ) {
  89. maskActive = false;
  90. }
  91. }
  92. }
  93. this.renderer.setRenderTarget( currentRenderTarget );
  94. },
  95. reset: function ( renderTarget ) {
  96. if ( renderTarget === undefined ) {
  97. var size = this.renderer.getSize( new THREE.Vector2() );
  98. this._pixelRatio = this.renderer.getPixelRatio();
  99. this._width = size.width;
  100. this._height = size.height;
  101. renderTarget = this.renderTarget1.clone();
  102. renderTarget.setSize( this._width * this._pixelRatio, this._height * this._pixelRatio );
  103. }
  104. this.renderTarget1.dispose();
  105. this.renderTarget2.dispose();
  106. this.renderTarget1 = renderTarget;
  107. this.renderTarget2 = renderTarget.clone();
  108. this.writeBuffer = this.renderTarget1;
  109. this.readBuffer = this.renderTarget2;
  110. },
  111. setSize: function ( width, height ) {
  112. this._width = width;
  113. this._height = height;
  114. var effectiveWidth = this._width * this._pixelRatio;
  115. var effectiveHeight = this._height * this._pixelRatio;
  116. this.renderTarget1.setSize( effectiveWidth, effectiveHeight );
  117. this.renderTarget2.setSize( effectiveWidth, effectiveHeight );
  118. for ( var i = 0; i < this.passes.length; i ++ ) {
  119. this.passes[ i ].setSize( effectiveWidth, effectiveHeight );
  120. }
  121. },
  122. setPixelRatio: function ( pixelRatio ) {
  123. this._pixelRatio = pixelRatio;
  124. this.setSize( this._width, this._height );
  125. }
  126. } );
  127. THREE.Pass = function () {
  128. // if set to true, the pass is processed by the composer
  129. this.enabled = true;
  130. // if set to true, the pass indicates to swap read and write buffer after rendering
  131. this.needsSwap = true;
  132. // if set to true, the pass clears its buffer before rendering
  133. this.clear = false;
  134. // 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. Object.assign( THREE.Pass.prototype, {
  138. setSize: function ( /* width, height */ ) {},
  139. render: function ( /* renderer, writeBuffer, readBuffer, deltaTime, maskActive */ ) {
  140. console.error( 'THREE.Pass: .render() must be implemented in derived pass.' );
  141. }
  142. } );
  143. // Helper for passes that need to fill the viewport with a single quad.
  144. THREE.Pass.FullScreenQuad = ( function () {
  145. var camera = new THREE.OrthographicCamera( - 1, 1, 1, - 1, 0, 1 );
  146. var geometry = new THREE.PlaneBufferGeometry( 2, 2 );
  147. var FullScreenQuad = function ( material ) {
  148. this._mesh = new THREE.Mesh( geometry, material );
  149. };
  150. Object.defineProperty( FullScreenQuad.prototype, 'material', {
  151. get: function () {
  152. return this._mesh.material;
  153. },
  154. set: function ( value ) {
  155. this._mesh.material = value;
  156. }
  157. } );
  158. Object.assign( FullScreenQuad.prototype, {
  159. dispose: function () {
  160. this._mesh.geometry.dispose();
  161. },
  162. render: function ( renderer ) {
  163. renderer.render( this._mesh, camera );
  164. }
  165. } );
  166. return FullScreenQuad;
  167. } )();