EffectComposer.js 5.2 KB

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