voxel/app.js
Seth Trowbridge d62a1e7357 fixed
they're now unified, but flipped :P
2022-04-10 15:55:39 -04:00

139 lines
3.7 KiB
JavaScript

// @ts-check
import * as T from 'three';
//import { FaceNormalsHelper } from 'three/examples/jsm/helpers/FaceNormalsHelper.js';
import { Scene } from './scene.js';
import Region from "./region.js";
let voxels = [];
voxels[Region.I(0, 0, 0)] = true;
let surfaceCubeGeometry = new T.BufferGeometry();
let surfaceCubeObject = new T.Mesh(
surfaceCubeGeometry,
new T.MeshPhongMaterial({color:0x666666, wireframe:false, flatShading:true, side:T.BackSide})
);
//var vnh = new FaceNormalsHelper( surfaceCubeObject, 1, 0xff0000 );
let rebuildSurface = inVoxels =>
{
let { pos, ind } = Region.Surface(inVoxels);
surfaceCubeGeometry.setAttribute("position", new T.BufferAttribute(pos, 3));
surfaceCubeGeometry.setIndex(ind);
};
rebuildSurface(voxels);
Scene({
DOM:"canvas",
Init(inScene)
{
let light = new T.PointLight(0xffffff, 1, 100)
light.position.set(5, 5, -5);
inScene.add(light)
inScene.add(surfaceCubeObject);
inScene.add(new T.GridHelper(10, 10));
inScene.add(new T.AxesHelper( 5 ));
},
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];
}
}
index = Region.I(Math.floor(hit.point.x), Math.floor(hit.point.y), Math.floor(hit.point.z));
voxels[index] = true;
rebuildSurface(voxels);
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;
}
*/