/** * @author spidersharma / http://eduperiment.com/ Inspired from Unreal Engine:: https://docs.unrealengine.com/latest/INT/Engine/Rendering/PostProcessEffects/Bloom/ */ THREE.UnrealBloomPass = function ( resolution, strength, radius, threshold ) { THREE.Pass.call( this ); this.strength = ( strength !== undefined ) ? strength : 1; this.radius = radius; this.threshold = threshold; this.resolution = ( resolution !== undefined ) ? new THREE.Vector2(resolution.x, resolution.y) : new THREE.Vector2(256, 256); // render targets var pars = { minFilter: THREE.LinearFilter, magFilter: THREE.LinearFilter, format: THREE.RGBAFormat }; this.renderTargetsHorizontal = []; this.renderTargetsVertical = []; this.nMips = 5; var resx = Math.round(this.resolution.x/2); var resy = Math.round(this.resolution.y/2); this.renderTargetBright = new THREE.WebGLRenderTarget( resx, resy, pars ); this.renderTargetBright.texture.generateMipmaps = false; for( var i=0; i\ varying vec2 vUv;\n\ uniform sampler2D colorTexture;\n\ uniform vec2 texSize;\ uniform vec2 direction;\ \ float gaussianPdf(in float x, in float sigma) {\ return 0.39894 * exp( -0.5 * x * x/( sigma * sigma))/sigma;\ }\ void main() {\n\ vec2 invSize = 1.0 / texSize;\ float fSigma = float(SIGMA);\ float weightSum = gaussianPdf(0.0, fSigma);\ vec3 diffuseSum = texture2D( colorTexture, vUv).rgb * weightSum;\ for( int i = 1; i < KERNEL_RADIUS; i ++ ) {\ float x = float(i);\ float w = gaussianPdf(x, fSigma);\ vec2 uvOffset = direction * invSize * x;\ vec3 sample1 = texture2D( colorTexture, vUv + uvOffset).rgb;\ vec3 sample2 = texture2D( colorTexture, vUv - uvOffset).rgb;\ diffuseSum += (sample1 + sample2) * w;\ weightSum += 2.0 * w;\ }\ gl_FragColor = vec4(diffuseSum/weightSum, 1.0);\n\ }" } ); }, getCompositeMaterial: function(nMips) { return new THREE.ShaderMaterial( { defines:{ "NUM_MIPS" : nMips }, uniforms: { "blurTexture1": { value: null }, "blurTexture2": { value: null }, "blurTexture3": { value: null }, "blurTexture4": { value: null }, "blurTexture5": { value: null }, "dirtTexture": { value: null }, "bloomStrength" : { value: 1.0 }, "bloomFactors" : { value: null }, "bloomTintColors" : { value: null }, "bloomRadius" : { value: 0.0 } }, vertexShader: "varying vec2 vUv;\n\ void main() {\n\ vUv = uv;\n\ gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );\n\ }", fragmentShader: "varying vec2 vUv;\ uniform sampler2D blurTexture1;\ uniform sampler2D blurTexture2;\ uniform sampler2D blurTexture3;\ uniform sampler2D blurTexture4;\ uniform sampler2D blurTexture5;\ uniform sampler2D dirtTexture;\ uniform float bloomStrength;\ uniform float bloomRadius;\ uniform float bloomFactors[NUM_MIPS];\ uniform vec3 bloomTintColors[NUM_MIPS];\ \ float lerpBloomFactor(const in float factor) { \ float mirrorFactor = 1.2 - factor;\ return mix(factor, mirrorFactor, bloomRadius);\ }\ \ void main() {\ gl_FragColor = bloomStrength * ( lerpBloomFactor(bloomFactors[0]) * vec4(bloomTintColors[0], 1.0) * texture2D(blurTexture1, vUv) + \ lerpBloomFactor(bloomFactors[1]) * vec4(bloomTintColors[1], 1.0) * texture2D(blurTexture2, vUv) + \ lerpBloomFactor(bloomFactors[2]) * vec4(bloomTintColors[2], 1.0) * texture2D(blurTexture3, vUv) + \ lerpBloomFactor(bloomFactors[3]) * vec4(bloomTintColors[3], 1.0) * texture2D(blurTexture4, vUv) + \ lerpBloomFactor(bloomFactors[4]) * vec4(bloomTintColors[4], 1.0) * texture2D(blurTexture5, vUv) );\ }" } ); } } ); THREE.UnrealBloomPass.BlurDirectionX = new THREE.Vector2( 1.0, 0.0 ); THREE.UnrealBloomPass.BlurDirectionY = new THREE.Vector2( 0.0, 1.0 );