public HardwareMeshProcessor(SoftwareMesh mesh, bool loadColors, bool loadUVs, bool loadNormals)
        {
            this.mesh = mesh;

            // get vertex component types
            positions = new List<Vector3[]>();
            foreach (var key in mesh.VertexComponentKeys)
            {
                switch (key.Key)
                {
                    case VertexComponentKeyTypes.Positions:
                        var vertexComponents = mesh.VetexComponents[key.Value];
                        if (vertexComponents.GetType() == typeof(Vector3[]))
                        {
                            positions.Add((Vector3[])vertexComponents);
                        }
                        else if (vertexComponents.GetType() == typeof(Vector2[]))
                        {
                            var verts = (Vector2[])vertexComponents;
                            var newVerts = new Vector3[verts.Length];
                            for (int i = 0; i != verts.Length; ++i) newVerts[i] = new Vector3(verts[i].X, verts[i].Y, 0);
                            positions.Add(newVerts);
                        }
                        else
                        {
                            Debug.ThrowError("HardwareMeshProcessor", "Unsuported VectorComponent type");
                        }
                        break;
                }
            }

            // get triangle component types
            if (loadColors) colorComponents = new List<TriangleColorComponent[]>();
            if (loadNormals) normalComponents = new List<TriangleNormalComponent[]>();
            if (loadUVs) uvComponents = new List<TriangleUVComponent[]>();
            foreach (var key in mesh.TriangleComponentKeys)
            {
                switch (key.Key)
                {
                    case TriangleComponentKeyTypes.ColorComponents:
                        if (loadColors) colorComponents.Add((TriangleColorComponent[])mesh.TriangleComponents[key.Value]);
                        break;

                    case TriangleComponentKeyTypes.NormalComponents:
                        if (loadNormals) normalComponents.Add((TriangleNormalComponent[])mesh.TriangleComponents[key.Value]);
                        break;

                    case TriangleComponentKeyTypes.UVComponents:
                        if (loadUVs) uvComponents.Add((TriangleUVComponent[])mesh.TriangleComponents[key.Value]);
                        break;
                }
            }

            // create triangles with there own verticies
            Verticies = new LinkedList<VertexProcessor>();
            Triangles = new List<TriangleProcessor>();
            foreach (var triangle in mesh.Triangles)
            {
                var newTriangle = new TriangleProcessor(triangle, this, loadColors, loadUVs, loadNormals);
            }

            // process (remove duplicate verticies from triangles)
            const float tolerance = .002f;
            int count = Triangles.Count-1, count2 = Triangles.Count;
            for (int i = 0; i != count; ++i)
            {
                for (int vi = 0; vi != 3; ++vi)
                {
                    var vertex = Triangles[i].Verticies[vi];
                    for (int i2 = i+1; i2 != count2; ++i2)
                    {
                        for (int vi2 = 0; vi2 != 3; ++vi2)
                        {
                            var vertex2 = Triangles[i2].Verticies[vi2];
                            if (vertex == vertex2) continue;

                            // position tolerance
                            bool canRemoveVertex = true;
                            for (int pi = 0; pi != vertex.Positions.Length; ++pi)
                            {
                                if (!vertex.Positions[pi].AproxEqualsBox(vertex2.Positions[pi], tolerance))
                                {
                                    canRemoveVertex = false;
                                    break;
                                }
                            }

                            // color tolerance
                            if (canRemoveVertex && loadColors)
                            {
                                for (int pi = 0; pi != vertex.Colors.Length; ++pi)
                                {
                                    if (!vertex.Colors[pi].AproxEqualsBox(vertex2.Colors[pi], tolerance))
                                    {
                                        canRemoveVertex = false;
                                        break;
                                    }
                                }
                            }

                            // normal tolerance
                            if (canRemoveVertex && loadNormals)
                            {
                                for (int pi = 0; pi != vertex.Normals.Length; ++pi)
                                {
                                    if (!vertex.Normals[pi].AproxEqualsBox(vertex2.Normals[pi], tolerance))
                                    {
                                        canRemoveVertex = false;
                                        break;
                                    }
                                }
                            }

                            // uv tolerance
                            if (canRemoveVertex && loadUVs)
                            {
                                for (int pi = 0; pi != vertex.UVs.Length; ++pi)
                                {
                                    if (!vertex.UVs[pi].AproxEqualsBox(vertex2.UVs[pi], tolerance))
                                    {
                                        canRemoveVertex = false;
                                        break;
                                    }
                                }
                            }

                            // remove vertex
                            if (canRemoveVertex)
                            {
                                Verticies.Remove(vertex2);
                                Triangles[i2].Verticies[vi2] = vertex;
                            }
                        }
                    }
                }
            }

            // process (set vertex indicies)
            int index = 0;
            foreach (var vertex in Verticies)
            {
                vertex.Index = index;
                ++index;
            }
        }
