Skip to content

Commit 7ea4f01

Browse files
authored
Merge branch 'main' into bugfix/shock-wave-filter
2 parents eb92fc1 + 1a956f5 commit 7ea4f01

12 files changed

+228
-24
lines changed

README.md

+2
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ If all else failes, you can manually download the bundled file from the [release
3939
| **AdjustmentFilter**<br>_pixi-filters/adjustment_<br>[View demo][Adjustment_demo] | ![adjustment](https://filters.pixijs.download/main/screenshots/adjustment.png?v=3) |
4040
| **AdvancedBloomFilter**<br>_pixi-filters/advanced-bloom_<br>[View demo][AdvancedBloom_demo] | ![advanced-bloom](https://filters.pixijs.download/main/screenshots/advanced-bloom.png?v=3) |
4141
| **AsciiFilter**<br>_pixi-filters/ascii_<br>[View demo][Ascii_demo] | ![ascii](https://filters.pixijs.download/main/screenshots/ascii.png?v=3) |
42+
| **BackdropBlurFilter**<br>_pixi-filters/backdrop-blur_<br>[View demo][BackdropBlur_demo] | ![backdrop-blur](https://filters.pixijs.download/main/screenshots/backdrop-blur.png?v=3) |
4243
| **BevelFilter**<br>_pixi-filters/bevel_<br>[View demo][Bevel_demo] | ![bevel](https://filters.pixijs.download/main/screenshots/bevel.png?v=3) |
4344
| **BloomFilter**<br>_pixi-filters/bloom_<br>[View demo][Bloom_demo] | ![bloom](https://filters.pixijs.download/main/screenshots/bloom.png?v=3) |
4445
| **BulgePinchFilter**<br>_pixi-filters/bulge-pinch_<br>[View demo][BulgePinch_demo] | ![bulge-pinch](https://filters.pixijs.download/main/screenshots/bulge-pinch.gif?v=3) |
@@ -120,6 +121,7 @@ API documention can be found [here](http://pixijs.io/filters/docs/).
120121
[Adjustment_demo]: https://filters.pixijs.download/main/examples/index.html?enabled=AdjustmentFilter
121122
[AdvancedBloom_demo]: https://filters.pixijs.download/main/examples/index.html?enabled=AdvancedBloomFilter
122123
[Ascii_demo]: https://filters.pixijs.download/main/examples/index.html?enabled=AsciiFilter
124+
[BackdropBlur_demo]: https://filters.pixijs.download/main/examples/index.html?enabled=BackdropBlurFilter
123125
[Bevel_demo]: https://filters.pixijs.download/main/examples/index.html?enabled=BevelFilter
124126
[Bloom_demo]: https://filters.pixijs.download/main/examples/index.html?enabled=BloomFilter
125127
[BulgePinch_demo]: https://filters.pixijs.download/main/examples/index.html?enabled=BulgePinchFilter

examples/src/DemoApplication.mjs

+30-16
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

@@ -96,7 +97,6 @@ export default class DemoApplication extends PIXI.Application
9697
// Setup the container
9798
this.pond = new PIXI.Container();
9899
this.pond.filterArea = this.filterArea;
99-
this.pond.filters = this.pondFilters;
100100
this.stage.addChild(this.pond);
101101

102102
// Setup the background image
@@ -208,8 +208,6 @@ export default class DemoApplication extends PIXI.Application
208208

209209
this.events.emit('animate', delta, animateTimer);
210210

211-
this.pond.filters = this.pondFilters;
212-
213211
if (!this.animating)
214212
{
215213
return;
@@ -224,7 +222,6 @@ export default class DemoApplication extends PIXI.Application
224222
const fish = this.fishes[i];
225223

226224
fish.direction += fish.turnSpeed * 0.01;
227-
fish.filters = this.fishFilters;
228225
fish.x += Math.sin(fish.direction) * fish.speed;
229226
fish.y += Math.cos(fish.direction) * fish.speed;
230227

@@ -302,28 +299,45 @@ export default class DemoApplication extends PIXI.Application
302299
// https://github.com/orgs/pixijs/projects/2/views/4?pane=issue&itemId=48582986
303300
const toggleFilter = (enabled) =>
304301
{
305-
if (enabled)
302+
if (options.fishOnly)
306303
{
307-
if (options.fishOnly)
304+
const fishFilters = [...this.fishFilters];
305+
306+
if (enabled)
308307
{
309-
this.fishFilters.push(filter);
308+
fishFilters.push(filter);
310309
}
311310
else
312311
{
313-
this.pondFilters.push(filter);
314-
}
315-
}
316-
else if (options.fishOnly)
317-
{
318-
const index = this.fishFilters.indexOf(filter);
312+
const index = fishFilters.indexOf(filter);
319313

320-
if (index !== -1) this.fishFilters.splice(index, 1);
314+
if (index !== -1) fishFilters.splice(index, 1);
315+
}
316+
this.fishFilters = fishFilters;
317+
this.fishes.forEach((fish) =>
318+
{
319+
fish.filters = fishFilters;
320+
});
321321
}
322322
else
323323
{
324-
const index = this.pondFilters.indexOf(filter);
324+
const pondFilters = [...this.pondFilters];
325+
326+
if (enabled)
327+
{
328+
pondFilters.push(filter);
329+
}
330+
else
331+
{
332+
const index = pondFilters.indexOf(filter);
333+
334+
if (index !== -1) pondFilters.splice(index, 1);
335+
}
325336

326-
if (index !== -1) this.pondFilters.splice(index, 1);
337+
this.pondFilters = pondFilters;
338+
// TODO: seems like a bug, requiring invalidation
339+
this.pond.filters = [];
340+
this.pond.filters = pondFilters;
327341
}
328342
};
329343

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';

scripts/screenshots/config.json

+12
Original file line numberDiff line numberDiff line change
@@ -572,6 +572,18 @@
572572
],
573573
"alpha": 0.8
574574
}
575+
},
576+
{
577+
"name": "BackdropBlurFilter",
578+
"filename": "backdrop-blur",
579+
"overlayOnly": true,
580+
"overlayOptions": {
581+
"alpha": 0.2,
582+
"tint": "white"
583+
},
584+
"arguments": {
585+
"strength": 4
586+
}
575587
}
576588
]
577589
}

scripts/screenshots/index.html

+3
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@
1919
display: grid;
2020
grid-template-columns: repeat(4, 1fr);
2121
}
22+
body.only {
23+
display: block;
24+
}
2225
img, canvas {
2326
width: 100%;
2427
display: block;

scripts/screenshots/renderer.js

+35-8
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
const { Application, Assets, Container, Sprite, ...PIXI } = require('pixi.js');
1+
const { Application, Assets, Container, Sprite, Texture, ...PIXI } = require('pixi.js');
22
const filters = require('../../lib');
33
const assert = require('assert');
44
const config = require('./config.json');
@@ -19,8 +19,6 @@ const outputOptions = {
1919
},
2020
};
2121

22-
const indexCount = 0;
23-
2422
const app = new Application();
2523

2624
app.init({
@@ -29,6 +27,7 @@ app.init({
2927
backgroundColor: outputOptions.border.color,
3028
autoStart: false,
3129
preference: 'webgpu',
30+
useBackBuffer: true,
3231
hello: true,
3332
}).then(() =>
3433
{
@@ -44,6 +43,7 @@ app.init({
4443
let preview;
4544
let bg;
4645
let fishes;
46+
let overlay;
4747
let displacement;
4848
let lightmap;
4949
let colormap;
@@ -65,20 +65,39 @@ app.init({
6565

6666
fishes = new Sprite(resources.previewFishes);
6767
bg = new Sprite(resources.previewBackground);
68+
overlay = new Sprite(Texture.WHITE);
6869

6970
fishes.scale.set(outputOptions.width / sourceAssetSize.width);
7071
bg.scale.set(outputOptions.width / sourceAssetSize.width);
72+
overlay.setSize(outputOptions.width - 60, outputOptions.height - 60);
73+
overlay.filterArea = new PIXI.Rectangle(
74+
-overlay.width / 2,
75+
-overlay.height / 2,
76+
overlay.width,
77+
overlay.height,
78+
);
79+
overlay.anchor.set(0.5);
80+
overlay.x = outputOptions.width / 2;
81+
overlay.y = outputOptions.height / 2;
7182

7283
preview = new Container();
73-
preview.addChild(bg, fishes);
84+
preview.addChild(bg, fishes, overlay);
7485

7586
app.stage.addChild(preview);
7687
next();
7788
});
7889

90+
const onlyImages = config.images.filter((obj) => obj.only);
91+
const images = onlyImages.length ? onlyImages : config.images;
92+
93+
if (onlyImages.length)
94+
{
95+
document.body.classList.add('only');
96+
}
97+
7998
async function next()
8099
{
81-
const obj = config.images[++index];
100+
const obj = images[++index];
82101

83102
if (obj)
84103
{
@@ -130,10 +149,18 @@ app.init({
130149
}
131150

132151
// Render the filter
133-
fishes.filters = [];
134-
preview.filters = [];
152+
fishes.filters = null;
153+
preview.filters = null;
154+
overlay.filters = null;
155+
overlay.visible = false;
135156

136-
if (obj.fishOnly)
157+
if (obj.overlayOnly)
158+
{
159+
overlay.filters = [filter];
160+
overlay.visible = true;
161+
Object.assign(overlay, obj.overlayOptions);
162+
}
163+
else if (obj.fishOnly)
137164
{
138165
fishes.filters = [filter];
139166
}
+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)