2022-04-10 12:42:24 -04:00
|
|
|
//@ ts-check
|
|
|
|
|
|
|
|
const Region =
|
2021-07-05 20:51:08 -04:00
|
|
|
{
|
2022-04-10 12:42:24 -04:00
|
|
|
/** @type {Region.CoordsToIndex} */
|
2021-07-07 15:12:39 -04:00
|
|
|
I(inX, inY, inZ)
|
2021-07-05 20:51:08 -04:00
|
|
|
{
|
2021-07-06 16:50:26 -04:00
|
|
|
return inX + inY*16 + inZ*256;
|
2021-07-05 20:51:08 -04:00
|
|
|
},
|
2022-04-10 12:42:24 -04:00
|
|
|
/** @type {Region.IndexToCoords} */
|
2021-07-07 15:12:39 -04:00
|
|
|
XYZ(inI)
|
2021-07-05 20:51:08 -04:00
|
|
|
{
|
2021-07-06 16:50:26 -04:00
|
|
|
let zWhole = Math.floor(inI / 256);
|
|
|
|
let zPart = inI % 256;
|
|
|
|
let yWhole = Math.floor(zPart / 16);
|
|
|
|
let yPart = zPart % 16;
|
2021-07-05 20:51:08 -04:00
|
|
|
return [yPart, yWhole, zWhole];
|
|
|
|
},
|
2022-04-10 12:42:24 -04:00
|
|
|
|
|
|
|
/** @type {Region.BufferBuilder} */
|
2021-07-10 11:27:44 -04:00
|
|
|
Surface(inVoxels)
|
2021-07-05 20:51:08 -04:00
|
|
|
{
|
2022-04-10 15:43:43 -04:00
|
|
|
var vertices = []; // output position buffer
|
|
|
|
var triPointVert = []; // output index buffer
|
2021-07-11 18:28:00 -04:00
|
|
|
|
2022-04-10 15:43:43 -04:00
|
|
|
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]} */
|
2021-07-11 18:28:00 -04:00
|
|
|
function Vert(inVoxIndex)
|
2021-07-07 15:12:39 -04:00
|
|
|
{
|
2021-07-11 18:28:00 -04:00
|
|
|
/*
|
|
|
|
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
|
|
|
|
*/
|
2022-04-10 15:43:43 -04:00
|
|
|
|
|
|
|
var vertPointer = voxPointVert[inVoxIndex]
|
|
|
|
if(vertPointer === undefined)// is there no vertex for this voxel?
|
2021-07-10 11:27:44 -04:00
|
|
|
{
|
|
|
|
vertPointer = vertices.length;
|
2021-07-11 18:28:00 -04:00
|
|
|
voxPointVert[inVoxIndex] = vertPointer;
|
2022-04-10 15:43:43 -04:00
|
|
|
|
2021-07-10 11:27:44 -04:00
|
|
|
vertPointNeighbors.push([]);
|
2022-04-10 15:43:43 -04:00
|
|
|
vertices.push(Region.XYZ(inVoxIndex));
|
2021-07-11 18:28:00 -04:00
|
|
|
|
|
|
|
return [vertPointer, false];
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return [vertPointer, true];
|
2021-07-10 11:27:44 -04:00
|
|
|
}
|
2021-07-07 15:12:39 -04:00
|
|
|
}
|
2021-07-11 18:28:00 -04:00
|
|
|
function Edge(vert1, vert1WasFull, vert2, vert2WasFull)
|
2021-07-07 15:12:39 -04:00
|
|
|
{
|
2021-07-11 18:28:00 -04:00
|
|
|
/*
|
|
|
|
Sets up neighbor relationships between vert1 and vert2.
|
|
|
|
Contains a check for if they have already been connected.
|
2021-07-07 15:12:39 -04:00
|
|
|
|
2021-07-11 18:28:00 -04:00
|
|
|
Impure refs: vertPointNeighbors
|
|
|
|
*/
|
2021-07-10 11:27:44 -04:00
|
|
|
|
2021-07-11 18:28:00 -04:00
|
|
|
let p1Neighbors = vertPointNeighbors[vert1];
|
|
|
|
let p2Neighbors = vertPointNeighbors[vert2];
|
2021-07-10 11:27:44 -04:00
|
|
|
|
2021-07-11 18:28:00 -04:00
|
|
|
// 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;
|
|
|
|
}
|
|
|
|
}
|
2021-07-10 11:27:44 -04:00
|
|
|
|
2021-07-11 18:28:00 -04:00
|
|
|
p1Neighbors.push(vert2);
|
|
|
|
p2Neighbors.push(vert1);
|
2021-07-07 15:12:39 -04:00
|
|
|
}
|
2021-07-11 18:28:00 -04:00
|
|
|
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);
|
2021-07-07 15:12:39 -04:00
|
|
|
|
2021-07-11 18:28:00 -04:00
|
|
|
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);
|
|
|
|
}
|
2021-07-10 11:27:44 -04:00
|
|
|
|
2021-07-06 16:50:26 -04:00
|
|
|
var i;
|
2021-07-11 18:28:00 -04:00
|
|
|
var x, y, z;
|
2021-07-07 15:12:39 -04:00
|
|
|
var BA, BU, BC, BR, TA, TU, TC, TR, CL, CD, CB;
|
|
|
|
for(i=0; i<inVoxels.length; i++)
|
2021-07-05 20:51:08 -04:00
|
|
|
{
|
2021-07-07 15:12:39 -04:00
|
|
|
if(inVoxels[i])
|
2021-07-05 20:51:08 -04:00
|
|
|
{
|
2021-07-11 18:28:00 -04:00
|
|
|
[x, y, z] = Region.XYZ(i);
|
2021-07-11 10:00:38 -04:00
|
|
|
|
2021-07-07 15:12:39 -04:00
|
|
|
// bottom: at, "up", corner, right
|
|
|
|
BA = i;
|
2021-07-11 10:00:38 -04:00
|
|
|
BU = Region.I(x, y+1, z );
|
|
|
|
BC = Region.I(x+1, y+1, z );
|
|
|
|
BR = Region.I(x+1, y, z );
|
2021-07-07 15:12:39 -04:00
|
|
|
|
|
|
|
// top: at, "up" corner, right
|
|
|
|
TA = Region.I(x, y, z+1);
|
2021-07-11 10:00:38 -04:00
|
|
|
TU = Region.I(x, y+1, z+1);
|
2021-07-07 15:12:39 -04:00
|
|
|
TC = Region.I(x+1, y+1, z+1);
|
2021-07-11 10:00:38 -04:00
|
|
|
TR = Region.I(x+1, y, z+1);
|
2021-07-07 15:12:39 -04:00
|
|
|
|
|
|
|
// check: left, "down", below
|
2021-07-11 10:00:38 -04:00
|
|
|
CL = Region.I(x-1, y, z );
|
|
|
|
CD = Region.I(x, y-1, z );
|
2021-07-07 15:12:39 -04:00
|
|
|
CB = Region.I(x, y, z-1);
|
|
|
|
|
2022-04-10 15:55:39 -04:00
|
|
|
if(!inVoxels[BU]){ Face(BU, BC, TC, TU); } // XZ (away from origin)
|
|
|
|
if(!inVoxels[BR]){ Face(BC, BR, TR, TC); } // YZ (away from origin)
|
|
|
|
if(!inVoxels[TA]){ Face(TA, TU, TC, TR); } // XY (away from origin)
|
2021-07-07 15:12:39 -04:00
|
|
|
|
2022-04-10 15:55:39 -04:00
|
|
|
if(!inVoxels[CL]){ Face(BA, BU, TU, TA); } // YZ (touches the origin)
|
|
|
|
if(!inVoxels[CD]){ Face(BR, BA, TA, TR); } // XZ (touches the origin)
|
|
|
|
if(!inVoxels[CB]){ Face(BU, BA, BR, BC); } // XY (touches the origin)
|
2021-07-05 21:36:52 -04:00
|
|
|
}
|
2021-07-06 16:50:26 -04:00
|
|
|
}
|
2021-07-10 11:27:44 -04:00
|
|
|
|
2022-04-10 12:42:24 -04:00
|
|
|
return {
|
|
|
|
pos: new Float32Array(vertices.flat()),
|
|
|
|
ind: triPointVert
|
|
|
|
};
|
2021-07-05 20:51:08 -04:00
|
|
|
}
|
|
|
|
};
|
2022-04-10 12:42:24 -04:00
|
|
|
|
|
|
|
export default Region;
|