mirror of
https://github.com/Azgaar/Fantasy-Map-Generator.git
synced 2025-12-16 17:31:24 +01:00
v. 0.59.12b
This commit is contained in:
parent
c0d622356b
commit
b1309eae71
1 changed files with 15 additions and 66 deletions
81
script.js
81
script.js
|
|
@ -16,7 +16,7 @@
|
||||||
|
|
||||||
// And particularry:
|
// And particularry:
|
||||||
// Migrate from d3-voronoi to mapbox-delunator or d3-delaunay
|
// Migrate from d3-voronoi to mapbox-delunator or d3-delaunay
|
||||||
// Use typed array instead of array of objects
|
// Use typed arrays instead of array of objects
|
||||||
// Get rid of jQuery as d3.js can almost all the same and more
|
// Get rid of jQuery as d3.js can almost all the same and more
|
||||||
// Re-build UI on reactive approach, vue.js
|
// Re-build UI on reactive approach, vue.js
|
||||||
|
|
||||||
|
|
@ -444,13 +444,8 @@ function fantasyMap() {
|
||||||
console.time("placePoints");
|
console.time("placePoints");
|
||||||
points = [];
|
points = [];
|
||||||
const mod = rn((graphWidth + graphHeight) / 1500, 2); // screen size modifier
|
const mod = rn((graphWidth + graphHeight) / 1500, 2); // screen size modifier
|
||||||
const radius = rn(5.2 * mod / graphSize, 2); // 5.2 is a radius to get 10k cells on 960 x 540 screen and size 1
|
const spacing = rn(7.5 * mod / graphSize, 2); // space between points before jirrering
|
||||||
const sampler = poissonDiscSampler(graphWidth, graphHeight, radius);
|
points = getJitteredGrid(spacing);
|
||||||
while (sample = sampler()) {
|
|
||||||
const x = rn(sample[0], 2);
|
|
||||||
const y = rn(sample[1], 2);
|
|
||||||
points.push([x, y]);
|
|
||||||
}
|
|
||||||
heights = new Uint8Array(points.length);
|
heights = new Uint8Array(points.length);
|
||||||
console.timeEnd("placePoints");
|
console.timeEnd("placePoints");
|
||||||
}
|
}
|
||||||
|
|
@ -6313,66 +6308,20 @@ function fantasyMap() {
|
||||||
console.timeEnd("loadMap");
|
console.timeEnd("loadMap");
|
||||||
}
|
}
|
||||||
|
|
||||||
// Poisson-disc sampling for a points
|
// get square grid with some jirrering
|
||||||
// Source: bl.ocks.org/mbostock/99049112373e12709381; Based on https://www.jasondavies.com/poisson-disc
|
function getJitteredGrid(spacing) {
|
||||||
function poissonDiscSampler(width, height, radius) {
|
const radius = spacing / 2; // square radius
|
||||||
var k = 5, // maximum number of points before rejection
|
const jittering = radius * 0.9; // max deviation
|
||||||
radius2 = radius * radius,
|
const jitter = function() {return Math.random() * 2 * jittering - jittering;}
|
||||||
R = 3 * radius2,
|
let points = [];
|
||||||
cellSize = radius * Math.SQRT1_2,
|
for (let y = radius; y < graphHeight; y += spacing) {
|
||||||
gridWidth = Math.ceil(width / cellSize),
|
for (let x = radius; x < graphWidth; x += spacing) {
|
||||||
gridHeight = Math.ceil(height / cellSize),
|
let xj = rn(x + jitter(), 2);
|
||||||
grid = new Array(gridWidth * gridHeight),
|
let yj = rn(y + jitter(), 2);
|
||||||
queue = [],
|
points.push([xj, yj]);
|
||||||
queueSize = 0,
|
|
||||||
sampleSize = 0;
|
|
||||||
return function() {
|
|
||||||
if (!sampleSize) return sample(Math.random() * width, Math.random() * height);
|
|
||||||
// Pick a random existing sample and remove it from the queue
|
|
||||||
while (queueSize) {
|
|
||||||
var i = Math.random() * queueSize | 0,
|
|
||||||
s = queue[i];
|
|
||||||
// Make a new candidate between [radius, 2 * radius] from the existing sample.
|
|
||||||
for (let j = 0; j < k; ++j) {
|
|
||||||
var a = 2 * Math.PI * Math.random(),
|
|
||||||
r = Math.sqrt(Math.random() * R + radius2),
|
|
||||||
x = s[0] + r * Math.cos(a),
|
|
||||||
y = s[1] + r * Math.sin(a);
|
|
||||||
// Reject candidates that are outside the allowed extent, or closer than 2 * radius to any existing sample
|
|
||||||
if (0 <= x && x < width && 0 <= y && y < height && far(x, y)) return sample(x, y);
|
|
||||||
}
|
|
||||||
queue[i] = queue[--queueSize];
|
|
||||||
queue.length = queueSize;
|
|
||||||
}
|
}
|
||||||
};
|
|
||||||
function far(x, y) {
|
|
||||||
var i = x / cellSize | 0,
|
|
||||||
j = y / cellSize | 0,
|
|
||||||
i0 = Math.max(i - 2, 0),
|
|
||||||
j0 = Math.max(j - 2, 0),
|
|
||||||
i1 = Math.min(i + 3, gridWidth),
|
|
||||||
j1 = Math.min(j + 3, gridHeight);
|
|
||||||
for (j = j0; j < j1; ++j) {
|
|
||||||
var o = j * gridWidth;
|
|
||||||
for (i = i0; i < i1; ++i) {
|
|
||||||
if (s = grid[o + i]) {
|
|
||||||
var s,
|
|
||||||
dx = s[0] - x,
|
|
||||||
dy = s[1] - y;
|
|
||||||
if (dx * dx + dy * dy < radius2) return false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
function sample(x, y) {
|
|
||||||
var s = [x, y];
|
|
||||||
queue.push(s);
|
|
||||||
grid[gridWidth * (y / cellSize | 0) + (x / cellSize | 0)] = s;
|
|
||||||
++sampleSize;
|
|
||||||
++queueSize;
|
|
||||||
return s;
|
|
||||||
}
|
}
|
||||||
|
return points;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Hotkeys, see github.com/Azgaar/Fantasy-Map-Generator/wiki/Hotkeys
|
// Hotkeys, see github.com/Azgaar/Fantasy-Map-Generator/wiki/Hotkeys
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue