void Awake() { meshFilter = GetComponent <MeshFilter>(); meshUtility = new MeshUtility(); DebugGraph.meshUtility = meshUtility; Vector3 base1 = new Vector3(0, -0.5f, 0.5f); Vector3 base2 = new Vector3(-0.5f, -0.5f, -0.5f); Vector3 base3 = new Vector3(0.5f, -0.5f, -0.5f); Vector3 top = new Vector3(0, 0.5f, 0); meshUtility.CreateTriangle(base1, base2, base3); meshUtility.CreateTriangle(base1, base2, top); meshUtility.CreateTriangle(base2, base3, top); meshUtility.CreateTriangle(base3, base1, top); meshFilter.sharedMesh = meshUtility.ToMesh(); }
public IEnumerator SplitEdge(Edge edge, MeshUtility newMeshUtility) { // split the edge Vertex v1 = edge.Vertices[0]; Vertex v2 = edge.Vertices[1]; Vertex newMiddle = new Vertex(edge.newPosition); newMiddle.isNew = true; Edge new1 = new Edge(v1, newMiddle); Edge new2 = new Edge(v2, newMiddle); new1.isNew = true; new2.isNew = true; // for each touching triangle for (int i = 0; i < 2; ++i) { Triangle t = edge.Triangles[i]; // create a new edge between middle vertex and opposite vertex List <Vertex> tVertices = new List <Vertex>(t.GetVertices()); if (!tVertices.Remove(v1)) { Debug.Log("Could not remove v1 from tVertices!"); } if (!tVertices.Remove(v2)) { Debug.Log("Could not remove v2 from tVertices!"); } Vertex opposite = tVertices[0]; Edge newEdge = new Edge(newMiddle, opposite); newEdge.isNew = true; // update triangle of the other edges of the old triangle List <Edge> tEdges = new List <Edge>(t.Edges); //List<Edge> tEdgesColorCopy = new List<Edge>(t.edges); //foreach (Edge e in tEdges) { // e.color = Color.yellow; //} //edge.color = Color.cyan; if (!tEdges.Remove(edge)) { Debug.Log("Could not remove edge from tEdges!"); } Vertex[] vertices = tEdges[0].Vertices; HashSet <Vertex> commonVertices = new HashSet <Vertex>(vertices); commonVertices.IntersectWith(new1.Vertices); Vertex newOpposite; if (!opposite.isNew) { newOpposite = new Vertex(opposite.newPosition); newOpposite.isNew = opposite.isNew; } else { newOpposite = opposite; } Vertex newV1; if (!v1.isNew) { newV1 = new Vertex(v1.newPosition); newV1.isNew = v1.isNew; } else { newV1 = v1; } Vertex newV2; if (!v2.isNew) { newV2 = new Vertex(v2.newPosition); newV2.isNew = v2.isNew; } else { newV2 = v2; } Triangle newT1; // Those triangles must be created for edges to be Triangle newT2; // correctly updated for the rest of the algorithm if (commonVertices.Count != 0) { newT1 = new Triangle(newEdge, new1, tEdges[0]); newT2 = new Triangle(newEdge, new2, tEdges[1]); } else { newT1 = new Triangle(newEdge, new2, tEdges[0]); newT2 = new Triangle(newEdge, new1, tEdges[1]); } Triangle tNew = new Triangle(new Edge(newOpposite, newV1), new Edge(newV1, newV2), new Edge(newV2, newOpposite)); newMeshUtility.CreateTriangle(newMiddle, newOpposite, newV1, /*Color.cyan*/ null, 0, tNew); newMeshUtility.CreateTriangle(newMiddle, newOpposite, newV2, /*Color.cyan*/ null, 0, tNew); // Update old edges' triangles // Note: the new triangles have already been added to old edges if (!tEdges[0].InternalRemoveTriangle(t)) { Debug.Log("Could not remove t from tEdges[0]!"); } if (!tEdges[1].InternalRemoveTriangle(t)) { Debug.Log("Could not remove t from tEdges[1]!"); } } //yield return new WaitForSeconds(0.1f); yield return(null); //foreach (Edge e in tEdgesColorCopy) { // e.color = Edge.defaultColor; //} }
void Awake() { meshFilter = GetComponent <MeshFilter>(); meshUtility = new MeshUtility(); DebugGraph.meshUtility = meshUtility; float t = (1.0f + Mathf.Sqrt(5.0f)) / 2.0f; Vector3 p1 = new Vector3(-1, t, 0).normalized; Vector3 p2 = new Vector3(1, t, 0).normalized; Vector3 p3 = new Vector3(-1, -t, 0).normalized; Vector3 p4 = new Vector3(1, -t, 0).normalized; Vector3 p5 = new Vector3(0, -1, t).normalized; Vector3 p6 = new Vector3(0, 1, t).normalized; Vector3 p7 = new Vector3(0, -1, -t).normalized; Vector3 p8 = new Vector3(0, 1, -t).normalized; Vector3 p9 = new Vector3(t, 0, -1).normalized; Vector3 p10 = new Vector3(t, 0, 1).normalized; Vector3 p11 = new Vector3(-t, 0, -1).normalized; Vector3 p12 = new Vector3(-t, 0, 1).normalized; meshUtility.CreateTriangle(p1, p12, p6); meshUtility.CreateTriangle(p1, p6, p2); meshUtility.CreateTriangle(p1, p2, p8); meshUtility.CreateTriangle(p1, p8, p11); meshUtility.CreateTriangle(p1, p11, p12); meshUtility.CreateTriangle(p2, p6, p10); meshUtility.CreateTriangle(p6, p12, p5); meshUtility.CreateTriangle(p12, p11, p3); meshUtility.CreateTriangle(p11, p8, p7); meshUtility.CreateTriangle(p8, p2, p9); meshUtility.CreateTriangle(p4, p10, p5); meshUtility.CreateTriangle(p4, p5, p3); meshUtility.CreateTriangle(p4, p3, p7); meshUtility.CreateTriangle(p4, p7, p9); meshUtility.CreateTriangle(p4, p9, p10); meshUtility.CreateTriangle(p5, p10, p6); meshUtility.CreateTriangle(p3, p5, p12); meshUtility.CreateTriangle(p7, p3, p11); meshUtility.CreateTriangle(p9, p7, p8); meshUtility.CreateTriangle(p10, p9, p2); meshFilter.sharedMesh = meshUtility.ToMesh(); }
public void Step() { MeshUtility temp = new MeshUtility(); setCoef(util.Vertices.Count); List <Vertex> vs = new List <Vertex>(); //Centers foreach (Triangle t in util.Triangles) { Vertex vert = new Vertex(((t.GetVertices()[0].Position + t.GetVertices()[1].Position + t.GetVertices()[2].Position) / 3)); temp.FindOrCreateVertex(vert); vs.Add(vert); } foreach (Vertex v in vs) { foreach (Vertex v2 in vs) { if (v != v2) { temp.FindOrCreateEdge(v, v2); } } } List <Edge> edges = new List <Edge>(); foreach (Edge e in temp.Edges) { edges.Add(e); } foreach (Edge e in edges) { foreach (Vertex v in vs) { if (v != e.Vertices[0] && v != e.Vertices[1] && e.Vertices[0] != e.Vertices[1]) { temp.CreateTriangle(v, e.Vertices[0], e.Vertices[1]); } } } foreach (Triangle t in util.Triangles) { Vector3 vt = getVertexPoint(t); temp.FindOrCreateVertex(vt); } //foreach (Vertex v in util.Vertices) //{ // for (int j = 0; j < v.GetTriangles().Length; j++) // { // Triangle t = v.GetTriangles()[j]; // Triangle tnext = null; // int i = 0; // do // { // if (i > v.GetTriangles().Length) // tnext = null; // else // tnext = v.GetTriangles()[i]; // temp.CreateTriangle(tnext.GetVertices()[0], tnext.GetVertices()[1], tnext.GetVertices()[2]); // t = tnext; // i++; // } while (t != v.GetTriangles()[j] && t != null); // } //} Debug.Log(temp.Vertices.Count); Debug.Log(temp.Edges.Count); Debug.Log(temp.Triangles.Count); util = temp; DebugGraph.meshUtility = util; filter.sharedMesh = util.ToMesh(); }