EffectComposer.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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. this.renderTargetParameters = { minFilter: THREE.LinearFilter, magFilter: THREE.LinearFilter, format: THREE.RGBFormat, stencilBufer: false };
  9. this.renderTarget1 = new THREE.WebGLRenderTarget( window.innerWidth, window.innerHeight, this.renderTargetParameters );
  10. }
  11. this.renderTarget2 = this.renderTarget1.clone();
  12. this.writeBuffer = this.renderTarget1;
  13. this.readBuffer = this.renderTarget2;
  14. this.passes = [];
  15. this.copyPass = new THREE.ShaderPass( THREE.ShaderExtras[ "screen" ] );
  16. };
  17. THREE.EffectComposer.prototype = {
  18. swapBuffers: function() {
  19. var tmp = this.readBuffer;
  20. this.readBuffer = this.writeBuffer;
  21. this.writeBuffer = tmp;
  22. },
  23. addPass: function ( pass ) {
  24. this.passes.push( pass );
  25. },
  26. render: function ( delta ) {
  27. this.writeBuffer = this.renderTarget1;
  28. this.readBuffer = this.renderTarget2;
  29. var maskActive = false;
  30. var i, il = this.passes.length;
  31. for ( i = 0; i < il; i ++ ) {
  32. this.passes[ i ].render( this.renderer, this.writeBuffer, this.readBuffer, delta, maskActive );
  33. if ( this.passes[ i ].needsSwap ) {
  34. if ( maskActive ) {
  35. var context = this.renderer.context;
  36. context.stencilFunc( context.NOTEQUAL, 1, 0xffffffff );
  37. this.copyPass.render( this.renderer, this.writeBuffer, this.readBuffer, delta );
  38. context.stencilFunc( context.EQUAL, 1, 0xffffffff );
  39. }
  40. this.swapBuffers();
  41. }
  42. if ( this.passes[ i ] instanceof THREE.MaskPass ) {
  43. maskActive = true;
  44. }
  45. if ( this.passes[ i ] instanceof THREE.ClearMaskPass ) {
  46. maskActive = false;
  47. }
  48. }
  49. },
  50. reset: function ( renderTarget ) {
  51. this.renderTarget1 = renderTarget;
  52. if ( this.renderTarget1 === undefined ) {
  53. this.renderTarget1 = new THREE.WebGLRenderTarget( window.innerWidth, window.innerHeight, this.renderTargetParameters );
  54. }
  55. this.renderTarget2 = this.renderTarget1.clone();
  56. this.writeBuffer = this.renderTarget1;
  57. this.readBuffer = this.renderTarget2;
  58. THREE.EffectComposer.quad.scale.set( window.innerWidth, window.innerHeight, 1 );
  59. THREE.EffectComposer.camera.left = window.innerWidth / - 2;
  60. THREE.EffectComposer.camera.right = window.innerWidth / 2;
  61. THREE.EffectComposer.camera.top = window.innerHeight / 2;
  62. THREE.EffectComposer.camera.bottom = window.innerHeight / - 2;
  63. THREE.EffectComposer.camera.updateProjectionMatrix();
  64. }
  65. };
  66. // shared fullscreen quad scene
  67. THREE.EffectComposer.geometry = new THREE.PlaneGeometry( 1, 1 );
  68. THREE.EffectComposer.quad = new THREE.Mesh( THREE.EffectComposer.geometry, null );
  69. THREE.EffectComposer.quad.position.z = -100;
  70. THREE.EffectComposer.quad.scale.set( window.innerWidth, window.innerHeight, 1 );
  71. THREE.EffectComposer.scene = new THREE.Scene();
  72. THREE.EffectComposer.scene.add( THREE.EffectComposer.quad );
  73. // shared ortho camera
  74. THREE.EffectComposer.camera = new THREE.OrthoCamera( window.innerWidth / - 2, window.innerWidth / 2, window.innerHeight / 2, window.innerHeight / - 2, -10000, 10000 );