UnrealBloomPass.js 12 KB

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