JavaScript version of mapgen2 polygon map generator algorithms
Switch branches/tags
Nothing to show
Clone or download
redblobgames Made recursive subdivision code easier to read
* Removed unnecessary arrow function
* Named the recursive call "subdivide"
Latest commit 29cbcfd Jun 18, 2018

JavaScript version of my Polygon Map Generator.

  • Most of the algorithms are reimplemented rather than ported, so there are some minor differences, but it mostly follows what’s described on this page.
  • The data structures are rather different. The mesh connectivity is separate from the generated map (elevation, rivers, biomes, etc.). The original project uses an “array of struct” approach whereas this one uses a “struct of arrays” approach (see explanation).
  • The naming convention for the data is x_property_y where x and y are r, s, or t indicating the type of the input (x) and output (y). For example, t_downslope_s would be an array indexed by a t (triangle) id, and returning an s (side) id.
  • The maps are created with 0 ≤ x ≤ 1000, 0 ≤ y ≤ 1000.

This repository contains the map generation algorithms but not the code for UI or rendering.


If your game needs polygon data:

let polygons = [];
for (let r = 0; r < map.mesh.numSolidRegions; r++) {
       biome: map.r_biome[r],
       vertices: map.mesh.r_circulate_t([], r)
                         .map((t) => map.t_pos([], t))

If you want the noisy edges instead, see map.s_lines[s] for the line segments that should be used instead of side s.

If your game needs the polygons split into triangles:

let triangles = [];
for (let s = 0; s < map.mesh.numSolidSides; s++) {
    let r = map.mesh.s_begin_r(s),
        t1 = map.mesh.s_inner_t(s),
        t2 = map.mesh.s_outer_t(s);
       biome: map.r_biome[r],
       indices: [
          map.r_pos([], r),
          map.t_pos([], t1),
          map.t_pos([], t2),

If your game needs the polygons split into tiles:

The map coordinates are 0 to 1000. Scale these to the desired size of the map. Use a polygon rasterization library like points-in-polygon to get a list of tiles for each polygon. I have not tried this yet.