EffectComposer.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. /**
  2. * @author alteredq / http://alteredqualia.com/
  3. */
  4. THREE.EffectComposer = function ( renderer, renderTarget ) {
  5. this.renderer = renderer;
  6. if ( renderTarget === undefined ) {
  7. var parameters = {
  8. minFilter: THREE.LinearFilter,
  9. magFilter: THREE.LinearFilter,
  10. format: THREE.RGBAFormat,
  11. stencilBuffer: false
  12. };
  13. var size = renderer.getDrawingBufferSize();
  14. renderTarget = new THREE.WebGLRenderTarget( size.width, size.height, parameters );
  15. renderTarget.texture.name = 'EffectComposer.rt1';
  16. }
  17. this.renderTarget1 = renderTarget;
  18. this.renderTarget2 = renderTarget.clone();
  19. this.renderTarget2.texture.name = 'EffectComposer.rt2';
  20. this.writeBuffer = this.renderTarget1;
  21. this.readBuffer = this.renderTarget2;
  22. this.passes = [];
  23. // dependencies
  24. if ( THREE.CopyShader === undefined ) {
  25. console.error( 'THREE.EffectComposer relies on THREE.CopyShader' );
  26. }
  27. if ( THREE.ShaderPass === undefined ) {
  28. console.error( 'THREE.EffectComposer relies on THREE.ShaderPass' );
  29. }
  30. this.copyPass = new THREE.ShaderPass( THREE.CopyShader );
  31. };
  32. Object.assign( THREE.EffectComposer.prototype, {
  33. swapBuffers: function () {
  34. var tmp = this.readBuffer;
  35. this.readBuffer = this.writeBuffer;
  36. this.writeBuffer = tmp;
  37. },
  38. addPass: function ( pass ) {
  39. this.passes.push( pass );
  40. var size = this.renderer.getDrawingBufferSize();
  41. pass.setSize( size.width, size.height );
  42. },
  43. insertPass: function ( pass, index ) {
  44. this.passes.splice( index, 0, pass );
  45. },
  46. render: function ( delta ) {
  47. var maskActive = false;
  48. var pass, i, il = this.passes.length;
  49. for ( i = 0; i < il; i ++ ) {
  50. pass = this.passes[ i ];
  51. if ( pass.enabled === false ) continue;
  52. pass.render( this.renderer, this.writeBuffer, this.readBuffer, delta, maskActive );
  53. if ( pass.needsSwap ) {
  54. if ( maskActive ) {
  55. var context = this.renderer.context;
  56. context.stencilFunc( context.NOTEQUAL, 1, 0xffffffff );
  57. this.copyPass.render( this.renderer, this.writeBuffer, this.readBuffer, delta );
  58. context.stencilFunc( context.EQUAL, 1, 0xffffffff );
  59. }
  60. this.swapBuffers();
  61. }
  62. if ( THREE.MaskPass !== undefined ) {
  63. if ( pass instanceof THREE.MaskPass ) {
  64. maskActive = true;
  65. } else if ( pass instanceof THREE.ClearMaskPass ) {
  66. maskActive = false;
  67. }
  68. }
  69. }
  70. },
  71. reset: function ( renderTarget ) {
  72. if ( renderTarget === undefined ) {
  73. var size = this.renderer.getDrawingBufferSize();
  74. renderTarget = this.renderTarget1.clone();
  75. renderTarget.setSize( size.width, size.height );
  76. }
  77. this.renderTarget1.dispose();
  78. this.renderTarget2.dispose();
  79. this.renderTarget1 = renderTarget;
  80. this.renderTarget2 = renderTarget.clone();
  81. this.writeBuffer = this.renderTarget1;
  82. this.readBuffer = this.renderTarget2;
  83. },
  84. setSize: function ( width, height ) {
  85. this.renderTarget1.setSize( width, height );
  86. this.renderTarget2.setSize( width, height );
  87. for ( var i = 0; i < this.passes.length; i ++ ) {
  88. this.passes[ i ].setSize( width, height );
  89. }
  90. }
  91. } );
  92. THREE.Pass = function () {
  93. // if set to true, the pass is processed by the composer
  94. this.enabled = true;
  95. // if set to true, the pass indicates to swap read and write buffer after rendering
  96. this.needsSwap = true;
  97. // if set to true, the pass clears its buffer before rendering
  98. this.clear = false;
  99. // if set to true, the result of the pass is rendered to screen
  100. this.renderToScreen = false;
  101. };
  102. Object.assign( THREE.Pass.prototype, {
  103. setSize: function ( width, height ) {},
  104. render: function ( renderer, writeBuffer, readBuffer, delta, maskActive ) {
  105. console.error( 'THREE.Pass: .render() must be implemented in derived pass.' );
  106. }
  107. } );