BSP Dungeon Generation
Recursively split a rectangle into smaller rectangles, place a room in each leaf and connect sibling subtrees with corridors, which gives connected room-and-corridor maps with no overlap.
Also known as binary space partition dungeon, room-and-corridor generation
In the family Procedural generation, layouts
Where it runs
Built inOfficial add-onPluginNone foundNot researched
| Engine | Support | How |
|---|---|---|
| three.js r187 | None found | |
| Babylon.js 9.38.0 | None found | |
| Godot 4.7 | None found | No built-in procedural BSP dungeon generator. |
| Unity | None found | |
| Unreal 5.7 | None found | |
| Blender 5.2 | None found | |
| WebGPU W3C | None found | |
| Metal | None found |
How it works
Start with the full map rectangle as the root. Recursively split a node horizontally or vertically (biased by aspect ratio) at a random ratio until the leaves reach a minimum size. In each leaf, carve a room with random size and offset that stays inside the leaf with a margin. Walk back up the tree: for each internal node, connect a room from the left subtree to one in the right subtree with an L- or Z-shaped corridor. Since every sibling pair is joined, the whole dungeon is connected, and the tree gives a natural hierarchy for placing keys, stairs and boss rooms. Extra loops can be added by connecting random non-sibling rooms.
When to choose it
Reach for it when
- generate roguelike dungeon rooms that never overlap
- need random room layouts connected by corridors
- procedurally partition a map into rooms
- want a simple dungeon generator with guaranteed connectivity
Choose it for roguelikes, level blockout and room layouts where guaranteed connectivity and no overlap are required. Prefer Wave Function Collapse when you want an example-driven look with local tile adjacency, MarkovJunior (Rewrite-Rule Generation) for organic cave or maze growth, and MAP-Elites (Quality-Diversity Search) to explore many parameter settings along descriptors like room count or loops.
Field marks
- Boxy, grid-aligned look with rooms resembling each other
- Pure tree connectivity makes dead-end rooms with no loops, so navigation is linear
- Corridors can cross unrelated rooms unless routed with collision checks
- Split ratios near 0 or 1 create tiny leaves that cannot fit a room, leaving sparse regions
Presets
BSP dungeon plate: recursive splits, one room per leaf, L-shaped corridors between siblings
{
"DEPTH": 4,
"SEED": 3,
"CORRIDOR_W": 0.009
}Show GLSL (49 lines)
// BSP dungeon: recursive rectangle split, one room per leaf, corridors join sibling subtrees.
const int DEPTH = 4;
const float SEED = 3.0;
const float CORRIDOR_W = 0.009;
float h(vec2 p) { return fract(sin(dot(p, vec2(127.1, 311.7)) + SEED) * 43758.5453); }
void splitR(vec2 lo, vec2 hi, int i, out bool v, out float c) {
vec2 s = hi - lo; v = s.x > s.y;
float r = 0.4 + 0.2 * h(lo * 7.0 + float(i));
c = v ? lo.x + s.x * r : lo.y + s.y * r;
}
vec2 leafC(vec2 lo, vec2 hi, int i) {
for (int k = 0; k < DEPTH; k++) {
if (k < i) continue;
bool v; float c; splitR(lo, hi, k, v, c);
if (v) hi.x = c; else hi.y = c;
}
return 0.5 * (lo + hi);
}
float segd(vec2 p, vec2 a, vec2 b) {
vec2 pa = p - a, ba = b - a;
return length(pa - ba * clamp(dot(pa, ba) / max(dot(ba, ba), 1e-6), 0.0, 1.0));
}
void mainImage(out vec4 fragColor, in vec2 fragCoord) {
vec2 uv = fragCoord / iResolution.xy;
vec2 lo = vec2(0.03), hi = vec2(0.97);
float cor = 1e9, split = 1e9;
for (int i = 0; i < DEPTH; i++) {
bool v; float c; splitR(lo, hi, i, v, c);
vec2 loA = lo, hiA = hi, loB = lo, hiB = hi;
if (v) { hiA.x = c; loB.x = c; } else { hiA.y = c; loB.y = c; }
vec2 a = leafC(loA, hiA, i + 1), b = leafC(loB, hiB, i + 1);
vec2 m = vec2(b.x, a.y);
cor = min(cor, min(segd(uv, a, m), segd(uv, m, b)));
split = min(split, v ? abs(uv.x - c) : abs(uv.y - c));
bool up = v ? uv.x > c : uv.y > c;
if (up) { lo = loB; hi = hiB; } else { lo = loA; hi = hiA; }
}
vec3 col = vec3(0.07, 0.08, 0.11) + 0.05 * h(floor(lo * 40.0));
vec2 ctr = 0.5 * (lo + hi), hs = (hi - lo) * (0.28 + 0.14 * h(lo * 3.0 + 1.0));
vec2 q = abs(uv - ctr) - hs;
float room = max(q.x, q.y);
vec3 rc = mix(vec3(0.75, 0.55, 0.30), vec3(0.45, 0.62, 0.70), h(lo * 5.0));
col = mix(col, vec3(0.35, 0.37, 0.42), smoothstep(0.004, 0.0, split - 0.002) * 0.6);
col = mix(col, vec3(0.55, 0.50, 0.42), smoothstep(CORRIDOR_W, CORRIDOR_W - 0.003, cor));
col = mix(col, rc * (0.85 + 0.15 * sin(uv.x * 200.0) * sin(uv.y * 200.0)), smoothstep(0.003, -0.003, room));
col = mix(col, vec3(0.1, 0.08, 0.06), smoothstep(0.004, 0.0, abs(room)) * 0.9);
fragColor = vec4(pow(col, vec3(0.8)), 1.0);
}
Attribution: written for this index
Measured GPU weight & timing
| Preset | GPU | Weight / p50 / p95 | Method |
|---|---|---|---|
| bsp-dungeon-generation-specimen | Tesla T4 1920x1080 | 7.96× baseline (moderate) · 0.262 ms / 0.269 ms | opengl-timer-query · 40 frames · GPU Weight Atlas · Colab T4 sweep |
| bsp-dungeon-generation-specimen | NVIDIA L4 1920x1080 | 6.97× baseline (moderate) · 0.111 ms / 0.111 ms · 2.4× faster on L4 | opengl-timer-query · Cross-architecture validation |
| bsp-dungeon-generation-specimen | NVIDIA A100-SXM4-40GB 1920x1080 | 5.10× baseline (light) · 0.147 ms / 0.147 ms · 1.8× faster on A100 | opengl-timer-query · Cross-architecture validation |
Sources
- primaryRogueBasin. Basic BSP Dungeon generation. roguebasin.com.Verified via live page · 2026-11-29