EffectComposer.js 6.1 KB

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