start jsdoc, reorganize
This commit is contained in:
parent
45a482e5e1
commit
da9be04163
143
app.js
Normal file
143
app.js
Normal file
@ -0,0 +1,143 @@
|
|||||||
|
// @ts-check
|
||||||
|
|
||||||
|
import * as T from 'three';
|
||||||
|
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})
|
||||||
|
);
|
||||||
|
|
||||||
|
let rebuildSurface = inVoxels =>
|
||||||
|
{
|
||||||
|
let { pos, ind } = Region.Surface(inVoxels);
|
||||||
|
|
||||||
|
surfaceCubeGeometry.setAttribute("position", new T.BufferAttribute(pos, 3));
|
||||||
|
surfaceCubeGeometry.setIndex(ind);
|
||||||
|
surfaceCubeGeometry.computeVertexNormals(true);
|
||||||
|
};
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
*/
|
185
index.html
185
index.html
@ -19,183 +19,12 @@
|
|||||||
</head>
|
</head>
|
||||||
<body>
|
<body>
|
||||||
<canvas></canvas>
|
<canvas></canvas>
|
||||||
<script type="module">
|
<script type="importmap">
|
||||||
import { Scene, THREE } from './scene.js';
|
{"imports": {
|
||||||
import { Region } from "./region.js";
|
"three": "https://esm.sh/three",
|
||||||
|
"three/": "https://esm.sh/three/"
|
||||||
let surfaceTest = () =>
|
}}
|
||||||
{
|
</script>
|
||||||
let limit = (x, y, z, ax, ay, az) =>
|
<script type="module" src="app.js"></script>
|
||||||
{
|
|
||||||
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;
|
|
||||||
}
|
|
||||||
|
|
||||||
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;
|
|
||||||
|
|
||||||
/*
|
|
||||||
voxels[Region.I(0, 0, 1)] = true;
|
|
||||||
voxels[Region.I(1, 0, 1)] = true;
|
|
||||||
voxels[Region.I(2, 0, 1)] = true;
|
|
||||||
voxels[Region.I(0, 1, 1)] = true;
|
|
||||||
voxels[Region.I(1, 1, 1)] = true;
|
|
||||||
voxels[Region.I(0, 2, 1)] = true;
|
|
||||||
|
|
||||||
voxels[Region.I(0, 0, 2)] = true;
|
|
||||||
voxels[Region.I(1, 0, 2)] = true;
|
|
||||||
voxels[Region.I(2, 0, 2)] = true;
|
|
||||||
voxels[Region.I(0, 1, 2)] = true;
|
|
||||||
voxels[Region.I(1, 1, 2)] = true;
|
|
||||||
voxels[Region.I(0, 2, 2)] = true;
|
|
||||||
|
|
||||||
voxels[Region.I(0, 0, 3)] = true;
|
|
||||||
voxels[Region.I(1, 0, 3)] = true;
|
|
||||||
voxels[Region.I(2, 0, 3)] = true;
|
|
||||||
voxels[Region.I(0, 1, 3)] = true;
|
|
||||||
voxels[Region.I(1, 1, 3)] = true;
|
|
||||||
voxels[Region.I(0, 2, 3)] = true;
|
|
||||||
*/
|
|
||||||
|
|
||||||
|
|
||||||
/*
|
|
||||||
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();
|
|
||||||
let surfaceCubeObject = new THREE.Mesh(
|
|
||||||
surfaceCubeGeometry,
|
|
||||||
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",
|
|
||||||
Init(inScene)
|
|
||||||
{
|
|
||||||
let light = new THREE.PointLight(0xffffff, 1, 100)
|
|
||||||
light.position.set(5, 5, -5);
|
|
||||||
inScene.add(light)
|
|
||||||
inScene.add(surfaceCubeObject);
|
|
||||||
//inScene.add(surfaceSmoothObject);
|
|
||||||
|
|
||||||
inScene.add(new THREE.GridHelper(10, 10));
|
|
||||||
inScene.add(new THREE.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(hit);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
Scene(surfaceTest());
|
|
||||||
|
|
||||||
</script>
|
|
||||||
|
|
||||||
</body>
|
</body>
|
||||||
</html>
|
</html>
|
17
region.js
17
region.js
@ -1,9 +1,13 @@
|
|||||||
export const Region =
|
//@ ts-check
|
||||||
|
|
||||||
|
const Region =
|
||||||
{
|
{
|
||||||
|
/** @type {Region.CoordsToIndex} */
|
||||||
I(inX, inY, inZ)
|
I(inX, inY, inZ)
|
||||||
{
|
{
|
||||||
return inX + inY*16 + inZ*256;
|
return inX + inY*16 + inZ*256;
|
||||||
},
|
},
|
||||||
|
/** @type {Region.IndexToCoords} */
|
||||||
XYZ(inI)
|
XYZ(inI)
|
||||||
{
|
{
|
||||||
let zWhole = Math.floor(inI / 256);
|
let zWhole = Math.floor(inI / 256);
|
||||||
@ -12,10 +16,11 @@ export const Region =
|
|||||||
let yPart = zPart % 16;
|
let yPart = zPart % 16;
|
||||||
return [yPart, yWhole, zWhole];
|
return [yPart, yWhole, zWhole];
|
||||||
},
|
},
|
||||||
|
|
||||||
|
/** @type {Region.BufferBuilder} */
|
||||||
Surface(inVoxels)
|
Surface(inVoxels)
|
||||||
{
|
{
|
||||||
var vertices = []; // array of surface vertices
|
var vertices = []; // array of surface vertices
|
||||||
var voxPointVert = []; // vox array, contains integer pointers to vertices
|
|
||||||
var triPointVert = []; // triangle array, contains integer pointers to vertices
|
var triPointVert = []; // triangle array, contains integer pointers to vertices
|
||||||
var vertPointNeighbors = []; // vertex array, contains arrays of integer pointers to vertices
|
var vertPointNeighbors = []; // vertex array, contains arrays of integer pointers to vertices
|
||||||
|
|
||||||
@ -30,6 +35,7 @@ export const Region =
|
|||||||
|
|
||||||
Impure refs: voxPointVert, vertPointNeighbors, vertices
|
Impure refs: voxPointVert, vertPointNeighbors, vertices
|
||||||
*/
|
*/
|
||||||
|
var voxPointVert = []; // vox array, contains integer pointers to vertices
|
||||||
var vertPointer = voxPointVert[inVoxIndex]
|
var vertPointer = voxPointVert[inVoxIndex]
|
||||||
if(vertPointer === undefined)
|
if(vertPointer === undefined)
|
||||||
{
|
{
|
||||||
@ -123,6 +129,11 @@ export const Region =
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return {vertices, triPointVert, vertPointNeighbors};
|
return {
|
||||||
|
pos: new Float32Array(vertices.flat()),
|
||||||
|
ind: triPointVert
|
||||||
|
};
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
export default Region;
|
6
scene.js
6
scene.js
@ -1,7 +1,6 @@
|
|||||||
import * as T from 'http://esm.sh/three';
|
import * as T from 'three';
|
||||||
import { OrbitControls } from 'http://esm.sh/three/examples/jsm/controls/OrbitControls.js';
|
import { OrbitControls } from 'three/examples/jsm/controls/OrbitControls.js';
|
||||||
|
|
||||||
export { T as THREE };
|
|
||||||
export function Scene(inSettings)
|
export function Scene(inSettings)
|
||||||
{
|
{
|
||||||
const renderer = new T.WebGLRenderer({canvas:document.querySelector(inSettings.DOM)});
|
const renderer = new T.WebGLRenderer({canvas:document.querySelector(inSettings.DOM)});
|
||||||
@ -83,7 +82,6 @@ export function Scene(inSettings)
|
|||||||
};
|
};
|
||||||
|
|
||||||
scene.add(light);
|
scene.add(light);
|
||||||
//scene.add(new T.GridHelper(10, 10));
|
|
||||||
light.position.set(-1, 2, 4);
|
light.position.set(-1, 2, 4);
|
||||||
camera.position.set(-1, 2, -1);
|
camera.position.set(-1, 2, -1);
|
||||||
camera.lookAt(new T.Vector3(0,0,0));
|
camera.lookAt(new T.Vector3(0,0,0));
|
||||||
|
10
types.d.ts
vendored
Normal file
10
types.d.ts
vendored
Normal file
@ -0,0 +1,10 @@
|
|||||||
|
module Region
|
||||||
|
{
|
||||||
|
type Voxels = Array<boolean>;
|
||||||
|
type Coords = [number, number, number]
|
||||||
|
type Index = number
|
||||||
|
type IndexToCoords = (index:Index)=> Coords
|
||||||
|
type CoordsToIndex = (x:number, y:number, z:number)=> Index
|
||||||
|
type BufferParts = { pos:Array<number>, ind:Array<number> }
|
||||||
|
type BufferBuilder = (voxels:Voxels)=>BufferParts
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user