2
0

UnrealBloomPass.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411
  1. import {
  2. AdditiveBlending,
  3. Color,
  4. LinearFilter,
  5. MeshBasicMaterial,
  6. RGBAFormat,
  7. ShaderMaterial,
  8. UniformsUtils,
  9. Vector2,
  10. Vector3,
  11. WebGLRenderTarget
  12. } from '../../../build/three.module.js';
  13. import { Pass } from '../postprocessing/Pass.js';
  14. import { CopyShader } from '../shaders/CopyShader.js';
  15. import { LuminosityHighPassShader } from '../shaders/LuminosityHighPassShader.js';
  16. /**
  17. * UnrealBloomPass is inspired by the bloom pass of Unreal Engine. It creates a
  18. * mip map chain of bloom textures and blurs them with different radii. Because
  19. * of the weighted combination of mips, and because larger blurs are done on
  20. * higher mips, this effect provides good quality and performance.
  21. *
  22. * Reference:
  23. * - https://docs.unrealengine.com/latest/INT/Engine/Rendering/PostProcessEffects/Bloom/
  24. */
  25. var UnrealBloomPass = function ( resolution, strength, radius, threshold ) {
  26. Pass.call( this );
  27. this.strength = ( strength !== undefined ) ? strength : 1;
  28. this.radius = radius;
  29. this.threshold = threshold;
  30. this.resolution = ( resolution !== undefined ) ? new Vector2( resolution.x, resolution.y ) : new Vector2( 256, 256 );
  31. // create color only once here, reuse it later inside the render function
  32. this.clearColor = new Color( 0, 0, 0 );
  33. // render targets
  34. var pars = { minFilter: LinearFilter, magFilter: LinearFilter, format: RGBAFormat };
  35. this.renderTargetsHorizontal = [];
  36. this.renderTargetsVertical = [];
  37. this.nMips = 5;
  38. var resx = Math.round( this.resolution.x / 2 );
  39. var resy = Math.round( this.resolution.y / 2 );
  40. this.renderTargetBright = new WebGLRenderTarget( resx, resy, pars );
  41. this.renderTargetBright.texture.name = 'UnrealBloomPass.bright';
  42. this.renderTargetBright.texture.generateMipmaps = false;
  43. for ( var i = 0; i < this.nMips; i ++ ) {
  44. var renderTargetHorizonal = new WebGLRenderTarget( resx, resy, pars );
  45. renderTargetHorizonal.texture.name = 'UnrealBloomPass.h' + i;
  46. renderTargetHorizonal.texture.generateMipmaps = false;
  47. this.renderTargetsHorizontal.push( renderTargetHorizonal );
  48. var renderTargetVertical = new WebGLRenderTarget( resx, resy, pars );
  49. renderTargetVertical.texture.name = 'UnrealBloomPass.v' + i;
  50. renderTargetVertical.texture.generateMipmaps = false;
  51. this.renderTargetsVertical.push( renderTargetVertical );
  52. resx = Math.round( resx / 2 );
  53. resy = Math.round( resy / 2 );
  54. }
  55. // luminosity high pass material
  56. if ( LuminosityHighPassShader === undefined )
  57. console.error( 'THREE.UnrealBloomPass relies on LuminosityHighPassShader' );
  58. var highPassShader = LuminosityHighPassShader;
  59. this.highPassUniforms = UniformsUtils.clone( highPassShader.uniforms );
  60. this.highPassUniforms[ 'luminosityThreshold' ].value = threshold;
  61. this.highPassUniforms[ 'smoothWidth' ].value = 0.01;
  62. this.materialHighPassFilter = new ShaderMaterial( {
  63. uniforms: this.highPassUniforms,
  64. vertexShader: highPassShader.vertexShader,
  65. fragmentShader: highPassShader.fragmentShader,
  66. defines: {}
  67. } );
  68. // Gaussian Blur Materials
  69. this.separableBlurMaterials = [];
  70. var kernelSizeArray = [ 3, 5, 7, 9, 11 ];
  71. var resx = Math.round( this.resolution.x / 2 );
  72. var resy = Math.round( this.resolution.y / 2 );
  73. for ( var i = 0; i < this.nMips; i ++ ) {
  74. this.separableBlurMaterials.push( this.getSeperableBlurMaterial( kernelSizeArray[ i ] ) );
  75. this.separableBlurMaterials[ i ].uniforms[ 'texSize' ].value = new Vector2( resx, resy );
  76. resx = Math.round( resx / 2 );
  77. resy = Math.round( resy / 2 );
  78. }
  79. // Composite material
  80. this.compositeMaterial = this.getCompositeMaterial( this.nMips );
  81. this.compositeMaterial.uniforms[ 'blurTexture1' ].value = this.renderTargetsVertical[ 0 ].texture;
  82. this.compositeMaterial.uniforms[ 'blurTexture2' ].value = this.renderTargetsVertical[ 1 ].texture;
  83. this.compositeMaterial.uniforms[ 'blurTexture3' ].value = this.renderTargetsVertical[ 2 ].texture;
  84. this.compositeMaterial.uniforms[ 'blurTexture4' ].value = this.renderTargetsVertical[ 3 ].texture;
  85. this.compositeMaterial.uniforms[ 'blurTexture5' ].value = this.renderTargetsVertical[ 4 ].texture;
  86. this.compositeMaterial.uniforms[ 'bloomStrength' ].value = strength;
  87. this.compositeMaterial.uniforms[ 'bloomRadius' ].value = 0.1;
  88. this.compositeMaterial.needsUpdate = true;
  89. var bloomFactors = [ 1.0, 0.8, 0.6, 0.4, 0.2 ];
  90. this.compositeMaterial.uniforms[ 'bloomFactors' ].value = bloomFactors;
  91. this.bloomTintColors = [ new Vector3( 1, 1, 1 ), new Vector3( 1, 1, 1 ), new Vector3( 1, 1, 1 ),
  92. new Vector3( 1, 1, 1 ), new Vector3( 1, 1, 1 ) ];
  93. this.compositeMaterial.uniforms[ 'bloomTintColors' ].value = this.bloomTintColors;
  94. // copy material
  95. if ( CopyShader === undefined ) {
  96. console.error( 'THREE.UnrealBloomPass relies on CopyShader' );
  97. }
  98. var copyShader = CopyShader;
  99. this.copyUniforms = UniformsUtils.clone( copyShader.uniforms );
  100. this.copyUniforms[ 'opacity' ].value = 1.0;
  101. this.materialCopy = new ShaderMaterial( {
  102. uniforms: this.copyUniforms,
  103. vertexShader: copyShader.vertexShader,
  104. fragmentShader: copyShader.fragmentShader,
  105. blending: AdditiveBlending,
  106. depthTest: false,
  107. depthWrite: false,
  108. transparent: true
  109. } );
  110. this.enabled = true;
  111. this.needsSwap = false;
  112. this._oldClearColor = new Color();
  113. this.oldClearAlpha = 1;
  114. this.basic = new MeshBasicMaterial();
  115. this.fsQuad = new Pass.FullScreenQuad( null );
  116. };
  117. UnrealBloomPass.prototype = Object.assign( Object.create( Pass.prototype ), {
  118. constructor: UnrealBloomPass,
  119. dispose: function () {
  120. for ( var i = 0; i < this.renderTargetsHorizontal.length; i ++ ) {
  121. this.renderTargetsHorizontal[ i ].dispose();
  122. }
  123. for ( var i = 0; i < this.renderTargetsVertical.length; i ++ ) {
  124. this.renderTargetsVertical[ i ].dispose();
  125. }
  126. this.renderTargetBright.dispose();
  127. },
  128. setSize: function ( width, height ) {
  129. var resx = Math.round( width / 2 );
  130. var resy = Math.round( height / 2 );
  131. this.renderTargetBright.setSize( resx, resy );
  132. for ( var i = 0; i < this.nMips; i ++ ) {
  133. this.renderTargetsHorizontal[ i ].setSize( resx, resy );
  134. this.renderTargetsVertical[ i ].setSize( resx, resy );
  135. this.separableBlurMaterials[ i ].uniforms[ 'texSize' ].value = new Vector2( resx, resy );
  136. resx = Math.round( resx / 2 );
  137. resy = Math.round( resy / 2 );
  138. }
  139. },
  140. render: function ( renderer, writeBuffer, readBuffer, deltaTime, maskActive ) {
  141. renderer.getClearColor( this._oldClearColor );
  142. this.oldClearAlpha = renderer.getClearAlpha();
  143. var oldAutoClear = renderer.autoClear;
  144. renderer.autoClear = false;
  145. renderer.setClearColor( this.clearColor, 0 );
  146. if ( maskActive ) renderer.state.buffers.stencil.setTest( false );
  147. // Render input to screen
  148. if ( this.renderToScreen ) {
  149. this.fsQuad.material = this.basic;
  150. this.basic.map = readBuffer.texture;
  151. renderer.setRenderTarget( null );
  152. renderer.clear();
  153. this.fsQuad.render( renderer );
  154. }
  155. // 1. Extract Bright Areas
  156. this.highPassUniforms[ 'tDiffuse' ].value = readBuffer.texture;
  157. this.highPassUniforms[ 'luminosityThreshold' ].value = this.threshold;
  158. this.fsQuad.material = this.materialHighPassFilter;
  159. renderer.setRenderTarget( this.renderTargetBright );
  160. renderer.clear();
  161. this.fsQuad.render( renderer );
  162. // 2. Blur All the mips progressively
  163. var inputRenderTarget = this.renderTargetBright;
  164. for ( var i = 0; i < this.nMips; i ++ ) {
  165. this.fsQuad.material = this.separableBlurMaterials[ i ];
  166. this.separableBlurMaterials[ i ].uniforms[ 'colorTexture' ].value = inputRenderTarget.texture;
  167. this.separableBlurMaterials[ i ].uniforms[ 'direction' ].value = UnrealBloomPass.BlurDirectionX;
  168. renderer.setRenderTarget( this.renderTargetsHorizontal[ i ] );
  169. renderer.clear();
  170. this.fsQuad.render( renderer );
  171. this.separableBlurMaterials[ i ].uniforms[ 'colorTexture' ].value = this.renderTargetsHorizontal[ i ].texture;
  172. this.separableBlurMaterials[ i ].uniforms[ 'direction' ].value = UnrealBloomPass.BlurDirectionY;
  173. renderer.setRenderTarget( this.renderTargetsVertical[ i ] );
  174. renderer.clear();
  175. this.fsQuad.render( renderer );
  176. inputRenderTarget = this.renderTargetsVertical[ i ];
  177. }
  178. // Composite All the mips
  179. this.fsQuad.material = this.compositeMaterial;
  180. this.compositeMaterial.uniforms[ 'bloomStrength' ].value = this.strength;
  181. this.compositeMaterial.uniforms[ 'bloomRadius' ].value = this.radius;
  182. this.compositeMaterial.uniforms[ 'bloomTintColors' ].value = this.bloomTintColors;
  183. renderer.setRenderTarget( this.renderTargetsHorizontal[ 0 ] );
  184. renderer.clear();
  185. this.fsQuad.render( renderer );
  186. // Blend it additively over the input texture
  187. this.fsQuad.material = this.materialCopy;
  188. this.copyUniforms[ 'tDiffuse' ].value = this.renderTargetsHorizontal[ 0 ].texture;
  189. if ( maskActive ) renderer.state.buffers.stencil.setTest( true );
  190. if ( this.renderToScreen ) {
  191. renderer.setRenderTarget( null );
  192. this.fsQuad.render( renderer );
  193. } else {
  194. renderer.setRenderTarget( readBuffer );
  195. this.fsQuad.render( renderer );
  196. }
  197. // Restore renderer settings
  198. renderer.setClearColor( this._oldClearColor, this.oldClearAlpha );
  199. renderer.autoClear = oldAutoClear;
  200. },
  201. getSeperableBlurMaterial: function ( kernelRadius ) {
  202. return new ShaderMaterial( {
  203. defines: {
  204. 'KERNEL_RADIUS': kernelRadius,
  205. 'SIGMA': kernelRadius
  206. },
  207. uniforms: {
  208. 'colorTexture': { value: null },
  209. 'texSize': { value: new Vector2( 0.5, 0.5 ) },
  210. 'direction': { value: new Vector2( 0.5, 0.5 ) }
  211. },
  212. vertexShader:
  213. 'varying vec2 vUv;\n\
  214. void main() {\n\
  215. vUv = uv;\n\
  216. gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );\n\
  217. }',
  218. fragmentShader:
  219. '#include <common>\
  220. varying vec2 vUv;\n\
  221. uniform sampler2D colorTexture;\n\
  222. uniform vec2 texSize;\
  223. uniform vec2 direction;\
  224. \
  225. float gaussianPdf(in float x, in float sigma) {\
  226. return 0.39894 * exp( -0.5 * x * x/( sigma * sigma))/sigma;\
  227. }\
  228. void main() {\n\
  229. vec2 invSize = 1.0 / texSize;\
  230. float fSigma = float(SIGMA);\
  231. float weightSum = gaussianPdf(0.0, fSigma);\
  232. vec3 diffuseSum = texture2D( colorTexture, vUv).rgb * weightSum;\
  233. for( int i = 1; i < KERNEL_RADIUS; i ++ ) {\
  234. float x = float(i);\
  235. float w = gaussianPdf(x, fSigma);\
  236. vec2 uvOffset = direction * invSize * x;\
  237. vec3 sample1 = texture2D( colorTexture, vUv + uvOffset).rgb;\
  238. vec3 sample2 = texture2D( colorTexture, vUv - uvOffset).rgb;\
  239. diffuseSum += (sample1 + sample2) * w;\
  240. weightSum += 2.0 * w;\
  241. }\
  242. gl_FragColor = vec4(diffuseSum/weightSum, 1.0);\n\
  243. }'
  244. } );
  245. },
  246. getCompositeMaterial: function ( nMips ) {
  247. return new ShaderMaterial( {
  248. defines: {
  249. 'NUM_MIPS': nMips
  250. },
  251. uniforms: {
  252. 'blurTexture1': { value: null },
  253. 'blurTexture2': { value: null },
  254. 'blurTexture3': { value: null },
  255. 'blurTexture4': { value: null },
  256. 'blurTexture5': { value: null },
  257. 'dirtTexture': { value: null },
  258. 'bloomStrength': { value: 1.0 },
  259. 'bloomFactors': { value: null },
  260. 'bloomTintColors': { value: null },
  261. 'bloomRadius': { value: 0.0 }
  262. },
  263. vertexShader:
  264. 'varying vec2 vUv;\n\
  265. void main() {\n\
  266. vUv = uv;\n\
  267. gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );\n\
  268. }',
  269. fragmentShader:
  270. 'varying vec2 vUv;\
  271. uniform sampler2D blurTexture1;\
  272. uniform sampler2D blurTexture2;\
  273. uniform sampler2D blurTexture3;\
  274. uniform sampler2D blurTexture4;\
  275. uniform sampler2D blurTexture5;\
  276. uniform sampler2D dirtTexture;\
  277. uniform float bloomStrength;\
  278. uniform float bloomRadius;\
  279. uniform float bloomFactors[NUM_MIPS];\
  280. uniform vec3 bloomTintColors[NUM_MIPS];\
  281. \
  282. float lerpBloomFactor(const in float factor) { \
  283. float mirrorFactor = 1.2 - factor;\
  284. return mix(factor, mirrorFactor, bloomRadius);\
  285. }\
  286. \
  287. void main() {\
  288. gl_FragColor = bloomStrength * ( lerpBloomFactor(bloomFactors[0]) * vec4(bloomTintColors[0], 1.0) * texture2D(blurTexture1, vUv) + \
  289. lerpBloomFactor(bloomFactors[1]) * vec4(bloomTintColors[1], 1.0) * texture2D(blurTexture2, vUv) + \
  290. lerpBloomFactor(bloomFactors[2]) * vec4(bloomTintColors[2], 1.0) * texture2D(blurTexture3, vUv) + \
  291. lerpBloomFactor(bloomFactors[3]) * vec4(bloomTintColors[3], 1.0) * texture2D(blurTexture4, vUv) + \
  292. lerpBloomFactor(bloomFactors[4]) * vec4(bloomTintColors[4], 1.0) * texture2D(blurTexture5, vUv) );\
  293. }'
  294. } );
  295. }
  296. } );
  297. UnrealBloomPass.BlurDirectionX = new Vector2( 1.0, 0.0 );
  298. UnrealBloomPass.BlurDirectionY = new Vector2( 0.0, 1.0 );
  299. export { UnrealBloomPass };