- Home /
reading vertices from a mesh always returns Vector3.zero
I'm trying to read the vertices from a mesh that I've imported into my scene from an fbx file.
Something like this:
public MeshFilter MyMesh;
.
.
.
for (int i=0; i<MyMesh.sharedMesh.vertices.Length; i++)
{
Debug.Log("Vertex #"+i + " "+MyMesh.sharedMesh.vertices[i]);
}
The mesh renders fine in the scene, and I print the correct number of vertices, but they always come out as (0,0,0).
Any ideas what I'm doing wrong?
Thanks.
Answer by rcg · Apr 13, 2011 at 03:46 AM
OK, I'm an idiot...great first post :) The sizes were smaller than the precision of the Vector3 string printing. Doesn't solve my real problem, but gets me past this false alarm. nothing to see here...
Answer by rcg · Apr 13, 2011 at 04:24 AM
In the interest of making up for my stupid question I'll post what ended up being the real problem, and the solution I found for it (although I'm still not sure why the solution was necessary).
I was trying to create a SkinnedMeshRenderer from a mesh template imported from an fbx file. I have hundreds of these in my scene, and to try and improve performance I wanted to procedurally generate a skinned mesh with one bone for each instance of the template I wanted in the scene.
I was following the example here:
But when I tried to do the following:
Mesh mesh = new Mesh();
mesh.vertices = new Vector3[TileMesh.sharedMesh.vertices.Length];
for (int i=0; i<TileMesh.sharedMesh.vertices.Length; i++)
{
mesh.vertices[i] = TileMesh.sharedMesh.vertices[i];
}
The values in mesh.vertices remained (0,0,0).
For whatever reason this (below) ended up working and fixing my problem. Perhaps it's because mesh.vertices is a read only property, I don't know. If that was the case I would have expected a compile error...but it's working now so I'm happy.
Mesh mesh = new Mesh();
Vector3 [] vertices = new Vector3[TileMesh.sharedMesh.vertices.Length];
for (int i=0; i<TileMesh.sharedMesh.vertices.Length; i++)
{
vertices[i] = TileMesh.sharedMesh.vertices[i];
}
mesh.vertices = vertices;
Still curious why I had to do this, but maybe this will help someone else in the future.
The docs for the $$anonymous$$esh class actually tell you to do that, and as the $$anonymous$$esh.vertices docs say, it returns a copy of the vertices. In other words, it's not a reference to the actual vertices (probably because it has to be uploaded to VRA$$anonymous$$ when you assign it back).