Search Unity

how to modify terrain heights at runtime without changing asset file

Discussion in 'Scripting' started by steveh2112, Jan 20, 2021.

  1. steveh2112

    steveh2112

    Joined:
    Aug 30, 2015
    Posts:
    314
    i need to modify the terrain at runtime and its working using terrainData.SetHeightsDelayLOD()
    terrainData.SetHeights() is way to slow

    but it changes the file in the assets folder which is not what i want and i expect slows things down unnecessarily.

    is there no way to use terrain (vs. mesh) with in memory modifications only

    thx
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,697
    You need to Instantiate a copy of the TerrainData and use that copy, otherwise you will modify what is on disk.

    If you want to see an example of this, check out my terrain deformation example in my MakeGeo project. Look for the
    TerrainDamager
    scene.

    MakeGeo is presently hosted at these locations:

    https://bitbucket.org/kurtdekker/makegeo

    https://github.com/kurtdekker/makegeo

    https://gitlab.com/kurtdekker/makegeo

    https://sourceforge.net/p/makegeo
     
  3. steveh2112

    steveh2112

    Joined:
    Aug 30, 2015
    Posts:
    314
    thanks, that's great, actually instantiate doesn't work for terrain so he wrote TerrainDataCloner which is the key to making it work
     
  4. steveh2112

    steveh2112

    Joined:
    Aug 30, 2015
    Posts:
    314
    i got it working but i have a high res height map, 4097x4097, so i just take a 32x32 patch and modify that and use terrainData.SetHeightsDelayLOD() on the patch, not the whole thing and it works great, and fast. i need it fast because i'm deforming the mesh in real time.

    problem is when i use the splatmaps, and use TerrainData.SetAlphamaps

    my terrain texture is 2048x2048 and if i set alpha maps on the whole splatmap, it grinds to a halt. so i tried just a patch and its faster but still way too slow.

    i wonder is there is a faster way to change the texture on terrain at runtime