# Scene split

`helix scene split` turns a monolithic environment into a Scene v2 package with a small eager spawn core and distance-streamed cells on a fixed grid.
It is the step between "one big shell GLB" (or a Scene document whose shell is one eager group) and an environment that the engine can stream.

```sh
helix scene split ./hyperspace-hq.glb -o ./hyperspace-split --cell-size 8 --core-radius 12
helix scene split ./world/scene.helix-scene.json -o ./world-split          # a Scene v2 document with an eager shell
helix scene split ./hyperspace-hq.glb -o /tmp/plan --dry-run --json         # numbers only, writes nothing
```

The command never publishes and never contacts a service. It writes a fresh folder (an existing, non-empty one is refused).

## Inputs

- A `.glb` (static triangle meshes; skins, animations and morph targets are refused, because baking a transform into them changes their meaning).
  The spawn is the first standable point the importer's spawn probe finds, or `--spawn x,y,z`.
- A Scene v2 document (or a folder with exactly one `*.helix-scene.json`). The eager closure of the chosen profile is examined node by node:
  - a node with a single-level bundle GLB, a `render.mesh` component, collision that is a triangle mesh of its own geometry, and nothing else pointing at it
    (no light, behavior, surface, HLOD entry, parent link) and a footprint wider than one cell is a **monolith**: it is split;
  - a node no wider than a cell keeps its Scene node id, resource and LOD (other code finds nodes by id) and is only re-homed to the cell holding its centre;
  - everything else stays in the spawn core untouched;
  - distance and portal groups, HLOD declarations and their resources, spawns, lights, behaviors and surfaces are carried through verbatim.

## What it writes

```
scene.helix-scene.json      validated Scene v2 (compact), sealed
cells/<sha256>.glb          one chunk per grid cell, content addressed
textures/tex-<sha24>.ktx2   shared textures, content addressed (default --textures ktx2)
assets/...                  resources the document still references (Scene input only)
split-report.json           the measurements below, plus a per-cell table
```

### The grid

Cells are square on the XZ plane, `--cell-size` metres (default 16), anchored at the world origin: cell `(cx, cz)` covers `[cx*size, (cx+1)*size)`.
Chunk geometry is stored in a frame whose origin is the cell corner, so a chunk's bytes depend only on what is inside that cell. Change one prop and only the cells it touches get a
new hash; every other chunk, and its CDN/browser cache entry, is unchanged. Splitting the same input twice is byte-identical (the report omits timings for the same reason).

### How geometry is divided

- A source node no wider than a cell is owned whole by the cell that holds its centre. It keeps its glTF node name, mesh and instancing, so code that finds exhibits or props by name still does.
- A wider node (floors, walls, terrain-like shells) is clipped along the grid planes, triangle by triangle, with attributes interpolated and normals/tangents renormalised.
  Intersections are computed in a canonical edge order, so two triangles sharing an edge produce the identical new vertex on both sides: no cracks, no T-junctions. Vertices on the cut are exact grid coordinates.
- Mirrored transforms flip winding, tangents keep their handedness.
- The report shows triangle conservation (`geometry.sourceTriangles` vs `outputTriangles`) and how many triangles were clipped.

### The spawn core

The core is every cell within `--core-radius` of the spawn (default half the stream radius, with the reason written to `core.justification`), plus any whole node that touches the core disc.
Collision under the spawn is therefore in the core: the command checks that the ground below the spawn is the same height in the source and in the core, and fails otherwise.
The core is one stream group (`mode: eager`); every other cell is its own `distance` group, because the engine charges memory per group and admits distance cells one by one.
Core priority is that of the eager group it replaces (100 by default); streamed cells are 10 below it, so the distance ordering decides what loads first.

### Budgets

Every cell carries `bounds` (tight, in world space), the group carries `priority` and `estimatedMemoryMiB`. The profile's `memoryBudgetMiB` is declared honestly:

```
max( manifest commitment = eager + largest single activation ,
     1.1 x the worst walking window = eager + every group with a cell inside the stream radius, at any stand point )
```

A stand point is a point in 3D: the engine admits a cell by its 3D distance to the camera, so the window sweeps the environment's height too (a cell on another floor counts from a point near it, not from one far below).

rounded up to a multiple of 8, never below 32. A texture shared by several cells is charged once, to the core, and not again to the cells that reuse it. The output is checked with the manifest validator,
including its memory proof, before anything is written.

### Textures and encodings

