List <Vector3> subDivideMesh(Mesh mesh, float maxLength) { DCEL newDCEL = new DCEL(mesh); List <Face> facesToSubdivide = new List <Face>(newDCEL.faces); Color[] edgeColors = new Color[6] { Color.red, Color.green, Color.blue, Color.yellow, Color.cyan, Color.magenta }; while (facesToSubdivide.Count > 0) { Face f = facesToSubdivide[facesToSubdivide.Count - 1]; facesToSubdivide.RemoveAt(facesToSubdivide.Count - 1); HalfEdge currEdge = f.edge; HalfEdge startEdge = currEdge; int edgeCount = 0; do { edgeCount++; currEdge = currEdge.next; }while(currEdge != f.edge); /* * if (edgeCount > 3) * { * do{ * //Debug.DrawLine(currEdge.vertex.vert, currEdge.next.vertex.vert, Color.black, 10); * currEdge = currEdge.next; * }while(currEdge != f.edge); * while(currEdge.vertex.newVertex == 0){ * currEdge = currEdge.next; * if (currEdge == f.edge){ * Debug.Log("ERROR"); * break; * } * } * Vector3 offset = new Vector3(0, 0f, 0); * Vector3[] verts = new Vector3[3]{currEdge.vertex.vert, currEdge.next.vertex.vert, currEdge.next.next.vertex.vert}; * Vector3 avg = verts[0]+verts[1]+verts[2]; * avg /= 3; * verts[0] += (avg - verts[0]) * 0.1f; * verts[1] += (avg - verts[1]) * 0.1f; * verts[2] += (avg - verts[2]) * 0.1f; * //Debug.DrawLine(verts[0], verts[1], Color.red, 10); * //Debug.DrawLine(verts[1], verts[2], Color.green, 10); * //Debug.DrawLine(verts[2], verts[0], Color.blue, 10); * }else{ */ int edgeIndex = 0; bool noSubDivision = true; do { float len = (currEdge.prev().vertex.vert - currEdge.vertex.vert).magnitude; /* * Vector3[] verts = new Vector3[3]{currEdge.vertex.vert, currEdge.next.vertex.vert, currEdge.next.next.vertex.vert}; * Vector3 avg = verts[0]+verts[1]+verts[2]; * avg /= 3; * verts[0] += (avg - verts[0]) * 0.1f; * verts[1] += (avg - verts[1]) * 0.1f; * verts[2] += (avg - verts[2]) * 0.1f; * Debug.DrawLine(verts[0], verts[1], Color.red, 10); * Debug.DrawLine(verts[1], verts[2], Color.green, 10); * Debug.DrawLine(verts[2], verts[0], Color.blue, 10); */ if (len > maxLength) { noSubDivision = false; Vertex newVert = new Vertex(currEdge.prev().vertex.vert / 2 + currEdge.vertex.vert / 2); newVert.newVertex = 2; HalfEdge newEdge = new HalfEdge(currEdge.vertex, currEdge.face); HalfEdge oppositeEdge = currEdge.opposite; currEdge.vertex = newVert; newEdge.next = currEdge.next; currEdge.next = newEdge; newVert.edge = newEdge; if (oppositeEdge != null) { HalfEdge newOppositeEdge = new HalfEdge(currEdge.prev().vertex, oppositeEdge.face); oppositeEdge.vertex = newVert; newOppositeEdge.next = oppositeEdge.next; oppositeEdge.next = newOppositeEdge; newOppositeEdge.opposite = currEdge; currEdge.opposite = newOppositeEdge; newEdge.opposite = oppositeEdge; oppositeEdge.opposite = newEdge; } currEdge = currEdge.next; } else { //Debug.DrawLine(currEdge.prev().vertex.vert, currEdge.vertex.vert, Color.white, 10); } currEdge = currEdge.next; edgeIndex++; }while (currEdge != startEdge); Vector3 avg = Vector3.zero; HalfEdge currForAvg = currEdge; int avgCount = 0; do { avg += currForAvg.vertex.vert; currForAvg = currForAvg.next; avgCount++; }while (currForAvg != currEdge); avg /= avgCount; edgeIndex = 0; do { Vector3 v1 = currEdge.prev().vertex.vert; Vector3 v2 = currEdge.vertex.vert; v1 += (avg - v1) * 0.1f; v2 += (avg - v2) * 0.1f; Debug.DrawLine(v1, v2, edgeColors[edgeIndex++ % 6], 10); currEdge = currEdge.next; }while (currEdge != startEdge); if (noSubDivision == false) { //facesToSubdivide.Add(currEdge.face); } if (noSubDivision) { /* * currEdge = f.edge; * Vector3 avg = Vector3.zero; * HalfEdge currForAvg = currEdge; * int avgCount = 0; * do{ * avg += currForAvg.vertex.vert; * currForAvg = currForAvg.next; * avgCount++; * }while (currForAvg != currEdge); * avg /= avgCount; * edgeIndex = 0; * do{ * Vector3 edgeStart = currEdge.prev().vertex.vert; * Vector3 edgeEnd = currEdge.vertex.vert; * Vector3 newEdgeStart = edgeStart + (avg - edgeStart) * 0.1f; * Vector3 newEdgeEnd = edgeEnd + (avg - edgeEnd) * 0.1f; * Debug.DrawRay(newEdgeStart, (newEdgeEnd - newEdgeStart) * 0.95f, edgeColors[edgeIndex%6], 10); * if (currEdge.prev().vertex.edge == currEdge) * Debug.DrawLine(currEdge.prev().vertex.vert, newEdgeStart, edgeColors[edgeIndex%6]*0.8f, 10); * currEdge = currEdge.next; * edgeIndex++; * }while (currEdge != f.edge); */ // } } } return(new List <Vector3>()); }