voxel/region.js

82 lines
1.8 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]
],
2021-07-06 16:50:26 -04:00
2021-07-05 20:51:08 -04:00
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-06 16:50:26 -04:00
CoordsToIndex(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-06 16:50:26 -04:00
IndexToCoords(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-06 16:50:26 -04:00
Update(inRegion)
2021-07-05 20:51:08 -04:00
{
2021-07-06 16:50:26 -04:00
inRegion.Filled = [];
inRegion.Surface = [];
var i;
for(i=0; i<inRegion.Voxels.length; i++)
2021-07-05 20:51:08 -04:00
{
2021-07-06 16:50:26 -04:00
if(inRegion.Voxels[i])
2021-07-05 20:51:08 -04:00
{
2021-07-06 16:50:26 -04:00
let coords = Region.IndexToCoords(i);
2021-07-05 21:36:52 -04:00
Kernel.Loop(Kernel.Corners, coords, (inX, inY, inZ)=>
2021-07-05 20:51:08 -04:00
{
2021-07-06 16:50:26 -04:00
let offsetIndex = Region.CoordsToIndex(inX, inY, inZ);
if(!inRegion.Voxels[offsetIndex])
2021-07-05 21:36:52 -04:00
{
2021-07-06 16:50:26 -04:00
inRegion.Surface.push(i);
2021-07-05 21:36:52 -04:00
return true;
}
});
}
2021-07-06 16:50:26 -04:00
}
console.log("looped over", i);
2021-07-05 20:51:08 -04:00
}
};