From f77b50cac98b6e9f77fcd1ffaebaf2e8ce75ddd3 Mon Sep 17 00:00:00 2001 From: TreetopFlyer Date: Sun, 11 Jul 2021 18:28:00 -0400 Subject: [PATCH] fixed missing neighbor bug --- index.html | 11 ++++--- region.js | 92 ++++++++++++++++++++++++++++++++++++------------------ scene.js | 2 +- 3 files changed, 70 insertions(+), 35 deletions(-) diff --git a/index.html b/index.html index 28a637c..c214b72 100644 --- a/index.html +++ b/index.html @@ -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){} }; diff --git a/region.js b/region.js index 9a4f739..43f1812 100644 --- a/region.js +++ b/region.js @@ -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]; + } + else + { + return [vertPointer, true]; } - return vertPointer; } - function Face(a, b, c, d) + function Edge(vert1, vert1WasFull, vert2, vert2WasFull) { - let p1 = Vert(a); - let p2 = Vert(b); - let p3 = Vert(c); - let p4 = Vert(d); + /* + 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