Skip to content

Commit f38e2fd

Browse files
authored
Fix blur background halo effect (#415)
1 parent 8195a95 commit f38e2fd

File tree

2 files changed

+44
-5
lines changed

2 files changed

+44
-5
lines changed

data/effects/kawase_blur.effect

+40-1
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,10 @@ float4 PSKawaseFocalBlur(VertDataOut v_in) : TARGET
7171
return sum;
7272
}
7373

74+
/**
75+
* Standard Kawase blur
76+
* While it's not being used, we keep it here for reference.
77+
*/
7478
float4 PSKawaseBlur(VertDataOut v_in) : TARGET
7579
{
7680
// Calculate the blur value from neighboring pixels
@@ -83,6 +87,41 @@ float4 PSKawaseBlur(VertDataOut v_in) : TARGET
8387
return sum;
8488
}
8589

90+
/**
91+
* Mask aware Kawase blur
92+
* Only uses pixels which are in the masked area for blur. This prevents the "Halo Effect" on
93+
* the border pixels of the mask.
94+
*/
95+
float4 PSKawaseBlurMaskAware(VertDataOut v_in) : TARGET
96+
{
97+
if (focalmask.Sample(textureSampler, v_in.uv).r == 0) {
98+
// No mask - return the original image value without any blur
99+
return image.Sample(textureSampler, v_in.uv);
100+
}
101+
102+
// Calculate the blur value from neighboring pixels
103+
104+
float alphaValue1 = focalmask.Sample(textureSampler, v_in.uv + float2( xOffset, yOffset)).r;
105+
float4 sum = image.Sample(textureSampler, v_in.uv + float2( xOffset, yOffset)) * alphaValue1;
106+
float pixelCounter = alphaValue1;
107+
108+
float alphaValue2 = focalmask.Sample(textureSampler, v_in.uv + float2(-xOffset, yOffset)).r;
109+
sum += image.Sample(textureSampler, v_in.uv + float2(-xOffset, yOffset)) * alphaValue2;
110+
pixelCounter += alphaValue2;
111+
112+
float alphaValue3 = focalmask.Sample(textureSampler, v_in.uv + float2( xOffset, -yOffset)).r;
113+
sum += image.Sample(textureSampler, v_in.uv + float2( xOffset, -yOffset)) * alphaValue3;
114+
pixelCounter += alphaValue3;
115+
116+
float alphaValue4 = focalmask.Sample(textureSampler, v_in.uv + float2(-xOffset, -yOffset)).r;
117+
sum += image.Sample(textureSampler, v_in.uv + float2(-xOffset, -yOffset)) * alphaValue4;
118+
pixelCounter += alphaValue4;
119+
120+
// Complement the blur pixels with a relative fraction of the center pixel
121+
return (sum + image.Sample(textureSampler, v_in.uv) * (4.0 - pixelCounter)) * 0.25;
122+
}
123+
124+
86125
technique DrawFocalBlur
87126
{
88127
pass
@@ -97,6 +136,6 @@ technique Draw
97136
pass
98137
{
99138
vertex_shader = VSDefault(v_in);
100-
pixel_shader = PSKawaseBlur(v_in);
139+
pixel_shader = PSKawaseBlurMaskAware(v_in);
101140
}
102141
}

data/effects/mask_alpha_filter.effect

+4-4
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,9 @@ float4 PSAlphaMaskRGBAWithBlur(VertDataOut v_in) : TARGET
4040
return outputRGBA;
4141
}
4242

43-
float4 PSAlphaMaskRGBAWithFocalBlur(VertDataOut v_in) : TARGET
43+
float4 PSTakeBlur(VertDataOut v_in) : TARGET
4444
{
45-
// Return the blurred image, the focal mask is already applied to the blurred image
45+
// Return the blurred image, assume any masking is already applied to the blurred image
4646
return float4(blurredBackground.Sample(textureSampler, v_in.uv).rgb, 1.0);
4747
}
4848

@@ -63,7 +63,7 @@ technique DrawWithBlur
6363
pass
6464
{
6565
vertex_shader = VSDefault(v_in);
66-
pixel_shader = PSAlphaMaskRGBAWithBlur(v_in);
66+
pixel_shader = PSTakeBlur(v_in);
6767
}
6868
}
6969

@@ -72,7 +72,7 @@ technique DrawWithFocalBlur
7272
pass
7373
{
7474
vertex_shader = VSDefault(v_in);
75-
pixel_shader = PSAlphaMaskRGBAWithFocalBlur(v_in);
75+
pixel_shader = PSTakeBlur(v_in);
7676
}
7777
}
7878

0 commit comments

Comments
 (0)