voxel/index.html

154 lines
4.5 KiB
HTML

<!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>
<script type="module">
import { Scene, THREE } from './scene.js';
import { Region } from "./region.js";
let surfaceTest = () =>
{
let average = (inArray, inPointInd, inOthersIndArray) =>
{
let sum = [0, 0, 0];
for(let i=0; i<inOthersIndArray.length; i++)
{
let otherInd = inOthersIndArray[i]*3;
sum[0] += inArray[otherInd+0];
sum[1] += inArray[otherInd+1];
sum[2] += inArray[otherInd+2];
}
let pointX = inPointInd*3 + 0;
let pointY = inPointInd*3 + 1;
let pointZ = inPointInd*3 + 2;
let reducer = inOthersIndArray.length;
let limit = (inCenter, inPoint) =>
{
var size = 0.49999;
var limit;
limit = inCenter - size;
if(inPoint <= limit)
{
return limit;
}
limit = inCenter + size;
if(inPoint >= limit)
{
return limit;
}
return inPoint;
}
inArray[pointX] = limit(Math.round(inArray[pointX]), sum[0]/reducer);
inArray[pointY] = limit(Math.round(inArray[pointY]), sum[1]/reducer);
inArray[pointZ] = limit(Math.round(inArray[pointZ]), sum[2]/reducer);
};
let smooth = (inVertsArray, inNeighborsArray, inTimes) =>
{
let count = inVertsArray.length/3;
for(let t=0; t<inTimes; t++)
{
for(let i=0; i<count; i++)
{
average(inVertsArray, i, inNeighborsArray[i]);
}
}
return inVertsArray;
}
let voxels = [];
voxels[Region.I(0, 0, 0)] = true;
voxels[Region.I(1, 0, 0)] = true;
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;
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;
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 {
DOM:"canvas",
Init(inScene)
{
let light = new THREE.PointLight(0xffffff, 1, 100)
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(inScene, inDelta){}
};
}
Scene(surfaceTest());
</script>
</body>
</html>