From c3f29258bea5a6d116d0f6cd4442381d6f359f0f Mon Sep 17 00:00:00 2001 From: Seth Trowbridge Date: Sun, 10 Apr 2022 15:43:43 -0400 Subject: [PATCH] normals trying to figure out whats going on with the random normal directions. --- app.js | 13 +++++-------- region.js | 45 +++++++++++++++++++++++++++++++-------------- 2 files changed, 36 insertions(+), 22 deletions(-) diff --git a/app.js b/app.js index 8e26df6..c7151ab 100644 --- a/app.js +++ b/app.js @@ -1,31 +1,29 @@ // @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; -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; let surfaceCubeGeometry = new T.BufferGeometry(); 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: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); - surfaceCubeGeometry.computeVertexNormals(true); + //surfaceCubeGeometry.computeVertexNormals(); }; rebuildSurface(voxels); @@ -38,7 +36,6 @@ Scene({ 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 )); }, diff --git a/region.js b/region.js index 2fcdd60..8e451ff 100644 --- a/region.js +++ b/region.js @@ -20,10 +20,27 @@ const Region = /** @type {Region.BufferBuilder} */ Surface(inVoxels) { - var vertices = []; // array of surface vertices - var triPointVert = []; // triangle array, contains integer pointers to vertices - var vertPointNeighbors = []; // vertex array, contains arrays of integer pointers to vertices + var vertices = []; // output position buffer + var triPointVert = []; // output index buffer + var vertPointNeighbors = []; // vertex array, contains arrays of integer pointers to vertices + var voxPointVert = []; // for each inVoxel; integer pointer to vertices output buffer + + /* + + inVoxels + | + [1 to 1] + | + voxPointVert---[optional pointers to]---> vertices <---- triPointVert + | + [1 to 1] + | + vertPointNeighbors + + */ + + /** @type {(voxIndex:number)=>[number, boolean]} */ function Vert(inVoxIndex) { /* @@ -35,15 +52,15 @@ const Region = Impure refs: voxPointVert, vertPointNeighbors, vertices */ - var voxPointVert = []; // vox array, contains integer pointers to vertices - var vertPointer = voxPointVert[inVoxIndex] - if(vertPointer === undefined) + + var vertPointer = voxPointVert[inVoxIndex] + if(vertPointer === undefined)// is there no vertex for this voxel? { - let vert = Region.XYZ(inVoxIndex); vertPointer = vertices.length; voxPointVert[inVoxIndex] = vertPointer; + vertPointNeighbors.push([]); - vertices.push(vert); + vertices.push(Region.XYZ(inVoxIndex)); return [vertPointer, false]; } @@ -119,13 +136,13 @@ const Region = CD = Region.I(x, y-1, z ); CB = Region.I(x, y, z-1); - if(!inVoxels[BU]){ Face(BU, BC, TC, TU); } - if(!inVoxels[BR]){ Face(BC, BR, TR, TC); } - if(!inVoxels[TA]){ Face(TA, TU, TC, TR); } + if(!inVoxels[BU]){ Face(BU, BC, TC, TU); } // XZ (away from origin) | "top" (these labels assume y-up) + if(!inVoxels[BR]){ Face(BC, BR, TR, TC); } // YZ (away from origin) | "side" + if(!inVoxels[TA]){ Face(TA, TU, TC, TR); } // XY (away from origin) | "side" - if(!inVoxels[CL]){ Face(BA, BU, TU, TA); } - if(!inVoxels[CD]){ Face(BR, BA, TA, TR); } - if(!inVoxels[CB]){ Face(BA, BU, BC, BR); } + if(!inVoxels[CL]){ Face(BA, BU, TU, TA); } // YZ (touches the origin) | "side" + if(!inVoxels[CD]){ Face(BR, BA, TA, TR); } // XZ (touches the origin) | "bottom" + if(!inVoxels[CB]){ Face(BA, BU, BC, BR); } // XY (touches the origin) | "side" <-- this one needs normals flipped } }