voxel/index.html

141 lines
3.0 KiB
HTML
Raw Permalink Normal View History

2021-07-05 20:51:08 -04:00
<!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">
2021-07-06 16:50:26 -04:00
import * as T from 'http://esm.sh/three';
2021-07-05 20:51:08 -04:00
import { OrbitControls } from 'http://esm.sh/three/examples/jsm/controls/OrbitControls.js';
2021-07-06 16:50:26 -04:00
import { Region, Kernel } from "./region.js";
2021-07-05 20:51:08 -04:00
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);
2021-07-06 16:50:26 -04:00
const geometry = new T.BoxGeometry(0.8, 0.8, 0.8);
2021-07-05 20:51:08 -04:00
const shader = new T.MeshPhongMaterial({color:0xFFCC00});
2021-07-06 16:50:26 -04:00
const shaderBlue = new T.MeshPhongMaterial({color:0x0033FF});
2021-07-05 20:51:08 -04:00
const object = new T.Mesh(geometry, shader);
const controls = new OrbitControls(camera, renderer.domElement);
controls.update();
scene.add(light);
scene.add(object);
2021-07-06 16:50:26 -04:00
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);
});
*/
2021-07-05 20:51:08 -04:00
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>
2021-07-06 16:50:26 -04:00
2021-07-05 20:51:08 -04:00
</body>
</html>