pixijs-scene-particle-container
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseParticleContainerContainerAssumes familiarity with . is a special leaf in a different sense: it contains instances in its own array and rejects normal PixiJS children. Use , not , and wrap the whole in a if you need to group it with other scene objects.
pixijs-scene-core-conceptsParticleContainerParticleparticleChildrenaddParticleaddChildParticleContainerContainerThe Particle API is new in v8 but is stable for production use.
ParticleContainerContainer本文假设你已熟悉。是一种特殊的叶节点:它在自身的数组中存储实例,且不接受普通PixiJS子元素。请使用而非;若需要将其与其他场景对象分组,请将整个包裹在中。
pixijs-scene-core-conceptsParticleContainerparticleChildrenParticleaddParticleaddChildParticleContainerContainerParticle API是v8中的新功能,但已可稳定用于生产环境。
Quick Start
快速开始
ts
const texture = await Assets.load("particle.png");
const container = new ParticleContainer({
texture,
boundsArea: new Rectangle(0, 0, app.screen.width, app.screen.height),
dynamicProperties: {
position: true,
rotation: false,
color: false,
},
});
for (let i = 0; i < 10000; i++) {
container.addParticle(
new Particle({
texture,
x: Math.random() * app.screen.width,
y: Math.random() * app.screen.height,
}),
);
}
app.stage.addChild(container);Related skills: (scene graph basics), (when you need full features per object), (shared textures, atlases), (batching, texture optimization), (wrap with other display objects).
pixijs-scene-core-conceptspixijs-scene-spritepixijs-assetspixijs-performancepixijs-scene-containerts
const texture = await Assets.load("particle.png");
const container = new ParticleContainer({
texture,
boundsArea: new Rectangle(0, 0, app.screen.width, app.screen.height),
dynamicProperties: {
position: true,
rotation: false,
color: false,
},
});
for (let i = 0; i < 10000; i++) {
container.addParticle(
new Particle({
texture,
x: Math.random() * app.screen.width,
y: Math.random() * app.screen.height,
}),
);
}
app.stage.addChild(container);相关技能: (场景图基础)、(当你需要每个对象具备完整功能时)、(共享纹理、图集)、(批处理、纹理优化)、(与其他显示对象一起包裹)。
pixijs-scene-core-conceptspixijs-scene-spritepixijs-assetspixijs-performancepixijs-scene-containerConstructor options
构造函数选项
ParticleContainerOptions
ParticleContainerOptions
All options (, , , , , , etc.) are also valid here — see . Note that is omitted: use instead.
ContainerpositionscaletintlabelfilterszIndexskills/pixijs-scene-core-concepts/references/constructor-options.mdchildrenparticles| Option | Type | Default | Description |
|---|---|---|---|
| | | Shared base texture for all particles. If omitted, the container falls back to the texture of the first particle added; every particle must share the same base texture source. |
| | | Initial array of |
| | | Flags for which particle attributes re-upload to the GPU every frame. Only |
| | | Rounds particle positions to the nearest pixel. Produces crisper rendering for pixel-art styles at the cost of smooth sub-pixel motion. |
| | default particle shader | Replaces the default particle shader. The custom shader must declare |
boundsAreaContainerParticleContainer(0, 0, 0, 0)boundsAreacontainsPoint所有选项(、、、、、等)在此处同样有效——详见。注意选项已被省略:请改用。
ContainerpositionscaletintlabelfilterszIndexskills/pixijs-scene-core-concepts/references/constructor-options.mdchildrenparticles| 选项 | 类型 | 默认值 | 说明 |
|---|---|---|---|
| | | 所有粒子共享的基础纹理。若省略,容器将回退为第一个添加的粒子的纹理;所有粒子必须共享同一基础纹理源。 |
| | | 初始的 |
| | | 控制哪些粒子属性会在每一帧重新上传至GPU的标记。默认仅 |
| | | 将粒子位置四舍五入到最近的像素。在像素艺术风格下能产生更清晰的渲染效果,但会牺牲平滑的亚像素运动。 |
| | 默认粒子着色器 | 替换默认的粒子着色器。自定义着色器必须声明 |
boundsAreaContainerParticleContainer(0, 0, 0, 0)boundsAreacontainsPointParticleOptions
ParticleOptions
ParticleContainerContainerOptions| Option | Type | Default | Description |
|---|---|---|---|
| | — | Required. Texture used to render this particle. All particles in the same |
| | | X position in the container's local space. |
| | | Y position in the container's local space. |
| | | Horizontal scale factor. |
| | | Vertical scale factor. |
| | | Horizontal anchor in 0–1 range; |
| | | Vertical anchor in 0–1 range; |
| | | Rotation in radians. |
| | | Tint color as hex number or CSS color string. Combined with |
| | | Transparency (0–1). Values outside the range are clamped. Combined with |
The constructor also accepts a bare as its sole argument (), which is shorthand for using the defaults above.
Texturenew Particle(texture)new Particle({ texture })Particle.defaultOptionsParticleContainerContainerOptions| 选项 | 类型 | 默认值 | 说明 |
|---|---|---|---|
| | — | 必填项。用于渲染该粒子的纹理。同一 |
| | | 粒子在容器局部空间中的X坐标。 |
| | | 粒子在容器局部空间中的Y坐标。 |
| | | 水平缩放因子。 |
| | | 垂直缩放因子。 |
| | | 水平锚点,范围0–1; |
| | | 垂直锚点,范围0–1; |
| | | 旋转角度(弧度)。 |
| | | 色调颜色,可用十六进制数或CSS颜色字符串表示。会与 |
| | | 透明度(0–1)。超出范围的值会被截断。会与 |
构造函数也接受单个作为唯一参数(),这是使用上述默认值的简写形式。
Texturenew Particle(texture)new Particle({ texture })Particle.defaultOptionsCore Patterns
核心模式
Particle creation
粒子创建
ts
const particle = new Particle({
texture,
x: 100,
y: 200,
scaleX: 0.5,
scaleY: 0.5,
anchorX: 0.5,
anchorY: 0.5,
rotation: Math.PI / 4,
tint: 0xff0000,
alpha: 0.8,
});
container.addParticle(particle);ParticlexyscaleXscaleYanchorXanchorYrotationcolortexturetintalphacolorYou can pass a directly as the sole argument: .
Texturenew Particle(texture)Override to change defaults globally:
Particle.defaultOptionsts
Particle.defaultOptions = {
...Particle.defaultOptions,
anchorX: 0.5,
anchorY: 0.5,
};ts
const particle = new Particle({
texture,
x: 100,
y: 200,
scaleX: 0.5,
scaleY: 0.5,
anchorX: 0.5,
anchorY: 0.5,
rotation: Math.PI / 4,
tint: 0xff0000,
alpha: 0.8,
});
container.addParticle(particle);ParticlexyscaleXscaleYanchorXanchorYrotationcolortexturetintalphacolor你可以直接传递作为唯一参数:。
Texturenew Particle(texture)通过重写来全局修改默认值:
Particle.defaultOptionsts
Particle.defaultOptions = {
...Particle.defaultOptions,
anchorX: 0.5,
anchorY: 0.5,
};Pre-populating with the particles option
使用particles选项预填充
ts
const particles = Array.from(
{ length: 10000 },
() =>
new Particle({
texture,
x: Math.random() * 800,
y: Math.random() * 600,
}),
);
const container = new ParticleContainer({
texture,
boundsArea: new Rectangle(0, 0, 800, 600),
particles,
});Passing in the constructor is equivalent to creating the container empty and calling for each one, but avoids per-call view updates.
particlesaddParticlets
const particles = Array.from(
{ length: 10000 },
() =>
new Particle({
texture,
x: Math.random() * 800,
y: Math.random() * 600,
}),
);
const container = new ParticleContainer({
texture,
boundsArea: new Rectangle(0, 0, 800, 600),
particles,
});在构造函数中传入等效于创建空容器后为每个粒子调用,但避免了每次调用的视图更新。
particlesaddParticleDynamic vs static properties and update()
动态与静态属性及update()
ts
const container = new ParticleContainer({
dynamicProperties: {
rotation: true,
},
});dynamicPropertiesParticleContainer.defaultOptions.dynamicPropertiests
{ vertex: false, position: true, rotation: false, uvs: false, color: false }You only need to override the properties you are animating; the rest inherit the defaults (position dynamic, everything else static). Five properties in total:
- : scale/anchor vertices
vertex positionrotation- : texture coordinates (for frame-swapped particles)
uvs - : tint and alpha
color
Mark only what you animate; static properties are cheaper. If you change a static property at runtime, call to re-upload:
container.update()ts
container.particleChildren.forEach((p) => {
p.tint = 0x00ff00;
});
container.update();ts
const container = new ParticleContainer({
dynamicProperties: {
rotation: true,
},
});dynamicPropertiesParticleContainer.defaultOptions.dynamicPropertiests
{ vertex: false, position: true, rotation: false, uvs: false, color: false }你只需重写需要动画的属性;其余属性继承默认值(位置为动态,其余为静态)。共有五个属性:
- :缩放/锚点顶点
vertex - :位置
position - :旋转
rotation - :纹理坐标(用于帧切换的粒子)
uvs - :色调与透明度
color
仅标记你需要动画的属性;静态属性的开销更低。如果在运行时修改了静态属性,请调用重新上传:
container.update()ts
container.particleChildren.forEach((p) => {
p.tint = 0x00ff00;
});
container.update();Batch operations on particleChildren
对particleChildren进行批量操作
ts
// Bulk add
const batch = [];
for (let i = 0; i < 5000; i++) {
batch.push(
new Particle({ texture, x: Math.random() * 800, y: Math.random() * 600 }),
);
}
container.particleChildren.push(...batch);
container.update();
// Bulk remove
container.particleChildren.length = 0;
container.update();addParticleaddParticleAtremoveParticleremoveParticleAtremoveParticlesupdate()ts
// 批量添加
const batch = [];
for (let i = 0; i < 5000; i++) {
batch.push(
new Particle({ texture, x: Math.random() * 800, y: Math.random() * 600 }),
);
}
container.particleChildren.push(...batch);
container.update();
// 批量移除
container.particleChildren.length = 0;
container.update();addParticleaddParticleAtremoveParticleremoveParticleAtremoveParticlesupdate()Texture and shader options
纹理与着色器选项
textureParticleContainerOptionsts
const container = new ParticleContainer({ texture });shaderShaderaPositionaUVaColordynamicPropertiests
const container = new ParticleContainer({ texture, shader: myCustomShader });ParticleContainerOptionstexturets
const container = new ParticleContainer({ texture });shaderShaderaPositionaUVaColordynamicPropertiests
const container = new ParticleContainer({ texture, shader: myCustomShader });Limitations
局限性
ParticleContainer- No filters, masks, or blend modes on individual particles.
- No nested children on particles.
- No automatic bounds calculation.
- All particles must share the same base texture source (atlases work; multiple unrelated textures do not).
- Custom shaders are supported via the option.
shader
ParticleContainer- 单个粒子不支持滤镜、遮罩或混合模式。
- 粒子不支持嵌套子元素。
- 不支持自动边界计算。
- 所有粒子必须共享同一基础纹理源(图集可行;多个不相关的纹理不可行)。
- 通过选项支持自定义着色器。
shader
Container method migration
容器方法迁移
ParticleContainerContainerParticleContainer| Standard Container method | ParticleContainer equivalent |
|---|---|
| |
| |
| |
| |
| |
| Access |
| Not available |
| Not available |
ParticleContainerContainerParticleContainer| 标准Container方法 | ParticleContainer等效方法 |
|---|---|
| |
| |
| |
| |
| |
| 直接访问 |
| 不可用 |
| 不可用 |
Common Mistakes
常见错误
[CRITICAL] Adding Sprites to ParticleContainer
[严重] 向ParticleContainer添加Sprite
Wrong:
ts
const container = new ParticleContainer();
const sprite = new Sprite(texture);
container.addChild(sprite);Correct:
ts
const container = new ParticleContainer();
const particle = new Particle(texture);
container.addParticle(particle);ParticleContainerSpriteaddChildParticleIParticleaddParticleParticleContainerSprite错误示例:
ts
const container = new ParticleContainer();
const sprite = new Sprite(texture);
container.addChild(sprite);正确示例:
ts
const container = new ParticleContainer();
const particle = new Particle(texture);
container.addParticle(particle);ParticleContainerSpriteaddChildParticleIParticleaddParticleParticleContainerSprite[HIGH] Not setting boundsArea on ParticleContainer
[高风险] 不为ParticleContainer设置boundsArea
Wrong:
ts
const container = new ParticleContainer();
// bounds is always (0, 0, 0, 0) — culling and hit testing failCorrect:
ts
const container = new ParticleContainer({
boundsArea: new Rectangle(0, 0, 800, 600),
});ParticleContainer(0, 0, 0, 0)boundsAreacontainsPointboundsArea错误示例:
ts
const container = new ParticleContainer();
// 边界始终为(0, 0, 0, 0) —— 剔除和点击测试会失败正确示例:
ts
const container = new ParticleContainer({
boundsArea: new Rectangle(0, 0, 800, 600),
});ParticleContainer(0, 0, 0, 0)boundsAreacontainsPointboundsArea[HIGH] Using children instead of particleChildren
[高风险] 使用children而非particleChildren
Wrong:
ts
container.addParticle(new Particle(texture));
console.log(container.children.length); // 0Correct:
ts
container.addParticle(new Particle(texture));
console.log(container.particleChildren.length); // 1Particles are stored in the array, not . The standard array is empty on a . All particle enumeration, counting, and manipulation must use plus the methods.
particleChildrenchildrenContainer.childrenParticleContainerparticleChildren*Particle错误示例:
ts
container.addParticle(new Particle(texture));
console.log(container.children.length); // 0正确示例:
ts
container.addParticle(new Particle(texture));
console.log(container.particleChildren.length); // 1粒子存储在数组中,而非。标准数组在上是空的。所有粒子的枚举、计数和操作都必须使用加上方法。
particleChildrenchildrenContainer.childrenParticleContainerparticleChildren*Particle[MEDIUM] Do not use ParticleContainer as a normal container
[中等风险] 不要将ParticleContainer当作普通容器使用
ParticleContainerParticleContainerContainerts
const world = new Container();
world.addChild(backgroundSprite, particleContainer, uiLayer);ParticleContainerParticleContainerContainerts
const world = new Container();
world.addChild(backgroundSprite, particleContainer, uiLayer);