EffectComposer.js 3.5 KB

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