Class TerrainIndexMapBaker
Bakes a terrain tile's index map (TerrainIndexMap) on the GPU, from the tile's own alphamaps, into an R32G32_UInt render texture the array terrain shader loads.
public static class TerrainIndexMapBaker
- Inheritance
-
TerrainIndexMapBaker
- Inherited Members
Remarks
From the GPU's alphamaps, because a terrain paint stroke writes them there first and synchronises the CPU copy only when the stroke ends: baking from them keeps painting live (the binder rebakes a tile on every alphamap change), as reading the alphamaps per pixel did. Each map is copied into one slice of a scratch array, and one full-screen triangle (FishTerrainIndexBake.shader) writes every texel's four strongest layers. A 512² tile of eight maps is eight copies and one small draw; nothing is read back.
Baked live while the ground is edited, stored for play (2026-10-16: "store index maps with the terrain at cut/repaint, still re-baked live while painting in the editor"). In edit mode every tile is baked here from its alphamaps, on every change. The cut also stores each tile's map, baked on the CPU by the same rule (Bake(float[,,])), in the scene's TerrainDetailBake; in play (the editor's and a player's) the binder uploads that (Upload(IndexMap, string, Shader)) when it is still the alphamaps' own, and bakes here only when it is not. A client build then leaves the alphamaps out of every tile whose stored map it has checked against them (ClientTerrainLayerStripper), and the CPU readers that used them (GroundColourMap, the grass's surface layers) read the stored map on such a tile.
Integer arithmetic on bytes, so the GPU bake is BakeTexel(byte[], int, int, out uint, out uint) exactly; the editor checks a sample of texels against it once per tile and session (Check(Terrain, RenderTexture, int, int)).
Fields
Format
The index map's format: x four ids, y four weights, a byte each.
public const GraphicsFormat Format = R32G32_UInt
Field Value
- GraphicsFormat
RemapWidth
How many channels the remap table names: every id a byte can hold.
public const int RemapWidth = 256
Field Value
ShaderName
The bake shader (FishTerrainIndexBake.shader).
public const string ShaderName = "Hidden/FishMMO/Terrain Index Bake"
Field Value
UploadFormat
The byte staging texture: two RGBA8 texels carry the two packed uint words, without filtering.
public const GraphicsFormat UploadFormat = R8G8B8A8_UNorm
Field Value
- GraphicsFormat
Properties
Blank
A 1 × 1 index map of unpainted ground (layer 0 alone): what a tile is given while it has none, so the shader's unsigned-integer texture is never left to Unity's float default.
public static RenderTexture Blank { get; }
Property Value
- RenderTexture
Methods
Bake(TerrainData, Shader, RenderTexture, int, int[])
Bakes data's index map into target (slice slice of an
array target), now. False, with one warning a session, when the device or shader cannot.
public static bool Bake(TerrainData data, Shader shader, RenderTexture target, int slice = 0, int[] sceneLayers = null)
Parameters
dataTerrainDatashaderShadertargetRenderTexturesliceintsceneLayersint[]The scene layer each of the tile's channels holds (Of(Terrain)): the ids are written as scene layers, as Bake(float[,,], int[]) writes them on the CPU. Null for a tile whose channels are the scene's.
Returns
CanBake(Shader)
True when this device can bake and read index maps with this shader.
public static bool CanBake(Shader shader)
Parameters
shaderShader
Returns
CanUpload(Shader)
Integer Texture2D creation is unsupported on some devices that can render and load integer targets. Pass the set's shader (ShaderOf(TerrainArraySet)): a player build carries it through the set, where a lookup by name can miss it.
public static bool CanUpload(Shader shader = null)
Parameters
shaderShader
Returns
Check(Terrain, RenderTexture, int, int)
Editor diagnostic: reads a few texels of a baked index map back and compares them with the CPU bake of the same texels from the terrain's CPU alphamaps (BakeTexel(byte[], int, int, out uint, out uint)), logging "[Terrain index] check '<tile>': ok" or a MISMATCH with the first texel that differs. Call it only when the CPU copy is current (not mid-stroke). Nothing reads the map back otherwise.
public static void Check(Terrain terrain, RenderTexture target, int slice = 0, int samples = 24)
Parameters
CopyInto(Texture, RenderTexture, int)
Copies a tile's index map (a baked one or an uploaded stored one, of Format) into slice
slice of an index array of its size: an exact texture copy, nothing converted. False where the
sizes or formats differ or the device cannot copy into a render texture.
public static bool CopyInto(Texture source, RenderTexture target, int slice)
Parameters
sourceTexturetargetRenderTexturesliceint
Returns
Create(int, int, int, string, bool)
An index map target: width × height texels (one per alphamap texel), an array of
slices when array or more than one.
public static RenderTexture Create(int width, int height, int slices, string name, bool array = false)
Parameters
Returns
- RenderTexture
Fits(RenderTexture, TerrainData)
True when target is the size an index map of data must be.
public static bool Fits(RenderTexture target, TerrainData data)
Parameters
targetRenderTexturedataTerrainData
Returns
MatchesAlphamaps(TerrainData, IndexMap, int, out string, int[])
Whether a stored map still says what the tile's CPU alphamaps say, at samples texels spread over
the tile, edges included (the CPU bake of each, BakeTexel(byte[], int, int, out uint, out uint), against the stored words):
the editor's guard against a map whose staleness was missed (an alphamap written with no terrain callback). True,
with nothing compared, for a tile without alphamaps. Reads a texel at a time, so it is cheap.
sceneLayers is the tile's channel map (Of(Terrain)): a stored map names scene layers.
public static bool MatchesAlphamaps(TerrainData data, TerrainDetailBake.IndexMap stored, int samples, out string firstBad, int[] sceneLayers = null)
Parameters
dataTerrainDatastoredTerrainDetailBake.IndexMapsamplesintfirstBadstringsceneLayersint[]
Returns
ReleaseScratch()
Frees the bake's own scratch texture and material (not the index maps, which their owners hold).
public static void ReleaseScratch()
ShaderOf(TerrainArraySet)
The bake shader a set carries (so a player build has it), else the project's by name.
public static Shader ShaderOf(TerrainArraySet set)
Parameters
setTerrainArraySet
Returns
- Shader
Upload(IndexMap, string, Shader)
Reconstructs stored words in an integer render target from two RGBA8 staging texels per index texel. This uses the same representation as live painting, avoiding integer Texture2D creation and its different capability rules. The temporary source has no mipmaps, sRGB conversion or filtering; the upload pass rounds bytes before packing. The caller owns the target. Stored CPU words and the final eight bytes per texel are unchanged.
public static RenderTexture Upload(TerrainDetailBake.IndexMap stored, string name, Shader shader = null)
Parameters
storedTerrainDetailBake.IndexMapnamestringshaderShader
Returns
- RenderTexture