UnrealBloomPass.js 12 KB

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