public static Edge GetEdge(Point A, Point B, IIsoSurface surface, List <Edge> edges) { Vector3 p; if (A.iso == 0) { p = A.p; } else if (B.iso == 0) { p = B.p; } else { Vector3 diff = B.p - A.p; Vector3 start = A.p; float ratio = 0.5f; p = start + (diff * ratio); float f = 0.25f; float iso; while ((iso = surface.sample(p.x, p.y, p.z)) > 0.0001) { ratio += f * Mathf.Sign(iso * A.iso); f /= 2; p = start + (diff * ratio); } } Vector3 n = surface.sampleDerivative(p.x, p.y, p.z); //Debug.DrawLine(A.p, B.p, new Color(0,0,1,0.5f), 30); return(new Edge(p, n, A, B, edges)); }
public void DebugDraw(IIsoSurface surface) { if (this.edges.Count < 1) { return; } Vector3 p = getPoint(); Debug.DrawRay(p, surface.sampleDerivative(p.x, p.y, p.z), new Color(0, 0.4f, 0, 0.4f)); //Debug.DrawLine(Vector3.zero, p, Color.blue); }
Point getPoint(int x, int y, int z, float cellSize, Point[,,] points, IIsoSurface surface) { Point point = points[x, y, z]; if (point == null) { Vector3 p = new Vector3(x * (float)cellSize, y * (float)cellSize, z * (float)cellSize); point = new Point(surface.sample(p.x, p.y, p.z), p, x, y, z); points[x, y, z] = point; } return(point); }
public Edge getEdge(Point point, IIsoSurface surface, List <Edge> edges) { //Debug.DrawLine(p, point.p, new Color(0.2f,0.2f,0.2f,0.2f), 30); foreach (Edge edge in this.edges) { if (edge.A == point || edge.B == point) { //Debug.DrawLine(p, point.p, new Color(1,0,0,0.4f), 30); return(edge); } } Edge e = Edge.GetEdge(this, point, surface, edges); //Debug.DrawLine(p, point.p, new Color(0,0,1,0.4f), 30); return(e); }
void addEdge(Point A, Point B, IIsoSurface surface, List <Edge> edges) { //Debug.DrawLine(A.p, B.p, new Color(0.2f,0.2f,0.2f,0.2f), 30); if ((A.iso < 0 && B.iso > 0) || (A.iso > 0 && B.iso < 0)) { Edge edge = A.getEdge(B, surface, edges); if (A != edge.A && B != edge.B) { if (A != edge.B && A != edge.A) { //Debug.DrawLine(A.p, B.p, new Color(0,1,0,0.4f), 30); //Debug.DrawLine(edge.A.p, edge.B.p, new Color(1,0,0,0.4f), 30); } } edges.Add(edge); this.edges.Add(edge); edge.addCell(this); } }
// Use this for initialization void Start() { //surface = new CircleSurface(4.25f, new Vector3(5,5,5), 0.0001f); surface = new Surface(5.4f); edges = new List <Edge>(); points = new Point[size + 1, size + 1, size + 1]; cells = new Cell[size, size, size]; for (int x = 0; x < size; x++) { for (int y = 0; y < 10; y++) { for (int z = 0; z < size; z++) { cells[x, y, z] = new Cell(x, y, z, cellSize, points, edges, surface); } } } meshFilter = GetComponent <MeshFilter>(); Mesh mesh = new Mesh(); List <Vector3> vertices = new List <Vector3>(); List <Vector3> normals = new List <Vector3>(); List <int> triangles = new List <int>(); foreach (Edge edge in edges) { int i = edge.Draw(vertices, normals, triangles, surface); Debug.Log("I: " + i); } mesh.vertices = vertices.ToArray(); mesh.normals = normals.ToArray(); mesh.triangles = triangles.ToArray(); meshFilter.mesh = mesh; }
public int Draw(List <Vector3> vertices, List <Vector3> normals, List <int> triangles, IIsoSurface surface) { if (this.cells.Count < 4) { return(0); } int start = vertices.Count; int i = 0; foreach (Cell cell in cells) { i++; Vector3 p = cell.getPoint(); vertices.Add(p); normals.Add(surface.sampleDerivative(p.x, p.y, p.z)); } List <int> tris = new List <int>(); tris.Add(start); tris.Add(start + 1); tris.Add(start + 2); tris.Add(start + 1); tris.Add(start + 3); tris.Add(start + 2); /*for(int j = 0; j < tris.Count; j++) * { * triangles.Add(tris[j]); * }*/ Vector3 diff = B.p - A.p; for (int j = 0; j < tris.Count; j++) { triangles.Add(tris[j]); } for (int j = tris.Count - 1; j > -1; j--) { triangles.Add(tris[j]); } /*triangles.Add(start + 2); * triangles.Add(start + 3); * triangles.Add(start + 1); * triangles.Add(start + 3); * triangles.Add(start + 1); * triangles.Add(start);*/ return(i); }
public Cell(int x, int y, int z, float cellSize, Point[,,] points, List <Edge> edges, IIsoSurface surface) { this.edges = new List <Edge>(); this.points = new Point[2, 2, 2]; for (int xx = 0; xx < 2; xx++) { for (int yy = 0; yy < 2; yy++) { for (int zz = 0; zz < 2; zz++) { this.points[xx, yy, zz] = getPoint(x + xx, y + yy, z + zz, cellSize, points, surface); } } } point = new Vector3(x + 0.5f, y + 0.5f, z + 0.5f); addEdge(this.points[0, 0, 0], this.points[1, 0, 0], surface, edges); addEdge(this.points[0, 0, 0], this.points[0, 1, 0], surface, edges); addEdge(this.points[0, 0, 0], this.points[0, 0, 1], surface, edges); addEdge(this.points[0, 0, 1], this.points[0, 1, 1], surface, edges); addEdge(this.points[0, 0, 1], this.points[1, 0, 1], surface, edges); addEdge(this.points[0, 1, 0], this.points[0, 1, 1], surface, edges); addEdge(this.points[0, 1, 0], this.points[1, 1, 0], surface, edges); addEdge(this.points[0, 1, 1], this.points[1, 1, 1], surface, edges); addEdge(this.points[1, 0, 0], this.points[1, 0, 1], surface, edges); addEdge(this.points[1, 0, 0], this.points[1, 1, 0], surface, edges); addEdge(this.points[1, 0, 1], this.points[1, 1, 1], surface, edges); addEdge(this.points[1, 1, 0], this.points[1, 1, 1], surface, edges); }