voxel/region.js

97 lines
2.8 KiB
JavaScript
Raw Normal View History

2021-07-05 20:51:08 -04:00
export const Region =
{
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
2021-07-11 10:00:38 -04:00
console.log("at", x, y, z);
2021-07-07 15:12:39 -04:00
// bottom: at, "up", corner, right
BA = i;
2021-07-11 10:00:38 -04:00
BU = Region.I(x, y+1, z );
BC = Region.I(x+1, y+1, z );
BR = Region.I(x+1, y, z );
2021-07-07 15:12:39 -04:00
// top: at, "up" corner, right
TA = Region.I(x, y, z+1);
2021-07-11 10:00:38 -04:00
TU = Region.I(x, y+1, z+1);
2021-07-07 15:12:39 -04:00
TC = Region.I(x+1, y+1, z+1);
2021-07-11 10:00:38 -04:00
TR = Region.I(x+1, y, z+1);
2021-07-07 15:12:39 -04:00
// check: left, "down", below
2021-07-11 10:00:38 -04:00
CL = Region.I(x-1, y, z );
CD = Region.I(x, y-1, z );
2021-07-07 15:12:39 -04:00
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
}
};