129 lines
4.3 KiB
JavaScript
129 lines
4.3 KiB
JavaScript
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)
|
|
{
|
|
/*
|
|
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<inVoxels.length; i++)
|
|
{
|
|
if(inVoxels[i])
|
|
{
|
|
[x, y, z] = Region.XYZ(i);
|
|
|
|
// bottom: at, "up", corner, right
|
|
BA = i;
|
|
BU = Region.I(x, y+1, z );
|
|
BC = Region.I(x+1, y+1, z );
|
|
BR = Region.I(x+1, y, z );
|
|
|
|
// top: at, "up" corner, right
|
|
TA = Region.I(x, y, z+1);
|
|
TU = Region.I(x, y+1, z+1);
|
|
TC = Region.I(x+1, y+1, z+1);
|
|
TR = Region.I(x+1, y, z+1);
|
|
|
|
// check: left, "down", below
|
|
CL = Region.I(x-1, y, z );
|
|
CD = Region.I(x, y-1, z );
|
|
CB = Region.I(x, y, z-1);
|
|
|
|
if(!inVoxels[BU]){ Face(BU, BC, TC, TU); }
|
|
if(!inVoxels[BR]){ Face(BC, BR, TR, TC); }
|
|
if(!inVoxels[TA]){ Face(TA, TU, TC, TR); }
|
|
|
|
if(!inVoxels[CL]){ Face(BA, BU, TU, TA); }
|
|
if(!inVoxels[CD]){ Face(BR, BA, TA, TR); }
|
|
if(!inVoxels[CB]){ Face(BA, BU, BC, BR); }
|
|
}
|
|
}
|
|
|
|
return {vertices, triPointVert, vertPointNeighbors};
|
|
}
|
|
};
|