Search Unity

Question Question about LOD implementation

Discussion in 'Shader Graph' started by janidsa, Apr 11, 2021.

  1. janidsa

    janidsa

    Joined:
    Aug 3, 2015
    Posts:
    5
    Hey guys, I've got a few newbie questions I'd like to ask, but before that, here's what I've got.

    I am rendering thousands of trees using GPU DrawMeshInstanced, and I am using a shader that I created for them in shader graph. From there, I would like to implement LOD that turns my meshes into billboards at far distances, but I am unsure how should I approach this.

    As far as I know, because I am rendering them using GPU DrawMeshInstanced, I am unable to use the regular LOD systems that one might attach into gameobjects normally.

    Would it be sensible to implement billboarding within the shader graph? Can the shader graph omit "useless" processes that aren't going to be applied into the end result with some form of an if node, or would I have to write that in code like "If (distanceToCamera < billboardDistance), draw mesh, else draw billboard" to ensure proper optimization?

    Thanks.

    EDIT: Here is an image of the current result what some of my trees look like near and far: http://puu.sh/Hx3rz/43953b6327.jpg
     
  2. Camarent

    Camarent

    Joined:
    Feb 19, 2014
    Posts:
    168
    I would recommend you look at https://docs.unity3d.com/Manual/CullingGroupAPI.html
    This api can help you to create custom lods but it still requiere to work on CPU. So you kinda process all culling events and determine where will be tree or bilboard then you draw with two step: models and bilboards.

    It is not the only way but I think the easiest to work with. Another can be combination of spatial grid system on CPU and compute shader lod system on GPU. You can increase scalability but it is more complex.
     
  3. janidsa

    janidsa

    Joined:
    Aug 3, 2015
    Posts:
    5
    Thanks for the pointers!

    I think I am already (sort of?) using the latter to some extent, if I've understood correctly. Just in case I am not, here's what I am doing:

    I have a seamless open world divided into chunks on a grid, in which I load the nearest 9 chunks containing terrain, trees, etc, where I store trees into a matrix 4x4. Once a chunk is loaded, it will grab all the stored matrices, and begin rendering them on GPU.

    Originally I was thinking that I would try to do the LOD system on GPU, but since you recommended using the CG API, does it work with matrices or something else that does not require instantiating actual gameobjects? I initially instantiated every object upon loading a chunk, but that would immediately cause a huge CPU bottleneck upon loading the chunks, which is why I moved it to GPU.
     
  4. Camarent

    Camarent

    Joined:
    Feb 19, 2014
    Posts:
    168
    CG API works with so called bounding sphere so it will not use any gameobjects at all. it will send you events when a sphere became visible or not. You manually control position of all spheres. If you plan to use it read carefully all recommendation from API page because it has few not obvious tricks.

    It is good that you already use grid system. You can create ComputeBuffer that will lod trees on gpu for specific grid but it kind of hard to tell if it will be more efficient than using CG API. One criteria for deciding what to use is to profile and check if you CPU bound or GPU bound. Depends of where you have more power to perform culling can help you to choose.

    Well, as everyone says you need to test and decide what suits you best.