Search Unity

How to load lightmaps in Unity5?

Discussion in 'General Graphics' started by zhuchun, Mar 18, 2015.

  1. zhuchun

    zhuchun

    Joined:
    Aug 11, 2012
    Posts:
    433
    Hi I'm upgrading my game from Unity4. There's a scene change lightmaps depends on the time. In Unity4 we have "lightmap_far", "lightmap_near" and "lightprobes" file after we bake the scene, so we can set the LightmapSettings.lightmaps/lightprobes.
    But in Unity5, baking a scene generate "Lightmap-0_comp_dire", "Lightmap-0_comp_light" and "LightmapSnapshot.asset". That's pretty different from the previous version o_O
    Where's the lightprobes? I assume it has been contained in the LightmapSnapshot.asset, but I have no idea about how to load it in script.
     
  2. Kuba

    Kuba

    Moderator

    Joined:
    Jan 13, 2009
    Posts:
    416
    You're right that light probes are contained in the LightmapSnapshot.asset when the non-continuous mode is used for baking. After the bake is done they are loaded into the scene and you can get the SH data via http://docs.unity3d.com/ScriptReference/LightProbes-bakedProbes.html and serialize it. I would not recommend switching the actual LightProbes object though, as it contains quite a bit more data needed for interpolation and for realtime GI (if it's enabled).
     
  3. zhuchun

    zhuchun

    Joined:
    Aug 11, 2012
    Posts:
    433
    Thank you! It seems harder than what we did before(Unity4 save files we need directly), I will try it now:)
    By the way, is that means we have(gonna have) a new way to change the Light on the fly? Such as replace the LightmapSnapshot.asset ?

    UPDATE:
    Serialize a SphericalHarmonicsL2[] to json looks like a good workaround, but there're some problems since the serializer returns null. After communicate with the author of JSON.NET for Unity, we're working on it.
     
    Last edited: Mar 21, 2015
  4. Kuba

    Kuba

    Moderator

    Joined:
    Jan 13, 2009
    Posts:
    416
    At least right now, the LightmapSnapshot.asset is an Editor-only concept. Not sure if it makes sense to have it at runtime.

    It mostly stores:
    • the lightmap index and lightmap scale/offset for static renderers
    • files needed for realtime GI
    • ambient probe
    I don't see a reason for replacing the first two at runtime and there is an API to change the ambient probe at runtime already.
    Lightmaps and reflection probes are stored outside of the LightmapSnapshot.asset and lightmaps can be replaced at runtime with LightmapSettings.lightmaps.

    Are there any use cases that the above doesn't cover?
     
  5. mikamikem

    mikamikem

    Joined:
    Mar 3, 2013
    Posts:
    10
    In our project in 4.6 we were using a single scene for multiple in-game days that contain different objects depending on the day. In 4.6 we cached off the lightmap index and scale/offset automatically during our lightbake process and re-apply them when loading the scene at runtime after enabling and disabling the objects that were needed for a given day.

    I'm not sure exactly how the realtime GI data works, but in this scenario we would at least want to maintain the ability we had in 4.x to swap the lightmap index and scale/offset values and if it matters, also the realtime GI info.
     
  6. zhuchun

    zhuchun

    Joined:
    Aug 11, 2012
    Posts:
    433
    Yes, your case is very similar to what we did. Unity5 make it harder to get info we need, especially the baked lightprobes.
    In fact it's also very hard to serialize the SphericalHarmonicsL2[] due to its struct, so I end up write a converter for it.
    @Kuba is right, that's the best practice.
     
  7. ProtonOne

    ProtonOne

    Joined:
    Mar 8, 2008
    Posts:
    406
    I just got hit by this issue converting a project from Unity 4 to 5.

    Previously a LightProbes.asset file was created by the Unity Editor that I could drag & drop into the inspector to easily access the light probes from other scenes. I have 16 scenes each with their own Light Probes.

    I start in 'SceneA', then addtiviely load 'SceneB'. When my character walks into 'SceneB' I changed LightmapSettings.lightProbes to the probes for 'SceneB'.

    I just tried this in Unity 5.0.2f1, but am not able to Serialze Light Probes:
    Code (csharp):
    1. SerializationException: Type UnityEngine.LightProbes is not marked as Serializable.
     
  8. zhuchun

    zhuchun

    Joined:
    Aug 11, 2012
    Posts:
    433
    Probes is still in LightmapSettings.lightProbes.bakedProbes, it's a SphericalHarmonicsL2[]. You can save the coefficents in a class as float[24] and serialize the class. It's not pretty decent but works.

     
  9. duange

    duange

    Joined:
    May 14, 2015
    Posts:
    2
    hello everyone,I have the same problem,I don't know how to load LightmapSnapshot in C# code, can anyone show me that? please!!! i really need that !
     
  10. zhuchun

    zhuchun

    Joined:
    Aug 11, 2012
    Posts:
    433
    For light probes, you can find it in my post.
    For the whole lightmapSnapShot, It seems you can't do it now, I guess Unity will release more features in next updates.
    In fact Unity is still working on reduce file size.

     
  11. duange

    duange

    Joined:
    May 14, 2015
    Posts:
    2
    my project use Lua code and a little bit of C# codes,so we build all needed scenes in a package,but the package does not contain LightmapSnapshot infomation,we lost all infomations of light ,if the lightmapSnapShot can't be loaded form Resources, is there any other way ?
     
  12. zhuchun

    zhuchun

    Joined:
    Aug 11, 2012
    Posts:
    433
    I google and found nothing a few weeks ago. The best idea is bake your scene again if there're no insane works to do. As far as I know, you can't load those data in Unity5 for now, so I bet there isn't any shortcut :\
     
  13. harish-g9

    harish-g9

    Joined:
    Jan 29, 2013
    Posts:
    7
    Hi,
    I have the same problem. I migrated the project from 4.5.5 to 5.1.2f1. I could see our app is not behaving properly and found out the reason. I was using lightmaps in exr format and applying the lightmaps runtime through script. Now Unity 5 has changed its way of applying Lightmaps and could generate Lightmapsnapshot. Is there any tool or something which converts Unity 4 lightmaps to Unity5 supported ones?
     
  14. zhuchun

    zhuchun

    Joined:
    Aug 11, 2012
    Posts:
    433
    As far as I know, nope.
    Workaround: Re-bake scenes and get their data, especially that SphericalHarmonicsL2 struct I metioned before. In this way you're able to save them as JSON or whatever format you want and you can access them like before. More importantly you don't have to modify a lot in your project, all you need to do is write a serialize script. BUT you may find more weakness in the future.
    Best(?) practice: Follow the official guide line and do it again, but seriously I have no idea what is lightmapSnapshot, no docs at all, leave alone modify it in script. At your own risk to explore it :\
     
  15. harish-g9

    harish-g9

    Joined:
    Jan 29, 2013
    Posts:
    7
    Hi Zhuchun,

    Thanks for your message. I rebaked individual models from different scenes and saving the light data from renderer in a data structure individually for each model. I applied the appropriate light data in the main scene depends on what object we select and load those lightmaps only. Its showing the light maps properly now. But, I have different lightmapsnapshot files for each individual model. But I cant apply them in the Lighting window for the model selected. I hope Unity will make it to change the snapshot file dynamically.
     
    afshin_a_1 likes this.
  16. zhuchun

    zhuchun

    Joined:
    Aug 11, 2012
    Posts:
    433
    In most case, all bake related data you need are lightmaps and lightprobes, they can be saved as you did. I do hope they will do it but it's taking way tooooooo long. I mean, Gee, last time when I hoping for that, it's 6 months before. Seriously I'll be kick out from crew if I can't solve a problem in half year lol. Maybe that's too mean, so let's leave it and make workarounds :)
     
  17. yu-wan

    yu-wan

    Joined:
    Jan 4, 2016
    Posts:
    18
    As my experiment in unity 5.4, unity only allows to modify the coefficient of light probes than fully control it. This means every time you wish to swap your light probes, you need to confirm all light probes' position and light probes number will not change. This is a kind of very limited 'snapshot'. Another way to do light probe rebuilding is to use unity's official API, but you need to clear all light probes first and adds light, which means you can only rebuild the direct light but indirect light cannot. You can use it as a trick to simulate indirectly(by adding some fake baked light sources and it's only for light probe baking). BTW, a very important concept is that: light probe is a property accompany with the scene. The coefficient of probes are only determined in when you press baking button in the lighting window. So if you remove the light probes in the scene, it won't take any effect until you press baking button again.
     
  18. zhuchun

    zhuchun

    Joined:
    Aug 11, 2012
    Posts:
    433
    Thanks for reply.
    Time has changed. I would say three words when I look back to this question,"Don't do it" :)
     
    afshin_a_1 likes this.
  19. songchaoxx

    songchaoxx

    Joined:
    Jul 18, 2017
    Posts:
    1
    Hello Guys,
    SphericalHarmonicsL2[,] allow to set/get the 9 coefficients. So I think I can save these coefficients to a file and use them to rebuild Lightprobes when a scene loaded.
    But the class LightProbes is a UnityEngine.Object, I cannot new it. My scene has cleard lightmapData, so LightmapSetting.lightProbes is null too , I cannot instantiate it.
    Is there any way to instantiate a LightProbes ?
     
  20. cgprojects

    cgprojects

    Joined:
    Jul 19, 2016
    Posts:
    46
    Did anyone get the lightprobe switcher working? I was able to implement the lightmap switcher but I have to get the light probes to change also.
     
    afshin_a_1 likes this.
  21. amcakebread

    amcakebread

    Joined:
    Nov 8, 2016
    Posts:
    28
    any chance of ever getting this mess sorted out? I have one scene and various rooms that I want to apply lighting to when I switch between them. Why on earth would anyone think that being restricted to one light map per scene is remotely sensible, and why make it close to impossible to manually swap light maps? This has become a real problem on my project so any short term suggestion for a fix would be appreciated
     
    afshin_a_1 likes this.
  22. afshin_a_1

    afshin_a_1

    Joined:
    Apr 26, 2015
    Posts:
    52
    @Kuba
    oh god why is that so hard for unity team to make it easy for lightmap swtiching with lightprobe support? with everything support. i mean, it's like we are hacking unity to do those things.
     
  23. zhuchun

    zhuchun

    Joined:
    Aug 11, 2012
    Posts:
    433
    Oh, am I still watching this old thread? Well, when we look back, we finally know why the lightmap support was awful years ago, THANKS to AUTODESK! Unity used Autodesk Beasts to bake lightmaps, however, Beasts itself is not well made and not welcomed, such a huge company was backing it up though. So eventually, Autodesk dropped it years ago with other useless middlewares. That sounds like a scam isn't it?

    https://www.autodesk.com/content/autodesk-game-middleware