EffectComposer.js 4.5 KB

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