Compare commits
6 Commits
master
...
13b0384c54
Author | SHA1 | Date | |
---|---|---|---|
13b0384c54 | |||
72e33a6256 | |||
7611192683 | |||
c419dca58f | |||
9e5ea8098c | |||
dc2f826688 |
241
index.html
241
index.html
@ -20,121 +20,146 @@
|
||||
<body>
|
||||
<canvas></canvas>
|
||||
<script type="module">
|
||||
import * as T from 'http://esm.sh/three';
|
||||
import { OrbitControls } from 'http://esm.sh/three/examples/jsm/controls/OrbitControls.js';
|
||||
import { Region, Kernel } from "./region.js";
|
||||
import { Region } from "./region.js";
|
||||
|
||||
const renderer = new T.WebGLRenderer({canvas:document.querySelector("canvas")});
|
||||
const scene = new T.Scene();
|
||||
const camera = new T.PerspectiveCamera(80, 2, 0.1, 10);
|
||||
const light = new T.DirectionalLight(0xFFFFFF, 1);
|
||||
const geometry = new T.BoxGeometry(0.8, 0.8, 0.8);
|
||||
const shader = new T.MeshPhongMaterial({color:0xFFCC00});
|
||||
const shaderBlue = new T.MeshPhongMaterial({color:0x0033FF});
|
||||
const object = new T.Mesh(geometry, shader);
|
||||
let voxels = [];
|
||||
voxels[Region.I(2, 2, 2)] = true;
|
||||
voxels[Region.I(3, 2, 2)] = true;
|
||||
console.log(Region.Surface(voxels));
|
||||
|
||||
const controls = new OrbitControls(camera, renderer.domElement);
|
||||
controls.update();
|
||||
</script>
|
||||
<!--
|
||||
<script type="module">
|
||||
import { Scene, THREE } from './scene.js';
|
||||
|
||||
scene.add(light);
|
||||
scene.add(object);
|
||||
scene.add(new T.GridHelper(10, 10));
|
||||
|
||||
|
||||
const geoBuff = new T.BufferGeometry();
|
||||
geoBuff.setAttribute("position", new T.BufferAttribute(new Float32Array(
|
||||
[
|
||||
0, 0, 0,
|
||||
1, 0, 0,
|
||||
1, 1, 0,
|
||||
0, 1, 0,
|
||||
2, 0, 1,
|
||||
2, 1, 1
|
||||
]
|
||||
), 3));
|
||||
|
||||
geoBuff.setIndex(
|
||||
[
|
||||
0, 1, 2,
|
||||
2, 3, 0,
|
||||
1, 4, 5,
|
||||
5, 2, 1
|
||||
]
|
||||
);
|
||||
geoBuff.computeVertexNormals();
|
||||
const geoObj = new T.Mesh(geoBuff, shader);
|
||||
shader.shading = T.FlatShading;
|
||||
scene.add(geoObj);
|
||||
|
||||
/*
|
||||
let r = Region.Create();
|
||||
let filler = (x, y, z) =>
|
||||
{
|
||||
r.Voxels[Region.CoordsToIndex(x, y, z)] = true;
|
||||
};
|
||||
filler(1, 1, 1);
|
||||
Kernel.Loop(Kernel.Corners, [1, 1, 1], filler);
|
||||
Kernel.Loop(Kernel.Cardinal, [1, 1, 1], filler);
|
||||
Region.Update(r);
|
||||
|
||||
r.Filled.forEach((inIndexMap) =>
|
||||
{
|
||||
let coords = Region.IndexToCoords(inIndexMap);
|
||||
let cube = new T.Mesh(geometry, shader);
|
||||
cube.position.set(...coords);
|
||||
scene.add(cube);
|
||||
});
|
||||
|
||||
r.Surface.forEach((inIndexMap) =>
|
||||
{
|
||||
let coords = Region.IndexToCoords(inIndexMap);
|
||||
let cube = new T.Mesh(geometry, shaderBlue);
|
||||
cube.position.set(...coords);
|
||||
scene.add(cube);
|
||||
});
|
||||
*/
|
||||
|
||||
light.position.set(-1, 2, 4);
|
||||
camera.position.set(0, 0, 2);
|
||||
|
||||
let dirty = false;
|
||||
const update = inTime =>
|
||||
{
|
||||
let timeNew = new Date();
|
||||
let timeDelta = timeNew - inTime;
|
||||
|
||||
let delta = timeDelta * 0.001;
|
||||
object.rotation.x += delta;
|
||||
|
||||
controls.update();
|
||||
|
||||
dirty = true;
|
||||
|
||||
setTimeout(()=>update(timeNew), 100);
|
||||
}
|
||||
|
||||
const render = inTime =>
|
||||
{
|
||||
if(dirty)
|
||||
let average = (inArray, inPointInd, ...inOthersInd) =>
|
||||
{
|
||||
renderer.render(scene, camera);
|
||||
dirty = false;
|
||||
let sum = [0, 0, 0];
|
||||
for(let i=0; i<inOthersInd.length; i++)
|
||||
{
|
||||
let otherInd = inOthersInd[i]*3;
|
||||
sum[0] += inArray[otherInd+0];
|
||||
sum[1] += inArray[otherInd+1];
|
||||
sum[2] += inArray[otherInd+2];
|
||||
}
|
||||
requestAnimationFrame(render);
|
||||
}
|
||||
|
||||
const resize = () =>
|
||||
{
|
||||
let canvas = renderer.domElement;
|
||||
camera.aspect = canvas.clientWidth / canvas.clientHeight;
|
||||
camera.updateProjectionMatrix();
|
||||
renderer.setSize(canvas.clientWidth, canvas.clientHeight, false);
|
||||
};
|
||||
let pointX = inPointInd*3 + 0;
|
||||
let pointY = inPointInd*3 + 1;
|
||||
let pointZ = inPointInd*3 + 2;
|
||||
let reducer = inOthersInd.length;
|
||||
let limit = (inCenter, inPoint) =>
|
||||
{
|
||||
var limit;
|
||||
limit = inCenter-0.49;
|
||||
if(inPoint <= limit)
|
||||
{
|
||||
return limit;
|
||||
}
|
||||
limit = inCenter + 0.49;
|
||||
if(inPoint >= limit)
|
||||
{
|
||||
return limit;
|
||||
}
|
||||
|
||||
return inPoint;
|
||||
}
|
||||
inArray[pointX] = limit(Math.round(inArray[pointX]), sum[0]/reducer);
|
||||
inArray[pointY] = limit(Math.round(inArray[pointY]), sum[1]/reducer);
|
||||
inArray[pointZ] = limit(Math.round(inArray[pointZ]), sum[2]/reducer);
|
||||
};
|
||||
let smooth = (inArray, inTimes) =>
|
||||
{
|
||||
let count = inArray.length/3;
|
||||
for(let t=0; t<inTimes; t++)
|
||||
{
|
||||
for(let i=0; i<count; i++)
|
||||
{
|
||||
if(i==0)
|
||||
{
|
||||
average(inArray, i, count-1, i+1);
|
||||
}
|
||||
else
|
||||
{
|
||||
if(i==count-1)
|
||||
{
|
||||
average(inArray, i, i-1, 0);
|
||||
}
|
||||
else
|
||||
{
|
||||
average(inArray, i, i-1, i+1);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
let testPoints =
|
||||
[
|
||||
0, 0, 0,
|
||||
0, 1, 0,
|
||||
1, 1, 0
|
||||
];
|
||||
console.log(testPoints);
|
||||
average(testPoints, 1, 0, 2);
|
||||
console.log(testPoints);
|
||||
*/
|
||||
|
||||
let path = (inPoints, inSmoothing, inColor) =>
|
||||
{
|
||||
let clone = [...inPoints];
|
||||
smooth(clone, inSmoothing);
|
||||
const geometry = new THREE.BufferGeometry();
|
||||
geometry.setAttribute('position', new THREE.Float32BufferAttribute( clone, 3 ));
|
||||
const materialLine = new THREE.LineBasicMaterial( { color: inColor, linewidth: 3 } );
|
||||
const materialPoint = new THREE.PointsMaterial( { color:inColor, size: 0.05 } );
|
||||
const output = new THREE.Group();
|
||||
|
||||
output.add( new THREE.Line( geometry, materialLine ) );
|
||||
output.add( new THREE.Points( geometry, materialPoint ) );
|
||||
return output;
|
||||
}
|
||||
|
||||
const points =
|
||||
[
|
||||
4, 0, 0,
|
||||
3, 0, 0,
|
||||
2, 0, 0,
|
||||
1, 0, 0,
|
||||
//
|
||||
0, 0, 0,
|
||||
0, 1, 0,
|
||||
1, 1, 0,
|
||||
2, 1, 0,
|
||||
2, 2, 0,
|
||||
3, 2, 0,
|
||||
4, 2, 0,
|
||||
4, 3, 0,
|
||||
//
|
||||
5, 3, 0,
|
||||
5, 2, 0,
|
||||
5, 1, 0,
|
||||
5, 0, 0,
|
||||
];
|
||||
|
||||
|
||||
/*
|
||||
Scene(
|
||||
{
|
||||
DOM:"canvas",
|
||||
Init(inScene)
|
||||
{
|
||||
inScene.add(path(points, 0, 0xFF0000));
|
||||
inScene.add(path(points, 10, 0x00ff00));
|
||||
},
|
||||
Update(scene, delta)
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
);
|
||||
*/
|
||||
|
||||
window.addEventListener("resize", resize);
|
||||
resize();
|
||||
render(0);
|
||||
update(new Date());
|
||||
</script>
|
||||
|
||||
</body>
|
||||
|
93
region.js
93
region.js
@ -20,7 +20,6 @@ export const Kernel =
|
||||
[ 0, 0, 1],
|
||||
[ 0, 0, -1]
|
||||
],
|
||||
|
||||
Loop(inList, inVector, inHandler)
|
||||
{
|
||||
for(let i=0; i<inList.length; i++)
|
||||
@ -43,11 +42,11 @@ export const Region =
|
||||
Surface:[]
|
||||
};
|
||||
},
|
||||
CoordsToIndex(inX, inY, inZ)
|
||||
I(inX, inY, inZ)
|
||||
{
|
||||
return inX + inY*16 + inZ*256;
|
||||
},
|
||||
IndexToCoords(inI)
|
||||
XYZ(inI)
|
||||
{
|
||||
let zWhole = Math.floor(inI / 256);
|
||||
let zPart = inI % 256;
|
||||
@ -55,27 +54,83 @@ export const Region =
|
||||
let yPart = zPart % 16;
|
||||
return [yPart, yWhole, zWhole];
|
||||
},
|
||||
Update(inRegion)
|
||||
Surface(inVoxels)
|
||||
{
|
||||
inRegion.Filled = [];
|
||||
inRegion.Surface = [];
|
||||
function Vert(a)
|
||||
{
|
||||
var vertPointer = voxPointVert[a]
|
||||
if(vertPointer === undefined)
|
||||
{
|
||||
let vert = Region.XYZ(a);
|
||||
vertPointer = vertices.length;
|
||||
voxPointVert[a] = vertPointer;
|
||||
vertPointNeighbors.push([]);
|
||||
vertices.push(vert);
|
||||
}
|
||||
return vertPointer;
|
||||
}
|
||||
function Face(a, b, c, d)
|
||||
{
|
||||
let p1 = Vert(a);
|
||||
let p2 = Vert(b);
|
||||
let p3 = Vert(c);
|
||||
let p4 = Vert(d);
|
||||
|
||||
triPointVert.push(p1, p2, p3, p3, p4, p1);
|
||||
|
||||
vertPointNeighbors[p1].push(p2);
|
||||
vertPointNeighbors[p2].push(p1);
|
||||
|
||||
vertPointNeighbors[p2].push(p3);
|
||||
vertPointNeighbors[p3].push(p2);
|
||||
|
||||
vertPointNeighbors[p3].push(p4);
|
||||
vertPointNeighbors[p4].push(p3);
|
||||
|
||||
vertPointNeighbors[p4].push(p1);
|
||||
vertPointNeighbors[p1].push(p2);
|
||||
}
|
||||
|
||||
var vertices = [];
|
||||
var voxPointVert = [];
|
||||
var triPointVert = [];
|
||||
var vertPointNeighbors = [];
|
||||
|
||||
var i;
|
||||
for(i=0; i<inRegion.Voxels.length; i++)
|
||||
var BA, BU, BC, BR, TA, TU, TC, TR, CL, CD, CB;
|
||||
for(i=0; i<inVoxels.length; i++)
|
||||
{
|
||||
if(inRegion.Voxels[i])
|
||||
if(inVoxels[i])
|
||||
{
|
||||
let coords = Region.IndexToCoords(i);
|
||||
Kernel.Loop(Kernel.Corners, coords, (inX, inY, inZ)=>
|
||||
{
|
||||
let offsetIndex = Region.CoordsToIndex(inX, inY, inZ);
|
||||
if(!inRegion.Voxels[offsetIndex])
|
||||
{
|
||||
inRegion.Surface.push(i);
|
||||
return true;
|
||||
}
|
||||
});
|
||||
let [x, y, z] = Region.XYZ(i);
|
||||
|
||||
// bottom: at, "up", corner, right
|
||||
BA = i;
|
||||
BU = Region.I(x+1, y, z);
|
||||
BC = Region.I(x+1, y+1, z);
|
||||
BR = Region.I(x, y+1, z);
|
||||
|
||||
// top: at, "up" corner, right
|
||||
TA = Region.I(x, y, z+1);
|
||||
TU = Region.I(x+1, y, z+1);
|
||||
TC = Region.I(x+1, y+1, z+1);
|
||||
TR = Region.I(x, y+1, z+1);
|
||||
|
||||
// check: left, "down", below
|
||||
CL = Region.I(x-1, y, z);
|
||||
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[CL]){ Face(BA, BU, TU, TA); }
|
||||
if(!inVoxels[CD]){ Face(BR, BA, TA, TR); }
|
||||
if(!inVoxels[CB]){ Face(BA, BU, BC, BR); }
|
||||
}
|
||||
}
|
||||
console.log("looped over", i);
|
||||
|
||||
return {vertices, voxPointVert, triPointVert, vertPointNeighbors};
|
||||
}
|
||||
};
|
||||
|
57
scene.js
Normal file
57
scene.js
Normal file
@ -0,0 +1,57 @@
|
||||
import * as T from 'http://esm.sh/three';
|
||||
import { OrbitControls } from 'http://esm.sh/three/examples/jsm/controls/OrbitControls.js';
|
||||
|
||||
export { T as THREE };
|
||||
export function Scene(inSettings)
|
||||
{
|
||||
const renderer = new T.WebGLRenderer({canvas:document.querySelector(inSettings.DOM)});
|
||||
const scene = new T.Scene();
|
||||
const camera = new T.PerspectiveCamera(80, 2, 0.1, 10);
|
||||
const light = new T.DirectionalLight(0xFFFFFF, 1);
|
||||
const controls = new OrbitControls(camera, renderer.domElement);
|
||||
let dirty = false;
|
||||
|
||||
const update = inTime =>
|
||||
{
|
||||
let timeNew = new Date();
|
||||
let timeDelta = timeNew - inTime;
|
||||
let delta = timeDelta * 0.001;
|
||||
|
||||
inSettings.Update(scene, delta);
|
||||
|
||||
controls.update();
|
||||
dirty = true;
|
||||
setTimeout(()=>update(timeNew), 50);
|
||||
}
|
||||
|
||||
const render = inTime =>
|
||||
{
|
||||
if(dirty)
|
||||
{
|
||||
renderer.render(scene, camera);
|
||||
dirty = false;
|
||||
}
|
||||
requestAnimationFrame(render);
|
||||
}
|
||||
|
||||
const resize = () =>
|
||||
{
|
||||
let canvas = renderer.domElement;
|
||||
camera.aspect = canvas.clientWidth / canvas.clientHeight;
|
||||
camera.updateProjectionMatrix();
|
||||
renderer.setSize(canvas.clientWidth, canvas.clientHeight, false);
|
||||
};
|
||||
|
||||
scene.add(light);
|
||||
//scene.add(new T.GridHelper(10, 10));
|
||||
light.position.set(-1, 2, 4);
|
||||
camera.position.set(0, 0, 2);
|
||||
controls.update();
|
||||
|
||||
inSettings.Init(scene);
|
||||
|
||||
window.addEventListener("resize", resize);
|
||||
resize();
|
||||
render(0);
|
||||
update(new Date());
|
||||
};
|
Loading…
Reference in New Issue
Block a user