Search Unity

  1. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Could DOTS fix the Unity terrain Tree performance problem?

Discussion in 'Entity Component System' started by Arowx, Jul 14, 2020.

  1. Arowx

    Arowx

    Joined:
    Nov 12, 2009
    Posts:
    8,194
    There has been a performance issue with Unity Terrain and Trees for quite a few years now (forum link)

    So could DOTS come to the rescue and provider super large forested terrains with amazing performance?

    After all it managed to make the mega city demo and provide a sea filled with millions of fish so why not a forest or jungle of trees?
     
    chanfort likes this.
  2. tertle

    tertle

    Joined:
    Jan 25, 2011
    Posts:
    3,735
    They are already working on a large scale dots terrain solution I believe?
     
    Last edited: Jul 15, 2020
  3. snacktime

    snacktime

    Joined:
    Apr 15, 2013
    Posts:
    3,356
    Vegetation studio came to the rescue a while ago.

    There are a couple of minor things in VS that could be improved by some of the new graphics api's, but for the most part it's already doing stuff right.

    Hybrid Renderer V2 has potential but also a lot of problems. We are still undecided on whether we will use it or BatchRendererGroup directly at release. And are gambling a bit on if either will be ready a year from now. DOTS rendering hasnt' been a smooth ride so far.
     
  4. Neto_Kokku

    Neto_Kokku

    Joined:
    Feb 15, 2018
    Posts:
    1,751
    Unity's default terrain+foliage+tree combo has been utter rubbish for several years and seems nobody at the company is willing to touch that mess. So we are left juggling asset-store solutions, DIY solutions, or waiting for their miraculous new (old) tech that is forever two years away.

    From my experience, I can only guess (because the terrain system is one of the most opaque of Unity's black boxes - even getting staff to spill the beans on its inner working is rare) that the CPU problems are caused by shoddly-written single-threaded code for culling and redundantly setting the plethora of material parameters used by the terrain and its brethen.

    Basically, the terrain was written for a time when PCs, consoles and mobiles were so weak you wouldn't notice its CPU cost because your game would struggle with everything else. Time passed, but it was never modernized to scale. Just like ports of very old games for modern consoles need to be re-factored to run well (examples: Ion Fury, Turok), code designed to deal with N things ten years ago often won't magically be able to handle N^2 things on newer hardware as is.
     
  5. runner78

    runner78

    Joined:
    Mar 14, 2015
    Posts:
    777
    Unity currently working on an new Environment System with non-destructive layers.
     
  6. Arowx

    Arowx

    Joined:
    Nov 12, 2009
    Posts:
    8,194
    It looks like the function/method PerpareSceneCullingParameters is one of the performance bottlenecks. Maybe a conversion on how it works to multi-threading (with a dash of Burst optimisation) would be enough as an interim solution without having to go full DOTS?
     
  7. Neto_Kokku

    Neto_Kokku

    Joined:
    Feb 15, 2018
    Posts:
    1,751
    Someone at Unity needs to bother first. But they hate working on existing code and prefer shiny new things, so the terrain/foliage/trees trio will remain unofficially deprecated until they bring their new terrain which I bet $5 bucks will only work with SRP/ECS, be experimental for two years, and miss some fundamental feature or two.

    My advice? Forget the terrain trees and see if one of the myriad asset store replacements suit your needs.
     
    Krajca likes this.
  8. runner78

    runner78

    Joined:
    Mar 14, 2015
    Posts:
    777
    The new Environment System if build on top of the exisiting terrrain system.
     
  9. PhilSA

    PhilSA

    Joined:
    Jul 11, 2013
    Posts:
    1,926
    Do you have a link for that? That would certainly be worrying
     
    Last edited: Jul 15, 2020
    Nyanpas, NotaNaN and dzamani like this.
  10. runner78

    runner78

    Joined:
    Mar 14, 2015
    Posts:
    777
    Unfortunately not, either I read it here in the forum or in one of the Unity now videos. I think Unity will only use the heigtmap / mesh part of the terrain.
     
  11. SenseEater

    SenseEater

    Joined:
    Nov 28, 2014
    Posts:
    84
    Currently there is VegetationStudioPro if you need something off the shelf right now that uses Jobs/Burst to procedurally place as much foliage you want with great performance. Its has its own LOD & Culling system. Worth looking into.
     
    ysundawa, Nyanpas and florianhanke like this.
  12. Arowx

    Arowx

    Joined:
    Nov 12, 2009
    Posts:
    8,194
    Even in the 2020 state of Unity blog post no mention of it but 3 calls for terrain and foliage improvements in the comments.
     
    Last edited: Jul 16, 2020
    Nyanpas likes this.
  13. runner78

    runner78

    Joined:
    Mar 14, 2015
    Posts:
    777
    I'm looking for a while for the source where I read it, I'm not sure of myself anymore.
     
  14. PhilSA

    PhilSA

    Joined:
    Jul 11, 2013
    Posts:
    1,926
    Is it possible you're remembering info about the "new terrain tools" and not the new DOTS environment system?
     
  15. SenseEater

    SenseEater

    Joined:
    Nov 28, 2014
    Posts:
    84
    Probably this


    upload_2020-7-16_19-7-33.png

    From timestamp :

     
  16. Arowx

    Arowx

    Joined:
    Nov 12, 2009
    Posts:
    8,194
    Without it specifically going into detail about foliage, roads and cities it could just be referring to the improvements in the terrain generation tools (the demo only shows terrain manipulation).

    The thing is the current system could probably be fixed to work with more foliage and quite quickly.

    Let's hope Foliage is not the new Prefabs (what everyone wants to see working in the engine but not a fancy and new enough project to excite the developers).
     
  17. chanfort

    chanfort

    Joined:
    Dec 15, 2013
    Posts:
    641
    Wasn't sure if you saw my posts in World Building forums. But Unity trees can already handle millions of trees if they are set through billboard renderer. And tens of thousands when using custom imposters. I am also quite keen to wait and see what comes up with Environment System as well but from talks slides only saw empty landscapes there without trees, so not sure if trees will be there. What would be good if trees would be written in DOTS would be C# approach which would allow us to better customise these...
     
    Arowx likes this.
  18. Arowx

    Arowx

    Joined:
    Nov 12, 2009
    Posts:
    8,194
    So do we need Unity to make a Mega Woods/Jungle demo or is the technology good enough to write our own?
     
  19. Nyanpas

    Nyanpas

    Joined:
    Dec 29, 2016
    Posts:
    406
    I am GPU-instancing around 900 million Creosote-bushes for my WebGL-game at 60 fps with custom jobs-based occlusion culling. It was not that hard to set up either except the calculations for how to set the density/spread of the shrubberies. (And I forgot about shader upwarmings but I know about it now...)
     
    Last edited: Jul 20, 2020
    Krajca and Arowx like this.
  20. Arowx

    Arowx

    Joined:
    Nov 12, 2009
    Posts:
    8,194
    Cool could you share some screenshots and more info on how you got this to work?

    This looks like an amazing in depth look at the current technology could you summarise your findings and update us on what is happening?
     
    chanfort likes this.
  21. chanfort

    chanfort

    Joined:
    Dec 15, 2013
    Posts:
    641
    So, here is my summary. Tree billboard renderer is pre - SpeedTree era of creating massive terrain forests in Unity. It is capable of rendering millions of trees. In order to set trees to work with billboard renderer, tree prefabs must not contain more than one MeshRenderer, LOD Groups and children game objects. Prefabs need to use Soft Occlusion shaders in order to work correctly. See my YouTube tutorial on how to setup it this way. Once set, it's easiest to spawn millions of trees with a basic script by randomising positions in XZ plane and getting heights in order to put trees on the surface of terrain.

    Since billboard renderer is for a while in Unity, it seems to be compatible with multiple platforms and works on older devices as well without need support for instancing and other modern techniques. So we can build retro games with massive forests this way :) .

    Once SpeedTree trees came into Unity, it became possible to use more complex trees with LOD groups, nested children game objects and multiple mesh renderers. However, since most of the games uses just several hundreds or several thousands of trees, performance issues didn't shown up so obviously and billboards became a bit forgotten. The performance of using this method is several orders of magnitude lower compared to billboards for large numbers of trees.

    Billboarding could have been forgotten also because of it's limitations, i.e. not supported tree instance rotations, no shadows, no interaction with wind in billboard mode, lighting refreshes only when rotating camera while in dynamic time of day environment, etc. The worrying bit here is that billboarding with Soft Occlusion shader is only supported in Built In render pipeline and not in URP nor in HDRP.

    So, due to these limitations I was also keen to look a bit more into imposters technique. It is more generic solution, which can be used not just for trees but for any object, including moving and rotating objects on runtime. It is much better for performance than to use SpeedTree method with LOD Groups and children game objects. While it gives greater quality, it has different limitations. This includes that instead of millions it's possible to use several tens of thousands of tree instances. Shader also still does not support shadows for imposter instances and largest numbers of trees at playable framerates can only be achieved using instancing. Underneath, imposters also use billboarding but in a different way.

    So if Unity at some point will write tree and grass systems, it would definitely need to include some sort of billboarding systems which could be easily configured by users.
     
  22. Nyanpas

    Nyanpas

    Joined:
    Dec 29, 2016
    Posts:
    406
  23. SenseEater

    SenseEater

    Joined:
    Nov 28, 2014
    Posts:
    84
    All i could see was solid pyramids...

    upload_2020-7-20_15-40-12.png

    Being able to push gazillion vertices aside, you need to at-least use alpha testing shader which is generally used for foliage and vegetation rendering.

    Not to take anything away from what you are doing but generally foliage rendering is bit more involved due to Vertex displacement & Alpha testing in place. Pushing crazy amount of identical geometry is relatively trivial task on Modern GPUs and APIs with techniques like Instancing & LOD solutions ( billboard , impostor , regular LOD ) etc.
     
    Nyanpas likes this.
  24. Neto_Kokku

    Neto_Kokku

    Joined:
    Feb 15, 2018
    Posts:
    1,751
    Are those Unity auto GPU-instanced, or are you manually instancing using Graphics.DrawMeshInstanced()?

    When using instancing for high-amount low-triangle things like grass you can improve performance by merging multiple copies of the mesh into a single mesh, then using SV_VertexID to figure out the "sub-instance" index.

    Instancing isn't free: there is a small cost per instance compared to just drawing a merged mesh. The disadvantage of merged meshes is that they increase memory consumption and can be hard to cull if their dimensions is too large. By combining the two, you can reach a balanced solution.
     
    Nyanpas, charleshendry and yondercode like this.
  25. Nyanpas

    Nyanpas

    Joined:
    Dec 29, 2016
    Posts:
    406
    That is unfortunately a very early prototype test that was to see if I at least could get something to work. I am aware of the + and the - of the different solutions but for the final version it will be slightly different again. It already "clumps" the plants together in groups of 11 (1 in the middle and 5 and 5 around it) so it has merging, but due to only using the plane normal as the height offset some shrubs will be in the ground/air when approaching edges, so for the next version it will be better. There is also vertex displacement but very subtle. I outright dropped transparency because I intend for this to run on a Switch at 60 fps, and also I can make it work with the artstyle.

    In the case about transparencies, have you seen this?
    https://medium.com/@bgolus/anti-aliased-alpha-test-the-esoteric-alpha-to-coverage-8b177335ae4f
     
    florianhanke likes this.