This commit is contained in:
Lucas Dower 2021-07-03 15:20:55 +01:00
parent 58de48547c
commit 4c50b94f6a
6 changed files with 125 additions and 32 deletions

View File

@ -10,7 +10,7 @@ class ArcballCamera {
this.zNear = zNear;
this.zFar = zFar;
this.actualDistance = 10.0;
this.actualDistance = 15.0;
this.actualAzimuth = 0.6;
this.actualElevation = 1.3;

View File

@ -3,14 +3,22 @@ const { Vector3 } = require('./src/vector.js');
const { Triangle } = require('./src/triangle.js');
const { VoxelManager } = require('./src/voxel_manager.js');
const renderer = new Renderer();
const voxelManager = new VoxelManager(0.25);
const voxelSize = 0.1;
const renderer = new Renderer(voxelSize);
const voxelManager = new VoxelManager(voxelSize);
const triangle = new Triangle(new Vector3(0, 0, 0), new Vector3(4, 3, 1), new Vector3(2, -3, -2));
const triangle2 = new Triangle(new Vector3(5, 2, -1), new Vector3(-2, 3, -1), new Vector3(0, 3, 2));
console.log(triangle2);
renderer.setStroke(new Vector3(1.0, 0.0, 0.0));
renderer.registerTriangle(triangle.v0, triangle.v1, triangle.v2);
renderer.registerTriangle(triangle);
renderer.registerTriangle(triangle2);
voxelManager.voxeliseTriangle(triangle, renderer);
voxelManager.voxeliseTriangle(triangle);
voxelManager.voxeliseTriangle(triangle2);
renderer.setStroke(new Vector3(1.0, 1.0, 1.0));
renderer.registerVoxels(voxelManager.voxels);
renderer.compileRegister();

60
src/hash_map.js Normal file
View File

@ -0,0 +1,60 @@
const { Vector3 } = require('./vector.js');
class HashMap {
constructor(numBins) {
this.numBins = numBins;
this.bins = new Array(numBins);
}
_getBin(key) {
const hash = key.hash(); // A bit naughty
return Math.abs(hash) % this.numBins;
}
add(key, value) {
const binIndex = this._getBin(key);
console.log(binIndex);
if (!this.bins[binIndex]) {
this.bins[binIndex] = [ {key: key, value: value} ];
} else {
this.bins[binIndex].push({key: key, value: value});
}
}
get(key) {
const binIndex = this._getBin(key);
if (!this.bins[binIndex]) {
return;
}
const list = this.bins[binIndex];
for (const item of list) {
if (item.key.equals(key)) {
return item.value;
}
}
}
}
const hashMap = new HashMap(4);
const v = new Vector3(2.0, 2.0, 3.0);
const v2 = new Vector3(2.0, 2.0, 5.0);
const v3 = new Vector3(-2.0, -2.0, -5.0);
const v4 = new Vector3(-2.0, -5.0, -7.0);
hashMap.add(v, true);
hashMap.add(v2, true);
hashMap.add(v3, true);
console.log(hashMap.bins[0]);
console.log(hashMap.bins[1]);
console.log(hashMap.bins[2]);
console.log(hashMap.bins[3]);
console.log(hashMap.get(v));
console.log(hashMap.get(v2));
console.log(hashMap.get(v3));
console.log(hashMap.get(v4));

View File

@ -7,7 +7,7 @@ const shaderManager = require('./shaders.js');
class Renderer {
constructor() {
constructor(voxelSize) {
this._gl = document.querySelector("#c").getContext("webgl");
this._fov = 30;
@ -25,6 +25,9 @@ class Renderer {
};
this._maxIndex = 0;
//this._voxelSize = voxelSize;
this._voxelSizeVector = new Vector3(voxelSize, voxelSize, voxelSize);
this._registerOpen = true;
}
@ -129,6 +132,7 @@ class Renderer {
};
}
/*
// Use when immediate drawing
drawBox(centre, size) {
const data = this._getBoxData(centre, size);
@ -140,18 +144,43 @@ class Renderer {
const data = this._getTriangleData(a, b, c);
this._drawData(data);
}
*/
// Use when drawing the same thing each frame
registerBox(centre, size) {
const data = this._getBoxData(centre, size);
this._addDataToRegister(data);
registerBox(centre, size, debug) {
if (debug) {
const data = this._getBoxData(centre, size);
this._addDataToRegister(data);
} else {
// TODO
}
}
// Use when drawing the same triangle each frame
/*
registerTriangle(a, b, c) {
const data = this._getTriangleData(a, b, c);
this._addDataToRegister(data);
}*/
registerTriangle(triangle, debug) {
if (debug) {
const data = this._getTriangleData(triangle.v0, triangle.v1, triangle.v2);
this._addDataToRegister(data);
} else {
// TODO
}
}
registerVoxel(centre) {
const data = this._getBoxData(centre, this._voxelSizeVector);
this._addDataToRegister(data);
}
registerVoxels(voxelCentres) {
for (const voxelCentre of voxelCentres) {
this.registerVoxel(voxelCentre);
}
}
_addDataToRegister(data) {

View File

@ -84,6 +84,17 @@ class Vector3 {
);
}
hash() {
const p0 = 73856093;
const p1 = 19349663;
const p2 = 83492791;
return (this.x * p0) ^ (this.y * p1) ^ (this.z * p2);
}
equals(vec) {
return this.x == vec.x && this.y == vec.y && this.z == vec.z;
}
}
module.exports.Vector3 = Vector3;

View File

@ -5,26 +5,7 @@ class VoxelManager {
constructor(voxelSize) {
this._voxelSize = voxelSize;
}
cubeifyAABB(aabb, gridSnap) {
const size = aabb.size;
const maxDimension = Math.max(size.x, size.y, size.z);
let newSize = new Vector3(maxDimension, maxDimension, maxDimension);
if (gridSnap) {
let newCentre = Vector3.divScalar(aabb.centre, this._voxelSize);
newCentre = Vector3.round(newCentre);
newCentre = Vector3.mulScalar(newCentre, this._voxelSize);
const offset = Vector3.sub(aabb.centre, newCentre);
console.log(offset);
//let newSize
return new AABB(newCentre, newSize);
}
return new AABB(aabb.centre, newSize);
this.voxels = [];
}
_getTriangleCubeAABB(triangle) {
@ -44,10 +25,11 @@ class VoxelManager {
return cubeAABB;
}
voxeliseTriangle(triangle, renderer) {
voxeliseTriangle(triangle) {
const cubeAABB = this._getTriangleCubeAABB(triangle);
renderer.setStroke(new Vector3(1.0, 1.0, 1.0));
//renderer.setStroke(new Vector3(1.0, 1.0, 1.0));
//let voxels = [];
let queue = [cubeAABB];
while (queue.length > 0) {
@ -58,10 +40,13 @@ class VoxelManager {
queue.push(...aabb.subdivide());
} else {
// We've reached the voxel level, stop
renderer.registerBox(aabb.centre, aabb.size);
//renderer.registerBox(aabb.centre, aabb.size);
this.voxels.push(aabb.centre);
}
}
}
//return voxels;
}
}