Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Problems with instantiating baked prefabs

Discussion in 'Global Illumination' started by GSdev, May 8, 2015.

Thread Status:
Not open for further replies.
  1. GSdev

    GSdev

    Joined:
    May 8, 2015
    Posts:
    14
    We've been having an issue with our lighting information being lost when instantiating baked prefabs. I created a topic in Unity Answers here:
    http://answers.unity3d.com/questions/962360/lost-lighting-information-on-baked-prefabs-when-in.html

    and I've noticed three other similar cases but nothing has been resolved:
    http://issuetracker.unity3d.com/iss...baked-gameobject-to-prefab-with-lightmap-info
    http://issuetracker.unity3d.com/issues/prefabs-dont-store-ligthtmap-information-correctly
    http://answers.unity3d.com/questions/929399/save-lightmap-in-prefab.html

    Anyone have any ideas??
     
    AntonioModer likes this.
  2. GSdev

    GSdev

    Joined:
    May 8, 2015
    Posts:
    14
    We were encountering lighting issues with baked prefabs that turned out to be a result of design changes within Unity 5. For background see: http://answers.unity3d.com/questions/962360/lost-lighting-information-on-baked-prefabs-when-in.html

    The workaround provided by Unity is an editor script

    Code (csharp):
    1.  
    2. struct LightmapInfo
    3. {
    4. Renderer target;
    5. Vector4 offsetScale;
    6. int lightmapIndex;
    7. }
    8.  
    9.  
    However after implementing this script we still are encountering issues. Attached is a photo of the original scene, the problem before the script, and the problem after using the script (visible in the camera preview)
    Any ideas on a solution?
    lighting issue.jpg
     
  3. Joachim_Ante

    Joachim_Ante

    Unity Technologies

    Joined:
    Mar 16, 2005
    Posts:
    5,203
    Can you post the full script of how you are handling storing the prefabs. Best would be a small sample project that has the script you use to create a full workflow of generating the prefabs from the scene with lightmap information
     
  4. GSdev

    GSdev

    Joined:
    May 8, 2015
    Posts:
    14
    Sure! Thanks for your help.
    The uploader said that the .7z file was too large to upload although it is only reading at just above 2mb so I've uploaded it to google drive.
    Once uncompressed, open the scene 'test' and go to the main camera window. From there just go through the buttons in order as seen in the attached image (apply and save -> Instantiate prefab -> Show original -> show instantiated, etc...) to see the problem we are encountering.
    https://goo.gl/esLQMq
     

    Attached Files:

    Last edited: Jun 23, 2015
  5. RogueCode

    RogueCode

    Joined:
    Apr 3, 2013
    Posts:
    230
    We're having the same issue. We've been happily thinking that our dynamically loaded objects and lightmaps were working great, until we tried to export a build.
    So @Joachim_Ante, any progress on this? Can we expect it to be patched? It's a pretty major issue for a whole range of games.
     
  6. Joachim_Ante

    Joachim_Ante

    Unity Technologies

    Joined:
    Mar 16, 2005
    Posts:
    5,203
    First of all reasoning behind the changes in Unity 5.

    Why we changed behaviour?
    Renderer.lightmapIndex and Renderer.lightmapScaleOffset in unity 4.6 was stored on each renderer. This made it possible to store this data in prefabs.

    In Unity 5 we have moved this data into the lightmap snapshot. The lightmap snapshot is an asset that can be stored next to the scene file. The reason we did this is because when we stored LightmapIndex, lightmapScale and lightmapOffset in the scene file, an automated process (clicking the bake button) resulted in lots of changes that are highly likely to cause merge conflicts in scene files.

    By storing all the generated data of the bake process in a separate file, merge conflicts on the scene are avoided and it becomes possible to work in a team on the same scene. As a side-effect of not storing the data on the renderer, it no longer gets stored in prefabs either.

    How you can handle lightmapped prefabs in 5.0
    Overall we very much think this is the right tradeoff. For the cases where you do need it to store the data with prefabs.

    I made a sample project to show how to do this.

    You can make a scene with all prefabs you want to bake. Each prefab one has to have the PrefabLightmapData in its root game object.

    Then you can just click on the Assets -> Assets -> Bake Prefab Lightmaps menu item.

    This will first bake all scene objects. Then extract all lightmap indices + scale and offset. Then it will store it in each prefab instance. Then it will replace all prefabs with the new information.

    At load time the script patches the lightmap settings, thus it applies the lightmaps, lightmap index & offset/scale to all renderers which need to be patched at load time.
     

    Attached Files:

    Last edited: Jul 4, 2015
  7. mweldon

    mweldon

    Joined:
    Apr 19, 2010
    Posts:
    109
    Does it still work if you bundle the prefab and lightmaps into an asset bundle, then try to download the bundle and instantiate from it? Because that's where my solution falls apart. Works fine if I load the assets locally, but doesn't seem to work with bundles.
     
  8. RogueCode

    RogueCode

    Joined:
    Apr 3, 2013
    Posts:
    230
    Thanks, I checked it out, but it has the same issue. It works fine in the editor, but not in builds (I tried PC and web).
     
  9. Joachim_Ante

    Joachim_Ante

    Unity Technologies

    Joined:
    Mar 16, 2005
    Posts:
    5,203
    The shader deployment pipeline automatically removes shader combinations relating to lightmaps if no lightmaps are setup in deployed scenes.

    You can override this behaviour in "Shader Stripping" in Edit -> Project Settings -> Graphics Settings.

    And ensure the specific lightmap modes that you are using in your project are supported.
     
  10. Joachim_Ante

    Joachim_Ante

    Unity Technologies

    Joined:
    Mar 16, 2005
    Posts:
    5,203
    I have updated the demo project above so it has said changes in Graphics Settings setup. Please let me know if the sample now works for you out of the box.
     
  11. GSdev

    GSdev

    Joined:
    May 8, 2015
    Posts:
    14
    Joachim_Ante said:
    Everything works as expected.

    I have attached a small sample project which shows how to use the API to store lightmap index / offset&scale in a prefab.

    Thanks to the script you provided this is no longer an issue, however the persistent issue that we need addressed is that the rendered scene in the camera preview is producing something completely different from our original design (as you can see in the attached photos)

    We are curious, @RogueCode as to what you have been doing since this issue affects us within the editor, well before we are able to get to the point of being able to export the project. Maybe you have some insight to help us resolve the issue.
     
  12. Joachim_Ante

    Joachim_Ante

    Unity Technologies

    Joined:
    Mar 16, 2005
    Posts:
    5,203
    @GSdev: have you looked at the sample project i have provided and tried to apply that to your project?
     
  13. RogueCode

    RogueCode

    Joined:
    Apr 3, 2013
    Posts:
    230
    Awesome, thank you so much (yes, works now)! I hadn't even thought about ShaderStripping.

    @GSdev, fortunately because of our workflow that issue you're seeing doesn't matter for us. Sorry I can't be more help.
     
  14. GSdev

    GSdev

    Joined:
    May 8, 2015
    Posts:
    14
    @Joachim_Ante Yes we have, in fact it seems like we get the bright lighting result even when using your original project. Your project was much darker so the change is not as conspicuous, but because ours is already quite bright the end result is completely blown out. Attached is our project with yours applied to it as well as a photo of the issue. lightmap.jpg
     

    Attached Files:

  15. mweldon

    mweldon

    Joined:
    Apr 19, 2010
    Posts:
    109
    Are you maybe double-lighting your baked scene? Because when I bake light into a renderer I set that object to a new layer called "Lightmapped Objects", then set the Culling Mask on all my light sources in game to exclude this layer.
     
  16. mweldon

    mweldon

    Joined:
    Apr 19, 2010
    Posts:
    109
    The problem with this demo is that it only works with one prefab. If you create 2 different prefabs, bake their light separately, then try to load them both into the LightmapInstances scene, it doesn't work. The prefab serializes the GUIDs for the lightmaps, but not the data itself. As soon as you "ExtractLightmapData" on a second prefab, the data required for the first prefab gets overwritten.

    Unless I'm missing something, you have to copy the lightmap .exr textures somewhere after you bake them, then load them explicitly in your Awake function.

    And that will work fine until you try to put both the lightmaps and prefab into an asset bundle.
     
  17. ShuraK

    ShuraK

    Joined:
    Jan 15, 2015
    Posts:
    23
    Could anyone tell me How to get Terrain's lightmap offset&scale infomation ?

    I've checked the API of terrain class, there's only lightmap index, nothing about offset&scale.
     
  18. Joachim_Ante

    Joachim_Ante

    Unity Technologies

    Joined:
    Mar 16, 2005
    Posts:
    5,203
    Copying of the textures is only necessary if at a later point you use the same scene to bake new textures.

    The setup i would suggest for a real production workflow is to make a single scene with all the prefabs instantiated (placed in seperate locations so lighting doesnt bounce between prefabs)

    So you run baking once. Then you extract lightmap index / scale offset for all prefabs.

    Effectively updating all prefabs at once.

    Alternatively one seperately named scene per prefab could work too, but that leads to more textures and smaller atlasses. If you are always using all the prefabs together at runtime, then packing it together in a single scene is definately the way to go.
     
  19. Joachim_Ante

    Joachim_Ante

    Unity Technologies

    Joined:
    Mar 16, 2005
    Posts:
    5,203
    I have updated the sample project to demonstrate that it works with two objects. The approach of a single scene to bake all prefabs, works very well and the workflow is now so you just open the scene and click the "Assets -> Bake Prefab Lightmaps" menu item. This finds all scene objects that have a LightmapPrefabData script on it and uploads the baked lightmap information to the prefabs.
     
  20. Mimicry

    Mimicry

    Joined:
    Apr 8, 2015
    Posts:
    11
    @Joachim_Ante The script works fine in play mode but it is still missing lightmap uv aka lightmapScaleOffset on my iOS device (iPhone 5)
    Could you please have a look at it?
    p.s The general gi directional mode is "non directional". IMG_6028.PNG

    Update :
    If these objects was set inactive in the editor and then set active in runtime by some other script, all the lightmaps look normal. Are there some differences about awake mechanism on iOS device? Confused :/
     
    Last edited: Jul 6, 2015
  21. Mimicry

    Mimicry

    Joined:
    Apr 8, 2015
    Posts:
    11
    And a comparison between play mode and iOS device runtime (iPad Air 2)
    Screen Shot 2015-07-06 at 12.28.53 PM.png
     
  22. ShuraK

    ShuraK

    Joined:
    Jan 15, 2015
    Posts:
    23

    Thx for the demonstration, it shows how to store and assign lightmap index , scale & offset for renderers. But it doesn't work when we use terrain system. since terrain doensn't have a renderer on it nor lightmapScale&offset property.
     
  23. mweldon

    mweldon

    Joined:
    Apr 19, 2010
    Posts:
    109
    So you want me to put every asset in the entire game (potentially hundreds of prefabs) into one scene and generate a single huge lightmap for all of them? Because it has to be big to get good resolution for every object. Then when we build a scene at runtime, no matter how many or how few objects are in a scene, we still have to load this huge texture? And at any point in the future if we add another asset to the game we have to rebuild our mega-lightmap? I'm sorry, but that would not be practical at all.

    Each lightmap is unique to a prefab. There should be no need to create a mega-lightmap unless to work around a deficiency in the engine. The solution needs to be scaleable and this one isn't.
     
  24. tswalk

    tswalk

    Joined:
    Jul 27, 2013
    Posts:
    1,109

    I think that's an option not the holy grail. I went through the script he provided, and it is just a helper to show how you can do it with multiple prefabs. I think in your case (and as in mine)... we could simply use this helper, bake out our maps and get the properties applied (per the helper script)... rename the files and organize however we choose, and use the prefabs individually as needed. The script will load each as specified in the properties.
     
    Last edited: Jul 6, 2015
  25. Joachim_Ante

    Joachim_Ante

    Unity Technologies

    Joined:
    Mar 16, 2005
    Posts:
    5,203
    I wouldn't suggest putting all prefabs of a whole game into one bake, or rather depends on what you do exactly. I would however suggest grouping things. (So that lightmaps can be packed together into reasonable sizes at least 512*512)

    So you can definitely create multiple scenes. And the scripts as they are should support this perfectly fine.

    On the contrary I believe an approach where you have one lightmap per prefab is in fact the less scalable solution. At least from the perspective of GPU performance. But if for obscure reasons you really wanted that. Then you could do that too, with the scripts provided you could create one scene for each prefab and then use the bake menu item on each scene for each prefab. Alternatively if you really want to go down this path for reasons i dont understand, then you could also extend the scripts to make a workflow where these scenes are generated automatically, or the textures are copied out. Either way there is a lot of flexibility and naturally game production pipelines are usually very specific to the specific issue you want to solve so if the demo project doesn't work for your specific needs then just change it so it does.
     
  26. GSdev

    GSdev

    Joined:
    May 8, 2015
    Posts:
    14
    @mweldon You're right, the lighting is getting doubled, but is this intentional? In the past when lighting was on “baked" mode the light wouldn't have any effect on objects outside of the lightmap.
     
  27. Joachim_Ante

    Joachim_Ante

    Unity Technologies

    Joined:
    Mar 16, 2005
    Posts:
    5,203
    Since the lights are in a seperate scene. The same type of patching has to be applied if you want to create lights that are "baked" or "mixed mode" and should apply as they do when you bake a scene.

    You can use this API:
    Basically the lightmap mode has to be either baked or mixed mode.

    And then you have to call this property in Awake on the light:
    http://docs.unity3d.com/ScriptReference/Light-alreadyLightmapped.html


    In my demo project, my assumption was that there are no lights present in the instantiated scene, since all prefabs have the baked lightmaps applied. But of course there are many setups you can create with lighting, so the above API should get you going.
     
  28. Joachim_Ante

    Joachim_Ante

    Unity Technologies

    Joined:
    Mar 16, 2005
    Posts:
    5,203
    In regards to Terrain. lightmap offset & scale. The API is not exposed in the runtime. We will fix that in 5.2.
     
  29. tswalk

    tswalk

    Joined:
    Jul 27, 2013
    Posts:
    1,109
    Joachim, I have noticed that after baking if I switched a point light from baked to realtime, it no longer interacts with the scene. Once I clear the baked data, the light switched from baked to realtime will function normally after the bake.

    Is this intended?
     
  30. GSdev

    GSdev

    Joined:
    May 8, 2015
    Posts:
    14
    @Joachim_Ante That's it!!! Problem is finally resolved. Sorry about the misunderstanding. We really appreciate your help through all of this.:)
     
  31. trumpets

    trumpets

    Joined:
    Mar 1, 2013
    Posts:
    9
    How can we modify the code to work with the directional map as well?
     
  32. Joachim_Ante

    Joachim_Ante

    Unity Technologies

    Joined:
    Mar 16, 2005
    Posts:
    5,203
    You need to store both lightmaps in the struct that serializes it.
     
  33. trumpets

    trumpets

    Joined:
    Mar 1, 2013
    Posts:
    9
    I do just that, I have the lightmaps array of size 2 and store the normal map and the directional. When I hit the Extract Data options it removes the directional :(
     
  34. Joachim_Ante

    Joachim_Ante

    Unity Technologies

    Joined:
    Mar 16, 2005
    Posts:
    5,203
  35. INedelcu

    INedelcu

    Unity Technologies

    Joined:
    Jul 14, 2015
    Posts:
    169
    The cause of the issue with iOS/Android was static batching. We fixed this in the project by removing the static batching flag from the prefab and the instances in the scenes.
    Static batching happens when the player is built and pretransforms the lightmapping UVs with the scale and offset. Setting those on a Renderer doesn't have any effect when the Renderer is a part of a static batch. We added a warning for this case in the Player in 5.2 and updated the script to warn about it in the Editor.
     

    Attached Files:

    Last edited: Jul 17, 2015
    AngryMuppet and Mimicry like this.
  36. WillemKokke

    WillemKokke

    Joined:
    Sep 6, 2014
    Posts:
    31
    We have several environments we want to reuse in different scenes, so this seems like a good solution to only have to light them once (and only have to ship the light maps once)

    Am I right in assuming that this could work for light probes as well, by assigning LightmapSettings.lightProbes with information stored in a prefab? (assuming that we have one prefab per scene, as I don't see an easy way to merge light probe information)
     
  37. samuraijhonson

    samuraijhonson

    Joined:
    Jul 28, 2015
    Posts:
    10
    does this script only work at runtime? what if we want to see it in the editor?
     
  38. Joachim_Ante

    Joachim_Ante

    Unity Technologies

    Joined:
    Mar 16, 2005
    Posts:
    5,203
    You could also hook it up with [ExecuteInEditMode]
     
  39. samuraijhonson

    samuraijhonson

    Joined:
    Jul 28, 2015
    Posts:
    10
    is t
    how do I exactly do this.

    I noticed after experimentation that the script only works half of the time and sometimes you get weird black spots on the lightmap object in the scene, is that a know issue?
     
  40. WillemKokke

    WillemKokke

    Joined:
    Sep 6, 2014
    Posts:
    31
    Make sure that directional mode in lighting settings is the same in the scene used to build the prefabs and the scene where they are used.

    I've attached a modified version of the script that adds a menu option which applies the the prefab light maps to the current scene. I know there is an issue with it that if you remove a prefab from the scene it still references the light map. Once I add an option to clean that up / prevent it, I'll post an updated version here
     

    Attached Files:

    AngryMuppet likes this.
  41. samuraijhonson

    samuraijhonson

    Joined:
    Jul 28, 2015
    Posts:
    10
    I tied the script but it's not working. Here is what I am doing:

    - In Unity, "General GI/Directional Mode" is set to "directional".
    - Attach the script to an object and set the lightmap index to 3.
    - The object (egg shaped) is not being assigned to lightmap 3 (red lightmap).
    illu_1.jpg illu_2.jpg
     
  42. samuraijhonson

    samuraijhonson

    Joined:
    Jul 28, 2015
    Posts:
    10
  43. AngryMuppet

    AngryMuppet

    Joined:
    Mar 28, 2014
    Posts:
    34
    Soo... having issues with this, currently running Unity v5.1.2p2

    I've got a scene with my prefabs in that I'm baking. I'm using the script from above on the root object. I'm then going back into my game scene and instantiating the prefab. What I see when I instantiate a prefab with the light map script attached is this:

    upload_2015-8-3_23-7-44.png

    What I'm seeing in the light-mapped scene is this:
    upload_2015-8-3_23-9-1.png

    Building for Android - geometry was marked static batched but setting it non-static batched has the same result.

    Any ideas?
     
    Last edited: Aug 4, 2015
  44. AngryMuppet

    AngryMuppet

    Joined:
    Mar 28, 2014
    Posts:
    34
    After playing more - it appears to work fine if I create be version of the prefab in the same scene as I bake the lightmaps (when I apply the lightmaps to the new prefabs they all look fine.)

    My problem occurs if I create the prefab in a new scene (tried in both my existing game scene and a new empty scene.)

    If I copy everything across into the bake scene (deleting the prefabs I use for backing) and then apply the maps it all works fine.

    Anyone any idea why this is the case? Are there some setting that are required from the bake scene that need copying across into new scenes?
     
    JConcha and jmcorallo like this.
  45. AngryMuppet

    AngryMuppet

    Joined:
    Mar 28, 2014
    Posts:
    34
    And a final thing I've realized after head banging for a few days - if you're instantiating your prefabs in a separate scene to the one you're baking in, you need to manually assign the lightmap snapshot you're using before building.

    At least this is if building to Android - it works fine in Editor without this step for some reason!
     
    Lorrak, FardinHaque and jasonlu00 like this.
  46. AngryMuppet

    AngryMuppet

    Joined:
    Mar 28, 2014
    Posts:
    34
    Another potential pitfall I've found on Android, once I'd baked my prefabs, I switched shaders to 'Mobile/Unlit (supports lightmaps)' and found all my lightmaps fine in editor but screwed on Android phone. Switching back to Mobile/Diffuse sorted the problem!
     
  47. WagDan

    WagDan

    Joined:
    Nov 7, 2014
    Posts:
    37
    Sorry to hijack this thread.

    We are using lightmaps unwrapped and baked externally in 3ds Max+Vray. We have assigned them using the "SetLightmapsManually.cs" script successfully. When we press play or build the lightmap info is lost.

    Does anyone have any pointers on how to fix this?
     
  48. RyuMaster

    RyuMaster

    Joined:
    Sep 13, 2010
    Posts:
    468
    WagDan, I assign all the lightmaps at runtime, why do you assign them before you press 'play'?
     
  49. WagDan

    WagDan

    Joined:
    Nov 7, 2014
    Posts:
    37
    We used the "SetLightmapsManually.cs" script. This originally assigned them in the editor. We managed to edit it to work in the builds and at run time.
     
  50. febucci

    febucci

    Joined:
    Sep 3, 2015
    Posts:
    19
    I have this problem with the script. When I bake a prefab (without the script, it doesnt "instantiate" baked btw) i have 1 SetPass calls, even if I duplicate it couple of times (wich means an awesome optimization).
    If I run the scripts it works, so prefab is spawning baked, but there are 15 SetPass calls, and +15 per instantiate.
    Is a script problem or is just mine ? I have 1 light baked and "baked gi", prefab is static etc.
    How to fix it ?
     
Thread Status:
Not open for further replies.