EffectComposer.js 6.4 KB

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