voxel/region.js

107 lines
2.5 KiB
JavaScript
Raw Normal View History

2021-07-05 20:51:08 -04:00
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()
{
2021-07-06 16:50:26 -04:00
return {
Voxels:[],
Filled:[],
Surface:[]
};
2021-07-05 20:51:08 -04:00
},
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
},
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];
},
2021-07-07 15:12:39 -04:00
Update(inVoxels)
2021-07-05 20:51:08 -04:00
{
2021-07-07 15:12:39 -04:00
function Face(a, b, c, d)
{
Edge(a, b);
Edge(b, c);
Edge(c, d);
Edge(d, a);
}
function Edge(a, b)
{
}
2021-07-06 16:50:26 -04:00
var i;
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-07 15:12:39 -04:00
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); }
2021-07-05 21:36:52 -04:00
}
2021-07-06 16:50:26 -04:00
}
2021-07-05 20:51:08 -04:00
}
};