export const Region = { I(inX, inY, inZ) { return inX + inY*16 + inZ*256; }, XYZ(inI) { let zWhole = Math.floor(inI / 256); let zPart = inI % 256; let yWhole = Math.floor(zPart / 16); let yPart = zPart % 16; return [yPart, yWhole, zWhole]; }, Surface(inVoxels) { var vertices = []; // array of surface vertices var voxPointVert = []; // vox array, contains integer pointers to vertices var triPointVert = []; // triangle array, contains integer pointers to vertices var vertPointNeighbors = []; // vertex array, contains arrays of integer pointers to vertices function Vert(inVoxIndex) { /* If not preexisting, create a vertex with the coordinates of the voxel at `voxPointVert[inVoxIndex]`. Add the new vertex to `vertices`. Initialize empty neighbor connections. Put the *index* of new vertex back in the voxel array at `inVoxIndex` Return the vertex *index*, plus a flag if a vertex was already there. Impure refs: voxPointVert, vertPointNeighbors, vertices */ var vertPointer = voxPointVert[inVoxIndex] if(vertPointer === undefined) { let vert = Region.XYZ(inVoxIndex); vertPointer = vertices.length; voxPointVert[inVoxIndex] = vertPointer; vertPointNeighbors.push([[], [], [], []]); vertices.push(vert); return [vertPointer, false]; } else { return [vertPointer, true]; } } function Edge(vert1, vert1WasFull, vert2, vert2WasFull, inOrientationGroup) { /* Sets up neighbor relationships between vert1 and vert2. Contains a check for if they have already been connected. Impure refs: vertPointNeighbors */ let p1Neighbors = vertPointNeighbors[vert1]; let p2Neighbors = vertPointNeighbors[vert2]; // if the two verts were already filled if((vert1WasFull && vert2WasFull)) { // it's possible that they are already connected if(p1Neighbors[0].indexOf(vert2) !== -1) { // quit if they are return; } } p1Neighbors[0].push(vert2); p2Neighbors[0].push(vert1); p1Neighbors[inOrientationGroup+1].push(vert2); p2Neighbors[inOrientationGroup+1].push(vert1); } function Face(a, b, c, d /* vox indices */, inOrientationGroup) { let [p1, p1WasFull] = Vert(a); let [p2, p2WasFull] = Vert(b); let [p3, p3WasFull] = Vert(c); let [p4, p4WasFull] = Vert(d); triPointVert.push(p1, p2, p3, p3, p4, p1); Edge(p1, p1WasFull, p2, p2WasFull, inOrientationGroup); Edge(p2, p2WasFull, p3, p3WasFull, inOrientationGroup); Edge(p3, p3WasFull, p4, p4WasFull, inOrientationGroup); Edge(p4, p4WasFull, p1, p1WasFull, inOrientationGroup); } var i; var x, y, z; var BA, BU, BC, BR, TA, TU, TC, TR, CL, CD, CB; for(i=0; i