Pixijs Blend Modes

作者 pixijs83760c6f5346MIT346 个星标收录于 2026年10月8日更新于 2026年10月8日仓库7天前更新

Use this skill when compositing display objects with blend modes in PixiJS v8. Covers standard modes (normal, add, multiply, screen, erase, min, max), advanced modes via pixi.js/advanced-blend-modes (color-burn, overlay, hard-light, etc.), batch-friendly ordering. Triggers on: blendMode, additive, multiply, screen, overlay, color-burn, color-dodge, advanced-blend-modes, glow, erase.

AI 生成的概览

讲解 PixiJS v8 混合模式合成:标准模式、高级模式、批处理顺序与常见错误。

功能
说明如何在 PixiJS v8 中通过设置 container.blendMode 合成显示对象,涵盖内置标准模式与基于滤镜的高级模式。它列出可用的高级模式,给出所需的扩展导入与 useBackBuffer 设置,并说明有利于批处理的子对象排序方式。文档还记录了常见错误,例如缺少导入、v7 的 BLEND_MODES 枚举已移除,以及高 DPI 下的分辨率问题。
适用场景
适用于为 PixiJS v8 场景添加发光、阴影、叠加等合成效果时。也适合在高级混合模式静默回退、模式切换导致绘制调用增加,或从 v7 混合模式枚举迁移时参考。
运行要求
需要项目中使用 PixiJS v8;高级模式需要导入 pixi.js/advanced-blend-modes,并在 WebGL 下于初始化时启用 useBackBuffer。不包含脚本或凭据。

Set container.blendMode to composite display objects with GPU blend equations (standard modes) or filter-based advanced modes. Blend-mode transitions break render batches, so group like-mode siblings together.

Quick Start

ts
const light = new Sprite(await Assets.load("light.png"));light.blendMode = "add";app.stage.addChild(light);
const shadow = new Sprite(await Assets.load("shadow.png"));shadow.blendMode = "multiply";app.stage.addChild(shadow);
import "pixi.js/advanced-blend-modes";const overlay = new Sprite(await Assets.load("overlay.png"));overlay.blendMode = "color-burn";app.stage.addChild(overlay);

Related skills: pixijs-filters (advanced modes use the filter pipeline), pixijs-performance (batching with blend modes), pixijs-color (color manipulation).

Core Patterns

Standard blend modes

Standard modes are built in and use GPU blend equations directly:

ts
import { Sprite } from "pixi.js";
sprite.blendMode = "normal"; // standard alpha compositing (effective default at root)sprite.blendMode = "add"; // additive (lighten, glow effects)sprite.blendMode = "multiply"; // multiply (darken, shadow effects)sprite.blendMode = "screen"; // screen (lighten, dodge effects)sprite.blendMode = "erase"; // erase pixels from render targetsprite.blendMode = "none"; // no blending, overwrites destinationsprite.blendMode = "inherit"; // inherit from parent (this is the actual default value)sprite.blendMode = "min"; // keeps minimum of source and destination (WebGL2+ only)sprite.blendMode = "max"; // keeps maximum of source and destination (WebGL2+ only)

These are hardware-accelerated and cheap. They do not require filters.

Advanced blend modes

Advanced modes require an explicit import to register the extensions. On the WebGL renderer they also require useBackBuffer: true at init time, or PixiJS logs a warning and the blend silently falls back:

ts
import "pixi.js/advanced-blend-modes";import { Application, Sprite, Assets } from "pixi.js";
const app = new Application();await app.init({ useBackBuffer: true }); // required for advanced modes on WebGL
const texture = await Assets.load("overlay.png");const overlay = new Sprite(texture);overlay.blendMode = "color-burn";

Available advanced modes:

