使用 XBPD 的布料模拟
Cloth Simulation with XBPD

原始链接: https://ricar.do/2026/11/26/cloth-sim.html

作者使用 sokol_gfx 构建了原生 3D 引擎 **drie**,其中 XPBD 布料模拟运行在计算着色器中。主要特性包括确定性求解器、折痕保持、粗模拟/精细显示效率、双向碰撞(布料与物体及其他布料)、空气动力学阻力,以及空闲布料的休眠优化。 仍存在两个主要问题。**缠结:**基于点的碰撞允许点穿过粗网格的间隙;空间哈希将不同布料实体的点推开以防止合并。**撕裂:**点穿过尖锐或薄的特征,被基于 tether 的拖拽手柄加剧;作者正在实验使用 SDF 来圆化角点,但尚未解决。 演示展示了该系统:手帕(自碰撞加上体积雾和 AO 等引擎特性)、家具覆盖(两种弯曲模型)和横幅(向布料发射球体)。所有示例均用 C3 编写,并通过 emscripten 使用 sokol 的 WebGPU 后端编译;开发过程中大量使用了 Claude Code 等 LLM。

A Hacker News 帖子展示了 ricardobeat 的“Cloth Simulation with XBPD”(XPBD 的笔误),获得 65 点和 20 条评论。该演示展示了基于物理的 XPBD 布料模拟,带自碰撞,目标是 WebGPU。评论者称赞其性能,包括在 iPhone 上流畅运行,并欣赏手帕演示的光照,该光照使用了 froxel 体积雾、辉光和镜头光晕效果。 讨论涵盖多个主题:一名用户请求更深入的布料动画教育可视化;其他人讨论虚拟试穿初创公司及其采用困难的原因,提到光照不匹配和不自然的结果。笔误争论指出“PBD”代表 Position Based Dynamics,而非“XBPD”。用户还分享了不依赖 WebGPU 的替代模拟器、一个链接的弹珠游戏,并指出 Chrome/Firefox 上有 10 倍速 bug。作者承认布料的弹跳略带橡胶感,网页精度降低,调优仍在进行中。总体而言,该帖子结合了对技术的赞赏与布料模拟和虚拟试穿的更广泛行业背景。
相关文章

原文

I’ve been writing a small native 3D engine called drie for the past couple weeks. It uses sokol_gfx for rendering, and I recently decided to take a stab at implementing a cloth simulation system.

It’s an XPBD (extended position-based dynamics) solver; the version you see here runs in compute shaders, but was originally running in the CPU — surprisingly the performance gap was not that large, but doing in the GPU makes it less susceptible to stutter.

Features

  • deterministic solver (helpful for replaying game scenes)
  • crease preserving, so pleated curtains remember their shape
  • coarse simulation, fine display for efficiency (tunable)
  • two-way collision with objects and other cloth
  • aerodynamic drag
  • sleep - saves compute when nothing is going on

The two-way collision is pretty sweet - as far as I’ve researched, very few engines have it without add-ons. The “Handkerchief” demo below shows what is possible with that.

Problem 1: tangles

The tricky part is keeping the simulation from tangling itself, as point-based collision still lets individual points slip through gaps in the coarse mesh (aka the cloth folds onto itself irreversibly). After a lot of manual tuning, I’ve found a setup that seems to prevent them most of the time, and the crease-remembering feature helps prevent these in the first place.

The same problem causes two sheets to “merge” together when laid on top of each other. To stop this, a spatial hash is used to compute collisions between cloth entities; points that come into the same region are pushed apart.

Problem 2: rips

In a similar fashion, points can slip through another object, especially sharp corners or thin features (the spacing between points is roughly 1.7cm in these demos). It is made worse by the fact the drag handles are implemented as a tether, where other points of the cloth are connected directly to it and move in the direction of the pull. The “right” way to do it, allowing the force to cascade through each of the points, unfortunately makes everything feel like jello; more accurate cloth sims avoid this by calculating the entire thing on every frame, which is unfeasible in real-time.

Cloth ripping through the corner of a table

I am currently experiment with using SDFs (Signed Distance Fields - short intro here) to replace the collision meshes, as they naturally round out all the corners, but it’s still an unresolved issue - try dragging the cloth across the small table in the furniture demo and you’ll see it.

Demos

These require a WebGPU enabled browser. Should work fine in Firefox, Chrome, Helium, Safari on Mac and iPhone. Otherwise click the “Video” option to see a short capture.

Handkerchiefs

A good test for the self-collision. Also shows some of the engine’s features: sunlight, volumetric fog, dynamic shadows, contact shadows, ambient occlusion, displacement maps, and more.

Use WASD + Q/E to move the camera, or click-and-hold with the right mouse button. Drag a cloth to pick it up.

or open this in fullscreen

Furniture drape

R to restart, B to switch between two bend models for the cloth. “Signed” mode holds up better (check the creases around the sofa), but is a bit more expensive to compute.

Banners

Click to fire off spheres, C to cut them off.

Tech

All examples are written in C3, and compiled for web using sokol’s WebGPU backend and the native c3c compiler emscripten output.

I used Claude Code to write most of these (but not to write any part of this post 🤮), the engine itself is developed using a melting pot of LLMs. Lazy ideas like “can we sample a more coarse grid in a separate pass” become a 5 minute experiment instead of a days-long quest.

联系我们 contact @ memedata.com