From c101c2f2f887352316b07ffd057bd4cc0305710e Mon Sep 17 00:00:00 2001 From: Seth Trowbridge Date: Sun, 10 Apr 2022 18:22:48 -0400 Subject: [PATCH] rebuild everything works --- app.js | 23 +++++++++++++++++------ 1 file changed, 17 insertions(+), 6 deletions(-) diff --git a/app.js b/app.js index 828a556..526726c 100644 --- a/app.js +++ b/app.js @@ -7,26 +7,34 @@ import Region from "./region.js"; let voxels = []; voxels[Region.I(0, 0, 0)] = true; +voxels[Region.I(1, 0, 0)] = true; +voxels[Region.I(1, 1, 0)] = true; +voxels[Region.I(1, 2, 0)] = true; let surfaceCubeGeometry = new T.BufferGeometry(); +surfaceCubeGeometry.setAttribute("position", new T.BufferAttribute(new Float32Array(), 3)); let surfaceCubeObject = new T.Mesh( surfaceCubeGeometry, - new T.MeshPhongMaterial({color:0x666666, wireframe:false, flatShading:true, side:T.DoubleSide}) + new T.MeshPhongMaterial({color:0x666666, wireframe:false, flatShading:false, side:T.BackSide}) ); - let surfaceCubeNormalHelper; let rebuildSurface = inVoxels => { let { pos, ind } = Region.Surface(inVoxels); - surfaceCubeGeometry.setAttribute("position", new T.BufferAttribute(pos, 3)); - surfaceCubeGeometry.setIndex(ind); + let surfaceCubePositions = new T.BufferAttribute(pos, 3); + surfaceCubePositions.setUsage( T.DynamicDrawUsage ); + surfaceCubeGeometry.dispose(); + surfaceCubeGeometry = new T.BufferGeometry(); + surfaceCubeGeometry.setAttribute("position", surfaceCubePositions); + surfaceCubeGeometry.setIndex(ind); surfaceCubeGeometry.computeVertexNormals(); - if(surfaceCubeNormalHelper) surfaceCubeNormalHelper.update(); + surfaceCubeObject.geometry = surfaceCubeGeometry; + }; rebuildSurface(voxels); @@ -50,7 +58,7 @@ Scene({ inScene.add(surfaceCubeObject); - inScene.add(new T.GridHelper(10, 10)); + //inScene.add(new T.GridHelper(10, 10)); inScene.add(new T.AxesHelper( 5 )); inScene.add( hitHelper ); @@ -76,6 +84,9 @@ Scene({ index = Region.I(Math.floor(hit.point.x), Math.floor(hit.point.y), Math.floor(hit.point.z)); voxels[index] = true; rebuildSurface(voxels); + inScene.remove(surfaceCubeNormalHelper); + surfaceCubeNormalHelper = new VertexNormalsHelper( surfaceCubeObject, -0.2 ); + inScene.add(surfaceCubeNormalHelper); hitHelper.position.set(hit.point.x, hit.point.y, hit.point.z); console.log(index, hit); }