EffectComposer.js 6.6 KB

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