private void SolveOneId(int id) { dictionaryOfBorderVertex.Clear(); dictionaryOfUsedVertex.Clear(); dictionaryOfUsedVertex.Add(id, null); BorderVertex borderVertex; foreach (Arc arc in dictionaryOfVertexForZone[id].ArcThisVertex) { if (!dictionaryOfUsedVertex.ContainsKey(arc.NextVertex.Id)) { borderVertex = new BorderVertex(); borderVertex.ArcUsed = arc; borderVertex.IdNext = arc.NextVertex.Id; dictionaryOfBorderVertex.Add(arc.WightArc, borderVertex); } } double wight; while (dictionaryOfBorderVertex.Count() > 0) { id = dictionaryOfBorderVertex.Min().Value.IdNext; wight = dictionaryOfBorderVertex.Min().Key; dictionaryOfUsedVertex.Add(id, dictionaryOfBorderVertex.Min().Value.ArcUsed); // Удаляем врешину из словаря границ. dictionaryOfBorderVertex.Remove(wight); foreach (Arc arc in dictionaryOfVertexForZone[id].ArcThisVertex) { if (!dictionaryOfUsedVertex.ContainsKey(arc.NextVertex.Id)) { AddUsedVertex(arc); borderVertex = new BorderVertex(); borderVertex.ArcUsed = arc; borderVertex.IdNext = arc.NextVertex.Id; dictionaryOfBorderVertex.Add(wight + arc.WightArc, borderVertex); } } } // Нужно выставить все полученные значения в наш граф foreach (KeyValuePair<int, Arc> kvp in dictionaryOfUsedVertex) { if (kvp.Value != null) { kvp.Value.AddIdInWay(id); } } dictionaryOfBorderVertex.Clear(); dictionaryOfUsedVertex.Clear(); return; }
private WorkingMesh MakeBorder(WorkingMesh source, Heightmap heightmap, int borderCount) { List <Vector3> vertices = source.vertices.ToList(); List <Vector3> normals = source.normals.ToList(); List <Vector2> uvs = source.uv.ToList(); List <int[]> subMeshTris = new List <int[]>(); int maxTris = 0; for (int si = 0; si < source.subMeshCount; ++si) { List <int> tris = source.GetTriangles(si).ToList(); List <Vector2Int> edges = GetEdgeList(tris); HashSet <int> vertexIndces = new HashSet <int>(); List <BorderVertex> edgeVertices = new List <BorderVertex>(); for (int ei = 0; ei < edges.Count; ++ei) { vertexIndces.Add(edges[ei].x); vertexIndces.Add(edges[ei].y); } List <BorderVertex> borderVertices = GenerateBorderVertices(heightmap, borderCount); //calculate closest vertex from border vertices. for (int i = 0; i < borderVertices.Count; ++i) { float closestDistance = Single.MaxValue; BorderVertex v = borderVertices[i]; foreach (var index in vertexIndces) { Vector3 pos = vertices[index]; float dist = Vector3.SqrMagnitude(pos - borderVertices[i].Pos); if (dist < closestDistance) { closestDistance = dist; v.ClosestIndex = index; } } borderVertices[i] = v; } //generate tris int startAddIndex = vertices.Count; for (int bi = 0; bi < borderVertices.Count; ++bi) { int next = (bi == borderVertices.Count - 1) ? 0 : bi + 1; tris.Add(bi + startAddIndex); tris.Add(borderVertices[bi].ClosestIndex); tris.Add(next + startAddIndex); Vector2 uv; uv.x = (borderVertices[bi].Pos.x - heightmap.Offset.x) / heightmap.Size.x; uv.y = (borderVertices[bi].Pos.z - heightmap.Offset.z) / heightmap.Size.z; vertices.Add(borderVertices[bi].Pos); if (m_hlod.UseNormal) { normals.Add(Vector3.up); } else { normals.Add(heightmap.GetInterpolatedNormal(uv.x, uv.y)); } uvs.Add(uv); if (borderVertices[bi].ClosestIndex == borderVertices[next].ClosestIndex) { continue; } tris.Add(borderVertices[bi].ClosestIndex); tris.Add(borderVertices[next].ClosestIndex); tris.Add(next + startAddIndex); } maxTris += tris.Count; subMeshTris.Add(tris.ToArray()); } WorkingMesh mesh = new WorkingMesh(Allocator.Persistent, vertices.Count, maxTris, subMeshTris.Count, 0); mesh.name = source.name; mesh.vertices = vertices.ToArray(); mesh.normals = normals.ToArray(); mesh.uv = uvs.ToArray(); for (int i = 0; i < subMeshTris.Count; ++i) { mesh.SetTriangles(subMeshTris[i], i); } return(mesh); }