Procedual terrain - How reduce vertexes?

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:

to

Current :

How merge the meshes at same face? Is there have any solution about this? Thank you!

Look for marching cube solutions and algorithms.

If you search marching cubes, you will find tons of resources on it.

2 Likes

Cool! Thank you so much~

You could look at blender’s dissolve code, or try to look at Unreal’s modelling tools source code, or find something on Github.

If you know all the meshes you want to combine into one, then you can just use Unity - Scripting API: Mesh.CombineMeshes 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.

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.

1 Like

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.

8400255--1109046--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.

8400255--1109055--upload_2022-8-29_23-6-26.png

1 Like

Another algorithm is a greedy meshing.
It perhaps is more suitable / easier to implement in OP case.

1 Like

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.

1 Like

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.

The answer OP is actually looking for is greedy meshing & friends, see this blog Meshing in a Minecraft Game – 0 FPS and part 2 here Meshing in a Minecraft Game (Part 2) – 0 FPS

1 Like