S&box Wiki

Cloth Physics

We advise against using this. It may be changed or removed in a future update. The tools used in this guide are outdated and experimental, there may be a better way to do cloth physics, or they may be reworked/removed entirely.

Setting Up

Some of the work will be done outside of ModelDoc, "Blender" is recommended as we'll be using its "Source Export Tools", more specifically a modified version of Rectus' Source tools, made by Moonded on GitHub (on the right you can download the latest version under "Releases").

As of writing this, the Source 2 Tools are outdated, you will need to revert your Blender version for the tools to work

The first step will require you to mark which part of the mesh will be affected by the Proxy, this will be the actual piece of cloth:

  1. In "Scene Properties", under "Source Engine Exportables" and "Float Maps", add cloth_enable by pressing the +
84e1a780559f53ac17fd20f47784983ffbda6ccf.png
  1. Select the desired part of the mesh
  2. Assign the selected mesh to the "cloth_enable" vertex group
image.jpg

You're done with the main mesh, you can now export it (although it's suggested you keep it, as you're going to need it to create the Proxy), the only format that currently supports cloth physics is ".DMX", its version is also important, this being "Binary 3" (or any higher version) and "Model 22".

3a41232745011407b18ec90e90979ad864b2715b.png

Creating the Proximity Mesh

The "Proximity mesh" ("Proxy" for short) is a cheap mesh that's used for cloth simulation.

Cloth nodes are generated based on the Proxy's vertices, which are used to deform the main mesh, it is important to keep the Proxy as low-poly as possible without compromising functionality.

While one can be made from scratch, it is highly recommended you make one by duplicating it from the original mesh, this way all of the vertex groups are preserved, which are supremely important for this process.

It is recommended for the proxy to be made up of mostly quads, tris will result in bad vertex shading on the final mesh, while ngons will render it completely unusable.
  1. Select the mesh assigned to the "cloth_enable" vertex group
  2. Duplicate it, in Blender, this is done with ⇧ shift+d, press "mouse 2" to leave the mesh in place.
  3. Separate the duplicated mesh from the main mesh, in Blender, this is done with P > Selection, this will become the Proxy
  4. Lower polycount, In Blender (2.8+) you have the option to un-subdivide mesh with ctrl+e > Un-Subdivide
  5. If the resulting mesh seems to differ slightly in shape, you can add the "Shrinkwrap" modifier and select the main mesh as target, the vertices will be projected onto the nearest surface for greater accuracy

The cloth will also need to stay fixed onto the model, else it will fall off, to do this we use another Vertex group called "cloth_goal_strength_v2", as the name implies, it's used for "strength" or "stiffness", the value you assign to the vertices is indicative to how much they'll follow their original parent bones, with 0 being none at all and 1 being fully, these will serve as your cloth's anchor point. Since the proxy is the one that's used for the simulation, the main mesh will not use this vertex group.

  1. Create a new Vertex group called cloth_goal_strength_v2 by adding it in the same way as step 1 of "Setting Up"
  2. Select which vertices will remain fixed
  3. Assign the selected vertices to the "cloth_goal_strength_v2" vertex group
image.jpg

With this, your proxy is done, export it with the same .DMX version you used earlier to export.

The 2 Vertex groups we used are the only ones that are required, but multiple additional vertex groups exist, most of which have an unknown function, you can view the full list here, on the Valve Developer Community.

Adding the Cloth in ModelDoc

After having placed both models in the correct folders, and having added the aforementioned main mesh inside MoodelDoc, you'll need to add a "ClothProxyMeshFile":

  1. +Add > ClothProxyMeshFile
  2. Select your proxy
  3. Check the Add Bones To Render Mesh box
  4. Compile

This will generate the cloth nodes, which also serve as bones, keep them to a minimum, in this case having too many cloth nodes may yield the opposite effect and result in worse cloth physics, especially for small meshes.

With this, the cloth is technically done, however, it is highly recommended to add "Cloth Parameters".

Adding Cloth Parameters

Cloth parameters will define how the cloth behaves, although a model can have multiple *"ClothProxyMeshFile"*s, only a single "ClothParams" can be used at a time, and it's shared between all proxies, the first instance of "ClothParams" will be used and the rest will be ignored.

  1. +Add > ClothParams, alternatively, you can right click the "ClothProxyMeshList" to quickly add "ClothParams", and even any additional "ClothProxyMeshFile" if needed
  2. Check boxes and pull sliders to your liking

ClothParams include many options to control the cloth, some of which were too subtle or unclear to know what they do with certainty, while some others are just educated guesses, any contribution or correction is appreciated.

ClothParams list:

Parameter Function Range
Stretchiness Controls the stretchiness 0→inf
Shear Stretch Unknown 0→inf
Extra Iterations Additional simulation checks 0→inf
Extra Goal Iterations Unknown 0→inf
Extra Pressure Iterations Unknown 0→inf
Goal Strength Bias Goal strength is globally added/subtracted -1→1
Default Gravity Scale Cloth gravity -inf→inf
Fast Drag Air drag for quick movements -inf→inf
Slow Drag Air drag for subtle movements -inf→inf
Smooth Rate Unknown ?→?
Internal Pressure Amount of air inside the mesh, requires model to be 3D -inf→inf
Windage Normal Unknown ?→inf
Windage Tangent Amount of wind generated from the mesh, requires Internal Pressure -inf→inf
Smooth Iterations Unknown 0→inf
Ground Friction Friction between cloth and the world ?→inf
World Collision Penetration Unknown ?→?
Add World Collision Radius Radius for collision check between cloth and the world is added/subtracted -inf→inf
Volumetric Solve Amount Unknown ?→?
Minimal Bend Prevents cloth to bend over a certain amount 0→1
Follow the Lead Unknown 0/1
Force World Collision On All Nodes Unknown 0/1
Collide With World Hulls Allows cloth collision with the world 0/1
Collide With World Meshes Unknown 0/1
Collide With World Capsules And Spheres Seemingly allows cloth collision with physics shapes, to verify 0/1
Bend-and-Stretch Springs Unknown 0/1
Rigid Edge Hinges The edges of 2D meshes will keep some shape 0/1
Soft Skin Animation Unknown 0/1
Bend Only Springs Unknown 0/1
Immovable Object (hint) Unknown 0/1

Adding collisions with Cloth Shapes

Cloth doesn't collide with Physics Shapes, instead, it has its own Cloth Shapes, these 3 being ClothShapeBox, ClothShapeCapsule and ClothShapeSphere.

You can add and modify them as you would with Physics Shapes, however, ClothShapeCapsule differs, being able to change the radius of each point instead of the capsule as a whole, making it arguably the best shape.

1c15fd9f0c6372fe6a497bfff8f6efd87db9556d.jpeg
Having the Cloth Shapes too thin may cause the cloth to phase through them, the same happens when the speed difference between cloth and shape is too great, stacking multiple Cloth Shapes does not help.
To Do: Figure out what "Cloth Collision Layers" are.