Class TerrainArrayBinder
Hands each terrain tile of a scene what the array terrain shader reads: the tile's index map (its four strongest layers per alphamap texel, baked here from its alphamaps), and the scene's baked texture arrays and layer table.
[ExecuteAlways]
[DisallowMultipleComponent]
[RequireComponent(typeof(ClientOnlyObject))]
public sealed class TerrainArrayBinder : MonoBehaviour
- Inheritance
-
ObjectComponentBehaviourMonoBehaviourTerrainArrayBinder
- Inherited Members
-
MonoBehaviour.IsInvoking()MonoBehaviour.CancelInvoke()MonoBehaviour.StopCoroutine(Coroutine)MonoBehaviour.StopAllCoroutines()MonoBehaviour.destroyCancellationTokenMonoBehaviour.useGUILayoutMonoBehaviour.didStartMonoBehaviour.didAwakeMonoBehaviour.runInEditModeBehaviour.enabledBehaviour.isActiveAndEnabledComponent.GetComponent<T>()Component.TryGetComponent<T>(out T)Component.GetComponentInChildren<T>()Component.GetComponentsInChildren<T>()Component.GetComponentInParent<T>()Component.GetComponentsInParent<T>()Component.GetComponents<T>()Component.GetComponentIndex()Component.CompareTag(TagHandle)Component.transformComponent.transformHandleComponent.gameObjectComponent.tagObject.GetEntityId()Object.GetInstanceID()Object.GetHashCode()Object.InstantiateAsync<T>(T)Object.InstantiateAsync<T>(T, Transform)Object.InstantiateAsync<T>(T, Vector3, Quaternion)Object.InstantiateAsync<T>(T, Transform, Vector3, Quaternion)Object.Instantiate(Object, Vector3, Quaternion)Object.Instantiate(Object, Vector3, Quaternion, Transform)Object.Instantiate(Object)Object.Instantiate(Object, Scene)Object.Instantiate<T>(T, InstantiateParameters)Object.Instantiate<T>(T, Vector3, Quaternion, InstantiateParameters)Object.Instantiate(Object, Transform)Object.Instantiate<T>(T)Object.Instantiate<T>(T, Vector3, Quaternion)Object.Instantiate<T>(T, Vector3, Quaternion, Transform)Object.Instantiate<T>(T, Transform)Object.Destroy(Object)Object.DestroyImmediate(Object)Object.DontDestroyOnLoad(Object)Object.DestroyObject(Object)Object.FindObjectsOfType<T>()Object.FindObjectsByType<T>(FindObjectsSortMode)Object.FindObjectsByType<T>(FindObjectsInactive, FindObjectsSortMode)Object.FindObjectOfType<T>()Object.FindFirstObjectByType<T>()Object.FindAnyObjectByType<T>()Object.FindFirstObjectByType<T>(FindObjectsInactive)Object.FindAnyObjectByType<T>(FindObjectsInactive)Object.FindObjectsByType<T>()Object.FindObjectsByType<T>(FindObjectsInactive)Object.ToString()Object.nameObject.hideFlags
Remarks
Why a component at all. Unity's terrain engine binds four splat textures per pass and
knows nothing of arrays, so something has to give every tile the rest through
UnityEngine.Terrain.SetSplatMaterialPropertyBlock(UnityEngine.MaterialPropertyBlock). A property block is not serialized, so it
is pushed whenever this enables — on scene load, after every domain reload, in edit mode as
in play mode (ExecuteAlways) — and again when a tile's alphamaps are replaced (a layer
added in the terrain inspector) or the scene's arrays are rebaked.
Painting stays live. The alphamaps are still the weights everything edits; each tile's index map is baked from them on the GPU (TerrainIndexMapBaker) when it is bound and, in the editor, again whenever they change (a brush stroke, a repaint, an undo), so a stroke is on screen the same frame. A scene may carry up to MaximumLayers layers and a pixel still reads four index texels.
Stored for play. In play (the editor's and a player's) a tile takes the index map the cut stored with the scene (IndexOf(TerrainData)) instead of baking one, while it is still the alphamaps' own — the same texels, so the ground looks the same either way — and a client build may then leave the alphamaps out (ClientTerrainLayerStripper). A tile with no usable stored map is baked from its alphamaps as before.
The arrays are found by the scene's name (TerrainArraySet): off disk in the editor, by address in a player. Nothing here holds a reference to them, because they are build output that each machine regenerates.
Client only. The object carries ClientOnlyObject, so server builds strip it with everything it references (and it destroys itself on a server that was built some other way), and in a server player this compiles to nothing but its serialized fields — a headless server has no use for a texture.
Fields
BackdropShaderName
The shader of the ground past the scene's edge (SceneBackdrop), which draws with this scene's arrays too: its renderers are bound like the tiles, with their own control maps.
public const string BackdropShaderName = "FishMMO/Backdrop Ground"
Field Value
EditorBakeIfStale
Set by the editor's array baker (an editor assembly this one cannot reference): bakes a scene's arrays if they are missing or stale, synchronously. Null outside the editor.
public static Func<Scene, int> EditorBakeIfStale
Field Value
MaximumControlMaps
The most control maps the backdrop's shader reads (SceneBackdropBuilder bakes its own); tiles read index maps.
public const int MaximumControlMaps = 8
Field Value
ObjectName
The binder object's name in a generated scene.
public const string ObjectName = "Terrain Arrays"
Field Value
ShaderName
The shader every tile drawn by this binder uses.
public const string ShaderName = "FishMMO/Weather Terrain Array"
Field Value
Properties
BoundSet
The set currently bound, or null.
public TerrainArraySet BoundSet { get; }
Property Value
Provenance
The provenance on the EditorOnly child, or null.
public TerrainArrayProvenance Provenance { get; }
Property Value
SceneKey
The scene name the arrays are baked under.
public string SceneKey { get; }
Property Value
Terrains
The tiles as assigned. See EffectiveTerrains() for the ones bound.
public List<Terrain> Terrains { get; }
Property Value
- List<Terrain>
TilePushes
Counts every push to a tile and every unbind: what a tile's block holds may have changed when it moves. The client's own heightmap draw (TerrainHeightmapSystem) copies the tiles' bindings into arrays and takes them again when it changes.
public static int TilePushes { get; }
Property Value
Methods
BackdropRenderers()
The renderers of this scene's backdrop that draw with BackdropShaderName.
public List<Renderer> BackdropRenderers()
Returns
- List<Renderer>
Remarks
A backdrop baked before it had control maps still uses URP/Lit and is left alone. Looked up on every push rather than kept, because a rebake swaps the material's shader under the same renderers. While the scene is still loading (OnEnable runs then) its roots cannot be listed, so the backdrops are found among the loaded objects instead; they are all deserialized before any OnEnable.
Bind()
Finds this scene's arrays and pushes everything to every tile, now.
public void Bind()
Remarks
Synchronous in the editor (the set is read straight off disk), so a caller about to render — the world map's overhead capture — sees the arrays on that very render. In a player the set arrives by address a frame or two later; the tiles draw plain grey ground until it does.
Bind(TerrainArraySet)
Supplies an already loaded scene bake (for local rendering tools that do not run client boot).
public void Bind(TerrainArraySet loadedSet)
Parameters
loadedSetTerrainArraySet
BindAll(Scene, bool)
Binds every binder in a scene, now, and returns how many it bound. In the editor it first
bakes the scene's arrays if they are missing or stale (bakeIfStale).
public static int BindAll(Scene scene, bool bakeIfStale = true)
Parameters
sceneScenebakeIfStalebool
Returns
Remarks
For anything that renders a scene it has just opened without waiting a frame — the world
map's overhead capture. OnEnable has already pushed by then, but only what was on disk at
that moment: a scene whose arrays were never baked on this machine would be photographed
grey. Everything here is synchronous in the editor, so the very next
Camera.Render sees the arrays.
BindAllLoaded()
public static void BindAllLoaded()
EffectiveTerrains()
The tiles bound: the assigned list, or every terrain in this scene when it is empty, so a tile added by hand to a scene whose list was never filled is still drawn.
public List<Terrain> EffectiveTerrains()
Returns
- List<Terrain>
IndexMapOf(Terrain)
The index map bound to a terrain by any enabled binder (baked here, or the scene's stored one), or null: for whatever else reads the ground's layers (the contact blend copies it into its own array).
public static Texture IndexMapOf(Terrain terrain)
Parameters
terrainTerrain
Returns
- Texture
Push()
Pushes the current set (or none) to every tile without looking for it again.
public void Push()
RepushScene(Scene)
Re-binds every enabled binder in every loaded scene; the baker calls it after writing sets.
public static void RepushScene(Scene scene)
Parameters
sceneScene
SetFor(Terrain)
The set bound to a terrain tile by whichever binder drew it last, or null: what names its layers.
public static TerrainArraySet SetFor(Terrain terrain)
Parameters
terrainTerrain