Compare commits

..

No commits in common. "f77b50cac98b6e9f77fcd1ffaebaf2e8ce75ddd3" and "4078e026efcbc02085d520ca95340b4c8647f0c7" have entirely different histories.

3 changed files with 79 additions and 73 deletions

View File

@ -26,31 +26,27 @@ import { Region } from "./region.js";
let surfaceTest = () =>
{
let voxels = [];
voxels[Region.I(0, 0, 0)] = true;
voxels[Region.I(1, 0, 0)] = true;
voxels[Region.I(2, 2, 2)] = true;
voxels[Region.I(2, 3, 2)] = true;
let surface = Region.Surface(voxels)
console.log(surface);
let surfaceGeometry = new THREE.BufferGeometry();
surfaceGeometry.setAttribute('position', new THREE.BufferAttribute(new Float32Array(surface.vertices.flat()), 3) );
surfaceGeometry.setAttribute('position', new THREE.BufferAttribute(surface.vert, 3) );
surfaceGeometry.setIndex(surface.triPointVert);
surfaceGeometry.computeVertexNormals(true);
surfaceGeometry.computeVertexNormals();
let surfaceObject = new THREE.Mesh(
surfaceGeometry,
new THREE.MeshPhongMaterial({color:0xaa9900, side:THREE.DoubleSide, shading:THREE.FlatShading})
new THREE.MeshBasicMaterial({color:0xaa9900, wireframe:true})
);
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){}
};

134
region.js
View File

@ -1,5 +1,47 @@
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;
@ -14,87 +56,55 @@ export const Region =
},
Surface(inVoxels)
{
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)
function Vert(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]
var vertPointer = voxPointVert[a]
if(vertPointer === undefined)
{
let vert = Region.XYZ(inVoxIndex);
let vert = Region.XYZ(a);
vertPointer = vertices.length;
voxPointVert[inVoxIndex] = vertPointer;
voxPointVert[a] = vertPointer;
vertPointNeighbors.push([]);
vertices.push(vert);
return [vertPointer, false];
}
else
{
return [vertPointer, true];
}
return vertPointer;
}
function Edge(vert1, vert1WasFull, vert2, vert2WasFull)
function Face(a, b, c, 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);
let p1 = Vert(a);
let p2 = Vert(b);
let p3 = Vert(c);
let p4 = Vert(d);
triPointVert.push(p1, p2, p3, p3, p4, p1);
Edge(p1, p1WasFull, p2, p2WasFull);
Edge(p2, p2WasFull, p3, p3WasFull);
Edge(p3, p3WasFull, p4, p4WasFull);
Edge(p4, p4WasFull, p1, p1WasFull);
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 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])
{
[x, y, z] = Region.XYZ(i);
let [x, y, z] = Region.XYZ(i);
console.log("at", x, y, z);
// bottom: at, "up", corner, right
BA = i;
@ -123,6 +133,6 @@ export const Region =
}
}
return {vertices, triPointVert, vertPointNeighbors};
return {vert:new Float32Array(vertices.flat()), voxPointVert, 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(-1, 2, -1);
camera.position.set(0, 5, 5);
camera.lookAt(new T.Vector3(0,0,0));
controls.update();