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 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();
surfaceGeometry.computeVertexNormals(true);
let surfaceObject = new THREE.Mesh(
surfaceGeometry,
new THREE.MeshBasicMaterial({color:0xaa9900, wireframe:true})
new THREE.MeshPhongMaterial({color:0xaa9900, side:THREE.DoubleSide, shading:THREE.FlatShading})
);
return {
DOM:"canvas",
Init(inScene)
{
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){}
};

136
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 =
{
Create()
{
return {
Voxels:[],
Filled:[],
Surface:[]
};
},
I(inX, inY, inZ)
{
return inX + inY*16 + inZ*256;
@ -56,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;
@ -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(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();