geometry buffer

This commit is contained in:
TreetopFlyer 2021-07-06 16:50:26 -04:00
parent 6225a1af13
commit c9905db6d2
2 changed files with 85 additions and 36 deletions

View File

@ -20,30 +20,17 @@
<body>
<canvas></canvas>
<script type="module">
import { Region, Kernel } from "./region.js";
let r = Region.Create();
let filler = (x, y, z)=>
{
r.voxels[Region.I(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.Surface(r);
</script>
<script type="module">
import * as T from 'http://esm.sh/three';
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";
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 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);
const controls = new OrbitControls(camera, renderer.domElement);
@ -51,6 +38,61 @@ controls.update();
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);
@ -94,5 +136,6 @@ resize();
render(0);
update(new Date());
</script>
</body>
</html>

View File

@ -20,6 +20,7 @@ export const Kernel =
[ 0, 0, 1],
[ 0, 0, -1]
],
Loop(inList, inVector, inHandler)
{
for(let i=0; i<inList.length; i++)
@ -36,40 +37,45 @@ export const Region =
{
Create()
{
return {voxels:[]};
return {
Voxels:[],
Filled:[],
Surface:[]
};
},
I(inX, inY, inZ)
CoordsToIndex(inX, inY, inZ)
{
return inX + inY*32 + inZ*1024;
return inX + inY*16 + inZ*256;
},
XYZ(inI)
IndexToCoords(inI)
{
let zWhole = Math.floor(inI / 1024);
let zPart = inI % 1024;
let yWhole = Math.floor(zPart / 32);
let yPart = zPart % 32;
let zWhole = Math.floor(inI / 256);
let zPart = inI % 256;
let yWhole = Math.floor(zPart / 16);
let yPart = zPart % 16;
return [yPart, yWhole, zWhole];
},
Surface(inRegion)
Update(inRegion)
{
let surface = [];
inRegion.voxels.forEach((inValue, inIndex) =>
inRegion.Filled = [];
inRegion.Surface = [];
var i;
for(i=0; i<inRegion.Voxels.length; i++)
{
if(inValue)
if(inRegion.Voxels[i])
{
let coords = Region.XYZ(inIndex);
let coords = Region.IndexToCoords(i);
Kernel.Loop(Kernel.Corners, coords, (inX, inY, inZ)=>
{
let kernel = Region.I(inX, inY, inZ);
if(!inRegion.voxels[kernel])
let offsetIndex = Region.CoordsToIndex(inX, inY, inZ);
if(!inRegion.Voxels[offsetIndex])
{
console.log(coords, "surface");
surface.push(inIndex);
inRegion.Surface.push(i);
return true;
}
});
}
});
return surface;
}
console.log("looped over", i);
}
};