UnrealBloomPass.js 12 KB

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