- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
- 96
- 97
- 98
- 99
- 100
- 101
- 102
- 103
- 104
- 105
- 106
- 107
- 108
- 109
- 110
- 111
- 112
- 113
- 114
- 115
- 116
- 117
- 118
- 119
- 120
- 121
- 122
- 123
- 124
- 125
- 126
- 127
- 128
- 129
- 130
- 131
- 132
- 133
- 134
- 135
- 136
- 137
- 138
- 139
- 140
- 141
- 142
- 143
- 144
- 145
- 146
- 147
- 148
- 149
- 150
- 151
- 152
- 153
- 154
- 155
- 156
- 157
- 158
- 159
- 160
- 161
- 162
- 163
- 164
- 165
- 166
- 167
- 168
- 169
- 170
- 171
- 172
- 173
import { Filter } from '@pixi/core';
import { settings } from '@pixi/settings';
import { BlurFilterPass } from './BlurFilterPass';
import { CLEAR_MODES } from '@pixi/constants';
import type { FilterSystem, RenderTexture } from '@pixi/core';
import type { BLEND_MODES } from '@pixi/constants';
/**
* The BlurFilter applies a Gaussian blur to an object.
*
* The strength of the blur can be set for the x-axis and y-axis separately.
* @memberof PIXI.filters
*/
export class BlurFilter extends Filter
{
public blurXFilter: BlurFilterPass;
public blurYFilter: BlurFilterPass;
private _repeatEdgePixels: boolean;
/**
* @param strength - The strength of the blur filter.
* @param quality - The quality of the blur filter.
* @param [resolution=PIXI.settings.FILTER_RESOLUTION] - The resolution of the blur filter.
* @param kernelSize - The kernelSize of the blur filter.Options: 5, 7, 9, 11, 13, 15.
*/
constructor(strength = 8, quality = 4, resolution = settings.FILTER_RESOLUTION, kernelSize = 5)
{
super();
this.blurXFilter = new BlurFilterPass(true, strength, quality, resolution, kernelSize);
this.blurYFilter = new BlurFilterPass(false, strength, quality, resolution, kernelSize);
this.resolution = resolution;
this.quality = quality;
this.blur = strength;
this.repeatEdgePixels = false;
}
/**
* Applies the filter.
* @param filterManager - The manager.
* @param input - The input target.
* @param output - The output target.
* @param clearMode - How to clear
*/
apply(filterManager: FilterSystem, input: RenderTexture, output: RenderTexture, clearMode: CLEAR_MODES): void
{
const xStrength = Math.abs(this.blurXFilter.strength);
const yStrength = Math.abs(this.blurYFilter.strength);
if (xStrength && yStrength)
{
const renderTarget = filterManager.getFilterTexture();
this.blurXFilter.apply(filterManager, input, renderTarget, CLEAR_MODES.CLEAR);
this.blurYFilter.apply(filterManager, renderTarget, output, clearMode);
filterManager.returnFilterTexture(renderTarget);
}
else if (yStrength)
{
this.blurYFilter.apply(filterManager, input, output, clearMode);
}
else
{
this.blurXFilter.apply(filterManager, input, output, clearMode);
}
}
protected updatePadding(): void
{
if (this._repeatEdgePixels)
{
this.padding = 0;
}
else
{
this.padding = Math.max(Math.abs(this.blurXFilter.strength), Math.abs(this.blurYFilter.strength)) * 2;
}
}
/**
* Sets the strength of both the blurX and blurY properties simultaneously
* @default 2
*/
get blur(): number
{
return this.blurXFilter.blur;
}
set blur(value: number)
{
this.blurXFilter.blur = this.blurYFilter.blur = value;
this.updatePadding();
}
/**
* Sets the number of passes for blur. More passes means higher quality bluring.
* @default 1
*/
get quality(): number
{
return this.blurXFilter.quality;
}
set quality(value: number)
{
this.blurXFilter.quality = this.blurYFilter.quality = value;
}
/**
* Sets the strength of the blurX property
* @default 2
*/
get blurX(): number
{
return this.blurXFilter.blur;
}
set blurX(value: number)
{
this.blurXFilter.blur = value;
this.updatePadding();
}
/**
* Sets the strength of the blurY property
* @default 2
*/
get blurY(): number
{
return this.blurYFilter.blur;
}
set blurY(value: number)
{
this.blurYFilter.blur = value;
this.updatePadding();
}
/**
* Sets the blendmode of the filter
* @default PIXI.BLEND_MODES.NORMAL
*/
get blendMode(): BLEND_MODES
{
return this.blurYFilter.blendMode;
}
set blendMode(value: BLEND_MODES)
{
this.blurYFilter.blendMode = value;
}
/**
* If set to true the edge of the target will be clamped
* @default false
*/
get repeatEdgePixels(): boolean
{
return this._repeatEdgePixels;
}
set repeatEdgePixels(value: boolean)
{
this._repeatEdgePixels = value;
this.updatePadding();
}
}