UnrealBloomPass.js 13 KB

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