Search Unity

Lag Issue Due to High Vertices in 3D

Discussion in 'General Graphics' started by DeemBabbu, Jun 24, 2020.

  1. DeemBabbu

    DeemBabbu

    Joined:
    Jul 18, 2019
    Posts:
    26
    I have 3D models with high vertices in my scene rendering at the same time which leads to lag issue in-game and the game slows down FPS fall down too much lower. is there any solution for that? upload_2020-6-24_11-23-35.png
     
  2. aleksandrk

    aleksandrk

    Unity Technologies

    Joined:
    Jul 3, 2017
    Posts:
    3,028
    Yes, there is.
    Search for LOD :)
     
  3. DeemBabbu

    DeemBabbu

    Joined:
    Jul 18, 2019
    Posts:
    26
    @aleksandrk upload_2020-6-24_11-38-41.png
    this is the whole model made up of small sphere meshes.

    upload_2020-6-24_11-43-38.png

    using the dynamic batching technique

    Dynamic batching (Meshes)
    Unity can automatically batch moving GameObjects into the same draw call if they share the same Material and fulfill other criteria. Dynamic batching is done automatically and does not require any additional effort on your side.

    • Batching dynamic GameObjects has certain overhead per vertex, so batching is applied only to Meshes containing no more than 900 vertex attributes, and no more than 300 vertices.
      • If your Shader
        is using Vertex Position, Normal and single UV, then you can batch up to 300 verts, while if your Shader is using Vertex Position, Normal, UV0, UV1, and Tangent, then only 180 verts.
      • Note: The attribute count limit might be changed in the future.
    as referred to in https://docs.unity3d.com/Manual/DrawCallBatching.html.

    problem is I cant decrease more detail in the sphere.

    is there any other solution for this ?
     
  4. aleksandrk

    aleksandrk

    Unity Technologies

    Joined:
    Jul 3, 2017
    Posts:
    3,028
    oh wow
    this will lag, for sure :D
    try instancing? :)
     
  5. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,352
    This is an extreme case for dynamic batching. There are highly optimized dynamic batching with particle systems and sprites, but many really anything over a few thousand vertices is quickly going to become slow.

    I'd highly recommend using camera facing quads with a normal map and alpha tested transparency to make it look like a sphere instead of geometry, on top of using instancing.
     
    Tartiflette likes this.
  6. DeemBabbu

    DeemBabbu

    Joined:
    Jul 18, 2019
    Posts:
    26

    Already tried Man :( No Luck
     
  7. aleksandrk

    aleksandrk

    Unity Technologies

    Joined:
    Jul 3, 2017
    Posts:
    3,028
    What's your target hardware?
    It might be that raytracing all the spheres that intersect the camera frustum is the cheapest way here :)
     
  8. DeemBabbu

    DeemBabbu

    Joined:
    Jul 18, 2019
    Posts:
    26
    Smart Phones (iOS and Android Devices)

    I don't have much idea about raytracing. Let me get some data about that.
     
  9. aleksandrk

    aleksandrk

    Unity Technologies

    Joined:
    Jul 3, 2017
    Posts:
    3,028
    no, in that case it's probably not worth it
    you can still use LOD stuff - replacing spheres with e.g. capsules when the object is further away
     
  10. DeemBabbu

    DeemBabbu

    Joined:
    Jul 18, 2019
    Posts:
    26
    Thanks For Your Help. Lot of new things I have learned. :)
     
    aleksandrk likes this.
  11. aleksandrk

    aleksandrk

    Unity Technologies

    Joined:
    Jul 3, 2017
    Posts:
    3,028
    Just in case - replace several spheres with one capsule :)
     
  12. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,352
    The real issues here aren't so much in the vertex count in itself, mobile GPUs are totally capable of rendering ~1m vertices at acceptable framerates. The issue here is the use of so many game objects & dynamic batching being done for so many meshes every update. You're getting CPU limited.

    The solution is don't use a bunch of dynamic game objects, and don't use dynamic batching. If the magnet structures are mostly static, then switching to building them only when they change (and hopefully that's not too often) will speed things up significantly. You can take a bunch of separate mesh renderers and combine them into a single mesh using Mesh.CombineMeshes() that you render by itself.


    However, you'd still be limited by the number of different colors you're rendering, which I'm guessing you're assigning via different materials. You'd be much better off with instancing or using vertex colors. You say you tried instancing, but if you have dynamic batching enabled, and you don't have your materials set to use instancing, Unity will usually default back to dynamic batching. You must disable dynamic batching for it to work. You'd also get a lot more mileage with the instancing if you used a shader that supported instanced color values, which the default Standard shader does not. Instancing may also be much slower to render on the GPU and if you rely on Unity's built in auto-instancing, it may still take quite a bit of CPU time.

    The best solution would be to write a custom instanced shader that also has an instanced color property, and drive that with a Graphics.DrawMeshInstanced() or better yet Graphics.DrawMeshInstancedIndirect() so you can customize it further with only a position, 1 dimensional size, and color and have the shader orient a quad that looks like a sphere towards the camera.

    The easier way to get here without needing to write as much custom code yourself may to abuse particle systems. You can create a mostly empty particle system, with a material using Particle/Standard Surface shader. Then you can use SetParticles() to set the Particle array with the positions, and colors you want and let the particle system handle the rest of it. By default the particles will be doing something similar to being dynamically batched, but it should be much faster than "real" dynamic batching.
     
  13. DeemBabbu

    DeemBabbu

    Joined:
    Jul 18, 2019
    Posts:
    26
    Let me try this
     
  14. DeemBabbu

    DeemBabbu

    Joined:
    Jul 18, 2019
    Posts:
    26

    Thanks for your time.I'll see into it
     
  15. aleksandrk

    aleksandrk

    Unity Technologies

    Joined:
    Jul 3, 2017
    Posts:
    3,028
    Keep in mind that this will not work on all Android devices. Also, Mali GPUs will not allow you to use ComputeBuffers outside of fragment and compute shaders when running on OpenGL ES.
     
    bgolus likes this.