EffectComposer.js 6.1 KB

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