dag-core.js  ·  kit-core.js  ·  scene-core.js  ·  universe-core.js  ·  interactive companion

DAGs & Kits

The kit world is 65,536 voxels on a side and holds 3.4 trillion solid voxels. It is stored in fewer nodes than a 100×100 thumbnail has pixels, and built in about fifty milliseconds. This page shows why: first the DAG, a tree that never stores the same subtree twice, and then the kit, a way of making content that gives the DAG almost nothing new to store.

65,536³
world side, D = 16
3.45 T
solid voxels
6,990
DAG nodes
371
Oh classes
48 ms
to build
descriptor kitDesc({ seed, D, libs, ornaments, ground, colourLevel }) ↓ kitPlan every choice is a hash of (seed, level, entry) 0.01–0.2 ms ↓ buildKit compose bottom-up: intern, transform, prune ~50 ms DAG node = (level, 8 child ids), never stored twice D = 4 + 2L
01

One node, many places

An octree splits space in half on every axis, again and again, until it reaches single voxels. Stored as a tree, every split costs a node, so a world with a billion voxels of surface costs about a billion nodes. Hash-consing stores the tree differently. A node is its key: its level and the ids of its eight children. Before a node is created, the store looks the key up. If that exact subtree already exists, its id is reused. The tree becomes a directed acyclic graph, where every repeat is a pointer.

The code is three-dimensional, with eight children a node. To make it drawable, the demo below is the same algorithm in two dimensions: a quadtree, four children a node, on a 32×32 grid. Paint on the grid (click and drag). On the right is the store: every unique node, level by level, with the number of places it appears.

The grid · paint to edit

The store · unique nodes, top level first

tree nodes (SVO) unique nodes (DAG) ×n = places it appears

Try Sierpiński. Its DAG has exactly one node a level, because each level is three copies of the level below and one empty quadrant. A 65,536³ version would still be one node a level. That is ifsandwhats.md §1's claim, and step 2 measured it on real content: a hash-consed voxel DAG is an iterated function system. Noise is the opposite case. It shares nodes only at the bottom, because a 2×2 block has just 15 non-empty patterns, and above that nothing repeats.

The real store (createStore in dag-core.js) works the same way with 9-word keys, [level, c0 … c7], in an open-addressing table. EMPTY = 0 is shared by every level and never stored. FULL = 1 is a solid voxel only. A solid 16³ block is an ordinary node, one per level, because special-casing it would cost a branch in every walk. Step 2 found that on block-showroom specimens, plain hash-consing is a bottom-of-the-tree effect: 2³ nodes collapse 3,605×, and above 32³ almost nothing repeats (hashcons-findings.md §2). The kit is built so that things repeat at every level.

02

The store only grows

Ids are handed out in creation order, and a node can only be interned once its children exist. So a child's id is always lower than its parent's. dag-core.js relies on that everywhere. createClasses and createCounts make one pass up the ids. census walks them down. prune marks what the roots reach in a single sweep from the top id to the bottom. It needs no stack, because by the time the sweep reaches a node, every node that could keep it has already been seen.

The store is append-only. Every edit you made above interned new nodes, and the old ones stayed behind. Below is every id the demo's store holds, coloured by level. Hollow cells are dead, meaning nothing reachable from the current root points to them. Hover over an id to see its children, which are always to the left. Then prune.

Real builds leave a lot behind. In the lattice at R = 5, the pieces that graft unions were 54–75% of the store. A kit leaves almost nothing: 6,270 interned and 6,261 kept at D = 14, because it is composed directly, with no unions. The prune still matters, because buildKit makes every library entry, and an entry no one picked is dropped. Every chunk worker gets a copy of the store, so dead nodes cost memory several times over.

Editing grows the store the same way. In the viewer a block, a carve and a drop are ops in one log (log-core.js), and each one interns the nodes its change needs into the live store and moves the root. Nothing is overwritten, so the old root is still whole, which is what an undo goes back to. That is also why a worker can follow an edit cheaply. Ids only grow, so the worker needs just the new end of the keys: a tail, under 1 KB a click on clay and terrain, against a whole snapshot (79 MB for the D = 12 caves). The tail goes on the same port as the jobs, before any job can name the new root, so a job never runs against nodes from its future (createTailer, stream-core.js). The dead nodes an edit leaves are pruned where a world leaves the page: a save writes only what its root reaches (flatten-core.js, vdag-core.js), and a placement prunes the child's keys before interning them (importSteps, §9).

