Game Development Stack Exchange is a question and answer site for professional and independent game developers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

So i have some procedurally generated mesh data and i want to reduce it down to its minimum number of verts. In case it matters this is a unity project.

Working on the basis of a simple example, lets assume a typical flat surface of points 2 by 3.

The point / vertex at [1,1] is used in many triangles.

I've generated mesh for a voxel type engine that adds verts to a list based on face visiblility and now I want to remove all the duplicates.

Can anyone come up with an efficient way of doing this because what i have is sooo bad its not even funny (and i don't even think it's logically correct) ...

private void Optimize()
    Vector3 v;
    Vector3 v2;
    for (int i = 0; i < Vertices.Count; i++)
        v = Vertices[i];
        for (int j = i+1; j < Vertices.Count; j++)
            v2 = Vertices[j];
            if (v.x == v2.x && v.y == v2.y && v.z == v2.z)
                for (int ind = 0; ind < Indices.Count; ind++)
                    if (Indices[ind] == j)
                        Indices[ind] = i;
                    else if (Indices[ind] > j && Indices[ind] > 0)


EDIT: Ok i managed to get this (code sample above updated) to render an "optimised" set of verts but the UV data is all wrong now, which would make sense because i'm basically just removing any UV Vector that represents a UV coord for a removed vert and not actually considering what I need to do to "fix the tri" so to speak.

The code now seemingly does work but its quite time consuming, still looking to further optimise.

share|improve this question
Since it's C# could you use a Dictionary's dislike of duplicates to remove duplicate vertices? – Dialock Apr 3 '13 at 16:13
i hadn't considered that ... mainly because at the moment im simply "removing from the target data" and using a dictionary like this would basically involve me allocating another chunk of ram. My logic was something like "for each vert, grab each vert after it and if its a dupe (by location), remove the dupe, remove any other data that points to the dupe (except indices), re-point those to the original". But for some reason this isn't working quite right. – War Apr 3 '13 at 17:35
when i say "isn't working quite right" i mean "results in 0 verts actually being rendered for some reason" – War Apr 3 '13 at 17:38
Maybe create a vertex object that contains a position, normal, and texture coordinate. Then remove duplicates of those. – Dialock Apr 3 '13 at 18:41
having thought about it ... you might be on to something there ... I was thinking about raw vertex info in terms of points but a vertex is more than just a point ... that may mean that my mesh is about as optimized as i can get it as the uv coordinates for each point will be different for each adjoined tri. – War Apr 3 '13 at 22:17

Unity's Mesh class has an Optimize method which should give you what you want.

From the wiki page:

You should use it if you generate a mesh from scratch procedurally and you want to trade better runtime performance against higher load time.

share|improve this answer
I just tried that out and dumping out the raw figures, it seems to make no difference to the vertex count ... what does it actually do? since the document just says "makes it faster" basically. My idea was to reduce duplication (not have the same vert twice in the mesh) but that creates uv hell apparently – War Apr 3 '13 at 22:14
If you don't understand how UVs, verts and faces interact you're going to have a bad time =) – Patrick Hughes Apr 4 '13 at 6:25
well i had it all working, i started with voxels because that's about as simple as life gets in game programming, but i'm now trying to extend that a little bit to optimise what I produced. My problem is that I hadn't fully appreciated the fact that a Vert is a shared point and UV coords mean different things to different tri's attached to that point. – War Apr 4 '13 at 15:53
I've slightly tweaked this code but taking the UV problem to 1 side this is producing the exact result i'm after. I now need to figure out how to texture my new optimised mesh and taking the UV approach i took when it was a voxel mesh likely won't work, i have something that effectively in terms of shape is not to different to an asteroid in a space game and I need to apply grass to the top and rock to the bottom. Any ideas? – War Apr 4 '13 at 15:57

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.