EffectComposer.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  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 { ClearMaskPass, MaskPass } 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.copyPass.material.blending = NoBlending;
  34. this.clock = new Clock();
  35. }
  36. swapBuffers() {
  37. const tmp = this.readBuffer;
  38. this.readBuffer = this.writeBuffer;
  39. this.writeBuffer = tmp;
  40. }
  41. addPass( pass ) {
  42. this.passes.push( pass );
  43. pass.setSize( this._width * this._pixelRatio, this._height * this._pixelRatio );
  44. }
  45. insertPass( pass, index ) {
  46. this.passes.splice( index, 0, pass );
  47. pass.setSize( this._width * this._pixelRatio, this._height * this._pixelRatio );
  48. }
  49. removePass( pass ) {
  50. const index = this.passes.indexOf( pass );
  51. if ( index !== - 1 ) {
  52. this.passes.splice( index, 1 );
  53. }
  54. }
  55. isLastEnabledPass( passIndex ) {
  56. for ( let i = passIndex + 1; i < this.passes.length; i ++ ) {
  57. if ( this.passes[ i ].enabled ) {
  58. return false;
  59. }
  60. }
  61. return true;
  62. }
  63. render( deltaTime ) {
  64. // deltaTime value is in seconds
  65. if ( deltaTime === undefined ) {
  66. deltaTime = this.clock.getDelta();
  67. }
  68. const currentRenderTarget = this.renderer.getRenderTarget();
  69. let maskActive = false;
  70. for ( let i = 0, il = this.passes.length; i < il; i ++ ) {
  71. const pass = this.passes[ i ];
  72. if ( pass.enabled === false ) continue;
  73. pass.renderToScreen = ( this.renderToScreen && this.isLastEnabledPass( i ) );
  74. pass.render( this.renderer, this.writeBuffer, this.readBuffer, deltaTime, maskActive );
  75. if ( pass.needsSwap ) {
  76. if ( maskActive ) {
  77. const context = this.renderer.getContext();
  78. const stencil = this.renderer.state.buffers.stencil;
  79. //context.stencilFunc( context.NOTEQUAL, 1, 0xffffffff );
  80. stencil.setFunc( context.NOTEQUAL, 1, 0xffffffff );
  81. this.copyPass.render( this.renderer, this.writeBuffer, this.readBuffer, deltaTime );
  82. //context.stencilFunc( context.EQUAL, 1, 0xffffffff );
  83. stencil.setFunc( context.EQUAL, 1, 0xffffffff );
  84. }
  85. this.swapBuffers();
  86. }
  87. if ( MaskPass !== undefined ) {
  88. if ( pass instanceof MaskPass ) {
  89. maskActive = true;
  90. } else if ( pass instanceof ClearMaskPass ) {
  91. maskActive = false;
  92. }
  93. }
  94. }
  95. this.renderer.setRenderTarget( currentRenderTarget );
  96. }
  97. reset( renderTarget ) {
  98. if ( renderTarget === undefined ) {
  99. const size = this.renderer.getSize( new Vector2() );
  100. this._pixelRatio = this.renderer.getPixelRatio();
  101. this._width = size.width;
  102. this._height = size.height;
  103. renderTarget = this.renderTarget1.clone();
  104. renderTarget.setSize( this._width * this._pixelRatio, this._height * this._pixelRatio );
  105. }
  106. this.renderTarget1.dispose();
  107. this.renderTarget2.dispose();
  108. this.renderTarget1 = renderTarget;
  109. this.renderTarget2 = renderTarget.clone();
  110. this.writeBuffer = this.renderTarget1;
  111. this.readBuffer = this.renderTarget2;
  112. }
  113. setSize( width, height ) {
  114. this._width = width;
  115. this._height = height;
  116. const effectiveWidth = this._width * this._pixelRatio;
  117. const effectiveHeight = this._height * this._pixelRatio;
  118. this.renderTarget1.setSize( effectiveWidth, effectiveHeight );
  119. this.renderTarget2.setSize( effectiveWidth, effectiveHeight );
  120. for ( let i = 0; i < this.passes.length; i ++ ) {
  121. this.passes[ i ].setSize( effectiveWidth, effectiveHeight );
  122. }
  123. }
  124. setPixelRatio( pixelRatio ) {
  125. this._pixelRatio = pixelRatio;
  126. this.setSize( this._width, this._height );
  127. }
  128. dispose() {
  129. this.renderTarget1.dispose();
  130. this.renderTarget2.dispose();
  131. this.copyPass.dispose();
  132. }
  133. }
  134. export { EffectComposer };
粤ICP备19079148号