- Home /
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.
Any suggestions what I should be looking for? I'm out of ideas.
Answer by aldonaletto · Nov 16, 2013 at 03:31 AM
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:
Another possibility is the triangles becoming empty (when the 3 vertices are in the same line).
Thanks for the reply. I don't think that's it though, because I only change the height (Y) coordinate of the vertices so the winding order should still be the same, and I can't see the mesh's textures from the other side either.
I modified meshes before without this issue, the only difference that I know of this time is the existence of submeshes. I wonder if it has something to do with that. $$anonymous$$aybe it resets some other array when I change the mesh.. the vertices.length stays the same though so I'd doubt that.
Actually, modifying the vertex position doesn't change its order in the triangle, but may reverse its front face - although this would make the inner side visible, what isn't happening. What about renderer.bounds? If badly calculated, it may become null or out of view, what would cull the mesh out during frustum culling.
I recalculated the bounds, that didn't help either.
But I think this is an issue of the past anyway. I finally stumbled upon the Terrain class (ugh), so I guess I was wasting my time by building a mesh map anyway.
Would upvote for your help if I could.
I don't know what was wrong with your mesh, but I have the power to upvote both of you. ;) The question for being well-phrased and supported with images, and the reply for much the same qualities.
Answer by piottej · Jan 05, 2015 at 09:13 AM
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 :(