How to optimise mesh data

Posted by Wardy on Game Development See other posts from Game Development or by Wardy
Published on 2013-04-03T10:54:58Z Indexed on 2013/07/02 23:19 UTC
Read the original article Hit count: 275

Filed under:
|
|
|

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)
                        Indices[ind]--;
                }

                Vertices.RemoveAt(j);
                Uvs.RemoveAt(j);
                Normals.RemoveAt(j);
            }
        }
    }
}

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.

© Game Development or respective owner

Related posts about c#

Related posts about unity