UnrealBloomPass.js 12 KB

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