Class TerrainIndexMap
The terrain's index map: for every texel of a tile's alphamaps, its four strongest layers and their weights, packed into eight bytes. What the array terrain shader reads in place of the alphamaps themselves, so a scene may carry up to MaximumLayers layers at the cost of four texel reads a pixel, however many it has.
public static class TerrainIndexMap
- Inheritance
-
TerrainIndexMap
- Inherited Members
Remarks
Why. The shader used to read every control map of a tile (four layers each) per pixel and keep the four strongest: 32 layers were eight maps and eight samples a pixel, and every layer more another map (2026-10-16: "every biome has unique textures for everything" needs far more than 32 layers a scene). The four strongest are found once per texel instead, here, and the pixel blends the four texels around it.
A texel is two 32-bit words (R32G32_UInt): x the four layer ids, a byte each, strongest first;
y their weights in bytes, each LESS the texel's fifth-strongest weight. That is the shader's seam-free rule
(FishTopLayers) applied at the texel: a layer that drops out of the four does so at zero, so neither the bake nor
the blend jumps where a fifth layer overtakes the fourth. Ground with four layers or fewer at a texel is stored
exactly as painted.
Ties go to the lower layer index, as the old per-pixel loop met them (it took the layers in index order and only a strictly greater weight moved one). Slots a texel has no layer for hold zero-weight layers, the lowest indices first, so the four ids of a texel are always distinct — the filter merges the four texels' entries by id.
The filter (Filter(uint[], int, int, float, float, int[], float[]), FishTerrainIndexMap.hlsl): ids cannot be filtered by the hardware, so the four texels a bilinear sample would read are loaded, each texel's weights scaled by its bilinear share, entries of the same layer summed, and the four strongest kept less the fifth, normalised — the old rule on the merged weights. Where every texel of the four has four layers or fewer, the merged weights ARE the bilinear weights of every layer, so the result is the old shader's exactly. Where a texel had a fifth layer, the blend there is the old one at that texel's centre and stays continuous between centres; it differs from the old per-pixel choice by at most the texels' own fifth weights.
Baked on the GPU from the live alphamaps (TerrainIndexMapBaker, FishTerrainIndexBake.shader) while the ground is edited, so a brush stroke is on screen the same frame. This class is the same bake on the CPU — the reference the GPU bake is checked against, the bake the cut STORES with the scene's terrain (TerrainDetailBake.IndexMap, what a player binds, so client builds need not ship the alphamaps), and the tests' — and the shader's filter, line for line.
Fields
MaximumLayers
The most layers an index map can name: a byte per id.
public const int MaximumLayers = 256
Field Value
Slots
Layers kept per texel.
public const int Slots = 4
Field Value
StoredFormat
The layout of a stored index map (TerrainDetailBake.IndexMap): bumped whenever BakeTexel(byte[], int, int, out uint, out uint) or the texel packing changes, so every map stored before reads as absent (the ground is baked live from the alphamaps where it has them; a client build keeps the alphamaps of a scene whose maps are not current).
public const int StoredFormat = 1
Field Value
TexelBytes
Bytes per texel (two 32-bit words).
public const int TexelBytes = 8
Field Value
Methods
Bake(float[,,])
A whole tile from its alphamaps as GetAlphamaps(int, int, int, int) returns them ([y, x, layer]): two
words a texel, texel (x, y) at 2 (y × width + x) — the GPU bake's texels, bit for bit (the same bytes through the
same BakeTexel(byte[], int, int, out uint, out uint)). What the cut stores.
public static uint[] Bake(float[,,] alphas)
Parameters
alphasfloat[,,]
Returns
- uint[]
Bake(Color32[][], int, int)
A whole tile from its control maps as UnityEngine.Texture2D.GetPixels32() returns them (map k holds layers 4k..4k+3 in r, g, b, a): two words a texel, texel (x, y) at 2 (y × width + x).
public static uint[] Bake(Color32[][] controls, int texels, int layers)
Parameters
Returns
- uint[]
BakeTexel(byte[], int, int, out uint, out uint)
One texel: the four strongest of layers weights (bytes, layer l at
weights[start + l]) and their weights less the fifth.
public static void BakeTexel(byte[] weights, int start, int layers, out uint ids, out uint packedWeights)
Parameters
ByteOf(float)
A splat weight as a byte, as the alphamaps hold it: GetAlphamaps(int, int, int, int) returns each byte over 255, so this gives the byte back exactly (the GPU bake reads the same bytes off the alphamap textures).
public static byte ByteOf(float weight)
Parameters
weightfloat
Returns
Filter(uint[], int, int, float, float, int[], float[])
The blend at a point of a tile: FishTerrainIndexMap.hlsl FishIndexFilter, line for line in float. Writes the four layers (strongest first) and their normalised weights; an unpainted point is layer 0 alone, as it always was.
public static void Filter(uint[] map, int width, int height, float u, float v, int[] ids, float[] weights)
Parameters
mapuint[]widthintheightintufloatThe tile's 0..1 coordinate across (x).
vfloatThe tile's 0..1 coordinate along (y).
idsint[]weightsfloat[]
IdOf(uint, int)
Layer id in slot slot of a texel's id word.
public static int IdOf(uint ids, int slot)
Parameters
Returns
IsUsable(int, bool, int, int, int, int, int)
Whether a stored index map may be drawn on a tile: of this StoredFormat, not marked stale since it was
stored, whole (two words per texel of a square resolution), and — where the tile still has its
alphamaps — of their resolution and layer count. A tile without alphamaps (a client build's copy) has nothing to
compare: the build checked the map against them before it left them out.
public static bool IsUsable(int format, bool stale, int resolution, int layers, int words, int dataResolution, int dataLayers)
Parameters
Returns
Merge(uint, uint, uint, uint, uint, uint, uint, uint, float, float, int[], float[])
The merge of four texels — (x0, y0), (x1, y0), (x0, y1), (x1, y1) — at the bilinear position (fx, fy) between them: FishIndexFilter's body.
public static void Merge(uint ids00, uint w00, uint ids10, uint w10, uint ids01, uint w01, uint ids11, uint w11, float fx, float fy, int[] ids, float[] weights)
Parameters
ids00uintw00uintids10uintw10uintids01uintw01uintids11uintw11uintfxfloatfyfloatidsint[]weightsfloat[]
Normalise(float, float, float, float, float, int, int, int, int, int[], float[])
The old shader's last step, shared by both: the fifth subtracted from the four, normalised; nothing left (an unpainted texel, or five layers tied) draws layer 0.
public static void Normalise(float t0, float t1, float t2, float t3, float fifth, int i0, int i1, int i2, int i3, int[] ids, float[] weights)
Parameters
t0floatt1floatt2floatt3floatfifthfloati0inti1inti2inti3intidsint[]weightsfloat[]
Same(uint[], uint[])
True when two maps hold the same texels (both null counts as the same).
public static bool Same(uint[] a, uint[] b)
Parameters
Returns
WeightOf(uint, int)
Weight byte in slot slot of a texel's weight word.
public static int WeightOf(uint weights, int slot)