Search Unity

Level of Detail management (LOD)

Discussion in 'Editor & General Support' started by twintower31, Nov 14, 2007.

  1. twintower31

    twintower31

    Joined:
    Oct 31, 2007
    Posts:
    89
    Hello,

    I don't really try (just a few yours) Unity, but I searched in documentation and forum and I didn't find anything on how LOD is managed.

    My app can contain 15 to 30 same characters with 5000 vertices. Of course, I don't need really 5000 vertices if some characters are far away.

    Should I have several models with different LOD, and import in Unity3d or something different ?

    If Unity is capable to optimize the management of the character if it's the same character in the scene ?

    Sorry if I missed the information in the doc.

    Thanks,

    Twin
     
  2. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    Yep. The terrain engine has built-in LOD, but aside from that it's up to you how you want to manage it. (Which is for the best since it varies depending on what you're doing.)

    --Eric
     
  3. Aras

    Aras

    Unity Technologies

    Joined:
    Nov 7, 2005
    Posts:
    4,770
    There's no built-in LOD for characters. However, it's quite easy to build multiple LOD versions of character meshes, and have a script dynamically change the mesh used based on the distance.

    Like Eric5h5 says, there's tons of totally different uses that people use Unity for, and a classic FPS-style LOD system might not be suitable for everyone.
     
  4. twintower31

    twintower31

    Joined:
    Oct 31, 2007
    Posts:
    89
    Thanks for the response.

    So, If I have 20 characters,
    on each Update(), I have to calculate the distance from the characters to the Camera and switch between a high and a low detailed model. By switching : I mean instanciate a new GameObject or it's better to create first 3x20 characters (3 for 3 levels of detail) at the beginning of my game and switch between after during the Update().

    Do someone has already manage LOD with several characters/models in Unity3d ?

    Thanks for your advice,

    Twin
     
  5. Aras

    Aras

    Unity Technologies

    Joined:
    Nov 7, 2005
    Posts:
    4,770
    No need to instantiate lots of objects or replace the objects. You can just change the mesh that is used in the MeshFilter component. Something like this (a very simple example with two LODs for clarity, assign meshes to be used in the inspector):

    Code (csharp):
    1.  
    2. var highLod : Mesh;
    3. var lowLod : Mesh;
    4. var distance = 10.0;
    5.  
    6. function Update ()
    7. {
    8.     var campos = Camera.main.transform.position;
    9.     var meshFilter : MeshFilter = GetComponent(MeshFilter);
    10.     if( (transform.position - campos).sqrMagnitude <
    11.         distance * distance )
    12.     {
    13.         // use high LOD
    14.         if( meshFilter.sharedMesh != highLod )
    15.             meshFilter.sharedMesh = highLod;
    16.     }
    17.     else
    18.     {
    19.         // use low LOD
    20.         if( meshFilter.sharedMesh != lowLod )
    21.             meshFilter.sharedMesh = lowLod;
    22.     }
    23. }
    24.  

    I think someone did use model switching similar to the above. Don't remember who...
     
    jovino likes this.
  6. twintower31

    twintower31

    Joined:
    Oct 31, 2007
    Posts:
    89
    Ok,

    I'm not very familiar with the MeshFilter but according to the doc, it shows or hide Meshes.

    If I use your code, if some characters are running, the
    switch between LOD will stop the current animation, because it's not the same GameObject ?
    Am I wrong ?

    Tw<in
     
  7. Aras

    Aras

    Unity Technologies

    Joined:
    Nov 7, 2005
    Posts:
    4,770
    All it does is changes which Mesh is used to display a character. The game object is not changed or switched at all.

    So animations should just work, scripts should continue running and so on; all it does is changes which mesh is used.
     
  8. twintower31

    twintower31

    Joined:
    Oct 31, 2007
    Posts:
    89
    When you talk about the update() function, is it the update function linked to each GameObject ?

    Because my app should create dynamically the characters , where definitions and positions comming from a XML file, I can not generate the the content of such an Update() function...

    Many 3D Engine propose such a feature, switching LOD with distance parameters, even generating the LOD inside the Editor. I think it should be considered as an enhancement for Unity3d team.
     
  9. Aras

    Aras

    Unity Technologies

    Joined:
    Nov 7, 2005
    Posts:
    4,770
    It's a script (my example above), that you attach to any object that you want to be LODed. So this function will execute on any game object the script is attached to. If you'll attach it to 20 characters - well, 20 characters will be LODed.

    If you will be instantiating those object from prefabs (highly recommended), then just include the script in a prefab. If you'll be assembling the objects completely from scratch, just add this script to any object you want to be LODed (using AddComponent).


    The script above does exactly that (with two LOD levels, but it's easy to expand)... While I agree that it's a nice to have feature, doing it manually is also quite easy. And the LOD logic is often very dependent on game specifics.
     
  10. twintower31

    twintower31

    Joined:
    Oct 31, 2007
    Posts:
    89
    Thanks for your quick and precise response.

    Your solution is perfect for my needs. I didn't imagine that Unity is so flexible, powerful and well architectured !

    This comment closes the post.
     
  11. holyfot

    holyfot

    Joined:
    Dec 16, 2016
    Posts:
    42
    For those of you googling and come across this post. I have made a LOD script for Characters/SkinnedMeshRenderers:

    https://pastebin.com/K1S4Zjh0
     
    wangzhijiang likes this.