UnrealBloomPass.js 13 KB

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