voxel/region.js
2021-07-05 20:51:08 -04:00

77 lines
1.7 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 {filled:[]};
},
I(inX, inY, inZ)
{
return inX + inY*32 + inZ*1024;
},
XYZ(inI)
{
let zWhole = Math.floor(inI / 1024);
let zPart = inI % 1024;
let yWhole = Math.floor(zPart / 32);
let yPart = zPart % 32;
return [yPart, yWhole, zWhole];
},
Surface(inRegion)
{
let surface = [];
inRegion.filled.forEach(inIndex =>
{
let coords = Region.XYZ(inIndex);
Kernel.Loop(Kernel.Corners, coords, (inX, inY, inZ)=>
{
let index = Region.I(inX, inY, inZ);
if(inRegion.filled[index] === undefined)
{
console.log(inX, inY, inZ, "surface");
surface.push(inIndex);
return true;
}
else
{
console.log(inX, inY, inZ, "filled");
}
});
});
return surface;
}
};