EffectComposer.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. import {
  2. Clock,
  3. Vector2,
  4. WebGLRenderTarget
  5. } from 'three';
  6. import { CopyShader } from '../shaders/CopyShader.js';
  7. import { ShaderPass } from './ShaderPass.js';
  8. import { MaskPass } from './MaskPass.js';
  9. import { ClearMaskPass } from './MaskPass.js';
  10. class EffectComposer {
  11. constructor( renderer, renderTarget ) {
  12. this.renderer = renderer;
  13. this._pixelRatio = renderer.getPixelRatio();
  14. if ( renderTarget === undefined ) {
  15. const size = renderer.getSize( new Vector2() );
  16. this._width = size.width;
  17. this._height = size.height;
  18. renderTarget = new WebGLRenderTarget( this._width * this._pixelRatio, this._height * this._pixelRatio );
  19. renderTarget.texture.name = 'EffectComposer.rt1';
  20. } else {
  21. this._width = renderTarget.width;
  22. this._height = renderTarget.height;
  23. }
  24. this.renderTarget1 = renderTarget;
  25. this.renderTarget2 = renderTarget.clone();
  26. this.renderTarget2.texture.name = 'EffectComposer.rt2';
  27. this.writeBuffer = this.renderTarget1;
  28. this.readBuffer = this.renderTarget2;
  29. this.renderToScreen = true;
  30. this.passes = [];
  31. this.copyPass = new ShaderPass( CopyShader );
  32. this.clock = new 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 ( MaskPass !== undefined ) {
  86. if ( pass instanceof MaskPass ) {
  87. maskActive = true;
  88. } else if ( pass instanceof 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 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. export { EffectComposer };