03

Turned copies

Hash-consing finds identical subtrees. A part that has been turned is not identical. Every node below the turn is new, all the way down to the voxels. The kit turns parts at every level, so in the plain DAG that is where it pays. The cure is to label the edges. Each pointer carries a transform, the store keeps one representative per class (nodes equal up to that transform), and a reader composes the labels as it descends.

Below, sixteen copies of one motif are placed, each turned by a hash. Compare three counts: the plain DAG, classes under the mirrors only (SSVDAG's choice, which is 8 mirrors in 3D and 4 here), and classes under the whole symmetry group (Oh, 48 transforms in 3D; D4, 8 here).

Sixteen placed copies

Nodes per level, three ways

levelsideplain DAGmirror classesfull-group classes

In 3D the kit sketch measured the same effect. With no turns, the D = 12 struct kit is 715 nodes, and with turns it is 6,273, 8.8× as many. Under 48 labels it is 325 classes against 326, so turning every part costs one class. Under SSVDAG's 8 mirrors it is 392 against 1,622, because mirrors can't express a rotation (kit-plan.md §1).

What a label costs is in the reader, not the storage. A 6-bit label fits in the pointer. A traversal keeps a running transform g, one byte, and does two table lookups per step:

// descend through an edge labelled `label` toward the octant you want, o
g      = MUL[g * 48 + label];        // compose: 48×48 table
stored = OCT[INV[g] * 8 + o];        // which stored octant holds it: 48×8 table

Mirrors alone reduce both lookups to XORs, which is why the SSVDAG paper stopped there. The DAG the chunk workers walk is still the plain one. createClasses measures what labels would save, and labels-and-bricks.md sets out the trade.

04

Two binary levels make a 4³ slot grid

A kit assembly is a 4×4×4 grid of slots, and each slot holds a whole entry from the level below. The DAG is binary, so 4³ is two levels of it: eight interns of 2³ slots, then one intern of those eight. There is no graft and no union, only intern. This is the whole of buildKit's composition:

// g[s]: the 64 slot ids, s = x + 4y + 16z.  One level-(k+2) node from them.
for (let O = 0; O < 8; O++){
  for (let o = 0; o < 8; o++) ch[o] = g[(2*(O&1) + (o&1)) + 4*(2*(O>>1&1) + (o>>1&1)) + 16*(2*(O>>2) + (o>>2))];
  top[O] = store.intern(k + 1, ch);
}
return store.intern(k + 2, top);

The 64 slots, as four z-slices · hover a slot

The same slots as two binary levels

The high bit of each coordinate picks the coarse octant O, and the low bit picks the fine one o. Read together, d = 8·O + o is the slot's Morton digit, the same octant-major order the radix-4 port settled on (radix4-addressing.md). That is why a 4³ level costs nothing extra. It was already two binary levels with a convenient name.

05

The library

A kit is nested 4³ assemblies over a small library. Level 0 is a library of 16³ parts: eight structural ones and, by default, four block-showroom specimens as ornaments. Each level above is a library of assemblies. An assembly picks a mask (which of its 64 slots are filled) and, for each filled slot, an entry of the level below and one of the 24 rotations. The top level has one entry, which is the world. Since parts are 2⁴ on a side and each level is two binary levels, D = 4 + 2L. The smallest kit, D = 6, is one level: a single 64³ assembly of parts standing on its floors. The viewer opens the kit at D = 8 (two levels, in level-1 colour frames), and the endless lattice's kits are D = 8 too. D = 14 is kitDesc's default, which the experiments and this page's numbers use.

Level 0 · the parts, drawn from kit-core.js's own PARTS, coloured as colour level 0 would

Level 0 · ornaments, ornamentOcc(n): block-showroom specimens at R = 16, one library for every seed

Masks · which of an assembly's 4³ slots are filled (MASKS)

Which masks an entry may pick depends on its level. That choice is the main control on the look:

wherefamilywhy
level 1frame, cross, arches, pillars, slabsdetailed: these are 64³ objects
level 2tower, pillars, frame, archestall: 256³ buildings
levels 3+frame, pillars, crossopen, so there's room to fly in
level L − 1, ground ontower, pillars, arches, framenothing hangs from nothing
the top, ground onpillars, court, skylinegrounded: the world stands on a floor
grounded entriespillars, slabs, court, skylineevery one fills its whole bottom layer

The ground is built as nested floors. Half of each level's library (rounded up) is grounded. A grounded entry fills its bottom layer, and each of those 16 slots holds a grounded entry from the level below, turned only about y (Y_TURNS, 4 of the 24). This bottoms out at the block, the slab and the step, all solid for y < 4. So a grounded world is solid in its bottom 4 voxels from edge to edge, and anything that stands on it stands on floors at every scale. The top two levels also place their children upright, so the large structure stands up. Below them, every slot keeps all 24 turns.

The first kitPlan marked a floor slot with s < 16 && y === 0. Since s = x + 4y + 16z, that is the z = 0 row only, so 4 of the 16 bottom slots were floors and the ground had holes. The dense stamp passed, because it stamps whatever the plan says. A separate test of the ground's promise caught it (kit-log.md §2). A stamp checks the composition, not the plan's promises.

06

The kit lab

This lab builds a real kit, in your browser, with the repo's own kitDesc, kitPlan and buildKit. It ray-casts the DAG on the CPU, a port of experiments/kit-sketch.mjs's tracer, and colours each hit with the kit's own kitColour. Every number below the picture is measured on this build, not copied. Drag to look around, scroll to move, and hold shift to move ×8. Hover over the picture to see the voxel's path through the plan, which is the same walk the colour field makes in each chunk worker. Far views sparkle: this is one ray a pixel at full detail, where the viewer would draw coarse lods with averaged colour.

This voxel's path through the plan

Hover over the picture.

The plan · every entry's mask (underlined: grounded)

The plan is a few typed arrays: per level, per entry, 64 slots of (child index, turn). It is rebuilt from the descriptor in a fraction of a millisecond, so nothing but the descriptor ever needs to be sent anywhere. A chunk worker gets { kind: 'kit', … } and rebuilds the colour field itself. The draws come in a fixed order (the mask, a skyline's heights, then each filled slot's child and turn), so adding one draw anywhere moves every later choice. That is why the D = 10 kit is pinned to 76,250,785 voxels and 3,092 nodes, in Node (kit-core.test.js, and vdag-core.test.js's fixture) and in the browser (test/browser/stream.test.js, the viewer's own build). The same descriptor names a kit in a universe: kit:5 in the endless lattice is kitDesc({ D: 8, seed: 5 }), built by every world worker from those few numbers (§10).

07

What a kit costs

The node count is a library bill. A turned entry is a new node all the way down, but it is hash-consed too: every other copy with the same turn shares all of it. So the store is bounded by roughly 24 × (the parts' nodes + 9 a library entry), whatever D is. Deeper kits add a level of library and almost no nodes. Below, the lab's current kit is rebuilt four ways, and each build gets its classes counted.

variantbuiltnodesOh classes (48)mirror classes (8) voxelsbuild ms
…

What the measured tables say (kit-findings.md §1):

D = 14 kits, nodes against library entries (measured, docs/kit-results.md)

Table view
librariesentriesnodesOh classesvoxelsbuild ms

A cache of copies would not pay. In the street views at D = 14 and 16, only 58–61% of the cut's chunks are distinct (lod, node) pairs, so about 40% are copies. But 94–97% of the chunk jobs' outputs are distinct, because a chunk's records also depend on its neighbours (the apron hides faces, and the seams open where a neighbour isn't drawn), and a kit's copies rarely have the same neighbours. The DAG shares the content. The views of it are still mostly unique.

08

Colour frames, and why far views go grey

The kit's colour is a stand-in you chose: the gamut of a voxel's position in its frame. The frame is the level-colourLevel entry the voxel lies in (level 2 by default, the 256³ buildings), turned with it. A turned copy shows the same colours, turned, so a repeat shows up as a repeated patch of colour. The demo below is a 2D slice of that: a grid of frames, each turned, and next to it what a coarse lod draws, the mean colour of each cell.

Per voxel

Per coarse cell (mean)

When a coarse cell is as large as a frame, the cell averages the frame's whole gamut, and the mean of a gamut is grey. From outside, the D = 14 kit's cut is 17 leaves at lods 6–9, which means cells of 64–512 voxels against 256³ frames. The opening view was confetti at D = 14 and grey at D = 16 (kit-findings.md §2). Nothing is broken. It is what a gamut looks like at a scale coarser than its frame. You picked (c), a two-scale colour: the fine gamut blended with a coarse one, read in level L − 1's frames. It is decided but not built yet. The "two-scale" chip shows roughly what it would do.

A walk through the plan for every point made colour most of a kit job, 30 ms per chunk against 6.4 ms without colour. The fix rests on one fact: level-c entries sit on a world-aligned grid, and a turn about a child's centre maps aligned cells to aligned cells. So each world cell of a frame's side is one affine map, a centre plus a signed permutation, read off four walks and cached. A cache hit costs nine multiply-adds, and since every value is an integer or a half, the cached map gives exactly the walk's answer (kit-log.md §4).

09

Placed copies: where the grid still counts

The kit is built so that every copy lands on the grid. A slot of a level-k assembly is a level-(2k + 4) node, so a copy of an entry is that entry's node, and costs nothing. Placing a whole world inside another one by hand (a knight in a kit, a drop in edit mode) has no such luck. scene-core's bakeScene brings the child into the parent's store, turns it with store.transform, and places it top down. A parent node that lands exactly on a child node is that node; only the nodes the offset cuts are new. Then it unions the result in. So the question for a second copy is how many of its nodes the first copy already made.

Below, the real bakeScene puts two copies of a D = 8 kit (an endless-lattice cell's world, kit:1) into an empty D = 10 world, all in one store, and counts nodes by level. The second copy is moved Δ voxels along x from the first and turned.

Nodes by level

levelsidefirst copysecond, not in the firstnew in the scene
…

The same copy as an instance


        

What it shows, and what scenegraph.md §11 measured with the knight:

Labelled edges would make the turn free, and nothing has them yet. Drawing the copy as an instance does it today, for every case at once. The world keeps its own DAG in its own frame, and the copy is a transform: a whole-voxel offset, one of the 48 turns and a power-of-two scale, which the vertex shader applies as a 3×3 matrix (turnMatrix). That costs no nodes, whatever Δ and the turn. The price is that an instance isn't part of the parent's DAG: a ray or an edit has to ask each world in its own frame (compositeWorld). That trade is what decided the universe (§10). Baking stays the way to make a placement part of a world: a drop in edit mode is a bake.

10

Universes, and how far is endless

When this page was first written, a universe of many worlds was a proposal (universekit.md): a universe tree whose top levels would be computed from a hash and never stored, with whole worlds as its leaves at level 16, all in one DAG over a shared base store. What was built (universe-plan.md, 2026-11-19) takes the other road, for §9's reason. Each world keeps its own DAG, in its own frame, and is drawn as an instance: { world, at, turn, scale }, a whole-voxel corner, one of the 48 turns and a power-of-two scale. Nothing is baked into a universe DAG, so a turned or off-grid copy costs no nodes. A world drawn many times is built once.

the plane instances kit:4182upright kit:4182turned 90° kit:4183at ×2 city:77upright city:77turned 180° flat:8park flat:8park worlds one store each kit:4182, D = 8~1,000 nodes kit:4183, D = 8~1,000 nodes city:77, D = 9built in a worker flat:88 nodes, every gap chunk cache keyed by (world, chunk): two instances of kit:4182 share every chunk; the shader turns them (uTurn) one cut over the forest (selectForest): each instance's tree cut at the eye mapped into its frame, one heap, one budget

A world is still named, not shipped, as the kit always was: kit:4182 is kitDesc({ D: 8, seed: 4182 }), and a world worker builds it from that. The endless lattice (endless:<seed>) takes this as far as it goes. The plane is cut into cells of 512, and which world a cell holds is a function of the seed and the cell's (i, j) alone (cellOf). Districts come from a slow field (districtOf), and a district's cells draw their worlds from a small pool of seeds, each turned about y. So a world built once is drawn by many cells, which is instancing doing at the scale of worlds what hash-consing does at the scale of nodes. The map below runs the real universe-core.js. Hover over a cell to see its world and every other cell that draws the same one, and click one to build it.

The page shows a window of this: the 69 cells within 4 of the cell you're over (endlessWindow), laid out on one plane at one height, with every grounded world's datum lined up. When you fly past the centre cell's edge by 64 voxels, the window re-centres and the page moves every position it holds by the same whole number of voxels (shiftOrigin). Whole numbers are exact in float64, so nothing on the screen moves, and no coordinate the page holds is ever more than 4,608 across, however far you fly. Worlds are built in two world workers, nearest first. Built worlds stay on the main thread under 96 MB, and the chunk workers hold only the window's. The cut over the forest is the LOD explainer's §2, and the renderer explainer's §11 draws a fixed universe from above.

So "how far is endless" stopped being a question about depth. A world's depth still matters, for what one world can be, and this ruler shows where each depth meets the engine. Slide D.

What isn't built: layers, a shared base store under worlds of several seeds (universe-plan Q7). Today each world worker holds a snapshot of every world the window wants, and no two worlds share a node. The kit's ornament library is one library for every seed already, so it would be the first layer.

11

The rules, and where to read

Each has a test behind it, and most are a sentence in CLAUDE.md. They're the finer points of the sections above, in the form you'd trip over them.

rulewhyheld by
ids are interned children-firstcreateClasses, census and prune walk ids in order on the strength of it; union, graft, importKeys and prune keep it, like every builder (§2)dag-core.test.js: importKeys "children first and merged", prune "in order"; kit-core.test.js: "ids are children-first"
never compare a count to 8level: ask isFullcounts are float64, and past level 17 a node one voxel short of full has the full count (§10's ruler)edit-core.test.js: "isFull against exact counts", where a job once differed from expandChunk; export-core.test.js at D = 20
a kit is its descriptorthe plan is rebuilt from a few numbers anywhere, so nothing else is sent to a worker, and kit:5 names a world (§6, §10)kit-core.test.js: "the plan is its descriptor"; the D = 10 pin there, in vdag-core.test.js and in the browser's stream.test.js
a kit's draws come in a fixed orderadding a draw anywhere moves every later choice, and so every kit (§6)kit-core.test.js: "the default kits are unchanged by the share and the library", "the ornament share: one draw"
a stamp checks the composition, not the plan's promisesthe first floors had holes and the dense stamp passed (§5)kit-core.test.js: "a grounded world is solid for y < 4, edge to edge", "the plan: floors, upright top levels"
specimen-core.js is a verbatim portthe ornaments are block-showroom's specimens; goldens come only from builds that equal the sibling's (§5)specimen-core.test.js: the goldens, and byte for byte against the sibling when it's beside the repo
an aligned copy costs only the levels above its alignmenta level-k node of a copy is the child cut at Δ mod 2k (§9)scene-core.test.js: "an aligned copy costs only the levels above it"; this page, live
a placement's two forms hold each otherbakeScene and compositeWorld share nothing but boxHits and child(), so each checks the other (§9)scene-core.test.js: "every bake is the dense stamp, and the composite answers as the bake does", and its mutants
an instance draws its bake's frameinstances that a bake could also make must draw the bake's frame, byte for byte; one plain instance draws the plain world's (§10)the browser's universe.test.js: "instances as their bake, one as the plain world"
a cell's world is a function of (seed, i, j)the same seed makes the same universe whichever way you fly in; the origin moves by whole voxels, so nothing on screen does (§10)universe-core.test.js: "the endless lattice", 63 mutants; endlessSelftest: a frame unchanged by the origin, byte for byte
a tail reaches a worker before any job naming its rooton the jobs' own port, in order; anywhere else a job could read a table from its future (§2)stream-core.test.js: "a runner fed tails equals a runner given a fresh snapshot", for blocks and drops too

Reading order for the code

Line numbers are looked up as the page loads, so they're right for the files you're serving. Click one to copy file:line.

#wherewhat to read for
1intern: a node is its key, looked up before it's made (§1); and the store's other builders.
2One sweep from the top id down, with no stack (§2).
3Oh classes under a group of transforms: what labelled edges would save (§3).
4 A count a node, and the exact full test past level 17.
5The plan: masks, children and turns, drawn in a fixed order; floors and upright levels (§5, §6).
6Composition by intern alone, then the prune (§4).
7Colour frames, and the affine map cached per frame cell (§8).
8 A placement baked into the parent's DAG, and the same placement asked in its own frame (§9).
9 An instance's frame and the 3×3 the shader turns it by (§9, §10).
10 Districts, pools of seeds, the window and the rolling origin (§10).
11Why a tail is enough to follow an edit, and why its order matters (§2).
12 The measurements behind §7, §9 and §10.

What this page leaves out