- Home /
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. :)
Answer by danielskovli · Mar 04, 2014 at 02:28 AM
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[i].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.
Hi $$anonymous$$,
Thank you for this answer! It makes sense to me in theory, although I'll need to try to give it a shot in practice and see how it turns out.
One question though: Do you know how to tell what vertex the ClothSkinningCoefficient in the array corresponds to? I'd need to know where on the Skinned $$anonymous$$esh that coefficient is located to know how to adjust the coefficient values.
Hmm, in my mesh the number of vertices in the coefficient array is equal to the number of vertices in the mesh - so I was just assu$$anonymous$$g the vertex numbers were the same.
I would be very surprised if this wasn't the case, but that said, I don't actually know. $$anonymous$$aybe you can tell us ;)
Well I figure the number of vertices is the same, but I'm trying to understand where each vertex in the array is located on the mesh. I'd be changing the coefficients' values based on their location; it wouldn't be the same values for every vertex.
Assu$$anonymous$$g that the vertex number is the same, you can get the local position of the vertex by accessing mesh.vertices[] (Vector3).
https://docs.unity3d.com/Documentation/ScriptReference/$$anonymous$$esh-vertices.html
That's the only sort of spacial information you can extract, as far as I know.
Thanks for that reference! I'll have to play around with this. I'm thinking of another approach as well, going back to your previous code example for inspiration. I'll try to work on this later this week or by the weekend.
Your answer
![](https://koobas.hobune.stream/wayback/20220613121311im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Clothing and weapon animation 1 Answer
Cloth Trampoline and 3rd Person Character 2 Answers
Cloth physics or clothing on a character. 1 Answer
How accurately can Unity simulate a shooting scenario (Cloth)? 1 Answer
Cloth problem 2 Answers