# 2. WorldGrid Cells and Hysteresis Part of [[Open-World Streaming]]. Previous: [[1. Streaming Mental Model and World Layout]]. Next: [[3. Additive Scene Streaming with SceneManager]]. > [!warning] Test status > Type-checked against 6000.5.8f1. **12/12** tests passed on .NET 10 (listed in part 6). The streamer's decisions are pure math: *which cell am I in, and which cells should load or unload?* Putting that math in a class with **no `UnityEngine` types** means you can test it in milliseconds and reuse it on a server. ```csharp // Assets/Scripts/Streaming/WorldGrid.cs using System; using System.Collections.Generic; /// <summary>A streaming cell on the XZ plane. Cell (0,0) covers [0, size) on both axes.</summary> public readonly struct CellCoord : IEquatable<CellCoord> { public readonly int X; public readonly int Z; public CellCoord(int x, int z) { X = x; Z = z; } public bool Equals(CellCoord other) => X == other.X && Z == other.Z; public override bool Equals(object obj) => obj is CellCoord other && Equals(other); public override int GetHashCode() => (X * 73856093) ^ (Z * 19349663); public override string ToString() => X + "_" + Z; /// <summary>Scene or address naming convention: World_3_-2</summary> public string SceneName(string prefix) => prefix + X + "_" + Z; } /// <summary> /// Pure streaming math: which cell a world position is in, and which cells to load/unload /// with hysteresis (unloadRadius > loadRadius) so walking along a border never thrashes. /// Works in double so it stays exact far from the floating origin. /// </summary> public static class WorldGrid { public static CellCoord CellAt(double worldX, double worldZ, double cellSize) { if (cellSize <= 0) throw new ArgumentOutOfRangeException(nameof(cellSize)); return new CellCoord((int)Math.Floor(worldX / cellSize), (int)Math.Floor(worldZ / cellSize)); } /// <summary>Chebyshev (square ring) distance in cells.</summary> public static int Distance(CellCoord a, CellCoord b) => Math.Max(Math.Abs(a.X - b.X), Math.Abs(a.Z - b.Z)); /// <summary> /// Fills toLoad (nearest first) with cells within loadRadius of center that are not in resident, /// and toUnload with resident cells farther than unloadRadius. Cells between the radii are kept. /// exists filters out cells that have no content (ocean, map edge). /// </summary> public static void Plan(CellCoord center, int loadRadius, int unloadRadius, ICollection<CellCoord> resident, Func<CellCoord, bool> exists, List<CellCoord> toLoad, List<CellCoord> toUnload) { if (loadRadius < 0) throw new ArgumentOutOfRangeException(nameof(loadRadius)); if (unloadRadius < loadRadius) throw new ArgumentException("unloadRadius must be >= loadRadius (hysteresis).", nameof(unloadRadius)); toLoad.Clear(); toUnload.Clear(); for (var r = 0; r <= loadRadius; r++) { for (var dz = -r; dz <= r; dz++) for (var dx = -r; dx <= r; dx++) { if (Math.Max(Math.Abs(dx), Math.Abs(dz)) != r) continue; // only the ring at distance r, so the list comes out nearest-first var c = new CellCoord(center.X + dx, center.Z + dz); if (resident.Contains(c)) continue; if (exists != null && !exists(c)) continue; toLoad.Add(c); } } foreach (var c in resident) { if (Distance(c, center) > unloadRadius) toUnload.Add(c); } } } ``` ## 2.1 Walkthrough - **`CellAt` uses `Math.Floor`, not a cast.** `(int)(-0.5 / 256)` is `0`, but the point is in cell `-1`. Truncation toward zero gives you a double-wide cell 0 and a seam of missing content around the axes. One test pins this down. - **Doubles in, ints out.** The caller passes the *true* world position, which is the floating-origin offset (a `double`) plus the local `float` position. That way cell lookup stays exact a thousand kilometres out (another test checks this). - **Ring order.** `Plan` walks rings `r = 0, 1, …, loadRadius` and adds only the cells exactly at distance `r`. So `toLoad` comes out **nearest first**: the player's own cell, then its neighbours. When loads are throttled, the cell you're standing on never waits behind a corner cell. - **Hysteresis.** Cells between `loadRadius` and `unloadRadius` are neither requested nor released. `unloadRadius < loadRadius` throws, because it would unload cells you just asked for. - **`exists`** filters cells with no content (open ocean, beyond the map edge), so the streamer doesn't fire loads that fail. Part 3 backs it with `Application.CanStreamedLevelBeLoaded`, and part 4 with the Addressables catalog. ## 2.2 A worked trace With `loadRadius = 1`, `unloadRadius = 2`, starting with nothing loaded: | Player cell | toLoad | toUnload | Resident after | |---|---|---|---| | (0, 0) | 9 cells, (0, 0) first | — | x ∈ [−1, 1] | | (1, 0) | 3 cells at x = 2 | — (x = −1 is distance 2, kept) | x ∈ [−1, 2] | | back to (0, 0) | — | — | unchanged: **no thrash** | | teleport to (3, 0) | the cells around (3, 0) | 6 cells: x = −1 (distance 4) **and** x = 0 (distance 3) | … | When I first wrote the tests, I expected only the x = −1 column to unload on the jump to (3, 0). That was wrong. x = 0 is at distance 3, which is also beyond the unload radius of 2. The planner was right and my expectation wasn't. It's a good reminder to write the trace down before trusting your intuition about Chebyshev rings. ## 2.3 Choosing the shape A square (Chebyshev) ring is simple and matches square cells. If cell loads are expensive, a circle saves the four corners. Replace `Distance` with Euclidean distance on cell centres, and keep the ring loop but skip cells where `dx² + dz² > r²`. Whatever you pick, use the **same** distance for loading and unloading, or hysteresis can break at the corners.