EffectComposer.js 3.4 KB

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