face and neighbor creation

This commit is contained in:
TreetopFlyer 2021-07-10 11:27:44 -04:00
parent 72e33a6256
commit 13b0384c54
2 changed files with 50 additions and 10 deletions

View File

@ -19,7 +19,16 @@
</head> </head>
<body> <body>
<canvas></canvas> <canvas></canvas>
<script type="module">
import { Region } from "./region.js";
let voxels = [];
voxels[Region.I(2, 2, 2)] = true;
voxels[Region.I(3, 2, 2)] = true;
console.log(Region.Surface(voxels));
</script>
<!--
<script type="module"> <script type="module">
import { Scene, THREE } from './scene.js'; import { Scene, THREE } from './scene.js';
@ -134,7 +143,7 @@
]; ];
/*
Scene( Scene(
{ {
DOM:"canvas", DOM:"canvas",
@ -149,6 +158,7 @@
} }
} }
); );
*/
</script> </script>

View File

@ -54,27 +54,55 @@ export const Region =
let yPart = zPart % 16; let yPart = zPart % 16;
return [yPart, yWhole, zWhole]; return [yPart, yWhole, zWhole];
}, },
Update(inVoxels) Surface(inVoxels)
{ {
function Vert(a)
{
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;
}
function Face(a, b, c, d) function Face(a, b, c, d)
{ {
Edge(a, b); let p1 = Vert(a);
Edge(b, c); let p2 = Vert(b);
Edge(c, d); let p3 = Vert(c);
Edge(d, a); let p4 = Vert(d);
}
function Edge(a, b)
{
triPointVert.push(p1, p2, p3, p3, p4, p1);
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);
} }
var vertices = [];
var voxPointVert = [];
var triPointVert = [];
var vertPointNeighbors = [];
var i; var i;
var BA, BU, BC, BR, TA, TU, TC, TR, CL, CD, CB; var BA, BU, BC, BR, TA, TU, TC, TR, CL, CD, CB;
for(i=0; i<inVoxels.length; i++) for(i=0; i<inVoxels.length; i++)
{ {
if(inVoxels[i]) if(inVoxels[i])
{ {
let [x, y, z] = Region.IndexToCoords(i); let [x, y, z] = Region.XYZ(i);
// bottom: at, "up", corner, right // bottom: at, "up", corner, right
BA = i; BA = i;
@ -102,5 +130,7 @@ export const Region =
if(!inVoxels[CB]){ Face(BA, BU, BC, BR); } if(!inVoxels[CB]){ Face(BA, BU, BC, BR); }
} }
} }
return {vertices, voxPointVert, triPointVert, vertPointNeighbors};
} }
}; };