2
0

BloomPass.js 3.7 KB

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