UnrealBloomPass.js 12 KB

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