voxel/region.js

76 lines
1.7 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-05 21:36:52 -04:00
return {voxels:[]};
2021-07-05 20:51:08 -04:00
},
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 = [];
2021-07-05 21:36:52 -04:00
inRegion.voxels.forEach((inValue, inIndex) =>
2021-07-05 20:51:08 -04:00
{
2021-07-05 21:36:52 -04:00
if(inValue)
2021-07-05 20:51:08 -04:00
{
2021-07-05 21:36:52 -04:00
let coords = Region.XYZ(inIndex);
Kernel.Loop(Kernel.Corners, coords, (inX, inY, inZ)=>
2021-07-05 20:51:08 -04:00
{
2021-07-05 21:36:52 -04:00
let kernel = Region.I(inX, inY, inZ);
if(!inRegion.voxels[kernel])
{
console.log(coords, "surface");
surface.push(inIndex);
return true;
}
});
}
2021-07-05 20:51:08 -04:00
});
return surface;
}
};