Compare commits

...

1 Commits

Author SHA1 Message Date
c9905db6d2 geometry buffer 2021-07-06 16:50:26 -04:00
2 changed files with 85 additions and 36 deletions

View File

@ -20,30 +20,17 @@
<body> <body>
<canvas></canvas> <canvas></canvas>
<script type="module"> <script type="module">
import { Region, Kernel } from "./region.js"; import * as T from 'http://esm.sh/three';
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 { OrbitControls } from 'http://esm.sh/three/examples/jsm/controls/OrbitControls.js'; 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 renderer = new T.WebGLRenderer({canvas:document.querySelector("canvas")});
const scene = new T.Scene(); const scene = new T.Scene();
const camera = new T.PerspectiveCamera(80, 2, 0.1, 10); const camera = new T.PerspectiveCamera(80, 2, 0.1, 10);
const light = new T.DirectionalLight(0xFFFFFF, 1); 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 shader = new T.MeshPhongMaterial({color:0xFFCC00});
const shaderBlue = new T.MeshPhongMaterial({color:0x0033FF});
const object = new T.Mesh(geometry, shader); const object = new T.Mesh(geometry, shader);
const controls = new OrbitControls(camera, renderer.domElement); const controls = new OrbitControls(camera, renderer.domElement);
@ -51,6 +38,61 @@ controls.update();
scene.add(light); scene.add(light);
scene.add(object); 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); light.position.set(-1, 2, 4);
camera.position.set(0, 0, 2); camera.position.set(0, 0, 2);
@ -94,5 +136,6 @@ resize();
render(0); render(0);
update(new Date()); update(new Date());
</script> </script>
</body> </body>
</html> </html>

View File

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