ModeEffect
color-burnDarkens by increasing contrast
color-dodgeBrightens by decreasing contrast
darkenKeeps darker of two layers
differenceAbsolute difference
divideDivides bottom by top
exclusionSimilar to difference, lower contrast
hard-lightMultiply or screen based on top layer
hard-mixHigh contrast threshold blend
lightenKeeps lighter of two layers
linear-burnAdds and subtracts to darken
linear-dodgeAdds layers together
linear-lightLinear burn or dodge based on top layer
luminosityLuminosity of top, hue/saturation of bottom
negationInverted difference
overlayMultiply or screen based on bottom layer
pin-lightReplaces based on lightness comparison
saturationSaturation of top, hue/luminosity of bottom
soft-lightGentle overlay effect
subtractSubtracts top from bottom
vivid-lightColor burn or dodge based on top layer
colorHue and saturation of top, luminosity of bottom

You set advanced blend modes the same way as standard ones, via the blendMode property. They use filters internally, so they cost more than standard modes.

Batch-friendly ordering

Different blend modes break the rendering batch. Order objects to minimize transitions:

ts
import { Container, Sprite } from "pixi.js";
const scene = new Container();scene.addChild(screenSprite1); // 'screen'scene.addChild(screenSprite2); // 'screen'scene.addChild(normalSprite1); // 'normal'scene.addChild(normalSprite2); // 'normal'

2 draw calls. Alternating order (screen, normal, screen, normal) would produce 4.

Common Mistakes

[HIGH] Not importing advanced-blend-modes extension

Wrong:

ts
import { Sprite } from "pixi.js";
sprite.blendMode = "color-burn"; // silently falls back to normal

Correct:

ts
import "pixi.js/advanced-blend-modes";import { Sprite } from "pixi.js";
sprite.blendMode = "color-burn";

Advanced blend modes (color-burn, overlay, etc.) require the extension import. Without it, only standard modes (normal, add, multiply, screen) are available. The invalid mode silently falls back.

[MEDIUM] Mixing blend modes across adjacent objects

Different blend modes break the render batch. screen / normal / screen / normal produces 4 draw calls, while screen / screen / normal / normal produces 2. Sort children so objects with the same blend mode are adjacent.

[HIGH] Using the v7 BLEND_MODES enum

Wrong:

ts
import { BLEND_MODES } from "pixi.js";
sprite.blendMode = BLEND_MODES.ADD; // runtime error: BLEND_MODES is undefined

Correct:

ts
sprite.blendMode = "add";

In v8, BLEND_MODES is a TypeScript type only (a union of string literals). There is no runtime enum export, so BLEND_MODES.ADD evaluates to accessing a property on undefined. Use the string form.

[HIGH] Advanced blend modes without useBackBuffer

Wrong:

ts
import "pixi.js/advanced-blend-modes";await app.init({  /* no useBackBuffer */});sprite.blendMode = "color-burn"; // logs a warning, falls back

Correct:

ts
import "pixi.js/advanced-blend-modes";await app.init({ useBackBuffer: true });sprite.blendMode = "color-burn";

Advanced modes read from the back buffer. On WebGL, the blend silently falls back if the back buffer is not enabled. WebGPU enables the back buffer unconditionally.

[MEDIUM] Advanced blend modes clipped or scaled on high-DPI renderers

Advanced blend modes are filter-based and use Filter.defaultOptions, whose resolution defaults to 1. On a high-DPI render target the blended object can look clipped, scaled, or only partially applied.

Wrong:

ts
import "pixi.js/advanced-blend-modes";
sprite.blendMode = "overlay"; // renders at resolution 1, can clip on retina

Correct:

ts
import { Filter } from "pixi.js";import "pixi.js/advanced-blend-modes";
Filter.defaultOptions.resolution = "inherit"; // set before creating affected objects
sprite.blendMode = "overlay";

Setting Filter.defaultOptions.resolution = "inherit" makes advanced blend modes render at the render target's resolution. This costs more memory and runtime, so apply it where fidelity matters.

API Reference

来源与署名

来源:pixijs/pixijs-skills位于skills/pixijs-blend-modes提交83760c6

许可证: MIT

内容归原作者所有。SourceWeft 从公开仓库中收录这些内容。

举报或申请下架