Add Delaunay triangulation and begin the Voronoi generation. Change the model to host the data on the worker only

This commit is contained in:
Peaceultime 2021-11-22 17:41:56 +01:00
parent 37d1051838
commit e4217d3f4d
3 changed files with 51 additions and 13 deletions

View File

@ -10,12 +10,15 @@ import Renderer from "./modules/renderer/renderer.mjs";
const thread = new Thread("./workers/base.mjs", "model", true);
await thread.setup();
const settings = { x: 200, y: 200, seed: 0, jitter: 0.5, width: window.innerWidth, height: window.innerHeight };
const grid = new Float32Array(await thread.model.fillArr(settings));
console.log(grid);
const view = new Float32Array(await thread.view.lerpToViewport(grid.buffer, settings));
console.log(view);
const settings = { x: 200, y: 200, seed: 0, jitter: 0.5, width: window.innerWidth, height: window.innerHeight };
await thread.model.generatePoints(settings);
await thread.model.generateTriangles(settings);
const delaunay = await thread.debug.getDelaunay();
console.log(delaunay);
const view = new Float32Array(await thread.view.lerpToViewport(settings));
Renderer.init();
Renderer.initGrid(view, settings);

View File

@ -105,7 +105,7 @@ function onmessage(thread)
globalThis.VERBOSE && console.debug(`Received message from ${thread._name} containing `, ret);
globalThis.TIMINGS && thread._calledFn !== "" && console.timeEnd("Measuring " + thread._calledMod + "." + thread._calledFn);
if(thread._waiting && ret)
if(thread._waiting)
{
globalThis.DEBUG && console.log(`Resolving ${thread._name}`);
thread._ret = ret;

View File

@ -1,9 +1,12 @@
import { Process } from "../utils/workerUtils.mjs";
import Noise from '../libs/alea.mjs'
import Delaunator from '../libs/delaunator.mjs'
import Delaunator from 'https://cdn.skypack.dev/delaunator@5.0.0';
//Settings contains x, y depth, seed, jitter
function fillArr(settings)
let settings = {};
const model = {};
//Settings contains x, y, seed, jitter
function generatePoints(settings)
{
const noise = Noise(settings.seed);
const grid = new Float32Array(settings.x * settings.y * 2);
@ -16,13 +19,43 @@ function fillArr(settings)
grid[i + 1] = _y + (noise() - 0.5) * settings.jitter;
}
return grid.buffer;
model.grid = grid;
}
function generateTriangles(settings)
{
if(!model.hasOwnProperty("grid"))
generatePoints(settings);
const delaunay = new Delaunator(model.grid);
model.delaunay = delaunay;
}
function generateVoronoi(settings)
{
function circumcenter(a, b, c)
{
const ad = a[0] * a[0] + a[1] * a[1];
const bd = b[0] * b[0] + b[1] * b[1];
const cd = c[0] * c[0] + c[1] * c[1];
const D = 2 * (a[0] * (b[1] - c[1]) + b[0] * (c[1] - a[1]) + c[0] * (a[1] - b[1]));
return [
1 / D * (ad * (b[1] - c[1]) + bd * (c[1] - a[1]) + cd * (a[1] - b[1])),
1 / D * (ad * (c[0] - b[0]) + bd * (a[0] - c[0]) + cd * (b[0] - a[0])),
];
}
}
function lerpToViewport(buffer, settings)
function getDelaunay()
{
return model.delaunay;
}
function lerpToViewport(settings)
{
const size = settings.x * settings.y;
const grid = new Float32Array(buffer);
const grid = model.grid;
const view = new Float32Array(size * 3);
for(let i = size - 1; i >= 0; --i)
{
@ -34,5 +67,7 @@ function lerpToViewport(buffer, settings)
}
//Process.register is the equivalent of export
Process.register("model", [fillArr]);
Process.register("model", [generatePoints, generateTriangles]);
Process.register("view", [lerpToViewport]);
Process.register("debug", [getDelaunay]);