Add Delaunay triangulation and begin the Voronoi generation. Change the model to host the data on the worker only
This commit is contained in:
parent
37d1051838
commit
e4217d3f4d
13
src/main.mjs
13
src/main.mjs
|
|
@ -10,12 +10,15 @@ import Renderer from "./modules/renderer/renderer.mjs";
|
||||||
|
|
||||||
const thread = new Thread("./workers/base.mjs", "model", true);
|
const thread = new Thread("./workers/base.mjs", "model", true);
|
||||||
await thread.setup();
|
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));
|
const settings = { x: 200, y: 200, seed: 0, jitter: 0.5, width: window.innerWidth, height: window.innerHeight };
|
||||||
console.log(view);
|
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.init();
|
||||||
Renderer.initGrid(view, settings);
|
Renderer.initGrid(view, settings);
|
||||||
|
|
|
||||||
|
|
@ -105,7 +105,7 @@ function onmessage(thread)
|
||||||
globalThis.VERBOSE && console.debug(`Received message from ${thread._name} containing `, ret);
|
globalThis.VERBOSE && console.debug(`Received message from ${thread._name} containing `, ret);
|
||||||
globalThis.TIMINGS && thread._calledFn !== "" && console.timeEnd("Measuring " + thread._calledMod + "." + thread._calledFn);
|
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}`);
|
globalThis.DEBUG && console.log(`Resolving ${thread._name}`);
|
||||||
thread._ret = ret;
|
thread._ret = ret;
|
||||||
|
|
|
||||||
|
|
@ -1,9 +1,12 @@
|
||||||
import { Process } from "../utils/workerUtils.mjs";
|
import { Process } from "../utils/workerUtils.mjs";
|
||||||
import Noise from '../libs/alea.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
|
let settings = {};
|
||||||
function fillArr(settings)
|
const model = {};
|
||||||
|
|
||||||
|
//Settings contains x, y, seed, jitter
|
||||||
|
function generatePoints(settings)
|
||||||
{
|
{
|
||||||
const noise = Noise(settings.seed);
|
const noise = Noise(settings.seed);
|
||||||
const grid = new Float32Array(settings.x * settings.y * 2);
|
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;
|
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 size = settings.x * settings.y;
|
||||||
const grid = new Float32Array(buffer);
|
const grid = model.grid;
|
||||||
const view = new Float32Array(size * 3);
|
const view = new Float32Array(size * 3);
|
||||||
for(let i = size - 1; i >= 0; --i)
|
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 is the equivalent of export
|
||||||
Process.register("model", [fillArr]);
|
Process.register("model", [generatePoints, generateTriangles]);
|
||||||
Process.register("view", [lerpToViewport]);
|
Process.register("view", [lerpToViewport]);
|
||||||
|
|
||||||
|
Process.register("debug", [getDelaunay]);
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue