Test some feature for the voronoi generation.

This commit is contained in:
Peaceultime 2022-12-12 23:41:04 +01:00
parent 0410e3d709
commit cbe0f0a5a8
3 changed files with 51 additions and 30 deletions

View File

@ -15,15 +15,15 @@ import Delaunator from 'https://cdn.skypack.dev/delaunator@5.0.0';
//await new Promise(r => setTimeout(r, 3000));
const settings = { model: { x: 50, y: 25, seed: 0, jitter: 0.2, }, view: { width: window.innerWidth, height: window.innerHeight, }, };
const settings = { model: { x: 30, y: 30, seed: 0, jitter: 0.1, }, view: { width: window.innerWidth, height: window.innerHeight, }, };
await thread.global.updateSettings(settings);
await thread.model.generateTriangles();
const view = await thread.view.lerpToViewport();
const debug = await thread.debug.getAll();
window.debug = await thread.debug.getAll();
Renderer.init();
Renderer.initGrid(view.position, view.vertices);
Renderer.initGrid(undefined, view.position, view.vertices);
})();

View File

@ -44,7 +44,7 @@ class Renderer
this._renderer.render(this._scene, this._camera);
}
static async initGrid(position, vertices) //Share the list of cell and the resolution in the model with the view
static async initGrid(centers, positions, vertices) //Share the list of cell and the resolution in the model with the view
{
if(this._threaded)
{
@ -53,16 +53,22 @@ class Renderer
}
else
{
/*const centerGeometry = new Three.BufferGeometry();
centerGeometry.setAttribute('position', new Three.Float32BufferAttribute( centers, 3));
*/
const geometry = new Three.BufferGeometry();
geometry.setIndex( new Three.Uint32BufferAttribute( vertices, 1 ) );
geometry.setAttribute('position', new Three.Float32BufferAttribute( position, 3));
geometry.setAttribute('position', new Three.Float32BufferAttribute( positions, 3));
const material = new Three.MeshBasicMaterial( { color: 0xff0000, wireframe: true, } );
const mesh = new Three.Mesh( geometry, material );
this._scene.add( mesh );
/*const points = new Three.Points( geometry, new Three.PointsMaterial( { size: 3.0 } ) );
this._scene.add( points );*/
const points = new Three.Points( geometry, new Three.PointsMaterial( { size: 3.0 } ) );
this._scene.add( points );
/*const center = new Three.Points( centerGeometry, new Three.PointsMaterial( { color: 0xffbb00, size: 2.0 } ) );
this._scene.add( center );*/
}
}
}

View File

@ -1,7 +1,8 @@
import { Process } from "../utils/workerUtils.mjs";
import Noise from '../libs/alea.mjs'
import Noise from '../libs/alea.mjs';
import Delaunator from 'https://cdn.skypack.dev/delaunator@5.0.0';
let settings = {};
const model = {};
const view = {};
@ -26,35 +27,39 @@ function generateTriangles()
const _y = (i / 2 - _x) / settings.model.x;
const angle = lerp(0, Math.PI * 2, rand());
const magnitude = lerp(0, settings.model.jitter, rand());
const magnitude = lerp(0, settings.model.jitter, 1);
grid[i ] = _x + Math.sin(angle) * magnitude;
grid[i + 1] = _y + Math.cos(angle) * magnitude;
}
model.delaunay = new Delaunator(grid);
//model.voronoi = model.delaunay.voronoi([-1, -1, settings.model.x + 1, settings.model.y + 1]);
//model.cells = [];
/*console.group("Cells");
let cell = model.voronoi.cellPolygons();
for(let i = 0, n = (model.delaunay.points.length / 2); i < n; i++)
{
const cells = model.voronoi._cell(i);
const val = cell.next().value.flat();
console.log(val, cells);
model.cells.push(...val);
}
console.groupEnd("Cells");*/
}
function generateVoronoi()
function relax(delaunay)
{
function circumcenter(a, b, c)
delaunay.coords = new Float32Array(delaunay.coords.length);
//For each voronoi cells, get the centroid
for(let i = 0; i < delaunay.trianglesLen; i++)
{
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])),
];
}
const coords = model.delaunay.coords;
const tri = model.delaunay.triangles;
for(let i = model.delaunay.trianglesLen - 1; i >= 0; --i)
{
tri[]
}
delaunay.coords =
}
function getDelaunay()
@ -69,8 +74,18 @@ function getAll()
//Settings view contains width & height
function lerpToViewport()
{
const size = settings.model.x * settings.model.y;
/*const centers = model.voronoi.circumcenters;
const centerSize = centers.length / 2;
view.centers = new Float32Array(centerSize * 3);
for(let i = centerSize - 1; i >= 0; --i)
{
view.centers[i * 3 ] = lerp(0, settings.view.width , inverse_lerp(0, settings.model.x - 1, centers[i * 2 ]));
view.centers[i * 3 + 1] = lerp(0, settings.view.height, inverse_lerp(0, settings.model.y - 1, centers[i * 2 + 1]));
view.centers[i * 3 + 2] = -2;
}*/
const grid = model.delaunay.coords;
const size = grid.length / 2;
view.grid = new Float32Array(size * 3);
for(let i = size - 1; i >= 0; --i)
{
@ -79,7 +94,7 @@ function lerpToViewport()
view.grid[i * 3 + 2] = -3;
}
return { position: view.grid, vertices: model.delaunay.triangles };
return { position: view.grid/*, centers: view.centers*/, vertices: model.delaunay.triangles };
}
//Process.register is the equivalent of export