only neighbors

This commit is contained in:
TreetopFlyer 2021-07-07 15:12:39 -04:00
parent 7611192683
commit 72e33a6256
2 changed files with 57 additions and 37 deletions

View File

@ -25,23 +25,19 @@
let average = (inArray, inPointInd, ...inOthersInd) => let average = (inArray, inPointInd, ...inOthersInd) =>
{ {
let pointX = inPointInd*3 + 0; let sum = [0, 0, 0];
let pointY = inPointInd*3 + 1;
let pointZ = inPointInd*3 + 2;
let centerX = Math.round(inArray[pointX]);
let centerY = Math.round(inArray[pointY]);
let centerZ = Math.round(inArray[pointZ]);
for(let i=0; i<inOthersInd.length; i++) for(let i=0; i<inOthersInd.length; i++)
{ {
let otherInd = inOthersInd[i]*3; let otherInd = inOthersInd[i]*3;
inArray[pointX] += inArray[otherInd+0]; sum[0] += inArray[otherInd+0];
inArray[pointY] += inArray[otherInd+1]; sum[1] += inArray[otherInd+1];
inArray[pointZ] += inArray[otherInd+2]; sum[2] += inArray[otherInd+2];
} }
let reducer = inOthersInd.length+1; let pointX = inPointInd*3 + 0;
let pointY = inPointInd*3 + 1;
let pointZ = inPointInd*3 + 2;
let reducer = inOthersInd.length;
let limit = (inCenter, inPoint) => let limit = (inCenter, inPoint) =>
{ {
var limit; var limit;
@ -58,10 +54,9 @@
return inPoint; return inPoint;
} }
inArray[pointX] = limit(Math.round(inArray[pointX]), sum[0]/reducer);
inArray[pointX] = limit(centerX, inArray[pointX]/reducer); inArray[pointY] = limit(Math.round(inArray[pointY]), sum[1]/reducer);
inArray[pointY] = limit(centerY, inArray[pointY]/reducer); inArray[pointZ] = limit(Math.round(inArray[pointZ]), sum[2]/reducer);
inArray[pointZ] = limit(centerZ, inArray[pointZ]/reducer);
}; };
let smooth = (inArray, inTimes) => let smooth = (inArray, inTimes) =>
{ {
@ -146,7 +141,7 @@
Init(inScene) Init(inScene)
{ {
inScene.add(path(points, 0, 0xFF0000)); inScene.add(path(points, 0, 0xFF0000));
inScene.add(path(points, 50, 0x00ff00)); inScene.add(path(points, 10, 0x00ff00));
}, },
Update(scene, delta) Update(scene, delta)
{ {

View File

@ -20,7 +20,6 @@ export const Kernel =
[ 0, 0, 1], [ 0, 0, 1],
[ 0, 0, -1] [ 0, 0, -1]
], ],
Loop(inList, inVector, inHandler) Loop(inList, inVector, inHandler)
{ {
for(let i=0; i<inList.length; i++) for(let i=0; i<inList.length; i++)
@ -43,11 +42,11 @@ export const Region =
Surface:[] Surface:[]
}; };
}, },
CoordsToIndex(inX, inY, inZ) I(inX, inY, inZ)
{ {
return inX + inY*16 + inZ*256; return inX + inY*16 + inZ*256;
}, },
IndexToCoords(inI) XYZ(inI)
{ {
let zWhole = Math.floor(inI / 256); let zWhole = Math.floor(inI / 256);
let zPart = inI % 256; let zPart = inI % 256;
@ -55,27 +54,53 @@ export const Region =
let yPart = zPart % 16; let yPart = zPart % 16;
return [yPart, yWhole, zWhole]; return [yPart, yWhole, zWhole];
}, },
Update(inRegion) Update(inVoxels)
{ {
inRegion.Filled = []; function Face(a, b, c, d)
inRegion.Surface = [];
var i;
for(i=0; i<inRegion.Voxels.length; i++)
{ {
if(inRegion.Voxels[i]) Edge(a, b);
Edge(b, c);
Edge(c, d);
Edge(d, a);
}
function Edge(a, b)
{
}
var i;
var BA, BU, BC, BR, TA, TU, TC, TR, CL, CD, CB;
for(i=0; i<inVoxels.length; i++)
{
if(inVoxels[i])
{ {
let coords = Region.IndexToCoords(i); let [x, y, z] = Region.IndexToCoords(i);
Kernel.Loop(Kernel.Corners, coords, (inX, inY, inZ)=>
{ // bottom: at, "up", corner, right
let offsetIndex = Region.CoordsToIndex(inX, inY, inZ); BA = i;
if(!inRegion.Voxels[offsetIndex]) BU = Region.I(x+1, y, z);
{ BC = Region.I(x+1, y+1, z);
inRegion.Surface.push(i); BR = Region.I(x, y+1, z);
return true;
} // top: at, "up" corner, right
}); TA = Region.I(x, y, z+1);
TU = Region.I(x+1, y, z+1);
TC = Region.I(x+1, y+1, z+1);
TR = Region.I(x, y+1, z+1);
// check: left, "down", below
CL = Region.I(x-1, y, z);
CD = Region.I(x, y-1, z);
CB = Region.I(x, y, z-1);
if(!inVoxels[BU]){ Face(BU, BC, TC, TU); }
if(!inVoxels[BR]){ Face(BC, BR, TR, TC); }
if(!inVoxels[TA]){ Face(TA, TU, TC, TR); }
if(!inVoxels[CL]){ Face(BA, BU, TU, TA); }
if(!inVoxels[CD]){ Face(BR, BA, TA, TR); }
if(!inVoxels[CB]){ Face(BA, BU, BC, BR); }
} }
} }
console.log("looped over", i);
} }
}; };