Skip to content

Examples: Simplify WebGPU Cloth Simulation with a re-usable class.#32964

Open
bandinopla wants to merge 10 commits intomrdoob:devfrom
bandinopla:example-clothsim-webgpu
Open

Examples: Simplify WebGPU Cloth Simulation with a re-usable class.#32964
bandinopla wants to merge 10 commits intomrdoob:devfrom
bandinopla:example-clothsim-webgpu

Conversation

@bandinopla
Copy link

@bandinopla bandinopla commented Feb 5, 2026

image

Encapsulated the functionality into a new object ClothSimulator so that the cloth functionality can be easily used by other users.

  • Added normals to the cloth's geometry so it can cast and receive shadows and be textured.
  • More than 1 sphere collider can be used ( useful to wrap some object with spheres to provide collision with dynamic objects ) and if 2 cloth's share the same position on their colliders it will give the illusion of being both in the same physical world.
  • Made the cloth move in world space to allow moving it or rotate it and seeing it flow.


import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
import { UltraHDRLoader } from 'three/addons/loaders/UltraHDRLoader.js';
import { ClothSimulator } from 'three/addons/objects/ClothSimulator.js';
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Better to place this module in three/addons/misc/ directory.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't a cloth the same as a water sim? Similar, I mean... three/addons/objects has 4 classes related to water. Why cloth should be in misc? It is an object, a cloth object. A mesh. It sounds like it should be in objects and looks like misc is more for things that are not literal objects in the scene, more like calculation managers.

@Mugen87
Copy link
Collaborator

Mugen87 commented Feb 5, 2026

I'm not sure if this refactoring is appropriate. ClothSimulator contains a lot of use case specific code which makes less suitable for a reusable component.

@mrdoob
Copy link
Owner

mrdoob commented Feb 5, 2026

World be great to have this as a reusable component indeed.
No one is going to use it otherwise.

@bandinopla
Copy link
Author

I'm not sure if this refactoring is appropriate. ClothSimulator contains a lot of use case specific code which makes less suitable for a reusable component.

what is the "lots of use case specific code " you see that makes it not reusable? can I get a few examples? I'm failing to see it.

@bandinopla
Copy link
Author

World be great to have this as a reusable component indeed. No one is going to use it otherwise.

What is the current failure to do so in the current code? I'm scratching my head on this one. I'm seeing a BufferGeometry mesh with a position and normal nodes. I can instantiate more than 1, each having their own settings... em I missing something?

@Mugen87
Copy link
Collaborator

Mugen87 commented Feb 5, 2026

E.g. what if users want other colliders than a sphere like box or capsule?

The mesh's material type (MeshPhysicalNodeMaterial) is currently hard-coded. Is there are way to configure a different material type?

@bandinopla
Copy link
Author

E.g. what if users want other colliders than a sphere like box or capsule?

That goes beyond the scope of the implementation of the original example that uses spheres to ease the calculations using distances only. I could argue that many other classes in the code base could also be faced against those same type of questions. The "what ifs" are very generous and fit in most holes. Any other issue besides that? I'm trying to learn from the masters.

* @param {Vector3} position Position of the sphere
* @param {number} [radius] Optional new radius
*/
setSphereCollider( index, position, radius ) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This method should accept an instance of THREE.Sphere.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

that would be overkill since the "colliders" are just used to get their position and radius in the computeVertexForces and calculate a push direction force.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added the possibility of either THREE.Sphere or the usual Vector3+radius combo. Still think it is overkill I will doubt myself on this one and just do it since it does make it look more "Three-like".

* Create visualization meshes for sphere colliders
* @returns Array of meshes that can be added to the scene
*/
createSphereVisualizers() {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe createSphereHelper()?

The actual visualization of the collider is app specific and should not be done by this component, imo.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The use of that method is for when the need to "visualize the spheres" arises. They are not really "helpers" since they don't do anything like a gizmo would more than just visualize. "Helper" is secondary to "Visualize" since the main goal is to "Visualize". In my humble opinion a "helper" is an object that helps you "do" something.

@bandinopla
Copy link
Author

MeshPhysicalNodeMaterial

I could make it so the options have a new optional property named "material" and clarify in the JSDoc that it must be a NodeMaterial and the positionNode and normalNode will be overriten ?

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.

3 participants