- Geometry: `EXT_meshopt_compression` with `KHR_mesh_quantization` (16-bit cell-local positions). `--encoding none` writes plain floats.
- Textures, `--textures ktx2` (default): each distinct image is encoded once (ETC1S for colour, UASTC for normals and data), written as `textures/tex-<hash>.ktx2`,
  and chunks reference it by relative URI with `KHR_texture_basisu`. The engine's shared-texture path therefore fetches and uploads each image once.
  `--textures keep` leaves textures as authored and embeds them in every chunk that uses them (large for texture-heavy shells; handy for fast tests). `--texture-cache <dir>` remembers encodes by content.

### Choosing sizes

- **Cell size** (default 16 m): small enough that a cell is a useful unit of loading, large enough that a chunk is not a handful of triangles. A compact interior such as a 60 m office reads better at 8 m
  (63 chunks, 23 KiB median); an outdoor shell at 16 m or 32 m. The cell-count caps are the upper bound on how fine it can get.
- **Core radius** (default half the stream radius): what must be on screen, with collision, before the player can move. Raise it for fast vehicles.
- **Stream radius** (default: the document's, else 2.5 cells): the engine admits every distance cell within this radius of the player and waits for them before the world is playable.
  A shell that fits entirely inside the radius therefore loads whole at spawn: the split then saves bytes through re-encoding (meshopt chunks, shared KTX2 textures) and lets the engine release cells as the player leaves,
  but it defers nothing. When you do not choose a radius and the whole environment fits (at most 256 MiB decoded), the split stretches a shorter default to the environment's *reach*
  (the farthest any cell is, in 3D, from any corner of the environment's box) plus 8 m, with a warning: a radius shorter than the reach leaves far cells unresident from inside the building (walls and floors missing from where you stand).
  A radius you pass is always honoured, with a warning when it is shorter than the reach. Pass a smaller `--stream-radius` to defer the far part (Hyperspace HQ at 20 m instead of 32 m: first playable 5.9 MB instead of 6.0 MB of shell, 0.7 MB streaming in on the walk, resident cells 38 down to 27).

### Far coverage

The command does not synthesise a far view. If the input has an HLOD declaration its clusters point at nodes in distance groups, which are not touched, so they are carried through unchanged.
The report says `farCoverage: none` when there is none; run `helix scene hlod` afterwards to add it.

### Caps

If the grid would exceed the Scene caps (1024 cells, 512 groups, 4 MiB document), an unset `--cell-size` doubles until it fits and the report warns; an explicit one fails with the numbers.

## Shrinking the textures afterwards: `helix scene retexture`

`helix scene retexture <split-dir> -o <dir>` takes a finished split (or any Scene v2 bundle whose textures are shared `tex-<sha24>.ktx2` files that the GLBs reference by name) and changes **only texture bytes**:
it never moves a node, cell, stream group, bound, spawn or budget, and it copies every GLB's binary chunk through byte for byte.

- **Merges textures that decode to the same picture.** `split` shares a texture by its source bytes, so an image a DCC export repeated with different encoder noise is encoded once per copy
  (Hyperspace HQ: five 253 KB metal/rough maps, 0.04-0.1/255 apart). Two textures with the same codec, colour space, size, mip count and role merge when their decoded top mips are at least `--merge-psnr` dB apart (default 44) and no texel differs by more than `--merge-max-delta` (default 24). The smaller file is kept; the GLBs that named the others get their image `uri` rewritten.
- **Re-encodes UASTC data and normal maps with RDO.** Tries the `--uastc-rdo` scalars highest first (default `2,1`) and keeps the first that saves at least 8 percent and stays at least `--reencode-psnr` dB (default 40) from the decoded original on the channels the map uses. Data and normal maps stay UASTC (rule P03 refuses ETC1S there, and its blocks facet a normal map); albedo is never re-encoded, so it cannot get a second generation of ETC1S loss.
- **Reseals.** Only the resource rows of files that changed (GLB name, bytes, hash; texture rows) are rewritten, then `revision` and `integrity` are recomputed and the document validated. A package with nothing to merge or re-encode comes out with the same revision.

Measured on Hyperspace HQ (`a275c74b@1.0.5`): 53 to 45 texture files, 4.47 to 2.70 MB; the 30 files fetched before first playable fall from 6.13 to 4.67 MB. The step is slow (UASTC RDO is a single-threaded WebAssembly encode, about 25 s per 512 px map). It never publishes; `--dry-run` reports without writing.

## The report

`split-report.json` (also `--json`) holds: input and output digests, grid, spawn (ground height below it, whether it is in the core), the core (radius, reason, cells, bytes, triangles, decoded MiB),
eager bytes and decoded memory before and after, chunk size and triangle distributions (min / median / p90 / max / total), the declared budget with its floor, geometry conservation,
package totals, encodings, far coverage, retained nodes and groups, warnings, and one row per cell: id, group, bytes, triangles, nodes, decoded MiB, bounds, priority, distance to the spawn.
