// 默认共两条边 才算 临面 protected static bool IsNeighbors(LODMesh lodMesh, int faceIndex1, int faceIndex2, int shareVertexCount = 2) { int count = 0; if (faceIndex1 != faceIndex2) { LODFace face1 = lodMesh.faces[faceIndex1]; LODFace face2 = lodMesh.faces[faceIndex2]; for (int i = 0; i < 3; ++i) { if (face1.vertexIndexList.Contains(face2.vertexIndexList[i])) { ++count; if (count == shareVertexCount) { return(true); } } } } return(false); }
protected static List <int> GetEdgeShareFaces(LODMesh mesh, int iIdx, int jIdx, List <int> remain = null) { RepeatedVertex u = mesh.vertices[iIdx]; RepeatedVertex v = mesh.vertices[jIdx]; List <int> totalFaces = u.neighborFaces; List <int> uvFaces = new List <int>(); for (int k = 0; k < totalFaces.Count; ++k) { LODFace face = mesh.faces[totalFaces[k]]; if (face.ContainVertex(jIdx)) { uvFaces.Add(totalFaces[k]); } else { if (remain != null) { remain.Add(totalFaces[k]); // 保留不共享边的面 } } } return(uvFaces); }
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); }
/// <summary> /// 两个面夹角作为塌陷值 /// 夹角计算采用法线的点积cos(theta) /// 由于cos在0-180度为递减函数,这里变换为增函数(1.0f - dot) * 0.5f /// 表明夹角越大,塌陷越大 /// </summary> /// <param name="mesh"></param> /// <param name="uFace"></param> /// <param name="vFace"></param> protected static float DotFace(LODMesh mesh, int uFace, int vFace) { float dot = Vector3.Dot(mesh.faces[uFace].normal, mesh.faces[vFace].normal); return((1.0f - dot) * 0.5f); }
/// <summary> /// 两个面夹角作为塌陷值 /// 夹角计算采用法线的点积cos(theta) /// 由于cos在0-180度为递减函数,这里变换为增函数(1.0f - dot) * 0.5f /// 表明夹角越大,塌陷越大 /// </summary> /// <param name="mesh"></param> /// <param name="uFace"></param> /// <param name="vFace"></param> protected static float EdgeLength(LODMesh mesh, int vIdx1, int vIdx2) { return(Vector3.Magnitude(mesh.vertices[vIdx1].vertex - mesh.vertices[vIdx2].vertex)); }
/// <summary> /// 判断 点是否是 一条边界边的点 /// </summary> protected static bool IsBorder(LODMesh mesh, int iIdx) { RepeatedVertex v = mesh.vertices[iIdx]; return(IsBorder(mesh, v)); }
/// <summary> /// 构造新的Mesh /// </summary> /// <param name="mesh"></param> protected static void ConsolidateMesh(LODMesh mesh) { }
/// <summary> /// 简化 /// </summary> /// <param name="mesh"></param> protected static void Simplify(LODMesh mesh) { InitializeAllEdgeCost(mesh); CollapseMesh(mesh); ConsolidateMesh(mesh); }
/// <summary> /// 处理入口 /// </summary> /// <param name="meshIn"></param> public static void Process(Mesh meshIn) { LODMesh mesh = ParseMesh(meshIn); Simplify(mesh); }