private static bool[,] CreateOptimizedFaces(ColorPlanePos pos, ColorPlane plane) { plane.vertices = new List <Vector3>(); plane.triangles = new List <int>(); int count = 0; bool[,] matrix = new bool[plane.sizeX, plane.sizeY]; foreach (VOXPos dot in plane.dots) { matrix[dot.x - plane.minX, dot.y - plane.minY] = true; count++; } SplitToRects(matrix, pos, plane, count); return(matrix); }
private static void SplitToRects(bool[,] matrix, ColorPlanePos pos, ColorPlane plane, int count) { int[,] h, w; h = new int[plane.sizeX, plane.sizeY]; w = new int[plane.sizeX, plane.sizeY]; while (count > 0) { int minw = 0, area = 0; int maxArea = 0; int[] maxFace = new int[4] { 0, 0, 0, 0 }; for (int j = 0; j < plane.sizeX; j++) { for (int i = 0; i < plane.sizeY; i++) { if (!matrix[j, i]) { continue; } if (j == 0) { h[j, i] = 1; } else { h[j, i] = h[j - 1, i] + 1; } if (i == 0) { w[j, i] = 1; } else { w[j, i] = w[j, i - 1] + 1; } minw = w[j, i]; for (int dh = 0; dh < h[j, i]; dh++) { if (w[j - dh, i] < minw) { minw = w[j - dh, i]; } area = (dh + 1) * minw; if (area > maxArea) { maxArea = area; maxFace[0] = i - minw + 1; maxFace[1] = j - dh; maxFace[2] = i; maxFace[3] = j; } } } } // add faces int vi = allVertices.Count; bool order = true; // TODO: add minX, minY if (pos.normal.y == -1) { allVertices.Add(new Vector3(maxFace[1], pos.pos + 1, maxFace[0])); allVertices.Add(new Vector3(maxFace[3] + 1, pos.pos + 1, maxFace[0])); allVertices.Add(new Vector3(maxFace[3] + 1, pos.pos + 1, maxFace[2] + 1)); allVertices.Add(new Vector3(maxFace[1], pos.pos + 1, maxFace[2] + 1)); float x1 = maxFace[1]; float x2 = maxFace[3] + 1; float y1 = maxFace[0]; float y2 = maxFace[2] + 1; x1 /= model.sizeX; x2 /= model.sizeX; y1 /= model.sizeY; y2 /= model.sizeY; allUVs.Add(new Vector2(x2, y1)); allUVs.Add(new Vector2(x1, y1)); allUVs.Add(new Vector2(x1, y2)); allUVs.Add(new Vector2(x2, y2)); } else if (pos.normal.y == 1) { allVertices.Add(new Vector3(maxFace[1], pos.pos, maxFace[0])); allVertices.Add(new Vector3(maxFace[3] + 1, pos.pos, maxFace[0])); allVertices.Add(new Vector3(maxFace[3] + 1, pos.pos, maxFace[2] + 1)); allVertices.Add(new Vector3(maxFace[1], pos.pos, maxFace[2] + 1)); order = false; } else if (pos.normal.z == -1) { allVertices.Add(new Vector3(maxFace[1], maxFace[0], pos.pos)); allVertices.Add(new Vector3(maxFace[3] + 1, maxFace[0], pos.pos)); allVertices.Add(new Vector3(maxFace[3] + 1, maxFace[2] + 1, pos.pos)); allVertices.Add(new Vector3(maxFace[1], maxFace[2] + 1, pos.pos)); } else if (pos.normal.z == 1) { allVertices.Add(new Vector3(maxFace[1], maxFace[0], pos.pos + 1)); allVertices.Add(new Vector3(maxFace[3] + 1, maxFace[0], pos.pos + 1)); allVertices.Add(new Vector3(maxFace[3] + 1, maxFace[2] + 1, pos.pos + 1)); allVertices.Add(new Vector3(maxFace[1], maxFace[2] + 1, pos.pos + 1)); order = false; } else if (pos.normal.x == -1) { allVertices.Add(new Vector3(pos.pos, maxFace[0], maxFace[1])); allVertices.Add(new Vector3(pos.pos, maxFace[0], maxFace[3] + 1)); allVertices.Add(new Vector3(pos.pos, maxFace[2] + 1, maxFace[3] + 1)); allVertices.Add(new Vector3(pos.pos, maxFace[2] + 1, maxFace[1])); order = false; } else if (pos.normal.x == 1) { allVertices.Add(new Vector3(pos.pos + 1, maxFace[0], maxFace[1])); allVertices.Add(new Vector3(pos.pos + 1, maxFace[0], maxFace[3] + 1)); allVertices.Add(new Vector3(pos.pos + 1, maxFace[2] + 1, maxFace[3] + 1)); allVertices.Add(new Vector3(pos.pos + 1, maxFace[2] + 1, maxFace[1])); } if (pos.normal.y != -1) { allUVs.Add(new Vector2(1, 0)); allUVs.Add(new Vector2(0, 0)); allUVs.Add(new Vector2(0, 1)); allUVs.Add(new Vector2(1, 1)); } List <int> list = null; if (!allTriangles.ContainsKey(pos.matID)) { list = allTriangles[pos.matID] = new List <int>(); } else { list = allTriangles[pos.matID]; } if (order) { list.Add(vi); list.Add(vi + 2); list.Add(vi + 1); list.Add(vi + 2); list.Add(vi); list.Add(vi + 3); } else { list.Add(vi); list.Add(vi + 1); list.Add(vi + 2); list.Add(vi + 2); list.Add(vi + 3); list.Add(vi); } for (int j = maxFace[1]; j <= maxFace[3]; j++) { for (int i = maxFace[0]; i <= maxFace[2]; i++) { matrix[j, i] = false; count--; } } for (int j = 0; j < plane.sizeX; j++) { for (int i = 0; i < plane.sizeY; i++) { w[j, i] = 0; h[j, i] = 0; } } } }