click-create voxels

This commit is contained in:
TreetopFlyer 2021-07-15 14:41:17 -04:00
parent b630e26cfd
commit 45a482e5e1
2 changed files with 88 additions and 22 deletions

View File

@ -125,29 +125,33 @@ let surfaceTest = () =>
*/
let surface = Region.Surface(voxels);
let vertsCube = new Float32Array(surface.vertices.flat());
/*
let testGeometry = new THREE.BufferGeometry();
let testAttribute = new THREE.BufferAttribute(new Float32Array(0));
testGeometry.setAttribute("position", testAttribute, 3);
console.log(testGeometry);
testAttribute.array = new Float32Array([1, 2, 3]);
testAttribute.needsUpdate = true;
console.log(testGeometry);
*/
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:0x666666, wireframe:true, side:THREE.DoubleSide})
);
let vertsSmooth = new Float32Array(smooth(surface.vertices.flat(), surface.vertPointNeighbors, 10));
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.MeshBasicMaterial({color:0xffffff, wireframe:true, side:THREE.DoubleSide, flatShading:true})
new THREE.MeshPhongMaterial({color:0x666666, wireframe:false, flatShading:true, side:THREE.DoubleSide})
);
let rebuildSurface = inVoxels =>
{
let surface = Region.Surface(inVoxels);
let vertsCube = new Float32Array(surface.vertices.flat());
surfaceCubeGeometry.setAttribute("position", new THREE.BufferAttribute(vertsCube, 3));
surfaceCubeGeometry.setIndex(surface.triPointVert);
surfaceCubeGeometry.computeVertexNormals(true);
};
rebuildSurface(voxels);
return {
DOM:"canvas",
@ -157,12 +161,34 @@ let surfaceTest = () =>
light.position.set(5, 5, -5);
inScene.add(light)
inScene.add(surfaceCubeObject);
inScene.add(surfaceSmoothObject);
//inScene.add(surfaceSmoothObject);
inScene.add(new THREE.GridHelper(10, 10));
inScene.add(new THREE.AxesHelper( 5 ));
},
Update(inScene, inDelta){}
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(hit);
}
}
}
};
}

View File

@ -11,13 +11,53 @@ export function Scene(inSettings)
const controls = new OrbitControls(camera, renderer.domElement);
let dirty = false;
const Screen = {
Dirty:false,
Drag:0,
Down:false,
Ray:new T.Raycaster(),
Mouse:new T.Vector2(),
Camera:camera,
Event:null,
Update:e=>
{
Screen.Event = e;
Screen.Dirty = true;
Screen.Mouse.x = ( e.clientX / renderer.domElement.clientWidth ) * 2 - 1;
Screen.Mouse.y = - ( e.clientY / renderer.domElement.clientHeight ) * 2 + 1;
Screen.Ray.setFromCamera( Screen.Mouse, Screen.Camera );
},
HandleMove:e=>
{
Screen.Drag = Screen.Down ? 2 : 0;
Screen.Update(e);
},
HandlePress:e=>
{
Screen.Down = true;
Screen.Drag = 1;
Screen.Update(e);
},
HandleRelease:e=>
{
Screen.Down = false;
Screen.Drag = 0;
Screen.Update(e);
}
};
renderer.domElement.addEventListener("pointermove", Screen.HandleMove);
renderer.domElement.addEventListener("pointerdown", Screen.HandlePress);
renderer.domElement.addEventListener("pointerup", Screen.HandleRelease);
const update = inTime =>
{
let timeNew = new Date();
let timeDelta = timeNew - inTime;
let delta = timeDelta * 0.001;
inSettings.Update(scene, delta);
inSettings.Update(scene, Screen, delta);
Screen.Dirty = false;
controls.update();
dirty = true;