voxel/index.html

181 lines
5.1 KiB
HTML
Raw Normal View History

2021-07-05 20:51:08 -04:00
<!doctype html>
<html>
<head>
<style>
html, body
{
margin:0;
}
canvas
{
position:absolute;
top:0;
left:0;
display:block;
width:100%;
height:100%;
}
</style>
</head>
<body>
<canvas></canvas>
2021-07-10 11:27:44 -04:00
<script type="module">
2021-07-11 09:24:39 -04:00
import { Scene, THREE } from './scene.js';
2021-07-10 11:27:44 -04:00
import { Region } from "./region.js";
2021-07-11 09:24:39 -04:00
let surfaceTest = () =>
{
2021-07-12 10:40:31 -04:00
let limit = (x, y, z, ax, ay, az) =>
{
let centerX = Math.round(x);
let centerY = Math.round(y);
let centerZ = Math.round(z);
let deltaX = ax-centerX;
let deltaY = ay-centerY;
let deltaZ = az-centerZ;
let absDeltaX = Math.abs(deltaX);
let absDeltaY = Math.abs(deltaY);
let absDeltaZ = Math.abs(deltaZ);
let max = absDeltaX;
if(absDeltaY > max){ max = absDeltaY; }
if(absDeltaZ > max){ max = absDeltaZ; }
2021-07-12 17:41:40 -04:00
let lerp = 0.45/max;
2021-07-12 10:40:31 -04:00
if(lerp <= 1)
{
return [
centerX + deltaX*lerp,
centerY + deltaY*lerp,
centerZ + deltaZ*lerp
];
}
else
{
return [ax, ay, az];
}
}
2021-07-12 17:41:40 -04:00
let average = (inArray, inPointInd, inOthersIndArray, inOriginals) =>
2021-07-05 20:51:08 -04:00
{
2021-07-07 15:12:39 -04:00
let sum = [0, 0, 0];
2021-07-12 09:05:15 -04:00
for(let i=0; i<inOthersIndArray.length; i++)
2021-07-07 07:00:50 -04:00
{
2021-07-12 09:05:15 -04:00
let otherInd = inOthersIndArray[i]*3;
2021-07-07 15:12:39 -04:00
sum[0] += inArray[otherInd+0];
sum[1] += inArray[otherInd+1];
sum[2] += inArray[otherInd+2];
2021-07-07 07:00:50 -04:00
}
2021-07-07 15:12:39 -04:00
let pointX = inPointInd*3 + 0;
let pointY = inPointInd*3 + 1;
let pointZ = inPointInd*3 + 2;
2021-07-12 09:05:15 -04:00
let reducer = inOthersIndArray.length;
2021-07-07 12:09:52 -04:00
2021-07-12 17:41:40 -04:00
let limited = limit(inOriginals[pointX], inOriginals[pointY], inOriginals[pointZ], sum[0]/reducer, sum[1]/reducer, sum[2]/reducer);
2021-07-12 10:40:31 -04:00
inArray[pointX] = limited[0];
inArray[pointY] = limited[1];
inArray[pointZ] = limited[2];
2021-07-07 07:00:50 -04:00
};
2021-07-12 09:05:15 -04:00
let smooth = (inVertsArray, inNeighborsArray, inTimes) =>
2021-07-07 07:26:21 -04:00
{
2021-07-12 09:05:15 -04:00
let count = inVertsArray.length/3;
2021-07-12 17:41:40 -04:00
let originals = [...inVertsArray];
2021-07-07 12:09:52 -04:00
for(let t=0; t<inTimes; t++)
2021-07-07 07:26:21 -04:00
{
2021-07-07 12:09:52 -04:00
for(let i=0; i<count; i++)
{
2021-07-12 17:41:40 -04:00
average(inVertsArray, i, inNeighborsArray[i][0], originals);
2021-07-07 12:09:52 -04:00
}
2021-07-07 07:26:21 -04:00
}
2021-07-12 09:05:15 -04:00
return inVertsArray;
2021-07-07 07:26:21 -04:00
}
2021-07-12 09:05:15 -04:00
let voxels = [];
voxels[Region.I(0, 0, 0)] = true;
voxels[Region.I(1, 0, 0)] = true;
2021-07-12 17:41:40 -04:00
/*
2021-07-12 09:05:15 -04:00
voxels[Region.I(2, 0, 0)] = true;
voxels[Region.I(0, 1, 0)] = true;
voxels[Region.I(1, 1, 0)] = true;
voxels[Region.I(0, 2, 0)] = true;
2021-07-12 17:41:40 -04:00
*/
2021-07-12 09:05:15 -04:00
2021-07-12 10:40:31 -04:00
/*
2021-07-12 09:05:15 -04:00
voxels[Region.I(0, 0, 1)] = true;
voxels[Region.I(1, 0, 1)] = true;
voxels[Region.I(2, 0, 1)] = true;
voxels[Region.I(0, 1, 1)] = true;
voxels[Region.I(1, 1, 1)] = true;
voxels[Region.I(0, 2, 1)] = true;
voxels[Region.I(0, 0, 2)] = true;
voxels[Region.I(1, 0, 2)] = true;
voxels[Region.I(2, 0, 2)] = true;
voxels[Region.I(0, 1, 2)] = true;
voxels[Region.I(1, 1, 2)] = true;
voxels[Region.I(0, 2, 2)] = true;
voxels[Region.I(0, 0, 3)] = true;
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;
2021-07-12 17:41:40 -04:00
voxels[Region.I(2, 1, 3)] = true;
2021-07-12 09:05:15 -04:00
voxels[Region.I(0, 2, 3)] = true;
2021-07-12 17:41:40 -04:00
voxels[Region.I(1, 2, 3)] = true;
voxels[Region.I(2, 2, 3)] = true;
*/
2021-07-12 09:05:15 -04:00
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,
2021-07-12 10:40:31 -04:00
new THREE.MeshBasicMaterial({color:0x666666, wireframe:true, side:THREE.DoubleSide})
2021-07-12 09:05:15 -04:00
);
2021-07-12 17:41:40 -04:00
let vertsSmooth = new Float32Array(smooth(surface.vertices.flat(), surface.vertPointNeighbors, 5));
2021-07-12 09:05:15 -04:00
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,
2021-07-12 17:41:40 -04:00
new THREE.MeshPhongMaterial({color:0xffffff, side:THREE.DoubleSide, flatShading:true})
2021-07-12 09:05:15 -04:00
);
2021-07-07 07:26:21 -04:00
2021-07-11 09:24:39 -04:00
return {
2021-07-07 07:00:50 -04:00
DOM:"canvas",
Init(inScene)
2021-07-12 09:05:15 -04:00
{
let light = new THREE.PointLight(0xffffff, 1, 100)
2021-07-12 10:40:31 -04:00
light.position.set(5, 5, -5);
2021-07-12 09:05:15 -04:00
inScene.add(light)
inScene.add(surfaceCubeObject);
inScene.add(surfaceSmoothObject);
2021-07-12 10:40:31 -04:00
inScene.add(new THREE.GridHelper(10, 10));
inScene.add(new THREE.AxesHelper( 5 ));
2021-07-07 07:00:50 -04:00
},
2021-07-12 09:05:15 -04:00
Update(inScene, inDelta){}
};
2021-07-07 07:00:50 -04:00
2021-07-12 09:05:15 -04:00
}
2021-07-11 09:24:39 -04:00
Scene(surfaceTest());
2021-07-05 20:51:08 -04:00
</script>
2021-07-06 16:50:26 -04:00
2021-07-05 20:51:08 -04:00
</body>
</html>