UnrealBloomPass.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  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. for ( let i = 0; i < this.separableBlurMaterials.length; i ++ ) {
  104. this.separableBlurMaterials[ i ].dispose();
  105. }
  106. this.compositeMaterial.dispose();
  107. this.materialCopy.dispose();
  108. this.basic.dispose(); //
  109. this.fsQuad.dispose();
  110. }
  111. setSize( width, height ) {
  112. let resx = Math.round( width / 2 );
  113. let resy = Math.round( height / 2 );
  114. this.renderTargetBright.setSize( resx, resy );
  115. for ( let i = 0; i < this.nMips; i ++ ) {
  116. this.renderTargetsHorizontal[ i ].setSize( resx, resy );
  117. this.renderTargetsVertical[ i ].setSize( resx, resy );
  118. this.separableBlurMaterials[ i ].uniforms[ 'texSize' ].value = new THREE.Vector2( resx, resy );
  119. resx = Math.round( resx / 2 );
  120. resy = Math.round( resy / 2 );
  121. }
  122. }
  123. render( renderer, writeBuffer, readBuffer, deltaTime, maskActive ) {
  124. renderer.getClearColor( this._oldClearColor );
  125. this.oldClearAlpha = renderer.getClearAlpha();
  126. const oldAutoClear = renderer.autoClear;
  127. renderer.autoClear = false;
  128. renderer.setClearColor( this.clearColor, 0 );
  129. if ( maskActive ) renderer.state.buffers.stencil.setTest( false ); // Render input to screen
  130. if ( this.renderToScreen ) {
  131. this.fsQuad.material = this.basic;
  132. this.basic.map = readBuffer.texture;
  133. renderer.setRenderTarget( null );
  134. renderer.clear();
  135. this.fsQuad.render( renderer );
  136. } // 1. Extract Bright Areas
  137. this.highPassUniforms[ 'tDiffuse' ].value = readBuffer.texture;
  138. this.highPassUniforms[ 'luminosityThreshold' ].value = this.threshold;
  139. this.fsQuad.material = this.materialHighPassFilter;
  140. renderer.setRenderTarget( this.renderTargetBright );
  141. renderer.clear();
  142. this.fsQuad.render( renderer ); // 2. Blur All the mips progressively
  143. let inputRenderTarget = this.renderTargetBright;
  144. for ( let i = 0; i < this.nMips; i ++ ) {
  145. this.fsQuad.material = this.separableBlurMaterials[ i ];
  146. this.separableBlurMaterials[ i ].uniforms[ 'colorTexture' ].value = inputRenderTarget.texture;
  147. this.separableBlurMaterials[ i ].uniforms[ 'direction' ].value = UnrealBloomPass.BlurDirectionX;
  148. renderer.setRenderTarget( this.renderTargetsHorizontal[ i ] );
  149. renderer.clear();
  150. this.fsQuad.render( renderer );
  151. this.separableBlurMaterials[ i ].uniforms[ 'colorTexture' ].value = this.renderTargetsHorizontal[ i ].texture;
  152. this.separableBlurMaterials[ i ].uniforms[ 'direction' ].value = UnrealBloomPass.BlurDirectionY;
  153. renderer.setRenderTarget( this.renderTargetsVertical[ i ] );
  154. renderer.clear();
  155. this.fsQuad.render( renderer );
  156. inputRenderTarget = this.renderTargetsVertical[ i ];
  157. } // Composite All the mips
  158. this.fsQuad.material = this.compositeMaterial;
  159. this.compositeMaterial.uniforms[ 'bloomStrength' ].value = this.strength;
  160. this.compositeMaterial.uniforms[ 'bloomRadius' ].value = this.radius;
  161. this.compositeMaterial.uniforms[ 'bloomTintColors' ].value = this.bloomTintColors;
  162. renderer.setRenderTarget( this.renderTargetsHorizontal[ 0 ] );
  163. renderer.clear();
  164. this.fsQuad.render( renderer ); // Blend it additively over the input texture
  165. this.fsQuad.material = this.materialCopy;
  166. this.copyUniforms[ 'tDiffuse' ].value = this.renderTargetsHorizontal[ 0 ].texture;
  167. if ( maskActive ) renderer.state.buffers.stencil.setTest( true );
  168. if ( this.renderToScreen ) {
  169. renderer.setRenderTarget( null );
  170. this.fsQuad.render( renderer );
  171. } else {
  172. renderer.setRenderTarget( readBuffer );
  173. this.fsQuad.render( renderer );
  174. } // Restore renderer settings
  175. renderer.setClearColor( this._oldClearColor, this.oldClearAlpha );
  176. renderer.autoClear = oldAutoClear;
  177. }
  178. getSeperableBlurMaterial( kernelRadius ) {
  179. return new THREE.ShaderMaterial( {
  180. defines: {
  181. 'KERNEL_RADIUS': kernelRadius,
  182. 'SIGMA': kernelRadius
  183. },
  184. uniforms: {
  185. 'colorTexture': {
  186. value: null
  187. },
  188. 'texSize': {
  189. value: new THREE.Vector2( 0.5, 0.5 )
  190. },
  191. 'direction': {
  192. value: new THREE.Vector2( 0.5, 0.5 )
  193. }
  194. },
  195. vertexShader: `varying vec2 vUv;
  196. void main() {
  197. vUv = uv;
  198. gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );
  199. }`,
  200. fragmentShader: `#include <common>
  201. varying vec2 vUv;
  202. uniform sampler2D colorTexture;
  203. uniform vec2 texSize;
  204. uniform vec2 direction;
  205. float gaussianPdf(in float x, in float sigma) {
  206. return 0.39894 * exp( -0.5 * x * x/( sigma * sigma))/sigma;
  207. }
  208. void main() {
  209. vec2 invSize = 1.0 / texSize;
  210. float fSigma = float(SIGMA);
  211. float weightSum = gaussianPdf(0.0, fSigma);
  212. vec3 diffuseSum = texture2D( colorTexture, vUv).rgb * weightSum;
  213. for( int i = 1; i < KERNEL_RADIUS; i ++ ) {
  214. float x = float(i);
  215. float w = gaussianPdf(x, fSigma);
  216. vec2 uvOffset = direction * invSize * x;
  217. vec3 sample1 = texture2D( colorTexture, vUv + uvOffset).rgb;
  218. vec3 sample2 = texture2D( colorTexture, vUv - uvOffset).rgb;
  219. diffuseSum += (sample1 + sample2) * w;
  220. weightSum += 2.0 * w;
  221. }
  222. gl_FragColor = vec4(diffuseSum/weightSum, 1.0);
  223. }`
  224. } );
  225. }
  226. getCompositeMaterial( nMips ) {
  227. return new THREE.ShaderMaterial( {
  228. defines: {
  229. 'NUM_MIPS': nMips
  230. },
  231. uniforms: {
  232. 'blurTexture1': {
  233. value: null
  234. },
  235. 'blurTexture2': {
  236. value: null
  237. },
  238. 'blurTexture3': {
  239. value: null
  240. },
  241. 'blurTexture4': {
  242. value: null
  243. },
  244. 'blurTexture5': {
  245. value: null
  246. },
  247. 'bloomStrength': {
  248. value: 1.0
  249. },
  250. 'bloomFactors': {
  251. value: null
  252. },
  253. 'bloomTintColors': {
  254. value: null
  255. },
  256. 'bloomRadius': {
  257. value: 0.0
  258. }
  259. },
  260. vertexShader: `varying vec2 vUv;
  261. void main() {
  262. vUv = uv;
  263. gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );
  264. }`,
  265. fragmentShader: `varying vec2 vUv;
  266. uniform sampler2D blurTexture1;
  267. uniform sampler2D blurTexture2;
  268. uniform sampler2D blurTexture3;
  269. uniform sampler2D blurTexture4;
  270. uniform sampler2D blurTexture5;
  271. uniform float bloomStrength;
  272. uniform float bloomRadius;
  273. uniform float bloomFactors[NUM_MIPS];
  274. uniform vec3 bloomTintColors[NUM_MIPS];
  275. float lerpBloomFactor(const in float factor) {
  276. float mirrorFactor = 1.2 - factor;
  277. return mix(factor, mirrorFactor, bloomRadius);
  278. }
  279. void main() {
  280. gl_FragColor = bloomStrength * ( lerpBloomFactor(bloomFactors[0]) * vec4(bloomTintColors[0], 1.0) * texture2D(blurTexture1, vUv) +
  281. lerpBloomFactor(bloomFactors[1]) * vec4(bloomTintColors[1], 1.0) * texture2D(blurTexture2, vUv) +
  282. lerpBloomFactor(bloomFactors[2]) * vec4(bloomTintColors[2], 1.0) * texture2D(blurTexture3, vUv) +
  283. lerpBloomFactor(bloomFactors[3]) * vec4(bloomTintColors[3], 1.0) * texture2D(blurTexture4, vUv) +
  284. lerpBloomFactor(bloomFactors[4]) * vec4(bloomTintColors[4], 1.0) * texture2D(blurTexture5, vUv) );
  285. }`
  286. } );
  287. }
  288. }
  289. UnrealBloomPass.BlurDirectionX = new THREE.Vector2( 1.0, 0.0 );
  290. UnrealBloomPass.BlurDirectionY = new THREE.Vector2( 0.0, 1.0 );
  291. THREE.UnrealBloomPass = UnrealBloomPass;
  292. } )();