UnrealBloomPass.js 12 KB

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