Documentation

Unity Engine

Download Unity

Unity Engine

TerrainEditUtility

Edits a region of a Terrain on the CPU: change heights and paint layers by rules.
Read time 2 minutesLast updated 22 days ago

Definition

public static class TerrainEditUtility

Remarks

These methods work on one TerrainData, without a Terrain component or a camera, and do not render anything. Positions are in meters on the X and Z axes of the Terrain's local space (see TerrainRegion), heights are in meters above the base of the Terrain, and slopes are in degrees.
Every edit widens its region to cover the cells of the grid it changes, changes nothing outside the region and its blend band, and reports what it changed. An edit that changes something is recorded in the current undo group under the name "Sculpt Terrain Heights" or "Paint Terrain Layers": call Undo.IncrementCurrentGroup first to make it an undo step of its own, and Undo.SetCurrentGroupName afterwards to rename it. An edit that changes nothing writes nothing and records nothing. TerrainEditUtility.GetHeightmapWeights and TerrainEditUtility.GetAlphamapWeights return the weights an edit of a region applies at each sample or texel, to read the area it covers.
Heights are stored with 16-bit precision (a step of TerrainData.size.y / 32766) and layer weights with 8-bit precision, so results are exact only to those steps. Alphamap texel i lies at i * size / (alphamapResolution - 1), so the edge texels lie on the edges of the Terrain, as the Terrain shaders and PaintContext sample them.
The methods edit a single Terrain tile and do not blend across neighbors: changing the samples on the edge of a tile opens a seam with its neighbors unless they are edited to match. They do not save the TerrainData asset; the edited TerrainData is marked dirty. If the heightmap was changed on the GPU, for example through PaintContext or TerrainData.CopyActiveRenderTextureToHeightmap, call TerrainData.SyncHeightmap first, otherwise these methods read, and TerrainEditUtility.SculptHeights overwrites, out-of-date heights.
Each call does all of its work on the main thread and does not limit how long that takes, which can be seconds for a large region of a high-resolution Terrain. In Play mode the changes also persist in the TerrainData asset: to keep the asset unchanged, edit a copy of the TerrainData and assign it to the Terrain and its TerrainCollider.

Static Methods

Method

Description

GetAlphamapWeightsGets the weight that an edit of a region applies at each alphamap texel it covers.
GetHeightmapWeightsGets the weight that an edit of a region applies at each heightmap sample it covers.
PaintLayersPaints terrain layers inside a region by rules.
SculptHeightsChanges the heights of a Terrain inside a region.