EffectComposer.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. /**
  2. * @author alteredq / http://alteredqualia.com/
  3. */
  4. THREE.EffectComposer = function ( renderer, renderTarget ) {
  5. this.renderer = renderer;
  6. this.renderTarget1 = renderTarget;
  7. if ( this.renderTarget1 === undefined ) {
  8. var width = window.innerWidth || 1;
  9. var height = window.innerHeight || 1;
  10. this.renderTargetParameters = { minFilter: THREE.LinearFilter, magFilter: THREE.LinearFilter, format: THREE.RGBFormat, stencilBuffer: false };
  11. this.renderTarget1 = new THREE.WebGLRenderTarget( width, height, this.renderTargetParameters );
  12. }
  13. this.renderTarget2 = this.renderTarget1.clone();
  14. this.writeBuffer = this.renderTarget1;
  15. this.readBuffer = this.renderTarget2;
  16. this.passes = [];
  17. if ( THREE.BlitShader === undefined )
  18. console.error( "THREE.EffectComposer relies on THREE.BlitShader" );
  19. this.copyPass = new THREE.ShaderPass( THREE.BlitShader );
  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. this.renderTarget1 = renderTarget;
  57. if ( this.renderTarget1 === undefined ) {
  58. this.renderTarget1 = new THREE.WebGLRenderTarget( window.innerWidth, window.innerHeight, this.renderTargetParameters );
  59. }
  60. this.renderTarget2 = this.renderTarget1.clone();
  61. this.writeBuffer = this.renderTarget1;
  62. this.readBuffer = this.renderTarget2;
  63. }
  64. };
  65. // shared ortho camera
  66. THREE.EffectComposer.camera = new THREE.OrthographicCamera( -1, 1, 1, -1, 0, 1 );
  67. THREE.EffectComposer.quad = new THREE.Mesh( new THREE.PlaneGeometry( 2, 2 ), null );
  68. THREE.EffectComposer.scene = new THREE.Scene();
  69. THREE.EffectComposer.scene.add( THREE.EffectComposer.quad );