Exemplo n.º 2
0
        public HardwareMeshProcessor(SoftwareMesh mesh, bool loadColors, bool loadUVs, bool loadNormals)
        {
            this.mesh = mesh;

            // get vertex component types
            positions = new List <Vector3[]>();
            foreach (var key in mesh.VertexComponentKeys)
            {
                switch (key.Key)
                {
                case VertexComponentKeyTypes.Positions:
                    var vertexComponents = mesh.VetexComponents[key.Value];
                    if (vertexComponents.GetType() == typeof(Vector3[]))
                    {
                        positions.Add((Vector3[])vertexComponents);
                    }
                    else if (vertexComponents.GetType() == typeof(Vector2[]))
                    {
                        var verts    = (Vector2[])vertexComponents;
                        var newVerts = new Vector3[verts.Length];
                        for (int i = 0; i != verts.Length; ++i)
                        {
                            newVerts[i] = new Vector3(verts[i].X, verts[i].Y, 0);
                        }
                        positions.Add(newVerts);
                    }
                    else
                    {
                        Debug.ThrowError("HardwareMeshProcessor", "Unsuported VectorComponent type");
                    }
                    break;
                }
            }

            // get triangle component types
            if (loadColors)
            {
                colorComponents = new List <TriangleColorComponent[]>();
            }
            if (loadNormals)
            {
                normalComponents = new List <TriangleNormalComponent[]>();
            }
            if (loadUVs)
            {
                uvComponents = new List <TriangleUVComponent[]>();
            }
            foreach (var key in mesh.TriangleComponentKeys)
            {
                switch (key.Key)
                {
                case TriangleComponentKeyTypes.ColorComponents:
                    if (loadColors)
                    {
                        colorComponents.Add((TriangleColorComponent[])mesh.TriangleComponents[key.Value]);
                    }
                    break;

                case TriangleComponentKeyTypes.NormalComponents:
                    if (loadNormals)
                    {
                        normalComponents.Add((TriangleNormalComponent[])mesh.TriangleComponents[key.Value]);
                    }
                    break;

                case TriangleComponentKeyTypes.UVComponents:
                    if (loadUVs)
                    {
                        uvComponents.Add((TriangleUVComponent[])mesh.TriangleComponents[key.Value]);
                    }
                    break;
                }
            }

            // create triangles with there own verticies
            Verticies = new LinkedList <VertexProcessor>();
            Triangles = new List <TriangleProcessor>();
            foreach (var triangle in mesh.Triangles)
            {
                var newTriangle = new TriangleProcessor(triangle, this, loadColors, loadUVs, loadNormals);
            }

            // process (remove duplicate verticies from triangles)
            const float tolerance = .002f;
            int         count = Triangles.Count - 1, count2 = Triangles.Count;

            for (int i = 0; i != count; ++i)
            {
                for (int vi = 0; vi != 3; ++vi)
                {
                    var vertex = Triangles[i].Verticies[vi];
                    for (int i2 = i + 1; i2 != count2; ++i2)
                    {
                        for (int vi2 = 0; vi2 != 3; ++vi2)
                        {
                            var vertex2 = Triangles[i2].Verticies[vi2];
                            if (vertex == vertex2)
                            {
                                continue;
                            }

                            // position tolerance
                            bool canRemoveVertex = true;
                            for (int pi = 0; pi != vertex.Positions.Length; ++pi)
                            {
                                if (!vertex.Positions[pi].AproxEqualsBox(vertex2.Positions[pi], tolerance))
                                {
                                    canRemoveVertex = false;
                                    break;
                                }
                            }

                            // color tolerance
                            if (canRemoveVertex && loadColors)
                            {
                                for (int pi = 0; pi != vertex.Colors.Length; ++pi)
                                {
                                    if (!vertex.Colors[pi].AproxEqualsBox(vertex2.Colors[pi], tolerance))
                                    {
                                        canRemoveVertex = false;
                                        break;
                                    }
                                }
                            }

                            // normal tolerance
                            if (canRemoveVertex && loadNormals)
                            {
                                for (int pi = 0; pi != vertex.Normals.Length; ++pi)
                                {
                                    if (!vertex.Normals[pi].AproxEqualsBox(vertex2.Normals[pi], tolerance))
                                    {
                                        canRemoveVertex = false;
                                        break;
                                    }
                                }
                            }

                            // uv tolerance
                            if (canRemoveVertex && loadUVs)
                            {
                                for (int pi = 0; pi != vertex.UVs.Length; ++pi)
                                {
                                    if (!vertex.UVs[pi].AproxEqualsBox(vertex2.UVs[pi], tolerance))
                                    {
                                        canRemoveVertex = false;
                                        break;
                                    }
                                }
                            }

                            // remove vertex
                            if (canRemoveVertex)
                            {
                                Verticies.Remove(vertex2);
                                Triangles[i2].Verticies[vi2] = vertex;
                            }
                        }
                    }
                }
            }

            // process (set vertex indicies)
            int index = 0;

            foreach (var vertex in Verticies)
            {
                vertex.Index = index;
                ++index;
            }
        }