---
id: "bsp-dungeon-generation"
title: "BSP Dungeon Generation"
aliases: [binary space partition dungeon, room-and-corridor generation]
domain: "procgen"
stage: "procedural"
realtime: "realtime"
platforms: [cpu, python, unity, unreal, godot]
year_introduced: 1998
outputs: [layout, graph]
style: "stylized"
dynamic: "static"
cost: "cheap"
authority: "RogueBasin"
url: "https://texelatlas.vercel.app/t/bsp-dungeon-generation/"
sources_verified_at: "2026-11-29"
---
# BSP Dungeon Generation (RogueBasin)

> 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.

## 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

## 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 use it

Choose it for roguelikes, level blockout and room layouts where guaranteed connectivity and no overlap are required. Prefer [Wave Function Collapse](https://texelatlas.vercel.app/t/wave-function-collapse.md) when you want an example-driven look with local tile adjacency, [MarkovJunior (Rewrite-Rule Generation)](https://texelatlas.vercel.app/t/markov-junior-rewrite-rules.md) for organic cave or maze growth, and [MAP-Elites (Quality-Diversity Search)](https://texelatlas.vercel.app/t/map-elites.md) to explore many parameter settings along descriptors like room count or loops.

## Cost

cheap — Linear in number of leaves; microseconds to milliseconds.
Measured GPU Weight: 7.96× baseline (moderate) — 0.2619 ms median / 0.2686 ms p95 on Tesla T4/PCIe/SSE2 @ 1920x1080 (opengl-timer-query).
Ada L4 scaling: 6.97× baseline (moderate) — 0.1108 ms median (2.36× faster on L4).
Ampere A100 scaling: 5.1× baseline (light) — 0.147 ms median (1.78× faster on A100).

## Failure modes

- 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

## Dataflow

- Inputs: —
- Outputs: layout, graph
- Platforms: cpu, python, unity, unreal, godot

## Presets

### BSP dungeon plate: recursive splits, one room per leaf, L-shaped corridors between siblings (glsl-fragment; rendered 2026-11-29)

```json
{
  "DEPTH": 4,
  "SEED": 3,
  "CORRIDOR_W": 0.009
}
```

```glsl
// 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

## Sources

1. [primary] RogueBasin Basic BSP Dungeon generation. roguebasin.com. https://www.roguebasin.com/index.php/Basic_BSP_Dungeon_generation (verified via http on 2026-11-29)

## Cite this entry

Texel. "BSP Dungeon Generation." https://texelatlas.vercel.app/t/bsp-dungeon-generation/ (accessed 2026-12-01).
