Search Unity

How do I reduce the polygons/tris/verts of a terrain in unity?

Discussion in 'General Discussion' started by Pigpork, Nov 20, 2021.

  1. Pigpork

    Pigpork

    Joined:
    Jul 4, 2017
    Posts:
    13
    I want to know how can I reduce the tris/verts of a terrain in a 3d unity game to be under 10k each
    And I have already done :
    - Reduced the far clipping plane of the fps controller camera to 90
    - Increased the pixel Error to the maximum of the terrain
    - Removed cast shadows from the terrain
    - Tried to export the terrain as an obj and reimport the obj into unity (somehow even tripled the verts and tris)


    And in the test scene (with just the fps controller, the directional light, terrain and one cube), I already have about 10-20 k tris and verts, please tell me how to reduce it further
     
  2. BIGTIMEMASTER

    BIGTIMEMASTER

    Joined:
    Jun 1, 2017
    Posts:
    5,181
    Its been a minute since I dealt with it, but AFAIK you get two ways to control the LOD of the terrain: Pixel error and base map distance.

    Unity - Manual: Terrain settings (unity3d.com)

    All these other things you mentioned won't effect the way the terrain LODS.

    Not sure why you think you need a terrain with less than 10k triangles. That's really low already. You could just use a mesh if its that big of a deal. The benefit of using the terrain system is to be able to get higher fidelity while making use of the auto-lod features.

    People have created custom solutions that are more performant than the unity in-built terrain tools.

    Foliage Optimization in Unity | Eastshade Studios

    Midway down the above article the author mentions some of the shortfalls they found with the unity terrain system and vaguely mentions what they did. But you'll need to understand how the system works pretty well to be able to write your own replacement.
     
  3. Pigpork

    Pigpork

    Joined:
    Jul 4, 2017
    Posts:
    13
    Oh okay thanks and I already reduced the base map distance to 150, and increased the pixel error to 200, so is there a way to increase the pixel error to over 200?
    And I have already read the unity manual page about terrain settings carefully.

    And for point 2, I have tried exporting my unity terrain to an obj, and then decimate it a ton in blender, yet when i place the newly decimated obj terrain in unity, im getting 3-4x more triangles and vertices on screen, vs the normal unity terrain for some reason

    And I'm pretty new so to say, in unity, since I haven't used it in years, so would it be feasible for me to make my own optimized terrain in a week or so?
     
  4. BIGTIMEMASTER

    BIGTIMEMASTER

    Joined:
    Jun 1, 2017
    Posts:
    5,181
    The triangle count on the .obj should be same in unity as it is in blender. That's something you want to verify to make sure it is import/exporting correctly.

    Making your own terrain system would be an advanced thing to do. I couldn't tell you what to do beyond the very basic premise. But I think it is something a programmer with some years of experience could manage without too much trouble. You might find some help from the jobs forums that is affordable.

    The basic idea isn't different to how LOD's work for any other sort of mesh really, except that the terrain is a contiguous mesh so you have to draw boundaries on it somehow. Usually it's done in a grid, hence square terrains with square, equidistant geometry. Then you define different layers of grids. The further from the camera a grid is, the more is gets unsubdivided. The closer you are, the more it subdivides.

    It may be worthwhile to explain what this terrain is meant to accomplish. What the game is, how big the terrain is, how fast player can move over it, what the graphics look like, etc. Because it may be possible to do something much simpler, like construct the world from just a few relatively low poly meshes and be done. And as always, regular static meshes can make use of LOD's but it's a very straightforward and efficient process just to manage a handful of models like that.


    edit* - and i dont know about taking overriding the limits of the values you can put in the inspector (pixel error and such). Of course it is certainly possible if you dig into the scripts but I don't know how involved that will be. And you assume that those limits are defined for some reason. Again, anything is possible with computers but it definitely gets more advanced and you'd probably need to ask in the specific forum for that topic, not general area.
     
  5. Pigpork

    Pigpork

    Joined:
    Jul 4, 2017
    Posts:
    13
    Ohh okay I see, and I want to make a very simple low poly terrain (like in the might and magic 6-8 games) for my game, and are about 2000x2000 (in terrain size), and for the terrain to be completely static (meaning nothing can be destroyed/modified ingame) and for it just to be a single model.
     
  6. BIGTIMEMASTER

    BIGTIMEMASTER

    Joined:
    Jun 1, 2017
    Posts:
    5,181
    Of course there could be something I don't know, but from looking at screenshots of those games I see top down camera, not very long views, and relatively low-poly graphics.

    In this case, I see no benefit to use the terrain system. I think you will save memory and have a more flexible workflow to work with regular meshes. The benefits of using the terrain system I think would be lost on a game like this.

    I think you could just use a plane, sculpt it a little bit in blender, slap some textures on, and send to unity and review how it looks. Perhaps the play area is one big plane, or maybe lots of smaller planes so that you could be occlusion culling them. Definitely background pieces could be separate meshes.

    Seams can be hidden very easily so there isn't really a need for a big contiguous mesh. And if you work with modular chunks, that may help you experiment with new designs more easily. You could build your prototype with a handful of chunks, even if it looks very repetitive and ugly. Then when you've figured out a good setup and the gameplay is working, then you can identify where you need custom meshes to take it from prototype to final draft.


    One thing that may be helpful though is to use the terrain system as a prototype because it allows you to make quick sculpt changes right in the editor. So if you wanted to do some quick experiments to see how different terrain shapes might look, you could do that with unity terrain. Then export it to use as an example.
     
    Last edited: Nov 21, 2021
  7. Pigpork

    Pigpork

    Joined:
    Jul 4, 2017
    Posts:
    13
    Ohhh okay I see and if I used a single large plane, will parts of the plane that are unviewable by the camera get culled? Or will the entire model be rendered at all times?
     
  8. BIGTIMEMASTER

    BIGTIMEMASTER

    Joined:
    Jun 1, 2017
    Posts:
    5,181
    I believe so. But don't take my word for it:

    Unity - Manual: Occlusion culling (unity3d.com)


    Quick glance through the docs and it looks like occlusion culling is delineating amongst game objects. So in most cases, that would be individual models/prefabs. There is more to it than that though. I think certain things get batched together. Perhaps based on materials. The key terms to look up would be "draw calls" and "batching". But I dont think you need to get super deep. Just follow best practices for environment artist and you'll be fine. Try to make efficient use with materials, use complex shaders strategically, reuse as much as you can.... don't worry about geometry too much. That is what computer does best. In some cases its better to use more geometry instead of extra textures like normal maps. Textures are what eat up the most memory and can really bloat a project.

    Another benefit to working with small modular chunks is that it gives flexibility. It is easy to combine things together. It is harder to separate them. So even if you learn something new, if you have the more flexible approach it is then easier to adapt if you want to change the way your system works.
     
    Last edited: Nov 21, 2021
  9. Pigpork

    Pigpork

    Joined:
    Jul 4, 2017
    Posts:
    13
    Ohh okay thanks for the advice, and I think that Im getting 300k tris and verts in a decimated terrain (obj file), vs alot less in the normal one, because there's no lods and the entire terrain model has to be rendered always, is there a way to make it behave like a normal unity terrain, or is there an asset on the asset store which allows for low poly terrain, while keeping the textures on the terrain (not turning the whole terrain into 3 colors with no detail).

    And I did look up on draw calls and batching, which helped, but how many draw calls is normal/ok for a 3d unity game?

    I also use the simplest textures and materials (no normal map, specular, etc, just a single plain texture per material, and the texture is usually 32x32 to 256x256 for non ui stuff, and is very compressed) And for shaders, what's the best one for performance? Since Im currently using legacy diffuse for everything and yea chunks would be a better idea than a large chunk
     
  10. BIGTIMEMASTER

    BIGTIMEMASTER

    Joined:
    Jun 1, 2017
    Posts:
    5,181
    Don't try to optimize first. Make the art look how it needs to look in the simplest way you can get it done.

    From there, it is much easier to figure out how to reduce and reuse your resources. But if you try to conform to some arbitrary standards before you do the work it will be impossible. You'll paint yourself into corners that dont exist.

    You'll need to understand how UV's and textures work. It would be well worth the time to do a handful of "beginner environment artist" tutorials.
     
    angrypenguin likes this.
  11. GimmyDev

    GimmyDev

    Joined:
    Oct 9, 2021
    Posts:
    160
    10 000 tris is 5000 quads
    a square terrain of 70 quads per side is 4900 quads (9800 tris)
    if you get chunks of 10 quads per side, a screen will have roughly 49 chunks (7x7 chunks array)

    If your terrain is top down, all you have to do is let unity cull with the frustrum far away terrain, each chunks would be it's own objects, you can probably precull by finding the center of the screen in world space and disable/enable all chunks beyond/within a distance.

    If you have a third person view with visible horizon, then LOD matter, it's basically the same, but you have for each chunks lower density mesh (for example 5x5) that get enable/disable by their own distance function. For further distance, you might consider merging lower density mesh (ie same quad size, but cover bigger space proportionally). Be careful that LOD transition might create cracks when you transition density.
     
  12. angrypenguin

    angrypenguin

    Joined:
    Dec 29, 2011
    Posts:
    15,620
    The terrain renderer component is not a standard mesh renderer, it does a bunch of internal optimisation. Keep in mind that it takes a height map as an input, as opposed to a mesh. It uses that to dynamically create an optimised mesh, so it's essentially an LOD system specific to that use case.

    The other thing you need to think about is how your terrain will be textured, assuming it will be. Terrains are usually textured using a different approach to standard meshes, with splat maps or a similar. This allows you to get a much higher visible texture resolution with a fraction of the resources.

    If you're making a top-down game then having your terrain as a mesh split into chunks may be a pretty effective way to go. Any chunk which is off-screen will be culled out, and there's no terrain system which has to keep updating a terrain mesh. I've heard of this being done on mobile in the past, I've no idea whether or not it's still a useful optimisation on modern devices.

    BTM's advice is solid. Start by doing whatever is required to get your game looking as intended. Then find out what actually makes it slow and learn how to fix those things. If you need to turn a terrain into a chunked mesh, for instance, there are tools for that.
     
    Dennis_eA likes this.
  13. Pigpork

    Pigpork

    Joined:
    Jul 4, 2017
    Posts:
    13
    Oh okay I see, but then even with just a blank canvas essentially, aka a scene with a small terrain and some 2d trees, and nothing else, seem to already eat up 160mb of ram, 10mb of vram , etc (checked in the profiler window) So im asking for help
     
  14. Pigpork

    Pigpork

    Joined:
    Jul 4, 2017
    Posts:
    13
    That sounds pretty complex, is there a way to do it with not much coding? And my game is a first person game, and I already reduced the far clip plane of the camera to only 75 (was 1000 originally) to reduce the graphic load
     
  15. GimmyDev

    GimmyDev

    Joined:
    Oct 9, 2021
    Posts:
    160
    There is no programming to do, it's the terrain budget calculation, if you have a close FOV it's not a problem to make LOD.

    Basically instead of making a big terrain, you break it into many mesh ie "chunks". Think like tiling.

    Example here is a terrain:


    It's made a little pieces (chunks) like that:


    Unity will cull those who aren't visible

    If you really need LOD, unity as a LOD system:
    https://docs.unity3d.com/Manual/LevelOfDetail.html

    Basically all you have to do is to make many version of the same chunk at various level of detailed like this:


    Low details get drawn at the distance, and detailed one get drawn close to the camera
     
  16. angrypenguin

    angrypenguin

    Joined:
    Dec 29, 2011
    Posts:
    15,620
    Have you tried reducing the heightmap resolution?
     
  17. Pigpork

    Pigpork

    Joined:
    Jul 4, 2017
    Posts:
    13
    Ohh okay thanks for telling me about it, and does unity automatically make far away objects be rendered in a lesser detail? And what program can I use to cut up a terrain model into chunks?
     
  18. Pigpork

    Pigpork

    Joined:
    Jul 4, 2017
    Posts:
    13
    I have, and it does nothing, except making the max height of the map be very limited
     
  19. hippocoder

    hippocoder

    Digital Ape

    Joined:
    Apr 11, 2010
    Posts:
    29,723
    But do you know why you want it under 10k? This mystery number will not automatically increase performance more than an efficient shader or less bandwidth.

    Saving verts usually only increases performance on modern hardware if the triangles were too small and caused a micro triangle issue (causes overdraw).
     
  20. GimmyDev

    GimmyDev

    Joined:
    Oct 9, 2021
    Posts:
    160
    1. You supply less detail model and unity switch it for you at your specified distance, it doesn't make it less details on its own.

    Alternative solution, you break the unity terrain into smaller unity terrain, since they have automatic decimation at distance, I don't know if that would works though since the whole terrain system is supposed to work like that (ie cull part outside of camera and decimate at distance). That would be something to try and then profile.

    2. How are you making the terrain in the first place?