UnrealBloomPass.js 12 KB

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