Skinned Cloth: Modifying vertex coefficients at runtime

Hello, I was not able to find this topic on the forums or here in Answers. I hope this is not a repeat.

Here is what I am attempting to do (purely hypothetical at this point):

I would like to use a Skinned Cloth component on a cloak that I will be attaching to a player character avatar. If that avatar equips a large weapon for use in combat, the weapon will be strapped to his back when not in use. In that situation, I would like to modify the cloak’s cloth vertices so that the cloak does not clip through the weapon while the character moves with it strapped to his back.

I suppose I could create two versions of the cloak: one for when the weapon is on the back and one for when it is in hand. I could have the vertices painted appropriately for each situation, then switch between the two prefabs as needed. However, I feel like that is an inelegant solution and one that may cause some graphical “popping” when the switch occurs.

Is it possible to update Skinned Cloth vertex coefficients at runtime? Could I, for example, store two sets of vertex weights in a script, and then use method calls to switch them out? Or should/can I attach two Skinned Cloth components to the cloak and dynamically set one or the other as active at runtime?

I’m a very novice programmer, so apologies in advance if I sound daft. :slight_smile:

Hi there,

I came past your post on my mission to find the answer to an identical problem I was faced with in my own scene.

Long story short, I found a solution to my specific situation, and maybe it would be of use to you as well.

The trick is to access the coefficients array under the SkinnedCloth object for your mesh.

Something like this:

/*
    Assuming your mesh has a Skinned Cloth object attached,
    along with a Skinned Mesh Renderer.
*/

private SkinnedCloth clothReference;
public bool yourTrigger = false;
public float yourNewValue = 5f;

void Start() {
    // you probably want to do some verification stuff here
    clothReference = GetComponent<SkinnedCloth>();
}

void Update() {
    if (yourTrigger) {
        ClothSkinningCoefficient[] newCoefficients = new ClothSkinningCoefficient[clothReference.coefficients.Length];
        for (int i = 0; i < newCoefficients.Length; i++ ) {
            newCoefficients*.maxDistance = yourNewValue;*

}

clothReference.coefficients = newCoefficients;
}
yourTrigger = false;
}
This example specifically targets the MaxDistance value, but you can change any of the coefficients you want. The important part is to clone out the coefficients array → make your edits → then replace the old array with your modified new one. Making edits directly in the original array won’t update the SkinnedCloth behaviour unfortunately.
Cheers,
Daniel
Edit: Typo in code. Also, I haven’t really tested any of the above - it was just from memory.