This commit is contained in:
TreetopFlyer 2021-07-05 20:51:08 -04:00
commit 32c436f40a
2 changed files with 174 additions and 0 deletions

98
index.html Normal file
View File

@ -0,0 +1,98 @@
<!doctype html>
<html>
<head>
<style>
html, body
{
margin:0;
}
canvas
{
position:absolute;
top:0;
left:0;
display:block;
width:100%;
height:100%;
}
</style>
</head>
<body>
<canvas></canvas>
<script type="module">
import { Region, Kernel } from "./region.js";
let r = Region.Create();
let filler = (x, y, z)=>
{
r.filled.push(Region.I(x, y, z));
};
filler(1, 1, 1);
Kernel.Loop(Kernel.Corners, [1, 1, 1], filler);
Kernel.Loop(Kernel.Cardinal, [1, 1, 1], filler);
Region.Surface(r);
</script>
<script type="module">
import * as T from 'http://esm.sh/three';
import { OrbitControls } from 'http://esm.sh/three/examples/jsm/controls/OrbitControls.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(1, 1, 1);
const shader = new T.MeshPhongMaterial({color:0xFFCC00});
const object = new T.Mesh(geometry, shader);
const controls = new OrbitControls(camera, renderer.domElement);
controls.update();
scene.add(light);
scene.add(object);
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)
{
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);
};
window.addEventListener("resize", resize);
resize();
render(0);
update(new Date());
</script>
</body>
</html>

76
region.js Normal file
View File

@ -0,0 +1,76 @@
export const Kernel =
{
Corners:
[
[ 1, 1, -1],
[ 1, -1, -1],
[-1, 1, -1],
[-1, -1, -1],
[ 1, 1, 1],
[ 1, -1, 1],
[-1, 1, 1],
[-1, -1, 1]
],
Cardinal:
[
[ 1, 0, 0],
[ 0, 1, 0],
[-1, 0, 0],
[ 0, -1, 0],
[ 0, 0, 1],
[ 0, 0, -1]
],
Loop(inList, inVector, inHandler)
{
for(let i=0; i<inList.length; i++)
{
let delta = inList[i];
if(inHandler(inVector[0]+delta[0], inVector[1]+delta[1], inVector[2]+delta[2]))
{
return;
}
}
}
};
export const Region =
{
Create()
{
return {filled:[]};
},
I(inX, inY, inZ)
{
return inX + inY*32 + inZ*1024;
},
XYZ(inI)
{
let zWhole = Math.floor(inI / 1024);
let zPart = inI % 1024;
let yWhole = Math.floor(zPart / 32);
let yPart = zPart % 32;
return [yPart, yWhole, zWhole];
},
Surface(inRegion)
{
let surface = [];
inRegion.filled.forEach(inIndex =>
{
let coords = Region.XYZ(inIndex);
Kernel.Loop(Kernel.Corners, coords, (inX, inY, inZ)=>
{
let index = Region.I(inX, inY, inZ);
if(inRegion.filled[index] === undefined)
{
console.log(inX, inY, inZ, "surface");
surface.push(inIndex);
return true;
}
else
{
console.log(inX, inY, inZ, "filled");
}
});
});
return surface;
}
};