|
| 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 | +} |
0 commit comments