voxel/region.js

137 lines
3.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-10 11:27:44 -04:00
Surface(inVoxels)
2021-07-05 20:51:08 -04:00
{
2021-07-10 11:27:44 -04:00
function Vert(a)
2021-07-07 15:12:39 -04:00
{
2021-07-10 11:27:44 -04:00
var vertPointer = voxPointVert[a]
if(vertPointer === undefined)
{
let vert = Region.XYZ(a);
vertPointer = vertices.length;
voxPointVert[a] = vertPointer;
vertPointNeighbors.push([]);
vertices.push(vert);
}
return vertPointer;
2021-07-07 15:12:39 -04:00
}
2021-07-10 11:27:44 -04:00
function Face(a, b, c, d)
2021-07-07 15:12:39 -04:00
{
2021-07-10 11:27:44 -04:00
let p1 = Vert(a);
let p2 = Vert(b);
let p3 = Vert(c);
let p4 = Vert(d);
triPointVert.push(p1, p2, p3, p3, p4, p1);
2021-07-07 15:12:39 -04:00
2021-07-10 11:27:44 -04:00
vertPointNeighbors[p1].push(p2);
vertPointNeighbors[p2].push(p1);
vertPointNeighbors[p2].push(p3);
vertPointNeighbors[p3].push(p2);
vertPointNeighbors[p3].push(p4);
vertPointNeighbors[p4].push(p3);
vertPointNeighbors[p4].push(p1);
vertPointNeighbors[p1].push(p2);
2021-07-07 15:12:39 -04:00
}
2021-07-10 11:27:44 -04:00
var vertices = [];
var voxPointVert = [];
var triPointVert = [];
var vertPointNeighbors = [];
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-10 11:27:44 -04:00
let [x, y, z] = Region.XYZ(i);
2021-07-07 15:12:39 -04:00
// 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-10 11:27:44 -04:00
2021-07-11 09:24:39 -04:00
return {vert:new Float32Array(vertices.flat()), voxPointVert, triPointVert, vertPointNeighbors};
2021-07-05 20:51:08 -04:00
}
};