- 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
- 174
- 175
- 176
- 177
- 178
- 179
- 180
- 181
- 182
- 183
- 184
- 185
- 186
- 187
- 188
- 189
- 190
- 191
- 192
- 193
- 194
- 195
- 196
- 197
- 198
- 199
- 200
- 201
- 202
- 203
- 204
- 205
- 206
- 207
- 208
- 209
- 210
- 211
- 212
- 213
- 214
- 215
- 216
- 217
- 218
- 219
- 220
- 221
import { ExtractBrightnessFilter } from './ExtractBrightnessFilter';
import { KawaseBlurFilter } from '@pixi/filter-kawase-blur';
import { vertex } from '@tools/fragments';
import fragment from './advanced-bloom.frag';
import { Filter } from '@pixi/core';
import { settings } from '@pixi/settings';
import type { FilterSystem, FilterState, RenderTexture } from '@pixi/core';
import type { CLEAR_MODES } from '@pixi/constants';
import type { PixelSizeValue } from '@pixi/filter-kawase-blur';
interface AdvancedBloomFilterOptions {
threshold: number,
bloomScale: number,
brightness: number,
kernels: number[] | null,
blur: number,
quality: number,
pixelSize: PixelSizeValue,
resolution: number,
}
/**
* The AdvancedBloomFilter applies a Bloom Effect to an object. Unlike the normal BloomFilter
* this had some advanced controls for adjusting the look of the bloom. Note: this filter
* is slower than normal BloomFilter.<br>
* 
*
* @class
* @extends PIXI.Filter
* @memberof PIXI.filters
* @see @pixi/filter-advanced-bloom
* @see pixi-filters
*/
class AdvancedBloomFilter extends Filter
{
/** Default construction options. */
public static readonly defaults: AdvancedBloomFilterOptions = {
threshold: 0.5,
bloomScale: 1.0,
brightness: 1.0,
kernels: null,
blur: 8,
quality: 4,
pixelSize: 1,
resolution: settings.FILTER_RESOLUTION,
};
/** To adjust the strength of the bloom. Higher values is more intense brightness. */
public bloomScale = 1;
/** The brightness, lower value is more subtle brightness, higher value is blown-out. */
public brightness = 1;
private _extractFilter: ExtractBrightnessFilter;
private _blurFilter: KawaseBlurFilter;
protected _resolution: number = settings.FILTER_RESOLUTION;
/**
* @param {object|number} [options] - The optional parameters of advanced bloom filter.
* When options is a number , it will be `options.threshold`.
* @param {number} [options.threshold=0.5] - Defines how bright a color needs to be to affect bloom.
* @param {number} [options.bloomScale=1.0] - To adjust the strength of the bloom. Higher values is
* more intense brightness.
* @param {number} [options.brightness=1.0] - The brightness, lower value is more subtle brightness,
* higher value is blown-out.
* @param {number} [options.blur=8] - Sets the strength of the Blur properties simultaneously
* @param {number} [options.quality=4] - The quality of the Blur filter.
* @param {number[]} [options.kernels=null] - The kernels of the Blur filter.
* @param {number|number[]|PIXI.Point} [options.pixelSize=1] - the pixelSize of the Blur filter.
* @param {number} [options.resolution=PIXI.settings.FILTER_RESOLUTION] - The resolution of the Blur filter.
*/
constructor(options?: Partial<AdvancedBloomFilterOptions>)
{
super(vertex, fragment);
if (typeof options === 'number')
{
options = { threshold: options };
}
const opt:AdvancedBloomFilterOptions = Object.assign(AdvancedBloomFilter.defaults, options);
this.bloomScale = opt.bloomScale;
this.brightness = opt.brightness;
const { kernels, blur, quality, pixelSize, resolution } = opt;
this._extractFilter = new ExtractBrightnessFilter(opt.threshold);
this._extractFilter.resolution = resolution;
this._blurFilter = kernels
? new KawaseBlurFilter(kernels)
: new KawaseBlurFilter(blur, quality);
this.pixelSize = pixelSize;
this.resolution = resolution;
}
/**
* Override existing apply method in PIXI.Filter
*
* @private
*/
apply(
filterManager: FilterSystem,
input: RenderTexture,
output: RenderTexture,
clear: CLEAR_MODES,
currentState?: FilterState): void
{
const brightTarget = filterManager.getFilterTexture();
this._extractFilter.apply(filterManager, input, brightTarget, 1, currentState);
const bloomTarget = filterManager.getFilterTexture();
this._blurFilter.apply(filterManager, brightTarget, bloomTarget, 1);
this.uniforms.bloomScale = this.bloomScale;
this.uniforms.brightness = this.brightness;
this.uniforms.bloomTexture = bloomTarget;
filterManager.applyFilter(this, input, output, clear);
filterManager.returnFilterTexture(bloomTarget);
filterManager.returnFilterTexture(brightTarget);
}
/**
* The resolution of the filter.
* @ignore
*/
get resolution(): number
{
return this._resolution;
}
set resolution(value: number)
{
this._resolution = value;
if (this._extractFilter)
{
this._extractFilter.resolution = value;
}
if (this._blurFilter)
{
this._blurFilter.resolution = value;
}
}
/**
* Defines how bright a color needs to be to affect bloom.
*
* @default 0.5
*/
get threshold(): number
{
return this._extractFilter.threshold;
}
set threshold(value: number)
{
this._extractFilter.threshold = value;
}
/**
* Sets the kernels of the Blur Filter
*/
get kernels(): number[]
{
return this._blurFilter.kernels;
}
set kernels(value: number[])
{
this._blurFilter.kernels = value;
}
/**
* Sets the strength of the Blur properties simultaneously
*
* @default 2
*/
get blur(): number
{
return this._blurFilter.blur;
}
set blur(value: number)
{
this._blurFilter.blur = value;
}
/**
* Sets the quality of the Blur Filter
*
* @default 4
*/
get quality(): number
{
return this._blurFilter.quality;
}
set quality(value: number)
{
this._blurFilter.quality = value;
}
/**
* Sets the pixelSize of the Kawase Blur filter
*
* @member {number|number[]|PIXI.Point}
* @default 1
*/
get pixelSize(): PixelSizeValue
{
return this._blurFilter.pixelSize;
}
set pixelSize(value: PixelSizeValue)
{
this._blurFilter.pixelSize = value;
}
}
export { AdvancedBloomFilter };
export type { AdvancedBloomFilterOptions };