UnrealBloomPass.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  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. THREE.UnrealBloomPass = function ( resolution, strength, radius, threshold ) {
  8. THREE.Pass.call( this );
  9. this.strength = ( strength !== undefined ) ? strength : 1;
  10. this.radius = radius;
  11. this.threshold = threshold;
  12. this.resolution = ( resolution !== undefined ) ? new THREE.Vector2( resolution.x, resolution.y ) : new THREE.Vector2( 256, 256 );
  13. // render targets
  14. var pars = { minFilter: THREE.LinearFilter, magFilter: THREE.LinearFilter, format: THREE.RGBAFormat };
  15. this.renderTargetsHorizontal = [];
  16. this.renderTargetsVertical = [];
  17. this.nMips = 5;
  18. var resx = Math.round( this.resolution.x / 2 );
  19. var resy = Math.round( this.resolution.y / 2 );
  20. this.renderTargetBright = new THREE.WebGLRenderTarget( resx, resy, pars );
  21. this.renderTargetBright.texture.name = "UnrealBloomPass.bright";
  22. this.renderTargetBright.texture.generateMipmaps = false;
  23. for ( var i = 0; i < this.nMips; i ++ ) {
  24. var renderTarget = new THREE.WebGLRenderTarget( resx, resy, pars );
  25. renderTarget.texture.name = "UnrealBloomPass.h" + i;
  26. renderTarget.texture.generateMipmaps = false;
  27. this.renderTargetsHorizontal.push( renderTarget );
  28. var renderTarget = new THREE.WebGLRenderTarget( resx, resy, pars );
  29. renderTarget.texture.name = "UnrealBloomPass.v" + i;
  30. renderTarget.texture.generateMipmaps = false;
  31. this.renderTargetsVertical.push( renderTarget );
  32. resx = Math.round( resx / 2 );
  33. resy = Math.round( resy / 2 );
  34. }
  35. // luminosity high pass material
  36. if ( THREE.LuminosityHighPassShader === undefined )
  37. console.error( "THREE.UnrealBloomPass relies on THREE.LuminosityHighPassShader" );
  38. var highPassShader = THREE.LuminosityHighPassShader;
  39. this.highPassUniforms = THREE.UniformsUtils.clone( highPassShader.uniforms );
  40. this.highPassUniforms[ "luminosityThreshold" ].value = threshold;
  41. this.highPassUniforms[ "smoothWidth" ].value = 0.01;
  42. this.materialHighPassFilter = new THREE.ShaderMaterial( {
  43. uniforms: this.highPassUniforms,
  44. vertexShader: highPassShader.vertexShader,
  45. fragmentShader: highPassShader.fragmentShader,
  46. defines: {}
  47. } );
  48. // Gaussian Blur Materials
  49. this.separableBlurMaterials = [];
  50. var kernelSizeArray = [ 3, 5, 7, 9, 11 ];
  51. var resx = Math.round( this.resolution.x / 2 );
  52. var resy = Math.round( this.resolution.y / 2 );
  53. for ( var i = 0; i < this.nMips; i ++ ) {
  54. this.separableBlurMaterials.push( this.getSeperableBlurMaterial( kernelSizeArray[ i ] ) );
  55. this.separableBlurMaterials[ i ].uniforms[ "texSize" ].value = new THREE.Vector2( resx, resy );
  56. resx = Math.round( resx / 2 );
  57. resy = Math.round( resy / 2 );
  58. }
  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. var 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 ),
  72. new THREE.Vector3( 1, 1, 1 ), new THREE.Vector3( 1, 1, 1 ) ];
  73. this.compositeMaterial.uniforms[ "bloomTintColors" ].value = this.bloomTintColors;
  74. // copy material
  75. if ( THREE.CopyShader === undefined ) {
  76. console.error( "THREE.BloomPass relies on THREE.CopyShader" );
  77. }
  78. var copyShader = THREE.CopyShader;
  79. this.copyUniforms = THREE.UniformsUtils.clone( copyShader.uniforms );
  80. this.copyUniforms[ "opacity" ].value = 1.0;
  81. this.materialCopy = new THREE.ShaderMaterial( {
  82. uniforms: this.copyUniforms,
  83. vertexShader: copyShader.vertexShader,
  84. fragmentShader: copyShader.fragmentShader,
  85. blending: THREE.AdditiveBlending,
  86. depthTest: false,
  87. depthWrite: false,
  88. transparent: true
  89. } );
  90. this.enabled = true;
  91. this.needsSwap = false;
  92. this.oldClearColor = new THREE.Color();
  93. this.oldClearAlpha = 1;
  94. this.camera = new THREE.OrthographicCamera( - 1, 1, 1, - 1, 0, 1 );
  95. this.scene = new THREE.Scene();
  96. this.basic = new THREE.MeshBasicMaterial();
  97. this.quad = new THREE.Mesh( new THREE.PlaneBufferGeometry( 2, 2 ), null );
  98. this.quad.frustumCulled = false; // Avoid getting clipped
  99. this.scene.add( this.quad );
  100. };
  101. THREE.UnrealBloomPass.prototype = Object.assign( Object.create( THREE.Pass.prototype ), {
  102. constructor: THREE.UnrealBloomPass,
  103. dispose: function () {
  104. for ( var i = 0; i < this.renderTargetsHorizontal.length; i ++ ) {
  105. this.renderTargetsHorizontal[ i ].dispose();
  106. }
  107. for ( var i = 0; i < this.renderTargetsVertical.length; i ++ ) {
  108. this.renderTargetsVertical[ i ].dispose();
  109. }
  110. this.renderTargetBright.dispose();
  111. },
  112. setSize: function ( width, height ) {
  113. var resx = Math.round( width / 2 );
  114. var resy = Math.round( height / 2 );
  115. this.renderTargetBright.setSize( resx, resy );
  116. for ( var i = 0; i < this.nMips; i ++ ) {
  117. this.renderTargetsHorizontal[ i ].setSize( resx, resy );
  118. this.renderTargetsVertical[ i ].setSize( resx, resy );
  119. this.separableBlurMaterials[ i ].uniforms[ "texSize" ].value = new THREE.Vector2( resx, resy );
  120. resx = Math.round( resx / 2 );
  121. resy = Math.round( resy / 2 );
  122. }
  123. },
  124. render: function ( renderer, writeBuffer, readBuffer, delta, maskActive ) {
  125. this.oldClearColor.copy( renderer.getClearColor() );
  126. this.oldClearAlpha = renderer.getClearAlpha();
  127. var oldAutoClear = renderer.autoClear;
  128. renderer.autoClear = false;
  129. renderer.setClearColor( new THREE.Color( 0, 0, 0 ), 0 );
  130. if ( maskActive ) renderer.context.disable( renderer.context.STENCIL_TEST );
  131. // Render input to screen
  132. if ( this.renderToScreen ) {
  133. this.quad.material = this.basic;
  134. this.basic.map = readBuffer.texture;
  135. renderer.render( this.scene, this.camera, undefined, true );
  136. }
  137. // 1. Extract Bright Areas
  138. this.highPassUniforms[ "tDiffuse" ].value = readBuffer.texture;
  139. this.highPassUniforms[ "luminosityThreshold" ].value = this.threshold;
  140. this.quad.material = this.materialHighPassFilter;
  141. renderer.render( this.scene, this.camera, this.renderTargetBright, true );
  142. // 2. Blur All the mips progressively
  143. var inputRenderTarget = this.renderTargetBright;
  144. for ( var i = 0; i < this.nMips; i ++ ) {
  145. this.quad.material = this.separableBlurMaterials[ i ];
  146. this.separableBlurMaterials[ i ].uniforms[ "colorTexture" ].value = inputRenderTarget.texture;
  147. this.separableBlurMaterials[ i ].uniforms[ "direction" ].value = THREE.UnrealBloomPass.BlurDirectionX;
  148. renderer.render( this.scene, this.camera, this.renderTargetsHorizontal[ i ], true );
  149. this.separableBlurMaterials[ i ].uniforms[ "colorTexture" ].value = this.renderTargetsHorizontal[ i ].texture;
  150. this.separableBlurMaterials[ i ].uniforms[ "direction" ].value = THREE.UnrealBloomPass.BlurDirectionY;
  151. renderer.render( this.scene, this.camera, this.renderTargetsVertical[ i ], true );
  152. inputRenderTarget = this.renderTargetsVertical[ i ];
  153. }
  154. // Composite All the mips
  155. this.quad.material = this.compositeMaterial;
  156. this.compositeMaterial.uniforms[ "bloomStrength" ].value = this.strength;
  157. this.compositeMaterial.uniforms[ "bloomRadius" ].value = this.radius;
  158. this.compositeMaterial.uniforms[ "bloomTintColors" ].value = this.bloomTintColors;
  159. renderer.render( this.scene, this.camera, this.renderTargetsHorizontal[ 0 ], true );
  160. // Blend it additively over the input texture
  161. this.quad.material = this.materialCopy;
  162. this.copyUniforms[ "tDiffuse" ].value = this.renderTargetsHorizontal[ 0 ].texture;
  163. if ( maskActive ) renderer.context.enable( renderer.context.STENCIL_TEST );
  164. if ( this.renderToScreen ) {
  165. renderer.render( this.scene, this.camera, undefined, false );
  166. } else {
  167. renderer.render( this.scene, this.camera, readBuffer, false );
  168. }
  169. // Restore renderer settings
  170. renderer.setClearColor( this.oldClearColor, this.oldClearAlpha );
  171. renderer.autoClear = oldAutoClear;
  172. },
  173. getSeperableBlurMaterial: function ( kernelRadius ) {
  174. return new THREE.ShaderMaterial( {
  175. defines: {
  176. "KERNEL_RADIUS": kernelRadius,
  177. "SIGMA": kernelRadius
  178. },
  179. uniforms: {
  180. "colorTexture": { value: null },
  181. "texSize": { value: new THREE.Vector2( 0.5, 0.5 ) },
  182. "direction": { value: new THREE.Vector2( 0.5, 0.5 ) }
  183. },
  184. vertexShader:
  185. "varying vec2 vUv;\n\
  186. void main() {\n\
  187. vUv = uv;\n\
  188. gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );\n\
  189. }",
  190. fragmentShader:
  191. "#include <common>\
  192. varying vec2 vUv;\n\
  193. uniform sampler2D colorTexture;\n\
  194. uniform vec2 texSize;\
  195. uniform vec2 direction;\
  196. \
  197. float gaussianPdf(in float x, in float sigma) {\
  198. return 0.39894 * exp( -0.5 * x * x/( sigma * sigma))/sigma;\
  199. }\
  200. void main() {\n\
  201. vec2 invSize = 1.0 / texSize;\
  202. float fSigma = float(SIGMA);\
  203. float weightSum = gaussianPdf(0.0, fSigma);\
  204. vec3 diffuseSum = texture2D( colorTexture, vUv).rgb * weightSum;\
  205. for( int i = 1; i < KERNEL_RADIUS; i ++ ) {\
  206. float x = float(i);\
  207. float w = gaussianPdf(x, fSigma);\
  208. vec2 uvOffset = direction * invSize * x;\
  209. vec3 sample1 = texture2D( colorTexture, vUv + uvOffset).rgb;\
  210. vec3 sample2 = texture2D( colorTexture, vUv - uvOffset).rgb;\
  211. diffuseSum += (sample1 + sample2) * w;\
  212. weightSum += 2.0 * w;\
  213. }\
  214. gl_FragColor = vec4(diffuseSum/weightSum, 1.0);\n\
  215. }"
  216. } );
  217. },
  218. getCompositeMaterial: function ( nMips ) {
  219. return new THREE.ShaderMaterial( {
  220. defines: {
  221. "NUM_MIPS": nMips
  222. },
  223. uniforms: {
  224. "blurTexture1": { value: null },
  225. "blurTexture2": { value: null },
  226. "blurTexture3": { value: null },
  227. "blurTexture4": { value: null },
  228. "blurTexture5": { value: null },
  229. "dirtTexture": { value: null },
  230. "bloomStrength": { value: 1.0 },
  231. "bloomFactors": { value: null },
  232. "bloomTintColors": { value: null },
  233. "bloomRadius": { value: 0.0 }
  234. },
  235. vertexShader:
  236. "varying vec2 vUv;\n\
  237. void main() {\n\
  238. vUv = uv;\n\
  239. gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );\n\
  240. }",
  241. fragmentShader:
  242. "varying vec2 vUv;\
  243. uniform sampler2D blurTexture1;\
  244. uniform sampler2D blurTexture2;\
  245. uniform sampler2D blurTexture3;\
  246. uniform sampler2D blurTexture4;\
  247. uniform sampler2D blurTexture5;\
  248. uniform sampler2D dirtTexture;\
  249. uniform float bloomStrength;\
  250. uniform float bloomRadius;\
  251. uniform float bloomFactors[NUM_MIPS];\
  252. uniform vec3 bloomTintColors[NUM_MIPS];\
  253. \
  254. float lerpBloomFactor(const in float factor) { \
  255. float mirrorFactor = 1.2 - factor;\
  256. return mix(factor, mirrorFactor, bloomRadius);\
  257. }\
  258. \
  259. void main() {\
  260. gl_FragColor = bloomStrength * ( lerpBloomFactor(bloomFactors[0]) * vec4(bloomTintColors[0], 1.0) * texture2D(blurTexture1, vUv) + \
  261. lerpBloomFactor(bloomFactors[1]) * vec4(bloomTintColors[1], 1.0) * texture2D(blurTexture2, vUv) + \
  262. lerpBloomFactor(bloomFactors[2]) * vec4(bloomTintColors[2], 1.0) * texture2D(blurTexture3, vUv) + \
  263. lerpBloomFactor(bloomFactors[3]) * vec4(bloomTintColors[3], 1.0) * texture2D(blurTexture4, vUv) + \
  264. lerpBloomFactor(bloomFactors[4]) * vec4(bloomTintColors[4], 1.0) * texture2D(blurTexture5, vUv) );\
  265. }"
  266. } );
  267. }
  268. } );
  269. THREE.UnrealBloomPass.BlurDirectionX = new THREE.Vector2( 1.0, 0.0 );
  270. THREE.UnrealBloomPass.BlurDirectionY = new THREE.Vector2( 0.0, 1.0 );