EffectComposer.js 6.3 KB

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