107 lines
2.5 KiB
JavaScript
107 lines
2.5 KiB
JavaScript
export const Kernel =
|
|
{
|
|
Corners:
|
|
[
|
|
[ 1, 1, -1],
|
|
[ 1, -1, -1],
|
|
[-1, 1, -1],
|
|
[-1, -1, -1],
|
|
[ 1, 1, 1],
|
|
[ 1, -1, 1],
|
|
[-1, 1, 1],
|
|
[-1, -1, 1]
|
|
],
|
|
Cardinal:
|
|
[
|
|
[ 1, 0, 0],
|
|
[ 0, 1, 0],
|
|
[-1, 0, 0],
|
|
[ 0, -1, 0],
|
|
[ 0, 0, 1],
|
|
[ 0, 0, -1]
|
|
],
|
|
Loop(inList, inVector, inHandler)
|
|
{
|
|
for(let i=0; i<inList.length; i++)
|
|
{
|
|
let delta = inList[i];
|
|
if(inHandler(inVector[0]+delta[0], inVector[1]+delta[1], inVector[2]+delta[2]))
|
|
{
|
|
return;
|
|
}
|
|
}
|
|
}
|
|
};
|
|
export const Region =
|
|
{
|
|
Create()
|
|
{
|
|
return {
|
|
Voxels:[],
|
|
Filled:[],
|
|
Surface:[]
|
|
};
|
|
},
|
|
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];
|
|
},
|
|
Update(inVoxels)
|
|
{
|
|
function Face(a, b, c, d)
|
|
{
|
|
Edge(a, b);
|
|
Edge(b, c);
|
|
Edge(c, d);
|
|
Edge(d, a);
|
|
}
|
|
function Edge(a, b)
|
|
{
|
|
|
|
}
|
|
|
|
var i;
|
|
var BA, BU, BC, BR, TA, TU, TC, TR, CL, CD, CB;
|
|
for(i=0; i<inVoxels.length; i++)
|
|
{
|
|
if(inVoxels[i])
|
|
{
|
|
let [x, y, z] = Region.IndexToCoords(i);
|
|
|
|
// bottom: at, "up", corner, right
|
|
BA = i;
|
|
BU = Region.I(x+1, y, z);
|
|
BC = Region.I(x+1, y+1, z);
|
|
BR = Region.I(x, y+1, z);
|
|
|
|
// top: at, "up" corner, right
|
|
TA = Region.I(x, y, z+1);
|
|
TU = Region.I(x+1, y, z+1);
|
|
TC = Region.I(x+1, y+1, z+1);
|
|
TR = Region.I(x, y+1, 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); }
|
|
}
|
|
}
|
|
}
|
|
};
|