BloomPass.js 3.6 KB

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