mirror of
https://github.com/Azgaar/Fantasy-Map-Generator.git
synced 2025-12-18 18:11:24 +01:00
refactor(es modules): split layers to modules
This commit is contained in:
parent
05564ef5d9
commit
7755d8b588
37 changed files with 2035 additions and 1998 deletions
62
src/layers/renderers/drawBiomes.js
Normal file
62
src/layers/renderers/drawBiomes.js
Normal file
|
|
@ -0,0 +1,62 @@
|
|||
import {clipPoly} from "/src/utils/lineUtils";
|
||||
|
||||
export function drawBiomes() {
|
||||
TIME && console.time("drawBiomes");
|
||||
biomes.selectAll("path").remove();
|
||||
|
||||
const {cells, vertices} = pack;
|
||||
const n = cells.i.length;
|
||||
const used = new Uint8Array(cells.i.length);
|
||||
const paths = new Array(biomesData.i.length).fill("");
|
||||
|
||||
for (const i of cells.i) {
|
||||
if (!cells.biome[i]) continue; // no need to mark marine biome (liquid water)
|
||||
if (used[i]) continue; // already marked
|
||||
const b = cells.biome[i];
|
||||
const onborder = cells.c[i].some(n => cells.biome[n] !== b);
|
||||
if (!onborder) continue;
|
||||
const edgeVerticle = cells.v[i].find(v => vertices.c[v].some(i => cells.biome[i] !== b));
|
||||
const chain = connectVertices(edgeVerticle, b);
|
||||
if (chain.length < 3) continue;
|
||||
const points = clipPoly(
|
||||
chain.map(v => vertices.p[v]),
|
||||
1
|
||||
);
|
||||
paths[b] += "M" + points.join("L") + "Z";
|
||||
}
|
||||
|
||||
paths.forEach(function (d, i) {
|
||||
if (d.length < 10) return;
|
||||
biomes
|
||||
.append("path")
|
||||
.attr("d", d)
|
||||
.attr("fill", biomesData.color[i])
|
||||
.attr("stroke", biomesData.color[i])
|
||||
.attr("id", "biome" + i);
|
||||
});
|
||||
|
||||
// connect vertices to chain
|
||||
function connectVertices(start, b) {
|
||||
const chain = []; // vertices chain to form a path
|
||||
for (let i = 0, current = start; i === 0 || (current !== start && i < 20000); i++) {
|
||||
const prev = chain[chain.length - 1]; // previous vertex in chain
|
||||
chain.push(current); // add current vertex to sequence
|
||||
const c = vertices.c[current]; // cells adjacent to vertex
|
||||
c.filter(c => cells.biome[c] === b).forEach(c => (used[c] = 1));
|
||||
const c0 = c[0] >= n || cells.biome[c[0]] !== b;
|
||||
const c1 = c[1] >= n || cells.biome[c[1]] !== b;
|
||||
const c2 = c[2] >= n || cells.biome[c[2]] !== b;
|
||||
const v = vertices.v[current]; // neighboring vertices
|
||||
if (v[0] !== prev && c0 !== c1) current = v[0];
|
||||
else if (v[1] !== prev && c1 !== c2) current = v[1];
|
||||
else if (v[2] !== prev && c0 !== c2) current = v[2];
|
||||
if (current === chain[chain.length - 1]) {
|
||||
ERROR && console.error("Next vertex is not found");
|
||||
break;
|
||||
}
|
||||
}
|
||||
return chain;
|
||||
}
|
||||
|
||||
TIME && console.timeEnd("drawBiomes");
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue