voxel/region.js
Seth Trowbridge c3f29258be normals
trying to figure out whats going on with the random normal directions.
2022-04-10 15:43:43 -04:00

156 lines
5.2 KiB
JavaScript

//@ ts-check
const Region =
{
/** @type {Region.CoordsToIndex} */
I(inX, inY, inZ)
{
return inX + inY*16 + inZ*256;
},
/** @type {Region.IndexToCoords} */
XYZ(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];
},
/** @type {Region.BufferBuilder} */
Surface(inVoxels)
{
var vertices = []; // output position buffer
var triPointVert = []; // output index buffer
var vertPointNeighbors = []; // vertex array, contains arrays of integer pointers to vertices
var voxPointVert = []; // for each inVoxel; integer pointer to vertices output buffer
/*
inVoxels
|
[1 to 1]
|
voxPointVert---[optional pointers to]---> vertices <---- triPointVert
|
[1 to 1]
|
vertPointNeighbors
*/
/** @type {(voxIndex:number)=>[number, boolean]} */
function Vert(inVoxIndex)
{
/*
If not preexisting, create a vertex with the coordinates of the voxel at `voxPointVert[inVoxIndex]`.
Add the new vertex to `vertices`.
Initialize empty neighbor connections.
Put the *index* of new vertex back in the voxel array at `inVoxIndex`
Return the vertex *index*, plus a flag if a vertex was already there.
Impure refs: voxPointVert, vertPointNeighbors, vertices
*/
var vertPointer = voxPointVert[inVoxIndex]
if(vertPointer === undefined)// is there no vertex for this voxel?
{
vertPointer = vertices.length;
voxPointVert[inVoxIndex] = vertPointer;
vertPointNeighbors.push([]);
vertices.push(Region.XYZ(inVoxIndex));
return [vertPointer, false];
}
else
{
return [vertPointer, true];
}
}
function Edge(vert1, vert1WasFull, vert2, vert2WasFull)
{
/*
Sets up neighbor relationships between vert1 and vert2.
Contains a check for if they have already been connected.
Impure refs: vertPointNeighbors
*/
let p1Neighbors = vertPointNeighbors[vert1];
let p2Neighbors = vertPointNeighbors[vert2];
// if the two verts were already filled
if((vert1WasFull && vert2WasFull))
{
// it's possible that they are already connected
if(p1Neighbors.indexOf(vert2) !== -1)
{
// quit if they are
return;
}
}
p1Neighbors.push(vert2);
p2Neighbors.push(vert1);
}
function Face(a, b, c, d /* vox indices */)
{
let [p1, p1WasFull] = Vert(a);
let [p2, p2WasFull] = Vert(b);
let [p3, p3WasFull] = Vert(c);
let [p4, p4WasFull] = Vert(d);
triPointVert.push(p1, p2, p3, p3, p4, p1);
Edge(p1, p1WasFull, p2, p2WasFull);
Edge(p2, p2WasFull, p3, p3WasFull);
Edge(p3, p3WasFull, p4, p4WasFull);
Edge(p4, p4WasFull, p1, p1WasFull);
}
var i;
var x, y, z;
var BA, BU, BC, BR, TA, TU, TC, TR, CL, CD, CB;
for(i=0; i<inVoxels.length; i++)
{
if(inVoxels[i])
{
[x, y, z] = Region.XYZ(i);
// bottom: at, "up", corner, right
BA = i;
BU = Region.I(x, y+1, z );
BC = Region.I(x+1, y+1, z );
BR = Region.I(x+1, y, z );
// top: at, "up" corner, right
TA = Region.I(x, y, z+1);
TU = Region.I(x, y+1, z+1);
TC = Region.I(x+1, y+1, z+1);
TR = Region.I(x+1, y, 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); } // XZ (away from origin) | "top" (these labels assume y-up)
if(!inVoxels[BR]){ Face(BC, BR, TR, TC); } // YZ (away from origin) | "side"
if(!inVoxels[TA]){ Face(TA, TU, TC, TR); } // XY (away from origin) | "side"
if(!inVoxels[CL]){ Face(BA, BU, TU, TA); } // YZ (touches the origin) | "side"
if(!inVoxels[CD]){ Face(BR, BA, TA, TR); } // XZ (touches the origin) | "bottom"
if(!inVoxels[CB]){ Face(BA, BU, BC, BR); } // XY (touches the origin) | "side" <-- this one needs normals flipped
}
}
return {
pos: new Float32Array(vertices.flat()),
ind: triPointVert
};
}
};
export default Region;