Search Unity

Good ways of optimising voxel terrain

Discussion in 'General Graphics' started by BubyMB, Jan 10, 2017.

  1. BubyMB

    BubyMB

    Joined:
    Jun 6, 2016
    Posts:
    140
    Hey guys, I have a script that generates terrain with 1x1x1 blocks, It creates thousands upon thousands of blocks;

    So I was wondering, how would i optimise this? The world is 200x200. I was wondering how mine craft does it since the world is so large, where on my main pc I can run 500-1000fps on V-Sync. I would assume Unity's rendering system would be fine, but apparently not.
     
  2. BubyMB

    BubyMB

    Joined:
    Jun 6, 2016
    Posts:
    140
    These were my results
    Main PC (2x GTX 1080 SLI (OC)): Low: 61 Avg:98 High: 120
    Second PC (1x Titan XP): Low: 34 Avg:73 High: 112
    Laptop (Intel 5000 HD Graphics): Low:3 Avg:5 High:7
     
  3. juicyz

    juicyz

    Joined:
    Jan 14, 2016
    Posts:
    85
    Render only the ones needed that are in the 'players view' plus a little. Then as the player moves, unload and load more. Basically it's the idea of chunks in minecraft. Do some searching on the forums here or google, there are a ton of tutorials and guides for this.
     
  4. BubyMB

    BubyMB

    Joined:
    Jun 6, 2016
    Posts:
    140
    I'm fairly sure unity already does that as a default.
     
  5. juicyz

    juicyz

    Joined:
    Jan 14, 2016
    Posts:
    85
    It does not. You are controlling the rendering. How does Unity know what you want and what you dont? It has a camera.isVisible variable on objects and such but that's not going to stop the rendering since you are controlling that. You can test this by pausing the game then going to the scene view and zooming out. You will see all your objects, not just the ones in camera view or whatever.

    This tutorial is pretty out of date I believe but I've done it once to see what it was about. https://forum.unity3d.com/threads/tutorial-procedural-meshes-and-voxel-terrain-c.198651/
     
  6. jc_lvngstn

    jc_lvngstn

    Joined:
    Jul 19, 2006
    Posts:
    1,508
    I think you may be confusing what Unity draws, versus you having portions of the world actually "in existence".
    With Minecraft, the entire "world" isn't "loaded" or there. It only creates portions of the world when players are nearby.

    See, what you do is break the world down into chunks. When the player walks into a new area, it "calculates" what blocks are in that area based off of noise functions and some other stuff. A chunk of the world may be like...a 32x32x32 area of blocks, or 32 wide by 32 deep by 64 tall, or however you want to organize the information.
    Then, you generate the mesh for that chunk depending on which block faces are visible. If you have a dirt block with the ONLY top side visible to the sky, when you create the mesh for that chunk you only include that face of the block that is visible.
    So all around the players, you generate the chunks in the region and show them the meshes. As you move far enough from a chunk that it is no longer visible, you toss that one and create the one you are coming close to.
    So this way, you only generate blocks/chunks for the area around the player. It's not a Unity problem, it's just reasonable.
     
  7. hippocoder

    hippocoder

    Digital Ape

    Joined:
    Apr 11, 2010
    Posts:
    29,723
    I believe leveraging a compute shader should make it incredibly, incredibly fast. So you'd probably want to look into that.