EffectComposer.js 6.2 KB

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