Runtime terrain editor -- runtime terrain editor

After the last article about terrain was written (it seems a bit far away), the runtime terrain editor has basically met its own needs. After taking some time to sort out and make it into an independent tool, unity2018.3 and above are available. At the beginning, it was also adapted to 2017. Later, because it did not need 2017 at all, it gave up its support for 2017.
Github source link

  • Support cross terrain editing
  • Brush
  • Raise, lower, smooth terrain
  • Planting and removal of trees
  • Planting and removal of grass
  • Map editing
  • Terrain height editing with brush support
  • Brush is supported for tree and grass editing
  • Withdrawal of operation

1, Runtime terrain editor

                          .

1. brush

Before Define Brush In this article, I talked about the idea of customizing the brush. I won't say much here, just how to create a picture to use as a brush.
                                . The transparency of the center of this map is high, and the more it goes out, the lower it is. Using this map to modify the terrain height is also an effect of high in the middle and low in the periphery.

                   .

                       .

public RuntimeTerrainEditor(Texture2D[] brushs = null)

2. Height modification

                             .

/// <summary>
///Change height (circle)
/// </summary>
///< param name = "center" > center point < / param >
///< param name = "radius" > radius < / param >
///< param name = "Opacity" > strength < / param >
///< param name = "isrise" > raise or lower < / param >
///< param name = "register undo" > register recall command < / param >
public void ChangeHeight(Vector3 center, float radius, float opacity, bool isRise = true, bool regesterUndo = false)
/// <summary>
///Change terrain height with custom brush
 /// </summary>
 ///< param name = "center" > center point < / param >
 ///< param name = "radius" > radius < / param >
 ///< param name = "Opacity" > strength < / param >
 ///< param name = "brushindex" > brush index < / param >
 ///< param name = "isrise" > raise or lower < / param >
 ///< param name = "register undo" > register recall command < / param >
public void ChangeHeightWithBrush(Vector3 center, float radius, float opacity, int brushIndex = 0, bool isRise = true, bool regesterUndo = false)
/// <summary>
///Smooth terrain
/// </summary>
///< param name = "center" > center point < / param >
///< param name = "radius" > radius < / param >
///< param name = "dev" > this is a parameter of Gaussian blur, which will affect the degree of smoothness < / param >
///< param name = "level" > radius to build Gaussian sum < / param >
///< param name = "register undo" > register recall command < / param >
public void Smooth(Vector3 center, float radius, float dev, int level = 3, bool regesterUndo = false)

3. add and delete trees

/// <summary>
///Create trees
/// </summary>
///< param name = "POS" > center point < / param >
///< param name = "count" > quantity < / param >
///< param name = "radius" > planting radius < / param >
///< param name = "index" > index of tree template < / param >
public void CreatTree(Vector3 pos, int count, int radius, int index = 0)
/// <summary>
///Remove the trees on the terrain without doing the multi map processing
/// </summary>
///< param name = "center" > center point < / param >
///< param name = "radius" > radius < / param >
///< param name = "index" > index of tree template < / param > 
public void RemoveTree(Vector3 center, float radius, int index = 0)

4. add and delete grass

/// <summary>
///Add details
/// </summary>
///< param name = "terrain" > target terrain < / param >
///< param name = "center" > target center point < / param >
///< param name = "radius" > radius < / param >
///< param name = "layer" > level < / param >
///< param name = "register undo" > register recall command < / param >
public void AddDetial(Vector3 center, float radius, int count, int layer = 0, bool regesterUndo = false)
/// <summary>
///Remove details
/// </summary>
///< param name = "terrain" > target terrain < / param >
///< param name = "center" > target center point < / param >
///< param name = "radius" > radius < / param >
///< param name = "layer" > level < / param >
///< param name = "register undo" > register recall command < / param >
public void RemoveDetial(Vector3 point, float radius, int layer = 0, bool regesterUndo = false)

5. Modify the map

/// <summary>
///Set up a map
/// </summary>
///< param name = "radius" > radius < / param >
///< param name = "point" > center point < / param >
///< param name = "index" > level < / param >
///< param name = "register undo" > register recall command < / param >
public void SetTextureNoMix(Vector3 point, float radius, int index, bool regesterUndo = false)

6. Withdrawal operation

   at present, except for the creation and deletion of trees, all operations support recall. When using these API s, set the parameter regesterUndo to true, indicating that this operation should be registered in the recall operation, and then call Undo() when you want to recall.

2, Configuration before use

1. Configure brush

                   .

2. Configure treePrototypes, detailPrototypes, terrainLayers

   before using the terrain editing tool, you need to set the template of tree, grass and map for the terrain in the scene, which can be set in the scene before running, or set with code.

Terrain.activeTerrain.terrainData.treePrototypes = ...
Terrain.activeTerrain.terrainData.detailPrototypes = ...
Terrain.activeTerrain.terrainData.terrainLayers = ...

                     .

/// <summary>
///Create a tree template from the prefab of the tree
/// </summary>
///< param name = "treeobjs" > tree prefabrication < / param >
///< returns > template < / returns >
public static TreePrototype[] CreatTreePrototype(GameObject[] treeObjs)
/// <summary>
///Create detail templates from maps
/// </summary>
///< param name = "textures" > map < / param >
///< returns > template < / returns >
public static DetailPrototype[] CreateDetailPrototype(Texture2D[] textures)

3. Configuration of basic terrain data

                           .

/// <summary>
///Configuration before use
/// </summary>
public void Config()

After the above steps are completed, you can use the terrain editor.

45 original articles published, 33 praised, 30000 visitors+
Private letter follow

Tags: github

Posted on Thu, 16 Jan 2020 08:23:23 -0800 by MikeL7