UnrealBloomPass.js 13 KB

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