| 1 | import { |
| 2 | AdditiveBlending, |
| 3 | Color, |
| 4 | HalfFloatType, |
| 5 | MeshBasicMaterial, |
| 6 | ShaderMaterial, |
| 7 | UniformsUtils, |
| 8 | Vector2, |
| 9 | Vector3, |
| 10 | WebGLRenderTarget |
| 11 | } from 'three'; |
| 12 | import { Pass, FullScreenQuad } from './Pass.js'; |
| 13 | import { CopyShader } from '../shaders/CopyShader.js'; |
| 14 | import { LuminosityHighPassShader } from '../shaders/LuminosityHighPassShader.js'; |
| 15 | |
| 16 | /** |
| 17 | * UnrealBloomPass is inspired by the bloom pass of Unreal Engine. It creates a |
| 18 | * mip map chain of bloom textures and blurs them with different radii. Because |
| 19 | * of the weighted combination of mips, and because larger blurs are done on |
| 20 | * higher mips, this effect provides good quality and performance. |
| 21 | * |
| 22 | * Reference: |
| 23 | * - https://docs.unrealengine.com/latest/INT/Engine/Rendering/PostProcessEffects/Bloom/ |
| 24 | */ |
| 25 | class UnrealBloomPass extends Pass { |
| 26 | |
| 27 | constructor( resolution, strength, radius, threshold ) { |
| 28 | |
| 29 | super(); |
| 30 | |
| 31 | this.strength = ( strength !== undefined ) ? strength : 1; |
| 32 | this.radius = radius; |
| 33 | this.threshold = threshold; |
| 34 | this.resolution = ( resolution !== undefined ) ? new Vector2( resolution.x, resolution.y ) : new Vector2( 256, 256 ); |
| 35 | |
| 36 | // create color only once here, reuse it later inside the render function |
| 37 | this.clearColor = new Color( 0, 0, 0 ); |
| 38 | |
| 39 | // render targets |
| 40 | this.renderTargetsHorizontal = []; |
| 41 | this.renderTargetsVertical = []; |
| 42 | this.nMips = 5; |
| 43 | let resx = Math.round( this.resolution.x / 2 ); |
| 44 | let resy = Math.round( this.resolution.y / 2 ); |
| 45 | |
| 46 | this.renderTargetBright = new WebGLRenderTarget( resx, resy, { type: HalfFloatType } ); |
| 47 | this.renderTargetBright.texture.name = 'UnrealBloomPass.bright'; |
| 48 | this.renderTargetBright.texture.generateMipmaps = false; |
| 49 | |
| 50 | for ( let i = 0; i < this.nMips; i ++ ) { |
| 51 | |
| 52 | const renderTargetHorizonal = new WebGLRenderTarget( resx, resy, { type: HalfFloatType } ); |
| 53 | |
| 54 | renderTargetHorizonal.texture.name = 'UnrealBloomPass.h' + i; |
| 55 | renderTargetHorizonal.texture.generateMipmaps = false; |
| 56 | |
| 57 | this.renderTargetsHorizontal.push( renderTargetHorizonal ); |
| 58 | |
| 59 | const renderTargetVertical = new WebGLRenderTarget( resx, resy, { type: HalfFloatType } ); |
| 60 | |
| 61 | renderTargetVertical.texture.name = 'UnrealBloomPass.v' + i; |
| 62 | renderTargetVertical.texture.generateMipmaps = false; |
| 63 | |
| 64 | this.renderTargetsVertical.push( renderTargetVertical ); |
| 65 | |
| 66 | resx = Math.round( resx / 2 ); |
| 67 | |
| 68 | resy = Math.round( resy / 2 ); |
| 69 | |
| 70 | } |
| 71 | |
| 72 | // luminosity high pass material |
| 73 | |
| 74 | const highPassShader = LuminosityHighPassShader; |
| 75 | this.highPassUniforms = UniformsUtils.clone( highPassShader.uniforms ); |
| 76 | |
| 77 | this.highPassUniforms[ 'luminosityThreshold' ].value = threshold; |
| 78 | this.highPassUniforms[ 'smoothWidth' ].value = 0.01; |
| 79 | |
| 80 | this.materialHighPassFilter = new ShaderMaterial( { |
| 81 | uniforms: this.highPassUniforms, |
| 82 | vertexShader: highPassShader.vertexShader, |
| 83 | fragmentShader: highPassShader.fragmentShader |
| 84 | } ); |
| 85 | |
| 86 | // gaussian blur materials |
| 87 | |
| 88 | this.separableBlurMaterials = []; |
| 89 | const kernelSizeArray = [ 3, 5, 7, 9, 11 ]; |
| 90 | resx = Math.round( this.resolution.x / 2 ); |
| 91 | resy = Math.round( this.resolution.y / 2 ); |
| 92 | |
| 93 | for ( let i = 0; i < this.nMips; i ++ ) { |
| 94 | |
| 95 | this.separableBlurMaterials.push( this.getSeperableBlurMaterial( kernelSizeArray[ i ] ) ); |
| 96 | |
| 97 | this.separableBlurMaterials[ i ].uniforms[ 'invSize' ].value = new Vector2( 1 / resx, 1 / resy ); |
| 98 | |
| 99 | resx = Math.round( resx / 2 ); |
| 100 | |
| 101 | resy = Math.round( resy / 2 ); |
| 102 | |
| 103 | } |
| 104 | |
| 105 | // composite material |
| 106 | |
| 107 | this.compositeMaterial = this.getCompositeMaterial( this.nMips ); |
| 108 | this.compositeMaterial.uniforms[ 'blurTexture1' ].value = this.renderTargetsVertical[ 0 ].texture; |
| 109 | this.compositeMaterial.uniforms[ 'blurTexture2' ].value = this.renderTargetsVertical[ 1 ].texture; |
| 110 | this.compositeMaterial.uniforms[ 'blurTexture3' ].value = this.renderTargetsVertical[ 2 ].texture; |
| 111 | this.compositeMaterial.uniforms[ 'blurTexture4' ].value = this.renderTargetsVertical[ 3 ].texture; |
| 112 | this.compositeMaterial.uniforms[ 'blurTexture5' ].value = this.renderTargetsVertical[ 4 ].texture; |
| 113 | this.compositeMaterial.uniforms[ 'bloomStrength' ].value = strength; |
| 114 | this.compositeMaterial.uniforms[ 'bloomRadius' ].value = 0.1; |
| 115 | |
| 116 | const bloomFactors = [ 1.0, 0.8, 0.6, 0.4, 0.2 ]; |
| 117 | this.compositeMaterial.uniforms[ 'bloomFactors' ].value = bloomFactors; |
| 118 | this.bloomTintColors = [ new Vector3( 1, 1, 1 ), new Vector3( 1, 1, 1 ), new Vector3( 1, 1, 1 ), new Vector3( 1, 1, 1 ), new Vector3( 1, 1, 1 ) ]; |
| 119 | this.compositeMaterial.uniforms[ 'bloomTintColors' ].value = this.bloomTintColors; |
| 120 | |
| 121 | // blend material |
| 122 | |
| 123 | const copyShader = CopyShader; |
| 124 | |
| 125 | this.copyUniforms = UniformsUtils.clone( copyShader.uniforms ); |
| 126 | |
| 127 | this.blendMaterial = new ShaderMaterial( { |
| 128 | uniforms: this.copyUniforms, |
| 129 | vertexShader: copyShader.vertexShader, |
| 130 | fragmentShader: copyShader.fragmentShader, |
| 131 | blending: AdditiveBlending, |
| 132 | depthTest: false, |
| 133 | depthWrite: false, |
| 134 | transparent: true |
| 135 | } ); |
| 136 | |
| 137 | this.enabled = true; |
| 138 | this.needsSwap = false; |
| 139 | |
| 140 | this._oldClearColor = new Color(); |
| 141 | this.oldClearAlpha = 1; |
| 142 | |
| 143 | this.basic = new MeshBasicMaterial(); |
| 144 | |
| 145 | this.fsQuad = new FullScreenQuad( null ); |
| 146 | |
| 147 | } |
| 148 | |
| 149 | dispose() { |
| 150 | |
| 151 | for ( let i = 0; i < this.renderTargetsHorizontal.length; i ++ ) { |
| 152 | |
| 153 | this.renderTargetsHorizontal[ i ].dispose(); |
| 154 | |
| 155 | } |
| 156 | |
| 157 | for ( let i = 0; i < this.renderTargetsVertical.length; i ++ ) { |
| 158 | |
| 159 | this.renderTargetsVertical[ i ].dispose(); |
| 160 | |
| 161 | } |
| 162 | |
| 163 | this.renderTargetBright.dispose(); |
| 164 | |
| 165 | // |
| 166 | |
| 167 | for ( let i = 0; i < this.separableBlurMaterials.length; i ++ ) { |
| 168 | |
| 169 | this.separableBlurMaterials[ i ].dispose(); |
| 170 | |
| 171 | } |
| 172 | |
| 173 | this.compositeMaterial.dispose(); |
| 174 | this.blendMaterial.dispose(); |
| 175 | this.basic.dispose(); |
| 176 | |
| 177 | // |
| 178 | |
| 179 | this.fsQuad.dispose(); |
| 180 | |
| 181 | } |
| 182 | |
| 183 | setSize( width, height ) { |
| 184 | |
| 185 | let resx = Math.round( width / 2 ); |
| 186 | let resy = Math.round( height / 2 ); |
| 187 | |
| 188 | this.renderTargetBright.setSize( resx, resy ); |
| 189 | |
| 190 | for ( let i = 0; i < this.nMips; i ++ ) { |
| 191 | |
| 192 | this.renderTargetsHorizontal[ i ].setSize( resx, resy ); |
| 193 | this.renderTargetsVertical[ i ].setSize( resx, resy ); |
| 194 | |
| 195 | this.separableBlurMaterials[ i ].uniforms[ 'invSize' ].value = new Vector2( 1 / resx, 1 / resy ); |
| 196 | |
| 197 | resx = Math.round( resx / 2 ); |
| 198 | resy = Math.round( resy / 2 ); |
| 199 | |
| 200 | } |
| 201 | |
| 202 | } |
| 203 | |
| 204 | render( renderer, writeBuffer, readBuffer, deltaTime, maskActive ) { |
| 205 | |
| 206 | renderer.getClearColor( this._oldClearColor ); |
| 207 | this.oldClearAlpha = renderer.getClearAlpha(); |
| 208 | const oldAutoClear = renderer.autoClear; |
| 209 | renderer.autoClear = false; |
| 210 | |
| 211 | renderer.setClearColor( this.clearColor, 0 ); |
| 212 | |
| 213 | if ( maskActive ) renderer.state.buffers.stencil.setTest( false ); |
| 214 | |
| 215 | // Render input to screen |
| 216 | |
| 217 | if ( this.renderToScreen ) { |
| 218 | |
| 219 | this.fsQuad.material = this.basic; |
| 220 | this.basic.map = readBuffer.texture; |
| 221 | |
| 222 | renderer.setRenderTarget( null ); |
| 223 | renderer.clear(); |
| 224 | this.fsQuad.render( renderer ); |
| 225 | |
| 226 | } |
| 227 | |
| 228 | // 1. Extract Bright Areas |
| 229 | |
| 230 | this.highPassUniforms[ 'tDiffuse' ].value = readBuffer.texture; |
| 231 | this.highPassUniforms[ 'luminosityThreshold' ].value = this.threshold; |
| 232 | this.fsQuad.material = this.materialHighPassFilter; |
| 233 | |
| 234 | renderer.setRenderTarget( this.renderTargetBright ); |
| 235 | renderer.clear(); |
| 236 | this.fsQuad.render( renderer ); |
| 237 | |
| 238 | // 2. Blur All the mips progressively |
| 239 | |
| 240 | let inputRenderTarget = this.renderTargetBright; |
| 241 | |
| 242 | for ( let i = 0; i < this.nMips; i ++ ) { |
| 243 | |
| 244 | this.fsQuad.material = this.separableBlurMaterials[ i ]; |
| 245 | |
| 246 | this.separableBlurMaterials[ i ].uniforms[ 'colorTexture' ].value = inputRenderTarget.texture; |
| 247 | this.separableBlurMaterials[ i ].uniforms[ 'direction' ].value = UnrealBloomPass.BlurDirectionX; |
| 248 | renderer.setRenderTarget( this.renderTargetsHorizontal[ i ] ); |
| 249 | renderer.clear(); |
| 250 | this.fsQuad.render( renderer ); |
| 251 | |
| 252 | this.separableBlurMaterials[ i ].uniforms[ 'colorTexture' ].value = this.renderTargetsHorizontal[ i ].texture; |
| 253 | this.separableBlurMaterials[ i ].uniforms[ 'direction' ].value = UnrealBloomPass.BlurDirectionY; |
| 254 | renderer.setRenderTarget( this.renderTargetsVertical[ i ] ); |
| 255 | renderer.clear(); |
| 256 | this.fsQuad.render( renderer ); |
| 257 | |
| 258 | inputRenderTarget = this.renderTargetsVertical[ i ]; |
| 259 | |
| 260 | } |
| 261 | |
| 262 | // Composite All the mips |
| 263 | |
| 264 | this.fsQuad.material = this.compositeMaterial; |
| 265 | this.compositeMaterial.uniforms[ 'bloomStrength' ].value = this.strength; |
| 266 | this.compositeMaterial.uniforms[ 'bloomRadius' ].value = this.radius; |
| 267 | this.compositeMaterial.uniforms[ 'bloomTintColors' ].value = this.bloomTintColors; |
| 268 | |
| 269 | renderer.setRenderTarget( this.renderTargetsHorizontal[ 0 ] ); |
| 270 | renderer.clear(); |
| 271 | this.fsQuad.render( renderer ); |
| 272 | |
| 273 | // Blend it additively over the input texture |
| 274 | |
| 275 | this.fsQuad.material = this.blendMaterial; |
| 276 | this.copyUniforms[ 'tDiffuse' ].value = this.renderTargetsHorizontal[ 0 ].texture; |
| 277 | |
| 278 | if ( maskActive ) renderer.state.buffers.stencil.setTest( true ); |
| 279 | |
| 280 | if ( this.renderToScreen ) { |
| 281 | |
| 282 | renderer.setRenderTarget( null ); |
| 283 | this.fsQuad.render( renderer ); |
| 284 | |
| 285 | } else { |
| 286 | |
| 287 | renderer.setRenderTarget( readBuffer ); |
| 288 | this.fsQuad.render( renderer ); |
| 289 | |
| 290 | } |
| 291 | |
| 292 | // Restore renderer settings |
| 293 | |
| 294 | renderer.setClearColor( this._oldClearColor, this.oldClearAlpha ); |
| 295 | renderer.autoClear = oldAutoClear; |
| 296 | |
| 297 | } |
| 298 | |
| 299 | getSeperableBlurMaterial( kernelRadius ) { |
| 300 | |
| 301 | const coefficients = []; |
| 302 | |
| 303 | for ( let i = 0; i < kernelRadius; i ++ ) { |
| 304 | |
| 305 | coefficients.push( 0.39894 * Math.exp( - 0.5 * i * i / ( kernelRadius * kernelRadius ) ) / kernelRadius ); |
| 306 | |
| 307 | } |
| 308 | |
| 309 | return new ShaderMaterial( { |
| 310 | |
| 311 | defines: { |
| 312 | 'KERNEL_RADIUS': kernelRadius |
| 313 | }, |
| 314 | |
| 315 | uniforms: { |
| 316 | 'colorTexture': { value: null }, |
| 317 | 'invSize': { value: new Vector2( 0.5, 0.5 ) }, // inverse texture size |
| 318 | 'direction': { value: new Vector2( 0.5, 0.5 ) }, |
| 319 | 'gaussianCoefficients': { value: coefficients } // precomputed Gaussian coefficients |
| 320 | }, |
| 321 | |
| 322 | vertexShader: |
| 323 | `varying vec2 vUv; |
| 324 | void main() { |
| 325 | vUv = uv; |
| 326 | gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 ); |
| 327 | }`, |
| 328 | |
| 329 | fragmentShader: |
| 330 | `#include <common> |
| 331 | varying vec2 vUv; |
| 332 | uniform sampler2D colorTexture; |
| 333 | uniform vec2 invSize; |
| 334 | uniform vec2 direction; |
| 335 | uniform float gaussianCoefficients[KERNEL_RADIUS]; |
| 336 | |
| 337 | void main() { |
| 338 | float weightSum = gaussianCoefficients[0]; |
| 339 | vec3 diffuseSum = texture2D( colorTexture, vUv ).rgb * weightSum; |
| 340 | for( int i = 1; i < KERNEL_RADIUS; i ++ ) { |
| 341 | float x = float(i); |
| 342 | float w = gaussianCoefficients[i]; |
| 343 | vec2 uvOffset = direction * invSize * x; |
| 344 | vec3 sample1 = texture2D( colorTexture, vUv + uvOffset ).rgb; |
| 345 | vec3 sample2 = texture2D( colorTexture, vUv - uvOffset ).rgb; |
| 346 | diffuseSum += (sample1 + sample2) * w; |
| 347 | weightSum += 2.0 * w; |
| 348 | } |
| 349 | gl_FragColor = vec4(diffuseSum/weightSum, 1.0); |
| 350 | }` |
| 351 | } ); |
| 352 | |
| 353 | } |
| 354 | |
| 355 | getCompositeMaterial( nMips ) { |
| 356 | |
| 357 | return new ShaderMaterial( { |
| 358 | |
| 359 | defines: { |
| 360 | 'NUM_MIPS': nMips |
| 361 | }, |
| 362 | |
| 363 | uniforms: { |
| 364 | 'blurTexture1': { value: null }, |
| 365 | 'blurTexture2': { value: null }, |
| 366 | 'blurTexture3': { value: null }, |
| 367 | 'blurTexture4': { value: null }, |
| 368 | 'blurTexture5': { value: null }, |
| 369 | 'bloomStrength': { value: 1.0 }, |
| 370 | 'bloomFactors': { value: null }, |
| 371 | 'bloomTintColors': { value: null }, |
| 372 | 'bloomRadius': { value: 0.0 } |
| 373 | }, |
| 374 | |
| 375 | vertexShader: |
| 376 | `varying vec2 vUv; |
| 377 | void main() { |
| 378 | vUv = uv; |
| 379 | gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 ); |
| 380 | }`, |
| 381 | |
| 382 | fragmentShader: |
| 383 | `varying vec2 vUv; |
| 384 | uniform sampler2D blurTexture1; |
| 385 | uniform sampler2D blurTexture2; |
| 386 | uniform sampler2D blurTexture3; |
| 387 | uniform sampler2D blurTexture4; |
| 388 | uniform sampler2D blurTexture5; |
| 389 | uniform float bloomStrength; |
| 390 | uniform float bloomRadius; |
| 391 | uniform float bloomFactors[NUM_MIPS]; |
| 392 | uniform vec3 bloomTintColors[NUM_MIPS]; |
| 393 | |
| 394 | float lerpBloomFactor(const in float factor) { |
| 395 | float mirrorFactor = 1.2 - factor; |
| 396 | return mix(factor, mirrorFactor, bloomRadius); |
| 397 | } |
| 398 | |
| 399 | void main() { |
| 400 | gl_FragColor = bloomStrength * ( lerpBloomFactor(bloomFactors[0]) * vec4(bloomTintColors[0], 1.0) * texture2D(blurTexture1, vUv) + |
| 401 | lerpBloomFactor(bloomFactors[1]) * vec4(bloomTintColors[1], 1.0) * texture2D(blurTexture2, vUv) + |
| 402 | lerpBloomFactor(bloomFactors[2]) * vec4(bloomTintColors[2], 1.0) * texture2D(blurTexture3, vUv) + |
| 403 | lerpBloomFactor(bloomFactors[3]) * vec4(bloomTintColors[3], 1.0) * texture2D(blurTexture4, vUv) + |
| 404 | lerpBloomFactor(bloomFactors[4]) * vec4(bloomTintColors[4], 1.0) * texture2D(blurTexture5, vUv) ); |
| 405 | }` |
| 406 | } ); |
| 407 | |
| 408 | } |
| 409 | |
| 410 | } |
| 411 | |
| 412 | UnrealBloomPass.BlurDirectionX = new Vector2( 1.0, 0.0 ); |
| 413 | UnrealBloomPass.BlurDirectionY = new Vector2( 0.0, 1.0 ); |
| 414 | |
| 415 | export { UnrealBloomPass }; |