EffectComposer.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. import {
  2. Clock,
  3. HalfFloatType,
  4. NoBlending,
  5. Vector2,
  6. WebGLRenderTarget
  7. } from 'three';
  8. import { CopyShader } from '../shaders/CopyShader.js';
  9. import { ShaderPass } from './ShaderPass.js';
  10. import { MaskPass } from './MaskPass.js';
  11. import { ClearMaskPass } from './MaskPass.js';
  12. class EffectComposer {
  13. constructor( renderer, renderTarget ) {
  14. this.renderer = renderer;
  15. this._pixelRatio = renderer.getPixelRatio();
  16. if ( renderTarget === undefined ) {
  17. const size = renderer.getSize( new Vector2() );
  18. this._width = size.width;
  19. this._height = size.height;
  20. renderTarget = new WebGLRenderTarget( this._width * this._pixelRatio, this._height * this._pixelRatio, { type: HalfFloatType } );
  21. renderTarget.texture.name = 'EffectComposer.rt1';
  22. } else {
  23. this._width = renderTarget.width;
  24. this._height = renderTarget.height;
  25. }
  26. this.renderTarget1 = renderTarget;
  27. this.renderTarget2 = renderTarget.clone();
  28. this.renderTarget2.texture.name = 'EffectComposer.rt2';
  29. this.writeBuffer = this.renderTarget1;
  30. this.readBuffer = this.renderTarget2;
  31. this.renderToScreen = true;
  32. this.passes = [];
  33. this.copyPass = new ShaderPass( CopyShader );
  34. this.copyPass.material.blending = NoBlending;
  35. this.clock = new Clock();
  36. }
  37. swapBuffers() {
  38. const tmp = this.readBuffer;
  39. this.readBuffer = this.writeBuffer;
  40. this.writeBuffer = tmp;
  41. }
  42. addPass( pass ) {
  43. this.passes.push( pass );
  44. pass.setSize( this._width * this._pixelRatio, this._height * this._pixelRatio );
  45. }
  46. insertPass( pass, index ) {
  47. this.passes.splice( index, 0, pass );
  48. pass.setSize( this._width * this._pixelRatio, this._height * this._pixelRatio );
  49. }
  50. removePass( pass ) {
  51. const index = this.passes.indexOf( pass );
  52. if ( index !== - 1 ) {
  53. this.passes.splice( index, 1 );
  54. }
  55. }
  56. isLastEnabledPass( passIndex ) {
  57. for ( let i = passIndex + 1; i < this.passes.length; i ++ ) {
  58. if ( this.passes[ i ].enabled ) {
  59. return false;
  60. }
  61. }
  62. return true;
  63. }
  64. render( deltaTime ) {
  65. // deltaTime value is in seconds
  66. if ( deltaTime === undefined ) {
  67. deltaTime = this.clock.getDelta();
  68. }
  69. const currentRenderTarget = this.renderer.getRenderTarget();
  70. let maskActive = false;
  71. for ( let i = 0, il = this.passes.length; i < il; i ++ ) {
  72. const pass = this.passes[ i ];
  73. if ( pass.enabled === false ) continue;
  74. pass.renderToScreen = ( this.renderToScreen && this.isLastEnabledPass( i ) );
  75. pass.render( this.renderer, this.writeBuffer, this.readBuffer, deltaTime, maskActive );
  76. if ( pass.needsSwap ) {
  77. if ( maskActive ) {
  78. const context = this.renderer.getContext();
  79. const stencil = this.renderer.state.buffers.stencil;
  80. //context.stencilFunc( context.NOTEQUAL, 1, 0xffffffff );
  81. stencil.setFunc( context.NOTEQUAL, 1, 0xffffffff );
  82. this.copyPass.render( this.renderer, this.writeBuffer, this.readBuffer, deltaTime );
  83. //context.stencilFunc( context.EQUAL, 1, 0xffffffff );
  84. stencil.setFunc( context.EQUAL, 1, 0xffffffff );
  85. }
  86. this.swapBuffers();
  87. }
  88. if ( MaskPass !== undefined ) {
  89. if ( pass instanceof MaskPass ) {
  90. maskActive = true;
  91. } else if ( pass instanceof ClearMaskPass ) {
  92. maskActive = false;
  93. }
  94. }
  95. }
  96. this.renderer.setRenderTarget( currentRenderTarget );
  97. }
  98. reset( renderTarget ) {
  99. if ( renderTarget === undefined ) {
  100. const size = this.renderer.getSize( new Vector2() );
  101. this._pixelRatio = this.renderer.getPixelRatio();
  102. this._width = size.width;
  103. this._height = size.height;
  104. renderTarget = this.renderTarget1.clone();
  105. renderTarget.setSize( this._width * this._pixelRatio, this._height * this._pixelRatio );
  106. }
  107. this.renderTarget1.dispose();
  108. this.renderTarget2.dispose();
  109. this.renderTarget1 = renderTarget;
  110. this.renderTarget2 = renderTarget.clone();
  111. this.writeBuffer = this.renderTarget1;
  112. this.readBuffer = this.renderTarget2;
  113. }
  114. setSize( width, height ) {
  115. this._width = width;
  116. this._height = height;
  117. const effectiveWidth = this._width * this._pixelRatio;
  118. const effectiveHeight = this._height * this._pixelRatio;
  119. this.renderTarget1.setSize( effectiveWidth, effectiveHeight );
  120. this.renderTarget2.setSize( effectiveWidth, effectiveHeight );
  121. for ( let i = 0; i < this.passes.length; i ++ ) {
  122. this.passes[ i ].setSize( effectiveWidth, effectiveHeight );
  123. }
  124. }
  125. setPixelRatio( pixelRatio ) {
  126. this._pixelRatio = pixelRatio;
  127. this.setSize( this._width, this._height );
  128. }
  129. dispose() {
  130. this.renderTarget1.dispose();
  131. this.renderTarget2.dispose();
  132. this.copyPass.dispose();
  133. }
  134. }
  135. export { EffectComposer };