Search Unity

Optimizing a big map (Shadows,Lights,GI ...)

Discussion in 'General Graphics' started by Opeth001, Nov 19, 2019.

  1. Opeth001

    Opeth001

    Joined:
    Jan 28, 2017
    Posts:
    1,117
    Hello Everyone,

    i have a scene with a map ~1.4KM² and around 400K GameObjects.

    !im totally new to Scene optimization!

    im trying to bake my whole scene lights and shadows, but each time the Unity Editor crashes in the midle of the baking process, i think i certainly did something wrong.

    i have few Questions about scene optimization.

    1) 99% of my GameObjects are static, some of them are big gameobjects other are small ones.
    what is the best approach to bake their Shadows ?

    2) i read on some forums that it's not good to always combine close GOs by marking them as Static sometimes it's better to use the Dynamic Batching instead.
    in which case should i use Dynamic Batching or Static Batching?
    how can i do it ?

    Thank you!

    [Edit] my game is a TopDown style.
     
    Last edited: Nov 22, 2019
  2. GuardHei

    GuardHei

    Joined:
    Feb 10, 2018
    Posts:
    89
    try dividing the scene into multiple scenes and load them async at runtime only when needed
     
    Opeth001 likes this.
  3. GuitarBro

    GuitarBro

    Joined:
    Oct 9, 2014
    Posts:
    180
    Another thing to consider is whether or not static lighting is even appropriate for your scene to begin with. You'll find that lightmaps tend to eat up space quite quickly on large maps (assuming you're baking with enough resolution that it looks decent) and are generally better suited to interior scenes where the bounds of the level tend to be smaller.

    In this case, you may actually find that mostly/fully dynamic lighting is worth the tradeoff here, though obviously global illumination (or the lack thereof) will likely be one of the hardest compromises. Unity promised a new GI solution coming around 2021 which should hopefully alleviate this issue, but that's a ways off still. In the meantime, you may see if you can get away with baked GI at a really low resolution + dynamic shadows to at least cut down on bake time and some storage cost.

    That being said, mesh batching/combining is still important for performance regardless of lighting technique, so your 2nd question is still valid. In general, the main drawback to combining meshes is the ability to cull them separately and use LODs (since it's now one big object). If you think the meshes will nearly always be rendered together and it makes sense to combine them, then combine away. However, it may actually be better to leave them separate so that you can use LODs to reduce the amount of polygons you have to render overall (in addition to allowing them to be culled). A great addition to this technique is to use an impostor as the final LOD level (I personally have gotten good results with Amplify Impostors on the asset store, though it's $60 at the time of writing) which lets you cut objects down to just a billboard with a bit of fancy trickery going on to fake depth/rotation.

    If you do leave them separate, I believe static batching is generally preferred for runtime performance and dynamic is better for less memory usage (see here: https://docs.unity3d.com/Manual/DrawCallBatching.html). Even better though, is if you are using the same material for many of them and they can be GPU instanced. The standard shader has a checkbox to enable instancing, so if you're just using the standard shader, I'd say give that a shot fist since it's easy and see how much it helps. If it's not enough (or the materials are can't be shared), batching the objects probably would be a good idea.

    Hope that helps answer your questions.
     
    Sebasxs, hippocoder and Opeth001 like this.
  4. Opeth001

    Opeth001

    Joined:
    Jan 28, 2017
    Posts:
    1,117
    Thanks @GuitarBro
    I forgot to specify that my game is a top down game so i don’t need LODs.
    I’m using unity 2019.3 and I’m facing a little problem I can’t bake shadows I marked all my gameobjects as static and after the baking process all shadows still dynamic.
    Also I have another question:
    is it possible to use GPU instancing and bake shadows at the same time ?

    For more infos, most of my objects are small props with same material and casting shadows.(grass...)
     
    Last edited: Nov 22, 2019
    ssingl2582 likes this.
  5. GuitarBro

    GuitarBro

    Joined:
    Oct 9, 2014
    Posts:
    180
    Ah, top down is even better because you can easily figure out what's offscreen and hide it. In that case, @GuardHei's suggestion of splitting the scene would be good on a large scale (don't want to make it too granular or you'll be constantly loading/unloading stuff as you move) but for smaller scale stuff, simply making sure that culling is enabled should help.

    As for the shadows, I'm not 100% sure, but I don't believe Unity allows baked shadows and GPU instancing. In that case, you'd probably want to use static batching for as much as possible and let dynamic objects be dynamically batched (as possible). If you split the scene, you could at least bake them individually which should be much faster to update if only one scene changes.
     
    Opeth001 likes this.
  6. Opeth001

    Opeth001

    Joined:
    Jan 28, 2017
    Posts:
    1,117
    @GuitarBro do you have any idea why shadows still dynamic even for static GameObjects ?
     
  7. GuitarBro

    GuitarBro

    Joined:
    Oct 9, 2014
    Posts:
    180
    Do you have baked lighting enabled in your lighting settings and the light casting the shadows has them set to "baked"?
     
  8. Opeth001

    Opeth001

    Joined:
    Jan 28, 2017
    Posts:
    1,117
    ScreenShot1 :



    ScreenShot2:
     
  9. GuitarBro

    GuitarBro

    Joined:
    Oct 9, 2014
    Posts:
    180
    Seems fine, but what about the Light itself?
     
  10. hippocoder

    hippocoder

    Digital Ape

    Joined:
    Apr 11, 2010
    Posts:
    29,723
    I'd suggest baking nothing at all for those scales. Use realtime shadows, control reflection probe / shader for a better bounce lighting look.

    You'll bake forever at those dimensions or you will be reducing the resolution so much it doesn't make sense. You have hit an AAA wall.
     
    jeroll3d and GuitarBro like this.
  11. Opeth001

    Opeth001

    Joined:
    Jan 28, 2017
    Posts:
    1,117
    Shadows are really changing the game visuals and makes it much better but using the real-time makes it unplayable for Low end devices.
    For examples in a Oneplus2 in certain regions the game goes under 20fps when still 60fps for SamsungS8 ( high drawcalls ).
     
  12. Opeth001

    Opeth001

    Joined:
    Jan 28, 2017
    Posts:
    1,117
    Mixed light.
     
  13. GuitarBro

    GuitarBro

    Joined:
    Oct 9, 2014
    Posts:
    180
    I believe that should work allow for some baking, assuming the objects are static. There may be something else for baking that I'm not remembering needs to be set (I almost never bake lights these days for the reasons @hippocoder mentioned).

    As for performance of realtime shadows though, have you made sure your shadowmap distance is set appropriately in the quality settings? You want to run it with the smallest distance and lowest resolution that looks acceptable. Realtime shadows get very costly the bigger they are.
     
    Opeth001 likes this.
  14. Opeth001

    Opeth001

    Joined:
    Jan 28, 2017
    Posts:
    1,117
    @GuitarBro my problem is im having a lot of small props concentrated in certain areas, so the realtime Shodows are creating high draw calls numbers (~250 ), which slows the rendering in mobile devices.
    this is why im trying to bake as much a possible.
     
  15. GuitarBro

    GuitarBro

    Joined:
    Oct 9, 2014
    Posts:
    180
    Those sound like they may be good candidates for combining the meshes. Another option is to disable casting shadows for those objects since they're small. Using a screenspace contact shadow solution might help make that more bearable, but you'd pay the cost of screenspace shadows then which may or may not be acceptable.

    As for why it's still using realtime shadows, did you make sure your Shadowmask mode is set to "Shadowmask" not "Distance Shadowmask" in the quality settings? If it's using "Distance Shadowmask" it chooses to use realtime shadows up to the shadow distance and falls back to baked only after that distance.
     
  16. Opeth001

    Opeth001

    Joined:
    Jan 28, 2017
    Posts:
    1,117
    yes i just verified and still not baking the Shadows.
    Note: the baking time is very low arround 30 seconds.
     
  17. GuitarBro

    GuitarBro

    Joined:
    Oct 9, 2014
    Posts:
    180
    Hm. The only other thing I can think of is that the objects are not marked as lightmap static. If they are, then I've got no clue. :confused:
     
  18. Opeth001

    Opeth001

    Joined:
    Jan 28, 2017
    Posts:
    1,117
    i dont see any lightmap static option.

     
  19. GuitarBro

    GuitarBro

    Joined:
    Oct 9, 2014
    Posts:
    180
    Oh apparently in 2019.2 they removed "Lightmap Static" and replaced it with the "Contribute GI" flag. Check that one and see if it that does it.
     
  20. GuitarBro

    GuitarBro

    Joined:
    Oct 9, 2014
    Posts:
    180
  21. Opeth001

    Opeth001

    Joined:
    Jan 28, 2017
    Posts:
    1,117
  22. GuitarBro

    GuitarBro

    Joined:
    Oct 9, 2014
    Posts:
    180
    Good luck!
     
    Opeth001 likes this.
  23. xVergilx

    xVergilx

    Joined:
    Dec 22, 2014
    Posts:
    3,296
    But then realtime shadows will eatup CPU like no tomorrow.

    I've been there. And yes, even at that scale its better to bake (IMO).

    It just takes a bit of time to iterate, and Bakery.

    The only solution I've found for the realtime lights is not to use shadows for them at all.
    Sometimes it works, sometimes - it doesn't. If it doesn't - probably worth baking it.

    On topic:
    Splitting scenes into multi-scene setup is a must at that scale.

    Baking each of them separately improved my baking time from 6h to 40m depending on what parts of the scene I need to rebake.

    Also, you might want to use something that combines meshes together to speed up baking / reduce shadows / draw call count.

    I'm using https://assetstore.unity.com/packages/templates/systems/easy-mesh-combiner-mt-138805 for this.

    Writing your own is easy too, but its already dirty cheap and does the trick for me.
     
    Last edited: Nov 25, 2019
  24. Opeth001

    Opeth001

    Joined:
    Jan 28, 2017
    Posts:
    1,117
    Thank you @xVergilx for your Help!!
    what is the best approach to bake small Gameobjects (Grass, Props ... ) ? also doing this for large Scenes will not increase the Player Build size ?
     
  25. xVergilx

    xVergilx

    Joined:
    Dec 22, 2014
    Posts:
    3,296
    Small static props are exactly mesh combining case (if they share same materials).
    Grass - not so much, if you need it to be affected by the wind etc.
    It might increase build size a bit, but it reduces drawcalls by a lot if you've got a lot of objects.
    (It was down from 100 -> 3 for me in some places)

    Build size increase is mostly neglegable. Lightmaps without combining will take way more space.

    If those are dynamic - don't bake them at all and add light probes. Use light probes for those instead.
     
    Last edited: Nov 26, 2019
  26. hippocoder

    hippocoder

    Digital Ape

    Joined:
    Apr 11, 2010
    Posts:
    29,723
    Some great tips there. So we've got: realtime and we've got baked. How about faked?

    The problem with these (IMHO) AAA scales is that they're really harsh on iteration. And indies need more iteration than a large studio which has experience and hardware to tackle iteration at a large scale.

    For that scale though, specially outdoors, it's not totally insane to think about a top down shadow texture. Each mesh outside simply reads it in the shader, then we simply construct some world space UV from x and z vert position. This means even a small hand drawn 1024x texture could probably manage just fine - and no bake pain.

    Baking on those scales, it's hilariously much faster to bang out a simple flat texture manually. You could also just generate this texture by projecting each mesh in the world's verts as well and the result would be probably slightly better than an incredibly low res bake. An auto bake like this would be instant and of comparable quality but lack in accuracy.

    I don't see anything more forgiving than this approach for a tiny team on ES2 ish hardware as a baseline. It's a pretty old technique too so it's proven at least.

    Ultimately lighting a world will need more than one size fits all, so there will probably be a need to use multiple solutions as brute force is not available.
     
    Opeth001 and xVergilx like this.
  27. xVergilx

    xVergilx

    Joined:
    Dec 22, 2014
    Posts:
    3,296
    Well, looking back right now, I should've probably chosen even an Unlit solution with completely faked out shadows.
    And probably something in low-poly style :p

    Hacking lights is probably a right solution for an indie.

    Hope it will be worth in the end though.
     
  28. Opeth001

    Opeth001

    Joined:
    Jan 28, 2017
    Posts:
    1,117
    i didnt get how do you fake shadows, as you can see our game is a lowpoly style and im really interested on this solution.

     
  29. xVergilx

    xVergilx

    Joined:
    Dec 22, 2014
    Posts:
    3,296
    Paint them basically into diffuse map. E.g. add a line at the bottom of the fence to "simulate" AO.
    And other places where you think shadows may be.

    Its never accurate, but looks good for some reason on low-poly style. And its free from the performance perspective.

    Alternatively, you could implement the same in your shader base, and use vertex painting as a guide, make it tweakable etc.
     
    BrandyStarbrite likes this.
  30. BrandyStarbrite

    BrandyStarbrite

    Joined:
    Aug 4, 2013
    Posts:
    2,076
    Nice to see, that someone else on this forum besides myself, knows about this technique. 10 points!:)
     
  31. sas67uss

    sas67uss

    Joined:
    Feb 8, 2020
    Posts:
    81
    Hi
    I have a suggestion please consult me.
    I thought maybe it could to bake shadow of tree into texture in blender then save it as PNG with alpha channel and import as sprite in project and attached it to the tree prefab as a child .
    Is this method good for optimization on mobile when we have forest or a hug number of trees ?

    Note to picture below that the palm shadow is a sprite.

    shadowSprite.jpg
     
  32. GuitarBro

    GuitarBro

    Joined:
    Oct 9, 2014
    Posts:
    180
    If your terrain is completely flat, then this may not be a bad option. Just make sure they all batch and don't take tons of draw calls.
     
  33. jeroll3d

    jeroll3d

    Joined:
    Nov 6, 2010
    Posts:
    249
    Hi

    Could we follow this debate? I have this problem and 100,000 doubts about it. I made a map, based on an asset I bought from the Store, which is composed of 'plans' + emissive lights + a direct light. There are no vertical objects on this map, except for two 'channels' as in the image. I always start from the principle of little = a lot, that is, I need to do a lot with little - thinking about performance. My idea is fictional. Here I will only work with one controllable object and several others with or using 'path'. It's something 'simple', so to speak. Each plane was increased by 'x10' (would be the 'terrain'). And I have 10 shots of these lined up as shown in the image. In particular, I'm always confused about the Unity units, it would be 1 = 1 meter, because then on the 'x, y and z' axes different units appear (here treated as 'thousand'). Besides the doubts of beginners, comes the more complex ones, such as bake light, how to use it on large expanses of land.

    I always try to read, losing my native language (Portuguese), the Unity manuals. They are detailed, but they just describe something, for the most part, I get lost in the 'how to'.

    There is the bake mesh, a technique used - from what I understand about the documents - to join objects in a single 'bake light'.

    Just now, I learned that we have other techniques, separating large maps, A, by smaller scenes, A1, A2, A3, etc... I just got lost in the rest of the explanation: ok, I separated the scenes, the parts of my terrain, ' what to do?'.

    These are simple topics, for those with experience, and complex ones at the same time because it will involve Unity's engineering.

    I have found little on the web, maybe because of this unfortunate language barrier that turns everything into a big, giant, hell. But, we insist :)

    In the image below, I try to explain what happens. Shader credits (purchased from the asset store) to its creator 'Andragor Inc' - (I just changed a few things in the shader).
    2022-01-10 (2).png 2022-01-10.png
     
  34. jeroll3d

    jeroll3d

    Joined:
    Nov 6, 2010
    Posts:
    249
    So, finally, the questions (between 1000 others questions :D ):

    - in this case, what the better choice to bake this huge map (planes with material and 1 single direct DARK light)?

    (Of course, I don't expect easy answers, but I would like some direction beyond the Unity manuals.)
     
  35. hippocoder

    hippocoder

    Digital Ape

    Joined:
    Apr 11, 2010
    Posts:
    29,723
    I don't bake any more in Unity but I have found bakery from asset store to be very good in the past. I guess I got very tired of the problems as you imagine :)

    But there are two things:

    1. bakery supports baking really large maps with nvidia RTX gpus
    2. *recent* Unity added a "tiled baking" which means hopefully you don't run out of memory, but I have not tested.

    The idea behind the baking of large places is... unless you break the map up into pieces, it will be 1 mesh = 1 lightmap texture so eventually you cannot fit the mesh uv into the texture and still have good quality. That's why people cut up meshes.
     
    jeroll3d likes this.
  36. jeroll3d

    jeroll3d

    Joined:
    Nov 6, 2010
    Posts:
    249
    About 'tiled baking'

    - I'll search about, but I suppose it's something in that image (second linked image). About 'Bakery', well, I'll stick with Unity for now, no addons. But I saw an interesting one, the 'mesh baker'.

    I baked a little while ago, under the conditions described in the image (first linked). It was very fast, no problems and via GPU.

    B01.png B02.png
     
  37. hippocoder

    hippocoder

    Digital Ape

    Joined:
    Apr 11, 2010
    Posts:
    29,723
    It looks great.