Skip to content

Commit 30fa03e

Browse files
authored
Merge pull request #18 from flycran/feat/convex-mesh
feat: 字体碰撞网格改用 poly-decomp-es 凸分解,替换 earcut 三角网格
2 parents f1f51ef + a15e227 commit 30fa03e

9 files changed

Lines changed: 522 additions & 674 deletions

File tree

‎README.md‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -119,10 +119,10 @@ that point.
119119

120120
`font-path` swaps the collision font. It accepts a local `.ttf`/`.otf` path or an `http(s)` URL.
121121
The bundled Liberation Serif Bold is used by default because its contours are clean and
122-
reliably triangulated. Custom fonts are risky:
122+
decompose stably. Custom fonts are not guaranteed to work:
123123

124-
- Contours that are not closed or that self-intersect are silently tolerated by most
125-
renderers, but the triangulation used for the physics collider rejects them.
124+
- Fonts with irregular contours (open or self-intersecting) may decompose into unreliable
125+
collision shapes.
126126
- Some fonts cannot be parsed by opentype.js at all (common ones such as DejaVu fail on their
127127
GSUB tables).
128128
- Different font metrics usually require re-tuning `font-size` and `text-top`.

‎README_zh.md‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -108,9 +108,9 @@ web preview 里验证。值中不能包含花括号、分号或 `]]>`。
108108
限制模拟时长,仍能拿到该时刻为止的轨迹。
109109

110110
`font-path` 用于替换碰撞字体,可以给本地 `.ttf`/`.otf` 路径,也可以直接给 `http(s)` 链接。
111-
默认使用内置的 Liberation Serif Bold,因为它轮廓规整、能稳定三角化。自定义字体有风险:
111+
默认使用内置的 Liberation Serif Bold,因为它轮廓规整、分解结果稳定。自定义字体不保证可用:
112112

113-
- 未闭合或自相交的轮廓,大多数渲染器会静默忽略,但物理碰撞体使用的三角化算法会直接报错。
113+
- 轮廓不规整(未闭合、自相交等)的字体,碰撞体的分解结果可能不可靠、形状异常。
114114
- 部分字体根本无法被 opentype.js 解析(例如很常见的 DejaVu 系列,会因 GSUB 表解析失败)。
115115
- 字体度量不同,通常需要重新调整 `font-size` 和 `text-top`。
116116

‎__tests__/parseTextPath.test.ts‎

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -69,19 +69,18 @@ describe('getTextPaths', () => {
6969
)
7070
})
7171

72-
it('emits path data, a bounding box and a triangulated mesh per glyph', async () => {
72+
it('emits path data, a bounding box and a convex mesh per glyph', async () => {
7373
const [glyph] = await getTextPaths('H', 40, DEFAULT_OPTIONS.font)
7474

7575
expect(glyph.pathData.startsWith('M')).toBe(true)
7676
expect(glyph.rect.width).toBeGreaterThan(0)
7777
expect(glyph.rect.height).toBeGreaterThan(0)
7878

79-
// 三角网格与同一份轮廓一一对应:索引按三元组组织,且不越界
80-
const vertexCount = glyph.paths.vertices.length / 2
81-
const indices = Array.from(glyph.paths.indices)
82-
expect(vertexCount).toBeGreaterThan(0)
83-
expect(indices.length % 3).toBe(0)
84-
expect(Math.max(...indices)).toBeLessThan(vertexCount)
79+
// 凸网格与同一份轮廓对应:每个凸块都是至少三个顶点的多边形
80+
expect(glyph.paths.polygons.length).toBeGreaterThan(0)
81+
for (const polygon of glyph.paths.polygons) {
82+
expect(polygon.length).toBeGreaterThanOrEqual(3)
83+
}
8584
})
8685

8786
it('returns one entry per glyph and scales with font size', async () => {

‎bun.lock‎

Lines changed: 3 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)