-
-
Notifications
You must be signed in to change notification settings - Fork 160
/
AdvancedBloomFilter.js
188 lines (163 loc) · 5.64 KB
/
AdvancedBloomFilter.js
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
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';
/**
* 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 {@link https://www.npmjs.com/package/@pixi/filter-advanced-bloom|@pixi/filter-advanced-bloom}
* @see {@link https://www.npmjs.com/package/pixi-filters|pixi-filters}
*
* @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.RESOLUTION] - The resolution of the Blur filter.
*/
class AdvancedBloomFilter extends Filter {
constructor(options) {
super(vertex, fragment);
if (typeof options === 'number') {
options = { threshold: options };
}
options = Object.assign({
threshold: 0.5,
bloomScale: 1.0,
brightness: 1.0,
kernels: null,
blur: 8,
quality: 4,
pixelSize: 1,
resolution: settings.RESOLUTION,
}, options);
/**
* To adjust the strength of the bloom. Higher values is more intense brightness.
*
* @member {number}
* @default 1.0
*/
this.bloomScale = options.bloomScale;
/**
* The brightness, lower value is more subtle brightness, higher value is blown-out.
*
* @member {number}
* @default 1.0
*/
this.brightness = options.brightness;
const { kernels, blur, quality, pixelSize, resolution } = options;
this._extractFilter = new ExtractBrightnessFilter(options.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, input, output, clear, currentState) {
const brightTarget = filterManager.getFilterTexture();
this._extractFilter.apply(filterManager, input, brightTarget, true, currentState);
const bloomTarget = filterManager.getFilterTexture();
this._blurFilter.apply(filterManager, brightTarget, bloomTarget, true, currentState);
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.
*
* @member {number}
*/
get resolution() {
return this._resolution;
}
set resolution(value) {
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.
*
* @member {number}
* @default 0.5
*/
get threshold() {
return this._extractFilter.threshold;
}
set threshold(value) {
this._extractFilter.threshold = value;
}
/**
* Sets the kernels of the Blur Filter
*
* @member {number}
* @default 4
*/
get kernels() {
return this._blurFilter.kernels;
}
set kernels(value) {
this._blurFilter.kernels = value;
}
/**
* Sets the strength of the Blur properties simultaneously
*
* @member {number}
* @default 2
*/
get blur() {
return this._blurFilter.blur;
}
set blur(value) {
this._blurFilter.blur = value;
}
/**
* Sets the quality of the Blur Filter
*
* @member {number}
* @default 4
*/
get quality() {
return this._blurFilter.quality;
}
set quality(value) {
this._blurFilter.quality = value;
}
/**
* Sets the pixelSize of the Kawase Blur filter
*
* @member {number|number[]|PIXI.Point}
* @default 1
*/
get pixelSize() {
return this._blurFilter.pixelSize;
}
set pixelSize(value) {
this._blurFilter.pixelSize = value;
}
}
export { AdvancedBloomFilter };