voxel/region.js
2021-07-06 16:50:26 -04:00

82 lines
1.8 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:[]
};
},
CoordsToIndex(inX, inY, inZ)
{
return inX + inY*16 + inZ*256;
},
IndexToCoords(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(inRegion)
{
inRegion.Filled = [];
inRegion.Surface = [];
var i;
for(i=0; i<inRegion.Voxels.length; i++)
{
if(inRegion.Voxels[i])
{
let coords = Region.IndexToCoords(i);
Kernel.Loop(Kernel.Corners, coords, (inX, inY, inZ)=>
{
let offsetIndex = Region.CoordsToIndex(inX, inY, inZ);
if(!inRegion.Voxels[offsetIndex])
{
inRegion.Surface.push(i);
return true;
}
});
}
}
console.log("looped over", i);
}
};