UnrealBloomPass.js 12 KB

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