Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Bug Increase of the number of triangles after a Mesh.CombineMeshes

Discussion in 'General Graphics' started by Magnilum, Sep 20, 2023.

  1. Magnilum

    Magnilum

    Joined:
    Jul 1, 2019
    Posts:
    154
    Hello,

    I have been working on a tool to combine meshes to get a performance gain for a mobile game.

    But when I used my mesh combiner, everything works perfectly but the only problem I saw is an increase of the number of triangles.

    I set up a simple scene in HDRP to show you.

    upload_2023-9-20_15-20-47.png
    It is composed of a bunch of 3D object with a custom one I made in blender. It is the one on the right which is selected. It is compose of 3 submeshes so 3 materials as you can see on the picture.

    When nothing is combined, I have the following stats:

    upload_2023-9-20_15-23-47.png

    (Do not pay attention to the FPS, I am on my laptop with low energy consumption enabled)
    As you can see, the number of triangles is about 19.6k, which is huge but I know that is depends on lights used in the scene and other things.

    When I combine every object, here are the stats I have:

    upload_2023-9-20_15-28-8.png
    As expected, the number of batches decreased (from 130 to 92) which is a good thing but the number of triangle has increased (from 19.6k to 22.1k) and I don't understand why!

    Here is the mesh generated:

    upload_2023-9-20_15-31-13.png
    After the combination, every object is disabled and only the game object with the mesh and the materials is enabled:

    upload_2023-9-20_15-35-14.png
    The strange things is, when I turn the shadows off, as expected the number of triangle is lower and the mesh combination has no impact except the number of batches:
    - Batches: 93 -> 75 when combined

    But the number of triangles remains the same, why?
    - Triangles: 8.8k -> 8.8k when combined

    I am not surprised to see that the shadows have a impact on the number of triangles but I don't understand why there is a difference between uncombined and combined mesh.

    If you have any answer, I would be very interested in it.

    Have a good day.
     
  2. c0d3_m0nk3y

    c0d3_m0nk3y

    Joined:
    Oct 21, 2021
    Posts:
    549
    The statistics show the number of rendered triangles, so if a triangle is rendered twice, it will be counted twice.
    Meshes often have to be rendered in multiple render passes like depth-prepass, g-buffer pass, shadow map passes and motion vector passes.

    My guess is that previously Unity was able to cull meshes that were not in the current shadow cascade. Now it has to render all the triangles for each cascade.

    Therefore it is important to only combine meshes that are close to each other and can be culled altogether, like for example a table with items on top.
     
  3. Magnilum

    Magnilum

    Joined:
    Jul 1, 2019
    Posts:
    154
    I had it in mind, no problem on this side, I know that is almost useless to make only one mesh which covers 10km² because of the culling and many other things.

    I would like to use this to combine together meshes with the same material like a cluster of barrels or basket of fruits.

    So, what could I do to remove this double render since nothing is changing between the non combined and combined objects. I mean, it is always the same number of vertices. Do I have to delete the game objects instead of disable them? Is it a problem with the HDRP?

    I have no idea.
     
  4. kdgalla

    kdgalla

    Joined:
    Mar 15, 2013
    Posts:
    4,353
    If the problem is exactly what c0d3_m0nk3y says it is, then there is no way to prevent it other than to not combine those objects or not cast shadows on those objects. Maybe you could fiddle with shadow quality settings, but I doubt if that's worth the trouble.

    What about performance? Does performance improve even with the added triangles?
     
    c0d3_m0nk3y likes this.
  5. Magnilum

    Magnilum

    Joined:
    Jul 1, 2019
    Posts:
    154
    I have not check the performance yet since I am working on the modeling and texturing of my scene. I have just made some props and saw that I had a lot of triangles and vertices and since I am targeting mobile, I am scared that after 3 barrels, one bench and 5 tables, I wou't be able to add more props due to a a lot of batches and triangles.

    Moreover, i wanted to see with what I have done if anything could fit in a scene without too much problems or else, I would review my way of producing assets.
     
  6. kdgalla

    kdgalla

    Joined:
    Mar 15, 2013
    Posts:
    4,353
    Right, that's why you check performance. Extra triangles are probably not going to make any difference up to a point.

    Also, most optimizations have trade-offs. They don't always help in every situation.
     
    c0d3_m0nk3y likes this.
  7. Magnilum

    Magnilum

    Joined:
    Jul 1, 2019
    Posts:
    154
    I saw somewhere that for a mobile, it is almost 100k triangles and 100 batches. I think it depends on many things too like the AI or other computations in game but is it a right order of magnitude?
     
  8. kdgalla

    kdgalla

    Joined:
    Mar 15, 2013
    Posts:
    4,353
    BTW, if what we're talking about here is going to be static level geometry, have you tried baking the shadows in to a a lightmap? Theoretically that should eliminate the need for the "shadow passes" mentioned earlier. I'm afraid I don't know enough about Unity's renderer to say for certain, but it may be worth a try.
     
    Magnilum likes this.
  9. Magnilum

    Magnilum

    Joined:
    Jul 1, 2019
    Posts:
    154
    Yes it is going to be static so you may are right. I will try it.
    But I am facing an issue with large scale environment. How to bake light for very large area?