EffectComposer.js 6.0 KB

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