EffectComposer.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. /**
  2. * @author alteredq / http://alteredqualia.com/
  3. */
  4. THREE.EffectComposer = function ( renderer, renderTarget ) {
  5. this.renderer = renderer;
  6. if ( renderTarget === undefined ) {
  7. var width = window.innerWidth || 1;
  8. var height = window.innerHeight || 1;
  9. var parameters = { minFilter: THREE.LinearFilter, magFilter: THREE.LinearFilter, format: THREE.RGBFormat, stencilBuffer: false };
  10. renderTarget = new THREE.WebGLRenderTarget( width, height, parameters );
  11. }
  12. this.renderTarget1 = renderTarget;
  13. this.renderTarget2 = renderTarget.clone();
  14. this.writeBuffer = this.renderTarget1;
  15. this.readBuffer = this.renderTarget2;
  16. this.passes = [];
  17. if ( THREE.CopyShader === undefined )
  18. console.error( "THREE.EffectComposer relies on THREE.CopyShader" );
  19. this.copyPass = new THREE.ShaderPass( THREE.CopyShader );
  20. };
  21. THREE.EffectComposer.prototype = {
  22. swapBuffers: function() {
  23. var tmp = this.readBuffer;
  24. this.readBuffer = this.writeBuffer;
  25. this.writeBuffer = tmp;
  26. },
  27. addPass: function ( pass ) {
  28. this.passes.push( pass );
  29. },
  30. insertPass: function ( pass, index ) {
  31. this.passes.splice( index, 0, pass );
  32. },
  33. render: function ( delta ) {
  34. this.writeBuffer = this.renderTarget1;
  35. this.readBuffer = this.renderTarget2;
  36. var maskActive = false;
  37. var pass, i, il = this.passes.length;
  38. for ( i = 0; i < il; i ++ ) {
  39. pass = this.passes[ i ];
  40. if ( !pass.enabled ) continue;
  41. pass.render( this.renderer, this.writeBuffer, this.readBuffer, delta, maskActive );
  42. if ( pass.needsSwap ) {
  43. if ( maskActive ) {
  44. var context = this.renderer.context;
  45. context.stencilFunc( context.NOTEQUAL, 1, 0xffffffff );
  46. this.copyPass.render( this.renderer, this.writeBuffer, this.readBuffer, delta );
  47. context.stencilFunc( context.EQUAL, 1, 0xffffffff );
  48. }
  49. this.swapBuffers();
  50. }
  51. if ( pass instanceof THREE.MaskPass ) {
  52. maskActive = true;
  53. } else if ( pass instanceof THREE.ClearMaskPass ) {
  54. maskActive = false;
  55. }
  56. }
  57. },
  58. reset: function ( renderTarget ) {
  59. if ( renderTarget === undefined ) {
  60. renderTarget = this.renderTarget1.clone();
  61. renderTarget.width = window.innerWidth;
  62. renderTarget.height = window.innerHeight;
  63. }
  64. this.renderTarget1 = renderTarget;
  65. this.renderTarget2 = renderTarget.clone();
  66. this.writeBuffer = this.renderTarget1;
  67. this.readBuffer = this.renderTarget2;
  68. },
  69. setSize: function ( width, height ) {
  70. var renderTarget = this.renderTarget1.clone();
  71. renderTarget.width = width;
  72. renderTarget.height = height;
  73. this.reset( renderTarget );
  74. }
  75. };
  76. // shared ortho camera
  77. THREE.EffectComposer.camera = new THREE.OrthographicCamera( -1, 1, 1, -1, 0, 1 );
  78. THREE.EffectComposer.quad = new THREE.Mesh( new THREE.PlaneGeometry( 2, 2 ), null );
  79. THREE.EffectComposer.scene = new THREE.Scene();
  80. THREE.EffectComposer.scene.add( THREE.EffectComposer.quad );