2
0

EffectComposer.js 6.3 KB

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