Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Question Procedual terrain - How reduce vertexes?

Discussion in 'General Discussion' started by Stormer2020, Aug 29, 2022.

  1. Stormer2020

    Stormer2020

    Joined:
    Sep 14, 2020
    Posts:
    89
    Hi~ I'm generating a voxel terrain at runtime.(MC like)

    The terrain is static, never change.

    So, I want to optimize it like below:

    1.jpg

    to
    2.jpg

    Current :
    a.jpg b.jpg

    How merge the meshes at same face? Is there have any solution about this? Thank you!
     
    Last edited: Aug 31, 2022
  2. Antypodish

    Antypodish

    Joined:
    Apr 29, 2014
    Posts:
    10,574
    Look for marching cube solutions and algorithms.

    If you search marching cubes, you will find tons of resources on it.
     
    CodeSmile and Stormer2020 like this.
  3. Stormer2020

    Stormer2020

    Joined:
    Sep 14, 2020
    Posts:
    89
    Cool! Thank you so much~
     
  4. Stardog

    Stardog

    Joined:
    Jun 28, 2010
    Posts:
    1,886
  5. MadeFromPolygons

    MadeFromPolygons

    Joined:
    Oct 5, 2013
    Posts:
    3,875
    If you know all the meshes you want to combine into one, then you can just use https://docs.unity3d.com/ScriptReference/Mesh.CombineMeshes.html at runtime to do this as the level loads

    But to do this more intelligently (say when you dont know all the meshes that need combining ahead of time) then an algorithmic approach is needed.

    Most marching cubes solutions will create merged meshes so as @Antypodish mentioned thats a good starting point.
     
  6. CodeSmile

    CodeSmile

    Joined:
    Apr 10, 2014
    Posts:
    4,019
    Note that the face as highlighted in blender is not something you'll get to see in Unity, this face will still be many triangles although much less than the usual two triangles per cube face.
     
    angrypenguin likes this.
  7. neginfinity

    neginfinity

    Joined:
    Jan 27, 2013
    Posts:
    13,321
    I think it is not really related to his problem.

    Instead of generating faces for each cube, process landscape in "slices" for the entire chunk.

    For example, you're in a 64x64x64 chunk, and start at the t op layer, and find cubes that need upper face generated.
    upload_2022-8-29_23-0-31.png

    You handle that in hierarchical fashion. First you check if entire, say, 64x64 area is filled with cubes that need need upper face generated and have the same material. If it is, you generate one big 64x64 quad for them all and call it done (and move onto next slice). If nothing in the area requires geometry to be generated you also call it done.

    If you're not done, you go to 32x32 cells (4 of them) and for each of them check if entire 32x32 area is empty (you're done in this case), or filled with the same material (you generate a 32x32 quad and you're done). Then you go this way down to 1x1 level, processing 16x16, 8x8, 4x4, 2x2. .

    This is best done using a quad tree for each slice. https://en.wikipedia.org/wiki/Quadtree

    This will result in this sort of triangulation which will save plenty of quads for you. It will not be the most optimal, but will greatly reduce number of faces generated.

    upload_2022-8-29_23-6-26.png
     
    Stormer2020 likes this.
  8. Antypodish

    Antypodish

    Joined:
    Apr 29, 2014
    Posts:
    10,574
    Another algorithm is a greedy meshing.
    It perhaps is more suitable / easier to implement in OP case.
     
    Stormer2020 likes this.
  9. angrypenguin

    angrypenguin

    Joined:
    Dec 29, 2011
    Posts:
    15,509
    Before thinking about how to optimise it, I'd first ask why it needs to be optimised.

    Chasing low poly counts for its own sake is a waste of time. I'd only worry about it if there's a huge number of these meshes, or if they have to be shown at long distances where the triangles would be less than a few pixels across on screen, and where profiling has shown it to make a difference.

    Also note that a face in a modelling program can be any shape, where on your GPU they are triangles.
     
    Stormer2020 likes this.
  10. runner78

    runner78

    Joined:
    Mar 14, 2015
    Posts:
    760
    I would say that it makes perfect sense for a terrain in particular, especially with higher visibility. at a distance, a cube quickly becomes rather small.. And then there is the fact that, depending on the visual range, a large number of these chunk meshes are created, with a visual range of 25 chunks, there were already over 25,000 chunks (cubic chunks). Even if not everyone has a mesh, without occlusion culling many rendered in camera frustum.
     
  11. Zuntatos

    Zuntatos

    Joined:
    Nov 18, 2012
    Posts:
    612
    Stormer2020 likes this.