/// this method renders two meshes: /// cmesh the collision mesh (just the outline vertices extruded) /// rmesh the render mesh (the 2d surface of the given voxel lattices) /// public void MarchSquares(out Mesh cmesh, out Mesh rmesh, ref SquareCell[,] cells, float isolevel) { Vector2 uvScale = new Vector2(1.0f / cells.GetLength(0), 1.0f / cells.GetLength(1)); // triangle index counter int tricount = 0; // collider triangle index counter int ctricount = 0; // mesh data arrays - just clear when reused if (vert == null) vert = new ArrayList(); else vert.Clear(); if (uv == null) uv = new ArrayList(); else uv.Clear(); if (tri == null) tri = new ArrayList(); else tri.Clear(); if (norm == null) norm = new ArrayList(); else norm.Clear(); // collider mesh arrays if (cvert == null) cvert = new ArrayList(); else cvert.Clear(); if (cuv == null) cuv = new ArrayList(); else cuv.Clear(); if (ctri == null) ctri = new ArrayList(); else ctri.Clear(); if (cnorm == null) cnorm = new ArrayList(); else cnorm.Clear(); for (int i = 0; i < cells.GetLength(0); i++) { for (int j = 0; j < cells.GetLength(1); j++) { SquareCell cell = cells[i,j]; Triangle[] triangles; Polygonise(cell, out triangles, isolevel); for (int k = 0; k < triangles.Length; k++) { Triangle triangle = triangles[k]; if (triangle != null) { Vector3 p0 = new Vector3(triangle.p[0].x, 0, triangle.p[0].y); Vector3 p1 = new Vector3(triangle.p[1].x, 0, triangle.p[1].y); Vector3 p2 = new Vector3(triangle.p[2].x, 0, triangle.p[2].y); /// Start Vertices One --------------------------------------- vert.Add(p0); vert.Add(p1); vert.Add(p2); // Triangles tri.Add(tricount); tri.Add(tricount+1); tri.Add(tricount+2); // Normals Vector3 vn1 = p0 - p1; Vector3 vn2 = p0 - p2; Vector3 n = Vector3.Normalize ( Vector3.Cross(vn1,vn2) ); norm.Add(n); norm.Add(n); norm.Add(n); uv.Add(Vector2.Scale(new Vector2 (p0.x, p0.z), new Vector2(uvScale.x, uvScale.y))); uv.Add(Vector2.Scale(new Vector2 (p1.x, p1.z), new Vector2(uvScale.x, uvScale.y))); uv.Add(Vector2.Scale(new Vector2 (p2.x, p2.z), new Vector2(uvScale.x, uvScale.y))); tricount += 3; /// END Vertices One --------------------------------------- if (triangle.outerline[0] != -1) { Vector3 o1 = new Vector3(triangle.p[triangle.outerline[0]].x, 0, triangle.p[triangle.outerline[0]].y); Vector3 o2 = new Vector3(triangle.p[triangle.outerline[1]].x, 0, triangle.p[triangle.outerline[1]].y); Vector3 bo1 = o1; o1.y = -1; // o1 transposed one unit down Vector3 bo2 = o2; o2.y = -1; // o2 transposed one unit down /// Start Vertices Two --------------------------------------- cvert.Add(o1); cvert.Add(o2); cvert.Add(bo1); // Triangles ctri.Add(ctricount); ctri.Add(ctricount+1); ctri.Add(ctricount+2); // Normals Vector3 ovn1 = o1 - o2; Vector3 ovn2 = o1 - bo1; Vector3 on = Vector3.Normalize ( Vector3.Cross(ovn1,ovn2) ); cnorm.Add(on); cnorm.Add(on); cnorm.Add(on); cuv.Add(Vector2.zero); cuv.Add(Vector2.zero); cuv.Add(Vector2.zero); ctricount += 3; /// END Vertices Two --------------------------------------- /// Start Vertices Three --------------------------------------- cvert.Add(bo2); cvert.Add(bo1); cvert.Add(o2); // Triangles ctri.Add(ctricount); ctri.Add(ctricount+1); ctri.Add(ctricount+2); // Normals Vector3 oovn1 = o2 - bo1; Vector3 oovn2 = o2 - bo2; Vector3 oon = Vector3.Normalize ( Vector3.Cross(oovn1,oovn2) )*-1; cnorm.Add(oon); cnorm.Add(oon); cnorm.Add(oon); cuv.Add(Vector2.zero); cuv.Add(Vector2.zero); cuv.Add(Vector2.zero); ctricount += 3; /// END Vertices Three --------------------------------------- } } } } } // prepare the collision mesh cmesh = new Mesh(); cmesh.vertices = (Vector3[]) cvert.ToArray(typeof(Vector3)); cmesh.uv = (Vector2[]) cuv.ToArray(typeof(Vector2)); cmesh.triangles = (int[]) ctri.ToArray(typeof(int)); cmesh.normals = (Vector3[]) cnorm.ToArray(typeof(Vector3)); // prepare the render mesh rmesh = new Mesh(); rmesh.vertices = (Vector3[]) vert.ToArray(typeof(Vector3)); rmesh.uv = (Vector2[]) uv.ToArray(typeof(Vector2)); rmesh.triangles = (int[]) tri.ToArray(typeof(int)); rmesh.normals = (Vector3[]) norm.ToArray(typeof(Vector3)); }