Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Create the Undo Terrain Hole and Restore Original Grass

Discussion in 'World Building' started by protopop, May 11, 2021.

  1. protopop

    protopop

    Joined:
    May 19, 2009
    Posts:
    1,557
    With help I have figured out how to create a terrain hole via script and to undo the hole, and how to create circular holes in terrain

    https://forum.unity.com/threads/cre...on-on-terrain-by-script.1096705/#post-7124681

    and how to save and restore the trees.

    Code (CSharp):
    1. private TreeInstance[] _originalTrees;
    2.  
    3. void backuporiginalterraintrees {
    4. _originalTrees = terrain.terrainData.treeInstances;
    5. }
    6.  
    7. void restoreoriginaltrees {
    8. terrain.terrainData.treeInstances = _originalTrees;
    9. }

    But I cant figure out how to save and restore terrain grass. I tried saving the terrain data to a variable then restoring it but it doesn't work.

    When I "undo"the terrain hole I am changing the boolean flags for each hole x,z location from false to true, but maybe I need to delete all references to the hole?Any help is appreciated

    E0uTgO0X0AMwwgd.jpeg
     
  2. wyattt_

    wyattt_

    Unity Technologies

    Joined:
    May 9, 2018
    Posts:
    424
    Heya! Is this something you are doing at runtime (in playmode) or something you are doing for edit-time (in editor)? Thanks.
     
    protopop likes this.
  3. protopop

    protopop

    Joined:
    May 19, 2009
    Posts:
    1,557
    Thanks for asking - it's at runtime:)

    I tried originalTerraindata = terrain.terraindata and the restoring that like terrain.terraindata = originalTerraindata but nothing happened. Then I used the method above for the trees which works great but grass I cant figure out.
     
  4. protopop

    protopop

    Joined:
    May 19, 2009
    Posts:
    1,557
    Just bumping this. I still can't figure out how to restore grass after you create and undo a hole.

    is there maybe an array or something that is storing the holes in a terrain? Something I can access and erase? Maybe that's preventing grass from spawning where a hole used to be?

    Im open to any ideas or suggestions.
     
  5. m_riley04

    m_riley04

    Joined:
    Apr 21, 2022
    Posts:
    4
    Hey there! I know this is a very old issue, but I solved this issue by storing the position that I was creating the hole around and simply setting all the holes to true.

    Code (CSharp):
    1.  public void RemoveDynamicHoles(TerrainData terrainData, Vector3 position, int width, int height)
    2. {
    3.     var b = new bool[width, height];
    4.  
    5.     // Iterate through every hole position
    6.     for (var x = 0; x < width; x++)
    7.     {
    8.         for (var y = 0; y < height; y++)
    9.         {
    10.             // Set it to true
    11.             b[x, y] = true;
    12.         }
    13.     }
    14.  
    15.     // Set the hole values
    16.     terrainData.SetHoles((int)position.x, (int)position.z, b);
    17. }
    If you ever figured this out a different way, I'd love to see how!