voxel/app.js

170 lines
4.7 KiB
JavaScript
Raw Permalink Normal View History

2022-04-10 12:42:24 -04:00
// @ts-check
import * as T from 'three';
2022-04-10 16:58:27 -04:00
import { VertexNormalsHelper } from 'three/examples/jsm/helpers/VertexNormalsHelper.js';
2022-04-10 12:42:24 -04:00
import { Scene } from './scene.js';
import Region from "./region.js";
let voxels = [];
voxels[Region.I(0, 0, 0)] = true;
2022-04-10 18:22:48 -04:00
voxels[Region.I(1, 0, 0)] = true;
voxels[Region.I(1, 1, 0)] = true;
voxels[Region.I(1, 2, 0)] = true;
2022-04-10 12:42:24 -04:00
let surfaceCubeGeometry = new T.BufferGeometry();
2022-04-10 18:22:48 -04:00
surfaceCubeGeometry.setAttribute("position", new T.BufferAttribute(new Float32Array(), 3));
2022-04-10 12:42:24 -04:00
let surfaceCubeObject = new T.Mesh(
surfaceCubeGeometry,
2022-04-10 18:22:48 -04:00
new T.MeshPhongMaterial({color:0x666666, wireframe:false, flatShading:false, side:T.BackSide})
2022-04-10 12:42:24 -04:00
);
2022-04-10 16:58:27 -04:00
let surfaceCubeNormalHelper;
2022-04-10 12:42:24 -04:00
let rebuildSurface = inVoxels =>
{
let { pos, ind } = Region.Surface(inVoxels);
2022-04-10 18:22:48 -04:00
let surfaceCubePositions = new T.BufferAttribute(pos, 3);
surfaceCubePositions.setUsage( T.DynamicDrawUsage );
2022-04-10 16:58:27 -04:00
2022-04-10 18:22:48 -04:00
surfaceCubeGeometry.dispose();
surfaceCubeGeometry = new T.BufferGeometry();
surfaceCubeGeometry.setAttribute("position", surfaceCubePositions);
surfaceCubeGeometry.setIndex(ind);
2022-04-10 16:58:27 -04:00
surfaceCubeGeometry.computeVertexNormals();
2022-04-10 18:22:48 -04:00
surfaceCubeObject.geometry = surfaceCubeGeometry;
2022-04-10 12:42:24 -04:00
};
rebuildSurface(voxels);
2022-04-10 16:58:27 -04:00
surfaceCubeNormalHelper = new VertexNormalsHelper( surfaceCubeObject, -0.2 );
const hitHelper = new T.Mesh
(
new T.BoxGeometry( 0.2, 0.2, 0.2 ),
new T.MeshBasicMaterial( {color: 0x00ff00} )
);
2022-04-10 12:42:24 -04:00
Scene({
DOM:"canvas",
Init(inScene)
{
let light = new T.PointLight(0xffffff, 1, 100)
light.position.set(5, 5, -5);
2022-04-10 16:58:27 -04:00
inScene.add(light);
2022-04-10 12:42:24 -04:00
inScene.add(surfaceCubeObject);
2022-04-10 16:58:27 -04:00
2022-04-10 18:22:48 -04:00
//inScene.add(new T.GridHelper(10, 10));
2022-04-10 12:42:24 -04:00
inScene.add(new T.AxesHelper( 5 ));
2022-04-10 16:58:27 -04:00
inScene.add( hitHelper );
inScene.add( surfaceCubeNormalHelper );
2022-04-10 12:42:24 -04:00
},
Update(inScene, inScreen, inDelta)
{
var i, hits, hit, index;
if(inScreen.Drag > 0)
{
hits = inScreen.Ray.intersectObject(surfaceCubeObject);
if(hits.length)
{
hit = hits[0];
for(i=1; i<hits.length; i++)
{
if(hits[i].distance < hit.distance)
{
hit = hits[i];
2022-04-10 16:58:27 -04:00
2022-04-10 12:42:24 -04:00
}
}
index = Region.I(Math.floor(hit.point.x), Math.floor(hit.point.y), Math.floor(hit.point.z));
voxels[index] = true;
rebuildSurface(voxels);
2022-04-10 18:22:48 -04:00
inScene.remove(surfaceCubeNormalHelper);
surfaceCubeNormalHelper = new VertexNormalsHelper( surfaceCubeObject, -0.2 );
inScene.add(surfaceCubeNormalHelper);
2022-04-10 16:58:27 -04:00
hitHelper.position.set(hit.point.x, hit.point.y, hit.point.z);
2022-04-10 12:42:24 -04:00
console.log(index, hit);
}
}
}
});
/* old smoothng functions
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; }
let lerp = 0.49/max;
if(lerp <= 1)
{
return [
centerX + deltaX*lerp,
centerY + deltaY*lerp,
centerZ + deltaZ*lerp
];
}
else
{
return [ax, ay, az];
}
}
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 limited = limit(inArray[pointX], inArray[pointY], inArray[pointZ], sum[0]/reducer, sum[1]/reducer, sum[2]/reducer);
inArray[pointX] = limited[0];
inArray[pointY] = limited[1];
inArray[pointZ] = limited[2];
};
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;
}
*/