We’re trying to write a small tool that transfers vertex colors from a source mesh to the ClothSkinningCoefficients of a cloth component (in another object).
In the picture below, you see the vertex colored mesh of on the left side, and cloth mesh in center.
After struggling with this for a while not getting the expected results, I started to dig a bit.
In the first picture I simply drew out the vertex positions of both meshes for each 10th vertex (red for the vertex painted mesh, green for the destination cloth mesh)… And as you see they both use the same indexed vertex buffer.
In the other picture I simply set the maxDistance of the coefficients to a simple function of the vertex positions X value (using the vertex position from the destination cloth mesh). Where I would have expected a gradient transition across the mesh I see the following pattern (right image).
So either I’m missing something simple, or the cloth component has its own indexing buffer saying which coefficient belongs to which vertex?
Has anybody had similar problems, trying to write these cloth coefficients from script?
I have had problems recently as well, not if you’ve already found a solution, but comparing the coefficients and the vertices of the mesh does not match. like you’ve tried to use only the red channel of the vertex colors to determine the maximum distance coefficient, but the results are unconvincing.
if (hair.meshIndex != hair._meshIndex) {
hair.container.sharedMesh = hair.meshes[hair.meshIndex];
hair._meshIndex = hair.meshIndex;
coefficients = new ClothSkinningCoefficient[cloth.coefficients.Length];
for (int i = 0; i < coefficients.Length; i++)
{
coefficients[i].maxDistance = hair.meshes[hair.meshIndex].colors[i].r;//Random.value;
}
cloth.coefficients = coefficients;
Debug.Log(cloth.coefficients.Length + " " + hair.meshes[hair.meshIndex].normals.Length);
}
As this page says: Cloth.vertices, The skinned mesh vertices (i.e. source mesh vertices) does not correspond to the Cloth.vertices & Cloth.coefficients.
In the following image I draw out both the source mesh vertices & cloth vertices:
So the problem is to figure out a ‘vertex index lookup’ from the source mesh to the cloth, so that we can set the cloth.coefficients etc. in script (knowing which source vertex the cloth coefficient belongs to)
A workaround then is to ‘transform’ the cloth vertices ‘back’ using more or less magic numbers, then doing a distance check… But this feels sooo hacky…
Please, would it not be a simple thing to expose this cloth vertex index lookup that you probably have hidden under the hood somewhere?
I’m creating a ‘vertex-index’-to-‘cloth-index’ lookup ScriptableObject asset, during the asset import… by simply comparing the positions of the vertices between the skinned mesh & the cloth vertices (which is slow, but ok since it is not during runtime)
Then during runtime, i can use the lookup table asset to quickly modify the coefficients as I want.
Thanks to both, I thought that this topic had been forgotten, my level of programming is not so advanced to get to see why the differences in the vertex count