BloomPass.js 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. import {
  2. AdditiveBlending,
  3. HalfFloatType,
  4. ShaderMaterial,
  5. UniformsUtils,
  6. Vector2,
  7. WebGLRenderTarget
  8. } from 'three';
  9. import { Pass, FullScreenQuad } from './Pass.js';
  10. import { ConvolutionShader } from '../shaders/ConvolutionShader.js';
  11. /**
  12. * A pass for a basic Bloom effect.
  13. *
  14. * {@link UnrealBloomPass} produces a more advanced Bloom but is also
  15. * more expensive.
  16. *
  17. * ```js
  18. * const effectBloom = new BloomPass( 0.75 );
  19. * composer.addPass( effectBloom );
  20. * ```
  21. *
  22. * @augments Pass
  23. */
  24. class BloomPass extends Pass {
  25. /**
  26. * Constructs a new Bloom pass.
  27. *
  28. * @param {number} [strength=1] - The Bloom strength.
  29. * @param {number} [kernelSize=25] - The kernel size.
  30. * @param {number} [sigma=4] - The sigma.
  31. */
  32. constructor( strength = 1, kernelSize = 25, sigma = 4 ) {
  33. super();
  34. // combine material
  35. /**
  36. * The combine pass uniforms.
  37. *
  38. * @type {Object}
  39. */
  40. this.combineUniforms = UniformsUtils.clone( CombineShader.uniforms );
  41. this.combineUniforms[ 'strength' ].value = strength;
  42. /**
  43. * The combine pass material.
  44. *
  45. * @type {ShaderMaterial}
  46. */
  47. this.materialCombine = new ShaderMaterial( {
  48. name: CombineShader.name,
  49. uniforms: this.combineUniforms,
  50. vertexShader: CombineShader.vertexShader,
  51. fragmentShader: CombineShader.fragmentShader,
  52. blending: AdditiveBlending,
  53. transparent: true
  54. } );
  55. // convolution material
  56. const convolutionShader = ConvolutionShader;
  57. /**
  58. * The convolution pass uniforms.
  59. *
  60. * @type {Object}
  61. */
  62. this.convolutionUniforms = UniformsUtils.clone( convolutionShader.uniforms );
  63. this.convolutionUniforms[ 'uImageIncrement' ].value = BloomPass.blurX;
  64. this.convolutionUniforms[ 'cKernel' ].value = buildKernel( sigma );
  65. /**
  66. * The convolution pass material.
  67. *
  68. * @type {ShaderMaterial}
  69. */
  70. this.materialConvolution = new ShaderMaterial( {
  71. name: convolutionShader.name,
  72. uniforms: this.convolutionUniforms,
  73. vertexShader: convolutionShader.vertexShader,
  74. fragmentShader: convolutionShader.fragmentShader,
  75. defines: {
  76. 'KERNEL_SIZE_FLOAT': kernelSize.toFixed( 1 ),
  77. 'KERNEL_SIZE_INT': kernelSize.toFixed( 0 )
  78. }
  79. } );
  80. /**
  81. * Overwritten to disable the swap.
  82. *
  83. * @type {boolean}
  84. * @default false
  85. */
  86. this.needsSwap = false;
  87. // internals
  88. this._renderTargetX = new WebGLRenderTarget( 1, 1, { type: HalfFloatType } ); // will be resized later
  89. this._renderTargetX.texture.name = 'BloomPass.x';
  90. this._renderTargetY = new WebGLRenderTarget( 1, 1, { type: HalfFloatType } ); // will be resized later
  91. this._renderTargetY.texture.name = 'BloomPass.y';
  92. this._fsQuad = new FullScreenQuad( null );
  93. }
  94. /**
  95. * Performs the Bloom pass.
  96. *
  97. * @param {WebGLRenderer} renderer - The renderer.
  98. * @param {WebGLRenderTarget} writeBuffer - The write buffer. This buffer is intended as the rendering
  99. * destination for the pass.
  100. * @param {WebGLRenderTarget} readBuffer - The read buffer. The pass can access the result from the
  101. * previous pass from this buffer.
  102. * @param {number} deltaTime - The delta time in seconds.
  103. * @param {boolean} maskActive - Whether masking is active or not.
  104. */
  105. render( renderer, writeBuffer, readBuffer, deltaTime, maskActive ) {
  106. if ( maskActive ) renderer.state.buffers.stencil.setTest( false );
  107. // Render quad with blurred scene into texture (convolution pass 1)
  108. this._fsQuad.material = this.materialConvolution;
  109. this.convolutionUniforms[ 'tDiffuse' ].value = readBuffer.texture;
  110. this.convolutionUniforms[ 'uImageIncrement' ].value = BloomPass.blurX;
  111. renderer.setRenderTarget( this._renderTargetX );
  112. renderer.clear();
  113. this._fsQuad.render( renderer );
  114. // Render quad with blurred scene into texture (convolution pass 2)
  115. this.convolutionUniforms[ 'tDiffuse' ].value = this._renderTargetX.texture;
  116. this.convolutionUniforms[ 'uImageIncrement' ].value = BloomPass.blurY;
  117. renderer.setRenderTarget( this._renderTargetY );
  118. renderer.clear();
  119. this._fsQuad.render( renderer );
  120. // Render original scene with superimposed blur to texture
  121. this._fsQuad.material = this.materialCombine;
  122. this.combineUniforms[ 'tDiffuse' ].value = this._renderTargetY.texture;
  123. if ( maskActive ) renderer.state.buffers.stencil.setTest( true );
  124. renderer.setRenderTarget( readBuffer );
  125. if ( this.clear ) renderer.clear();
  126. this._fsQuad.render( renderer );
  127. }
  128. /**
  129. * Sets the size of the pass.
  130. *
  131. * @param {number} width - The width to set.
  132. * @param {number} height - The width to set.
  133. */
  134. setSize( width, height ) {
  135. this._renderTargetX.setSize( width, height );
  136. this._renderTargetY.setSize( width, height );
  137. }
  138. /**
  139. * Frees the GPU-related resources allocated by this instance. Call this
  140. * method whenever the pass is no longer used in your app.
  141. */
  142. dispose() {
  143. this._renderTargetX.dispose();
  144. this._renderTargetY.dispose();
  145. this.materialCombine.dispose();
  146. this.materialConvolution.dispose();
  147. this._fsQuad.dispose();
  148. }
  149. }
  150. const CombineShader = {
  151. name: 'CombineShader',
  152. uniforms: {
  153. 'tDiffuse': { value: null },
  154. 'strength': { value: 1.0 }
  155. },
  156. vertexShader: /* glsl */`
  157. varying vec2 vUv;
  158. void main() {
  159. vUv = uv;
  160. gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );
  161. }`,
  162. fragmentShader: /* glsl */`
  163. uniform float strength;
  164. uniform sampler2D tDiffuse;
  165. varying vec2 vUv;
  166. void main() {
  167. vec4 texel = texture2D( tDiffuse, vUv );
  168. gl_FragColor = strength * texel;
  169. }`
  170. };
  171. BloomPass.blurX = new Vector2( 0.001953125, 0.0 );
  172. BloomPass.blurY = new Vector2( 0.0, 0.001953125 );
  173. function gauss( x, sigma ) {
  174. return Math.exp( - ( x * x ) / ( 2.0 * sigma * sigma ) );
  175. }
  176. function buildKernel( sigma ) {
  177. // We loop off the sqrt(2 * pi) * sigma term, since we're going to normalize anyway.
  178. const kMaxKernelSize = 25;
  179. let kernelSize = 2 * Math.ceil( sigma * 3.0 ) + 1;
  180. if ( kernelSize > kMaxKernelSize ) kernelSize = kMaxKernelSize;
  181. const halfWidth = ( kernelSize - 1 ) * 0.5;
  182. const values = new Array( kernelSize );
  183. let sum = 0.0;
  184. for ( let i = 0; i < kernelSize; ++ i ) {
  185. values[ i ] = gauss( i - halfWidth, sigma );
  186. sum += values[ i ];
  187. }
  188. // normalize the kernel
  189. for ( let i = 0; i < kernelSize; ++ i ) values[ i ] /= sum;
  190. return values;
  191. }
  192. export { BloomPass };
粤ICP备19079148号