public static CellInfo[,,] GenVertices(int resolution, UtilFuncs.Sampler samp) { CellInfo[,,] cellInfos = new CellInfo[resolution, resolution, resolution]; for (int x = 0; x < resolution; x++) { for (int y = 0; y < resolution; y++) { for (int z = 0; z < resolution; z++) { //Vector3 p = new Vector3(x, y, z); byte caseCode = 0; sbyte[] densities = new sbyte[8]; for (int i = 0; i < 8; i++) { Vector3 pos = new Vector3(x, y, z) + DCC.vfoffsets[i]; densities[i] = (sbyte)Mathf.Clamp((samp(pos.x, pos.y, pos.z) * 127f), -127f, 127f); //data[index + inArray[i]]; if (densities[i] < 0) { caseCode |= (byte)(1 << i); } } Vector3[] cpositions = new Vector3[4]; Vector3[] cnormals = new Vector3[4]; int edgeCount = 0; for (int i = 0; i < 12 && edgeCount < 4; i++) { byte c1 = (byte)DCC.edgevmap[i][0]; byte c2 = (byte)DCC.edgevmap[i][1]; Vector3 p1 = new Vector3(x, y, z) + DCC.vfoffsets[c1]; Vector3 p2 = new Vector3(x, y, z) + DCC.vfoffsets[c2]; bool m1 = ((caseCode >> c1) & 1) == 1; bool m2 = ((caseCode >> c2) & 1) == 1; if (m1 != m2) { cpositions[edgeCount] = ApproximateZeroCrossingPosition(p1, p2, samp); cnormals[edgeCount] = CalculateSurfaceNormal(cpositions[edgeCount], samp); edgeCount++; } } if (edgeCount == 0) { continue; } CellInfo cellInfo = new CellInfo(); QEF.QEFSolver qef = new QEF.QEFSolver(); for (int i = 0; i < edgeCount; i++) { qef.Add(cpositions[i], cnormals[i]); } cellInfo.Position = qef.Solve(0.0001f, 4, 0.0001f); //drawInfo.index = vertices.Count; Vector3 max = new Vector3(x, y, z) + Vector3.one; if (cellInfo.Position.x < x || cellInfo.Position.x > max.x || cellInfo.Position.y < y || cellInfo.Position.y > max.y || cellInfo.Position.z < z || cellInfo.Position.z > max.z) { cellInfo.Position = qef.MassPoint; } //vertices.Add(drawInfo.position); for (int i = 0; i < edgeCount; i++) { cellInfo.Normal += cnormals[i]; } cellInfo.Normal = Vector3.Normalize(cellInfo.Normal); //CalculateSurfaceNormal(drawInfo.position, samp); //normals.Add(drawInfo.averageNormal); cellInfo.Corners = caseCode; cellInfos[x, y, z] = cellInfo; } } } return(cellInfos); }
public static void GenIndices(CellInfo[,,] cellInfos, List <int> indices, List <Vector3> vertices, List <Vector3> normals) { int resolution = cellInfos.GetLength(0); for (int x = 0; x < resolution; x++) { for (int y = 0; y < resolution; y++) { for (int z = 0; z < resolution; z++) { if (x == resolution - 1 || y == resolution - 1 || z == resolution - 1) { continue; } CellInfo cellInfo = cellInfos[x, y, z]; if (cellInfo.Corners == 0) { continue; } byte caseCode = (byte)cellInfo.Corners; Vector3 p = new Vector3(x, y, z); Vector3Int[][] DCEdgeOffsets = { new Vector3Int[] { new Vector3Int(0, 0, 1), new Vector3Int(0, 1, 0), new Vector3Int(0, 1, 1) }, new Vector3Int[] { new Vector3Int(0, 0, 1), new Vector3Int(1, 0, 0), new Vector3Int(1, 0, 1) }, new Vector3Int[] { new Vector3Int(0, 1, 0), new Vector3Int(1, 0, 0), new Vector3Int(1, 1, 0) } }; Vector3Int[] Maxs = { new Vector3Int(0, 1, 1), new Vector3Int(1, 0, 1), new Vector3Int(1, 1, 0) }; CellInfo[] infos = new CellInfo[4]; infos[0] = cellInfo; //int v0 = drawInfo.index; for (int edgeNum = 0; edgeNum < 3; edgeNum++) { Vector3 max = Maxs[edgeNum]; Vector3Int[] ofs = DCEdgeOffsets[edgeNum]; if (p.x + max.x >= resolution || p.y + max.y >= resolution || p.z + max.z >= resolution) { continue; } int ei0 = FarEdges[edgeNum, 0]; int ei1 = FarEdges[edgeNum, 1]; bool edge1 = (caseCode & (1 << ei0)) == (1 << ei0); bool edge2 = (caseCode & (1 << ei1)) == (1 << ei1); if (edge1 == edge2) { continue; } for (int v = 0; v < 3; v++) { infos[v + 1] = cellInfos[(int)p.x + ofs[v].x, (int)p.y + ofs[v].y, (int)p.z + ofs[v].z]; } CellInfo[] i1 = { infos[0], infos[1], infos[3] }; CellInfo[] i2 = { infos[0], infos[3], infos[2] }; if (((caseCode >> ei0) & 1) == 1 != (edgeNum == 1)) { i1[0] = infos[1]; i1[1] = infos[0]; i2[0] = infos[3]; i2[1] = infos[0]; } for (int i = 0; i < 3; i++) { indices.Add(vertices.Count); vertices.Add(i1[i].Position); normals.Add(i1[i].Normal); } for (int i = 0; i < 3; i++) { indices.Add(vertices.Count); vertices.Add(i2[i].Position); normals.Add(i2[i].Normal); } } } } } }