- Home /
Procedurally generated Mesh not updating?
I have a procedurally generated 2D mesh in a 2D scheme.
void ConstructMesh()
{
mMesh.Clear();
mMesh.vertices = vertices;
//mMesh.normals = normals;
mMesh.triangles = triangles;
mMesh.uv = uvs;
mMeshFilter.sharedMesh = mMesh;
mMesh.RecalculateNormals();
mMesh.RecalculateBounds();
//mMesh.Optimize ();
}
This is called after each update. However, it would appear that it only works once. Basically my script adds quads (2 triangles) in repetition with the intent to create a trail. I see the values updating correctly, but only one quad (The first one) gets displayed.
mMeshFilter = GetComponent<MeshFilter>();
mMesh = mMeshFilter.sharedMesh;
The mesh is fetched like this.
You don't need to:
m$$anonymous$$eshFilter.shared$$anonymous$$esh = m$$anonymous$$esh;
When you retrieve a mesh from a mesh filter, you get a reference.
Also, it can be dangerous to use Shared$$anonymous$$esh:
From Unity Docs:
It is recommended to use this function only for reading mesh data and not for writing, since you might modify imported assets and all objects that use this mesh will be affected. Also, be aware that is not possible to undo the changes done to this mesh.
But, that risk is up to you.
Lastly, you are not showing how you are generating your vertices/triangles/UVs. That could be the reason you are having a problem. So, please give us more information.
I am having the same issue..
I can load in any mesh from an array but only the first one I load actually renders. Any help would be greatly appreciated!
Answer by Spinnernicholas · Dec 26, 2013 at 06:30 PM
Use the built in Line Renderer. I think is does basically what you are trying to do. And, you can change it procedurally at runtime.
You may also want to check out the Trail Renderer.