Search Unity

  1. Good news ✨ We have more Unite Now videos available for you to watch on-demand! Come check them out and ask our experts any questions!
    Dismiss Notice
  2. Ever participated in one our Game Jams? Want pointers on your project? Our Evangelists will be available on Friday to give feedback. Come share your games with us!
    Dismiss Notice

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:
    626
    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:
    63
    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:
    86
    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.
     
    hippocoder and Opeth001 like this.
  4. Opeth001

    Opeth001

    Joined:
    Jan 28, 2017
    Posts:
    626
    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
  5. GuitarBro

    GuitarBro

    Joined:
    Oct 9, 2014
    Posts:
    86
    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:
    626
    @GuitarBro do you have any idea why shadows still dynamic even for static GameObjects ?
     
  7. GuitarBro

    GuitarBro

    Joined:
    Oct 9, 2014
    Posts:
    86
    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:
    626
    ScreenShot1 :



    ScreenShot2:
     
  9. GuitarBro

    GuitarBro

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

    hippocoder

    Digital Ape Moderator

    Joined:
    Apr 11, 2010
    Posts:
    26,729
    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.
     
    GuitarBro likes this.
  11. Opeth001

    Opeth001

    Joined:
    Jan 28, 2017
    Posts:
    626
    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:
    626
    Mixed light.
     
  13. GuitarBro

    GuitarBro

    Joined:
    Oct 9, 2014
    Posts:
    86
    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:
    626
    @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:
    86
    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:
    626
    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:
    86
    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:
    626
    i dont see any lightmap static option.

     
  19. GuitarBro

    GuitarBro

    Joined:
    Oct 9, 2014
    Posts:
    86
    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:
    86
  21. Opeth001

    Opeth001

    Joined:
    Jan 28, 2017
    Posts:
    626
  22. GuitarBro

    GuitarBro

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

    xVergilx

    Joined:
    Dec 22, 2014
    Posts:
    2,255
    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:
    626
    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:
    2,255
    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 Moderator

    Joined:
    Apr 11, 2010
    Posts:
    26,729
    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:
    2,255
    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:
    626
    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:
    2,255
    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:
    1,240
    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:
    42
    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:
    86
    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.
     
unityunity