Search Unity

Official [2020.1] New Feature - Auto Pack Margin

Discussion in 'Global Illumination' started by kristijonas_unity, Feb 13, 2020.

  1. kristijonas_unity

    kristijonas_unity

    Unity Technologies

    Joined:
    Feb 8, 2018
    Posts:
    1,080
    PackingComparison-Shaded.jpg
    Shaded view. Notice dark artifacts around the objects' edges caused by the UV overlaps.

    PackingComparison-UVOverlap.jpg UV Overlap debug view. Notice the lack of overlaps in the image on the right.

    Overview
    This feature was developed by Rasmus Rønn Nielsen (@rasmusn).

    In the Mesh Importer users have the ability to automatically generate lightmap UVs. Traditionally, we had a setting called Pack Margin. One could use this setting to deal with texel bleeding across lightmap islands. However, for various reasons this setting is extremely difficult to use and is generally disliked. This feature introduces a new way to work with lightmap UV margins. The new method allows the user to set Min Lightmap Resolution and Min Object Scale. These can be thought of as "a contract". As long as the imported model is used in scenes that has a lightmap resolution of at least [Min Lightmap Resolution] and as long as instantiations of the object is scaled to at least [Min Object Scale], then Unity will guarantee no bleeding across islands. This is achieved by conservatively calculating a pack margin that is large enough to avoid leaks.

    Unfortunately, larger margins can of course also cause lower texture utilization. There's no free lunch, but at least we now get correctness.

    Setup instructions
    New pack margin options are available in the Mesh Importer window. Select your mesh, and make sure that Generate Lightmap UVs flag is enabled. Unfold Lightmap UVs settings, and set the Margin Method to Calculate to access the feature:

    CornellBox-MeshImporterSettings.png

    Feature access & feedback
    You can get access to the feature by downloading the latest 2020.1 alpha build via the Unity Hub, or via this link. We are looking forward to hearing your feedback!
     
    Last edited: Mar 9, 2020
  2. AcidArrow

    AcidArrow

    Joined:
    May 20, 2010
    Posts:
    11,724
    Cool. If I understand correctly it's only an option if you're completely generating Lightmap UVs.

    Since the unwrapping of lightmap UVs leaves a lot of things to be desired, could we perhaps have a hybrid method that takes user authored islands and just does the packing with the new margin stuff?

    I have given up on Generate Lightmap UVs and getting the margins to look correct with hand authored UVs is kind of a pain, so it would be nice if we had tools to help with that too.
     
  3. Kuba

    Kuba

    Moderator

    Joined:
    Jan 13, 2009
    Posts:
    416
    AlanMattano likes this.
  4. rasmusn

    rasmusn

    Unity Technologies

    Joined:
    Nov 23, 2017
    Posts:
    103
    @AcidArrow @Kuba This new feature is unfortunately tightly coupled to our internal UV unwrapper, so we cannot just "apply it" to user authored UVs. That being said, I agree that such a feature makes total sense.

    Given a UV layout without overlaps (in continuous UV space), Unity should ideally be able to transform this into a "resolution-aware" UV layout that respect baking settings (e.g. Lightmap Resolution) such that no texel bleeding can happen during runtime. With such a feature, users could exploit unwrapping algorithms of DCC tools and it would "just work" (tm) when passed to Unity even after changes to Unity baking settings. But, alas, our roadmap does not (currently) set aside much time to improve in this area.
     
  5. AcidArrow

    AcidArrow

    Joined:
    May 20, 2010
    Posts:
    11,724
    I have to say then, I'm slightly confused on the purpose of the feature.

    "Generate Lightmap UVs" produces very inefficient UVs. It was an okay algorithm 8 years ago, and it's still largely the same. I can't imagine anyone using it for anything other than quick and dirty UVs in early development, since it wastes a ton of lightmap space.

    (and I have to say, I don't know why Unity isn't more invested in improving their packing/atlasing stuff, there are a ton of promising projects around these days you could get ideas from : https://github.com/Microsoft/UVAtlas or https://github.com/Thekla/thekla_atlas )

    So, while it's "nice" that "Generate lightmap UVs" has proper packing distances, it feels like the feature is wasted, since it's a polishing feature that is applied to something that people shouldn't be using for their final UVs anyway.

    There are a ton of small lightmapping adjacent features that would be really great to have (storing lighting values in vertices, I believe it has been discussed for a decade almost now) and apparently, there isn't room in the roadmap for them, so it's frustrating that when we finally get something, it's somewhat... misguided. Or at least, that's what it looks like to me.
     
    Last edited: Feb 20, 2020
  6. DanielRiches

    DanielRiches

    Joined:
    Nov 23, 2019
    Posts:
    166
    using Unity 2020.1.0a25.3171
    using HDRP 7.1.8

    Im sure a previous commenter also had this problem, but I think if you change the Min Lightmap Resolution setting in the advanced tab of the Generate Mesh UV's sub-menu to something higher than your current Lightmap Resolution in the Lighting settings tab, it bugs and stops you using your keyboard to change values in the lighting tab, you can still use your mouse to change the values, it requires a restart of the project.
     
  7. AcidArrow

    AcidArrow

    Joined:
    May 20, 2010
    Posts:
    11,724
    DanielRiches likes this.
  8. rasmusn

    rasmusn

    Unity Technologies

    Joined:
    Nov 23, 2017
    Posts:
    103
    I agree that the current situation is bad and that there are plenty of proven techniques that we should learn from and adopt. The reason that we aren't is that we have a lot of other stuff on our plate currently. I hate to sound like a broken record, but it is the truth.

    It is my impression that a lot of people are using the built-in UV generator (bad as it may be). For them, it is nice to at least be able to get correct lightmaps (as in no texel bleeding). With this feature, you can get that. Therefore I think it is a bit much to say that the feature is "wasted".

    The problem with that approach is that it scales linearly with vertex count (regular lightmaps don't). This means that it will be great for some games, but really bad for others. For that reason, we are hesitant investing too much into per-vertex lighting. I think a modern implementation of light probes has a better chance of being a better replacement/improvement of lightmaps that works across a wide range of game types and assets, but that is a different discussion :)
     
    MrMcCoy and AcidArrow like this.
  9. rasmusn

    rasmusn

    Unity Technologies

    Joined:
    Nov 23, 2017
    Posts:
    103
    Thanks for mentioning that @AcidArrow.
    @DoomPriestK To what @AcidArrow already said, I can add that the bug has now been fixed and will be part of one of the coming patch updates to 2020.1 (and will of course also be fixed 2020.2).
     
    DanielRiches likes this.
  10. ivank

    ivank

    Joined:
    Nov 16, 2013
    Posts:
    105
    Hi @rasmusn , your statement brings up my years-old problem/question regarding the Unity "baking" process:
    is there any workflow that allows imho quite logical option - to use the UVs generated and imported together with the 3D object itself - in an external DCC application (3DS Max for example), most of which have far superior unwrapping algorithms and provide much better control over it?

    There is somewhere here years-long forum thread discussing and begging for such option as the current Unity UV unwrapper is really really bad and ability to use imported well-optimised UVs would most probably clearly outweight all the benefits of the feature you have "advertised" here...
    One of the topics discussed there was also - if we try to generate the unwrapped UVs for baking externally - which UV channel should be used as it seemed not quite clearly mentioned in the Unity documentation... is this clear now?
     
    Lars-Steenhoff likes this.
  11. rasmusn

    rasmusn

    Unity Technologies

    Joined:
    Nov 23, 2017
    Posts:
    103
    Yes, that is possible. Just let your DCC write the UV layout to the secondary UV channel and Unity should pick it up (assuming you have turned "Auto generate UVs" off in the Model Importer). Do note however, that your object's UV rectangle will be scaled (uniformly) and positioned inside a lightmap. This scaling/positioning is currently not configureable but several people have requested this. So far we have unfortunately not been able to find time for such a feature in our roadmap. Unity performs the scaling automatically to maintain the same "texel per unit distance" resolution across the entire scene (unless you have configured it differently, e.g. via the "Scale in Lightmap" parameter). Note that even though your charts have no overlap in the continous UV space [0,1]^2, data may "bleed" across charts due to bilinear filtering on the GPU.

    I could not find it in the docs, so my answer would be no :) I will report this to our docs team right away.
     
    hippocoder, MrMcCoy and ivank like this.
  12. hippocoder

    hippocoder

    Digital Ape

    Joined:
    Apr 11, 2010
    Posts:
    29,723
    Thanks for this! It's been one of my number one irritations if I'm honest, with Unity's lightmaps. I even have had to use dodgy lightmap scales to edge things into shape. So this is a big improvement, possibly more than most people realise.

    What remains, I think is to improve lightmap resolution consistency where there are a lot of separate lightmap surfaces. Unity seems to struggle here, with some objects sharing, sometimes waste space sometimes it just has poor results but AFAIK that just is a whole different kettle.

    Is it available for Bakery / Other lightmappers? I guess so providing it uses only Unity's lightmap UVs.

    @guycalledfrank any thoughts?
     
    AlanMattano and rasmusn like this.
  13. AcidArrow

    AcidArrow

    Joined:
    May 20, 2010
    Posts:
    11,724
    AFAIK bakery already adjusts padding plus other stuff. It doesn’t need this.
     
  14. JoNax97

    JoNax97

    Joined:
    Feb 4, 2016
    Posts:
    611
    Well in any case, if the upcoming feature is good enough for Bakery to use, maybe they can start using it and free some dev time for other features.
     
  15. d-3

    d-3

    Joined:
    Oct 18, 2009
    Posts:
    213
    Packing by group will be a nice feature to implement to!! so my chair in room A will not be on atlas 31 with room Z
     
  16. DonCornholio

    DonCornholio

    Joined:
    Feb 27, 2017
    Posts:
    92
    Yes! This was exactly what i was thinking when I read the paragraph. Please unity devs! That would be an amazing feature :)
     
  17. JamesArndt

    JamesArndt

    Joined:
    Dec 1, 2009
    Posts:
    2,932
    I understand @AcidArrow's sentiment completely. It's not entirely "wasted" effort, but the effort should be expended on dealing with more important issues at hand..i.e. the poor packing algorithms for generating UVs. I've been a user of Unity for over a decade and to see that a decent auto UV algorithm hasn't been developed within that time frame is disheartening. So I understand what he meant. Who cares about pack margins when the UVs themselves are terribly wasteful. We're talking about facilitating millions of developers to produce games and products that will ship out into the world with poor memory management...or rather poorer than it needs to be out of the box. Why not focus on empowering developers to manage memory more efficiently? Versus continuing down the path of ignoring the problem of wasteful texture memory utilization?

    PS - As an aside, did Unity ever get in that functionality for passing through user-authored lightmap UVs without repacking them, shifting or moving them around on atlases? Basically pass them directly through not altered in any way...how it was done in Beast.
     
  18. rasmusn

    rasmusn

    Unity Technologies

    Joined:
    Nov 23, 2017
    Posts:
    103
    Maybe you already know this, but for what it is worth you can achieve this by manually setting the Baked Tag. Objects with different Baked Tag will go to different lightmaps. You can use this to ensure that objects that are close to each other go to the same lightmap (or set of lightmaps if you have too many objects to fit into one).
     
    futurlab_xbox likes this.
  19. d-3

    d-3

    Joined:
    Oct 18, 2009
    Posts:
    213
    Rasmusn - thank you for the information! it´s 2019. feature very cool.. I still in the 2018LTS so not deep familiar with a lots of hidden new features!! really nice to know!! the next game will be 2019.3... this one for quest i will keep on 2018 until we release the update!
    cheers!
     
    rasmusn likes this.
  20. rasmusn

    rasmusn

    Unity Technologies

    Joined:
    Nov 23, 2017
    Posts:
    103
    Thanks for your sincere comment @JamesArndt.

    I already answered your other recent post that is similar to the one in this thread, but I will reply here as well for completeness.

    I hate to sound like a broken record, but a lot of UV work got de-prioritized against other high-priority fixes and improvements. The fact that this has been a known problem for so long is definitely not something we are proud of or have forgotten about. We are currently looking into ways we can repurpose our resources in order to find manpower to address this. I know this is not the answer you had hoped for, but I can tell you we are trying our best with the resources and other obligations that we have.
     
  21. awenc-lsvr

    awenc-lsvr

    Joined:
    Jan 13, 2020
    Posts:
    2
    Hi,
    Reviving the lightmapping thread a bit...
    the "Generate Lightmap UVs" checkbox is very handy, sure it makes ugly UV's on "soft/organic-like" meshes, but still. It's a One click feature that let's you bake any objects pretty efficiently. not overlapp, no unfolding hassle...
    and I still author my own UV2s when necessary...
    So auto pack margin feature, yeah that's cool. but the feature that changed a lot the final quality for me on Automatic UV's is the Stitch Seams option couple years ago.
    Now even with poor UV2, the final result is very decent (in terms of quality, now memory waste on lightmap is another story)
    I baked this helico interior a few months ago (see image) with GPU and Automatic UV's, and it's fast and clean... while a few years ago it wouldn't have been that easy.

    Now about a pass through option ;)
    There used to be a Lock Atlas feature with Beast back with Unity 4.
    and it was possible to force bake the lightmap within 0 and 1. no offset, no tilling.
    I'm trying to reproduce that on a project right now... and I still get some tilling no matter what. also padding is 2 as a minimum value, tried editing that padding value manualy in Lighitng Setting file, doesn't seem to take.
    My goal is to bake some lightmap in Unity for specific animated objects and use those lightmap in the shadergraph to lerp between 2 states or more. to have Animated Lightmap. and right now I'm a bit stuck...

    @rasmusn
    I've been using Unity for almost a decade, and I'm now really happy with the Auto Generate Lightmap UVs and the actual, pretty robust, GPU lightmapper !
     

    Attached Files:

    JamesArndt likes this.
  22. awenc-lsvr

    awenc-lsvr

    Joined:
    Jan 13, 2020
    Posts:
    2
    I've tried a workaround to solve my actual problem :
    I have several objects I want to bake in one single Lightmap, onto my prepared Layout, to be used in a shadergraph later.
    So I combined those objects to have one single Mesh for the bake, to bake lightmap on the layout without any repack by Unity.
    But I was still ending with some small offset and tilling like mentionned in previous post.
    The workaround :
    I added some cubes to the combined mesh, really small/invisible, just to have UVs present at the four corners of the layout.
    So Unity detects my layout as a full 0 to 1 UVs and it doesn't apply any offset or tilling on that. (see attachment (this is a test, UVs are "auto-layout" in Maya))
    It's kind of a trick more than a real process, but maybe it can help other people.
     

    Attached Files:

    Last edited: Mar 21, 2022
    JamesArndt likes this.
  23. rasmusn

    rasmusn

    Unity Technologies

    Joined:
    Nov 23, 2017
    Posts:
    103
    @awenc-lsvr Thanks for your feedback! That work-around is pretty clever I must say :).

    I cannot promise when or if the following will be delivered, but these are some things are actively looking at which I suppose you'll like:
    • Revised architecture and API of the light baker. The old global Lightmapping.Bake() API is coupled to the Lighting window. We'd like to ada a new lower-level bake API with no global side-effects where the user is given much more control (at the cost of less convenience). For example, they'll be able to fully control the input to this bake (as opposed to Unity automatically extracting everything that is in the scene) and also where the outputs go (e.g. the API won't be coupled to MeshRenderer at all).
    • Atlassing will be extracted from baking such that it can be passed in as an input to the bake. Alongside the new API mentioned above, this means that users who use the new lower level API can pass in _any_ atlas/UV-set they want via C#. This should unlock the "Lock Atlas usage" that you mention.
    • We are considering to replace our current (old) atlassing solution with something similar to what Frostbite presented in 2018 (see attached image). This is fully automatic, has high utilization, and guarantees no bilinear sampling leaks across charts. Effectively, this would remove the need for the "Calculated Pack Margin", "Minimum Lightmap Resolution", "Padding", "Pack Margin" settings because everything will just work (tm).
    Again, I cannot promise if/when we will deliver these :).
     

    Attached Files:

    ivank, radiatoryang, Gametyme and 3 others like this.
  24. Ruchir

    Ruchir

    Joined:
    May 26, 2015
    Posts:
    934
    Maybe you guys should pin this thread :)
     
  25. AcidArrow

    AcidArrow

    Joined:
    May 20, 2010
    Posts:
    11,724
    If I understand correctly, this will generate UVs for the whole scene at once, so packing will be more efficient since you're getting rid of the requirement of having a mesh UVs be their own little box and then having those boxes packed together.

    That has the potential to work well, but can you please make sure if this feature ever sees the light of day, that there is a mode where we can supply the UV islands ourselves, and then your algorithm can move scale etc them as it sees fit?

    Even if the new UV generation algorithm is great, being able to control where to have or not have seams is a must have and the simplest way for this would be for the algorithm to accept user authored UVs as input and then repacking them.
     
  26. AcidArrow

    AcidArrow

    Joined:
    May 20, 2010
    Posts:
    11,724
    Also, looking at that linked PDF, this is a feature that would be useful for Unity and potentially an easy-ish implementation?
    upload_2022-4-11_4-31-47.png

    At the very least it would make it easier for people on the forums to explain why their lightmaps look weird, but it would also be useful for lightmappers to identify potential errors (I generally never use double sided GI unless I have a very specific reason). It could be a debug view in the scene view.
     
  27. kristijonas_unity

    kristijonas_unity

    Unity Technologies

    Joined:
    Feb 8, 2018
    Posts:
    1,080
    I agree, this would be useful. I have submitted a usability request for this feature.
     
    JamesArndt likes this.
  28. rasmusn

    rasmusn

    Unity Technologies

    Joined:
    Nov 23, 2017
    Posts:
    103
    Yes, exactly.

    Yes, agreed. The method I refer to do indeed take UV unwraps as input, and these can be hand-authored or automatically generated by Unity or another program. The packing algorithm will then identify each chart in the UV unwraps, and pack these efficiently and safely in the list of lightmaps (using only translation + scaling).

    So this allows the user to control seams but it doesn't allow the user to control the entire lightmap chart layout.

    That said, the architecture refactoring mentioned above involves decoupling lightmap packing from light baking, and therefore it would also enable 100% pass-through UVs, i.e. at mode where we don't try to packing anything. However, if the packing algorithm mentioned above work as nicely I think it does, then I'm not sure this pass-through mode is still relevant. I'd love to hear what you think about this.
     
    AcidArrow likes this.
  29. unity_G0e-NMyc_OeQlg

    unity_G0e-NMyc_OeQlg

    Joined:
    Jun 21, 2022
    Posts:
    1
    Hi, I'm updating a project (from Unity 2019 to 2020) which had several levels with light baked and autogenerated UVs, when I open the project with the new version it recalculates all auto UV's and now my lights are all broken, Have you anticipated this? is there a fix that doesn't involve baking all of my levels again?
     
  30. YJack

    YJack

    Joined:
    Apr 7, 2010
    Posts:
    44
    Wow! How I missed that. Awesome feature, indeed. Congrats!
     
  31. rasmusn

    rasmusn

    Unity Technologies

    Joined:
    Nov 23, 2017
    Posts:
    103
    Not that I know of, no. Unfortunately.

    Ideally, we should somehow make it an opt-in whether you want to use a new version of the UV auto unwrapper. But this comes with its own set of complexity given the current state of the ModelImporter (how do you expose this choice in the UI, how do you serialize it to disk, how do manage users who switch back and forth between versions, how do you handle unwrap settings that may make sense with one version of the unwrapper but not another one, etc). And given that additional complexity isn't exactly what Unity needs currently, my guess is that the author of this change assessed that requiring a rebake was the better compromise.

    I understand why this may not be a satisfactory answer for you but at least I hope it shows that this isn't exactly a trivial problem either.
     
  32. second_ich

    second_ich

    Joined:
    Oct 17, 2020
    Posts:
    39


    Any news about this feature? Unity should be able to just use the imported UVs and repack the individual UV islands with an adjustable padding. This would make lightmaps much more efficient and save a lot of time.
     
  33. wagner_de_souza

    wagner_de_souza

    Joined:
    Apr 9, 2019
    Posts:
    15
    In Unity 2021 its not working.
    This packing is terrible.
     

    Attached Files:

  34. AcidArrow

    AcidArrow

    Joined:
    May 20, 2010
    Posts:
    11,724
    Start a new thread and post more information, the packing is generally still pretty bad, but this feature generally works, from that image I cannot tell what the problem is.
     
    rasmusn likes this.
  35. rasmusn

    rasmusn

    Unity Technologies

    Joined:
    Nov 23, 2017
    Posts:
    103
    Are you referring to the Auto Margin feature (in the Model Importer) specifically or the general packing algorithm? These are two different algorithms (one generates input for the other, if configured to do so).

    Or maybe you are implying that the large area of unused space in the attached lightmap means that there must be a bug in the packing? With the current packing implementation, that is actually not the case. The algorithm tries to reduce the size of the last of your lightmaps, and it does so by halving the lightmap resolution until the objects in it no longer fits. In your case, you would not be able to half the lightmap size one more time and still have the objects fit, and therefore it doesn't. This behaviour yields this funny-looking L shape.

    Note that I'm not saying that our current packing strategy is great. I'm only saying that it seems be doing what it was originally designed to do (based on the few details you have shared).

    Please share more details about your problem or [file a bug](https://forum.unity.com/threads/2020-1-new-feature-auto-pack-margin.827859/), and we'd be happy to take a deeper look. Thanks.
     
    Last edited: Apr 2, 2024