// @ts-check import * as T from 'three'; import { VertexNormalsHelper } from 'three/examples/jsm/helpers/VertexNormalsHelper.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(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:false, side:T.BackSide}) ); let surfaceCubeNormalHelper; let rebuildSurface = inVoxels => { let { pos, ind } = Region.Surface(inVoxels); 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(); surfaceCubeObject.geometry = surfaceCubeGeometry; }; rebuildSurface(voxels); 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} ) ); 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 )); inScene.add( hitHelper ); inScene.add( surfaceCubeNormalHelper ); }, 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 { 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 { let count = inVertsArray.length/3; for(let t=0; t