//@ ts-check const Region = { /** @type {Region.CoordsToIndex} */ I(inX, inY, inZ) { return inX + inY*16 + inZ*256; }, /** @type {Region.IndexToCoords} */ 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]; }, /** @type {Region.BufferBuilder} */ Surface(inVoxels) { var vertices = []; // output position buffer var triPointVert = []; // output index buffer var vertPointNeighbors = []; // vertex array, contains arrays of integer pointers to vertices var voxPointVert = []; // for each inVoxel; integer pointer to vertices output buffer /* inVoxels | [1 to 1] | voxPointVert---[optional pointers to]---> vertices <---- triPointVert | [1 to 1] | vertPointNeighbors */ /** @type {(voxIndex:number)=>[number, boolean]} */ 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)// is there no vertex for this voxel? { vertPointer = vertices.length; voxPointVert[inVoxIndex] = vertPointer; vertPointNeighbors.push([]); vertices.push(Region.XYZ(inVoxIndex)); return [vertPointer, false]; } else { return [vertPointer, true]; } } function Edge(vert1, vert1WasFull, vert2, vert2WasFull) { /* 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.indexOf(vert2) !== -1) { // quit if they are return; } } p1Neighbors.push(vert2); p2Neighbors.push(vert1); } function Face(a, b, c, d /* vox indices */) { 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); Edge(p2, p2WasFull, p3, p3WasFull); Edge(p3, p3WasFull, p4, p4WasFull); Edge(p4, p4WasFull, p1, p1WasFull); } var i; var x, y, z; var BA, BU, BC, BR, TA, TU, TC, TR, CL, CD, CB; for(i=0; i