EffectComposer.js 6.0 KB

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