UnrealBloomPass.js 12 KB

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