Apply WebGL Filters to Pixi.js Containers and Sprites
WebGL filters in Pixi.js are powerful tools that allow developers to
apply real-time visual effects, such as blurs, color adjustments, and
displacement maps, directly to display objects. This article explains
what WebGL filters are and provides a direct, step-by-step guide on how
to apply them to both Container and Sprite
instances in your Pixi.js projects.
What Are WebGL Filters?
In Pixi.js, filters are special shader programs (specifically fragment shaders) that run directly on the GPU. Instead of rendering a display object directly to the screen, Pixi.js renders the object to a temporary texture, applies the filter’s shader effects to that texture, and then renders the final, modified image to the screen.
Pixi.js comes with several built-in filters, including: * BlurFilter: Creates a gaussian blur effect. * ColorMatrixFilter: Adjusts colors, brightness, contrast, and saturation. * AlphaFilter: Modifies the transparency of an object. * DisplacementFilter: Uses a texture to distort the pixels of another object.
You can also write custom GLSL fragment shaders to create unique visual effects.
How to Apply Filters
Applying a filter is the same for both Sprite and
Container objects because both classes inherit from
DisplayObject, which contains the filters
property. The filters property accepts an array of filter
instances, meaning you can apply multiple filters to a single object
simultaneously.
Step 1: Instantiate the Filter
First, create an instance of the filter you want to use and configure its properties.
// Create a blur filter instance
const blurFilter = new PIXI.BlurFilter();
// Configure the filter properties (e.g., strength of the blur)
blurFilter.blur = 8;Step 2: Assign the Filter to a Sprite or Container
Assign the filter instance inside an array to the
.filters property of your target object.
Applying to a Sprite:
// Create a Sprite
const sprite = PIXI.Sprite.from('player.png');
// Apply the filter to the Sprite
sprite.filters = [blurFilter];Applying to a Container:
Applying a filter to a Container will apply the effect
to the container itself and all of its children as a single flat
image.
// Create a Container and add children
const gameUI = new PIXI.Container();
gameUI.addChild(buttonSprite);
gameUI.addChild(textObject);
// Apply the filter to the entire Container
gameUI.filters = [blurFilter];Step 3: Modifying or Removing Filters
You can update filter properties dynamically in your game loop to create animations, or remove them entirely when they are no longer needed.
// To update a filter in an animation loop:
app.ticker.add(() => {
blurFilter.blur = Math.sin(Date.now() * 0.002) * 10;
});
// To remove all filters from an object:
sprite.filters = null;Performance Considerations
While WebGL filters are highly optimized, they do require additional
GPU memory and processing power. Each filtered object requires at least
one extra render pass. To maintain a high framerate: * Combine
objects: Apply a single filter to a parent
Container rather than applying individual filters to many
child Sprite objects. * Adjust padding: If
a filter (like a blur) cuts off at the edges of your sprite, increase
the filter.padding property (e.g.,
blurFilter.padding = 10) to expand the render area. *
Clean up: Set .filters = null when an
effect is finished to free up GPU resources.