Skip to content

Commit d0e7a8a

Browse files
authored
Feature: Add BackdropBlurFilter (#446)
1 parent c324261 commit d0e7a8a

8 files changed

+147
-0
lines changed

examples/src/DemoApplication.mjs

+1
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ export default class DemoApplication extends PIXI.Application
7171
height: this.initHeight,
7272
autoStart: false,
7373
preference,
74+
useBackBuffer: true,
7475
});
7576
}
7677

examples/src/filters/backdropBlur.mjs

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
export default function ()
2+
{
3+
this.addFilter('BackdropBlurFilter', {
4+
fishOnly: true,
5+
oncreate(folder)
6+
{
7+
folder.add(this, 'blur', 0, 100);
8+
folder.add(this, 'quality', 1, 10);
9+
},
10+
});
11+
}

examples/src/filters/index.mjs

+1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ export { default as adjustment } from './adjustment.mjs';
44
export { default as advancedBloom } from './advanced-bloom.mjs';
55
export { default as alpha } from './alpha.mjs';
66
export { default as ascii } from './ascii.mjs';
7+
export { default as backdropBlur } from './backdropBlur.mjs';
78
export { default as bevel } from './bevel.mjs';
89
export { default as bloom } from './bloom.mjs';
910
export { default as blur } from './blur.mjs';
+93
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
import {
2+
BlurFilter,
3+
BlurFilterOptions,
4+
Filter,
5+
FilterSystem,
6+
GlProgram,
7+
GpuProgram,
8+
RenderSurface,
9+
Texture,
10+
TexturePool,
11+
} from 'pixi.js';
12+
import { vertex, wgslVertex } from '../defaults';
13+
import fragment from './backdrop-blur-blend.frag';
14+
import wgslFragment from './backdrop-blur-blend.wgsl';
15+
16+
export type BackdropBlurFilterOptions = BlurFilterOptions;
17+
18+
/**
19+
* The BackdropBlurFilter applies a Gaussian blur to everything behind an object, and then draws the object on top of it.
20+
*
21+
* @class
22+
* @extends BlurFilter
23+
* @see {@link https://www.npmjs.com/package/pixi-filters|pixi-filters}
24+
*/
25+
export class BackdropBlurFilter extends BlurFilter
26+
{
27+
private _blendPass: Filter;
28+
29+
/**
30+
* @param options - The options of the blur filter.
31+
* @param options.strength - The strength of the blur filter.
32+
* @param options.quality - The quality of the blur filter.
33+
* @param options.kernelSize - The kernelSize of the blur filter.Options: 5, 7, 9, 11, 13, 15.
34+
*/
35+
constructor(options?: BackdropBlurFilterOptions)
36+
{
37+
super(options);
38+
39+
this.blendRequired = true;
40+
this.padding = 0;
41+
42+
this._blendPass = new Filter({
43+
gpuProgram: GpuProgram.from({
44+
vertex: {
45+
source: wgslVertex,
46+
entryPoint: 'mainVertex',
47+
},
48+
fragment: {
49+
source: wgslFragment,
50+
entryPoint: 'mainFragment',
51+
},
52+
}),
53+
glProgram: GlProgram.from({
54+
vertex,
55+
fragment,
56+
name: 'drop-shadow-filter',
57+
}),
58+
resources: {
59+
uBackground: Texture.EMPTY,
60+
},
61+
});
62+
}
63+
64+
/**
65+
* Override existing apply method in `Filter`
66+
* @override
67+
* @ignore
68+
*/
69+
public apply(
70+
filterManager: FilterSystem,
71+
input: Texture,
72+
output: RenderSurface,
73+
clearMode: boolean
74+
): void
75+
{
76+
// @ts-expect-error - this should probably not be grabbed from a private property
77+
const backTexture = filterManager._activeFilterData.backTexture;
78+
79+
const blurredBackground = TexturePool.getSameSizeTexture(input);
80+
81+
super.apply(filterManager, backTexture, blurredBackground, true);
82+
83+
this._blendPass.resources.uBackground = blurredBackground.source;
84+
this._blendPass.apply(filterManager, input, output, clearMode);
85+
86+
TexturePool.returnTexture(blurredBackground);
87+
}
88+
89+
protected updatePadding(): void
90+
{
91+
this.padding = 0;
92+
}
93+
}
+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
precision highp float;
2+
in vec2 vTextureCoord;
3+
out vec4 finalColor;
4+
5+
uniform sampler2D uTexture;
6+
uniform sampler2D uBackground;
7+
8+
void main(void){
9+
vec4 front = texture(uTexture, vTextureCoord);
10+
vec4 back = texture(uBackground, vTextureCoord);
11+
12+
if (front.a == 0.0) {
13+
discard;
14+
}
15+
16+
vec3 color = mix(back.rgb, front.rgb / front.a, front.a);
17+
18+
finalColor = vec4(color, 1.0);
19+
}
+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
@group(0) @binding(1) var uTexture: texture_2d<f32>;
2+
@group(0) @binding(2) var uSampler: sampler;
3+
@group(1) @binding(0) var uBackground: texture_2d<f32>;
4+
5+
@fragment
6+
fn mainFragment(
7+
@builtin(position) position: vec4<f32>,
8+
@location(0) uv : vec2<f32>
9+
) -> @location(0) vec4<f32> {
10+
var front: vec4<f32> = textureSample(uTexture, uSampler, uv);
11+
var back: vec4<f32> = textureSample(uBackground, uSampler, uv);
12+
13+
if (front.a == 0.0) {
14+
discard;
15+
}
16+
17+
var color: vec3<f32> = mix(back.rgb, front.rgb / front.a, front.a);
18+
19+
return vec4<f32>(color, 1.0);
20+
}

src/backdrop-blur/index.ts

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export * from './BackdropBlurFilter';

src/index.ts

+1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
export * from './adjustment';
22
export * from './advanced-bloom';
33
export * from './ascii';
4+
export * from './backdrop-blur';
45
export * from './bevel';
56
export * from './bloom';
67
export * from './bulge-pinch';

0 commit comments

Comments
 (0)