Compare commits

...

2 Commits

Author SHA1 Message Date
f77b50cac9 fixed missing neighbor bug 2021-07-11 18:28:00 -04:00
20e8d90ec3 add light to show back 2021-07-11 16:44:34 -04:00
3 changed files with 73 additions and 79 deletions

View File

@ -26,27 +26,31 @@ import { Region } from "./region.js";
let surfaceTest = () => let surfaceTest = () =>
{ {
let voxels = []; let voxels = [];
voxels[Region.I(2, 2, 2)] = true; voxels[Region.I(0, 0, 0)] = true;
voxels[Region.I(2, 3, 2)] = true; voxels[Region.I(1, 0, 0)] = true;
let surface = Region.Surface(voxels) let surface = Region.Surface(voxels)
console.log(surface); console.log(surface);
let surfaceGeometry = new THREE.BufferGeometry(); 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.setIndex(surface.triPointVert);
surfaceGeometry.computeVertexNormals(); surfaceGeometry.computeVertexNormals(true);
let surfaceObject = new THREE.Mesh( let surfaceObject = new THREE.Mesh(
surfaceGeometry, surfaceGeometry,
new THREE.MeshBasicMaterial({color:0xaa9900, wireframe:true}) new THREE.MeshPhongMaterial({color:0xaa9900, side:THREE.DoubleSide, shading:THREE.FlatShading})
); );
return { return {
DOM:"canvas", DOM:"canvas",
Init(inScene) Init(inScene)
{ {
let light = new THREE.PointLight(0xffffff, 1, 100)
light.position.set(0, 2, -4);
inScene.add(light)
inScene.add(surfaceObject); inScene.add(surfaceObject);
inScene.add(new THREE.GridHelper(10, 10)); inScene.add(new THREE.GridHelper(10, 10));
inScene.add(new THREE.AxesHelper( 5 ));
}, },
Update(inScene, inDelta){} Update(inScene, inDelta){}
}; };

134
region.js
View File

@ -1,47 +1,5 @@
export const Kernel =
{
Corners:
[
[ 1, 1, -1],
[ 1, -1, -1],
[-1, 1, -1],
[-1, -1, -1],
[ 1, 1, 1],
[ 1, -1, 1],
[-1, 1, 1],
[-1, -1, 1]
],
Cardinal:
[
[ 1, 0, 0],
[ 0, 1, 0],
[-1, 0, 0],
[ 0, -1, 0],
[ 0, 0, 1],
[ 0, 0, -1]
],
Loop(inList, inVector, inHandler)
{
for(let i=0; i<inList.length; i++)
{
let delta = inList[i];
if(inHandler(inVector[0]+delta[0], inVector[1]+delta[1], inVector[2]+delta[2]))
{
return;
}
}
}
};
export const Region = export const Region =
{ {
Create()
{
return {
Voxels:[],
Filled:[],
Surface:[]
};
},
I(inX, inY, inZ) I(inX, inY, inZ)
{ {
return inX + inY*16 + inZ*256; return inX + inY*16 + inZ*256;
@ -56,55 +14,87 @@ export const Region =
}, },
Surface(inVoxels) 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) if(vertPointer === undefined)
{ {
let vert = Region.XYZ(a); let vert = Region.XYZ(inVoxIndex);
vertPointer = vertices.length; vertPointer = vertices.length;
voxPointVert[a] = vertPointer; voxPointVert[inVoxIndex] = vertPointer;
vertPointNeighbors.push([]); vertPointNeighbors.push([]);
vertices.push(vert); 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); Sets up neighbor relationships between vert1 and vert2.
let p3 = Vert(c); Contains a check for if they have already been connected.
let p4 = Vert(d);
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); triPointVert.push(p1, p2, p3, p3, p4, p1);
vertPointNeighbors[p1].push(p2); Edge(p1, p1WasFull, p2, p2WasFull);
vertPointNeighbors[p2].push(p1); Edge(p2, p2WasFull, p3, p3WasFull);
Edge(p3, p3WasFull, p4, p4WasFull);
vertPointNeighbors[p2].push(p3); Edge(p4, p4WasFull, p1, p1WasFull);
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 x, y, z;
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.XYZ(i); [x, y, z] = Region.XYZ(i);
console.log("at", x, y, z);
// bottom: at, "up", corner, right // bottom: at, "up", corner, right
BA = i; BA = i;
@ -133,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(light);
//scene.add(new T.GridHelper(10, 10)); //scene.add(new T.GridHelper(10, 10));
light.position.set(-1, 2, 4); 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)); camera.lookAt(new T.Vector3(0,0,0));
controls.update(); controls.update();