Mesh goes invisible after modifying vertices

Hello,

I have a mesh whose vertices I’m modifying at runtime by script. After I modify them, the mesh goes invisible, and I’m clueless about the why. The modification of the mesh works alright, I can see that when I pause the editor and click at the gameobject in the list. It just won’t display.

A MeshRenderer is attached and it displays fine before the modification.

The mesh contains several submeshes (maybe that’s important?)… Before and after the modification of the vertices, the object properties looks exactly the same in the inspector, with all materials and components applied.

Here’s some code (simplified):

mesh = go.GetComponent<MeshFilter>().mesh;
vertices = mesh.vertices;

tempVector = new Vector3(x, c1, z);
vertices[0] = tempVector;

tempVector = new Vector3(x, c2, z);
vertices[1] = tempVector;

tempVector = new Vector3(x, c3, z);
vertices[2] = tempVector;

tempVector = new Vector3(x, c4, z);
vertices[3] = tempVector;

mesh.vertices = vertices;
go.GetComponent<MeshFilter>().mesh = mesh;

Any idea anyone? Thanks.

EDIT: Attaching two screenshots. Those 3 chunks in the background haven’t had their meshs modified, the other one did.

Other than that they’re the same, instantiated from a prefab. Only the Y-values of the vertices have been changed. The mesh is there, just invisible.

Maybe the vertices are becoming reversely wound after modified. Remember that the front face of a triangle is defined by its winding order: the front face is the one where the vertices are in clockwise order. Modifying the position of a single vertex may make the triangle face the opposite side, like below:

17980-backfacedtriangle.png

Another possibility is the triangles becoming empty (when the 3 vertices are in the same line).

Hi guys, I’m having the exact same issue with various meshes I modify. I only change the vertices positions. I checked several times and I can’t see my mesh from any point of view in the scene.

Same as aldonaletto, I recalculated the bounds of the mesh and the normals through script after deformation.

One more piece of information : If I check/uncheck any toggle in the renderer component window (eg “CastShadows”, “Receive Shadows”) or if I uncheck and recheck the MeshRenderer Component, my mesh becomes visible.

Another piece of information. I used to do my mesh modifications in a corroutine started by a manager behaviour. This manager used to check all the meshes that could need a distortion. But I changed this to trigger the modification at runtime in the “OnBecameVisible” method to optimize my app a bit. (Instead of having to check all meshes everytime I can, I only check the meshes that become visible) I didn’t have the issue before. I checked several times and I don’t see anything else changed in my code than moving my distortion from the corroutine to the OnBecameVisible method.

The issue seems to be related to some kind of renderer refresh. I really need to deform meshes at runtime for my app.
FYI I tried to disable & re-enable the mesh renderer component through script after applying the modification to my mesh to see if this triggers some kind of refresh or whatever. It seems to work, my meshes seems to always be visible. But this is so ugly to have to do that… Does anyone has an idea of how to solve this ?

EDIT : Btw, I’m not quite sure if I had to post an “answer” or a “comment” to resurrect this thread to get my answer. Feel free to tell me if this should be a comment and I’ll remove this answer to put it as comment. And Sorry for my poor level of english :frowning: