EffectComposer.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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. render: function ( delta ) {
  31. this.writeBuffer = this.renderTarget1;
  32. this.readBuffer = this.renderTarget2;
  33. var maskActive = false;
  34. var pass, i, il = this.passes.length;
  35. for ( i = 0; i < il; i ++ ) {
  36. pass = this.passes[ i ];
  37. if ( !pass.enabled ) continue;
  38. pass.render( this.renderer, this.writeBuffer, this.readBuffer, delta, maskActive );
  39. if ( pass.needsSwap ) {
  40. if ( maskActive ) {
  41. var context = this.renderer.context;
  42. context.stencilFunc( context.NOTEQUAL, 1, 0xffffffff );
  43. this.copyPass.render( this.renderer, this.writeBuffer, this.readBuffer, delta );
  44. context.stencilFunc( context.EQUAL, 1, 0xffffffff );
  45. }
  46. this.swapBuffers();
  47. }
  48. if ( pass instanceof THREE.MaskPass ) {
  49. maskActive = true;
  50. } else if ( pass instanceof THREE.ClearMaskPass ) {
  51. maskActive = false;
  52. }
  53. }
  54. },
  55. reset: function ( renderTarget ) {
  56. if ( renderTarget === undefined ) {
  57. renderTarget = this.renderTarget1.clone();
  58. renderTarget.width = window.innerWidth;
  59. renderTarget.height = window.innerHeight;
  60. }
  61. this.renderTarget1 = renderTarget;
  62. this.renderTarget2 = renderTarget.clone();
  63. this.writeBuffer = this.renderTarget1;
  64. this.readBuffer = this.renderTarget2;
  65. },
  66. setSize: function ( width, height ) {
  67. var renderTarget = this.renderTarget1.clone();
  68. renderTarget.width = width;
  69. renderTarget.height = height;
  70. this.reset( renderTarget );
  71. }
  72. };
  73. // shared ortho camera
  74. THREE.EffectComposer.camera = new THREE.OrthographicCamera( -1, 1, 1, -1, 0, 1 );
  75. THREE.EffectComposer.quad = new THREE.Mesh( new THREE.PlaneGeometry( 2, 2 ), null );
  76. THREE.EffectComposer.scene = new THREE.Scene();
  77. THREE.EffectComposer.scene.add( THREE.EffectComposer.quad );