New: Nature Shaders

Interaction, Wind, Dynamic Masking, and Better Shading for your vegetation.

Learn more
This page may not work correctly inside the Unity editor. Open in browser

Editing terrain details at runtime

It is possible to edit terrain details at runtime if you need to make permanent changes to your terrain.

How to edit the terrain

  1. Edit Unity's terrain data using TerrainData.SetDetailLayer.
  2. Call RebuildBuffers on the Nature Renderer component using the parameter TerrainChangedFlags.RemoveDirtyDetailsImmediately.
  3. Nature Renderer will automatically find the modified part of your terrain and rebuild the internal buffers for that area. Rebuilding is done on a separate worker thread and can take a few frames to finish. During this time the old terrain data is still shown.

Note: If you change the heightmap of the terrain or add new details that did not exist before, then Nature Renderer has to rebuild the entire terrain instead of only the modified area. This can take 1 or 2 seconds for larger terrains.

Modified example from Unity's documentation

using UnityEngine;
using VisualDesignCafe.Rendering.Nature;
public class ExampleClass : MonoBehaviour
{
    // Set all pixels in a detail map below a certain 
    // threshold to zero.
    void DetailMapCutoff(Terrain t, float threshold)
    {
        // Get all of layer zero.
        var map = 
          t.terrainData.GetDetailLayer(    
            0,              
            0,          
            t.terrainData.detailWidth,       
            t.terrainData.detailHeight, 
            0);
        
        // For each pixel in the detail map...
        for (int y = 0; y < t.terrainData.detailHeight; y++)
        {
            for (int x = 0; x < t.terrainData.detailWidth; x++)
            {
                // If the pixel value is below the threshold then
                // set it to zero.
                if (map[x, y] < threshold)
                {
                    map[x, y] = 0;
                }
            }
        }
        
        // Assign the modified map back.
        t.terrainData.SetDetailLayer(0, 0, 0, map);
        
        // Update Nature Renderer's buffers.
        t.GetComponent<NatureRenderer>().RebuildBuffers( 
        	TerrainChangedFlags.RemoveDirtyDetailsImmediately );
    }
}

 

 

Was this article helpful?
1 out of 2 found this helpful