UnrealBloomPass.js 11 KB

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