Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice

Generating Terrain at runtime

Discussion in 'Scripting' started by Florian22222, Nov 6, 2012.

  1. Florian22222

    Florian22222

    Joined:
    Jul 28, 2012
    Posts:
    22
    Hello there!

    I am making a multiplayer game, where I want to generate terrain at runtime as it´s discovered(Minecraft has a similar approach). I dont want any loading screens etc. I want to do it at runtime.
    So my first thoughts were about assetbundles. But the problem is that those only support static terrains, but I want to generate them at runtime.
    The second approach would be to multithread the generation. The random generation itself(returns a float[][] of heights) would be possible, but since Unity doesnt support multithreading access, I cant assign the generated values to a TerrainData.

    Is there any workaround or way to do this?

    Thanks in advance!
     
  2. atrakeur

    atrakeur

    Joined:
    Jun 14, 2011
    Posts:
    134
    Generate your float array in another thread, them apply it to terrain in the main thread
    Unity isn't thread safe so I'm afraid it's the only way to do it
     
  3. Florian22222

    Florian22222

    Joined:
    Jul 28, 2012
    Posts:
    22
    So there´s no way to prevent the game from lagging, when applying the float array and updating the terrain collider?
     
  4. jc_lvngstn

    jc_lvngstn

    Joined:
    Jul 19, 2006
    Posts:
    1,508
    Are you actually creating a blocky, Minecraft style terrain that you can tunnel into, or a traditional terrain, that is based on a heightmap? Sounds like you are going for the traditional terrain, but you just want to generate it on the fly.

    You should be able to generate the terrain data (the floats) in a separate thread. Then, create the terrain from that in your main thread.

    In order to cut down on the lag, you'll need to create small terrains. But you'll have to experiment with the size of each small terrain, how many trees, how much grass, and textures. Obviously...the more variety you have and the more dense your vegetation and such, the slower the terrain creation will be.

    I haven't actually profiled it...but I think trees are the main cause of lag. Create a terrain about 2048x2048, with about 300k trees, and watch your computer crawl over to the corner and pee all over itself. Remove them and watch it fly. Which is kind of surprising, the vast majority of them are billboarded, so I don't know why it takes such a hit. Maybe all of the tree colliders...hmmm now I'm wondering why I haven't tested that.

    There are whispers of an improved terrain engine at some point in the future, though I haven't heard any mention of what features it will have.
     
  5. Florian22222

    Florian22222

    Joined:
    Jul 28, 2012
    Posts:
    22
    Thanks for your reply!

    I am creating a game with traditional terrain.

    Yes this was one thought, but I don´t like the approach because there is still a lag...
    I really hope they create this feature somewhen in the future :)

    Now I probably need to use different small islands and travel between them with a ship...

    UPDATE:
    I found this link http://forum.unity3d.com/threads/68807-Infinite-Terrain-Free-Project-Source on the unity forums.
    It uses the exact same method as you suggested. Creating little tiles...
     
    Last edited: Nov 6, 2012
  6. jc_lvngstn

    jc_lvngstn

    Joined:
    Jul 19, 2006
    Posts:
    1,508
    Yeah. I'd be curious to see how smooth that terrain gen is with a decent variety and amount of trees, grass, and textures. It's a neat demo, but I only see one texture. Terrain generates pretty quickly there :)
     
  7. Florian22222

    Florian22222

    Joined:
    Jul 28, 2012
    Posts:
    22
    my first attempt would be to load the terrain from a file. maybe ill try it on the weekend
     
  8. Florian22222

    Florian22222

    Joined:
    Jul 28, 2012
    Posts:
    22
    ok so i tried it out. it loads in the data from a file asynchronously.
    my problem is a good compromise between RAM and Lag.
    when i have 32x32 tiles, there´s a lag in updating the terrain collider.
    those small tiles take up a lot of RAM because the overhead seems to be really big.

    any idea how i can split the updating of the terrain collider into a few frames?
    Code (csharp):
    1. tile.GetComponent<TerrainCollider>().terrainData = terrainData;
     
  9. alexzzzz

    alexzzzz

    Joined:
    Nov 20, 2010
    Posts:
    1,447
    I experimented with procedurally generated terrains over a year ago: http://forum.unity3d.com/threads/68470-Colliderless-Trees...-again?p=470217&viewfull=1#post470217

    Standalone build: http://dl.dropbox.com/u/102171874/27.zip
    Each time you press Backspace, a new island is generated on the fly somewhere nearby the first one.

    PS
    If I remember correctly, the islands consist of 4..16 terrains. The height and splat-maps are 256x256.
     
    Last edited: Nov 9, 2012
  10. jc_lvngstn

    jc_lvngstn

    Joined:
    Jul 19, 2006
    Posts:
    1,508
    How many trees do you have, per terrain?
     
  11. alexzzzz

    alexzzzz

    Joined:
    Nov 20, 2010
    Posts:
    1,447
    ~4000 trees per 1024x1024m terrain. Adding a tree is fast, rebuilding a terrain's tree collider is slow.
     
  12. jc_lvngstn

    jc_lvngstn

    Joined:
    Jul 19, 2006
    Posts:
    1,508
    Hmmm. I've split my main terrain up, into smaller ones that are 128x128. But, the terrain itself is scaled up, so it's not so small. I try to keep less than 2000 trees per terrain, and it can still produce a slight hitch when a couple terrains load. Not a big deal, but it's there. I keep wondering how hard it would be for the community to just roll its own.
     
  13. Florian22222

    Florian22222

    Joined:
    Jul 28, 2012
    Posts:
    22
    I didnt even do trees yet!
    as alex already said, building the terrain collider slows everything.
    i am switching to another engine, because this is not acceptable for my game...
     
  14. Timurid

    Timurid

    Joined:
    Jun 9, 2014
    Posts:
    1
    *Necromastery* *whoosh whoosh*

    Wrong.
    There is a difference between "engine not acceptable" and "built-in components not acceptable and I cant develop my own".
     
    pointcache and Tactical_Beard like this.