2
0

BloomPass.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. /**
  2. * @author alteredq / http://alteredqualia.com/
  3. */
  4. THREE.BloomPass = function ( strength, kernelSize, sigma, resolution ) {
  5. strength = ( strength !== undefined ) ? strength : 1;
  6. kernelSize = ( kernelSize !== undefined ) ? kernelSize : 25;
  7. sigma = ( sigma !== undefined ) ? sigma : 4.0;
  8. resolution = ( resolution !== undefined ) ? resolution : 256;
  9. // render targets
  10. var pars = { minFilter: THREE.LinearFilter, magFilter: THREE.LinearFilter, format: THREE.RGBFormat };
  11. this.renderTargetX = new THREE.WebGLRenderTarget( resolution, resolution, pars );
  12. this.renderTargetY = new THREE.WebGLRenderTarget( resolution, resolution, pars );
  13. // blit material
  14. if ( THREE.BlitShader === undefined )
  15. console.error( "THREE.BloomPass relies on THREE.BlitShader" );
  16. var blitShader = THREE.BlitShader;
  17. this.blitUniforms = THREE.UniformsUtils.clone( blitShader.uniforms );
  18. this.blitUniforms[ "opacity" ].value = strength;
  19. this.materialBlit = new THREE.ShaderMaterial( {
  20. uniforms: this.blitUniforms,
  21. vertexShader: blitShader.vertexShader,
  22. fragmentShader: blitShader.fragmentShader,
  23. blending: THREE.AdditiveBlending,
  24. transparent: true
  25. } );
  26. // convolution material
  27. if ( THREE.ConvolutionShader === undefined )
  28. console.error( "THREE.BloomPass relies on THREE.ConvolutionShader" );
  29. var convolutionShader = THREE.ConvolutionShader;
  30. this.convolutionUniforms = THREE.UniformsUtils.clone( convolutionShader.uniforms );
  31. this.convolutionUniforms[ "uImageIncrement" ].value = THREE.BloomPass.blurx;
  32. this.convolutionUniforms[ "cKernel" ].value = THREE.ConvolutionShader.buildKernel( sigma );
  33. this.materialConvolution = new THREE.ShaderMaterial( {
  34. uniforms: this.convolutionUniforms,
  35. vertexShader: convolutionShader.vertexShader,
  36. fragmentShader: convolutionShader.fragmentShader,
  37. defines: {
  38. "KERNEL_SIZE_FLOAT": kernelSize.toFixed( 1 ),
  39. "KERNEL_SIZE_INT": kernelSize.toFixed( 0 )
  40. }
  41. } );
  42. this.enabled = true;
  43. this.needsSwap = false;
  44. this.clear = false;
  45. };
  46. THREE.BloomPass.prototype = {
  47. render: function ( renderer, writeBuffer, readBuffer, delta, maskActive ) {
  48. if ( maskActive ) renderer.context.disable( renderer.context.STENCIL_TEST );
  49. // Render quad with blured scene into texture (convolution pass 1)
  50. THREE.EffectComposer.quad.material = this.materialConvolution;
  51. this.convolutionUniforms[ "tDiffuse" ].value = readBuffer;
  52. this.convolutionUniforms[ "uImageIncrement" ].value = THREE.BloomPass.blurX;
  53. renderer.render( THREE.EffectComposer.scene, THREE.EffectComposer.camera, this.renderTargetX, true );
  54. // Render quad with blured scene into texture (convolution pass 2)
  55. this.convolutionUniforms[ "tDiffuse" ].value = this.renderTargetX;
  56. this.convolutionUniforms[ "uImageIncrement" ].value = THREE.BloomPass.blurY;
  57. renderer.render( THREE.EffectComposer.scene, THREE.EffectComposer.camera, this.renderTargetY, true );
  58. // Render original scene with superimposed blur to texture
  59. THREE.EffectComposer.quad.material = this.materialBlit;
  60. this.blitUniforms[ "tDiffuse" ].value = this.renderTargetY;
  61. if ( maskActive ) renderer.context.enable( renderer.context.STENCIL_TEST );
  62. renderer.render( THREE.EffectComposer.scene, THREE.EffectComposer.camera, readBuffer, this.clear );
  63. }
  64. };
  65. THREE.BloomPass.blurX = new THREE.Vector2( 0.001953125, 0.0 );
  66. THREE.BloomPass.blurY = new THREE.Vector2( 0.0, 0.001953125 );