EffectComposer.js 2.8 KB

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