Table of Contents
- GPUClothSimulator
- Debugging
- Simulation Parameters
- External Forces
- Distance Constraint
- Bending Constraint
- Bending Method
- Damping Factor (Isometric Method Exclusive)
- Max Displacement (Isometric Method Exclusive)
- Max Magnitude (Isometric Method Exclusive)
- Bending Allowance
- Velocity Damping
- Pinned Constraints
- Constraint Weightmap
- Show Gizmos
- Weightmap
- Update Constraints
- Clear Constraints
- Create Collision Mesh
- Collider Mesh
- Print Constraint List
- Print Masses List
- Print Pinned List
- Collision
- Compute Shader
- API
- Void InitializeSimulation()
- Void UpdateConstraints()
- float MMin = 0.0f ; MMax = 1.0f
- float CMin = 0.0f ; CMax = 1.0f
- List vertexMasses
- List Constraints
- List AlphaVertices
- int[] PinnedVertices
- List CollidableMeshes
- List CollidablePlanes
- List Capsules
- List Spheres
- List Cubes
- List SphereColliderPairs
- SkinnedMeshRenderer skinnedMeshRenderer;
- List physicsToFullVertexMapping
GPUClothSimulator
- Component: GameObject
The component attached to the Cloth. With this component you can maximize your customization and optimization by sending a mass amount of data to the GPU to process.
Debugging
Enable/Disable Debugging
Bool: Default False.
- True: When enabled, the script sends the GPU all values (Damping, Mass, Stretch Stiffness, etc.) so you can adjust and debug life in play mode.
- False: This is default. Keep this false to save resources by NOT sending the GPU values that does not change in every update.
Copy/Load All Settings
Function.
- Copy All Settings: Select this to copy all the values of the entire component into a JSON format onto your clipboard. This is used so yu can edit in play mode, Copy. then go back to edit mode where you would have your values reset, then select the Load All Settings, and it will grab all the settings off your Keyboard. Also great for copying values from one component to another.
- Load All Settings: Select this to grab the settings from the clipboard and apply them to this component.
Load All Defaults
Function
- Sets all values beck to default values.
Simulation Parameters
Initialize at Start
Bool: Default True
- Determines if information gets sent and initializes at start. If you will be equipting this cloth, then leave it as false, and when you are ready call "InitializeSimulation()" on this script. This will help with loading all colliders before starting the simulation. Once simulation starts, you can not add colliders.
Initialize Simulation
Function
- This is here just to help you understand the "InitializeSimulation()" part. when you click this button, it calls that function. button is only visible before the initialization has been started.
Time Step
Float: Default 0.009.
- Time step: The amount of time waited before running another calculation. Lower number is more accurate, but more expensive
Iterations
Float: Default 15.
- Iterations: Controls how many times the constraints are solved in each update. Higher number is more accurate, but more expensive.
Mass Multiplier
Float: Default 1.
- Mass Multiplier: A higher value results in vertices that are less responsive to forces and constraints, making the cloth appear heavier and less flexible. The mass is a multiplier of the Green color in the Weight map, and the slider here amplifies the values of every Vertice. Calculation: (Lerp(0.1, 1, VertexMasses[id.x]) * Mass. (See Tutorial links for more info)
External Forces
Gravity Direction
Enum: Default Y-Minus.
- Turn your Gizmo to local, then select the SkinnedMeshRendered Component. Then select the Move tool and loot to see what arrow is pointing up (IF any) and then use that to look at your normal directional Gizmo to see which Direction it is, that will tell you which direction you need to choose here.
Gravity Power
Float: Default 9.81.
- The scale of the Gravity. I have no real idea how we get this number, but all AL and google search seems to turn up to use this number for the most realistic gravity.
Wind
Vector3: Default Vector3.zero.
- Change the values to control which direction the wind is coming from, if any.
Distance Constraint
Compression Stiffness
Float: Min 0, Max 10, Default 1.
- Compression Stiffness: The amount of resistance applied to the cloth to keep it from COMPRESSING.
Stretch Stiffness
Float: Min 0, Max 10, Default 4.
- Stretch Stiffness: The amount of resistance applied to the cloth to keep it from STRETCHING.
Bending Constraint
Bending Method
Enum: Default DihedralMethod.
- Dihedral Method: Less expensive, has more basic physics.
- Isometric Method: Hold the shape in the cloth better, but at a cost in computing.
Damping Factor (Isometric Method Exclusive)
Float: Min 0, Max 2, Default 0.95.
- Reduces the magnitude of the displacement to prevent excessive movement and ensure stability.
Max Displacement (Isometric Method Exclusive)
Float: Min 0.00001, Max 0.0008, Default 0.0001.
- Limits the maximum displacement that can be applied to each vertex component individually.
Max Magnitude (Isometric Method Exclusive)
Float: Min 0, Max 0.05, Default 0.005.
- Limits the overall magnitude of the displacement vector to maintain the integrity of the cloth simulation.
Bending Allowance
Float: Min 0, Max 1, Default 0.57.
- Limits the ability to bend by a value.
Velocity Damping
Damping Method
Enum: Default SmartDamping
- No Damping - This is Very bouncy, very unstable.
- Simple Damping - This is semi-realistic but cost friendly.
- Smart Damping - This is realistic, but more expensive.
- Weight map - This uses the blue value on your weight map to determine the value for each Vertice.
Damping Stiffness
Float: Min 0, Max 0.5, Default 0.025.
- This controls the damping value of the unpinned vertices. The more damping, the slower and more restricted the vertices are.
Pinned Constraints
Pinned Constraints
List<Int>: Default 0.
- This is simply a display to show what vertices are actually being pinned. I did this so you can explore and see if there is any that were meant to be pinned or not meant to be pinned.
- X: Simply Remove a Vertice from being Pinned.
- Add Vertice: Add a Vertice to be pinned. Note: When you click the "Add Vertice" it will add a new entry to the list, and it will default to 0. if you forget to change it then it will not hurt anything.
Constraint Weightmap
Show Gizmos
Bool: Default false.
- Show or hide the Gizmos which represent the vertices and colored according to the Weight map. If the texture is not present, the Gizmos will not show up.
Weightmap
Texture
- The texture we call the weight map. this uses all channels for various purposes to help make the cloth as realistic as possible. The Alpha channel controls what the collision mesh will look like. The blue channel controls the Damping constraints, the green controls the mass, the black is the pinned vertices. I recommend using blender and the UV editor to get a printout of the UV map and edit it in Gimp and Blender for best use. Please refer to the tutorials on how to do this.
Update Constraints
Function
- Reads the Texture and the meshes UV to fill the Mass, Damping, Pinned, and collision vertices lists. This function has to be used or else the cloth will just fall. Please see the tutorials on making a weight map for a mesh.
Clear Constraints
Function
- This clears all the constraints lists. I have no real situation where this would be used in honesty, but I made the function in case one ever needs it.
Create Collision Mesh
Function
- Create the collision mesh. We make a new mesh using the ALPHA channel of the Weight map. The purpose of this mesh is to optimize the communication between the GPU and CPU. The pinned vertices never change so there is no reason to ever calculate the new positions for them, so we make a new mesh and make a conversion map as well in the background to remember which Vertice in this sub mesh matches up with which one in the full mesh and update only the vertices needed on the full scaled mesh. Meanwhile we also use this mesh as a collision detection mesh in the mesh filters reducing the cost of this function as well.
Collider Mesh
Mesh
- The collision and sub mesh of the full mesh used to detect collision and reduce the information traveling between the GPU and CPU.
Print Constraint List
Function
- Prints a list of all vertices controlled by the Damping weight map and their weights to the console for debugging purposes.
Print Masses List
Function
- Prints a list of all vertices controlled by the Masses weight map and their weights to the console for debugging purposes.
Print Pinned List
Function
- Prints a list of all vertices controlled by the Pinned weight map to the console for debugging purposes.
Collision
Double Sided Collision
Bool: Default True.
- Double Sided Collision adds a second mesh but reversed and adds a mesh collider as well all using the Sub Mesh. this helps detect collision from both sides of the mesh and makes the collision much more precise, but at a fairly heavy cost. if the mesh is low resolution, then the cost is lesser. If the mesh is very high resolution and has not many colliders, you may consider disabling this.
Colliders
Dropdown List
- Lists of various colliders that are useable with this component.
Add (Collider type)
Function
- Adds a new empty entry to the collider type list. Please do not leave any empty, this will cause issues. There is an "X" button near the collider to remove it.
Collision Padding
Float: Min 0.001, Max 1, Default 0.001
- Adds X amount of padding between the cloth and collision to help with clipping.
Note: on a cube, be cautious around the corners. If touching more than one side, it pushes in more than one direction causing it to bounce, more noticeably as the friction is higher
Collision Entry
Object Field
- Enter the Collider type here.
Compute Shader
PBDClothSolver
Object Field
- The magic happens here! The script here is a compute shader file which is operated by the GPU instead of the normal C# scripts we see which are ran by the CPU. The cloth physics and collision are all calculated inside of this script.
Work Group Size
Enum: Default X64
- An Enum which is made up by a list of X4 values which calculates the number of threads on the GPU to use. The higher resolution of a mesh will need a higher value. If a value too high is selected the mesh may explode and/or dead space will be used inside the GPU which will result in a loss of optimization. Lower values will mean less threads will be used causing more time for the calculations to be made but also comes with more stability and less chance of wasted space int he threads. Every mesh will have a magic number to use for best results.
Calculate workgroup size
Function
- Calculates the resolution of the mesh compared to the threads needed for best performance. If this causes the mesh to explode, try selecting a lower value until this is resolved.
API
Void InitializeSimulation()
- Call to make the simulation start. This is only needed if you disable the "Initialization at start". Leaving this disabled is good for if you instantiate the cloth and need to add colliders. You can add colliders or update constraints then call this.
Void UpdateConstraints()
- Call to make the script read the weightmap and recalculate the lists of Vertice masses, Vertice Damping, Pinned Vertices, and the Simulation Vertices.
float MMin = 0.0f ; MMax = 1.0f
- This adjusts the minimum and maximum allowed value for the weightmap Vertice masses. When the weightmap is being read and telling each Vertice their mass, it uses a Larp mathematical function. Larp(MMin, MMax, VerticeWeight) - As the weights are read, it is ultimately configured according to the min and max values. The editor is defaulted to MMin 0.0, while max is 1.0. if you change these, it will allow the sliders in the editor to be able to travel further.
float CMin = 0.0f ; CMax = 1.0f
- Adjusts the min and max for the Velocity Damping per vertice. please read "float MMin = 0.0f ; MMax = 1.0f" for more details on how this works
List vertexMasses
- lists each Vertice's mass according to the weightmap. Get/Set enabled.
List Constraints
- lists each Vertice's Damping according to the weightmap. Get/Set enabled.
List AlphaVertices
- lists each Vertice's Simulation Status according to the weightmap. Get/Set enabled. Largly used to create the Collider Mesh and to optimize the amount of data being called to and from the GPU Compute file.
int[] PinnedVertices
- lists each Vertice's Pinned status according to the weightmap. Get/Set enabled. Take note this is not a List like the others.
List CollidableMeshes
- Use this to add MESH Colliders, or to get the full list of.
List CollidablePlanes
- Use this to add PLANE Colliders, or to get the full list of.
List Capsules
- Use this to add CAPSULE Colliders, or to get the full list of.
List Spheres
- Use this to add SPHERE Colliders, or to get the full list of.
List Cubes
- Use this to add BOX Colliders, or to get the full list of.
List SphereColliderPairs
- Use this to add SPHERECOLLIDERPAIRS Colliders, or to get the full list of.
SkinnedMeshRenderer skinnedMeshRenderer;
- The SkinnedMeshRenderer that is being manipulated. This is filled by the editor script and if it ever detects it null, it will try to set it by using the gameObject it is attached to. If you never click on the gameobject after setting this then you could set the script on any game object, however complicated that may become.
List physicsToFullVertexMapping
- this is a list of a class which contains the information on which vertice lines up with which vertice in relation to the main mesh, and the SubMesh which is Created using the Alpha channel on the Weightmap.
public class SerializableVertexMapping {
public int fullMeshIndex;
public int physicsMeshIndex;
}