UNPKG

9.04 kBTypeScriptView Raw
1import { Shader } from '../shader/Shader';
2import { State } from '../state/State';
3import type { MSAA_QUALITY, BLEND_MODES, CLEAR_MODES } from '@pixi/constants';
4import type { Dict } from '@pixi/utils';
5import type { RenderTexture } from '../renderTexture/RenderTexture';
6import type { FilterState } from './FilterState';
7import type { FilterSystem } from './FilterSystem';
8/**
9 * A filter is a special shader that applies post-processing effects to an input texture and writes into an output
10 * render-target.
11 *
12 * {@link http://pixijs.io/examples/#/filters/blur-filter.js Example} of the
13 * {@link PIXI.filters.BlurFilter BlurFilter}.
14 *
15 * ### Usage
16 * Filters can be applied to any DisplayObject or Container.
17 * PixiJS' `FilterSystem` renders the container into temporary Framebuffer,
18 * then filter renders it to the screen.
19 * Multiple filters can be added to the `filters` array property and stacked on each other.
20 *
21 * ```js
22 * import { Container, Filter } from 'pixi.js';
23 * const filter = new Filter(myShaderVert, myShaderFrag, { myUniform: 0.5 });
24 * const container = new Container();
25 * container.filters = [filter];
26 * ```
27 *
28 * ### Previous Version Differences
29 *
30 * In PixiJS **v3**, a filter was always applied to _whole screen_.
31 *
32 * In PixiJS **v4**, a filter can be applied _only part of the screen_.
33 * Developers had to create a set of uniforms to deal with coordinates.
34 *
35 * In PixiJS **v5** combines _both approaches_.
36 * Developers can use normal coordinates of v3 and then allow filter to use partial Framebuffers,
37 * bringing those extra uniforms into account.
38 *
39 * Also be aware that we have changed default vertex shader, please consult
40 * {@link https://github.com/pixijs/pixi.js/wiki/v5-Creating-filters Wiki}.
41 *
42 * ### Frames
43 *
44 * The following table summarizes the coordinate spaces used in the filtering pipeline:
45 *
46 * <table>
47 * <thead>
48 * <tr>
49 * <th>Coordinate Space</th>
50 * <th>Description</th>
51 * </tr>
52 * </thead>
53 * <tbody>
54 * <tr>
55 * <td>Texture Coordinates</td>
56 * <td>
57 * The texture (or UV) coordinates in the input base-texture's space. These are normalized into the (0,1) range along
58 * both axes.
59 * </td>
60 * </tr>
61 * <tr>
62 * <td>World Space</td>
63 * <td>
64 * A point in the same space as the world bounds of any display-object (i.e. in the scene graph's space).
65 * </td>
66 * </tr>
67 * <tr>
68 * <td>Physical Pixels</td>
69 * <td>
70 * This is base-texture's space with the origin on the top-left. You can calculate these by multiplying the texture
71 * coordinates by the dimensions of the texture.
72 * </td>
73 * </tr>
74 * </tbody>
75 * </table>
76 *
77 * ### Built-in Uniforms
78 *
79 * PixiJS viewport uses screen (CSS) coordinates, `(0, 0, renderer.screen.width, renderer.screen.height)`,
80 * and `projectionMatrix` uniform maps it to the gl viewport.
81 *
82 * **uSampler**
83 *
84 * The most important uniform is the input texture that container was rendered into.
85 * _Important note: as with all Framebuffers in PixiJS, both input and output are
86 * premultiplied by alpha._
87 *
88 * By default, input normalized coordinates are passed to fragment shader with `vTextureCoord`.
89 * Use it to sample the input.
90 *
91 * ```js
92 * import { Filter } from 'pixi.js';
93 * const fragment = `
94 * varying vec2 vTextureCoord;
95 * uniform sampler2D uSampler;
96 * void main(void)
97 * {
98 * gl_FragColor = texture2D(uSampler, vTextureCoord);
99 * }
100 * `;
101 *
102 * const myFilter = new Filter(null, fragment);
103 * ```
104 *
105 * This filter is just one uniform less than {@link PIXI.filters.AlphaFilter AlphaFilter}.
106 *
107 * **outputFrame**
108 *
109 * The `outputFrame` holds the rectangle where filter is applied in screen (CSS) coordinates.
110 * It's the same as `renderer.screen` for a fullscreen filter.
111 * Only a part of `outputFrame.zw` size of temporary Framebuffer is used,
112 * `(0, 0, outputFrame.width, outputFrame.height)`,
113 *
114 * Filters uses this quad to normalized (0-1) space, its passed into `aVertexPosition` attribute.
115 * To calculate vertex position in screen space using normalized (0-1) space:
116 *
117 * ```glsl
118 * vec4 filterVertexPosition( void )
119 * {
120 * vec2 position = aVertexPosition * max(outputFrame.zw, vec2(0.)) + outputFrame.xy;
121 * return vec4((projectionMatrix * vec3(position, 1.0)).xy, 0.0, 1.0);
122 * }
123 * ```
124 *
125 * **inputSize**
126 *
127 * Temporary framebuffer is different, it can be either the size of screen, either power-of-two.
128 * The `inputSize.xy` are size of temporary framebuffer that holds input.
129 * The `inputSize.zw` is inverted, it's a shortcut to evade division inside the shader.
130 *
131 * Set `inputSize.xy = outputFrame.zw` for a fullscreen filter.
132 *
133 * To calculate input normalized coordinate, you have to map it to filter normalized space.
134 * Multiply by `outputFrame.zw` to get input coordinate.
135 * Divide by `inputSize.xy` to get input normalized coordinate.
136 *
137 * ```glsl
138 * vec2 filterTextureCoord( void )
139 * {
140 * return aVertexPosition * (outputFrame.zw * inputSize.zw); // same as /inputSize.xy
141 * }
142 * ```
143 *
144 * **resolution**
145 *
146 * The `resolution` is the ratio of screen (CSS) pixels to real pixels.
147 *
148 * **inputPixel**
149 *
150 * `inputPixel.xy` is the size of framebuffer in real pixels, same as `inputSize.xy * resolution`
151 * `inputPixel.zw` is inverted `inputPixel.xy`.
152 *
153 * It's handy for filters that use neighbour pixels, like {@link PIXI.filters.FXAAFilter FXAAFilter}.
154 *
155 * **inputClamp**
156 *
157 * If you try to get info from outside of used part of Framebuffer - you'll get undefined behaviour.
158 * For displacements, coordinates has to be clamped.
159 *
160 * The `inputClamp.xy` is left-top pixel center, you may ignore it, because we use left-top part of Framebuffer
161 * `inputClamp.zw` is bottom-right pixel center.
162 *
163 * ```glsl
164 * vec4 color = texture2D(uSampler, clamp(modifiedTextureCoord, inputClamp.xy, inputClamp.zw));
165 * ```
166 *
167 * Or:
168 *
169 * ```glsl
170 * vec4 color = texture2D(uSampler, min(modifigedTextureCoord, inputClamp.zw));
171 * ```
172 *
173 * ### Additional Information
174 *
175 * Complete documentation on Filter usage is located in the
176 * {@link https://github.com/pixijs/pixi.js/wiki/v5-Creating-filters Wiki}.
177 *
178 * Since PixiJS only had a handful of built-in filters, additional filters can be downloaded
179 * {@link https://github.com/pixijs/pixi-filters here} from the PixiJS Filters repository.
180 * @memberof PIXI
181 */
182export declare class Filter extends Shader {
183 /**
184 * The padding of the filter. Some filters require extra space to breath such as a blur.
185 * Increasing this will add extra width and height to the bounds of the object that the
186 * filter is applied to.
187 */
188 padding: number;
189 /** The samples of the filter. */
190 multisample: MSAA_QUALITY;
191 /** If enabled is true the filter is applied, if false it will not. */
192 enabled: boolean;
193 /**
194 * If enabled, PixiJS will fit the filter area into boundaries for better performance.
195 * Switch it off if it does not work for specific shader.
196 * @default true
197 */
198 autoFit: boolean;
199 /**
200 * Legacy filters use position and uvs from attributes (set by filter system)
201 * @readonly
202 */
203 legacy: boolean;
204 /** The WebGL state the filter requires to render. */
205 state: State;
206 protected _resolution: number;
207 /**
208 * @param vertexSrc - The source of the vertex shader.
209 * @param fragmentSrc - The source of the fragment shader.
210 * @param uniforms - Custom uniforms to use to augment the built-in ones.
211 */
212 constructor(vertexSrc?: string, fragmentSrc?: string, uniforms?: Dict<any>);
213 /**
214 * Applies the filter
215 * @param {PIXI.FilterSystem} filterManager - The renderer to retrieve the filter from
216 * @param {PIXI.RenderTexture} input - The input render target.
217 * @param {PIXI.RenderTexture} output - The target to output to.
218 * @param {PIXI.CLEAR_MODES} [clearMode] - Should the output be cleared before rendering to it.
219 * @param {object} [_currentState] - It's current state of filter.
220 * There are some useful properties in the currentState :
221 * target, filters, sourceFrame, destinationFrame, renderTarget, resolution
222 */
223 apply(filterManager: FilterSystem, input: RenderTexture, output: RenderTexture, clearMode?: CLEAR_MODES, _currentState?: FilterState): void;
224 /**
225 * Sets the blend mode of the filter.
226 * @default PIXI.BLEND_MODES.NORMAL
227 */
228 get blendMode(): BLEND_MODES;
229 set blendMode(value: BLEND_MODES);
230 /**
231 * The resolution of the filter. Setting this to be lower will lower the quality but
232 * increase the performance of the filter.
233 */
234 get resolution(): number;
235 set resolution(value: number);
236 /**
237 * The default vertex shader source
238 * @constant
239 */
240 static get defaultVertexSrc(): string;
241 /**
242 * The default fragment shader source
243 * @constant
244 */
245 static get defaultFragmentSrc(): string;
246 /** Used for caching shader IDs. */
247 static SOURCE_KEY_MAP: Dict<string>;
248}