BloomPass.js 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. ( function () {
  2. class BloomPass extends THREE.Pass {
  3. constructor( strength = 1, kernelSize = 25, sigma = 4, resolution = 256 ) {
  4. super(); // render targets
  5. const pars = {
  6. minFilter: THREE.LinearFilter,
  7. magFilter: THREE.LinearFilter,
  8. format: THREE.RGBAFormat
  9. };
  10. this.renderTargetX = new THREE.WebGLRenderTarget( resolution, resolution, pars );
  11. this.renderTargetX.texture.name = 'BloomPass.x';
  12. this.renderTargetY = new THREE.WebGLRenderTarget( resolution, resolution, pars );
  13. this.renderTargetY.texture.name = 'BloomPass.y'; // copy material
  14. if ( THREE.CopyShader === undefined ) console.error( 'THREE.BloomPass relies on THREE.CopyShader' );
  15. const copyShader = THREE.CopyShader;
  16. this.copyUniforms = THREE.UniformsUtils.clone( copyShader.uniforms );
  17. this.copyUniforms[ 'opacity' ].value = strength;
  18. this.materialCopy = new THREE.ShaderMaterial( {
  19. uniforms: this.copyUniforms,
  20. vertexShader: copyShader.vertexShader,
  21. fragmentShader: copyShader.fragmentShader,
  22. blending: THREE.AdditiveBlending,
  23. transparent: true
  24. } ); // convolution material
  25. if ( THREE.ConvolutionShader === undefined ) console.error( 'THREE.BloomPass relies on THREE.ConvolutionShader' );
  26. const convolutionShader = THREE.ConvolutionShader;
  27. this.convolutionUniforms = THREE.UniformsUtils.clone( convolutionShader.uniforms );
  28. this.convolutionUniforms[ 'uImageIncrement' ].value = BloomPass.blurX;
  29. this.convolutionUniforms[ 'cKernel' ].value = THREE.ConvolutionShader.buildKernel( sigma );
  30. this.materialConvolution = new THREE.ShaderMaterial( {
  31. uniforms: this.convolutionUniforms,
  32. vertexShader: convolutionShader.vertexShader,
  33. fragmentShader: convolutionShader.fragmentShader,
  34. defines: {
  35. 'KERNEL_SIZE_FLOAT': kernelSize.toFixed( 1 ),
  36. 'KERNEL_SIZE_INT': kernelSize.toFixed( 0 )
  37. }
  38. } );
  39. this.needsSwap = false;
  40. this.fsQuad = new THREE.FullScreenQuad( null );
  41. }
  42. render( renderer, writeBuffer, readBuffer, deltaTime, maskActive ) {
  43. if ( maskActive ) renderer.state.buffers.stencil.setTest( false ); // Render quad with blured scene into texture (convolution pass 1)
  44. this.fsQuad.material = this.materialConvolution;
  45. this.convolutionUniforms[ 'tDiffuse' ].value = readBuffer.texture;
  46. this.convolutionUniforms[ 'uImageIncrement' ].value = BloomPass.blurX;
  47. renderer.setRenderTarget( this.renderTargetX );
  48. renderer.clear();
  49. this.fsQuad.render( renderer ); // Render quad with blured scene into texture (convolution pass 2)
  50. this.convolutionUniforms[ 'tDiffuse' ].value = this.renderTargetX.texture;
  51. this.convolutionUniforms[ 'uImageIncrement' ].value = BloomPass.blurY;
  52. renderer.setRenderTarget( this.renderTargetY );
  53. renderer.clear();
  54. this.fsQuad.render( renderer ); // Render original scene with superimposed blur to texture
  55. this.fsQuad.material = this.materialCopy;
  56. this.copyUniforms[ 'tDiffuse' ].value = this.renderTargetY.texture;
  57. if ( maskActive ) renderer.state.buffers.stencil.setTest( true );
  58. renderer.setRenderTarget( readBuffer );
  59. if ( this.clear ) renderer.clear();
  60. this.fsQuad.render( renderer );
  61. }
  62. }
  63. BloomPass.blurX = new THREE.Vector2( 0.001953125, 0.0 );
  64. BloomPass.blurY = new THREE.Vector2( 0.0, 0.001953125 );
  65. THREE.BloomPass = BloomPass;
  66. } )();