10-08-2020, 12:38 PM
(10-08-2020, 12:18 PM)josemendez Wrote: Hi there,Thank you very much, you help me a lot.
In your OnGizmos method, you're constantly doing the equivalent to this:
Code:for (int i = 0; i < count; i++)
meshFilter.mesh.vertices[i]
This is extremely slow, creates a lot of garbage (as you're requesting a copy of all vertices of the mesh every iteration fo the for loop, that the GC has to collect) and will also result in incorrect behavior as you're creating a new instance of the cloth mesh by calling meshA.mesh instead of meshA.sharedMesh. This is clearly stated in Unity's documentation:
https://docs.unity3d.com/ScriptReference...-mesh.html
Calling meshFilter.mesh completely ditches the deformed mesh created by Obi and replaces it with a copy of the original, undeformed mesh. That's why you don't see the mesh moving at all.
Instead, you want something that looks like this:
Code:// grab the original mesh (not a copy of it) and request a copy of its vertices, only once.
Vector3[] vertices = meshA.sharedMesh.vertices;
// reuse our copy of the vertices for all iterations:
for (int i = 0; i < gatherA.Count; i++)
{
Vector3 p1 = meshA.transform.TransformPoint(vertices[gatherA[i].v1]);
Vector3 p2 = meshA.transform.TransformPoint(vertices[gatherA[i].v2]);
Gizmos.DrawLine(p1, p2);
}
Same for meshB.
In the future, I will pay attention to these programming details, and strive to write high-quality code.
Cheers.