Skip to content

Examples: Add webgpu_compute_cloth #31123

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
May 18, 2025
Merged

Examples: Add webgpu_compute_cloth #31123

merged 8 commits into from
May 18, 2025

Conversation

holtsetio
Copy link
Contributor

@holtsetio holtsetio commented May 17, 2025

Description

This adds a simple cloth simulation example. It uses a verlet system running in compute shaders and is also a nice showcase for MeshPhysicalNodeMaterial's sheen properties.

live example

@holtsetio
Copy link
Contributor Author

holtsetio commented May 17, 2025

Possible improvements:

  • Add a ground and shadows
  • right now the verlet vertex forces are accumulated by looping over the affecting springs. Using fixed point atomics instead might be a bit slower and less precise, but the code might be easier to follow.
  • Right now the cloth mesh is a two-sided plane. Giving it a little bit of depth/volume would look way better, but code would become more verbose.

@RenaudRohlinger
Copy link
Collaborator

RenaudRohlinger commented May 18, 2025

The example is currently broken in WebGL due to exceeding the limit on instancedAttributes. In WebGL2, the maximum number of separate transform feedback varyings is typically 4. To work around this, we could use interleaved mode with transform feedback, which allows up to 64 components via MAX_TRANSFORM_FEEDBACK_INTERLEAVED_COMPONENTS.

But switching to interleaved mode would increase complexity and the webgl fallback of the compute shaders is already tricky so I would avoid such change.

For now, a simpler fix is to pack multiple uint values into a single uvec4 instanced attribute, which should keep us within the WebGL2 limits and get the fallback working again without major changes.

@sunag sunag added this to the r177 milestone May 18, 2025
@sunag sunag changed the title Add webgpu_compute_cloth example Add webgpu_compute_cloth example May 18, 2025
@sunag sunag changed the title Add webgpu_compute_cloth example Examples: Add webgpu_compute_cloth May 18, 2025
@sunag
Copy link
Collaborator

sunag commented May 18, 2025

I followed your other examples on X, they are all amazing, thanks for sharing.

I made a small update to the way normalView was being replaced.

@sunag sunag merged commit cd3aa0d into mrdoob:dev May 18, 2025
11 checks passed
@holtsetio holtsetio deleted the example-cloth branch May 24, 2025 10:47
@kungfooman
Copy link

Super cool example! I implemented verlet physics once too after I figured out that the Hitman series used it (at least in the oldest releases). I would love to see more verlet physics for everything - you can even use it for skeletal animation blending e.g.! ❣️ Will you work more with verlet?

Even tho I'm on Linux and WebGPU is broken on my setup, this still works anyway 🤯 Does it just fallback to WebGL? I guess than it's not just "WebGPU".

RuthySheffi pushed a commit to RuthySheffi/three.js that referenced this pull request Jun 5, 2025
* Add webgpu_compute_cloth example

* fix render calls

* remove metalness

* Add exception to puppeteer.js

* Fix the cloth demo in WebGL2 Fallback

* update normalView replacement approach

* cleanup

* update method

---------
RuthySheffi pushed a commit to RuthySheffi/three.js that referenced this pull request Jun 5, 2025
* Add webgpu_compute_cloth example

* fix render calls

* remove metalness

* Add exception to puppeteer.js

* Fix the cloth demo in WebGL2 Fallback

* update normalView replacement approach

* cleanup

* update method

---------
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants