2
0

EffectComposer.js 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  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. 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, i, il = this.passes.length;
  73. for ( i = 0; i < il; i ++ ) {
  74. pass = this.passes[ i ];
  75. if ( pass.enabled === false ) continue;
  76. pass.renderToScreen = ( this.renderToScreen && this.isLastEnabledPass( i ) );
  77. pass.render( this.renderer, this.writeBuffer, this.readBuffer, deltaTime, maskActive );
  78. if ( pass.needsSwap ) {
  79. if ( maskActive ) {
  80. var context = this.renderer.getContext();
  81. var stencil = this.renderer.state.buffers.stencil;
  82. //context.stencilFunc( context.NOTEQUAL, 1, 0xffffffff );
  83. stencil.setFunc( context.NOTEQUAL, 1, 0xffffffff );
  84. this.copyPass.render( this.renderer, this.writeBuffer, this.readBuffer, deltaTime );
  85. //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. THREE.Pass = function () {
  133. // if set to true, the pass is processed by the composer
  134. this.enabled = true;
  135. // if set to true, the pass indicates to swap read and write buffer after rendering
  136. this.needsSwap = true;
  137. // if set to true, the pass clears its buffer before rendering
  138. this.clear = false;
  139. // if set to true, the result of the pass is rendered to screen. This is set automatically by EffectComposer.
  140. this.renderToScreen = false;
  141. };
  142. Object.assign( THREE.Pass.prototype, {
  143. setSize: function ( /* width, height */ ) {},
  144. render: function ( /* renderer, writeBuffer, readBuffer, deltaTime, maskActive */ ) {
  145. console.error( 'THREE.Pass: .render() must be implemented in derived pass.' );
  146. }
  147. } );
  148. // Helper for passes that need to fill the viewport with a single quad.
  149. THREE.Pass.FullScreenQuad = ( function () {
  150. var camera = new THREE.OrthographicCamera( - 1, 1, 1, - 1, 0, 1 );
  151. var geometry = new THREE.PlaneGeometry( 2, 2 );
  152. var FullScreenQuad = function ( material ) {
  153. this._mesh = new THREE.Mesh( geometry, material );
  154. };
  155. Object.defineProperty( FullScreenQuad.prototype, 'material', {
  156. get: function () {
  157. return this._mesh.material;
  158. },
  159. set: function ( value ) {
  160. this._mesh.material = value;
  161. }
  162. } );
  163. Object.assign( FullScreenQuad.prototype, {
  164. dispose: function () {
  165. this._mesh.geometry.dispose();
  166. },
  167. render: function ( renderer ) {
  168. renderer.render( this._mesh, camera );
  169. }
  170. } );
  171. return FullScreenQuad;
  172. } )();