pixijs-blend-modes
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseSet 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.
container.blendMode设置,即可通过GPU混合公式(标准模式)或基于滤镜的高级模式合成显示对象。混合模式的切换会打断渲染批处理,因此请将相同模式的同级对象归为一组。
container.blendModeQuick 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: (advanced modes use the filter pipeline), (batching with blend modes), (color manipulation).
pixijs-filterspixijs-performancepixijs-colorts
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);相关技能: (高级模式使用滤镜管线)、(混合模式下的批处理)、(颜色操作)。
pixijs-filterspixijs-performancepixijs-colorCore 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 target
sprite.blendMode = "none"; // no blending, overwrites destination
sprite.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.
标准模式是内置的,直接使用GPU混合公式:
ts
import { Sprite } from "pixi.js";
sprite.blendMode = "normal"; // 标准alpha合成(根节点的默认有效模式)
sprite.blendMode = "add"; // 加法模式(提亮、发光效果)
sprite.blendMode = "multiply"; // 正片叠底(加深、阴影效果)
sprite.blendMode = "screen"; // 滤色(提亮、减淡效果)
sprite.blendMode = "erase"; // 擦除渲染目标中的像素
sprite.blendMode = "none"; // 无混合,直接覆盖目标
sprite.blendMode = "inherit"; // 继承父级模式(这是实际的默认值)
sprite.blendMode = "min"; // 保留源和目标中的最小值(仅WebGL2+支持)
sprite.blendMode = "max"; // 保留源和目标中的最大值(仅WebGL2+支持)这些模式由硬件加速,性能开销低,无需使用滤镜。
Advanced blend modes
高级混合模式
Advanced modes require an explicit import to register the extensions. On the WebGL renderer they also require at init time, or PixiJS logs a warning and the blend silently falls back:
useBackBuffer: truets
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:
| Mode | Effect |
|---|---|
| Darkens by increasing contrast |
| Brightens by decreasing contrast |
| Keeps darker of two layers |
| Absolute difference |
| Divides bottom by top |
| Similar to difference, lower contrast |
| Multiply or screen based on top layer |
| High contrast threshold blend |
| Keeps lighter of two layers |
| Adds and subtracts to darken |
| Adds layers together |
| Linear burn or dodge based on top layer |
| Luminosity of top, hue/saturation of bottom |
| Inverted difference |
| Multiply or screen based on bottom layer |
| Replaces based on lightness comparison |
| Saturation of top, hue/luminosity of bottom |
| Gentle overlay effect |
| Subtracts top from bottom |
| Color burn or dodge based on top layer |
| Hue and saturation of top, luminosity of bottom |
You set advanced blend modes the same way as standard ones, via the property. They use filters internally, so they cost more than standard modes.
blendMode高级模式需要显式导入以注册扩展。在WebGL渲染器中,还需要在初始化时设置,否则PixiJS会记录警告并自动回退到标准模式:
useBackBuffer: truets
import "pixi.js/advanced-blend-modes";
import { Application, Sprite, Assets } from "pixi.js";
const app = new Application();
await app.init({ useBackBuffer: true }); // WebGL下使用高级模式的必要配置
const texture = await Assets.load("overlay.png");
const overlay = new Sprite(texture);
overlay.blendMode = "color-burn";可用的高级模式:
| 模式 | 效果 |
|---|---|
| 通过提高对比度来加深颜色 |
| 通过降低对比度来提亮颜色 |
| 保留两个图层中较暗的部分 |
| 计算颜色的绝对差 |
| 用底层颜色除以上层颜色 |
| 与difference类似,但对比度更低 |
| 根据上层颜色选择正片叠底或滤色 |
| 高对比度阈值混合 |
| 保留两个图层中较亮的部分 |
| 通过加减操作加深颜色 |
| 将两个图层颜色相加 |
| 根据上层颜色选择linear burn或linear dodge |
| 保留上层的亮度,底层的色相/饱和度 |
| 反转颜色差值 |
| 根据底层颜色选择正片叠底或滤色 |
| 根据亮度比较结果替换颜色 |
| 保留上层的饱和度,底层的色相/亮度 |
| 柔和的叠加效果 |
| 用底层颜色减去上层颜色 |
| 根据上层颜色选择color burn或color dodge |
| 保留上层的色相和饱和度,底层的亮度 |
设置高级混合模式的方式与标准模式相同,都是通过属性。它们内部使用滤镜,因此性能开销比标准模式更高。
blendModeBatch-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 () would produce 4.
screen, normal, screen, normal不同的混合模式会打断渲染批处理。对对象进行排序以减少模式切换:
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次绘制调用。如果交替排序()则会产生4次绘制调用。
screen、normal、screen、normalCommon Mistakes
常见错误
[HIGH] Not importing advanced-blend-modes extension
[高风险] 未导入advanced-blend-modes扩展
Wrong:
ts
import { Sprite } from "pixi.js";
sprite.blendMode = "color-burn"; // silently falls back to normalCorrect:
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.
错误示例:
ts
import { Sprite } from "pixi.js";
sprite.blendMode = "color-burn"; // 自动回退到normal模式正确示例:
ts
import "pixi.js/advanced-blend-modes";
import { Sprite } from "pixi.js";
sprite.blendMode = "color-burn";高级混合模式(color-burn、overlay等)需要导入扩展。如果不导入,仅能使用标准模式(normal、add、multiply、screen),无效模式会自动回退。
[MEDIUM] Mixing blend modes across adjacent objects
[中风险] 在相邻对象间混合不同的混合模式
Different blend modes break the render batch. produces 4 draw calls, while produces 2. Sort children so objects with the same blend mode are adjacent.
screen / normal / screen / normalscreen / screen / normal / normal不同的混合模式会打断渲染批处理。会产生4次绘制调用,而仅产生2次。请对子级进行排序,让相同混合模式的对象相邻。
screen / normal / screen / normalscreen / screen / normal / normal[HIGH] Using the v7 BLEND_MODES enum
[高风险] 使用v7版本的BLEND_MODES枚举
Wrong:
ts
import { BLEND_MODES } from "pixi.js";
sprite.blendMode = BLEND_MODES.ADD; // runtime error: BLEND_MODES is undefinedCorrect:
ts
sprite.blendMode = "add";In v8, is a TypeScript type only (a union of string literals). There is no runtime enum export, so evaluates to accessing a property on . Use the string form.
BLEND_MODESBLEND_MODES.ADDundefined错误示例:
ts
import { BLEND_MODES } from "pixi.js";
sprite.blendMode = BLEND_MODES.ADD; // 运行时错误:BLEND_MODES未定义正确示例:
ts
sprite.blendMode = "add";在v8版本中,仅作为TypeScript类型存在(字符串字面量的联合类型)。没有运行时枚举导出,因此会尝试访问的属性。请使用字符串形式。
BLEND_MODESBLEND_MODES.ADDundefined[HIGH] Advanced blend modes without useBackBuffer
[高风险] 使用高级混合模式但未启用useBackBuffer
Wrong:
ts
import "pixi.js/advanced-blend-modes";
await app.init({
/* no useBackBuffer */
});
sprite.blendMode = "color-burn"; // logs a warning, falls backCorrect:
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.
错误示例:
ts
import "pixi.js/advanced-blend-modes";
await app.init({
/* 未设置useBackBuffer */
});
sprite.blendMode = "color-burn"; // 记录警告并回退正确示例:
ts
import "pixi.js/advanced-blend-modes";
await app.init({ useBackBuffer: true });
sprite.blendMode = "color-burn";高级模式需要读取后台缓冲区。在WebGL中,如果未启用后台缓冲区,混合会自动回退。WebGPU会无条件启用后台缓冲区。