fixed missing neighbor bug

This commit is contained in:
TreetopFlyer 2021-07-11 18:28:00 -04:00
parent 20e8d90ec3
commit f77b50cac9
3 changed files with 70 additions and 35 deletions

View File

@ -26,13 +26,13 @@ import { Region } from "./region.js";
let surfaceTest = () =>
{
let voxels = [];
voxels[Region.I(2, 2, 2)] = true;
voxels[Region.I(2, 3, 2)] = true;
voxels[Region.I(0, 0, 0)] = true;
voxels[Region.I(1, 0, 0)] = true;
let surface = Region.Surface(voxels)
console.log(surface);
let surfaceGeometry = new THREE.BufferGeometry();
surfaceGeometry.setAttribute('position', new THREE.BufferAttribute(surface.vert, 3) );
surfaceGeometry.setAttribute('position', new THREE.BufferAttribute(new Float32Array(surface.vertices.flat()), 3) );
surfaceGeometry.setIndex(surface.triPointVert);
surfaceGeometry.computeVertexNormals(true);
@ -45,9 +45,12 @@ let surfaceTest = () =>
DOM:"canvas",
Init(inScene)
{
inScene.add(new THREE.PointLight(0xffffff, 1, 100))
let light = new THREE.PointLight(0xffffff, 1, 100)
light.position.set(0, 2, -4);
inScene.add(light)
inScene.add(surfaceObject);
inScene.add(new THREE.GridHelper(10, 10));
inScene.add(new THREE.AxesHelper( 5 ));
},
Update(inScene, inDelta){}
};

View File

@ -14,55 +14,87 @@ export const Region =
},
Surface(inVoxels)
{
function Vert(a)
var vertices = []; // array of surface vertices
var voxPointVert = []; // vox array, contains integer pointers to vertices
var triPointVert = []; // triangle array, contains integer pointers to vertices
var vertPointNeighbors = []; // vertex array, contains arrays of integer pointers to vertices
function Vert(inVoxIndex)
{
var vertPointer = voxPointVert[a]
/*
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)
{
let vert = Region.XYZ(a);
let vert = Region.XYZ(inVoxIndex);
vertPointer = vertices.length;
voxPointVert[a] = vertPointer;
voxPointVert[inVoxIndex] = vertPointer;
vertPointNeighbors.push([]);
vertices.push(vert);
return [vertPointer, false];
}
return vertPointer;
}
function Face(a, b, c, d)
else
{
let p1 = Vert(a);
let p2 = Vert(b);
let p3 = Vert(c);
let p4 = Vert(d);
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);
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);
Edge(p1, p1WasFull, p2, p2WasFull);
Edge(p2, p2WasFull, p3, p3WasFull);
Edge(p3, p3WasFull, p4, p4WasFull);
Edge(p4, p4WasFull, p1, p1WasFull);
}
var vertices = [];
var voxPointVert = [];
var triPointVert = [];
var vertPointNeighbors = [];
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])
{
let [x, y, z] = Region.XYZ(i);
console.log("at", x, y, z);
[x, y, z] = Region.XYZ(i);
// bottom: at, "up", corner, right
BA = i;
@ -91,6 +123,6 @@ export const Region =
}
}
return {vert:new Float32Array(vertices.flat()), voxPointVert, triPointVert, vertPointNeighbors};
return {vertices, triPointVert, vertPointNeighbors};
}
};

View File

@ -45,7 +45,7 @@ export function Scene(inSettings)
scene.add(light);
//scene.add(new T.GridHelper(10, 10));
light.position.set(-1, 2, 4);
camera.position.set(0, 5, 5);
camera.position.set(-1, 2, -1);
camera.lookAt(new T.Vector3(0,0,0));
controls.update();