Skip to content

Commit d7186e7

Browse files
authored
UnrealBloomPass: Fix alpha handling. (#32517)
1 parent f3bb889 commit d7186e7

1 file changed

Lines changed: 53 additions & 19 deletions

File tree

examples/jsm/postprocessing/UnrealBloomPass.js

Lines changed: 53 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
import {
2-
AdditiveBlending,
32
Color,
3+
CustomBlending,
44
HalfFloatType,
55
MeshBasicMaterial,
6+
OneFactor,
67
ShaderMaterial,
78
UniformsUtils,
89
Vector2,
@@ -187,7 +188,11 @@ class UnrealBloomPass extends Pass {
187188
uniforms: this.copyUniforms,
188189
vertexShader: CopyShader.vertexShader,
189190
fragmentShader: CopyShader.fragmentShader,
190-
blending: AdditiveBlending,
191+
blending: CustomBlending,
192+
blendSrc: OneFactor,
193+
blendDst: OneFactor,
194+
blendSrcAlpha: OneFactor,
195+
blendDstAlpha: OneFactor,
191196
depthTest: false,
192197
depthWrite: false,
193198
transparent: true
@@ -399,33 +404,46 @@ class UnrealBloomPass extends Pass {
399404
'gaussianCoefficients': { value: coefficients } // precomputed Gaussian coefficients
400405
},
401406

402-
vertexShader:
403-
`varying vec2 vUv;
407+
vertexShader: /* glsl */`
408+
409+
varying vec2 vUv;
410+
404411
void main() {
412+
405413
vUv = uv;
406414
gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );
415+
407416
}`,
408417

409-
fragmentShader:
410-
`#include <common>
418+
fragmentShader: /* glsl */`
419+
420+
#include <common>
421+
411422
varying vec2 vUv;
423+
412424
uniform sampler2D colorTexture;
413425
uniform vec2 invSize;
414426
uniform vec2 direction;
415427
uniform float gaussianCoefficients[KERNEL_RADIUS];
416428
417429
void main() {
430+
418431
float weightSum = gaussianCoefficients[0];
419432
vec3 diffuseSum = texture2D( colorTexture, vUv ).rgb * weightSum;
420-
for( int i = 1; i < KERNEL_RADIUS; i ++ ) {
421-
float x = float(i);
433+
434+
for ( int i = 1; i < KERNEL_RADIUS; i ++ ) {
435+
436+
float x = float( i );
422437
float w = gaussianCoefficients[i];
423438
vec2 uvOffset = direction * invSize * x;
424439
vec3 sample1 = texture2D( colorTexture, vUv + uvOffset ).rgb;
425440
vec3 sample2 = texture2D( colorTexture, vUv - uvOffset ).rgb;
426441
diffuseSum += ( sample1 + sample2 ) * w;
442+
427443
}
444+
428445
gl_FragColor = vec4( diffuseSum, 1.0 );
446+
429447
}`
430448
} );
431449

@@ -451,15 +469,21 @@ class UnrealBloomPass extends Pass {
451469
'bloomRadius': { value: 0.0 }
452470
},
453471

454-
vertexShader:
455-
`varying vec2 vUv;
472+
vertexShader: /* glsl */`
473+
474+
varying vec2 vUv;
475+
456476
void main() {
477+
457478
vUv = uv;
458479
gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );
480+
459481
}`,
460482

461-
fragmentShader:
462-
`varying vec2 vUv;
483+
fragmentShader: /* glsl */`
484+
485+
varying vec2 vUv;
486+
463487
uniform sampler2D blurTexture1;
464488
uniform sampler2D blurTexture2;
465489
uniform sampler2D blurTexture3;
@@ -470,17 +494,27 @@ class UnrealBloomPass extends Pass {
470494
uniform float bloomFactors[NUM_MIPS];
471495
uniform vec3 bloomTintColors[NUM_MIPS];
472496
473-
float lerpBloomFactor(const in float factor) {
497+
float lerpBloomFactor( const in float factor ) {
498+
474499
float mirrorFactor = 1.2 - factor;
475-
return mix(factor, mirrorFactor, bloomRadius);
500+
return mix( factor, mirrorFactor, bloomRadius );
501+
476502
}
477503
478504
void main() {
479-
gl_FragColor = bloomStrength * ( lerpBloomFactor(bloomFactors[0]) * vec4(bloomTintColors[0], 1.0) * texture2D(blurTexture1, vUv) +
480-
lerpBloomFactor(bloomFactors[1]) * vec4(bloomTintColors[1], 1.0) * texture2D(blurTexture2, vUv) +
481-
lerpBloomFactor(bloomFactors[2]) * vec4(bloomTintColors[2], 1.0) * texture2D(blurTexture3, vUv) +
482-
lerpBloomFactor(bloomFactors[3]) * vec4(bloomTintColors[3], 1.0) * texture2D(blurTexture4, vUv) +
483-
lerpBloomFactor(bloomFactors[4]) * vec4(bloomTintColors[4], 1.0) * texture2D(blurTexture5, vUv) );
505+
506+
// 3.0 for backwards compatibility with previous alpha-based intensity
507+
vec3 bloom = 3.0 * bloomStrength * (
508+
lerpBloomFactor( bloomFactors[ 0 ] ) * bloomTintColors[ 0 ] * texture2D( blurTexture1, vUv ).rgb +
509+
lerpBloomFactor( bloomFactors[ 1 ] ) * bloomTintColors[ 1 ] * texture2D( blurTexture2, vUv ).rgb +
510+
lerpBloomFactor( bloomFactors[ 2 ] ) * bloomTintColors[ 2 ] * texture2D( blurTexture3, vUv ).rgb +
511+
lerpBloomFactor( bloomFactors[ 3 ] ) * bloomTintColors[ 3 ] * texture2D( blurTexture4, vUv ).rgb +
512+
lerpBloomFactor( bloomFactors[ 4 ] ) * bloomTintColors[ 4 ] * texture2D( blurTexture5, vUv ).rgb
513+
);
514+
515+
float bloomAlpha = max( bloom.r, max( bloom.g, bloom.b ) );
516+
gl_FragColor = vec4( bloom, bloomAlpha );
517+
484518
}`
485519
} );
486520

0 commit comments

Comments
 (0)