示例#1
0
        public static void MergeVertex(List <Vector2> vertes, List <int> indices, out List <Vector2> retained, out List <int> indexLst)
        {
            retained = new List <Vector2>();
            indexLst = new List <int>();
            GroupHash <Vertex2Hash> groups = new GroupHash <Vertex2Hash>();

            for (int i = 0; i < vertes.Count; ++i)
            {
                groups.Add(new Vertex2Hash(i, vertes[i]));
            }
            List <List <Vertex2Hash> > results = groups.GetResult();
            Dictionary <int, int>      tmp     = new Dictionary <int, int>();

            for (int i = 0; i < results.Count; ++i)
            {
                Vector2 tmpV = new Vector2(0.0f, 0.0f);
                foreach (Vertex2Hash obj in results[i])
                {
                    tmp.Add(obj.mIndex, i);
                    tmpV += obj.mVertex;
                }
                retained.Add(tmpV * 1.0f / results[i].Count);
            }
            foreach (int ind in indices)
            {
                indexLst.Add(tmp[ind]);
            }
            int        count  = indexLst.Count / 3;
            List <int> remove = new List <int>();

            for (int i = 0; i < count; ++i)
            {
                int i1 = indexLst[i * 3];
                int i2 = indexLst[i * 3 + 1];
                int i3 = indexLst[i * 3 + 2];
                if (i1 == i2 || i2 == i3 || i1 == i3)
                {
                    remove.Add(i * 3);
                    remove.Add(i * 3 + 1);
                    remove.Add(i * 3 + 2);
                }
            }
            for (int i = remove.Count - 1; i >= 0; --i)
            {
                indexLst.RemoveAt(remove[i]);
            }
        }
示例#2
0
        protected static LODMesh ParseMesh(Mesh meshIn)
        {
            LODMesh        lodMesh  = new LODMesh(meshIn);
            List <Vector3> vertices = new List <Vector3>();

            meshIn.GetVertices(vertices);
            List <LODVertex>      lodVertices = Convert(vertices);
            GroupHash <LODVertex> group       = new GroupHash <LODVertex>();

            group.Objects = lodVertices;

            List <List <LODVertex> >         merged = group.GetResult();
            Dictionary <int, RepeatedVertex> originalVertexMapRepeatedIndex = new Dictionary <int, RepeatedVertex>();

            for (int i = 0; i < merged.Count; ++i)
            {
                RepeatedVertex rpv = new RepeatedVertex(merged[i], i);
                lodMesh.vertices.Add(rpv);
                foreach (LODVertex v in merged[i])
                {
                    originalVertexMapRepeatedIndex.Add(v.originalIndex, rpv);
                }
            }
            Dictionary <int, LODSubMesh> originalFaceMapSubMeshIndex = new Dictionary <int, LODSubMesh>();

            int        count     = meshIn.subMeshCount;
            List <int> triangles = new List <int>();

            for (int i = 0; i < count; ++i)
            {
                LODSubMesh subMesh = new LODSubMesh(i);
                lodMesh.subMeshes.Add(subMesh);
                triangles.Clear();
                meshIn.GetTriangles(triangles, i);

                int cnt = triangles.Count;
                for (int j = 0; j < cnt; j += 3)
                {
                    int tmp0 = triangles[j];
                    int tmp1 = triangles[j + 1];
                    int tmp2 = triangles[j + 2];

                    int idx0 = originalVertexMapRepeatedIndex[tmp0].index;
                    int idx1 = originalVertexMapRepeatedIndex[tmp1].index;
                    int idx2 = originalVertexMapRepeatedIndex[tmp2].index;

                    int faceIndex = lodMesh.faces.Count;
                    originalFaceMapSubMeshIndex.Add(faceIndex, subMesh);
                    subMesh.faceIndices.Add(faceIndex);
                    lodMesh.faces.Add(new LODFace(lodMesh, lodMesh.subMeshes.Count - 1, idx0, idx1, idx2));

                    // 绑定临点
                    originalVertexMapRepeatedIndex[idx0].neighborVertices.Add(idx1);
                    originalVertexMapRepeatedIndex[idx0].neighborVertices.Add(idx2);

                    originalVertexMapRepeatedIndex[idx1].neighborVertices.Add(idx0);
                    originalVertexMapRepeatedIndex[idx1].neighborVertices.Add(idx2);

                    originalVertexMapRepeatedIndex[idx2].neighborVertices.Add(idx0);
                    originalVertexMapRepeatedIndex[idx2].neighborVertices.Add(idx1);

                    // 边
                    LODEdge e0 = new LODEdge(idx0, idx1);
                    LODEdge e1 = new LODEdge(idx0, idx2);
                    LODEdge e2 = new LODEdge(idx1, idx2);

                    LODEdge ee0 = lodMesh.edges.Find((item) => { return(e0.Equals(item)); });
                    LODEdge ee1 = lodMesh.edges.Find((item) => { return(e1.Equals(item)); });
                    LODEdge ee2 = lodMesh.edges.Find((item) => { return(e2.Equals(item)); });
                    if (ee0 != null)
                    {
                        ee0.Share();
                    }
                    else
                    {
                        lodMesh.edges.Add(e0);
                    }
                    if (ee1 != null)
                    {
                        ee1.Share();
                    }
                    else
                    {
                        lodMesh.edges.Add(e1);
                    }
                    if (ee2 != null)
                    {
                        ee2.Share();
                    }
                    else
                    {
                        lodMesh.edges.Add(e2);
                    }

                    // 临面
                    originalVertexMapRepeatedIndex[idx0].neighborFaces.Add(faceIndex);
                    originalVertexMapRepeatedIndex[idx1].neighborFaces.Add(faceIndex);
                    originalVertexMapRepeatedIndex[idx2].neighborFaces.Add(faceIndex);
                }
            }

            //for (int i = 0; i < lodMesh.faces.Count - 1; ++i)
            //{
            //    // Fill the neighbor faces information in face list
            //    for (int j = i + 1; j < lodMesh.faces.Count; ++j)
            //    {
            //        if (IsNeighbors(lodMesh, i, j))
            //        {
            //            lodMesh.faces[i].neighboringFaceIndexList.Add(j);
            //            lodMesh.faces[j].neighboringFaceIndexList.Add(i);
            //        }
            //    }
            //}

            return(lodMesh);
        }