55 RigidBodyDesc ,
66 World
77} from '@dimforge/rapier2d-compat'
8+ import type { Polygon } from 'poly-decomp-es'
89import {
910 DEFAULT_OPTIONS ,
1011 FRICTION ,
@@ -147,8 +148,8 @@ async function createWord(width: number, height: number) {
147148
148149export interface TextPathResult {
149150 paths : {
150- vertices : Float32Array
151- indices : Uint32Array
151+ /** 字形子轮廓凹分解出的凸多边形集合(字形局部坐标,y 轴向下) */
152+ polygons : Polygon [ ]
152153 }
153154 pathData : string
154155 rect : {
@@ -217,8 +218,6 @@ export interface SimulationResult {
217218 time : number
218219 textPaths : Pick < TextPathResult , 'rect' | 'pathData' > [ ]
219220 contributions : ContributionJson [ ]
220- /** 用户名文字碰撞体的三角网格(世界坐标),用于 web 端渲染 */
221- textTriangles : number [ ] [ ]
222221 /** 文字在物理世界中的 X 偏移量 */
223222 offsetX : number
224223 /** 文字在物理世界中的 Y 偏移量 */
@@ -277,8 +276,8 @@ export interface SimulationOptions {
277276 * 自定义字体(.ttf/.otf):本地路径、远程 URL(字符串或 URL 对象),或
278277 * 已读取好的 Buffer / ArrayBuffer(如 `files[0].arrayBuffer()` 的结果),
279278 * 默认使用内置的 Liberation Serif Bold。
280- * 谨慎修改:部分字体存在未闭合或自相交的轮廓,操作系统渲染时会自动忽略这些瑕疵,
281- * 但本项目的轮廓三角化算法会直接报错; 另有部分字体因 opentype.js 的 GSUB 解析限制无法使用。
279+ * 谨慎修改:轮廓不规整(未闭合、自相交等)的字体,分解出的碰撞形状可能不可靠;
280+ * 另有部分字体因 opentype.js 的 GSUB 解析限制无法使用。
282281 * 字体度量差异也可能导致文字位置和碰撞形态需要重新调整
283282 */
284283 font ?: FontSource
@@ -290,8 +289,6 @@ export interface SimulationWorld {
290289 /** 贡献方块(含轨迹记录器),每步迭代后调用 update 采集轨迹 */
291290 contributions : Contribution [ ]
292291 textPaths : TextPathResult [ ]
293- /** 文字碰撞体的三角网格(世界坐标),用于 web 端渲染 */
294- textTriangles : number [ ] [ ]
295292 /** 文字在物理世界中的 X 偏移量 */
296293 offsetX : number
297294 /** 文字在物理世界中的 Y 偏移量 */
@@ -321,7 +318,7 @@ export async function createSimulationWorld(
321318 const world = await createWord ( width , height )
322319 const contributions : Contribution [ ] = [ ]
323320
324- // 创建用户名文字静止刚体 (居中对齐),同时收集三角网格数据
321+ // 创建用户名文字静止碰撞体 (居中对齐)
325322 const textPaths = await getTextPaths ( text , fontSize , font )
326323
327324 const { centerY, offsetX } = getTextBoundBox ( textPaths , {
@@ -330,35 +327,24 @@ export async function createSimulationWorld(
330327 textTop
331328 } )
332329
333- const textTriangles : number [ ] [ ] = [ ]
334-
335330 for ( const { paths } of textPaths ) {
336- const { vertices, indices } = paths
337- if ( ! vertices . length && ! indices . length ) continue
338- // 翻转 Y 轴:opentype 字形在基线下方(y 为负),物理世界需要正向
339- const flippedVertices = new Float32Array ( vertices . length )
340- for ( let i = 0 ; i < vertices . length ; i += 2 ) {
341- flippedVertices [ i ] = vertices [ i ]
342- flippedVertices [ i + 1 ] = - vertices [ i + 1 ]
343- }
344- world . createCollider (
345- ColliderDesc . trimesh ( flippedVertices , indices ) . setFriction ( FRICTION ) ,
346- world . createRigidBody (
347- RigidBodyDesc . fixed ( ) . setTranslation ( offsetX , centerY )
348- )
331+ const { polygons } = paths
332+ if ( ! polygons . length ) continue
333+ // 同一字形的全部凸块挂在共享的静止刚体上,作为 compound collider 参与碰撞
334+ const textBody = world . createRigidBody (
335+ RigidBodyDesc . fixed ( ) . setTranslation ( offsetX , centerY )
349336 )
350- for ( let i = 0 ; i < indices . length ; i += 3 ) {
351- const i0 = indices [ i ] * 2 ,
352- i1 = indices [ i + 1 ] * 2 ,
353- i2 = indices [ i + 2 ] * 2
354- textTriangles . push ( [
355- offsetX + flippedVertices [ i0 ] ,
356- centerY + flippedVertices [ i0 + 1 ] ,
357- offsetX + flippedVertices [ i1 ] ,
358- centerY + flippedVertices [ i1 + 1 ] ,
359- offsetX + flippedVertices [ i2 ] ,
360- centerY + flippedVertices [ i2 + 1 ]
361- ] )
337+ for ( const polygon of polygons ) {
338+ // 翻转 Y 轴:opentype 字形在基线下方(y 为负),物理世界需要正向
339+ const flippedVertices = new Float32Array ( polygon . length * 2 )
340+ for ( let i = 0 ; i < polygon . length ; i ++ ) {
341+ flippedVertices [ i * 2 ] = polygon [ i ] [ 0 ]
342+ flippedVertices [ i * 2 + 1 ] = - polygon [ i ] [ 1 ]
343+ }
344+ // 顶点近乎共线的退化凸块求不出凸包,跳过
345+ const colliderDesc = ColliderDesc . convexHull ( flippedVertices )
346+ if ( ! colliderDesc ) continue
347+ world . createCollider ( colliderDesc . setFriction ( FRICTION ) , textBody )
362348 }
363349 }
364350
@@ -413,7 +399,6 @@ export async function createSimulationWorld(
413399 world,
414400 contributions,
415401 textPaths,
416- textTriangles,
417402 offsetX,
418403 centerY
419404 }
@@ -430,7 +415,7 @@ export async function startSimulation(
430415 maxSimulationTime,
431416 speed
432417 } = withDefaults ( DEFAULT_OPTIONS , options )
433- const { world, contributions, textPaths, textTriangles , offsetX, centerY } =
418+ const { world, contributions, textPaths, offsetX, centerY } =
434419 await createSimulationWorld ( options )
435420
436421 // 容差从 SVG 像素换算回物理坐标单位,让压缩函数与渲染缩放解耦;
@@ -504,7 +489,6 @@ export async function startSimulation(
504489 pathData : pathData ,
505490 rect : rect
506491 } ) ) ,
507- textTriangles,
508492 offsetX,
509493 centerY
510494 }
0 commit comments