surface smoothing started

This commit is contained in:
TreetopFlyer 2021-07-12 09:05:15 -04:00
parent f77b50cac9
commit 9dc5b911fa

View File

@ -25,46 +25,12 @@ import { Region } from "./region.js";
let surfaceTest = () => let surfaceTest = () =>
{ {
let voxels = []; let average = (inArray, inPointInd, inOthersIndArray) =>
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(new Float32Array(surface.vertices.flat()), 3) );
surfaceGeometry.setIndex(surface.triPointVert);
surfaceGeometry.computeVertexNormals(true);
let surfaceObject = new THREE.Mesh(
surfaceGeometry,
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){}
};
}
let smoothTest = () =>
{
let average = (inArray, inPointInd, ...inOthersInd) =>
{ {
let sum = [0, 0, 0]; let sum = [0, 0, 0];
for(let i=0; i<inOthersInd.length; i++) for(let i=0; i<inOthersIndArray.length; i++)
{ {
let otherInd = inOthersInd[i]*3; let otherInd = inOthersIndArray[i]*3;
sum[0] += inArray[otherInd+0]; sum[0] += inArray[otherInd+0];
sum[1] += inArray[otherInd+1]; sum[1] += inArray[otherInd+1];
sum[2] += inArray[otherInd+2]; sum[2] += inArray[otherInd+2];
@ -73,16 +39,17 @@ let smoothTest = () =>
let pointX = inPointInd*3 + 0; let pointX = inPointInd*3 + 0;
let pointY = inPointInd*3 + 1; let pointY = inPointInd*3 + 1;
let pointZ = inPointInd*3 + 2; let pointZ = inPointInd*3 + 2;
let reducer = inOthersInd.length; let reducer = inOthersIndArray.length;
let limit = (inCenter, inPoint) => let limit = (inCenter, inPoint) =>
{ {
var size = 0.49999;
var limit; var limit;
limit = inCenter-0.49; limit = inCenter - size;
if(inPoint <= limit) if(inPoint <= limit)
{ {
return limit; return limit;
} }
limit = inCenter + 0.49; limit = inCenter + size;
if(inPoint >= limit) if(inPoint >= limit)
{ {
return limit; return limit;
@ -94,80 +61,90 @@ let smoothTest = () =>
inArray[pointY] = limit(Math.round(inArray[pointY]), sum[1]/reducer); inArray[pointY] = limit(Math.round(inArray[pointY]), sum[1]/reducer);
inArray[pointZ] = limit(Math.round(inArray[pointZ]), sum[2]/reducer); inArray[pointZ] = limit(Math.round(inArray[pointZ]), sum[2]/reducer);
}; };
let smooth = (inArray, inTimes) => let smooth = (inVertsArray, inNeighborsArray, inTimes) =>
{ {
let count = inArray.length/3; let count = inVertsArray.length/3;
for(let t=0; t<inTimes; t++) for(let t=0; t<inTimes; t++)
{ {
for(let i=0; i<count; i++) for(let i=0; i<count; i++)
{ {
if(i==0) average(inVertsArray, i, inNeighborsArray[i]);
{
average(inArray, i, count-1, i+1);
}
else
{
if(i==count-1)
{
average(inArray, i, i-1, 0);
}
else
{
average(inArray, i, i-1, i+1);
} }
} }
return inVertsArray;
} }
}
}
let path = (inPoints, inSmoothing, inColor) =>
{
let clone = [...inPoints];
smooth(clone, inSmoothing);
const geometry = new THREE.BufferGeometry();
geometry.setAttribute('position', new THREE.Float32BufferAttribute( clone, 3 ));
const materialLine = new THREE.LineBasicMaterial( { color: inColor, linewidth: 3 } );
const materialPoint = new THREE.PointsMaterial( { color:inColor, size: 0.05 } );
const output = new THREE.Group();
output.add( new THREE.Line( geometry, materialLine ) ); let voxels = [];
output.add( new THREE.Points( geometry, materialPoint ) ); voxels[Region.I(0, 0, 0)] = true;
return output; voxels[Region.I(1, 0, 0)] = true;
} voxels[Region.I(2, 0, 0)] = true;
const points = voxels[Region.I(0, 1, 0)] = true;
[ voxels[Region.I(1, 1, 0)] = true;
4, 0, 0, voxels[Region.I(0, 2, 0)] = true;
3, 0, 0,
2, 0, 0, voxels[Region.I(0, 0, 1)] = true;
1, 0, 0, voxels[Region.I(1, 0, 1)] = true;
// voxels[Region.I(2, 0, 1)] = true;
0, 0, 0, voxels[Region.I(0, 1, 1)] = true;
0, 1, 0, voxels[Region.I(1, 1, 1)] = true;
1, 1, 0, voxels[Region.I(0, 2, 1)] = true;
2, 1, 0,
2, 2, 0, voxels[Region.I(0, 0, 2)] = true;
3, 2, 0, voxels[Region.I(1, 0, 2)] = true;
4, 2, 0, voxels[Region.I(2, 0, 2)] = true;
4, 3, 0, voxels[Region.I(0, 1, 2)] = true;
// voxels[Region.I(1, 1, 2)] = true;
5, 3, 0, voxels[Region.I(0, 2, 2)] = true;
5, 2, 0,
5, 1, 0, voxels[Region.I(0, 0, 3)] = true;
5, 0, 0, voxels[Region.I(1, 0, 3)] = true;
]; voxels[Region.I(2, 0, 3)] = true;
voxels[Region.I(0, 1, 3)] = true;
voxels[Region.I(1, 1, 3)] = true;
voxels[Region.I(0, 2, 3)] = true;
let surface = Region.Surface(voxels);
let vertsCube = new Float32Array(surface.vertices.flat());
let surfaceCubeGeometry = new THREE.BufferGeometry();
surfaceCubeGeometry.setAttribute('position', new THREE.BufferAttribute(vertsCube, 3) );
surfaceCubeGeometry.setIndex(surface.triPointVert);
surfaceCubeGeometry.computeVertexNormals(true);
let surfaceCubeObject = new THREE.Mesh(
surfaceCubeGeometry,
new THREE.MeshBasicMaterial({color:0xaa9900, wireframe:true, side:THREE.DoubleSide, shading:THREE.FlatShading})
);
let vertsSmooth = new Float32Array(smooth(surface.vertices.flat(), surface.vertPointNeighbors, 5));
let surfaceSmoothGeometry = new THREE.BufferGeometry();
surfaceSmoothGeometry.setAttribute('position', new THREE.BufferAttribute(vertsSmooth, 3) );
surfaceSmoothGeometry.setIndex(surface.triPointVert);
surfaceSmoothGeometry.computeVertexNormals(true);
let surfaceSmoothObject = new THREE.Mesh(
surfaceSmoothGeometry,
new THREE.MeshPhongMaterial({color:0xffffff, side:THREE.DoubleSide, shading:THREE.FlatShading})
);
return { return {
DOM:"canvas", DOM:"canvas",
Init(inScene) Init(inScene)
{ {
inScene.add(path(points, 0, 0xFF0000)); let light = new THREE.PointLight(0xffffff, 1, 100)
inScene.add(path(points, 10, 0x00ff00)); light.position.set(0, 2, -4);
inScene.add(light)
inScene.add(surfaceCubeObject);
inScene.add(surfaceSmoothObject);
//inScene.add(new THREE.GridHelper(10, 10));
//inScene.add(new THREE.AxesHelper( 5 ));
}, },
Update(scene, delta) Update(inScene, inDelta){}
{ };
} }
}
};
Scene(surfaceTest()); Scene(surfaceTest());