pixijs-scene-mesh

pixijs-scene-mesh

热门

在 PixiJS v8 中渲染自定义几何体时使用此技能。包含具有 MeshGeometry(positions、uvs、indices、topology)的 Mesh、用于逐帧顶点动画的 MeshSimple、用于细分变形的 MeshPlane、用于沿路径跟随纹理的 MeshRope 以及用于 2.5D 角点的 PerspectiveMesh。触发条件:Mesh、MeshGeometry、MeshSimple、MeshPlane、MeshRope、PerspectiveMesh、positions、uvs、indices、topology、setCorners、构造函数选项、MeshOptions、MeshPlaneOptions、MeshRopeOptions、SimpleMeshOptions、PerspectivePlaneOptions。

293Star
16Fork
更新于 2026/6/4
SKILL.md
只读
名称
pixijs-scene-mesh
描述

在 PixiJS v8 中渲染自定义几何体时使用此技能。包含具有 MeshGeometry(positions、uvs、indices、topology)的 Mesh、用于逐帧顶点动画的 MeshSimple、用于细分变形的 MeshPlane、用于沿路径跟随纹理的 MeshRope 以及用于 2.5D 角点的 PerspectiveMesh。触发条件:Mesh、MeshGeometry、MeshSimple、MeshPlane、MeshRope、PerspectiveMesh、positions、uvs、indices、topology、setCorners、构造函数选项、MeshOptions、MeshPlaneOptions、MeshRopeOptions、SimpleMeshOptions、PerspectivePlaneOptions。

Mesh 可以通过纹理或自定义着色器渲染任意 2D(或透视投影的)几何体。PixiJS 提供了基础 Mesh 类以及针对常见形状的四个专业子类:MeshSimple、MeshPlane、MeshRope 和 PerspectiveMesh。选择符合你形状需求的子类;当需要完全的顶点级控制或自定义着色器时,请使用基础 Mesh。

假设你已熟悉 pixijs-scene-core-concepts。Mesh 是叶节点;它们不能包含子节点。如需将多个 Mesh 分组,请将它们封装在 Container 中。

快速入门

const texture = await Assets.load("pattern.png");

const geometry = new MeshGeometry({
  positions: new Float32Array([0, 0, 100, 0, 100, 100, 0, 100]),
  uvs: new Float32Array([0, 0, 1, 0, 1, 1, 0, 1]),
  indices: new Uint32Array([0, 1, 2, 0, 2, 3]),
  topology: "triangle-list",
});

const mesh = new Mesh({
  geometry,
  texture,
  roundPixels: false,
});
app.stage.addChild(mesh);

每个 Mesh 子类都接收单个选项对象。基础 Mesh 需要提供 geometry;子类(MeshSimple、MeshPlane、MeshRope、PerspectiveMesh)会在内部构建几何体,因此需要传入 texture。完整字段列表请参阅各变体的参考文档。

变体

变体 适用场景 权衡 参考文档
Mesh 完全控制、自定义几何体、自定义着色器 需要自己构建 MeshGeometry references/mesh.md
MeshSimple 带有逐帧顶点动画的快速纹理形状 轻量封装;自动更新顶点缓冲区 references/mesh-simple.md
MeshPlane 用于扭曲效果的细分纹理矩形 拓扑结构固定;由 verticesX/verticesY 控制密度 references/mesh-plane.md
MeshRope 沿折线路径跟随的纹理 每个点都会弯曲;平滑曲线需要大量控制点 references/mesh-rope.md
PerspectiveMesh 具有透视角点的 2D 平面 非真正 3D;仅在 UV 层面上进行透视纠正 references/mesh-perspective.md

如何选择

  • “我需要一个带纹理的四边形” → 使用 Sprite(参见 pixijs-scene-sprite),而非 Mesh。Mesh 适用于 Sprite 无法胜任的场景。
  • “我需要变形一个纹理矩形” → 使用 MeshPlane。设置 verticesX/verticesY 以达到所需的平滑度。
  • “我需要一条跟随多点移动的绳子或轨迹” → 使用 MeshRope。通过 width 控制厚度;使用 textureScale: 0 拉伸纹理,或 > 0 重复纹理。
  • “我需要倾斜的 2D 卡片或地面” → 使用 PerspectiveMesh。传入四个角点位置;虽然不是真正 3D,但对于 2.5D 效果已足够好。
  • “我需要一个简单形状且具有逐帧动画的顶点” → 使用 MeshSimple。它会自动为你处理缓冲区更新流程。
  • “我需要自定义着色器或特殊的几何体” → 使用带有手动构建的 MeshGeometry 的基础 Mesh。有关着色器编写请参见 pixijs-custom-rendering。
  • “我需要真正的 3D 渲染” → 请使用专门的 3D 库。PerspectiveMesh 仅在 UV 级别模拟透视,没有深度缓冲区。

快速概念

MeshGeometry 拥有顶点数据

MeshGeometry 包含 positions、uvs、indices 和 topology。你可以在多个 Mesh 实例之间共享同一个 geometry;positions 具有引用计数。

批处理 (Batching)

仅当 Mesh 使用 MeshGeometry、没有自定义着色器、没有深度或剔除状态,且符合 'auto' 规则(batchMode = 'auto' 且顶点数 ≤100)时,才会进行批处理(与其他绘制调用合并)。自定义着色器始终独立渲染。

拓扑结构属于 geometry,而非 mesh

new MeshGeometry({ topology: 'triangle-strip' });拓扑结构是 geometry 的属性。默认值为 'triangle-list';如果你的数据组织方式不同,请明确指定。

额外控制项

  • new MeshGeometry({ shrinkBuffersToFit: true }) — 创建时将 GPU 缓冲区存储空间裁剪为实际顶点数量。在传入大型一次性几何体时使用该项。
  • Mesh.containsPoint(point) — 感知拓扑结构的碰撞检测,通过遍历三角形进行判断。适用于任何 MeshGeometry,包括自定义布局。
  • new Mesh({ geometry, state }) — 传入 State 对象以控制混合、深度和剔除。如果设置了深度或剔除标记,批处理将自动禁用。省略时默认为 State.for2d()。

常见错误

[高风险] 使用旧版 SimpleMesh / SimplePlane / SimpleRope 名称

错误:

import { SimpleRope } from "pixi.js";
const rope = new SimpleRope(texture, points);

正确:

import { MeshRope } from "pixi.js";
const rope = new MeshRope({ texture, points });

v8 中的重命名:SimpleMesh → MeshSimple,SimplePlane → MeshPlane,SimpleRope → MeshRope。全部改用选项对象构造函数。

[高风险] 为 MeshGeometry 使用位置构造函数参数

错误:

const geom = new MeshGeometry(vertices, uvs, indices);

正确:

const geom = new MeshGeometry({
  positions: vertices,
  uvs,
  indices,
  topology: "triangle-list",
});

v8 使用选项对象。注意属性名为 positions 而非 vertices;vertices 名称仅在 MeshSimple 中使用。

[中风险] 向 mesh 添加子节点

错误:

mesh.addChild(otherMesh);

正确:

const group = new Container();
group.addChild(mesh, otherMesh);

Mesh 设置了 allowChildren = false。添加子节点会打印弃用警告。请在普通的 Container 内对 mesh 进行分组。

API 参考