2
0

BloomPass.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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. // copy material
  14. if ( THREE.CopyShader === undefined )
  15. console.error( "THREE.BloomPass relies on THREE.CopyShader" );
  16. var copyShader = THREE.CopyShader;
  17. this.copyUniforms = THREE.UniformsUtils.clone( copyShader.uniforms );
  18. this.copyUniforms[ "opacity" ].value = strength;
  19. this.materialCopy = new THREE.ShaderMaterial( {
  20. uniforms: this.copyUniforms,
  21. vertexShader: copyShader.vertexShader,
  22. fragmentShader: copyShader.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. this.camera = new THREE.OrthographicCamera( -1, 1, 1, -1, 0, 1 );
  46. this.scene = new THREE.Scene();
  47. this.quad = new THREE.Mesh( new THREE.PlaneBufferGeometry( 2, 2 ), null );
  48. this.scene.add( this.quad );
  49. };
  50. THREE.BloomPass.prototype = {
  51. render: function ( renderer, writeBuffer, readBuffer, delta, maskActive ) {
  52. if ( maskActive ) renderer.context.disable( renderer.context.STENCIL_TEST );
  53. // Render quad with blured scene into texture (convolution pass 1)
  54. this.quad.material = this.materialConvolution;
  55. this.convolutionUniforms[ "tDiffuse" ].value = readBuffer;
  56. this.convolutionUniforms[ "uImageIncrement" ].value = THREE.BloomPass.blurX;
  57. renderer.render( this.scene, this.camera, this.renderTargetX, true );
  58. // Render quad with blured scene into texture (convolution pass 2)
  59. this.convolutionUniforms[ "tDiffuse" ].value = this.renderTargetX;
  60. this.convolutionUniforms[ "uImageIncrement" ].value = THREE.BloomPass.blurY;
  61. renderer.render( this.scene, this.camera, this.renderTargetY, true );
  62. // Render original scene with superimposed blur to texture
  63. this.quad.material = this.materialCopy;
  64. this.copyUniforms[ "tDiffuse" ].value = this.renderTargetY;
  65. if ( maskActive ) renderer.context.enable( renderer.context.STENCIL_TEST );
  66. renderer.render( this.scene, this.camera, readBuffer, this.clear );
  67. }
  68. };
  69. THREE.BloomPass.blurX = new THREE.Vector2( 0.001953125, 0.0 );
  70. THREE.BloomPass.blurY = new THREE.Vector2( 0.0, 0.001953125 );